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