1 /*
2 * Copyright (c) 2020, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "test_platform.h"
30
31 #include <openthread/config.h>
32
33 #include "common/pool.hpp"
34 #include "instance/instance.hpp"
35
36 #include "test_util.h"
37
38 namespace ot {
39
40 struct EntryBase
41 {
42 EntryBase *mNext;
43 };
44
45 struct Entry : public EntryBase, LinkedListEntry<Entry>
46 {
47 public:
Entryot::Entry48 Entry(void)
49 : mInitWithInstance(false)
50 {
51 }
52
Initot::Entry53 void Init(Instance &) { mInitWithInstance = true; }
54
IsInitializedWithInstanceot::Entry55 bool IsInitializedWithInstance(void) const { return mInitWithInstance; }
56
57 private:
58 bool mInitWithInstance;
59 };
60
61 enum : uint16_t
62 {
63 kPoolSize = 11,
64 };
65
66 typedef Pool<Entry, kPoolSize> EntryPool;
67
68 static Entry sNonPoolEntry;
69
VerifyEntry(EntryPool & aPool,const Entry & aEntry,bool aInitWithInstance)70 void VerifyEntry(EntryPool &aPool, const Entry &aEntry, bool aInitWithInstance)
71 {
72 uint16_t index;
73 const EntryPool &constPool = const_cast<const EntryPool &>(aPool);
74
75 VerifyOrQuit(aPool.IsPoolEntry(aEntry));
76 VerifyOrQuit(!aPool.IsPoolEntry(sNonPoolEntry), "Pool::IsPoolEntry() succeeded for non-pool entry");
77
78 index = aPool.GetIndexOf(aEntry);
79 VerifyOrQuit(&aPool.GetEntryAt(index) == &aEntry);
80 VerifyOrQuit(&constPool.GetEntryAt(index) == &aEntry);
81
82 VerifyOrQuit(aEntry.IsInitializedWithInstance() == aInitWithInstance, "Pool did not correctly Init() entry");
83 }
84
TestPool(EntryPool & aPool,bool aInitWithInstance)85 void TestPool(EntryPool &aPool, bool aInitWithInstance)
86 {
87 Entry *entries[kPoolSize];
88
89 VerifyOrQuit(aPool.GetSize() == kPoolSize);
90
91 for (Entry *&entry : entries)
92 {
93 entry = aPool.Allocate();
94 VerifyOrQuit(entry != nullptr, "Pool::Allocate() failed");
95
96 VerifyEntry(aPool, *entry, aInitWithInstance);
97 }
98
99 for (uint16_t numEntriesToFree = 1; numEntriesToFree <= kPoolSize; numEntriesToFree++)
100 {
101 VerifyOrQuit(aPool.Allocate() == nullptr, "Pool::Allocate() did not fail when all pool entries were allocated");
102
103 for (uint16_t i = 0; i < numEntriesToFree; i++)
104 {
105 VerifyEntry(aPool, *entries[i], aInitWithInstance);
106 aPool.Free(*entries[i]);
107 }
108
109 for (uint16_t i = 0; i < numEntriesToFree; i++)
110 {
111 entries[i] = aPool.Allocate();
112 VerifyOrQuit(entries[i] != nullptr, "Pool::Allocate() failed");
113
114 VerifyEntry(aPool, *entries[i], aInitWithInstance);
115 }
116 }
117
118 VerifyOrQuit(aPool.Allocate() == nullptr, "Pool::Allocate() did not fail when all pool entries were allocated");
119 }
120
TestPool(void)121 void TestPool(void)
122 {
123 Instance *instance = testInitInstance();
124 EntryPool pool1;
125 EntryPool pool2(*instance);
126
127 TestPool(pool1, /* aInitWithInstance */ false);
128 TestPool(pool2, /* aInitWithInstance */ true);
129
130 testFreeInstance(instance);
131 }
132
133 } // namespace ot
134
main(void)135 int main(void)
136 {
137 ot::TestPool();
138 printf("All tests passed\n");
139 return 0;
140 }
141