tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / sw / qa / core / Test-BigPtrArray.cxx
blob6754cda04007804bc4c464adbf784090188a26fb
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 <cppunit/TestAssert.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
24 //#define TIMELOG for measuring performance
26 #include <bparr.hxx>
28 namespace /* private */
30 const sal_Int32 NUM_ENTRIES = 10;
32 class BigPtrEntryMock : public BigPtrEntry
34 public:
35 explicit BigPtrEntryMock(sal_Int32 count) : count_(count)
39 sal_Int32 getCount() const
41 return count_;
44 sal_Int32 Position() const
46 return GetPos();
49 private:
50 sal_Int32 count_;
53 void fillBigPtrArray(BigPtrArray& bparr, sal_Int32 numEntries)
55 for (sal_Int32 i = 0; i < numEntries; i++)
56 bparr.Insert(new BigPtrEntryMock(i), bparr.Count());
59 bool checkElementPositions(const BigPtrArray& bparr)
61 for (sal_Int32 i = 0; i < bparr.Count(); i++)
63 if (static_cast<BigPtrEntryMock*>(bparr[i])->Position() != i)
64 return false;
66 return true;
69 void releaseBigPtrArrayContent(BigPtrArray const & bparr)
71 for (sal_Int32 i = 0; i < bparr.Count(); i++)
72 delete bparr[i];
76 class BigPtrArrayUnittest : public CppUnit::TestFixture
78 public:
80 BigPtrArrayUnittest()
84 /** Test constructor/destructor
85 The size of the BigPtrArray
86 aka the 'Count' should be 0
87 initially.
89 void test_ctor()
91 BigPtrArray bparr;
93 CPPUNIT_ASSERT_EQUAL_MESSAGE
95 "BigPtrArray ctor failed",
96 static_cast<sal_Int32>(0), bparr.Count()
100 void test_insert_entries_at_front()
102 BigPtrArray bparr;
104 for (sal_Int32 i = 0; i < NUM_ENTRIES; i++)
106 sal_Int32 oldCount = bparr.Count();
107 bparr.Insert(new BigPtrEntryMock(i), 0);
108 CPPUNIT_ASSERT_EQUAL_MESSAGE
110 "test_insert_entries_at_front failed",
111 oldCount + 1, bparr.Count()
115 for (sal_Int32 i = 0, j = NUM_ENTRIES - 1; i < NUM_ENTRIES; i++, j--)
117 CPPUNIT_ASSERT_EQUAL_MESSAGE
119 "test_insert_entries_at_front failed",
120 j, static_cast<BigPtrEntryMock*>(bparr[i])->getCount()
124 CPPUNIT_ASSERT_MESSAGE
126 "test_insert_entries_at_front failed",
127 checkElementPositions(bparr)
130 releaseBigPtrArrayContent(bparr);
133 void test_insert_entries_in_the_middle()
135 BigPtrArray bparr;
137 fillBigPtrArray(bparr, NUM_ENTRIES);
139 sal_Int32 oldCount = bparr.Count();
141 bparr.Insert(new BigPtrEntryMock(NUM_ENTRIES), bparr.Count() / 2);
143 CPPUNIT_ASSERT_EQUAL_MESSAGE
145 "test_insert_entries_in_the_middle failed",
146 oldCount + 1, bparr.Count()
148 CPPUNIT_ASSERT_EQUAL_MESSAGE
150 "test_insert_entries_in_the_middle failed",
151 NUM_ENTRIES, static_cast<BigPtrEntryMock*>(bparr[bparr.Count() / 2])->getCount()
154 CPPUNIT_ASSERT_MESSAGE
156 "test_insert_entries_in_the_middle failed",
157 checkElementPositions(bparr)
160 releaseBigPtrArrayContent(bparr);
163 void test_insert_at_already_used_index()
165 BigPtrArray bparr;
167 fillBigPtrArray(bparr, NUM_ENTRIES);
169 const sal_Int32 oldCount = bparr.Count();
171 // insert 5 elements
172 for (sal_Int32 i = 0, j = 30; i < 5; i++, j++)
173 bparr.Insert(new BigPtrEntryMock(j), i);
175 CPPUNIT_ASSERT_EQUAL_MESSAGE
177 "test_insert_at_already_used_index failed",
178 oldCount + 5, bparr.Count()
181 // now, first 5 elements have counts: 30,31,..34
182 // next 10 elements have counts: 0,1,..9
183 for (sal_Int32 i = 0, j = 30; i < bparr.Count(); i++, j++)
185 CPPUNIT_ASSERT_EQUAL_MESSAGE
187 "test_insert_at_already_used_index failed",
188 (i < 5 ? j : i - 5), static_cast<BigPtrEntryMock*>(bparr[i])->getCount()
192 CPPUNIT_ASSERT_MESSAGE
194 "test_insert_at_already_used_index failed",
195 checkElementPositions(bparr)
198 releaseBigPtrArrayContent(bparr);
201 void test_insert_at_end()
203 BigPtrArray bparr;
205 fillBigPtrArray(bparr, NUM_ENTRIES);
207 sal_Int32 oldCount = bparr.Count();
208 bparr.Insert(new BigPtrEntryMock(NUM_ENTRIES), bparr.Count());
210 CPPUNIT_ASSERT_EQUAL_MESSAGE
212 "test_insert_at_end failed",
213 oldCount + 1, bparr.Count()
215 CPPUNIT_ASSERT_EQUAL_MESSAGE
217 "test_insert_at_end failed",
218 NUM_ENTRIES, static_cast<BigPtrEntryMock*>(bparr[bparr.Count()-1])->getCount()
221 CPPUNIT_ASSERT_MESSAGE
223 "test_insert_at_end failed",
224 checkElementPositions(bparr)
227 releaseBigPtrArrayContent(bparr);
230 void test_remove_at_front()
232 BigPtrArray bparr;
234 fillBigPtrArray(bparr, NUM_ENTRIES);
236 for (sal_Int32 i = 0; i < NUM_ENTRIES; i++)
238 sal_Int32 oldCount = bparr.Count();
240 delete bparr[0]; // release content
241 bparr.Remove(0); // remove item from container
243 CPPUNIT_ASSERT_EQUAL_MESSAGE
245 "test_remove_at_front failed (wrong count)",
246 oldCount - 1, bparr.Count()
249 for (sal_Int32 j = 0, k = i + 1; j < bparr.Count(); j++, k++)
251 CPPUNIT_ASSERT_EQUAL_MESSAGE
253 "test_remove_at_front failed",
254 k, static_cast<BigPtrEntryMock*>(bparr[j])->getCount()
258 CPPUNIT_ASSERT_MESSAGE
260 "test_remove_at_front failed",
261 checkElementPositions(bparr)
266 void test_remove_at_back()
268 BigPtrArray bparr;
270 fillBigPtrArray(bparr, NUM_ENTRIES);
272 for (int i = NUM_ENTRIES - 1; i >= 0; i--)
274 sal_Int32 oldCount = bparr.Count();
275 delete bparr[i];
276 bparr.Remove(i);
278 CPPUNIT_ASSERT_EQUAL_MESSAGE
280 "test_remove_at_back failed (wrong count)",
281 (oldCount - 1), bparr.Count()
284 for (sal_Int32 j = 0; j < bparr.Count(); j++)
286 CPPUNIT_ASSERT_EQUAL_MESSAGE
288 "test_remove_at_back failed",
289 j, static_cast<BigPtrEntryMock*>(bparr[j])->getCount()
293 CPPUNIT_ASSERT_MESSAGE
295 "test_remove_at_back failed",
296 checkElementPositions(bparr)
301 void test_remove_in_the_middle()
303 BigPtrArray bparr;
305 fillBigPtrArray(bparr, NUM_ENTRIES);
307 while (bparr.Count())
309 sal_Int32 oldCount = bparr.Count();
310 sal_Int32 oldElement = static_cast<BigPtrEntryMock*>(bparr[bparr.Count() / 2])->getCount();
312 delete bparr[bparr.Count() / 2];
313 bparr.Remove(bparr.Count() / 2);
315 CPPUNIT_ASSERT_EQUAL_MESSAGE
317 "test_remove_in_the_middle failed (wrong count)",
318 oldCount - 1, bparr.Count()
321 for (sal_Int32 i = 0; i < bparr.Count(); i++)
323 CPPUNIT_ASSERT_MESSAGE
325 "test_remove_in_the_middle failed",
326 static_cast<BigPtrEntryMock*>(bparr[i])->getCount() != oldElement
330 CPPUNIT_ASSERT_MESSAGE
332 "test_remove_in_the_middle failed",
333 checkElementPositions(bparr)
338 void test_remove_multiple_elements_at_once()
340 BigPtrArray bparr;
342 fillBigPtrArray(bparr, NUM_ENTRIES);
344 while(bparr.Count())
346 sal_Int32 nRemove = std::min<sal_Int32>(bparr.Count(), 3);
347 sal_Int32 oldCount = bparr.Count();
349 for (sal_Int32 i = 0; i < nRemove; i++)
350 delete bparr[i];
352 bparr.Remove(0, nRemove);
354 CPPUNIT_ASSERT_EQUAL_MESSAGE
356 "test_remove_multiple_elements_at_once failed",
357 oldCount - nRemove, bparr.Count()
360 CPPUNIT_ASSERT_MESSAGE
362 "test_remove_multiple_elements_at_once failed",
363 checkElementPositions(bparr)
368 void test_remove_all_elements_at_once()
370 BigPtrArray bparr;
372 fillBigPtrArray(bparr, NUM_ENTRIES);
374 releaseBigPtrArrayContent(bparr);
375 bparr.Remove(0, bparr.Count());
377 CPPUNIT_ASSERT_EQUAL_MESSAGE
379 "test_remove_all_elements_at_once failed",
380 static_cast<sal_Int32>(0), bparr.Count()
384 void test_move_elements_from_lower_to_higher_pos()
386 BigPtrArray bparr;
388 fillBigPtrArray(bparr, NUM_ENTRIES);
390 for (sal_Int32 i = 0; i < NUM_ENTRIES - 1; i++)
392 bparr.Move(i, i + 2);
395 for (sal_Int32 i = 0; i < (NUM_ENTRIES - 1); i++)
397 CPPUNIT_ASSERT_EQUAL_MESSAGE
399 "test_move_elements_from_lower_to_higher_pos failed",
400 (i + 1), static_cast<BigPtrEntryMock*>(bparr[i])->getCount()
404 CPPUNIT_ASSERT_EQUAL_MESSAGE
406 "test_move_elements_from_lower_to_higher_pos failed",
407 static_cast<sal_Int32>(0), static_cast<BigPtrEntryMock*>(bparr[NUM_ENTRIES -1])->getCount()
410 CPPUNIT_ASSERT_MESSAGE
412 "test_move_elements_from_lower_to_higher_pos failed",
413 checkElementPositions(bparr)
416 releaseBigPtrArrayContent(bparr);
419 void test_move_elements_from_higher_to_lower_pos()
421 BigPtrArray bparr;
423 fillBigPtrArray(bparr, NUM_ENTRIES);
425 for (int i = NUM_ENTRIES - 1; i >= 1; i--)
427 bparr.Move(i, i - 1);
430 CPPUNIT_ASSERT_EQUAL_MESSAGE
432 "test_move_elements_from_higher_to_lower_pos failed",
433 (NUM_ENTRIES - 1), static_cast<BigPtrEntryMock*>(bparr[0])->getCount()
436 for (sal_Int32 i = 1; i < NUM_ENTRIES; i++)
438 CPPUNIT_ASSERT_EQUAL_MESSAGE
440 "test_move_elements_from_higher_to_lower_pos failed",
441 (i - 1), static_cast<BigPtrEntryMock*>(bparr[i])->getCount()
445 CPPUNIT_ASSERT_MESSAGE
447 "test_move_elements_from_higher_to_lower_pos failed",
448 checkElementPositions(bparr)
451 releaseBigPtrArrayContent(bparr);
454 void test_move_to_same_position()
456 BigPtrArray bparr;
458 fillBigPtrArray(bparr, NUM_ENTRIES);
460 for (sal_Int32 i = 0; i < NUM_ENTRIES; i++)
462 bparr.Move(i, i);
465 for (sal_Int32 i = 0; i < NUM_ENTRIES; i++)
467 CPPUNIT_ASSERT_EQUAL_MESSAGE
469 "test_move_to_same_position failed",
470 i, static_cast<BigPtrEntryMock*>(bparr[i])->getCount()
474 CPPUNIT_ASSERT_MESSAGE
476 "test_move_to_same_position failed",
477 checkElementPositions(bparr)
480 releaseBigPtrArrayContent(bparr);
483 void test_replace_elements()
485 BigPtrArray bparr;
487 fillBigPtrArray(bparr, NUM_ENTRIES);
489 for (sal_Int32 i = 0, j = NUM_ENTRIES - 1; i < NUM_ENTRIES; i++, j--)
491 delete bparr[i];
492 bparr.Replace(i, new BigPtrEntryMock(j));
495 for (sal_Int32 i = 0; i < NUM_ENTRIES; i++)
497 CPPUNIT_ASSERT_EQUAL_MESSAGE
499 "test_replace_elements failed",
500 (NUM_ENTRIES - i - 1), static_cast<BigPtrEntryMock*>(bparr[i])->getCount()
504 CPPUNIT_ASSERT_MESSAGE
506 "test_replace_elements failed",
507 checkElementPositions(bparr)
510 releaseBigPtrArrayContent(bparr);
513 CPPUNIT_TEST_SUITE(BigPtrArrayUnittest);
514 CPPUNIT_TEST(test_ctor);
515 CPPUNIT_TEST(test_insert_entries_at_front);
516 CPPUNIT_TEST(test_insert_entries_in_the_middle);
517 CPPUNIT_TEST(test_insert_at_already_used_index);
518 CPPUNIT_TEST(test_insert_at_end);
519 CPPUNIT_TEST(test_remove_at_front);
520 CPPUNIT_TEST(test_remove_at_back);
521 CPPUNIT_TEST(test_remove_in_the_middle);
522 CPPUNIT_TEST(test_remove_multiple_elements_at_once);
523 CPPUNIT_TEST(test_remove_all_elements_at_once);
524 CPPUNIT_TEST(test_move_elements_from_lower_to_higher_pos);
525 CPPUNIT_TEST(test_move_elements_from_higher_to_lower_pos);
526 CPPUNIT_TEST(test_move_to_same_position);
527 CPPUNIT_TEST(test_replace_elements);
528 CPPUNIT_TEST_SUITE_END();
531 #if defined TIMELOG
533 const char* const START = "START: ";
534 const char* const END = "END: ";
536 class PerformanceTracer
538 public:
540 public:
541 explicit PerformanceTracer(const string& methodName) :
542 startString_(START),
543 endString_(END)
545 startString_ += methodName;
546 endString_ += methodName;
549 ~PerformanceTracer()
553 private:
554 string startString_;
555 string endString_;
558 class BigPtrArrayPerformanceTest : public CppUnit::TestFixture
560 public:
561 BigPtrArrayPerformanceTest()
565 void test_insert_at_end_1000()
566 { test_insert_at_end("1000"); }
568 void test_insert_at_end_10000()
569 { test_insert_at_end("10000"); }
571 void test_insert_at_end_100000()
572 { test_insert_at_end("100000"); }
574 void test_insert_at_end_1000000()
575 { test_insert_at_end("1000000"); }
577 void test_insert_at_front_1000()
578 { test_insert_at_front("1000"); }
580 void test_insert_at_front_10000()
581 { test_insert_at_front("10000"); }
583 void test_insert_at_front_100000()
584 { test_insert_at_front("100000"); }
586 void test_insert_at_front_1000000()
587 { test_insert_at_front("1000000"); }
589 CPPUNIT_TEST_SUITE(BigPtrArrayPerformanceTest);
590 CPPUNIT_TEST(test_insert_at_end_1000);
591 CPPUNIT_TEST(test_insert_at_end_10000);
592 CPPUNIT_TEST(test_insert_at_end_100000);
593 CPPUNIT_TEST(test_insert_at_end_1000000);
594 CPPUNIT_TEST(test_insert_at_front_1000);
595 CPPUNIT_TEST(test_insert_at_front_10000);
596 CPPUNIT_TEST(test_insert_at_front_100000);
597 CPPUNIT_TEST(test_insert_at_front_1000000);
598 CPPUNIT_TEST_SUITE_END();
600 private:
601 void test_insert_at_end(const char* numElements)
603 OStringBuffer buff("test_insert_at_end ");
604 buff.append(numElements);
605 int n = atoi(numElements);
606 PerformanceTracer tracer(buff.getStr());
607 BigPtrArray bparr;
608 for (int i = 0; i < n; i++)
609 bparr.Insert(new BigPtrEntryMock(i), bparr.Count());
611 releaseBigPtrArrayContent(bparr);
614 void test_insert_at_front(const char* numElements)
616 OStringBuffer buff("test_insert_at_front ");
617 buff.append(numElements);
618 int n = atoi(numElements);
619 PerformanceTracer tracer(buff.getStr());
620 BigPtrArray bparr;
621 for (int i = 0; i < n; i++)
622 bparr.Insert(new BigPtrEntryMock(i), 0);
624 releaseBigPtrArrayContent(bparr);
628 #endif
630 // register test suites
631 CPPUNIT_TEST_SUITE_REGISTRATION(BigPtrArrayUnittest);
632 #ifdef TIMELOG
633 CPPUNIT_TEST_SUITE_REGISTRATION(BigPtrArrayPerformanceTest);
634 #endif
636 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */