Branch data 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 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include <sal/types.h>
21 : : #include "cppunit/TestAssert.h"
22 : : #include "cppunit/TestFixture.h"
23 : : #include "cppunit/extensions/HelperMacros.h"
24 : :
25 : : #include <o3tl/vector_pool.hxx>
26 : :
27 : : using namespace ::o3tl;
28 : :
29 [ - + ]: 30 : class vector_pool_test : public CppUnit::TestFixture
30 : : {
31 : : public:
32 : 5 : void testPoolBasics()
33 : : {
34 [ + - ]: 5 : vector_pool<int> aPool;
35 : :
36 [ + - ]: 5 : std::ptrdiff_t nIdx1 = aPool.alloc();
37 [ + - ]: 5 : std::ptrdiff_t nIdx2 = aPool.alloc();
38 [ + - ]: 5 : std::ptrdiff_t nIdx3 = aPool.alloc();
39 : :
40 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator idx order 1", nIdx1 < nIdx2 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
41 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator idx order 2", nIdx2 < nIdx3 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
42 : :
43 [ + - ]: 5 : aPool.free(nIdx2);
44 [ + - ]: 5 : aPool.free(nIdx3);
45 : :
46 [ + - ]: 5 : nIdx2 = aPool.alloc();
47 [ + - ]: 5 : nIdx3 = aPool.alloc();
48 : :
49 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator idx order 1 after fragmentation", nIdx1 < nIdx3 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
50 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator idx order 2 after fragmentation", nIdx3 < nIdx2 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
51 : 5 : }
52 : :
53 : 5 : void testPoolValueSemantics()
54 : : {
55 [ + - ]: 5 : vector_pool<int> aPool;
56 : :
57 [ + - ]: 5 : std::ptrdiff_t nIdx1 = aPool.store(0);
58 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 1", aPool.get(nIdx1) == 0 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
59 : :
60 [ + - ]: 5 : std::ptrdiff_t nIdx2 = aPool.store(1);
61 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 2", aPool.get(nIdx2) == 1 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
62 : :
63 [ + - ]: 5 : std::ptrdiff_t nIdx3 = aPool.store(2);
64 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 3", aPool.get(nIdx3) == 2 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
65 : :
66 [ + - ]: 5 : aPool.free(nIdx2);
67 [ + - ]: 5 : aPool.free(nIdx3);
68 : :
69 [ + - ]: 5 : nIdx2 = aPool.store(1);
70 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 2 after fragmentation", aPool.get(nIdx2) == 1 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
71 : :
72 [ + - ]: 5 : nIdx3 = aPool.store(2);
73 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 3 after fragmentation", aPool.get(nIdx3) == 2 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
74 : 5 : }
75 : :
76 : : // Change the following lines only, if you add, remove or rename
77 : : // member functions of the current class,
78 : : // because these macros are need by auto register mechanism.
79 : :
80 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(vector_pool_test);
[ + - ][ + - ]
[ # # ]
81 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testPoolBasics);
[ + - ][ + - ]
[ + - ][ + - ]
82 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testPoolValueSemantics);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
83 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
84 : : };
85 : :
86 : : // -----------------------------------------------------------------------------
87 [ + - ][ + - ]: 15 : CPPUNIT_TEST_SUITE_REGISTRATION(vector_pool_test);
88 : :
89 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|