xref: /trunk/main/o3tl/qa/test-vector_pool.cxx (revision 18cf0442)
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 "gtest/gtest.h"
26 #include "postextstl.h"
27 
28 #include <o3tl/vector_pool.hxx>
29 
30 using namespace ::o3tl;
31 
32 class vector_pool_test : public ::testing::Test
33 {
34 public:
35 };
36 
TEST_F(vector_pool_test,testPoolBasics)37 TEST_F(vector_pool_test, 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     ASSERT_TRUE(nIdx1 < nIdx2) << "allocator idx order 1";
46     ASSERT_TRUE(nIdx2 < nIdx3) << "allocator idx order 2";
47 
48     aPool.free(nIdx2);
49     aPool.free(nIdx3);
50 
51     nIdx2 = aPool.alloc();
52     nIdx3 = aPool.alloc();
53 
54     ASSERT_TRUE(nIdx1 < nIdx3) << "allocator idx order 1 after fragmentation";
55     ASSERT_TRUE(nIdx3 < nIdx2) << "allocator idx order 2 after fragmentation";
56 }
57 
TEST_F(vector_pool_test,testPoolValueSemantics)58 TEST_F(vector_pool_test, testPoolValueSemantics)
59 {
60     vector_pool<int> aPool;
61 
62     std::ptrdiff_t nIdx1 = aPool.store(0);
63     ASSERT_TRUE(aPool.get(nIdx1) == 0) << "allocator value semantics 1";
64 
65     std::ptrdiff_t nIdx2 = aPool.store(1);
66     ASSERT_TRUE(aPool.get(nIdx2) == 1) << "allocator value semantics 2";
67 
68     std::ptrdiff_t nIdx3 = aPool.store(2);
69     ASSERT_TRUE(aPool.get(nIdx3) == 2) << "allocator value semantics 3";
70 
71     aPool.free(nIdx2);
72     aPool.free(nIdx3);
73 
74     nIdx2 = aPool.store(1);
75     ASSERT_TRUE(aPool.get(nIdx2) == 1) << "allocator value semantics 2 after fragmentation";
76 
77     nIdx3 = aPool.store(2);
78     ASSERT_TRUE(aPool.get(nIdx3) == 2) << "allocator value semantics 3 after fragmentation";
79 }
80 
81