1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <sal/config.h>
11 #include <test/bootstrapfixture.hxx>
15 #include <rangelst.hxx>
17 class Test
: public test::BootstrapFixture
25 virtual void setUp() override
;
26 virtual void tearDown() override
;
28 void testDeleteArea_4Ranges();
29 void testDeleteArea_3Ranges();
30 void testDeleteArea_3Ranges_Case2();
31 void testDeleteArea_3Ranges_Case3();
32 void testDeleteArea_3Ranges_Case4();
33 void testDeleteArea_3Ranges_Case5();
34 void testDeleteArea_2Ranges();
35 void testDeleteArea_2Ranges_Case2();
36 void testDeleteArea_2Ranges_Case3();
37 void testDeleteArea_2Ranges_Case4();
38 void testDeleteArea_2Ranges_Case5();
39 void testDeleteArea_2Ranges_Case6();
40 void testDeleteArea_2Ranges_Case7();
41 void testDeleteArea_2Ranges_Case8();
42 void testDeleteArea_1Range();
43 void testDeleteArea_0Ranges();
44 void testJoin_Case1();
45 void testJoin_Case2();
46 void testJoin_Case3();
47 void testJoin_Case4();
48 void testJoin_Case5();
49 void testGetIntersectedRange();
51 void testUpdateReference_DeleteRow();
52 void testUpdateReference_DeleteLastRow();
53 void testUpdateReference_DeleteCol();
58 CPPUNIT_TEST_SUITE(Test
);
59 CPPUNIT_TEST(testDeleteArea_4Ranges
);
60 CPPUNIT_TEST(testDeleteArea_3Ranges
);
61 CPPUNIT_TEST(testDeleteArea_3Ranges_Case2
);
62 CPPUNIT_TEST(testDeleteArea_3Ranges_Case3
);
63 CPPUNIT_TEST(testDeleteArea_3Ranges_Case4
);
64 CPPUNIT_TEST(testDeleteArea_3Ranges_Case5
);
65 CPPUNIT_TEST(testDeleteArea_2Ranges
);
66 CPPUNIT_TEST(testDeleteArea_2Ranges_Case2
);
67 CPPUNIT_TEST(testDeleteArea_2Ranges_Case3
);
68 CPPUNIT_TEST(testDeleteArea_2Ranges_Case4
);
69 CPPUNIT_TEST(testDeleteArea_2Ranges_Case5
);
70 CPPUNIT_TEST(testDeleteArea_2Ranges_Case6
);
71 CPPUNIT_TEST(testDeleteArea_2Ranges_Case7
);
72 CPPUNIT_TEST(testDeleteArea_2Ranges_Case8
);
73 CPPUNIT_TEST(testDeleteArea_1Range
);
74 CPPUNIT_TEST(testDeleteArea_0Ranges
);
75 CPPUNIT_TEST(testJoin_Case1
);
76 CPPUNIT_TEST(testJoin_Case2
);
77 CPPUNIT_TEST(testJoin_Case3
);
78 CPPUNIT_TEST(testJoin_Case4
);
79 CPPUNIT_TEST(testJoin_Case5
);
80 CPPUNIT_TEST(testUpdateReference_DeleteRow
);
81 CPPUNIT_TEST(testUpdateReference_DeleteLastRow
);
82 CPPUNIT_TEST(testUpdateReference_DeleteCol
);
83 CPPUNIT_TEST(testGetIntersectedRange
);
84 CPPUNIT_TEST(testInsertRow
);
85 CPPUNIT_TEST(testInsertCol
);
86 CPPUNIT_TEST_SUITE_END();
90 ScDocShellRef m_xDocShRef
;
95 BootstrapFixture::setUp();
98 m_xDocShRef
= new ScDocShell(
99 SfxModelFlags::EMBEDDED_OBJECT
|
100 SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS
|
101 SfxModelFlags::DISABLE_DOCUMENT_RECOVERY
);
103 m_pDoc
= &m_xDocShRef
->GetDocument();
106 void Test::tearDown()
109 BootstrapFixture::tearDown();
112 void Test::testDeleteArea_4Ranges()
114 ScRangeList
aList(ScRange(0,0,0,5,5,0));
115 aList
.DeleteArea(2,2,0,3,3,0);
117 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), aList
.size());
118 for(SCCOL nCol
= 0; nCol
<= 5; ++nCol
)
120 for(SCROW nRow
= 0; nRow
<= 5; ++nRow
)
122 if((nCol
== 2 || nCol
== 3) && ( nRow
== 2 || nRow
== 3))
123 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
125 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
130 void Test::testDeleteArea_3Ranges()
132 ScRangeList
aList(ScRange(1,1,0,6,6,0));
133 aList
.DeleteArea(3,3,0,8,4,0);
135 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
136 for(SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
138 for(SCROW nRow
= 1; nRow
<= 6; ++nRow
)
140 if((nRow
== 3 || nRow
== 4) && (nCol
>= 3))
141 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
143 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
147 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(28), aList
.GetCellCount());
150 void Test::testDeleteArea_3Ranges_Case2()
152 ScRangeList
aList(ScRange(1,1,0,6,6,0));
153 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
154 aList
.DeleteArea(0,2,0,2,4,0);
155 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
157 // Column 1-2 && Row 2-4 should not be in the range list. The rest should
159 for (SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
161 for (SCROW nRow
= 1; nRow
<= 6; ++nRow
)
163 if ((1 <= nCol
&& nCol
<= 2) && (2 <= nRow
&& nRow
<= 4))
164 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
166 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
171 void Test::testDeleteArea_3Ranges_Case3()
173 ScRangeList
aList(ScRange(1,5,0,6,11,0));
174 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
175 aList
.DeleteArea(3,2,0,4,8,0);
176 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
178 // Column 3-4 && Row 5-8 should not be in the range list.
179 for (SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
181 for (SCROW nRow
= 5; nRow
<= 11; ++nRow
)
183 if ((3 <= nCol
&& nCol
<= 4) && (5 <= nRow
&& nRow
<= 8))
184 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
186 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
191 void Test::testDeleteArea_3Ranges_Case4()
193 ScRangeList
aList(ScRange(1,5,0,6,11,0));
194 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
195 aList
.DeleteArea(3,5,0,4,5,0);
196 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
198 // Column 3-4 && Row 5 should not be in the range list.
199 for (SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
201 for (SCROW nRow
= 5; nRow
<= 11; ++nRow
)
203 if ((3 <= nCol
&& nCol
<= 4) && 5 == nRow
)
204 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
206 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
211 void Test::testDeleteArea_3Ranges_Case5()
213 ScRangeList
aList(ScRange(1,5,0,6,11,0));
214 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
215 aList
.DeleteArea(6,7,0,6,9,0);
216 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
218 // Column 6 && Row 7-9 should not be in the range list.
219 for (SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
221 for (SCROW nRow
= 5; nRow
<= 11; ++nRow
)
223 if ( nCol
== 6 && (7 <= nRow
&& nRow
<= 9))
224 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
226 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
231 void Test::testDeleteArea_2Ranges()
233 ScRangeList
aList(ScRange(0,0,0,5,5,5));
234 ScRangeList
aList2(aList
);
236 aList
.DeleteArea(4,4,0,6,7,0);
237 aList2
.DeleteArea(4,4,0,6,7,0);
238 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
239 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList2
.size());
241 for(SCCOL nCol
= 0; nCol
<= 5; ++nCol
)
243 for(SCROW nRow
= 0; nRow
<= 5; ++nRow
)
245 if(nCol
>=4 && nRow
>= 4)
246 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
248 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
253 void Test::testDeleteArea_2Ranges_Case2()
255 ScRangeList
aList(ScRange(1,1,0,1,5,0));
256 aList
.DeleteArea(0,3,0,MAXCOL
,3,0);
258 for(SCROW nRow
= 1; nRow
<= 5; ++nRow
)
261 CPPUNIT_ASSERT(!aList
.In(ScRange(1,3,0)));
263 CPPUNIT_ASSERT(aList
.In(ScRange(1,nRow
,0)));
265 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), aList
.GetCellCount());
268 void Test::testDeleteArea_2Ranges_Case3()
270 ScRangeList
aList(ScRange(0,5,0,2,10,0));
271 aList
.DeleteArea(2,3,0,3,7,0);
272 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
274 // Column 2 Row 5-7 shouldn't be in the list.
275 for (SCCOL nCol
= 0; nCol
<= 2; ++nCol
)
277 for (SCROW nRow
= 5; nRow
<= 10; ++nRow
)
279 if (nCol
== 2 && (5 <= nRow
&& nRow
<= 7))
280 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
,nRow
,0)));
282 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
,nRow
,0)));
287 void Test::testDeleteArea_2Ranges_Case4()
289 ScRangeList
aList(ScRange(2,3,0,4,7,0));
290 aList
.DeleteArea(0,1,0,2,5,0);
291 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
293 // Column 2 Row 3-5 shouldn't be in the list.
294 for (SCCOL nCol
= 2; nCol
<= 4; ++nCol
)
296 for (SCROW nRow
= 3; nRow
<= 7; ++nRow
)
298 if (nCol
== 2 && (3 <= nRow
&& nRow
<= 5))
299 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
,nRow
,0)));
301 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
,nRow
,0)));
306 void Test::testDeleteArea_2Ranges_Case5()
308 ScRangeList
aList(ScRange(2,2,0,5,5,0));
309 aList
.DeleteArea(4,5,0,5,5,0);
310 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
312 // Column 4 and 5 Row 5 shouldn't be in the list.
313 for(SCCOL nCol
= 2; nCol
<= 5; ++nCol
)
315 for(SCROW nRow
= 2; nRow
<= 5; ++nRow
)
317 if(nRow
== 5 && 4 <= nCol
)
318 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
320 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
325 void Test::testDeleteArea_2Ranges_Case6()
327 ScRangeList
aList(ScRange(2,2,0,5,5,0));
328 aList
.DeleteArea(4,2,0,5,2,0);
329 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
331 // Column 4 and 5 Row 2 shouldn't be in the list.
332 for(SCCOL nCol
= 2; nCol
<= 5; ++nCol
)
334 for(SCROW nRow
= 2; nRow
<= 5; ++nRow
)
336 if(nRow
== 2 && 4 <= nCol
)
337 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
339 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
344 void Test::testDeleteArea_2Ranges_Case7()
346 ScRangeList
aList(ScRange(2,2,0,5,5,0));
347 aList
.DeleteArea(2,5,0,2,5,0);
348 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
350 // Column 2 Row 5 shouldn't be in the list.
351 for(SCCOL nCol
= 2; nCol
<= 5; ++nCol
)
353 for(SCROW nRow
= 2; nRow
<= 5; ++nRow
)
355 if(nRow
== 5 && nCol
== 2)
356 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
358 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
363 void Test::testDeleteArea_2Ranges_Case8()
365 ScRangeList
aList(ScRange(2,2,0,5,5,0));
366 aList
.DeleteArea(2,2,0,3,2,0);
367 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
369 // Column 2 & 3 Row 2 shouldn't be in the list.
370 for(SCCOL nCol
= 2; nCol
<= 5; ++nCol
)
372 for(SCROW nRow
= 2; nRow
<= 5; ++nRow
)
374 if(nRow
== 2 && nCol
<= 3)
375 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
377 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
382 void Test::testDeleteArea_1Range()
384 ScRangeList
aList(ScRange(1,1,0,3,3,0));
385 aList
.DeleteArea(1,1,0,2,3,0);
387 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
389 for(SCROW nRow
= 1; nRow
<= 3; ++nRow
)
391 CPPUNIT_ASSERT(aList
.In(ScRange(3,nRow
,0)));
393 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.GetCellCount());
396 void Test::testDeleteArea_0Ranges()
398 ScRangeList
aList(ScRange(1,1,0,3,3,0));
399 aList
.DeleteArea(1,1,0,3,3,0);
401 CPPUNIT_ASSERT(aList
.empty());
403 ScRangeList
aList2(ScRange(1,1,0,3,3,0));
404 aList2
.DeleteArea(0,0,0,4,4,0);
406 CPPUNIT_ASSERT(aList
.empty());
409 void Test::testJoin_Case1()
412 aList
.push_back(ScRange(1,1,0,3,3,0));
413 aList
.Join(ScRange(4,1,0,6,3,0));
415 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
416 CPPUNIT_ASSERT_EQUAL( ScRange(1,1,0,6,3,0), aList
[0]);
419 void Test::testJoin_Case2()
422 aList
.push_back(ScRange(1,1,0,3,3,0));
423 aList
.push_back(ScRange(4,1,0,6,3,0));
424 aList
.push_back(ScRange(7,1,0,9,3,0));
426 aList
.Join(aList
[2], true);
428 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
429 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,9,3,0), aList
[0]);
432 void Test::testJoin_Case3()
435 aList
.Join(ScRange(1,1,0,6,6,0));
436 aList
.Join(ScRange(3,3,0,4,4,0));
438 // The second one should have been swallowed by the first one
439 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
440 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,6,6,0), aList
[0]);
442 // Add a disjoint one
443 aList
.Join(ScRange(8,8,0,9,9,0));
445 // Should be two ones now
446 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
447 // The first one should still be as is
448 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,6,6,0), aList
[0]);
449 // Ditto for the second one
450 CPPUNIT_ASSERT_EQUAL(ScRange(8,8,0,9,9,0), aList
[1]);
453 void Test::testJoin_Case4()
456 aList
.Join(ScRange(1,1,0,2,6,0));
457 // Join a range that overlaps it and extends it vertically
458 aList
.Join(ScRange(1,4,0,2,8,0));
460 // The one range in the list should have been extended
461 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
462 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,2,8,0), aList
[0]);
464 // Join a range that overlaps it and extends it horizontally
465 aList
.Join(ScRange(2,1,0,4,8,0));
467 // Again, should have just been extended
468 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
469 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,4,8,0), aList
[0]);
471 // And then the same but on top / to the left of existing range
473 aList2
.Join(ScRange(4,4,0,8,8,0));
474 aList2
.Join(ScRange(4,1,0,8,6,0));
476 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList2
.size());
477 CPPUNIT_ASSERT_EQUAL(ScRange(4,1,0,8,8,0), aList2
[0]);
479 aList2
.Join(ScRange(1,1,0,6,8,0));
481 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList2
.size());
482 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,8,8,0), aList2
[0]);
485 void Test::testJoin_Case5()
488 aList
.Join(ScRange(0,0,0,4,4,0));
489 aList
.Join(ScRange(8,0,0,10,4,0));
491 // Nothing special so far, two disjoint ranges
492 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
493 CPPUNIT_ASSERT_EQUAL(ScRange(0,0,0,4,4,0), aList
[0]);
494 CPPUNIT_ASSERT_EQUAL(ScRange(8,0,0,10,4,0), aList
[1]);
496 // This should join the two ranges into one
497 aList
.Join(ScRange(5,0,0,9,4,0));
499 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
500 CPPUNIT_ASSERT_EQUAL(ScRange(0,0,0,10,4,0), aList
[0]);
503 void Test::testUpdateReference_DeleteRow()
505 ScRangeList
aList(ScRange(1,1,0,4,4,0));
506 bool bUpdated
= aList
.UpdateReference(URM_INSDEL
, m_pDoc
, ScRange(0,3,0,MAXCOL
,MAXROW
,0), 0, -1, 0);
507 CPPUNIT_ASSERT(bUpdated
);
509 for(SCCOL nCol
= 1; nCol
<= 4; ++nCol
)
511 for(SCROW nRow
= 1; nRow
<= 3; ++nRow
)
513 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
515 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, 4, 0)));
517 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(12), aList
.GetCellCount());
519 ScRangeList
aList2(ScRange(2,2,0,2,2,0));
520 aList2
.UpdateReference(URM_INSDEL
, m_pDoc
, ScRange(0,3,0,MAXCOL
,MAXROW
,0), 0, -1, 0);
521 CPPUNIT_ASSERT(aList2
.empty());
524 void Test::testUpdateReference_DeleteLastRow()
526 ScRangeList
aList(ScRange(1,1,0,4,4,0));
527 bool bUpdated
= aList
.UpdateReference(URM_INSDEL
, m_pDoc
, ScRange(0,4,0,MAXCOL
,4,0), 0, -1, 0);
528 CPPUNIT_ASSERT(bUpdated
);
531 void Test::testUpdateReference_DeleteCol()
533 ScRangeList
aList(ScRange(1,1,0,4,4,0));
534 bool bUpdated
= aList
.UpdateReference(URM_INSDEL
, m_pDoc
, ScRange(3,0,0,MAXCOL
,MAXROW
,0), -1, 0, 0);
535 CPPUNIT_ASSERT(bUpdated
);
537 for(SCROW nRow
= 1; nRow
<= 4; ++nRow
)
539 for(SCCOL nCol
= 1; nCol
<= 3; ++nCol
)
541 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
543 CPPUNIT_ASSERT(!aList
.In(ScRange(4, nRow
, 0)));
545 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(12), aList
.GetCellCount());
548 void Test::testGetIntersectedRange()
550 ScRangeList
aList(ScRange(2, 2, 0, 5, 5, 0));
551 ScRangeList aIntersecting
= aList
.GetIntersectedRange(ScRange(0, 0, 0, 3, 3, 0));
552 CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(2,2,0,3,3,0)), aIntersecting
);
555 void Test::testInsertRow()
557 ScRangeList
aList(ScRange(1,1,0,4,4,0));
558 aList
.InsertRow(0, 0, MAXCOL
, 5, 2);
559 CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(1,1,0,4,6,0)), aList
);
562 void Test::testInsertCol()
564 ScRangeList
aList(ScRange(1,1,0,4,4,0));
565 aList
.InsertCol(0, 0, MAXROW
, 5, 2);
566 CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(1,1,0,6,4,0)), aList
);
569 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
571 CPPUNIT_PLUGIN_IMPLEMENT();
572 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */