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/.
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
37 namespace /* private */
39 const sal_uLong NUM_ENTRIES
= 10;
41 class BigPtrEntryMock
: public BigPtrEntry
44 explicit BigPtrEntryMock(sal_uLong count
) : count_(count
)
48 sal_uLong
getCount() const
53 sal_uLong
Position() const
62 void dumpBigPtrArray(const BigPtrArray
& bparr
)
67 void fillBigPtrArray(BigPtrArray
& bparr
, sal_uLong numEntries
)
69 for (sal_uLong i
= 0; i
< numEntries
; i
++)
70 bparr
.Insert(new BigPtrEntryMock(i
), bparr
.Count());
73 void printMethodName(const char* name
)
78 bool checkElementPositions(const BigPtrArray
& bparr
)
80 for (sal_uLong i
= 0; i
< bparr
.Count(); i
++)
82 if (static_cast<BigPtrEntryMock
*>(bparr
[i
])->Position() != i
)
88 void releaseBigPtrArrayContent(BigPtrArray
& bparr
)
90 for (sal_uLong i
= 0; i
< bparr
.Count(); i
++)
95 class BigPtrArrayUnittest
: public CppUnit::TestFixture
103 /** Test constructor/destructor
104 The size of the BigPtrArray
105 aka the 'Count' should be 0
110 printMethodName("test_ctor\n");
114 CPPUNIT_ASSERT_EQUAL_MESSAGE
116 "BigPtrArray ctor failed",
117 static_cast<sal_uLong
>(0), bparr
.Count()
121 void test_insert_entries_at_front()
123 printMethodName("test_insert_entries_at_front\n");
127 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
129 sal_uLong oldCount
= bparr
.Count();
130 bparr
.Insert(new BigPtrEntryMock(i
), 0);
131 CPPUNIT_ASSERT_EQUAL_MESSAGE
133 "test_insert_entries_at_front failed",
134 oldCount
+ 1, bparr
.Count()
138 for (sal_uLong i
= 0, j
= NUM_ENTRIES
- 1; i
< NUM_ENTRIES
; i
++, j
--)
140 CPPUNIT_ASSERT_EQUAL_MESSAGE
142 "test_insert_entries_at_front failed",
143 j
, static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
147 CPPUNIT_ASSERT_MESSAGE
149 "test_insert_entries_at_front failed",
150 checkElementPositions(bparr
)
153 releaseBigPtrArrayContent(bparr
);
154 dumpBigPtrArray(bparr
);
157 void test_insert_entries_in_the_middle()
159 printMethodName("test_insert_entries_in_the_middle\n");
163 fillBigPtrArray(bparr
, NUM_ENTRIES
);
164 dumpBigPtrArray(bparr
);
166 sal_uLong oldCount
= bparr
.Count();
168 bparr
.Insert(new BigPtrEntryMock(NUM_ENTRIES
), bparr
.Count() / 2);
170 CPPUNIT_ASSERT_MESSAGE
172 "test_insert_entries_in_the_middle failed",
173 (oldCount
+ 1 == bparr
.Count() && static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count() / 2])->getCount() == NUM_ENTRIES
)
176 CPPUNIT_ASSERT_MESSAGE
178 "test_insert_entries_in_the_middle failed",
179 checkElementPositions(bparr
)
182 releaseBigPtrArrayContent(bparr
);
183 dumpBigPtrArray(bparr
);
186 void test_insert_at_already_used_index()
188 printMethodName("test_insert_at_already_used_index\n");
192 fillBigPtrArray(bparr
, NUM_ENTRIES
);
193 dumpBigPtrArray(bparr
);
195 const sal_uLong oldCount
= bparr
.Count();
198 for (sal_uLong i
= 0, j
= 30; i
< 5; i
++, j
++)
199 bparr
.Insert(new BigPtrEntryMock(j
), i
);
201 CPPUNIT_ASSERT_EQUAL_MESSAGE
203 "test_insert_at_already_used_index failed",
204 oldCount
+ 5, bparr
.Count()
207 // now, first 5 elements have counts: 30,31,..34
208 // next 10 elements have counts: 0,1,..9
209 for (sal_uLong i
= 0, j
= 30; i
< bparr
.Count(); i
++, j
++)
211 CPPUNIT_ASSERT_EQUAL_MESSAGE
213 "test_insert_at_already_used_index failed",
214 (i
< 5 ? j
: i
- 5), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
218 CPPUNIT_ASSERT_MESSAGE
220 "test_insert_at_already_used_index failed",
221 checkElementPositions(bparr
)
224 releaseBigPtrArrayContent(bparr
);
225 dumpBigPtrArray(bparr
);
228 void test_insert_at_end()
230 printMethodName("test_insert_at_end\n");
234 fillBigPtrArray(bparr
, NUM_ENTRIES
);
235 dumpBigPtrArray(bparr
);
237 sal_uLong oldCount
= bparr
.Count();
238 bparr
.Insert(new BigPtrEntryMock(NUM_ENTRIES
), bparr
.Count());
240 CPPUNIT_ASSERT_MESSAGE
242 "test_insert_at_end failed",
243 (oldCount
+ 1 == bparr
.Count() && static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count()-1])->getCount() == NUM_ENTRIES
)
246 CPPUNIT_ASSERT_MESSAGE
248 "test_insert_at_end failed",
249 checkElementPositions(bparr
)
252 releaseBigPtrArrayContent(bparr
);
253 dumpBigPtrArray(bparr
);
256 void test_remove_at_front()
258 printMethodName("test_remove_at_front\n");
262 fillBigPtrArray(bparr
, NUM_ENTRIES
);
263 dumpBigPtrArray(bparr
);
265 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
267 sal_uLong oldCount
= bparr
.Count();
269 delete bparr
[0]; // release content
270 bparr
.Remove(0); // remove item from container
272 CPPUNIT_ASSERT_EQUAL_MESSAGE
274 "test_remove_at_front failed (wrong count)",
275 oldCount
- 1, bparr
.Count()
278 for (sal_uLong j
= 0, k
= i
+ 1; j
< bparr
.Count(); j
++, k
++)
280 CPPUNIT_ASSERT_EQUAL_MESSAGE
282 "test_remove_at_front failed",
283 k
, static_cast<BigPtrEntryMock
*>(bparr
[j
])->getCount()
287 CPPUNIT_ASSERT_MESSAGE
289 "test_remove_at_front failed",
290 checkElementPositions(bparr
)
293 dumpBigPtrArray(bparr
);
297 void test_remove_at_back()
299 printMethodName("test_remove_at_back\n");
303 fillBigPtrArray(bparr
, NUM_ENTRIES
);
304 dumpBigPtrArray(bparr
);
306 for (int i
= NUM_ENTRIES
- 1; i
>= 0; i
--)
308 sal_uLong oldCount
= bparr
.Count();
312 CPPUNIT_ASSERT_EQUAL_MESSAGE
314 "test_remove_at_back failed (wrong count)",
315 (oldCount
- 1), bparr
.Count()
318 for (sal_uLong j
= 0; j
< bparr
.Count(); j
++)
320 CPPUNIT_ASSERT_EQUAL_MESSAGE
322 "test_remove_at_back failed",
323 j
, static_cast<BigPtrEntryMock
*>(bparr
[j
])->getCount()
327 CPPUNIT_ASSERT_MESSAGE
329 "test_remove_at_back failed",
330 checkElementPositions(bparr
)
333 dumpBigPtrArray(bparr
);
337 void test_remove_in_the_middle()
339 printMethodName("test_remove_in_the_middle\n");
343 fillBigPtrArray(bparr
, NUM_ENTRIES
);
344 dumpBigPtrArray(bparr
);
346 while (bparr
.Count())
348 sal_uLong oldCount
= bparr
.Count();
349 sal_uLong oldElement
= static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count() / 2])->getCount();
351 delete bparr
[bparr
.Count() / 2];
352 bparr
.Remove(bparr
.Count() / 2);
354 CPPUNIT_ASSERT_EQUAL_MESSAGE
356 "test_remove_in_the_middle failed (wrong count)",
357 oldCount
- 1, bparr
.Count()
360 for (sal_uLong i
= 0; i
< bparr
.Count(); i
++)
362 CPPUNIT_ASSERT_MESSAGE
364 "test_remove_in_the_middle failed",
365 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() != oldElement
369 CPPUNIT_ASSERT_MESSAGE
371 "test_remove_in_the_middle failed",
372 checkElementPositions(bparr
)
375 dumpBigPtrArray(bparr
);
379 void test_remove_multiple_elements_at_once()
381 printMethodName("test_remove_multiple_elements_at_once\n");
385 fillBigPtrArray(bparr
, NUM_ENTRIES
);
386 dumpBigPtrArray(bparr
);
390 sal_uLong nRemove
= (bparr
.Count() > 3) ? 3 : bparr
.Count();
391 sal_uLong oldCount
= bparr
.Count();
393 for (sal_uLong i
= 0; i
< nRemove
; i
++)
396 bparr
.Remove(0, nRemove
);
398 CPPUNIT_ASSERT_EQUAL_MESSAGE
400 "test_remove_multiple_elements_at_once failed",
401 oldCount
- nRemove
, bparr
.Count()
404 CPPUNIT_ASSERT_MESSAGE
406 "test_remove_multiple_elements_at_once failed",
407 checkElementPositions(bparr
)
410 dumpBigPtrArray(bparr
);
414 void test_remove_all_elements_at_once()
416 printMethodName("test_remove_all_elements_at_once\n");
420 fillBigPtrArray(bparr
, NUM_ENTRIES
);
421 dumpBigPtrArray(bparr
);
423 releaseBigPtrArrayContent(bparr
);
424 bparr
.Remove(0, bparr
.Count());
426 CPPUNIT_ASSERT_EQUAL_MESSAGE
428 "test_remove_all_elements_at_once failed",
429 static_cast<sal_uLong
>(0), bparr
.Count()
432 dumpBigPtrArray(bparr
);
435 void test_move_elements_from_lower_to_higher_pos()
437 printMethodName("test_move_elements_from_lower_to_higher_pos\n");
441 fillBigPtrArray(bparr
, NUM_ENTRIES
);
442 dumpBigPtrArray(bparr
);
444 for (sal_uLong i
= 0; i
< NUM_ENTRIES
- 1; i
++)
446 bparr
.Move(i
, i
+ 2);
447 dumpBigPtrArray(bparr
);
450 for (sal_uLong i
= 0; i
< (NUM_ENTRIES
- 1); i
++)
452 CPPUNIT_ASSERT_EQUAL_MESSAGE
454 "test_move_elements_from_lower_to_higher_pos failed",
455 (i
+ 1), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
459 CPPUNIT_ASSERT_EQUAL_MESSAGE
461 "test_move_elements_from_lower_to_higher_pos failed",
462 static_cast<sal_uLong
>(0), static_cast<BigPtrEntryMock
*>(bparr
[NUM_ENTRIES
-1])->getCount()
465 CPPUNIT_ASSERT_MESSAGE
467 "test_move_elements_from_lower_to_higher_pos failed",
468 checkElementPositions(bparr
)
471 releaseBigPtrArrayContent(bparr
);
474 void test_move_elements_from_higher_to_lower_pos()
476 printMethodName("test_move_elements_from_higher_to_lower_pos\n");
480 fillBigPtrArray(bparr
, NUM_ENTRIES
);
481 dumpBigPtrArray(bparr
);
483 for (int i
= NUM_ENTRIES
- 1; i
>= 1; i
--)
485 bparr
.Move(i
, i
- 1);
486 dumpBigPtrArray(bparr
);
489 CPPUNIT_ASSERT_EQUAL_MESSAGE
491 "test_move_elements_from_higher_to_lower_pos failed",
492 (NUM_ENTRIES
- 1), static_cast<BigPtrEntryMock
*>(bparr
[0])->getCount()
495 for (sal_uLong i
= 1; i
< NUM_ENTRIES
; i
++)
497 CPPUNIT_ASSERT_EQUAL_MESSAGE
499 "test_move_elements_from_higher_to_lower_pos failed",
500 (i
- 1), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
504 CPPUNIT_ASSERT_MESSAGE
506 "test_move_elements_from_higher_to_lower_pos failed",
507 checkElementPositions(bparr
)
510 releaseBigPtrArrayContent(bparr
);
513 void test_move_to_same_position()
515 printMethodName("test_move_to_same_position\n");
519 fillBigPtrArray(bparr
, NUM_ENTRIES
);
520 dumpBigPtrArray(bparr
);
522 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
527 dumpBigPtrArray(bparr
);
529 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
531 CPPUNIT_ASSERT_EQUAL_MESSAGE
533 "test_move_to_same_position failed",
534 i
, static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
538 CPPUNIT_ASSERT_MESSAGE
540 "test_move_to_same_position failed",
541 checkElementPositions(bparr
)
544 releaseBigPtrArrayContent(bparr
);
545 dumpBigPtrArray(bparr
);
548 void test_replace_elements()
550 printMethodName("test_replace_elements\n");
554 fillBigPtrArray(bparr
, NUM_ENTRIES
);
555 dumpBigPtrArray(bparr
);
557 for (sal_uLong i
= 0, j
= NUM_ENTRIES
- 1; i
< NUM_ENTRIES
; i
++, j
--)
560 bparr
.Replace(i
, new BigPtrEntryMock(j
));
561 dumpBigPtrArray(bparr
);
564 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
566 CPPUNIT_ASSERT_EQUAL_MESSAGE
568 "test_replace_elements failed",
569 (NUM_ENTRIES
- i
- 1), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
573 CPPUNIT_ASSERT_MESSAGE
575 "test_replace_elements failed",
576 checkElementPositions(bparr
)
579 releaseBigPtrArrayContent(bparr
);
582 CPPUNIT_TEST_SUITE(BigPtrArrayUnittest
);
583 CPPUNIT_TEST(test_ctor
);
584 CPPUNIT_TEST(test_insert_entries_at_front
);
585 CPPUNIT_TEST(test_insert_entries_in_the_middle
);
586 CPPUNIT_TEST(test_insert_at_already_used_index
);
587 CPPUNIT_TEST(test_insert_at_end
);
588 CPPUNIT_TEST(test_remove_at_front
);
589 CPPUNIT_TEST(test_remove_at_back
);
590 CPPUNIT_TEST(test_remove_in_the_middle
);
591 CPPUNIT_TEST(test_remove_multiple_elements_at_once
);
592 CPPUNIT_TEST(test_remove_all_elements_at_once
);
593 CPPUNIT_TEST(test_move_elements_from_lower_to_higher_pos
);
594 CPPUNIT_TEST(test_move_elements_from_higher_to_lower_pos
);
595 CPPUNIT_TEST(test_move_to_same_position
);
596 CPPUNIT_TEST(test_replace_elements
);
597 CPPUNIT_TEST_SUITE_END();
602 const char* const START
= "START: ";
603 const char* const END
= "END: ";
605 class PerformanceTracer
610 explicit PerformanceTracer(const string
& methodName
) :
614 startString_
+= methodName
;
615 endString_
+= methodName
;
627 class BigPtrArrayPerformanceTest
: public CppUnit::TestFixture
630 BigPtrArrayPerformanceTest()
634 void test_insert_at_end_1000()
635 { test_insert_at_end("1000"); }
637 void test_insert_at_end_10000()
638 { test_insert_at_end("10000"); }
640 void test_insert_at_end_100000()
641 { test_insert_at_end("100000"); }
643 void test_insert_at_end_1000000()
644 { test_insert_at_end("1000000"); }
646 void test_insert_at_front_1000()
647 { test_insert_at_front("1000"); }
649 void test_insert_at_front_10000()
650 { test_insert_at_front("10000"); }
652 void test_insert_at_front_100000()
653 { test_insert_at_front("100000"); }
655 void test_insert_at_front_1000000()
656 { test_insert_at_front("1000000"); }
658 CPPUNIT_TEST_SUITE(BigPtrArrayPerformanceTest
);
659 CPPUNIT_TEST(test_insert_at_end_1000
);
660 CPPUNIT_TEST(test_insert_at_end_10000
);
661 CPPUNIT_TEST(test_insert_at_end_100000
);
662 CPPUNIT_TEST(test_insert_at_end_1000000
);
663 CPPUNIT_TEST(test_insert_at_front_1000
);
664 CPPUNIT_TEST(test_insert_at_front_10000
);
665 CPPUNIT_TEST(test_insert_at_front_100000
);
666 CPPUNIT_TEST(test_insert_at_front_1000000
);
667 CPPUNIT_TEST_SUITE_END();
670 void test_insert_at_end(const char* numElements
)
672 OStringBuffer
buff("test_insert_at_end ");
673 buff
.append(numElements
);
674 int n
= atoi(numElements
);
675 PerformanceTracer
tracer(buff
.getStr());
677 for (int i
= 0; i
< n
; i
++)
678 bparr
.Insert(new BigPtrEntryMock(i
), bparr
.Count());
680 releaseBigPtrArrayContent(bparr
);
683 void test_insert_at_front(const char* numElements
)
685 OStringBuffer
buff("test_insert_at_front ");
686 buff
.append(numElements
);
687 int n
= atoi(numElements
);
688 PerformanceTracer
tracer(buff
.getStr());
690 for (int i
= 0; i
< n
; i
++)
691 bparr
.Insert(new BigPtrEntryMock(i
), 0);
693 releaseBigPtrArrayContent(bparr
);
699 // register test suites
700 CPPUNIT_TEST_SUITE_REGISTRATION(BigPtrArrayUnittest
);
702 CPPUNIT_TEST_SUITE_REGISTRATION(BigPtrArrayPerformanceTest
);
705 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */