xref: /aoo42x/main/o3tl/qa/test-vector_pool.cxx (revision 31682d32)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // autogenerated file with codegen.pl
23 
24 #include "preextstl.h"
25 #include "cppunit/TestAssert.h"
26 #include "cppunit/TestFixture.h"
27 #include "cppunit/extensions/HelperMacros.h"
28 #include "postextstl.h"
29 
30 #include <o3tl/vector_pool.hxx>
31 
32 using namespace ::o3tl;
33 
34 class vector_pool_test : public CppUnit::TestFixture
35 {
36 public:
37     void testPoolBasics()
38     {
39         vector_pool<int> aPool;
40 
41         std::ptrdiff_t nIdx1 = aPool.alloc();
42         std::ptrdiff_t nIdx2 = aPool.alloc();
43         std::ptrdiff_t nIdx3 = aPool.alloc();
44 
45         CPPUNIT_ASSERT_MESSAGE("allocator idx order 1", nIdx1 < nIdx2 );
46         CPPUNIT_ASSERT_MESSAGE("allocator idx order 2", nIdx2 < nIdx3 );
47 
48         aPool.free(nIdx2);
49         aPool.free(nIdx3);
50 
51         nIdx2 = aPool.alloc();
52         nIdx3 = aPool.alloc();
53 
54         CPPUNIT_ASSERT_MESSAGE("allocator idx order 1 after fragmentation", nIdx1 < nIdx3 );
55         CPPUNIT_ASSERT_MESSAGE("allocator idx order 2 after fragmentation", nIdx3 < nIdx2 );
56     }
57 
58     void testPoolValueSemantics()
59     {
60         vector_pool<int> aPool;
61 
62         std::ptrdiff_t nIdx1 = aPool.store(0);
63         CPPUNIT_ASSERT_MESSAGE("allocator value semantics 1", aPool.get(nIdx1) == 0 );
64 
65         std::ptrdiff_t nIdx2 = aPool.store(1);
66         CPPUNIT_ASSERT_MESSAGE("allocator value semantics 2", aPool.get(nIdx2) == 1 );
67 
68         std::ptrdiff_t nIdx3 = aPool.store(2);
69         CPPUNIT_ASSERT_MESSAGE("allocator value semantics 3", aPool.get(nIdx3) == 2 );
70 
71         aPool.free(nIdx2);
72         aPool.free(nIdx3);
73 
74         nIdx2 = aPool.store(1);
75         CPPUNIT_ASSERT_MESSAGE("allocator value semantics 2 after fragmentation", aPool.get(nIdx2) == 1 );
76 
77         nIdx3 = aPool.store(2);
78         CPPUNIT_ASSERT_MESSAGE("allocator value semantics 3 after fragmentation", aPool.get(nIdx3) == 2 );
79     }
80 
81     // Change the following lines only, if you add, remove or rename
82     // member functions of the current class,
83     // because these macros are need by auto register mechanism.
84 
85     CPPUNIT_TEST_SUITE(vector_pool_test);
86     CPPUNIT_TEST(testPoolBasics);
87     CPPUNIT_TEST(testPoolValueSemantics);
88     CPPUNIT_TEST_SUITE_END();
89 };
90 
91 // -----------------------------------------------------------------------------
92 CPPUNIT_TEST_SUITE_REGISTRATION(vector_pool_test);
93