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 1 : PoolItemTest() {}
22 2 : virtual ~PoolItemTest() {}
23 :
24 : void testPool();
25 :
26 : // Adds code needed to register the test suite
27 2 : CPPUNIT_TEST_SUITE(PoolItemTest);
28 :
29 1 : CPPUNIT_TEST(testPool);
30 :
31 : // End of test suite definition
32 5 : CPPUNIT_TEST_SUITE_END();
33 : };
34 :
35 1 : void PoolItemTest::testPool()
36 : {
37 : SfxItemInfo aItems[] =
38 : { { 0, SfxItemPoolFlags::POOLABLE },
39 : { 1, SfxItemPoolFlags::NONE /* not poolable */ },
40 : { 2, SfxItemPoolFlags::NOT_POOLABLE },
41 : { 3, SfxItemPoolFlags::NONE /* not poolable */}
42 1 : };
43 :
44 1 : SfxItemPool *pPool = new SfxItemPool("testpool", 0, 3, aItems);
45 1 : SfxItemPool_Impl *pImpl = SfxItemPool_Impl::GetImpl(pPool);
46 1 : CPPUNIT_ASSERT(pImpl != NULL);
47 1 : CPPUNIT_ASSERT(pImpl->maPoolItems.size() == 4);
48 :
49 : // Poolable
50 1 : SfxVoidItem aItemZero( 0 );
51 2 : SfxVoidItem aNotherZero( 0 );
52 :
53 : {
54 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[0] == NULL);
55 1 : const SfxPoolItem &rVal = pPool->Put(aItemZero);
56 1 : CPPUNIT_ASSERT(rVal == aItemZero);
57 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[0] != NULL);
58 1 : const SfxPoolItem &rVal2 = pPool->Put(aNotherZero);
59 1 : CPPUNIT_ASSERT(rVal2 == rVal);
60 1 : CPPUNIT_ASSERT(&rVal2 == &rVal);
61 :
62 : // Clones on Put ...
63 1 : CPPUNIT_ASSERT(&rVal2 != &aItemZero);
64 1 : CPPUNIT_ASSERT(&rVal2 != &aNotherZero);
65 1 : CPPUNIT_ASSERT(&rVal != &aItemZero);
66 1 : CPPUNIT_ASSERT(&rVal != &aNotherZero);
67 : }
68 :
69 : // non-poolable
70 2 : SfxVoidItem aItemOne( 1 );
71 2 : SfxVoidItem aNotherOne( 1 );
72 : {
73 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[1] == NULL);
74 1 : const SfxPoolItem &rVal = pPool->Put(aItemOne);
75 1 : CPPUNIT_ASSERT(rVal == aItemOne);
76 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[1] != NULL);
77 :
78 1 : const SfxPoolItem &rVal2 = pPool->Put(aNotherOne);
79 1 : CPPUNIT_ASSERT(rVal2 == rVal);
80 1 : CPPUNIT_ASSERT(&rVal2 != &rVal);
81 : }
82 :
83 : // not-poolable
84 2 : SfxVoidItem aItemTwo( 2 );
85 2 : SfxVoidItem aNotherTwo( 2 );
86 : {
87 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[2] == NULL);
88 1 : const SfxPoolItem &rVal = pPool->Put(aItemTwo);
89 : // those guys just don't go in ...
90 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[2] == NULL);
91 1 : CPPUNIT_ASSERT(rVal == aItemOne);
92 : }
93 :
94 : // Test rehash
95 5 : for (size_t i = 0; i < pImpl->maPoolItems.size(); ++i)
96 : {
97 4 : SfxPoolItemArray_Impl *pSlice = pImpl->maPoolItems[i];
98 4 : if (pSlice)
99 2 : pSlice->ReHash();
100 : }
101 :
102 : // Test removal.
103 2 : SfxVoidItem aRemoveThree(3);
104 2 : SfxVoidItem aNotherThree(3);
105 1 : const SfxPoolItem &rKeyThree = pPool->Put(aRemoveThree);
106 1 : pPool->Put(aNotherThree);
107 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[3]->size() > 0);
108 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[3]->maFree.size() == 0);
109 1 : pPool->Remove(rKeyThree);
110 1 : CPPUNIT_ASSERT(pImpl->maPoolItems[3]->maFree.size() == 1);
111 1 : pPool->Put(aNotherThree);
112 2 : CPPUNIT_ASSERT(pImpl->maPoolItems[3]->maFree.size() == 0);
113 1 : }
114 :
115 1 : CPPUNIT_TEST_SUITE_REGISTRATION(PoolItemTest);
116 :
117 4 : CPPUNIT_PLUGIN_IMPLEMENT();
|