Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestPool.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    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, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *      Unit tests for the Chip Pool API.
23  *
24  */
25
26 #include <set>
27
28 #include <support/Pool.h>
29 #include <support/UnitTestRegistration.h>
30
31 #include <nlunit-test.h>
32
33 namespace chip {
34
35 template <class T, size_t N>
36 size_t GetNumObjectsInUse(BitMapObjectPool<T, N> & pool)
37 {
38     size_t count = 0;
39     pool.ForEachActiveObject([&count](void *) {
40         ++count;
41         return true;
42     });
43     return count;
44 }
45
46 } // namespace chip
47
48 namespace {
49
50 using namespace chip;
51
52 void TestReleaseNull(nlTestSuite * inSuite, void * inContext)
53 {
54     constexpr const size_t size = 10;
55     BitMapObjectPool<uint32_t, size> pool;
56     pool.ReleaseObject(nullptr);
57     NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == 0);
58     NL_TEST_ASSERT(inSuite, pool.Allocated() == 0);
59 }
60
61 void TestCreateReleaseObject(nlTestSuite * inSuite, void * inContext)
62 {
63     constexpr const size_t size = 100;
64     BitMapObjectPool<uint32_t, size> pool;
65     uint32_t * obj[size];
66     for (size_t i = 0; i < pool.Size(); ++i)
67     {
68         obj[i] = pool.CreateObject();
69         NL_TEST_ASSERT(inSuite, obj[i] != nullptr);
70         NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == i + 1);
71         NL_TEST_ASSERT(inSuite, pool.Allocated() == i + 1);
72     }
73
74     uint32_t * fail = pool.CreateObject();
75     NL_TEST_ASSERT(inSuite, fail == nullptr);
76     NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == size);
77     NL_TEST_ASSERT(inSuite, pool.Allocated() == size);
78     NL_TEST_ASSERT(inSuite, pool.Exhausted());
79
80     pool.ReleaseObject(obj[55]);
81     NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == size - 1);
82     NL_TEST_ASSERT(inSuite, pool.Allocated() == size - 1);
83     NL_TEST_ASSERT(inSuite, !pool.Exhausted());
84     NL_TEST_ASSERT(inSuite, obj[55] == pool.CreateObject());
85     NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == size);
86     NL_TEST_ASSERT(inSuite, pool.Allocated() == size);
87     NL_TEST_ASSERT(inSuite, pool.Exhausted());
88
89     fail = pool.CreateObject();
90     NL_TEST_ASSERT(inSuite, fail == nullptr);
91     NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == size);
92     NL_TEST_ASSERT(inSuite, pool.Allocated() == size);
93     NL_TEST_ASSERT(inSuite, pool.Exhausted());
94
95     for (size_t i = 0; i < pool.Size(); ++i)
96     {
97         pool.ReleaseObject(obj[i]);
98         NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == size - i - 1);
99         NL_TEST_ASSERT(inSuite, pool.Allocated() == size - i - 1);
100     }
101 }
102
103 void TestCreateReleaseStruct(nlTestSuite * inSuite, void * inContext)
104 {
105     struct S
106     {
107         S(std::set<S *> & set) : mSet(set) { mSet.insert(this); }
108         ~S() { mSet.erase(this); }
109         std::set<S *> & mSet;
110     };
111
112     std::set<S *> objs1;
113
114     constexpr const size_t size = 100;
115     BitMapObjectPool<S, size> pool;
116     S * objs2[size];
117     for (size_t i = 0; i < pool.Size(); ++i)
118     {
119         objs2[i] = pool.CreateObject(objs1);
120         NL_TEST_ASSERT(inSuite, objs2[i] != nullptr);
121         NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == i + 1);
122         NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == objs1.size());
123     }
124     for (size_t i = 0; i < pool.Size(); ++i)
125     {
126         pool.ReleaseObject(objs2[i]);
127         NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == size - i - 1);
128         NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == objs1.size());
129     }
130 }
131
132 int Setup(void * inContext)
133 {
134     return SUCCESS;
135 }
136
137 int Teardown(void * inContext)
138 {
139     return SUCCESS;
140 }
141
142 } // namespace
143
144 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
145 /**
146  *   Test Suite. It lists all the test functions.
147  */
148 static const nlTest sTests[] = { NL_TEST_DEF_FN(TestReleaseNull), NL_TEST_DEF_FN(TestCreateReleaseObject),
149                                  NL_TEST_DEF_FN(TestCreateReleaseStruct), NL_TEST_SENTINEL() };
150
151 int TestPool()
152 {
153     nlTestSuite theSuite = { "CHIP Pool tests", &sTests[0], Setup, Teardown };
154
155     // Run test suit againt one context.
156     nlTestRunner(&theSuite, nullptr);
157     return nlTestRunnerStats(&theSuite);
158 }
159
160 CHIP_REGISTER_TEST_SUITE(TestPool);