Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #include <svl/itempool.hxx>
11 : #include <poolio.hxx>
12 :
13 : #include <cppunit/TestAssert.h>
14 : #include <cppunit/TestFixture.h>
15 : #include <cppunit/extensions/HelperMacros.h>
16 : #include <cppunit/plugin/TestPlugIn.h>
17 :
18 : class PoolItemTest : public CppUnit::TestFixture
19 : {
20 : public:
21 2 : PoolItemTest() {}
22 4 : virtual ~PoolItemTest() {}
23 :
24 : void testPool();
25 :
26 : // Adds code needed to register the test suite
27 4 : CPPUNIT_TEST_SUITE(PoolItemTest);
28 :
29 2 : CPPUNIT_TEST(testPool);
30 :
31 : // End of test suite definition
32 4 : CPPUNIT_TEST_SUITE_END();
33 : };
34 :
35 2 : void PoolItemTest::testPool()
36 : {
37 : SfxItemInfo aItems[] =
38 : { { 0, SFX_ITEM_POOLABLE },
39 : { 1, 0 /* not poolable */ },
40 : { 2, SFX_ITEM_NOT_POOLABLE },
41 : { 3, 0 /* not poolable */}
42 2 : };
43 :
44 2 : SfxItemPool *pPool = new SfxItemPool("testpool", 0, 3, aItems);
45 2 : SfxItemPool_Impl *pImpl = SfxItemPool_Impl::GetImpl(pPool);
46 2 : CPPUNIT_ASSERT(pImpl != NULL);
47 2 : CPPUNIT_ASSERT(pImpl->maPoolItems.size() == 4);
48 :
49 : // Poolable
50 2 : SfxVoidItem aItemZero( 0 );
51 4 : SfxVoidItem aNotherZero( 0 );
52 :
53 : {
54 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[0] == NULL);
55 2 : const SfxPoolItem &rVal = pPool->Put(aItemZero);
56 2 : CPPUNIT_ASSERT(rVal == aItemZero);
57 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[0] != NULL);
58 2 : const SfxPoolItem &rVal2 = pPool->Put(aNotherZero);
59 2 : CPPUNIT_ASSERT(rVal2 == rVal);
60 2 : CPPUNIT_ASSERT(&rVal2 == &rVal);
61 :
62 : // Clones on Put ...
63 2 : CPPUNIT_ASSERT(&rVal2 != &aItemZero);
64 2 : CPPUNIT_ASSERT(&rVal2 != &aNotherZero);
65 2 : CPPUNIT_ASSERT(&rVal != &aItemZero);
66 2 : CPPUNIT_ASSERT(&rVal != &aNotherZero);
67 : }
68 :
69 : // non-poolable
70 4 : SfxVoidItem aItemOne( 1 );
71 4 : SfxVoidItem aNotherOne( 1 );
72 : {
73 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[1] == NULL);
74 2 : const SfxPoolItem &rVal = pPool->Put(aItemOne);
75 2 : CPPUNIT_ASSERT(rVal == aItemOne);
76 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[1] != NULL);
77 :
78 2 : const SfxPoolItem &rVal2 = pPool->Put(aNotherOne);
79 2 : CPPUNIT_ASSERT(rVal2 == rVal);
80 2 : CPPUNIT_ASSERT(&rVal2 != &rVal);
81 : }
82 :
83 : // not-poolable
84 4 : SfxVoidItem aItemTwo( 2 );
85 4 : SfxVoidItem aNotherTwo( 2 );
86 : {
87 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[2] == NULL);
88 2 : const SfxPoolItem &rVal = pPool->Put(aItemTwo);
89 : // those guys just don't go in ...
90 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[2] == NULL);
91 2 : CPPUNIT_ASSERT(rVal == aItemOne);
92 : }
93 :
94 : // Test rehash
95 10 : for (size_t i = 0; i < pImpl->maPoolItems.size(); ++i)
96 : {
97 8 : SfxPoolItemArray_Impl *pSlice = pImpl->maPoolItems[i];
98 8 : if (pSlice)
99 4 : pSlice->ReHash();
100 : }
101 :
102 : // Test removal.
103 4 : SfxVoidItem aRemoveThree(3);
104 4 : SfxVoidItem aNotherThree(3);
105 2 : const SfxPoolItem &rKeyThree = pPool->Put(aRemoveThree);
106 2 : pPool->Put(aNotherThree);
107 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[3]->size() > 0);
108 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[3]->maFree.size() == 0);
109 2 : pPool->Remove(rKeyThree);
110 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[3]->maFree.size() == 1);
111 2 : pPool->Put(aNotherThree);
112 4 : CPPUNIT_ASSERT(pImpl->maPoolItems[3]->maFree.size() == 0);
113 2 : }
114 :
115 2 : CPPUNIT_TEST_SUITE_REGISTRATION(PoolItemTest);
116 :
117 8 : CPPUNIT_PLUGIN_IMPLEMENT();
|