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>
12 #include "document.hxx"
15 #include "rangelst.hxx"
17 class Test
: public test::BootstrapFixture
{
21 virtual void tearDown();
23 void testDeleteArea_4Ranges();
24 void testDeleteArea_3Ranges();
25 void testDeleteArea_3Ranges_Case2();
26 void testDeleteArea_3Ranges_Case3();
27 void testDeleteArea_3Ranges_Case4();
28 void testDeleteArea_3Ranges_Case5();
29 void testDeleteArea_2Ranges();
30 void testDeleteArea_2Ranges_Case2();
31 void testDeleteArea_2Ranges_Case3();
32 void testDeleteArea_2Ranges_Case4();
33 void testDeleteArea_2Ranges_Case5();
34 void testDeleteArea_2Ranges_Case6();
35 void testDeleteArea_2Ranges_Case7();
36 void testDeleteArea_2Ranges_Case8();
37 void testDeleteArea_1Range();
38 void testDeleteArea_0Ranges();
39 void testJoin_Case1();
40 void testJoin_Case2();
41 void testGetIntersectedRange();
43 void testUpdateReference_DeleteRow();
44 void testUpdateReference_DeleteCol();
46 CPPUNIT_TEST_SUITE(Test
);
47 CPPUNIT_TEST(testDeleteArea_4Ranges
);
48 CPPUNIT_TEST(testDeleteArea_3Ranges
);
49 CPPUNIT_TEST(testDeleteArea_3Ranges_Case2
);
50 CPPUNIT_TEST(testDeleteArea_3Ranges_Case3
);
51 CPPUNIT_TEST(testDeleteArea_3Ranges_Case4
);
52 CPPUNIT_TEST(testDeleteArea_3Ranges_Case5
);
53 CPPUNIT_TEST(testDeleteArea_2Ranges
);
54 CPPUNIT_TEST(testDeleteArea_2Ranges_Case2
);
55 CPPUNIT_TEST(testDeleteArea_2Ranges_Case3
);
56 CPPUNIT_TEST(testDeleteArea_2Ranges_Case4
);
57 CPPUNIT_TEST(testDeleteArea_2Ranges_Case5
);
58 CPPUNIT_TEST(testDeleteArea_2Ranges_Case6
);
59 CPPUNIT_TEST(testDeleteArea_2Ranges_Case7
);
60 CPPUNIT_TEST(testDeleteArea_2Ranges_Case8
);
61 CPPUNIT_TEST(testDeleteArea_1Range
);
62 CPPUNIT_TEST(testDeleteArea_0Ranges
);
63 CPPUNIT_TEST(testJoin_Case1
);
64 CPPUNIT_TEST(testJoin_Case2
);
65 CPPUNIT_TEST(testUpdateReference_DeleteRow
);
66 CPPUNIT_TEST(testUpdateReference_DeleteCol
);
67 CPPUNIT_TEST(testGetIntersectedRange
);
68 CPPUNIT_TEST_SUITE_END();
73 ScDocShellRef m_xDocShRef
;
76 static std::ostream
& operator<<(std::ostream
& rStrm
, const ScAddress
& rAddr
)
78 rStrm
<< "Col: " << rAddr
.Col() << " Row: " << rAddr
.Row() << " Tab: " << rAddr
.Tab() << "\n";
82 static std::ostream
& operator<<(std::ostream
& rStrm
, const ScRange
& rRange
)
84 rStrm
<< "ScRange: " << rRange
.aStart
<< rRange
.aEnd
<< "\n";
88 static std::ostream
& operator<<(std::ostream
& rStrm
, const ScRangeList
& rList
)
90 rStrm
<< "ScRangeList: \n";
91 for(size_t i
= 0; i
< rList
.size(); ++i
)
98 BootstrapFixture::setUp();
101 m_xDocShRef
= new ScDocShell(
103 SFXMODEL_DISABLE_EMBEDDED_SCRIPTS
|
104 SFXMODEL_DISABLE_DOCUMENT_RECOVERY
);
106 m_pDoc
= m_xDocShRef
->GetDocument();
109 void Test::tearDown()
112 BootstrapFixture::tearDown();
115 void Test::testDeleteArea_4Ranges()
117 ScRangeList
aList(ScRange(0,0,0,5,5,0));
118 aList
.DeleteArea(2,2,0,3,3,0);
120 CPPUNIT_ASSERT_EQUAL(aList
.size(), static_cast<size_t>(4));
121 for(SCCOL nCol
= 0; nCol
<= 5; ++nCol
)
123 for(SCROW nRow
= 0; nRow
<= 5; ++nRow
)
125 if((nCol
== 2 || nCol
== 3) && ( nRow
== 2 || nRow
== 3))
126 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
128 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
133 void Test::testDeleteArea_3Ranges()
135 ScRangeList
aList(ScRange(1,1,0,6,6,0));
136 aList
.DeleteArea(3,3,0,8,4,0);
138 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
139 for(SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
141 for(SCROW nRow
= 1; nRow
<= 6; ++nRow
)
143 if((nRow
== 3 || nRow
== 4) && (nCol
>= 3))
144 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
146 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
150 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(28), aList
.GetCellCount());
153 void Test::testDeleteArea_3Ranges_Case2()
155 ScRangeList
aList(ScRange(1,1,0,6,6,0));
156 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
157 aList
.DeleteArea(0,2,0,2,4,0);
158 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
160 // Column 1-2 && Row 2-4 should not be in the range list. The rest should
162 for (SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
164 for (SCROW nRow
= 1; nRow
<= 6; ++nRow
)
166 if ((1 <= nCol
&& nCol
<= 2) && (2 <= nRow
&& nRow
<= 4))
167 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
169 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
174 void Test::testDeleteArea_3Ranges_Case3()
176 ScRangeList
aList(ScRange(1,5,0,6,11,0));
177 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
178 aList
.DeleteArea(3,2,0,4,8,0);
179 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
181 // Column 3-4 && Row 5-8 should not be in the range list.
182 for (SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
184 for (SCROW nRow
= 5; nRow
<= 11; ++nRow
)
186 if ((3 <= nCol
&& nCol
<= 4) && (5 <= nRow
&& nRow
<= 8))
187 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
189 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
194 void Test::testDeleteArea_3Ranges_Case4()
196 ScRangeList
aList(ScRange(1,5,0,6,11,0));
197 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
198 aList
.DeleteArea(3,5,0,4,5,0);
199 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
201 // Column 3-4 && Row 5 should not be in the range list.
202 for (SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
204 for (SCROW nRow
= 5; nRow
<= 11; ++nRow
)
206 if ((3 <= nCol
&& nCol
<= 4) && 5 == nRow
)
207 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
209 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
214 void Test::testDeleteArea_3Ranges_Case5()
216 ScRangeList
aList(ScRange(1,5,0,6,11,0));
217 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
218 aList
.DeleteArea(6,7,0,6,9,0);
219 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.size());
221 // Column 6 && Row 7-9 should not be in the range list.
222 for (SCCOL nCol
= 1; nCol
<= 6; ++nCol
)
224 for (SCROW nRow
= 5; nRow
<= 11; ++nRow
)
226 if ( nCol
== 6 && (7 <= nRow
&& nRow
<= 9))
227 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
229 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
234 void Test::testDeleteArea_2Ranges()
236 ScRangeList
aList(ScRange(0,0,0,5,5,5));
237 ScRangeList
aList2(aList
);
239 aList
.DeleteArea(4,4,0,6,7,0);
240 aList2
.DeleteArea(4,4,0,6,7,0);
241 CPPUNIT_ASSERT_EQUAL(aList
.size(), static_cast<size_t>(2));
242 CPPUNIT_ASSERT_EQUAL(aList2
.size(), static_cast<size_t>(2));
244 for(SCCOL nCol
= 0; nCol
<= 5; ++nCol
)
246 for(SCROW nRow
= 0; nRow
<= 5; ++nRow
)
248 if(nCol
>=4 && nRow
>= 4)
249 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
251 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
256 void Test::testDeleteArea_2Ranges_Case2()
258 ScRangeList
aList(ScRange(1,1,0,1,5,0));
259 aList
.DeleteArea(0,3,0,MAXCOL
,3,0);
261 for(SCROW nRow
= 1; nRow
<= 5; ++nRow
)
264 CPPUNIT_ASSERT(!aList
.In(ScRange(1,3,0)));
266 CPPUNIT_ASSERT(aList
.In(ScRange(1,nRow
,0)));
268 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), aList
.GetCellCount());
271 void Test::testDeleteArea_2Ranges_Case3()
273 ScRangeList
aList(ScRange(0,5,0,2,10,0));
274 aList
.DeleteArea(2,3,0,3,7,0);
275 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
277 // Column 2 Row 5-7 shouldn't be in the list.
278 for (SCCOL nCol
= 0; nCol
<= 2; ++nCol
)
280 for (SCROW nRow
= 5; nRow
<= 10; ++nRow
)
282 if (nCol
== 2 && (5 <= nRow
&& nRow
<= 7))
283 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
,nRow
,0)));
285 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
,nRow
,0)));
290 void Test::testDeleteArea_2Ranges_Case4()
292 ScRangeList
aList(ScRange(2,3,0,4,7,0));
293 aList
.DeleteArea(0,1,0,2,5,0);
294 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
296 // Column 2 Row 3-5 shouldn't be in the list.
297 for (SCCOL nCol
= 2; nCol
<= 4; ++nCol
)
299 for (SCROW nRow
= 3; nRow
<= 7; ++nRow
)
301 if (nCol
== 2 && (3 <= nRow
&& nRow
<= 5))
302 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
,nRow
,0)));
304 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
,nRow
,0)));
309 void Test::testDeleteArea_2Ranges_Case5()
311 ScRangeList
aList(ScRange(2,2,0,5,5,0));
312 aList
.DeleteArea(4,5,0,5,5,0);
313 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
315 // Column 4 and 5 Row 5 shouldn't be in the list.
316 for(SCCOL nCol
= 2; nCol
<= 5; ++nCol
)
318 for(SCROW nRow
= 2; nRow
<= 5; ++nRow
)
320 if(nRow
== 5 && 4 <= nCol
)
321 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
323 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
328 void Test::testDeleteArea_2Ranges_Case6()
330 ScRangeList
aList(ScRange(2,2,0,5,5,0));
331 aList
.DeleteArea(4,2,0,5,2,0);
332 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
334 // Column 4 and 5 Row 2 shouldn't be in the list.
335 for(SCCOL nCol
= 2; nCol
<= 5; ++nCol
)
337 for(SCROW nRow
= 2; nRow
<= 5; ++nRow
)
339 if(nRow
== 2 && 4 <= nCol
)
340 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
342 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
347 void Test::testDeleteArea_2Ranges_Case7()
349 ScRangeList
aList(ScRange(2,2,0,5,5,0));
350 aList
.DeleteArea(2,5,0,2,5,0);
351 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
353 // Column 2 Row 5 shouldn't be in the list.
354 for(SCCOL nCol
= 2; nCol
<= 5; ++nCol
)
356 for(SCROW nRow
= 2; nRow
<= 5; ++nRow
)
358 if(nRow
== 5 && nCol
== 2)
359 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
361 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
366 void Test::testDeleteArea_2Ranges_Case8()
368 ScRangeList
aList(ScRange(2,2,0,5,5,0));
369 aList
.DeleteArea(2,2,0,3,2,0);
370 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList
.size());
372 // Column 2 & 3 Row 2 shouldn't be in the list.
373 for(SCCOL nCol
= 2; nCol
<= 5; ++nCol
)
375 for(SCROW nRow
= 2; nRow
<= 5; ++nRow
)
377 if(nRow
== 2 && nCol
<= 3)
378 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, nRow
, 0)));
380 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
385 void Test::testDeleteArea_1Range()
387 ScRangeList
aList(ScRange(1,1,0,3,3,0));
388 aList
.DeleteArea(1,1,0,2,3,0);
390 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
392 for(SCROW nRow
= 1; nRow
<= 3; ++nRow
)
394 CPPUNIT_ASSERT(aList
.In(ScRange(3,nRow
,0)));
396 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList
.GetCellCount());
399 void Test::testDeleteArea_0Ranges()
401 ScRangeList
aList(ScRange(1,1,0,3,3,0));
402 aList
.DeleteArea(1,1,0,3,3,0);
404 CPPUNIT_ASSERT(aList
.empty());
406 ScRangeList
aList2(ScRange(1,1,0,3,3,0));
407 aList2
.DeleteArea(0,0,0,4,4,0);
409 CPPUNIT_ASSERT(aList
.empty());
412 void Test::testJoin_Case1()
415 aList
.push_back(new ScRange(1,1,0,3,3,0));
416 aList
.Join(ScRange(4,1,0,6,3,0));
418 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
419 CPPUNIT_ASSERT( ScRange(1,1,0,6,3,0) == *aList
[0]);
422 void Test::testJoin_Case2()
425 aList
.push_back(new ScRange(1,1,0,3,3,0));
426 aList
.push_back(new ScRange(4,1,0,6,3,0));
427 aList
.push_back(new ScRange(7,1,0,9,3,0));
429 aList
.Join(*aList
[2], true);
431 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList
.size());
432 CPPUNIT_ASSERT(ScRange(1,1,0,9,3,0) == *aList
[0]);
435 void Test::testUpdateReference_DeleteRow()
437 ScRangeList
aList(ScRange(1,1,0,4,4,0));
438 bool bUpdated
= aList
.UpdateReference(URM_INSDEL
, m_pDoc
, ScRange(0,3,0,MAXCOL
,MAXROW
,0), 0, -1, 0);
439 CPPUNIT_ASSERT(bUpdated
);
441 for(SCCOL nCol
= 1; nCol
<= 4; ++nCol
)
443 for(SCROW nRow
= 1; nRow
<= 3; ++nRow
)
445 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
447 CPPUNIT_ASSERT(!aList
.In(ScRange(nCol
, 4, 0)));
449 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(12), aList
.GetCellCount());
451 ScRangeList
aList2(ScRange(2,2,0,2,2,0));
452 aList2
.UpdateReference(URM_INSDEL
, m_pDoc
, ScRange(0,3,0,MAXCOL
,MAXROW
,0), 0, -1, 0);
453 CPPUNIT_ASSERT(aList2
.empty());
456 void Test::testUpdateReference_DeleteCol()
458 ScRangeList
aList(ScRange(1,1,0,4,4,0));
459 bool bUpdated
= aList
.UpdateReference(URM_INSDEL
, m_pDoc
, ScRange(3,0,0,MAXCOL
,MAXROW
,0), -1, 0, 0);
460 CPPUNIT_ASSERT(bUpdated
);
462 for(SCROW nRow
= 1; nRow
<= 4; ++nRow
)
464 for(SCCOL nCol
= 1; nCol
<= 3; ++nCol
)
466 CPPUNIT_ASSERT(aList
.In(ScRange(nCol
, nRow
, 0)));
468 CPPUNIT_ASSERT(!aList
.In(ScRange(4, nRow
, 0)));
470 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(12), aList
.GetCellCount());
473 void Test::testGetIntersectedRange()
475 ScRangeList
aList(ScRange(2, 2, 0, 5, 5, 0));
476 ScRangeList aIntersecting
= aList
.GetIntersectedRange(ScRange(0, 0, 0, 3, 3, 0));
477 CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(2,2,0,3,3,0)), aIntersecting
);
480 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
482 CPPUNIT_PLUGIN_IMPLEMENT();
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */