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 fillBigPtrArray(BigPtrArray
& bparr
, sal_uLong numEntries
)
64 for (sal_uLong i
= 0; i
< numEntries
; i
++)
65 bparr
.Insert(new BigPtrEntryMock(i
), bparr
.Count());
68 bool checkElementPositions(const BigPtrArray
& bparr
)
70 for (sal_uLong i
= 0; i
< bparr
.Count(); i
++)
72 if (static_cast<BigPtrEntryMock
*>(bparr
[i
])->Position() != i
)
78 void releaseBigPtrArrayContent(BigPtrArray
const & bparr
)
80 for (sal_uLong i
= 0; i
< bparr
.Count(); i
++)
85 class BigPtrArrayUnittest
: public CppUnit::TestFixture
93 /** Test constructor/destructor
94 The size of the BigPtrArray
95 aka the 'Count' should be 0
102 CPPUNIT_ASSERT_EQUAL_MESSAGE
104 "BigPtrArray ctor failed",
105 static_cast<sal_uLong
>(0), bparr
.Count()
109 void test_insert_entries_at_front()
113 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
115 sal_uLong oldCount
= bparr
.Count();
116 bparr
.Insert(new BigPtrEntryMock(i
), 0);
117 CPPUNIT_ASSERT_EQUAL_MESSAGE
119 "test_insert_entries_at_front failed",
120 oldCount
+ 1, bparr
.Count()
124 for (sal_uLong i
= 0, j
= NUM_ENTRIES
- 1; i
< NUM_ENTRIES
; i
++, j
--)
126 CPPUNIT_ASSERT_EQUAL_MESSAGE
128 "test_insert_entries_at_front failed",
129 j
, static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
133 CPPUNIT_ASSERT_MESSAGE
135 "test_insert_entries_at_front failed",
136 checkElementPositions(bparr
)
139 releaseBigPtrArrayContent(bparr
);
142 void test_insert_entries_in_the_middle()
146 fillBigPtrArray(bparr
, NUM_ENTRIES
);
148 sal_uLong oldCount
= bparr
.Count();
150 bparr
.Insert(new BigPtrEntryMock(NUM_ENTRIES
), bparr
.Count() / 2);
152 CPPUNIT_ASSERT_MESSAGE
154 "test_insert_entries_in_the_middle failed",
155 (oldCount
+ 1 == bparr
.Count() && static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count() / 2])->getCount() == NUM_ENTRIES
)
158 CPPUNIT_ASSERT_MESSAGE
160 "test_insert_entries_in_the_middle failed",
161 checkElementPositions(bparr
)
164 releaseBigPtrArrayContent(bparr
);
167 void test_insert_at_already_used_index()
171 fillBigPtrArray(bparr
, NUM_ENTRIES
);
173 const sal_uLong oldCount
= bparr
.Count();
176 for (sal_uLong i
= 0, j
= 30; i
< 5; i
++, j
++)
177 bparr
.Insert(new BigPtrEntryMock(j
), i
);
179 CPPUNIT_ASSERT_EQUAL_MESSAGE
181 "test_insert_at_already_used_index failed",
182 oldCount
+ 5, bparr
.Count()
185 // now, first 5 elements have counts: 30,31,..34
186 // next 10 elements have counts: 0,1,..9
187 for (sal_uLong i
= 0, j
= 30; i
< bparr
.Count(); i
++, j
++)
189 CPPUNIT_ASSERT_EQUAL_MESSAGE
191 "test_insert_at_already_used_index failed",
192 (i
< 5 ? j
: i
- 5), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
196 CPPUNIT_ASSERT_MESSAGE
198 "test_insert_at_already_used_index failed",
199 checkElementPositions(bparr
)
202 releaseBigPtrArrayContent(bparr
);
205 void test_insert_at_end()
209 fillBigPtrArray(bparr
, NUM_ENTRIES
);
211 sal_uLong oldCount
= bparr
.Count();
212 bparr
.Insert(new BigPtrEntryMock(NUM_ENTRIES
), bparr
.Count());
214 CPPUNIT_ASSERT_MESSAGE
216 "test_insert_at_end failed",
217 (oldCount
+ 1 == bparr
.Count() && static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count()-1])->getCount() == NUM_ENTRIES
)
220 CPPUNIT_ASSERT_MESSAGE
222 "test_insert_at_end failed",
223 checkElementPositions(bparr
)
226 releaseBigPtrArrayContent(bparr
);
229 void test_remove_at_front()
233 fillBigPtrArray(bparr
, NUM_ENTRIES
);
235 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
237 sal_uLong oldCount
= bparr
.Count();
239 delete bparr
[0]; // release content
240 bparr
.Remove(0); // remove item from container
242 CPPUNIT_ASSERT_EQUAL_MESSAGE
244 "test_remove_at_front failed (wrong count)",
245 oldCount
- 1, bparr
.Count()
248 for (sal_uLong j
= 0, k
= i
+ 1; j
< bparr
.Count(); j
++, k
++)
250 CPPUNIT_ASSERT_EQUAL_MESSAGE
252 "test_remove_at_front failed",
253 k
, static_cast<BigPtrEntryMock
*>(bparr
[j
])->getCount()
257 CPPUNIT_ASSERT_MESSAGE
259 "test_remove_at_front failed",
260 checkElementPositions(bparr
)
265 void test_remove_at_back()
269 fillBigPtrArray(bparr
, NUM_ENTRIES
);
271 for (int i
= NUM_ENTRIES
- 1; i
>= 0; i
--)
273 sal_uLong oldCount
= bparr
.Count();
277 CPPUNIT_ASSERT_EQUAL_MESSAGE
279 "test_remove_at_back failed (wrong count)",
280 (oldCount
- 1), bparr
.Count()
283 for (sal_uLong j
= 0; j
< bparr
.Count(); j
++)
285 CPPUNIT_ASSERT_EQUAL_MESSAGE
287 "test_remove_at_back failed",
288 j
, static_cast<BigPtrEntryMock
*>(bparr
[j
])->getCount()
292 CPPUNIT_ASSERT_MESSAGE
294 "test_remove_at_back failed",
295 checkElementPositions(bparr
)
300 void test_remove_in_the_middle()
304 fillBigPtrArray(bparr
, NUM_ENTRIES
);
306 while (bparr
.Count())
308 sal_uLong oldCount
= bparr
.Count();
309 sal_uLong oldElement
= static_cast<BigPtrEntryMock
*>(bparr
[bparr
.Count() / 2])->getCount();
311 delete bparr
[bparr
.Count() / 2];
312 bparr
.Remove(bparr
.Count() / 2);
314 CPPUNIT_ASSERT_EQUAL_MESSAGE
316 "test_remove_in_the_middle failed (wrong count)",
317 oldCount
- 1, bparr
.Count()
320 for (sal_uLong i
= 0; i
< bparr
.Count(); i
++)
322 CPPUNIT_ASSERT_MESSAGE
324 "test_remove_in_the_middle failed",
325 static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount() != oldElement
329 CPPUNIT_ASSERT_MESSAGE
331 "test_remove_in_the_middle failed",
332 checkElementPositions(bparr
)
337 void test_remove_multiple_elements_at_once()
341 fillBigPtrArray(bparr
, NUM_ENTRIES
);
345 sal_uLong nRemove
= std::min
<sal_uLong
>(bparr
.Count(), 3);
346 sal_uLong oldCount
= bparr
.Count();
348 for (sal_uLong i
= 0; i
< nRemove
; i
++)
351 bparr
.Remove(0, nRemove
);
353 CPPUNIT_ASSERT_EQUAL_MESSAGE
355 "test_remove_multiple_elements_at_once failed",
356 oldCount
- nRemove
, bparr
.Count()
359 CPPUNIT_ASSERT_MESSAGE
361 "test_remove_multiple_elements_at_once failed",
362 checkElementPositions(bparr
)
367 void test_remove_all_elements_at_once()
371 fillBigPtrArray(bparr
, NUM_ENTRIES
);
373 releaseBigPtrArrayContent(bparr
);
374 bparr
.Remove(0, bparr
.Count());
376 CPPUNIT_ASSERT_EQUAL_MESSAGE
378 "test_remove_all_elements_at_once failed",
379 static_cast<sal_uLong
>(0), bparr
.Count()
383 void test_move_elements_from_lower_to_higher_pos()
387 fillBigPtrArray(bparr
, NUM_ENTRIES
);
389 for (sal_uLong i
= 0; i
< NUM_ENTRIES
- 1; i
++)
391 bparr
.Move(i
, i
+ 2);
394 for (sal_uLong i
= 0; i
< (NUM_ENTRIES
- 1); i
++)
396 CPPUNIT_ASSERT_EQUAL_MESSAGE
398 "test_move_elements_from_lower_to_higher_pos failed",
399 (i
+ 1), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
403 CPPUNIT_ASSERT_EQUAL_MESSAGE
405 "test_move_elements_from_lower_to_higher_pos failed",
406 static_cast<sal_uLong
>(0), static_cast<BigPtrEntryMock
*>(bparr
[NUM_ENTRIES
-1])->getCount()
409 CPPUNIT_ASSERT_MESSAGE
411 "test_move_elements_from_lower_to_higher_pos failed",
412 checkElementPositions(bparr
)
415 releaseBigPtrArrayContent(bparr
);
418 void test_move_elements_from_higher_to_lower_pos()
422 fillBigPtrArray(bparr
, NUM_ENTRIES
);
424 for (int i
= NUM_ENTRIES
- 1; i
>= 1; i
--)
426 bparr
.Move(i
, i
- 1);
429 CPPUNIT_ASSERT_EQUAL_MESSAGE
431 "test_move_elements_from_higher_to_lower_pos failed",
432 (NUM_ENTRIES
- 1), static_cast<BigPtrEntryMock
*>(bparr
[0])->getCount()
435 for (sal_uLong i
= 1; i
< NUM_ENTRIES
; i
++)
437 CPPUNIT_ASSERT_EQUAL_MESSAGE
439 "test_move_elements_from_higher_to_lower_pos failed",
440 (i
- 1), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
444 CPPUNIT_ASSERT_MESSAGE
446 "test_move_elements_from_higher_to_lower_pos failed",
447 checkElementPositions(bparr
)
450 releaseBigPtrArrayContent(bparr
);
453 void test_move_to_same_position()
457 fillBigPtrArray(bparr
, NUM_ENTRIES
);
459 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
464 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
466 CPPUNIT_ASSERT_EQUAL_MESSAGE
468 "test_move_to_same_position failed",
469 i
, static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
473 CPPUNIT_ASSERT_MESSAGE
475 "test_move_to_same_position failed",
476 checkElementPositions(bparr
)
479 releaseBigPtrArrayContent(bparr
);
482 void test_replace_elements()
486 fillBigPtrArray(bparr
, NUM_ENTRIES
);
488 for (sal_uLong i
= 0, j
= NUM_ENTRIES
- 1; i
< NUM_ENTRIES
; i
++, j
--)
491 bparr
.Replace(i
, new BigPtrEntryMock(j
));
494 for (sal_uLong i
= 0; i
< NUM_ENTRIES
; i
++)
496 CPPUNIT_ASSERT_EQUAL_MESSAGE
498 "test_replace_elements failed",
499 (NUM_ENTRIES
- i
- 1), static_cast<BigPtrEntryMock
*>(bparr
[i
])->getCount()
503 CPPUNIT_ASSERT_MESSAGE
505 "test_replace_elements failed",
506 checkElementPositions(bparr
)
509 releaseBigPtrArrayContent(bparr
);
512 CPPUNIT_TEST_SUITE(BigPtrArrayUnittest
);
513 CPPUNIT_TEST(test_ctor
);
514 CPPUNIT_TEST(test_insert_entries_at_front
);
515 CPPUNIT_TEST(test_insert_entries_in_the_middle
);
516 CPPUNIT_TEST(test_insert_at_already_used_index
);
517 CPPUNIT_TEST(test_insert_at_end
);
518 CPPUNIT_TEST(test_remove_at_front
);
519 CPPUNIT_TEST(test_remove_at_back
);
520 CPPUNIT_TEST(test_remove_in_the_middle
);
521 CPPUNIT_TEST(test_remove_multiple_elements_at_once
);
522 CPPUNIT_TEST(test_remove_all_elements_at_once
);
523 CPPUNIT_TEST(test_move_elements_from_lower_to_higher_pos
);
524 CPPUNIT_TEST(test_move_elements_from_higher_to_lower_pos
);
525 CPPUNIT_TEST(test_move_to_same_position
);
526 CPPUNIT_TEST(test_replace_elements
);
527 CPPUNIT_TEST_SUITE_END();
532 const char* const START
= "START: ";
533 const char* const END
= "END: ";
535 class PerformanceTracer
540 explicit PerformanceTracer(const string
& methodName
) :
544 startString_
+= methodName
;
545 endString_
+= methodName
;
557 class BigPtrArrayPerformanceTest
: public CppUnit::TestFixture
560 BigPtrArrayPerformanceTest()
564 void test_insert_at_end_1000()
565 { test_insert_at_end("1000"); }
567 void test_insert_at_end_10000()
568 { test_insert_at_end("10000"); }
570 void test_insert_at_end_100000()
571 { test_insert_at_end("100000"); }
573 void test_insert_at_end_1000000()
574 { test_insert_at_end("1000000"); }
576 void test_insert_at_front_1000()
577 { test_insert_at_front("1000"); }
579 void test_insert_at_front_10000()
580 { test_insert_at_front("10000"); }
582 void test_insert_at_front_100000()
583 { test_insert_at_front("100000"); }
585 void test_insert_at_front_1000000()
586 { test_insert_at_front("1000000"); }
588 CPPUNIT_TEST_SUITE(BigPtrArrayPerformanceTest
);
589 CPPUNIT_TEST(test_insert_at_end_1000
);
590 CPPUNIT_TEST(test_insert_at_end_10000
);
591 CPPUNIT_TEST(test_insert_at_end_100000
);
592 CPPUNIT_TEST(test_insert_at_end_1000000
);
593 CPPUNIT_TEST(test_insert_at_front_1000
);
594 CPPUNIT_TEST(test_insert_at_front_10000
);
595 CPPUNIT_TEST(test_insert_at_front_100000
);
596 CPPUNIT_TEST(test_insert_at_front_1000000
);
597 CPPUNIT_TEST_SUITE_END();
600 void test_insert_at_end(const char* numElements
)
602 OStringBuffer
buff("test_insert_at_end ");
603 buff
.append(numElements
);
604 int n
= atoi(numElements
);
605 PerformanceTracer
tracer(buff
.getStr());
607 for (int i
= 0; i
< n
; i
++)
608 bparr
.Insert(new BigPtrEntryMock(i
), bparr
.Count());
610 releaseBigPtrArrayContent(bparr
);
613 void test_insert_at_front(const char* numElements
)
615 OStringBuffer
buff("test_insert_at_front ");
616 buff
.append(numElements
);
617 int n
= atoi(numElements
);
618 PerformanceTracer
tracer(buff
.getStr());
620 for (int i
= 0; i
< n
; i
++)
621 bparr
.Insert(new BigPtrEntryMock(i
), 0);
623 releaseBigPtrArrayContent(bparr
);
629 // register test suites
630 CPPUNIT_TEST_SUITE_REGISTRATION(BigPtrArrayUnittest
);
632 CPPUNIT_TEST_SUITE_REGISTRATION(BigPtrArrayPerformanceTest
);
635 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */