bump product version to 5.0.4.1
[LibreOffice.git] / sw / qa / core / Test-BigPtrArray.cxx
blob54ae06c4ed0f3dd694088fc9a6b5a9f537c92c0d
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include <rtl/strbuf.hxx>
22 #include <cppunit/TestAssert.h>
23 #include <cppunit/TestFixture.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/plugin/TestPlugIn.h>
27 //#define TIMELOG for measuring performance
29 #include <string.h>
30 #include <stdlib.h>
32 #include <bparr.hxx>
33 #include <ndarr.hxx>
35 using namespace std;
37 namespace /* private */
39 const sal_uLong NUM_ENTRIES = 10;
41 class BigPtrEntryMock : public BigPtrEntry
43 public:
44 BigPtrEntryMock(sal_uLong count) : count_(count)
48 virtual ~BigPtrEntryMock()
52 sal_uLong getCount() const
54 return count_;
57 sal_uLong Position() const
59 return GetPos();
62 private:
63 sal_uLong count_;
66 void dumpBigPtrArray(const BigPtrArray& bparr)
68 (void)bparr;
71 void fillBigPtrArray(BigPtrArray& bparr, sal_uLong numEntries)
73 for (sal_uLong i = 0; i < numEntries; i++)
74 bparr.Insert(new BigPtrEntryMock(i), bparr.Count());
77 void printMethodName(const char* name)
79 (void)name;
82 bool checkElementPositions(const BigPtrArray& bparr)
84 for (sal_uLong i = 0; i < bparr.Count(); i++)
86 if (static_cast<BigPtrEntryMock*>(bparr[i])->Position() != i)
87 return false;
89 return true;
92 void releaseBigPtrArrayContent(BigPtrArray& bparr)
94 for (sal_uLong i = 0; i < bparr.Count(); i++)
95 delete bparr[i];
99 class BigPtrArrayUnittest : public CppUnit::TestFixture
101 public:
103 BigPtrArrayUnittest()
107 /** Test constructor/destructor
108 The size of the BigPtrArray
109 aka the 'Count' should be 0
110 initially.
112 void test_ctor()
114 printMethodName("test_ctor\n");
116 BigPtrArray bparr;
118 CPPUNIT_ASSERT_MESSAGE
120 "BigPtrArray ctor failed",
121 bparr.Count() == 0
125 void test_insert_entries_at_front()
127 printMethodName("test_insert_entries_at_front\n");
129 BigPtrArray bparr;
131 for (sal_uLong i = 0; i < NUM_ENTRIES; i++)
133 sal_uLong oldCount = bparr.Count();
134 bparr.Insert(new BigPtrEntryMock(i), 0);
135 CPPUNIT_ASSERT_MESSAGE
137 "test_insert_entries_at_front failed",
138 (bparr.Count() == oldCount + 1)
142 for (sal_uLong i = 0, j = NUM_ENTRIES - 1; i < NUM_ENTRIES; i++, j--)
144 CPPUNIT_ASSERT_MESSAGE
146 "test_insert_entries_at_front failed",
147 static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == j
151 CPPUNIT_ASSERT_MESSAGE
153 "test_insert_entries_at_front failed",
154 checkElementPositions(bparr)
157 releaseBigPtrArrayContent(bparr);
158 dumpBigPtrArray(bparr);
161 void test_insert_entries_in_the_middle()
163 printMethodName("test_insert_entries_in_the_middle\n");
165 BigPtrArray bparr;
167 fillBigPtrArray(bparr, NUM_ENTRIES);
168 dumpBigPtrArray(bparr);
170 sal_uLong oldCount = bparr.Count();
172 bparr.Insert(new BigPtrEntryMock(NUM_ENTRIES), bparr.Count() / 2);
174 CPPUNIT_ASSERT_MESSAGE
176 "test_insert_entries_in_the_middle failed",
177 (oldCount + 1 == bparr.Count() && static_cast<BigPtrEntryMock*>(bparr[bparr.Count() / 2])->getCount() == NUM_ENTRIES)
180 CPPUNIT_ASSERT_MESSAGE
182 "test_insert_entries_in_the_middle failed",
183 checkElementPositions(bparr)
186 releaseBigPtrArrayContent(bparr);
187 dumpBigPtrArray(bparr);
190 void test_insert_at_already_used_index()
192 printMethodName("test_insert_at_already_used_index\n");
194 BigPtrArray bparr;
196 fillBigPtrArray(bparr, NUM_ENTRIES);
197 dumpBigPtrArray(bparr);
199 const sal_uLong oldCount = bparr.Count();
201 // insert 5 elements
202 for (sal_uLong i = 0, j = 30; i < 5; i++, j++)
203 bparr.Insert(new BigPtrEntryMock(j), i);
205 CPPUNIT_ASSERT_MESSAGE
207 "test_insert_at_already_used_index failed",
208 (oldCount + 5 == bparr.Count())
211 // now, first 5 elements have counts: 30,31,..34
212 // next 10 elements have counts: 0,1,..9
213 for (sal_uLong i = 0, j = 30; i < bparr.Count(); i++, j++)
215 CPPUNIT_ASSERT_MESSAGE
217 "test_insert_at_already_used_index failed",
218 static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (i < 5 ? j : i - 5)
222 CPPUNIT_ASSERT_MESSAGE
224 "test_insert_at_already_used_index failed",
225 checkElementPositions(bparr)
228 releaseBigPtrArrayContent(bparr);
229 dumpBigPtrArray(bparr);
232 void test_insert_at_end()
234 printMethodName("test_insert_at_end\n");
236 BigPtrArray bparr;
238 fillBigPtrArray(bparr, NUM_ENTRIES);
239 dumpBigPtrArray(bparr);
241 sal_uLong oldCount = bparr.Count();
242 bparr.Insert(new BigPtrEntryMock(NUM_ENTRIES), bparr.Count());
244 CPPUNIT_ASSERT_MESSAGE
246 "test_insert_at_end failed",
247 (oldCount + 1 == bparr.Count() && static_cast<BigPtrEntryMock*>(bparr[bparr.Count()-1])->getCount() == NUM_ENTRIES)
250 CPPUNIT_ASSERT_MESSAGE
252 "test_insert_at_end failed",
253 checkElementPositions(bparr)
256 releaseBigPtrArrayContent(bparr);
257 dumpBigPtrArray(bparr);
260 void test_remove_at_front()
262 printMethodName("test_remove_at_front\n");
264 BigPtrArray bparr;
266 fillBigPtrArray(bparr, NUM_ENTRIES);
267 dumpBigPtrArray(bparr);
269 for (sal_uLong i = 0; i < NUM_ENTRIES; i++)
271 sal_uLong oldCount = bparr.Count();
273 delete bparr[0]; // release content
274 bparr.Remove(0); // remove item from container
276 CPPUNIT_ASSERT_MESSAGE
278 "test_remove_at_front failed (wrong count)",
279 (oldCount - 1 == bparr.Count())
282 for (sal_uLong j = 0, k = i + 1; j < bparr.Count(); j++, k++)
284 CPPUNIT_ASSERT_MESSAGE
286 "test_remove_at_front failed",
287 static_cast<BigPtrEntryMock*>(bparr[j])->getCount() == k
291 CPPUNIT_ASSERT_MESSAGE
293 "test_remove_at_front failed",
294 checkElementPositions(bparr)
297 dumpBigPtrArray(bparr);
301 void test_remove_at_back()
303 printMethodName("test_remove_at_back\n");
305 BigPtrArray bparr;
307 fillBigPtrArray(bparr, NUM_ENTRIES);
308 dumpBigPtrArray(bparr);
310 for (int i = NUM_ENTRIES - 1; i >= 0; i--)
312 sal_uLong oldCount = bparr.Count();
313 delete bparr[i];
314 bparr.Remove(i);
316 CPPUNIT_ASSERT_MESSAGE
318 "test_remove_at_back failed (wrong count)",
319 (oldCount - 1 == bparr.Count())
322 for (sal_uLong j = 0; j < bparr.Count(); j++)
324 CPPUNIT_ASSERT_MESSAGE
326 "test_remove_at_back failed",
327 static_cast<BigPtrEntryMock*>(bparr[j])->getCount() == j
331 CPPUNIT_ASSERT_MESSAGE
333 "test_remove_at_back failed",
334 checkElementPositions(bparr)
337 dumpBigPtrArray(bparr);
341 void test_remove_in_the_middle()
343 printMethodName("test_remove_in_the_middle\n");
345 BigPtrArray bparr;
347 fillBigPtrArray(bparr, NUM_ENTRIES);
348 dumpBigPtrArray(bparr);
350 while (bparr.Count())
352 sal_uLong oldCount = bparr.Count();
353 sal_uLong oldElement = static_cast<BigPtrEntryMock*>(bparr[bparr.Count() / 2])->getCount();
355 delete bparr[bparr.Count() / 2];
356 bparr.Remove(bparr.Count() / 2);
358 CPPUNIT_ASSERT_MESSAGE
360 "test_remove_in_the_middle failed (wrong count)",
361 (oldCount - 1 == bparr.Count())
364 for (sal_uLong i = 0; i < bparr.Count(); i++)
366 CPPUNIT_ASSERT_MESSAGE
368 "test_remove_in_the_middle failed",
369 static_cast<BigPtrEntryMock*>(bparr[i])->getCount() != oldElement
373 CPPUNIT_ASSERT_MESSAGE
375 "test_remove_in_the_middle failed",
376 checkElementPositions(bparr)
379 dumpBigPtrArray(bparr);
383 void test_remove_multiple_elements_at_once()
385 printMethodName("test_remove_multiple_elements_at_once\n");
387 BigPtrArray bparr;
389 fillBigPtrArray(bparr, NUM_ENTRIES);
390 dumpBigPtrArray(bparr);
392 while(bparr.Count())
394 sal_uLong nRemove = (bparr.Count() > 3) ? 3 : bparr.Count();
395 sal_uLong oldCount = bparr.Count();
397 for (sal_uLong i = 0; i < nRemove; i++)
398 delete bparr[i];
400 bparr.Remove(0, nRemove);
402 CPPUNIT_ASSERT_MESSAGE
404 "test_remove_multiple_elements_at_once failed",
405 (oldCount - nRemove == bparr.Count())
408 CPPUNIT_ASSERT_MESSAGE
410 "test_remove_multiple_elements_at_once failed",
411 checkElementPositions(bparr)
414 dumpBigPtrArray(bparr);
418 void test_remove_all_elements_at_once()
420 printMethodName("test_remove_all_elements_at_once\n");
422 BigPtrArray bparr;
424 fillBigPtrArray(bparr, NUM_ENTRIES);
425 dumpBigPtrArray(bparr);
427 releaseBigPtrArrayContent(bparr);
428 bparr.Remove(0, bparr.Count());
430 CPPUNIT_ASSERT_MESSAGE
432 "test_remove_all_elements_at_once failed",
433 bparr.Count() == 0
436 dumpBigPtrArray(bparr);
439 void test_move_elements_from_lower_to_higher_pos()
441 printMethodName("test_move_elements_from_lower_to_higher_pos\n");
443 BigPtrArray bparr;
445 fillBigPtrArray(bparr, NUM_ENTRIES);
446 dumpBigPtrArray(bparr);
448 for (sal_uLong i = 0; i < NUM_ENTRIES - 1; i++)
450 bparr.Move(i, i + 2);
451 dumpBigPtrArray(bparr);
454 for (sal_uLong i = 0; i < (NUM_ENTRIES - 1); i++)
456 CPPUNIT_ASSERT_MESSAGE
458 "test_move_elements_from_lower_to_higher_pos failed",
459 static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (i + 1)
463 CPPUNIT_ASSERT_MESSAGE
465 "test_move_elements_from_lower_to_higher_pos failed",
466 static_cast<BigPtrEntryMock*>(bparr[NUM_ENTRIES -1])->getCount() == 0
469 CPPUNIT_ASSERT_MESSAGE
471 "test_move_elements_from_lower_to_higher_pos failed",
472 checkElementPositions(bparr)
475 releaseBigPtrArrayContent(bparr);
478 void test_move_elements_from_higher_to_lower_pos()
480 printMethodName("test_move_elements_from_higher_to_lower_pos\n");
482 BigPtrArray bparr;
484 fillBigPtrArray(bparr, NUM_ENTRIES);
485 dumpBigPtrArray(bparr);
487 for (int i = NUM_ENTRIES - 1; i >= 1; i--)
489 bparr.Move(i, i - 1);
490 dumpBigPtrArray(bparr);
493 CPPUNIT_ASSERT_MESSAGE
495 "test_move_elements_from_higher_to_lower_pos failed",
496 static_cast<BigPtrEntryMock*>(bparr[0])->getCount() == (NUM_ENTRIES - 1)
499 for (sal_uLong i = 1; i < NUM_ENTRIES; i++)
501 CPPUNIT_ASSERT_MESSAGE
503 "test_move_elements_from_higher_to_lower_pos failed",
504 static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (i - 1)
508 CPPUNIT_ASSERT_MESSAGE
510 "test_move_elements_from_higher_to_lower_pos failed",
511 checkElementPositions(bparr)
514 releaseBigPtrArrayContent(bparr);
517 void test_move_to_same_position()
519 printMethodName("test_move_to_same_position\n");
521 BigPtrArray bparr;
523 fillBigPtrArray(bparr, NUM_ENTRIES);
524 dumpBigPtrArray(bparr);
526 for (sal_uLong i = 0; i < NUM_ENTRIES; i++)
528 bparr.Move(i, i);
531 dumpBigPtrArray(bparr);
533 for (sal_uLong i = 0; i < NUM_ENTRIES; i++)
535 CPPUNIT_ASSERT_MESSAGE
537 "test_move_to_same_position failed",
538 static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == i
542 CPPUNIT_ASSERT_MESSAGE
544 "test_move_to_same_position failed",
545 checkElementPositions(bparr)
548 releaseBigPtrArrayContent(bparr);
549 dumpBigPtrArray(bparr);
552 void test_replace_elements()
554 printMethodName("test_replace_elements\n");
556 BigPtrArray bparr;
558 fillBigPtrArray(bparr, NUM_ENTRIES);
559 dumpBigPtrArray(bparr);
561 for (sal_uLong i = 0, j = NUM_ENTRIES - 1; i < NUM_ENTRIES; i++, j--)
563 delete bparr[i];
564 bparr.Replace(i, new BigPtrEntryMock(j));
565 dumpBigPtrArray(bparr);
568 for (sal_uLong i = 0; i < NUM_ENTRIES; i++)
570 CPPUNIT_ASSERT_MESSAGE
572 "test_replace_elements failed",
573 static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (NUM_ENTRIES - i - 1)
577 CPPUNIT_ASSERT_MESSAGE
579 "test_replace_elements failed",
580 checkElementPositions(bparr)
583 releaseBigPtrArrayContent(bparr);
586 CPPUNIT_TEST_SUITE(BigPtrArrayUnittest);
587 CPPUNIT_TEST(test_ctor);
588 CPPUNIT_TEST(test_insert_entries_at_front);
589 CPPUNIT_TEST(test_insert_entries_in_the_middle);
590 CPPUNIT_TEST(test_insert_at_already_used_index);
591 CPPUNIT_TEST(test_insert_at_end);
592 CPPUNIT_TEST(test_remove_at_front);
593 CPPUNIT_TEST(test_remove_at_back);
594 CPPUNIT_TEST(test_remove_in_the_middle);
595 CPPUNIT_TEST(test_remove_multiple_elements_at_once);
596 CPPUNIT_TEST(test_remove_all_elements_at_once);
597 CPPUNIT_TEST(test_move_elements_from_lower_to_higher_pos);
598 CPPUNIT_TEST(test_move_elements_from_higher_to_lower_pos);
599 CPPUNIT_TEST(test_move_to_same_position);
600 CPPUNIT_TEST(test_replace_elements);
601 CPPUNIT_TEST_SUITE_END();
604 const char* START = "START: ";
605 const char* END = "END: ";
607 #if defined TIMELOG
609 class PerformanceTracer
611 public:
613 public:
614 PerformanceTracer(const string& methodName) :
615 startString_(START),
616 endString_(END)
618 startString_ += methodName;
619 endString_ += methodName;
622 ~PerformanceTracer()
626 private:
627 string startString_;
628 string endString_;
631 class BigPtrArrayPerformanceTest : public CppUnit::TestFixture
633 public:
634 BigPtrArrayPerformanceTest()
638 void test_insert_at_end_1000()
639 { test_insert_at_end("1000"); }
641 void test_insert_at_end_10000()
642 { test_insert_at_end("10000"); }
644 void test_insert_at_end_100000()
645 { test_insert_at_end("100000"); }
647 void test_insert_at_end_1000000()
648 { test_insert_at_end("1000000"); }
650 void test_insert_at_front_1000()
651 { test_insert_at_front("1000"); }
653 void test_insert_at_front_10000()
654 { test_insert_at_front("10000"); }
656 void test_insert_at_front_100000()
657 { test_insert_at_front("100000"); }
659 void test_insert_at_front_1000000()
660 { test_insert_at_front("1000000"); }
662 CPPUNIT_TEST_SUITE(BigPtrArrayPerformanceTest);
663 CPPUNIT_TEST(test_insert_at_end_1000);
664 CPPUNIT_TEST(test_insert_at_end_10000);
665 CPPUNIT_TEST(test_insert_at_end_100000);
666 CPPUNIT_TEST(test_insert_at_end_1000000);
667 CPPUNIT_TEST(test_insert_at_front_1000);
668 CPPUNIT_TEST(test_insert_at_front_10000);
669 CPPUNIT_TEST(test_insert_at_front_100000);
670 CPPUNIT_TEST(test_insert_at_front_1000000);
671 CPPUNIT_TEST_SUITE_END();
673 private:
674 void test_insert_at_end(const char* numElements)
676 OStringBuffer buff("test_insert_at_end ");
677 buff.append(numElements);
678 int n = atoi(numElements);
679 PerformanceTracer tracer(buff.getStr());
680 BigPtrArray bparr;
681 for (int i = 0; i < n; i++)
682 bparr.Insert(new BigPtrEntryMock(i), bparr.Count());
684 releaseBigPtrArrayContent(bparr);
687 void test_insert_at_front(const char* numElements)
689 OStringBuffer buff("test_insert_at_front ");
690 buff.append(numElements);
691 int n = atoi(numElements);
692 PerformanceTracer tracer(buff.getStr());
693 BigPtrArray bparr;
694 for (int i = 0; i < n; i++)
695 bparr.Insert(new BigPtrEntryMock(i), 0);
697 releaseBigPtrArrayContent(bparr);
701 #endif
703 // register test suites
704 CPPUNIT_TEST_SUITE_REGISTRATION(BigPtrArrayUnittest);
705 #ifdef TIMELOG
706 CPPUNIT_TEST_SUITE_REGISTRATION(BigPtrArrayPerformanceTest);
707 #endif
709 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */