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 12 : class vector_pool_test : public CppUnit::TestFixture
30 : {
31 : public:
32 2 : void testPoolBasics()
33 : {
34 2 : vector_pool<int> aPool;
35 :
36 2 : std::ptrdiff_t nIdx1 = aPool.alloc();
37 2 : std::ptrdiff_t nIdx2 = aPool.alloc();
38 2 : std::ptrdiff_t nIdx3 = aPool.alloc();
39 :
40 2 : CPPUNIT_ASSERT_MESSAGE("allocator idx order 1", nIdx1 < nIdx2 );
41 2 : CPPUNIT_ASSERT_MESSAGE("allocator idx order 2", nIdx2 < nIdx3 );
42 :
43 2 : aPool.free(nIdx2);
44 2 : aPool.free(nIdx3);
45 :
46 2 : nIdx2 = aPool.alloc();
47 2 : nIdx3 = aPool.alloc();
48 :
49 2 : CPPUNIT_ASSERT_MESSAGE("allocator idx order 1 after fragmentation", nIdx1 < nIdx3 );
50 2 : CPPUNIT_ASSERT_MESSAGE("allocator idx order 2 after fragmentation", nIdx3 < nIdx2 );
51 2 : }
52 :
53 2 : void testPoolValueSemantics()
54 : {
55 2 : vector_pool<int> aPool;
56 :
57 2 : std::ptrdiff_t nIdx1 = aPool.store(0);
58 2 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 1", aPool.get(nIdx1) == 0 );
59 :
60 2 : std::ptrdiff_t nIdx2 = aPool.store(1);
61 2 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 2", aPool.get(nIdx2) == 1 );
62 :
63 2 : std::ptrdiff_t nIdx3 = aPool.store(2);
64 2 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 3", aPool.get(nIdx3) == 2 );
65 :
66 2 : aPool.free(nIdx2);
67 2 : aPool.free(nIdx3);
68 :
69 2 : nIdx2 = aPool.store(1);
70 2 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 2 after fragmentation", aPool.get(nIdx2) == 1 );
71 :
72 2 : nIdx3 = aPool.store(2);
73 2 : CPPUNIT_ASSERT_MESSAGE("allocator value semantics 3 after fragmentation", aPool.get(nIdx3) == 2 );
74 2 : }
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 4 : CPPUNIT_TEST_SUITE(vector_pool_test);
81 2 : CPPUNIT_TEST(testPoolBasics);
82 2 : CPPUNIT_TEST(testPoolValueSemantics);
83 4 : CPPUNIT_TEST_SUITE_END();
84 : };
85 :
86 :
87 6 : CPPUNIT_TEST_SUITE_REGISTRATION(vector_pool_test);
88 :
89 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|