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 constexpr uint16_t kPoolSize = 11;
62
63 typedef Pool<Entry, kPoolSize> EntryPool;
64
65 static Entry sNonPoolEntry;
66
VerifyEntry(EntryPool & aPool,const Entry & aEntry,bool aInitWithInstance)67 void VerifyEntry(EntryPool &aPool, const Entry &aEntry, bool aInitWithInstance)
68 {
69 uint16_t index;
70 const EntryPool &constPool = const_cast<const EntryPool &>(aPool);
71
72 VerifyOrQuit(aPool.IsPoolEntry(aEntry));
73 VerifyOrQuit(!aPool.IsPoolEntry(sNonPoolEntry), "Pool::IsPoolEntry() succeeded for non-pool entry");
74
75 index = aPool.GetIndexOf(aEntry);
76 VerifyOrQuit(&aPool.GetEntryAt(index) == &aEntry);
77 VerifyOrQuit(&constPool.GetEntryAt(index) == &aEntry);
78
79 VerifyOrQuit(aEntry.IsInitializedWithInstance() == aInitWithInstance, "Pool did not correctly Init() entry");
80 }
81
TestPool(EntryPool & aPool,bool aInitWithInstance)82 void TestPool(EntryPool &aPool, bool aInitWithInstance)
83 {
84 Entry *entries[kPoolSize];
85
86 VerifyOrQuit(aPool.GetSize() == kPoolSize);
87
88 for (Entry *&entry : entries)
89 {
90 entry = aPool.Allocate();
91 VerifyOrQuit(entry != nullptr, "Pool::Allocate() failed");
92
93 VerifyEntry(aPool, *entry, aInitWithInstance);
94 }
95
96 for (uint16_t numEntriesToFree = 1; numEntriesToFree <= kPoolSize; numEntriesToFree++)
97 {
98 VerifyOrQuit(aPool.Allocate() == nullptr, "Pool::Allocate() did not fail when all pool entries were allocated");
99
100 for (uint16_t i = 0; i < numEntriesToFree; i++)
101 {
102 VerifyEntry(aPool, *entries[i], aInitWithInstance);
103 aPool.Free(*entries[i]);
104 }
105
106 for (uint16_t i = 0; i < numEntriesToFree; i++)
107 {
108 entries[i] = aPool.Allocate();
109 VerifyOrQuit(entries[i] != nullptr, "Pool::Allocate() failed");
110
111 VerifyEntry(aPool, *entries[i], aInitWithInstance);
112 }
113 }
114
115 VerifyOrQuit(aPool.Allocate() == nullptr, "Pool::Allocate() did not fail when all pool entries were allocated");
116 }
117
TestPool(void)118 void TestPool(void)
119 {
120 Instance *instance = testInitInstance();
121 EntryPool pool1;
122 EntryPool pool2(*instance);
123
124 TestPool(pool1, /* aInitWithInstance */ false);
125 TestPool(pool2, /* aInitWithInstance */ true);
126
127 testFreeInstance(instance);
128 }
129
130 } // namespace ot
131
main(void)132 int main(void)
133 {
134 ot::TestPool();
135 printf("All tests passed\n");
136 return 0;
137 }
138