tdf#158198 sw: prevent xBookmark.getAnchor().setString("") from deleting
[LibreOffice.git] / sc / qa / unit / ucalc_rangelst.cxx
blob364bd40d69d34e8201eff49202cb7ecac144a174
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <sal/config.h>
11 #include "helper/qahelper.hxx"
13 #include <rangelst.hxx>
15 class Test : public ScUcalcTestBase
19 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_4Ranges)
21 ScRangeList aList(ScRange(0,0,0,5,5,0));
22 aList.DeleteArea(2,2,0,3,3,0);
24 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), aList.size());
25 for(SCCOL nCol = 0; nCol <= 5; ++nCol)
27 for(SCROW nRow = 0; nRow <= 5; ++nRow)
29 if((nCol == 2 || nCol == 3) && ( nRow == 2 || nRow == 3))
30 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
31 else
32 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
37 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_3Ranges)
39 ScRangeList aList(ScRange(1,1,0,6,6,0));
40 aList.DeleteArea(3,3,0,8,4,0);
42 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
43 for(SCCOL nCol = 1; nCol <= 6; ++nCol)
45 for(SCROW nRow = 1; nRow <= 6; ++nRow)
47 if((nRow == 3 || nRow == 4) && (nCol >= 3))
48 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
49 else
50 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
54 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt64>(28), aList.GetCellCount());
57 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_3Ranges_Case2)
59 ScRangeList aList(ScRange(1,1,0,6,6,0));
60 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
61 aList.DeleteArea(0,2,0,2,4,0);
62 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
64 // Column 1-2 && Row 2-4 should not be in the range list. The rest should
65 // be in the list.
66 for (SCCOL nCol = 1; nCol <= 6; ++nCol)
68 for (SCROW nRow = 1; nRow <= 6; ++nRow)
70 if ((1 <= nCol && nCol <= 2) && (2 <= nRow && nRow <= 4))
71 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
72 else
73 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
78 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_3Ranges_Case3)
80 ScRangeList aList(ScRange(1,5,0,6,11,0));
81 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
82 aList.DeleteArea(3,2,0,4,8,0);
83 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
85 // Column 3-4 && Row 5-8 should not be in the range list.
86 for (SCCOL nCol = 1; nCol <= 6; ++nCol)
88 for (SCROW nRow = 5; nRow <= 11; ++nRow)
90 if ((3 <= nCol && nCol <= 4) && (5 <= nRow && nRow <= 8))
91 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
92 else
93 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
98 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_3Ranges_Case4)
100 ScRangeList aList(ScRange(1,5,0,6,11,0));
101 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
102 aList.DeleteArea(3,5,0,4,5,0);
103 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
105 // Column 3-4 && Row 5 should not be in the range list.
106 for (SCCOL nCol = 1; nCol <= 6; ++nCol)
108 for (SCROW nRow = 5; nRow <= 11; ++nRow)
110 if ((3 <= nCol && nCol <= 4) && 5 == nRow )
111 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
112 else
113 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
118 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_3Ranges_Case5)
120 ScRangeList aList(ScRange(1,5,0,6,11,0));
121 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
122 aList.DeleteArea(6,7,0,6,9,0);
123 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aList.size());
125 // Column 6 && Row 7-9 should not be in the range list.
126 for (SCCOL nCol = 1; nCol <= 6; ++nCol)
128 for (SCROW nRow = 5; nRow <= 11; ++nRow)
130 if ( nCol == 6 && (7 <= nRow && nRow <= 9))
131 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
132 else
133 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
138 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_2Ranges)
140 ScRangeList aList(ScRange(0,0,0,5,5,5));
141 ScRangeList aList2(aList);
143 aList.DeleteArea(4,4,0,6,7,0);
144 aList2.DeleteArea(4,4,0,6,7,0);
145 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
146 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList2.size());
148 for(SCCOL nCol = 0; nCol <= 5; ++nCol)
150 for(SCROW nRow = 0; nRow <= 5; ++nRow)
152 if(nCol>=4 && nRow >= 4)
153 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
154 else
155 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
160 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_2Ranges_Case2)
162 ScRangeList aList(ScRange(1,1,0,1,5,0));
163 aList.DeleteArea(0,3,0,ScSheetLimits::CreateDefault().MaxCol(),3,0);
165 for(SCROW nRow = 1; nRow <= 5; ++nRow)
167 if(nRow == 3)
168 CPPUNIT_ASSERT(!aList.Contains(ScRange(1,3,0)));
169 else
170 CPPUNIT_ASSERT(aList.Contains(ScRange(1,nRow,0)));
172 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt64>(4), aList.GetCellCount());
175 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_2Ranges_Case3)
177 ScRangeList aList(ScRange(0,5,0,2,10,0));
178 aList.DeleteArea(2,3,0,3,7,0);
179 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
181 // Column 2 Row 5-7 shouldn't be in the list.
182 for (SCCOL nCol = 0; nCol <= 2; ++nCol)
184 for (SCROW nRow = 5; nRow <= 10; ++nRow)
186 if (nCol == 2 && (5 <= nRow && nRow <= 7))
187 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol,nRow,0)));
188 else
189 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol,nRow,0)));
194 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_2Ranges_Case4)
196 ScRangeList aList(ScRange(2,3,0,4,7,0));
197 aList.DeleteArea(0,1,0,2,5,0);
198 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
200 // Column 2 Row 3-5 shouldn't be in the list.
201 for (SCCOL nCol = 2; nCol <= 4; ++nCol)
203 for (SCROW nRow = 3; nRow <= 7; ++nRow)
205 if (nCol == 2 && (3 <= nRow && nRow <= 5))
206 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol,nRow,0)));
207 else
208 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol,nRow,0)));
213 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_2Ranges_Case5)
215 ScRangeList aList(ScRange(2,2,0,5,5,0));
216 aList.DeleteArea(4,5,0,5,5,0);
217 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
219 // Column 4 and 5 Row 5 shouldn't be in the list.
220 for(SCCOL nCol = 2; nCol <= 5; ++nCol)
222 for(SCROW nRow = 2; nRow <= 5; ++nRow)
224 if(nRow == 5 && 4 <= nCol)
225 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
226 else
227 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
232 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_2Ranges_Case6)
234 ScRangeList aList(ScRange(2,2,0,5,5,0));
235 aList.DeleteArea(4,2,0,5,2,0);
236 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
238 // Column 4 and 5 Row 2 shouldn't be in the list.
239 for(SCCOL nCol = 2; nCol <= 5; ++nCol)
241 for(SCROW nRow = 2; nRow <= 5; ++nRow)
243 if(nRow == 2 && 4 <= nCol)
244 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
245 else
246 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
251 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_2Ranges_Case7)
253 ScRangeList aList(ScRange(2,2,0,5,5,0));
254 aList.DeleteArea(2,5,0,2,5,0);
255 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
257 // Column 2 Row 5 shouldn't be in the list.
258 for(SCCOL nCol = 2; nCol <= 5; ++nCol)
260 for(SCROW nRow = 2; nRow <= 5; ++nRow)
262 if(nRow == 5 && nCol == 2)
263 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
264 else
265 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
270 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_2Ranges_Case8)
272 ScRangeList aList(ScRange(2,2,0,5,5,0));
273 aList.DeleteArea(2,2,0,3,2,0);
274 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
276 // Column 2 & 3 Row 2 shouldn't be in the list.
277 for(SCCOL nCol = 2; nCol <= 5; ++nCol)
279 for(SCROW nRow = 2; nRow <= 5; ++nRow)
281 if(nRow == 2 && nCol <= 3)
282 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, nRow, 0)));
283 else
284 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
289 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_1Range)
291 ScRangeList aList(ScRange(1,1,0,3,3,0));
292 aList.DeleteArea(1,1,0,2,3,0);
294 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
296 for(SCROW nRow = 1; nRow <= 3; ++nRow)
298 CPPUNIT_ASSERT(aList.Contains(ScRange(3,nRow,0)));
300 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt64>(3), aList.GetCellCount());
303 CPPUNIT_TEST_FIXTURE(Test, testDeleteArea_0Ranges)
305 ScRangeList aList(ScRange(1,1,0,3,3,0));
306 aList.DeleteArea(1,1,0,3,3,0);
308 CPPUNIT_ASSERT(aList.empty());
310 ScRangeList aList2(ScRange(1,1,0,3,3,0));
311 aList2.DeleteArea(0,0,0,4,4,0);
313 CPPUNIT_ASSERT(aList.empty());
316 CPPUNIT_TEST_FIXTURE(Test, testJoin_Case1)
318 ScRangeList aList;
319 aList.push_back(ScRange(1,1,0,3,3,0));
320 aList.Join(ScRange(4,1,0,6,3,0));
322 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
323 CPPUNIT_ASSERT_EQUAL( ScRange(1,1,0,6,3,0), aList[0]);
326 CPPUNIT_TEST_FIXTURE(Test, testJoin_Case2)
328 ScRangeList aList;
329 aList.push_back(ScRange(1,1,0,3,3,0));
330 aList.push_back(ScRange(4,1,0,6,3,0));
331 aList.push_back(ScRange(7,1,0,9,3,0));
333 aList.Join(aList[2], true);
335 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
336 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,9,3,0), aList[0]);
339 CPPUNIT_TEST_FIXTURE(Test, testJoin_Case3)
341 ScRangeList aList;
342 aList.Join(ScRange(1,1,0,6,6,0));
343 aList.Join(ScRange(3,3,0,4,4,0));
345 // The second one should have been swallowed by the first one
346 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
347 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,6,6,0), aList[0]);
349 // Add a disjoint one
350 aList.Join(ScRange(8,8,0,9,9,0));
352 // Should be two ones now
353 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
354 // The first one should still be as is
355 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,6,6,0), aList[0]);
356 // Ditto for the second one
357 CPPUNIT_ASSERT_EQUAL(ScRange(8,8,0,9,9,0), aList[1]);
360 CPPUNIT_TEST_FIXTURE(Test, testJoin_Case4)
362 ScRangeList aList;
363 aList.Join(ScRange(1,1,0,2,6,0));
364 // Join a range that overlaps it and extends it vertically
365 aList.Join(ScRange(1,4,0,2,8,0));
367 // The one range in the list should have been extended
368 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
369 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,2,8,0), aList[0]);
371 // Join a range that overlaps it and extends it horizontally
372 aList.Join(ScRange(2,1,0,4,8,0));
374 // Again, should have just been extended
375 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
376 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,4,8,0), aList[0]);
378 // And then the same but on top / to the left of existing range
379 ScRangeList aList2;
380 aList2.Join(ScRange(4,4,0,8,8,0));
381 aList2.Join(ScRange(4,1,0,8,6,0));
383 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList2.size());
384 CPPUNIT_ASSERT_EQUAL(ScRange(4,1,0,8,8,0), aList2[0]);
386 aList2.Join(ScRange(1,1,0,6,8,0));
388 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList2.size());
389 CPPUNIT_ASSERT_EQUAL(ScRange(1,1,0,8,8,0), aList2[0]);
392 CPPUNIT_TEST_FIXTURE(Test, testJoin_Case5)
394 ScRangeList aList;
395 aList.Join(ScRange(0,0,0,4,4,0));
396 aList.Join(ScRange(8,0,0,10,4,0));
398 // Nothing special so far, two disjoint ranges
399 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aList.size());
400 CPPUNIT_ASSERT_EQUAL(ScRange(0,0,0,4,4,0), aList[0]);
401 CPPUNIT_ASSERT_EQUAL(ScRange(8,0,0,10,4,0), aList[1]);
403 // This should join the two ranges into one
404 aList.Join(ScRange(5,0,0,9,4,0));
406 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aList.size());
407 CPPUNIT_ASSERT_EQUAL(ScRange(0,0,0,10,4,0), aList[0]);
410 CPPUNIT_TEST_FIXTURE(Test, testUpdateReference_DeleteRow)
412 ScRangeList aList(ScRange(1,1,0,4,4,0));
413 bool bUpdated = aList.UpdateReference(URM_INSDEL, m_pDoc, ScRange(0,3,0,m_pDoc->MaxCol(),m_pDoc->MaxRow(),0), 0, -1, 0);
414 CPPUNIT_ASSERT(bUpdated);
416 for(SCCOL nCol = 1; nCol <= 4; ++nCol)
418 for(SCROW nRow = 1; nRow <= 3; ++nRow)
420 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
422 CPPUNIT_ASSERT(!aList.Contains(ScRange(nCol, 4, 0)));
424 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt64>(12), aList.GetCellCount());
426 ScRangeList aList2(ScRange(2,2,0,2,2,0));
427 aList2.UpdateReference(URM_INSDEL, m_pDoc, ScRange(0,3,0,m_pDoc->MaxCol(),m_pDoc->MaxRow(),0), 0, -1, 0);
428 CPPUNIT_ASSERT(aList2.empty());
430 ScRangeList aList3;
431 aList3.push_back(ScRange(2,2,0,2,8,0));
432 aList3.push_back(ScRange(4,2,0,4,8,0));
433 aList3.UpdateReference(URM_INSDEL, m_pDoc, ScRange(2,5,0,m_pDoc->MaxCol(),m_pDoc->MaxRow(),0), 0, -1, 0);
434 // Verify all ranges in the list have been updated properly.
435 CPPUNIT_ASSERT_EQUAL(size_t(2), aList3.size());
436 CPPUNIT_ASSERT_EQUAL(ScRange(2,2,0,2,7,0), aList3[0]);
437 CPPUNIT_ASSERT_EQUAL(ScRange(4,2,0,4,7,0), aList3[1]);
439 ScRangeList aList4(ScRange(0,0,0,m_pDoc->MaxCol(),m_pDoc->MaxRow(),0));
440 ScRangeList aList4Copy = aList4;
441 aList4.UpdateReference(URM_INSDEL, m_pDoc, ScRange(14,3,0,m_pDoc->MaxCol(),7,0), 0, -2, 0);
442 CPPUNIT_ASSERT_EQUAL(aList4Copy, aList4);
445 CPPUNIT_TEST_FIXTURE(Test, testUpdateReference_DeleteLastRow)
447 ScRangeList aList(ScRange(1,1,0,4,4,0));
448 bool bUpdated = aList.UpdateReference(URM_INSDEL, m_pDoc, ScRange(0,4,0,m_pDoc->MaxCol(),4,0), 0, -1, 0);
449 CPPUNIT_ASSERT(bUpdated);
452 CPPUNIT_TEST_FIXTURE(Test, testUpdateReference_DeleteCol)
454 ScRangeList aList(ScRange(1,1,0,4,4,0));
455 bool bUpdated = aList.UpdateReference(URM_INSDEL, m_pDoc, ScRange(3,0,0,m_pDoc->MaxCol(),m_pDoc->MaxRow(),0), -1, 0, 0);
456 CPPUNIT_ASSERT(bUpdated);
458 for(SCROW nRow = 1; nRow <= 4; ++nRow)
460 for(SCCOL nCol = 1; nCol <= 3; ++nCol)
462 CPPUNIT_ASSERT(aList.Contains(ScRange(nCol, nRow, 0)));
464 CPPUNIT_ASSERT(!aList.Contains(ScRange(4, nRow, 0)));
466 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt64>(12), aList.GetCellCount());
469 CPPUNIT_TEST_FIXTURE(Test, testGetIntersectedRange)
471 ScRangeList aList(ScRange(2, 2, 0, 5, 5, 0));
472 ScRangeList aIntersecting = aList.GetIntersectedRange(ScRange(0, 0, 0, 3, 3, 0));
473 CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(2,2,0,3,3,0)), aIntersecting);
476 CPPUNIT_TEST_FIXTURE(Test, testInsertRow)
478 ScRangeList aList(ScRange(1,1,0,4,4,0));
479 aList.InsertRow(0, 0, m_pDoc->MaxCol(), 5, 2);
480 CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(1,1,0,4,6,0)), aList);
483 CPPUNIT_TEST_FIXTURE(Test, testInsertCol)
485 ScRangeList aList(ScRange(1,1,0,4,4,0));
486 aList.InsertCol(0, 0, m_pDoc->MaxRow(), 5, 2);
487 CPPUNIT_ASSERT_EQUAL(ScRangeList(ScRange(1,1,0,6,4,0)), aList);
490 CPPUNIT_PLUGIN_IMPLEMENT();
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */