1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Test-BigPtrArray.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <cppunit/simpleheader.hxx>
39 #include <rtl/logfile.hxx>
41 #undef ENABLE_DEBUG_OUTPUT
46 namespace /* private */
48 const int NUM_ENTRIES
= 100000;
50 class BigPtrEntryMock
: public BigPtrEntry
53 BigPtrEntryMock(int count
) : count_(count
)
59 #ifdef ENABLE_DEBUG_OUTPUT
60 printf("Destructor called (%i)\n", count_
);
69 void setCount(int newCount
)
74 ULONG
Position() const
83 BOOL
AddToCount(const ElementPtr
& rElem
, void* pArgs
)
85 BigPtrEntryMock
* const pbem
= static_cast<BigPtrEntryMock
* const>(rElem
);
86 pbem
->setCount(pbem
->getCount() + *((int*)pArgs
));
90 void dumpBigPtrArray(const BigPtrArray
& bparr
)
92 #ifdef ENABLE_DEBUG_OUTPUT
93 for (int i
= 0; i
< bparr
.Count(); i
++)
94 printf("bparr[%i,%i]: %i\n", i
, static_cast<BigPtrEntryMock
*>(bparr
[i
])->Position(), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount());
100 void fillBigPtrArray(BigPtrArray
& bparr
, ULONG numEntries
)
102 for (int i
= 0; i
< numEntries
; i
++)
103 bparr
.Insert(new BigPtrEntryMock(i
), bparr
.Count());
106 void printMethodName(const char* name
)
108 #ifdef ENABLE_DEBUG_OUTPUT
113 bool checkElementPositions(const BigPtrArray
& bparr
)
115 for (int i
= 0; i
< bparr
.Count(); i
++)
117 if (static_cast<BigPtrEntryMock
*>(bparr
[i
])->Position() != i
)
123 void releaseBigPtrArrayContent(BigPtrArray
& bparr
)
125 for (int i
= 0; i
< bparr
.Count(); i
++)
129 RTL_LOGFILE_CONTEXT(logFile
, "BigPtrArray performance measures" );
132 class BigPtrArrayUnittest
: public CppUnit::TestFixture
136 BigPtrArrayUnittest()
140 /** Test constructor/destructor
141 The size of the BigPtrArray
142 aka the 'Count' should be 0
147 printMethodName("test_ctor\n");
151 CPPUNIT_ASSERT_MESSAGE
153 "BigPtrArray ctor failed",
158 void test_insert_entries_at_front()
160 printMethodName("test_insert_entries_at_front\n");
162 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "START: test_insert_entries_at_front");
166 for (int i
= 0; i
< NUM_ENTRIES
; i
++)
168 ULONG oldCount
= bparr
.Count();
169 bparr
.Insert(new BigPtrEntryMock(i
), 0);
170 CPPUNIT_ASSERT_MESSAGE
172 "test_insert_entries_at_front failed",
173 (bparr
.Count() == oldCount
+ 1)
177 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "END: test_insert_entries_at_front");
179 for (int i
= 0, j
= NUM_ENTRIES
- 1; i
< NUM_ENTRIES
; i
++, j
--)
181 CPPUNIT_ASSERT_MESSAGE
183 "test_insert_entries_at_front failed",
184 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == j
188 CPPUNIT_ASSERT_MESSAGE
190 "test_insert_entries_at_front failed",
191 checkElementPositions(bparr
)
194 releaseBigPtrArrayContent(bparr
);
195 dumpBigPtrArray(bparr
);
198 void test_insert_entries_in_the_middle()
200 printMethodName("test_insert_entries_in_the_middle\n");
202 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "START: test_insert_entries_in_the_middle");
206 fillBigPtrArray(bparr
, NUM_ENTRIES
);
207 dumpBigPtrArray(bparr
);
209 ULONG oldCount
= bparr
.Count();
211 bparr
.Insert(new BigPtrEntryMock(NUM_ENTRIES
), bparr
.Count() / 2);
213 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "END: test_insert_entries_in_the_middle");
215 CPPUNIT_ASSERT_MESSAGE
217 "test_insert_entries_in_the_middle failed",
218 (oldCount
+ 1 == bparr
.Count() && static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count() / 2])->getCount() == NUM_ENTRIES
)
221 CPPUNIT_ASSERT_MESSAGE
223 "test_insert_entries_in_the_middle failed",
224 checkElementPositions(bparr
)
227 releaseBigPtrArrayContent(bparr
);
228 dumpBigPtrArray(bparr
);
231 void test_insert_at_already_used_index()
233 printMethodName("test_insert_at_already_used_index\n");
235 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "START: test_insert_at_already_used_index");
239 fillBigPtrArray(bparr
, NUM_ENTRIES
);
240 dumpBigPtrArray(bparr
);
242 int oldCount
= bparr
.Count();
244 for (int i
= 0, j
= -5; i
< 5; i
++, j
++)
245 bparr
.Insert(new BigPtrEntryMock(j
), i
);
247 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "END: test_insert_at_already_used_index");
249 CPPUNIT_ASSERT_MESSAGE
251 "test_insert_at_already_used_index failed",
252 (oldCount
+ 5 == bparr
.Count())
255 for (int i
= 0, j
= -5; i
< bparr
.Count(); i
++, j
++)
257 CPPUNIT_ASSERT_MESSAGE
259 "test_insert_at_already_used_index failed",
260 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == j
264 CPPUNIT_ASSERT_MESSAGE
266 "test_insert_at_already_used_index failed",
267 checkElementPositions(bparr
)
270 releaseBigPtrArrayContent(bparr
);
271 dumpBigPtrArray(bparr
);
274 void test_insert_at_end()
276 printMethodName("test_insert_at_end\n");
280 fillBigPtrArray(bparr
, NUM_ENTRIES
);
281 dumpBigPtrArray(bparr
);
283 ULONG oldCount
= bparr
.Count();
284 bparr
.Insert(new BigPtrEntryMock(NUM_ENTRIES
), bparr
.Count());
286 CPPUNIT_ASSERT_MESSAGE
288 "test_insert_at_end failed",
289 (oldCount
+ 1 == bparr
.Count() && static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count()-1])->getCount() == NUM_ENTRIES
)
292 CPPUNIT_ASSERT_MESSAGE
294 "test_insert_at_end failed",
295 checkElementPositions(bparr
)
298 releaseBigPtrArrayContent(bparr
);
299 dumpBigPtrArray(bparr
);
302 void test_remove_at_front()
304 printMethodName("test_remove_at_front\n");
306 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "START: test_remove_at_front");
310 fillBigPtrArray(bparr
, NUM_ENTRIES
);
311 dumpBigPtrArray(bparr
);
313 for (int i
= 0; i
< NUM_ENTRIES
; i
++)
315 ULONG oldCount
= bparr
.Count();
317 delete bparr
[0]; // release content
318 bparr
.Remove(0); // remove item from container
320 CPPUNIT_ASSERT_MESSAGE
322 "test_remove_at_front failed (wrong count)",
323 (oldCount
- 1 == bparr
.Count())
326 for (int j
= 0, k
= i
+ 1; j
< bparr
.Count(); j
++, k
++)
328 CPPUNIT_ASSERT_MESSAGE
330 "test_remove_at_front failed",
331 static_cast<BigPtrEntryMock
*>(bparr
[j
])->getCount() == k
335 CPPUNIT_ASSERT_MESSAGE
337 "test_remove_at_front failed",
338 checkElementPositions(bparr
)
341 dumpBigPtrArray(bparr
);
344 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "END: test_remove_at_front");
347 void test_remove_at_back()
349 printMethodName("test_remove_at_back\n");
353 fillBigPtrArray(bparr
, NUM_ENTRIES
);
354 dumpBigPtrArray(bparr
);
356 for (int i
= NUM_ENTRIES
- 1; i
>= 0; i
--)
358 ULONG oldCount
= bparr
.Count();
362 CPPUNIT_ASSERT_MESSAGE
364 "test_remove_at_back failed (wrong count)",
365 (oldCount
- 1 == bparr
.Count())
368 for (int j
= 0; j
< bparr
.Count(); j
++)
370 CPPUNIT_ASSERT_MESSAGE
372 "test_remove_at_back failed",
373 static_cast<BigPtrEntryMock
*>(bparr
[j
])->getCount() == j
377 CPPUNIT_ASSERT_MESSAGE
379 "test_remove_at_back failed",
380 checkElementPositions(bparr
)
383 dumpBigPtrArray(bparr
);
387 void test_remove_in_the_middle()
389 printMethodName("test_remove_in_the_middle\n");
391 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "START: test_remove_in_the_middle");
395 fillBigPtrArray(bparr
, NUM_ENTRIES
);
396 dumpBigPtrArray(bparr
);
398 while (bparr
.Count())
400 ULONG oldCount
= bparr
.Count();
401 int oldElement
= static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count() / 2])->getCount();
403 delete bparr
[bparr
.Count() / 2];
404 bparr
.Remove(bparr
.Count() / 2);
406 CPPUNIT_ASSERT_MESSAGE
408 "test_remove_in_the_middle failed (wrong count)",
409 (oldCount
- 1 == bparr
.Count())
412 for (int i
= 0; i
< bparr
.Count(); i
++)
414 CPPUNIT_ASSERT_MESSAGE
416 "test_remove_in_the_middle failed",
417 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() != oldElement
421 CPPUNIT_ASSERT_MESSAGE
423 "test_remove_in_the_middle failed",
424 checkElementPositions(bparr
)
427 dumpBigPtrArray(bparr
);
429 RTL_LOGFILE_CONTEXT_TRACE(logFile
, "END: test_remove_in_the_middle");
432 void test_remove_multiple_elements_at_once()
434 printMethodName("test_remove_multiple_elements_at_once\n");
438 fillBigPtrArray(bparr
, NUM_ENTRIES
);
439 dumpBigPtrArray(bparr
);
443 ULONG nRemove
= (bparr
.Count() > 3) ? 3 : bparr
.Count();
444 ULONG oldCount
= bparr
.Count();
446 for (int i
= 0; i
< nRemove
; i
++)
449 bparr
.Remove(0, nRemove
);
451 CPPUNIT_ASSERT_MESSAGE
453 "test_remove_multiple_elements_at_once failed",
454 (oldCount
- nRemove
== bparr
.Count())
457 CPPUNIT_ASSERT_MESSAGE
459 "test_remove_multiple_elements_at_once failed",
460 checkElementPositions(bparr
)
463 dumpBigPtrArray(bparr
);
467 void test_remove_all_elements_at_once()
469 printMethodName("test_remove_all_elements_at_once\n");
473 fillBigPtrArray(bparr
, NUM_ENTRIES
);
474 dumpBigPtrArray(bparr
);
476 releaseBigPtrArrayContent(bparr
);
477 bparr
.Remove(0, bparr
.Count());
479 CPPUNIT_ASSERT_MESSAGE
481 "test_remove_all_elements_at_once failed",
485 dumpBigPtrArray(bparr
);
488 void test_move_elements_from_lower_to_higher_pos()
490 printMethodName("test_move_elements_from_lower_to_higher_pos\n");
494 fillBigPtrArray(bparr
, NUM_ENTRIES
);
495 dumpBigPtrArray(bparr
);
497 for (int i
= 0; i
< NUM_ENTRIES
- 1; i
++)
499 bparr
.Move(i
, i
+ 2);
500 dumpBigPtrArray(bparr
);
503 for (int i
= 0; i
< (NUM_ENTRIES
- 1); i
++)
505 CPPUNIT_ASSERT_MESSAGE
507 "test_move_elements_from_lower_to_higher_pos failed",
508 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == (i
+ 1)
512 CPPUNIT_ASSERT_MESSAGE
514 "test_move_elements_from_lower_to_higher_pos failed",
515 static_cast<BigPtrEntryMock
*>(bparr
[NUM_ENTRIES
-1])->getCount() == 0
518 CPPUNIT_ASSERT_MESSAGE
520 "test_move_elements_from_lower_to_higher_pos failed",
521 checkElementPositions(bparr
)
524 releaseBigPtrArrayContent(bparr
);
527 void test_move_elements_from_higher_to_lower_pos()
529 printMethodName("test_move_elements_from_higher_to_lower_pos\n");
533 fillBigPtrArray(bparr
, NUM_ENTRIES
);
534 dumpBigPtrArray(bparr
);
536 for (int i
= NUM_ENTRIES
- 1; i
>= 1; i
--)
538 bparr
.Move(i
, i
- 1);
539 dumpBigPtrArray(bparr
);
542 CPPUNIT_ASSERT_MESSAGE
544 "test_move_elements_from_higher_to_lower_pos failed",
545 static_cast<BigPtrEntryMock
*>(bparr
[0])->getCount() == (NUM_ENTRIES
- 1)
548 for (int i
= 1; i
< NUM_ENTRIES
; i
++)
550 CPPUNIT_ASSERT_MESSAGE
552 "test_move_elements_from_higher_to_lower_pos failed",
553 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == (i
- 1)
557 CPPUNIT_ASSERT_MESSAGE
559 "test_move_elements_from_higher_to_lower_pos failed",
560 checkElementPositions(bparr
)
563 releaseBigPtrArrayContent(bparr
);
566 void test_move_to_same_position()
568 printMethodName("test_move_to_same_position\n");
572 fillBigPtrArray(bparr
, NUM_ENTRIES
);
573 dumpBigPtrArray(bparr
);
575 for (int i
= 0; i
< NUM_ENTRIES
; i
++)
580 dumpBigPtrArray(bparr
);
582 for (int i
= 0; i
< NUM_ENTRIES
; i
++)
584 CPPUNIT_ASSERT_MESSAGE
586 "test_move_to_same_position failed",
587 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == i
591 CPPUNIT_ASSERT_MESSAGE
593 "test_move_to_same_position failed",
594 checkElementPositions(bparr
)
597 releaseBigPtrArrayContent(bparr
);
598 dumpBigPtrArray(bparr
);
601 void test_replace_elements()
603 printMethodName("test_replace_elements\n");
607 fillBigPtrArray(bparr
, NUM_ENTRIES
);
608 dumpBigPtrArray(bparr
);
610 for (int i
= 0, j
= NUM_ENTRIES
- 1; i
< NUM_ENTRIES
; i
++, j
--)
613 bparr
.Replace(i
, new BigPtrEntryMock(j
));
614 dumpBigPtrArray(bparr
);
617 for (int i
= 0; i
< NUM_ENTRIES
; i
++)
619 CPPUNIT_ASSERT_MESSAGE
621 "test_replace_elements failed",
622 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == (NUM_ENTRIES
- i
- 1)
626 CPPUNIT_ASSERT_MESSAGE
628 "test_replace_elements failed",
629 checkElementPositions(bparr
)
632 releaseBigPtrArrayContent(bparr
);
637 printMethodName("test_for_each\n");
641 fillBigPtrArray(bparr
, NUM_ENTRIES
);
642 dumpBigPtrArray(bparr
);
645 bparr
.ForEach(AddToCount
, &addCount
);
647 for (int i
= 0; i
< NUM_ENTRIES
; i
++)
649 CPPUNIT_ASSERT_MESSAGE
651 "test_for_each failed",
652 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == (i
+1)
656 releaseBigPtrArrayContent(bparr
);
657 dumpBigPtrArray(bparr
);
660 void test_for_some1()
662 printMethodName("test_for_some1\n");
666 fillBigPtrArray(bparr
, NUM_ENTRIES
);
667 dumpBigPtrArray(bparr
);
670 bparr
.ForEach(0, NUM_ENTRIES
/ 2, AddToCount
, &addCount
);
673 for (/* */; i
< NUM_ENTRIES
/ 2; i
++)
675 CPPUNIT_ASSERT_MESSAGE
677 "test_for_some1 failed",
678 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == (i
+1)
682 for (/* */; i
< NUM_ENTRIES
; i
++)
684 CPPUNIT_ASSERT_MESSAGE
686 "test_for_some1 failed",
687 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == (i
)
691 releaseBigPtrArrayContent(bparr
);
692 dumpBigPtrArray(bparr
);
695 void test_for_some2()
697 printMethodName("test_for_some2\n");
701 fillBigPtrArray(bparr
, NUM_ENTRIES
);
702 dumpBigPtrArray(bparr
);
705 bparr
.ForEach(NUM_ENTRIES
/ 2, NUM_ENTRIES
, AddToCount
, &addCount
);
708 for (/* */; i
< NUM_ENTRIES
/ 2; i
++)
710 CPPUNIT_ASSERT_MESSAGE
712 "test_for_some2 failed",
713 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == (i
)
717 for (/* */; i
< NUM_ENTRIES
; i
++)
719 CPPUNIT_ASSERT_MESSAGE
721 "test_for_some2 failed",
722 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == (i
+1)
726 releaseBigPtrArrayContent(bparr
);
727 dumpBigPtrArray(bparr
);
730 void test_for_some3()
732 printMethodName("test_for_some3\n");
736 fillBigPtrArray(bparr
, NUM_ENTRIES
);
737 dumpBigPtrArray(bparr
);
740 bparr
.ForEach(0, 0, AddToCount
, &addCount
);
742 for (int i
= 0; i
< NUM_ENTRIES
; i
++)
744 CPPUNIT_ASSERT_MESSAGE
746 "test_for_some3 failed",
747 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() == i
750 releaseBigPtrArrayContent(bparr
);
753 CPPUNIT_TEST_SUITE(BigPtrArrayUnittest
);
754 CPPUNIT_TEST(test_ctor
);
755 CPPUNIT_TEST(test_insert_entries_at_front
);
756 CPPUNIT_TEST(test_insert_entries_in_the_middle
);
757 CPPUNIT_TEST(test_insert_at_already_used_index
);
758 CPPUNIT_TEST(test_insert_at_end
);
759 CPPUNIT_TEST(test_remove_at_front
);
760 CPPUNIT_TEST(test_remove_at_back
);
761 CPPUNIT_TEST(test_remove_in_the_middle
);
762 CPPUNIT_TEST(test_remove_multiple_elements_at_once
);
763 CPPUNIT_TEST(test_remove_all_elements_at_once
);
764 CPPUNIT_TEST(test_move_elements_from_lower_to_higher_pos
);
765 CPPUNIT_TEST(test_move_elements_from_higher_to_lower_pos
);
766 CPPUNIT_TEST(test_replace_elements
);
767 CPPUNIT_TEST(test_for_each
);
768 CPPUNIT_TEST(test_for_some1
);
769 CPPUNIT_TEST(test_for_some2
);
770 CPPUNIT_TEST(test_for_some3
);
771 CPPUNIT_TEST_SUITE_END();
774 const char* START
= "START: ";
775 const char* END
= "END: ";
777 class PerformanceTracer
782 PerformanceTracer(const string
& methodName
) :
786 startString_
+= methodName
;
787 endString_
+= methodName
;
788 RTL_LOGFILE_CONTEXT_TRACE(logFile
, startString_
.c_str());
793 RTL_LOGFILE_CONTEXT_TRACE(logFile
, endString_
.c_str());
801 class BigPtrArrayPerformanceTest
: public CppUnit::TestFixture
804 BigPtrArrayPerformanceTest()
808 void test_insert_at_end_1000()
809 { test_insert_at_end("1000"); }
811 void test_insert_at_end_10000()
812 { test_insert_at_end("10000"); }
814 void test_insert_at_end_100000()
815 { test_insert_at_end("100000"); }
817 void test_insert_at_end_1000000()
818 { test_insert_at_end("1000000"); }
820 void test_insert_at_front_1000()
821 { test_insert_at_front("1000"); }
823 void test_insert_at_front_10000()
824 { test_insert_at_front("10000"); }
826 void test_insert_at_front_100000()
827 { test_insert_at_front("100000"); }
829 void test_insert_at_front_1000000()
830 { test_insert_at_front("1000000"); }
832 CPPUNIT_TEST_SUITE(BigPtrArrayPerformanceTest
);
833 CPPUNIT_TEST(test_insert_at_end_1000
);
834 CPPUNIT_TEST(test_insert_at_end_10000
);
835 CPPUNIT_TEST(test_insert_at_end_100000
);
836 CPPUNIT_TEST(test_insert_at_end_1000000
);
837 CPPUNIT_TEST(test_insert_at_front_1000
);
838 CPPUNIT_TEST(test_insert_at_front_10000
);
839 CPPUNIT_TEST(test_insert_at_front_100000
);
840 CPPUNIT_TEST(test_insert_at_front_1000000
);
841 CPPUNIT_TEST_SUITE_END();
844 void test_insert_at_end(const char* numElements
)
846 char buff
[100] = { 0 };
847 strcat(buff
, "test_insert_at_end ");
848 strcat(buff
, numElements
);
849 int n
= atoi(numElements
);
850 PerformanceTracer
tracer(buff
);
852 for (int i
= 0; i
< n
; i
++)
853 bparr
.Insert(new BigPtrEntryMock(i
), bparr
.Count());
855 releaseBigPtrArrayContent(bparr
);
858 void test_insert_at_front(const char* numElements
)
860 char buff
[100] = { 0 };
861 strcat(buff
, "test_insert_at_front ");
862 strcat(buff
, numElements
);
863 int n
= atoi(numElements
);
864 PerformanceTracer
tracer(buff
);
866 for (int i
= 0; i
< n
; i
++)
867 bparr
.Insert(new BigPtrEntryMock(i
), 0);
869 releaseBigPtrArrayContent(bparr
);
874 //#####################################
875 // register test suites
876 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BigPtrArrayUnittest
, "BigPtrArrayUnittest");
877 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BigPtrArrayPerformanceTest
, "BigPtrArrayPerformanceTest");