summaryrefslogtreecommitdiffstats
path: root/svl/qa/unit/items/test_itempool.cxx
blob: 2cb751d4fd77ae7f7fa31974c0ea30f3c3cc2952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <svl/itempool.hxx>
#include <svl/voiditem.hxx>
#include <poolio.hxx>

#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>

class PoolItemTest : public CppUnit::TestFixture
{
  public:
             PoolItemTest() {}

    void testPool();

    // Adds code needed to register the test suite
    CPPUNIT_TEST_SUITE(PoolItemTest);

    CPPUNIT_TEST(testPool);

    // End of test suite definition
    CPPUNIT_TEST_SUITE_END();
};

void PoolItemTest::testPool()
{
    SfxItemInfo const aItems[] =
    {
        // _nSID, _bNeedsPoolRegistration, _bShareable
        { 4, true,  true },
        { 3, true,  false /* test NeedsPoolRegistration */ },
        { 2, false, false },
        { 1, true,  false /* test NeedsPoolRegistration */}
    };

    rtl::Reference<SfxItemPool> pPool = new SfxItemPool("testpool", 1, 4, aItems);

    // Poolable
    SfxVoidItem aItemOne( 1 );
    SfxVoidItem aNotherOne( 1 );

    {
        CPPUNIT_ASSERT(nullptr == pPool->ppRegisteredSfxPoolItems);
        const SfxPoolItem &rVal = pPool->DirectPutItemInPool(aItemOne);
        CPPUNIT_ASSERT(bool(rVal == aItemOne));
        CPPUNIT_ASSERT(nullptr != pPool->ppRegisteredSfxPoolItems);
        CPPUNIT_ASSERT(nullptr != pPool->ppRegisteredSfxPoolItems[0]);
        CPPUNIT_ASSERT(!pPool->ppRegisteredSfxPoolItems[0]->empty());
        const SfxPoolItem &rVal2 = pPool->DirectPutItemInPool(aNotherOne);
        CPPUNIT_ASSERT(bool(rVal2 == rVal));

        // ITEM: With leaving the paradigm that internally an already
        //   existing Item with true = operator==() (which is very
        //   expensive) the ptr's are no longer required to be equal,
        //   but the content-compare *is*
        CPPUNIT_ASSERT(SfxPoolItem::areSame(rVal, rVal2));

        // Clones on Put ...
        // ptr compare OK, we want to check just the ptrs here
        CPPUNIT_ASSERT(!areSfxPoolItemPtrsEqual(&rVal2, &aItemOne));
        CPPUNIT_ASSERT(!areSfxPoolItemPtrsEqual(&rVal2, &aNotherOne));
        CPPUNIT_ASSERT(!areSfxPoolItemPtrsEqual(&rVal, &aItemOne));
        CPPUNIT_ASSERT(!areSfxPoolItemPtrsEqual(&rVal, &aNotherOne));
    }

    // non-poolable
    SfxVoidItem aItemTwo( 2 );
    SfxVoidItem aNotherTwo( 2 );
    {
        CPPUNIT_ASSERT(nullptr == pPool->ppRegisteredSfxPoolItems[1]);
        const SfxPoolItem &rVal = pPool->DirectPutItemInPool(aItemTwo);
        CPPUNIT_ASSERT(bool(rVal == aItemTwo));
        CPPUNIT_ASSERT(nullptr != pPool->ppRegisteredSfxPoolItems[1]);
        CPPUNIT_ASSERT(!pPool->ppRegisteredSfxPoolItems[1]->empty());
        const SfxPoolItem &rVal2 = pPool->DirectPutItemInPool(aNotherTwo);
        CPPUNIT_ASSERT(bool(rVal2 == rVal));
        // ptr compare OK, we want to check just the ptrs here
        CPPUNIT_ASSERT(!areSfxPoolItemPtrsEqual(&rVal2, &rVal));
    }

    // Test removal.
    SfxVoidItem aRemoveFour(4);
    SfxVoidItem aNotherFour(4);

    const SfxPoolItem &rKeyFour = pPool->DirectPutItemInPool(aRemoveFour);
    pPool->DirectPutItemInPool(aNotherFour);
    CPPUNIT_ASSERT(pPool->ppRegisteredSfxPoolItems[3]->size() > 0);
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), pPool->ppRegisteredSfxPoolItems[3]->size());
    pPool->DirectRemoveItemFromPool(rKeyFour);
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pPool->ppRegisteredSfxPoolItems[3]->size());
    pPool->DirectPutItemInPool(aNotherFour);
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), pPool->ppRegisteredSfxPoolItems[3]->size());
}


CPPUNIT_TEST_SUITE_REGISTRATION(PoolItemTest);

CPPUNIT_PLUGIN_IMPLEMENT();