Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #include <sal/config.h>
11 : #include <test/bootstrapfixture.hxx>
12 : #include "helper/qahelper.hxx"
13 : #include "document.hxx"
14 : #include "docsh.hxx"
15 :
16 : #include "rangelst.hxx"
17 :
18 46 : class Test : public test::BootstrapFixture
19 : {
20 : public:
21 23 : Test()
22 23 : : m_pDoc(NULL)
23 : {
24 23 : }
25 :
26 : virtual void setUp() SAL_OVERRIDE;
27 : virtual void tearDown() SAL_OVERRIDE;
28 :
29 : void testDeleteArea_4Ranges();
30 : void testDeleteArea_3Ranges();
31 : void testDeleteArea_3Ranges_Case2();
32 : void testDeleteArea_3Ranges_Case3();
33 : void testDeleteArea_3Ranges_Case4();
34 : void testDeleteArea_3Ranges_Case5();
35 : void testDeleteArea_2Ranges();
36 : void testDeleteArea_2Ranges_Case2();
37 : void testDeleteArea_2Ranges_Case3();
38 : void testDeleteArea_2Ranges_Case4();
39 : void testDeleteArea_2Ranges_Case5();
40 : void testDeleteArea_2Ranges_Case6();
41 : void testDeleteArea_2Ranges_Case7();
42 : void testDeleteArea_2Ranges_Case8();
43 : void testDeleteArea_1Range();
44 : void testDeleteArea_0Ranges();
45 : void testJoin_Case1();
46 : void testJoin_Case2();
47 : void testGetIntersectedRange();
48 :
49 : void testUpdateReference_DeleteRow();
50 : void testUpdateReference_DeleteCol();
51 :
52 : void testInsertRow();
53 : void testInsertCol();
54 :
55 2 : CPPUNIT_TEST_SUITE(Test);
56 1 : CPPUNIT_TEST(testDeleteArea_4Ranges);
57 1 : CPPUNIT_TEST(testDeleteArea_3Ranges);
58 1 : CPPUNIT_TEST(testDeleteArea_3Ranges_Case2);
59 1 : CPPUNIT_TEST(testDeleteArea_3Ranges_Case3);
60 1 : CPPUNIT_TEST(testDeleteArea_3Ranges_Case4);
61 1 : CPPUNIT_TEST(testDeleteArea_3Ranges_Case5);
62 1 : CPPUNIT_TEST(testDeleteArea_2Ranges);
63 1 : CPPUNIT_TEST(testDeleteArea_2Ranges_Case2);
64 1 : CPPUNIT_TEST(testDeleteArea_2Ranges_Case3);
65 1 : CPPUNIT_TEST(testDeleteArea_2Ranges_Case4);
66 1 : CPPUNIT_TEST(testDeleteArea_2Ranges_Case5);
67 1 : CPPUNIT_TEST(testDeleteArea_2Ranges_Case6);
68 1 : CPPUNIT_TEST(testDeleteArea_2Ranges_Case7);
69 1 : CPPUNIT_TEST(testDeleteArea_2Ranges_Case8);
70 1 : CPPUNIT_TEST(testDeleteArea_1Range);
71 1 : CPPUNIT_TEST(testDeleteArea_0Ranges);
72 1 : CPPUNIT_TEST(testJoin_Case1);
73 1 : CPPUNIT_TEST(testJoin_Case2);
74 1 : CPPUNIT_TEST(testUpdateReference_DeleteRow);
75 1 : CPPUNIT_TEST(testUpdateReference_DeleteCol);
76 1 : CPPUNIT_TEST(testGetIntersectedRange);
77 1 : CPPUNIT_TEST(testInsertRow);
78 1 : CPPUNIT_TEST(testInsertCol);
79 5 : CPPUNIT_TEST_SUITE_END();
80 :
81 : private:
82 : ScDocument *m_pDoc;
83 : ScDocShellRef m_xDocShRef;
84 : };
85 :
86 23 : void Test::setUp()
87 : {
88 23 : BootstrapFixture::setUp();
89 :
90 23 : ScDLL::Init();
91 69 : m_xDocShRef = new ScDocShell(
92 : SfxModelFlags::EMBEDDED_OBJECT |
93 46 : SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS |
94 69 : SfxModelFlags::DISABLE_DOCUMENT_RECOVERY);
95 :
96 23 : m_pDoc = &m_xDocShRef->GetDocument();
97 23 : }
98 :
99 23 : void Test::tearDown()
100 : {
101 23 : m_xDocShRef.Clear();
102 23 : BootstrapFixture::tearDown();
103 23 : }
104 :
105 1 : void Test::testDeleteArea_4Ranges()
106 : {
107 1 : ScRangeList aList(ScRange(0,0,0,5,5,0));
108 1 : aList.DeleteArea(2,2,0,3,3,0);
109 :
110 1 : CPPUNIT_ASSERT_EQUAL(aList.size(), static_cast<size_t>(4));
111 7 : for(SCCOL nCol = 0; nCol <= 5; ++nCol)
112 : {
113 42 : for(SCROW nRow = 0; nRow <= 5; ++nRow)
114 : {
115 36 : if((nCol == 2 || nCol == 3) && ( nRow == 2 || nRow == 3))
116 4 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
117 : else
118 32 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
119 : }
120 1 : }
121 1 : }
122 :
123 1 : void Test::testDeleteArea_3Ranges()
124 : {
125 1 : ScRangeList aList(ScRange(1,1,0,6,6,0));
126 1 : aList.DeleteArea(3,3,0,8,4,0);
127 :
128 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
129 7 : for(SCCOL nCol = 1; nCol <= 6; ++nCol)
130 : {
131 42 : for(SCROW nRow = 1; nRow <= 6; ++nRow)
132 : {
133 36 : if((nRow == 3 || nRow == 4) && (nCol >= 3))
134 8 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
135 : else
136 28 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
137 : }
138 : }
139 :
140 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(28), aList.GetCellCount());
141 1 : }
142 :
143 1 : void Test::testDeleteArea_3Ranges_Case2()
144 : {
145 1 : ScRangeList aList(ScRange(1,1,0,6,6,0));
146 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
147 1 : aList.DeleteArea(0,2,0,2,4,0);
148 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
149 :
150 : // Column 1-2 && Row 2-4 should not be in the range list. The rest should
151 : // be in the list.
152 7 : for (SCCOL nCol = 1; nCol <= 6; ++nCol)
153 : {
154 42 : for (SCROW nRow = 1; nRow <= 6; ++nRow)
155 : {
156 36 : if ((1 <= nCol && nCol <= 2) && (2 <= nRow && nRow <= 4))
157 6 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
158 : else
159 30 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
160 : }
161 1 : }
162 1 : }
163 :
164 1 : void Test::testDeleteArea_3Ranges_Case3()
165 : {
166 1 : ScRangeList aList(ScRange(1,5,0,6,11,0));
167 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
168 1 : aList.DeleteArea(3,2,0,4,8,0);
169 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
170 :
171 : // Column 3-4 && Row 5-8 should not be in the range list.
172 7 : for (SCCOL nCol = 1; nCol <= 6; ++nCol)
173 : {
174 48 : for (SCROW nRow = 5; nRow <= 11; ++nRow)
175 : {
176 42 : if ((3 <= nCol && nCol <= 4) && (5 <= nRow && nRow <= 8))
177 8 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
178 : else
179 34 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
180 : }
181 1 : }
182 1 : }
183 :
184 1 : void Test::testDeleteArea_3Ranges_Case4()
185 : {
186 1 : ScRangeList aList(ScRange(1,5,0,6,11,0));
187 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
188 1 : aList.DeleteArea(3,5,0,4,5,0);
189 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
190 :
191 : // Column 3-4 && Row 5 should not be in the range list.
192 7 : for (SCCOL nCol = 1; nCol <= 6; ++nCol)
193 : {
194 48 : for (SCROW nRow = 5; nRow <= 11; ++nRow)
195 : {
196 42 : if ((3 <= nCol && nCol <= 4) && 5 == nRow )
197 2 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
198 : else
199 40 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
200 : }
201 1 : }
202 1 : }
203 :
204 1 : void Test::testDeleteArea_3Ranges_Case5()
205 : {
206 1 : ScRangeList aList(ScRange(1,5,0,6,11,0));
207 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
208 1 : aList.DeleteArea(6,7,0,6,9,0);
209 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
210 :
211 : // Column 6 && Row 7-9 should not be in the range list.
212 7 : for (SCCOL nCol = 1; nCol <= 6; ++nCol)
213 : {
214 48 : for (SCROW nRow = 5; nRow <= 11; ++nRow)
215 : {
216 42 : if ( nCol == 6 && (7 <= nRow && nRow <= 9))
217 3 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
218 : else
219 39 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
220 : }
221 1 : }
222 1 : }
223 :
224 1 : void Test::testDeleteArea_2Ranges()
225 : {
226 1 : ScRangeList aList(ScRange(0,0,0,5,5,5));
227 2 : ScRangeList aList2(aList);
228 :
229 1 : aList.DeleteArea(4,4,0,6,7,0);
230 1 : aList2.DeleteArea(4,4,0,6,7,0);
231 1 : CPPUNIT_ASSERT_EQUAL(aList.size(), static_cast<size_t>(2));
232 1 : CPPUNIT_ASSERT_EQUAL(aList2.size(), static_cast<size_t>(2));
233 :
234 7 : for(SCCOL nCol = 0; nCol <= 5; ++nCol)
235 : {
236 42 : for(SCROW nRow = 0; nRow <= 5; ++nRow)
237 : {
238 36 : if(nCol>=4 && nRow >= 4)
239 4 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
240 : else
241 32 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
242 : }
243 1 : }
244 1 : }
245 :
246 1 : void Test::testDeleteArea_2Ranges_Case2()
247 : {
248 1 : ScRangeList aList(ScRange(1,1,0,1,5,0));
249 1 : aList.DeleteArea(0,3,0,MAXCOL,3,0);
250 :
251 6 : for(SCROW nRow = 1; nRow <= 5; ++nRow)
252 : {
253 5 : if(nRow == 3)
254 1 : CPPUNIT_ASSERT(!aList.In(ScRange(1,3,0)));
255 : else
256 4 : CPPUNIT_ASSERT(aList.In(ScRange(1,nRow,0)));
257 : }
258 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), aList.GetCellCount());
259 1 : }
260 :
261 1 : void Test::testDeleteArea_2Ranges_Case3()
262 : {
263 1 : ScRangeList aList(ScRange(0,5,0,2,10,0));
264 1 : aList.DeleteArea(2,3,0,3,7,0);
265 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
266 :
267 : // Column 2 Row 5-7 shouldn't be in the list.
268 4 : for (SCCOL nCol = 0; nCol <= 2; ++nCol)
269 : {
270 21 : for (SCROW nRow = 5; nRow <= 10; ++nRow)
271 : {
272 18 : if (nCol == 2 && (5 <= nRow && nRow <= 7))
273 3 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol,nRow,0)));
274 : else
275 15 : CPPUNIT_ASSERT(aList.In(ScRange(nCol,nRow,0)));
276 : }
277 1 : }
278 1 : }
279 :
280 1 : void Test::testDeleteArea_2Ranges_Case4()
281 : {
282 1 : ScRangeList aList(ScRange(2,3,0,4,7,0));
283 1 : aList.DeleteArea(0,1,0,2,5,0);
284 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
285 :
286 : // Column 2 Row 3-5 shouldn't be in the list.
287 4 : for (SCCOL nCol = 2; nCol <= 4; ++nCol)
288 : {
289 18 : for (SCROW nRow = 3; nRow <= 7; ++nRow)
290 : {
291 15 : if (nCol == 2 && (3 <= nRow && nRow <= 5))
292 3 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol,nRow,0)));
293 : else
294 12 : CPPUNIT_ASSERT(aList.In(ScRange(nCol,nRow,0)));
295 : }
296 1 : }
297 1 : }
298 :
299 1 : void Test::testDeleteArea_2Ranges_Case5()
300 : {
301 1 : ScRangeList aList(ScRange(2,2,0,5,5,0));
302 1 : aList.DeleteArea(4,5,0,5,5,0);
303 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
304 :
305 : // Column 4 and 5 Row 5 shouldn't be in the list.
306 5 : for(SCCOL nCol = 2; nCol <= 5; ++nCol)
307 : {
308 20 : for(SCROW nRow = 2; nRow <= 5; ++nRow)
309 : {
310 16 : if(nRow == 5 && 4 <= nCol)
311 2 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
312 : else
313 14 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
314 : }
315 1 : }
316 1 : }
317 :
318 1 : void Test::testDeleteArea_2Ranges_Case6()
319 : {
320 1 : ScRangeList aList(ScRange(2,2,0,5,5,0));
321 1 : aList.DeleteArea(4,2,0,5,2,0);
322 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
323 :
324 : // Column 4 and 5 Row 2 shouldn't be in the list.
325 5 : for(SCCOL nCol = 2; nCol <= 5; ++nCol)
326 : {
327 20 : for(SCROW nRow = 2; nRow <= 5; ++nRow)
328 : {
329 16 : if(nRow == 2 && 4 <= nCol)
330 2 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
331 : else
332 14 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
333 : }
334 1 : }
335 1 : }
336 :
337 1 : void Test::testDeleteArea_2Ranges_Case7()
338 : {
339 1 : ScRangeList aList(ScRange(2,2,0,5,5,0));
340 1 : aList.DeleteArea(2,5,0,2,5,0);
341 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
342 :
343 : // Column 2 Row 5 shouldn't be in the list.
344 5 : for(SCCOL nCol = 2; nCol <= 5; ++nCol)
345 : {
346 20 : for(SCROW nRow = 2; nRow <= 5; ++nRow)
347 : {
348 16 : if(nRow == 5 && nCol == 2)
349 1 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
350 : else
351 15 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
352 : }
353 1 : }
354 1 : }
355 :
356 1 : void Test::testDeleteArea_2Ranges_Case8()
357 : {
358 1 : ScRangeList aList(ScRange(2,2,0,5,5,0));
359 1 : aList.DeleteArea(2,2,0,3,2,0);
360 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
361 :
362 : // Column 2 & 3 Row 2 shouldn't be in the list.
363 5 : for(SCCOL nCol = 2; nCol <= 5; ++nCol)
364 : {
365 20 : for(SCROW nRow = 2; nRow <= 5; ++nRow)
366 : {
367 16 : if(nRow == 2 && nCol <= 3)
368 2 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, nRow, 0)));
369 : else
370 14 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
371 : }
372 1 : }
373 1 : }
374 :
375 1 : void Test::testDeleteArea_1Range()
376 : {
377 1 : ScRangeList aList(ScRange(1,1,0,3,3,0));
378 1 : aList.DeleteArea(1,1,0,2,3,0);
379 :
380 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
381 :
382 4 : for(SCROW nRow = 1; nRow <= 3; ++nRow)
383 : {
384 3 : CPPUNIT_ASSERT(aList.In(ScRange(3,nRow,0)));
385 : }
386 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.GetCellCount());
387 1 : }
388 :
389 1 : void Test::testDeleteArea_0Ranges()
390 : {
391 1 : ScRangeList aList(ScRange(1,1,0,3,3,0));
392 1 : aList.DeleteArea(1,1,0,3,3,0);
393 :
394 1 : CPPUNIT_ASSERT(aList.empty());
395 :
396 2 : ScRangeList aList2(ScRange(1,1,0,3,3,0));
397 1 : aList2.DeleteArea(0,0,0,4,4,0);
398 :
399 2 : CPPUNIT_ASSERT(aList.empty());
400 1 : }
401 :
402 1 : void Test::testJoin_Case1()
403 : {
404 1 : ScRangeList aList;
405 1 : aList.push_back(new ScRange(1,1,0,3,3,0));
406 1 : aList.Join(ScRange(4,1,0,6,3,0));
407 :
408 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
409 1 : CPPUNIT_ASSERT( ScRange(1,1,0,6,3,0) == *aList[0]);
410 1 : }
411 :
412 1 : void Test::testJoin_Case2()
413 : {
414 1 : ScRangeList aList;
415 1 : aList.push_back(new ScRange(1,1,0,3,3,0));
416 1 : aList.push_back(new ScRange(4,1,0,6,3,0));
417 1 : aList.push_back(new ScRange(7,1,0,9,3,0));
418 :
419 1 : aList.Join(*aList[2], true);
420 :
421 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
422 1 : CPPUNIT_ASSERT(ScRange(1,1,0,9,3,0) == *aList[0]);
423 1 : }
424 :
425 1 : void Test::testUpdateReference_DeleteRow()
426 : {
427 1 : ScRangeList aList(ScRange(1,1,0,4,4,0));
428 1 : bool bUpdated = aList.UpdateReference(URM_INSDEL, m_pDoc, ScRange(0,3,0,MAXCOL,MAXROW,0), 0, -1, 0);
429 1 : CPPUNIT_ASSERT(bUpdated);
430 :
431 5 : for(SCCOL nCol = 1; nCol <= 4; ++nCol)
432 : {
433 16 : for(SCROW nRow = 1; nRow <= 3; ++nRow)
434 : {
435 12 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
436 : }
437 4 : CPPUNIT_ASSERT(!aList.In(ScRange(nCol, 4, 0)));
438 : }
439 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(12), aList.GetCellCount());
440 :
441 2 : ScRangeList aList2(ScRange(2,2,0,2,2,0));
442 1 : aList2.UpdateReference(URM_INSDEL, m_pDoc, ScRange(0,3,0,MAXCOL,MAXROW,0), 0, -1, 0);
443 2 : CPPUNIT_ASSERT(aList2.empty());
444 1 : }
445 :
446 1 : void Test::testUpdateReference_DeleteCol()
447 : {
448 1 : ScRangeList aList(ScRange(1,1,0,4,4,0));
449 1 : bool bUpdated = aList.UpdateReference(URM_INSDEL, m_pDoc, ScRange(3,0,0,MAXCOL,MAXROW,0), -1, 0, 0);
450 1 : CPPUNIT_ASSERT(bUpdated);
451 :
452 5 : for(SCROW nRow = 1; nRow <= 4; ++nRow)
453 : {
454 16 : for(SCCOL nCol = 1; nCol <= 3; ++nCol)
455 : {
456 12 : CPPUNIT_ASSERT(aList.In(ScRange(nCol, nRow, 0)));
457 : }
458 4 : CPPUNIT_ASSERT(!aList.In(ScRange(4, nRow, 0)));
459 : }
460 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(12), aList.GetCellCount());
461 1 : }
462 :
463 1 : void Test::testGetIntersectedRange()
464 : {
465 1 : ScRangeList aList(ScRange(2, 2, 0, 5, 5, 0));
466 2 : ScRangeList aIntersecting = aList.GetIntersectedRange(ScRange(0, 0, 0, 3, 3, 0));
467 2 : CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(2,2,0,3,3,0)), aIntersecting);
468 1 : }
469 :
470 1 : void Test::testInsertRow()
471 : {
472 1 : ScRangeList aList(ScRange(1,1,0,4,4,0));
473 1 : aList.InsertRow(0, 0, MAXCOL, 5, 2);
474 1 : CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(1,1,0,4,6,0)), aList);
475 1 : }
476 :
477 1 : void Test::testInsertCol()
478 : {
479 1 : ScRangeList aList(ScRange(1,1,0,4,4,0));
480 1 : aList.InsertCol(0, 0, MAXROW, 5, 2);
481 1 : CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(1,1,0,6,4,0)), aList);
482 1 : }
483 :
484 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
485 :
486 4 : CPPUNIT_PLUGIN_IMPLEMENT();
487 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|