3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2016-2017 Nest Labs, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * Unit tests for the Chip Pool API.
28 #include <support/Pool.h>
29 #include <support/UnitTestRegistration.h>
31 #include <nlunit-test.h>
35 template <class T, size_t N>
36 size_t GetNumObjectsInUse(BitMapObjectPool<T, N> & pool)
39 pool.ForEachActiveObject([&count](void *) {
52 void TestReleaseNull(nlTestSuite * inSuite, void * inContext)
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);
61 void TestCreateReleaseObject(nlTestSuite * inSuite, void * inContext)
63 constexpr const size_t size = 100;
64 BitMapObjectPool<uint32_t, size> pool;
66 for (size_t i = 0; i < pool.Size(); ++i)
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);
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());
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());
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());
95 for (size_t i = 0; i < pool.Size(); ++i)
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);
103 void TestCreateReleaseStruct(nlTestSuite * inSuite, void * inContext)
107 S(std::set<S *> & set) : mSet(set) { mSet.insert(this); }
108 ~S() { mSet.erase(this); }
109 std::set<S *> & mSet;
114 constexpr const size_t size = 100;
115 BitMapObjectPool<S, size> pool;
117 for (size_t i = 0; i < pool.Size(); ++i)
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());
124 for (size_t i = 0; i < pool.Size(); ++i)
126 pool.ReleaseObject(objs2[i]);
127 NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == size - i - 1);
128 NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == objs1.size());
132 int Setup(void * inContext)
137 int Teardown(void * inContext)
144 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
146 * Test Suite. It lists all the test functions.
148 static const nlTest sTests[] = { NL_TEST_DEF_FN(TestReleaseNull), NL_TEST_DEF_FN(TestCreateReleaseObject),
149 NL_TEST_DEF_FN(TestCreateReleaseStruct), NL_TEST_SENTINEL() };
153 nlTestSuite theSuite = { "CHIP Pool tests", &sTests[0], Setup, Teardown };
155 // Run test suit againt one context.
156 nlTestRunner(&theSuite, nullptr);
157 return nlTestRunnerStats(&theSuite);
160 CHIP_REGISTER_TEST_SUITE(TestPool);