xref: /haiku/src/tests/system/kernel/util/BOpenHashTableTest.cpp (revision 2112748284f2249e5b811947e2fbdc4b95ea95d6)
1aa7ac127SKyle Ambroff-Kao #include <cppunit/TestAssert.h>
2aa7ac127SKyle Ambroff-Kao #include <cppunit/TestCaller.h>
3aa7ac127SKyle Ambroff-Kao #include <cppunit/TestSuite.h>
4aa7ac127SKyle Ambroff-Kao #include <TestShell.h>
5aa7ac127SKyle Ambroff-Kao 
6aa7ac127SKyle Ambroff-Kao #include <os/support/ObjectList.h>
7aa7ac127SKyle Ambroff-Kao #include <shared/AutoDeleter.h>
8aa7ac127SKyle Ambroff-Kao #include <util/OpenHashTable.h>
9aa7ac127SKyle Ambroff-Kao 
10aa7ac127SKyle Ambroff-Kao #include "BOpenHashTableTest.h"
11aa7ac127SKyle Ambroff-Kao 
12aa7ac127SKyle Ambroff-Kao 
13aa7ac127SKyle Ambroff-Kao namespace {
14aa7ac127SKyle Ambroff-Kao 
15aa7ac127SKyle Ambroff-Kao class Entry {
16aa7ac127SKyle Ambroff-Kao public:
Entry(uint32_t value)17aa7ac127SKyle Ambroff-Kao 	Entry(uint32_t value)
18aa7ac127SKyle Ambroff-Kao 		:
19aa7ac127SKyle Ambroff-Kao 		fValue(value),
20aa7ac127SKyle Ambroff-Kao 		fNext(NULL)
21aa7ac127SKyle Ambroff-Kao 	{
22aa7ac127SKyle Ambroff-Kao 	}
23aa7ac127SKyle Ambroff-Kao 
Value() const24aa7ac127SKyle Ambroff-Kao 	const uint32_t Value() const
25aa7ac127SKyle Ambroff-Kao 	{
26aa7ac127SKyle Ambroff-Kao 		return fValue;
27aa7ac127SKyle Ambroff-Kao 	}
28aa7ac127SKyle Ambroff-Kao 
Next() const29aa7ac127SKyle Ambroff-Kao 	Entry* Next() const
30aa7ac127SKyle Ambroff-Kao 	{
31aa7ac127SKyle Ambroff-Kao 		return fNext;
32aa7ac127SKyle Ambroff-Kao 	}
33aa7ac127SKyle Ambroff-Kao 
34aa7ac127SKyle Ambroff-Kao private:
35aa7ac127SKyle Ambroff-Kao 	uint32_t	fValue;
36aa7ac127SKyle Ambroff-Kao 	Entry		*fNext;
37aa7ac127SKyle Ambroff-Kao 
38aa7ac127SKyle Ambroff-Kao 	friend class EntryDefinition;
39aa7ac127SKyle Ambroff-Kao };
40aa7ac127SKyle Ambroff-Kao 
41aa7ac127SKyle Ambroff-Kao 
42aa7ac127SKyle Ambroff-Kao class EntryDefinition {
43aa7ac127SKyle Ambroff-Kao public:
44aa7ac127SKyle Ambroff-Kao 	typedef uint32_t KeyType;
45aa7ac127SKyle Ambroff-Kao 	typedef Entry ValueType;
46aa7ac127SKyle Ambroff-Kao 
HashKey(const KeyType & key) const47aa7ac127SKyle Ambroff-Kao 	size_t HashKey(const KeyType& key) const
48aa7ac127SKyle Ambroff-Kao 	{
49aa7ac127SKyle Ambroff-Kao 		return key;
50aa7ac127SKyle Ambroff-Kao 	}
51aa7ac127SKyle Ambroff-Kao 
Hash(Entry * entry) const52aa7ac127SKyle Ambroff-Kao 	size_t Hash(Entry* entry) const
53aa7ac127SKyle Ambroff-Kao 	{
54aa7ac127SKyle Ambroff-Kao 		return entry->fValue;
55aa7ac127SKyle Ambroff-Kao 	}
56aa7ac127SKyle Ambroff-Kao 
Compare(const KeyType & key,Entry * entry) const57aa7ac127SKyle Ambroff-Kao 	bool Compare(const KeyType& key, Entry* entry) const
58aa7ac127SKyle Ambroff-Kao 	{
59aa7ac127SKyle Ambroff-Kao 		return key == entry->fValue;
60aa7ac127SKyle Ambroff-Kao 	}
61aa7ac127SKyle Ambroff-Kao 
GetLink(Entry * entry) const62aa7ac127SKyle Ambroff-Kao 	Entry*& GetLink(Entry* entry) const
63aa7ac127SKyle Ambroff-Kao 	{
64aa7ac127SKyle Ambroff-Kao 		return entry->fNext;
65aa7ac127SKyle Ambroff-Kao 	}
66aa7ac127SKyle Ambroff-Kao };
67aa7ac127SKyle Ambroff-Kao 
68aa7ac127SKyle Ambroff-Kao }
69aa7ac127SKyle Ambroff-Kao 
70aa7ac127SKyle Ambroff-Kao 
Suite()71aa7ac127SKyle Ambroff-Kao CppUnit::Test* BOpenHashTableTest::Suite()
72aa7ac127SKyle Ambroff-Kao {
73aa7ac127SKyle Ambroff-Kao 	CppUnit::TestSuite* suite = new CppUnit::TestSuite("BOpenHashTable");
74aa7ac127SKyle Ambroff-Kao 
75aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
76aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Insert test",
77aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::InsertTest));
78aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
79*21127482SKyle Ambroff-Kao 					   "BOpenHashTable::Insert unchecked test",
80*21127482SKyle Ambroff-Kao 					   &BOpenHashTableTest::InsertUncheckedTest));
81*21127482SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
82*21127482SKyle Ambroff-Kao 					   "BOpenHashTable::Insert unchecked uninitialized test",
83*21127482SKyle Ambroff-Kao 					   &BOpenHashTableTest::InsertUncheckedUninitializedTest));
84*21127482SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
85aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Iterate and count test",
86aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::IterateAndCountTest));
87aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
88aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Lookup test",
89aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::LookupTest));
90aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
91aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Resize test",
92aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::ResizeTest));
93aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
94aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Remove test",
95aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::RemoveTest));
96aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
97*21127482SKyle Ambroff-Kao 					   "BOpenHashTable::Remove unchecked test",
98*21127482SKyle Ambroff-Kao 					   &BOpenHashTableTest::RemoveUncheckedTest));
99*21127482SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
100*21127482SKyle Ambroff-Kao 					   "BOpenHashTable::Remove when not present test",
101*21127482SKyle Ambroff-Kao 					   &BOpenHashTableTest::RemoveWhenNotPresentTest));
102*21127482SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
103aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Duplicate insert test",
104aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::DuplicateInsertTest));
105aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
106aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Disable auto expand",
107aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::DisableAutoExpandTest));
108aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
109aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Init with zero size",
110aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::InitWithZeroSizeTest));
111aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
112aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Clear test",
113aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::ClearTest));
114aa7ac127SKyle Ambroff-Kao 	suite->addTest(new CppUnit::TestCaller<BOpenHashTableTest>(
115aa7ac127SKyle Ambroff-Kao 					   "BOpenHashTable::Clear and return test",
116aa7ac127SKyle Ambroff-Kao 					   &BOpenHashTableTest::ClearAndReturnTest));
117aa7ac127SKyle Ambroff-Kao 
118aa7ac127SKyle Ambroff-Kao 	return suite;
119aa7ac127SKyle Ambroff-Kao }
120aa7ac127SKyle Ambroff-Kao 
121aa7ac127SKyle Ambroff-Kao 
BOpenHashTableTest(std::string name)122aa7ac127SKyle Ambroff-Kao BOpenHashTableTest::BOpenHashTableTest(std::string name)
123aa7ac127SKyle Ambroff-Kao 	: BTestCase(name)
124aa7ac127SKyle Ambroff-Kao {
125aa7ac127SKyle Ambroff-Kao }
126aa7ac127SKyle Ambroff-Kao 
127aa7ac127SKyle Ambroff-Kao 
InsertTest()128aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::InsertTest()
129aa7ac127SKyle Ambroff-Kao {
130aa7ac127SKyle Ambroff-Kao 	Entry entry(123);
131aa7ac127SKyle Ambroff-Kao 
132aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
133aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(), B_OK);
134aa7ac127SKyle Ambroff-Kao 
135aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Insert(&entry), B_OK);
136aa7ac127SKyle Ambroff-Kao }
137aa7ac127SKyle Ambroff-Kao 
138aa7ac127SKyle Ambroff-Kao 
InsertUncheckedTest()139*21127482SKyle Ambroff-Kao void BOpenHashTableTest::InsertUncheckedTest()
140*21127482SKyle Ambroff-Kao {
141*21127482SKyle Ambroff-Kao 	Entry entry(123);
142*21127482SKyle Ambroff-Kao 
143*21127482SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
144*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(), B_OK);
145*21127482SKyle Ambroff-Kao 
146*21127482SKyle Ambroff-Kao 	table.InsertUnchecked(&entry);
147*21127482SKyle Ambroff-Kao }
148*21127482SKyle Ambroff-Kao 
149*21127482SKyle Ambroff-Kao 
InsertUncheckedUninitializedTest()150*21127482SKyle Ambroff-Kao void BOpenHashTableTest::InsertUncheckedUninitializedTest()
151*21127482SKyle Ambroff-Kao {
152*21127482SKyle Ambroff-Kao 	Entry entry(123);
153*21127482SKyle Ambroff-Kao 
154*21127482SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
155*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(), B_OK);
156*21127482SKyle Ambroff-Kao 
157*21127482SKyle Ambroff-Kao 	table.InsertUnchecked(&entry);
158*21127482SKyle Ambroff-Kao }
159*21127482SKyle Ambroff-Kao 
160*21127482SKyle Ambroff-Kao 
IterateAndCountTest()161aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::IterateAndCountTest() {
162aa7ac127SKyle Ambroff-Kao 	const size_t kEntryCount = 20;
163aa7ac127SKyle Ambroff-Kao 
164aa7ac127SKyle Ambroff-Kao 	BObjectList<Entry> entries(20, true);
165aa7ac127SKyle Ambroff-Kao 
166aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
167aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(kEntryCount * 2), B_OK);
168aa7ac127SKyle Ambroff-Kao 
169aa7ac127SKyle Ambroff-Kao 	for (uint32_t i = 0; i < kEntryCount; ++i) {
170aa7ac127SKyle Ambroff-Kao 		Entry* entry = new Entry(i);
171aa7ac127SKyle Ambroff-Kao 		entries.AddItem(entry);
172aa7ac127SKyle Ambroff-Kao 		CPPUNIT_ASSERT_EQUAL(table.Insert(entry), B_OK);
173aa7ac127SKyle Ambroff-Kao 	}
174aa7ac127SKyle Ambroff-Kao 
175aa7ac127SKyle Ambroff-Kao 	// Verify that the table contains the expected values.
176aa7ac127SKyle Ambroff-Kao 	uint64_t map = 0;
177aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition>::Iterator iterator = table.GetIterator();
178aa7ac127SKyle Ambroff-Kao 	while (iterator.HasNext()) {
179aa7ac127SKyle Ambroff-Kao 		Entry* entry = iterator.Next();
180aa7ac127SKyle Ambroff-Kao 		CPPUNIT_ASSERT_EQUAL(0, map & (1 << entry->Value()));
181*21127482SKyle Ambroff-Kao 		map |= (1 << entry->Value());
182aa7ac127SKyle Ambroff-Kao 	}
183aa7ac127SKyle Ambroff-Kao 
184aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(map, (1 << kEntryCount) - 1);
185aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(kEntryCount, table.CountElements());
186aa7ac127SKyle Ambroff-Kao }
187aa7ac127SKyle Ambroff-Kao 
188aa7ac127SKyle Ambroff-Kao 
ResizeTest()189aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::ResizeTest()
190aa7ac127SKyle Ambroff-Kao {
191aa7ac127SKyle Ambroff-Kao 	// This is the same as IterateAndCountTest, except that the table will
192aa7ac127SKyle Ambroff-Kao 	// be resized mid insertion.
193aa7ac127SKyle Ambroff-Kao 	const size_t kEntryCount = 20;
194aa7ac127SKyle Ambroff-Kao 	BObjectList<Entry> entries(20, true);
195aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
196aa7ac127SKyle Ambroff-Kao 
197aa7ac127SKyle Ambroff-Kao 	// Start off with capacity for 8 elements. This will mean that the table
198aa7ac127SKyle Ambroff-Kao 	// will be resized in the loop below since we are inserting 20 elements.
199aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(8), B_OK);
200aa7ac127SKyle Ambroff-Kao 
201aa7ac127SKyle Ambroff-Kao 	for (uint32_t i = 0; i < kEntryCount; ++i) {
202aa7ac127SKyle Ambroff-Kao 		Entry* entry = new Entry(i);
203aa7ac127SKyle Ambroff-Kao 		entries.AddItem(entry);
204aa7ac127SKyle Ambroff-Kao 		CPPUNIT_ASSERT_EQUAL(table.Insert(entry), B_OK);
205aa7ac127SKyle Ambroff-Kao 	}
206aa7ac127SKyle Ambroff-Kao 
207aa7ac127SKyle Ambroff-Kao 	// Verify that after resize the expected elements are present within
208aa7ac127SKyle Ambroff-Kao 	// the table.
209aa7ac127SKyle Ambroff-Kao 	uint64_t map = 0;
210aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition>::Iterator iterator = table.GetIterator();
211aa7ac127SKyle Ambroff-Kao 	while (iterator.HasNext()) {
212aa7ac127SKyle Ambroff-Kao 		Entry* entry = iterator.Next();
213aa7ac127SKyle Ambroff-Kao 		CPPUNIT_ASSERT_EQUAL(0, map & (1 << entry->Value()));
214aa7ac127SKyle Ambroff-Kao 		map |= (1 << entry->Value());
215aa7ac127SKyle Ambroff-Kao 	}
216aa7ac127SKyle Ambroff-Kao 
217aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(map, (1 << kEntryCount) - 1);
218aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(kEntryCount, table.CountElements());
219aa7ac127SKyle Ambroff-Kao }
220aa7ac127SKyle Ambroff-Kao 
221aa7ac127SKyle Ambroff-Kao 
LookupTest()222aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::LookupTest() {
223aa7ac127SKyle Ambroff-Kao 	Entry entry(123);
224aa7ac127SKyle Ambroff-Kao 
225aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
226aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(0), B_OK);
227aa7ac127SKyle Ambroff-Kao 
228aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Insert(&entry), B_OK);
229aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry);
230aa7ac127SKyle Ambroff-Kao }
231aa7ac127SKyle Ambroff-Kao 
232aa7ac127SKyle Ambroff-Kao 
RemoveTest()233aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::RemoveTest() {
234aa7ac127SKyle Ambroff-Kao 	Entry entry(123);
235aa7ac127SKyle Ambroff-Kao 
236aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
237aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(0), B_OK);
238aa7ac127SKyle Ambroff-Kao 
239aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Insert(&entry), B_OK);
240aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry);
241aa7ac127SKyle Ambroff-Kao 
242aa7ac127SKyle Ambroff-Kao 	table.Remove(&entry);
243aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), NULL);
244aa7ac127SKyle Ambroff-Kao }
245aa7ac127SKyle Ambroff-Kao 
246aa7ac127SKyle Ambroff-Kao 
RemoveUncheckedTest()247*21127482SKyle Ambroff-Kao void BOpenHashTableTest::RemoveUncheckedTest()
248*21127482SKyle Ambroff-Kao {
249*21127482SKyle Ambroff-Kao 	Entry entry(123);
250*21127482SKyle Ambroff-Kao 
251*21127482SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
252*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(0), B_OK);
253*21127482SKyle Ambroff-Kao 
254*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Insert(&entry), B_OK);
255*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry);
256*21127482SKyle Ambroff-Kao 
257*21127482SKyle Ambroff-Kao 	table.RemoveUnchecked(&entry);
258*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), NULL);
259*21127482SKyle Ambroff-Kao }
260*21127482SKyle Ambroff-Kao 
261*21127482SKyle Ambroff-Kao 
RemoveWhenNotPresentTest()262*21127482SKyle Ambroff-Kao void BOpenHashTableTest::RemoveWhenNotPresentTest()
263*21127482SKyle Ambroff-Kao {
264*21127482SKyle Ambroff-Kao 	Entry entry1(123);
265*21127482SKyle Ambroff-Kao 	Entry entry2(456);
266*21127482SKyle Ambroff-Kao 	Entry entry3(789);
267*21127482SKyle Ambroff-Kao 
268*21127482SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
269*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(), B_OK);
270*21127482SKyle Ambroff-Kao 
271*21127482SKyle Ambroff-Kao 	// Only add the first two entries.
272*21127482SKyle Ambroff-Kao 	table.Insert(&entry1);
273*21127482SKyle Ambroff-Kao 	table.Insert(&entry2);
274*21127482SKyle Ambroff-Kao 
275*21127482SKyle Ambroff-Kao 	// entry3 is not in the table, but we'll remove it anyway.
276*21127482SKyle Ambroff-Kao 	table.Remove(&entry3);
277*21127482SKyle Ambroff-Kao 	table.RemoveUnchecked(&entry3);
278*21127482SKyle Ambroff-Kao 
279*21127482SKyle Ambroff-Kao 	// The original two entries should still be there.
280*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.CountElements(), 2);
281*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry1);
282*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(456), &entry2);
283*21127482SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(789), NULL);
284*21127482SKyle Ambroff-Kao }
285*21127482SKyle Ambroff-Kao 
286*21127482SKyle Ambroff-Kao 
DuplicateInsertTest()287aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::DuplicateInsertTest()
288aa7ac127SKyle Ambroff-Kao {
289aa7ac127SKyle Ambroff-Kao 	Entry entry(123);
290aa7ac127SKyle Ambroff-Kao 
291aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition, true, true> table;
292aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(0), B_OK);
293aa7ac127SKyle Ambroff-Kao 
294aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Insert(&entry), B_OK);
295aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry);
296aa7ac127SKyle Ambroff-Kao 
297aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_DEBUGGER(table.Insert(&entry));
298aa7ac127SKyle Ambroff-Kao 
299aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry);
300aa7ac127SKyle Ambroff-Kao 
301aa7ac127SKyle Ambroff-Kao 	// The item can basically never be removed now since there is a cycle,
302aa7ac127SKyle Ambroff-Kao 	// but we'll break into the debugger on remove when that happens as well.
303aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_DEBUGGER(table.Remove(&entry));
304aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry);
305aa7ac127SKyle Ambroff-Kao 
306aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_DEBUGGER(table.Remove(&entry));
307aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry);
308aa7ac127SKyle Ambroff-Kao }
309aa7ac127SKyle Ambroff-Kao 
310aa7ac127SKyle Ambroff-Kao 
DisableAutoExpandTest()311aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::DisableAutoExpandTest()
312aa7ac127SKyle Ambroff-Kao {
313aa7ac127SKyle Ambroff-Kao 	// Insert multiple items into a table with a fixed size of 1. This
314aa7ac127SKyle Ambroff-Kao 	// essentially turns this BOpenHashTable into a linked list, since resize
315aa7ac127SKyle Ambroff-Kao 	// will never occur.
316aa7ac127SKyle Ambroff-Kao 	Entry entry1(123);
317aa7ac127SKyle Ambroff-Kao 	Entry entry2(456);
318aa7ac127SKyle Ambroff-Kao 
319aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition, false> table;
320aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(1), B_OK);
321aa7ac127SKyle Ambroff-Kao 
322aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Insert(&entry1), B_OK);
323aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Insert(&entry2), B_OK);
324aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.CountElements(), 2);
325aa7ac127SKyle Ambroff-Kao }
326aa7ac127SKyle Ambroff-Kao 
327aa7ac127SKyle Ambroff-Kao 
InitWithZeroSizeTest()328aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::InitWithZeroSizeTest()
329aa7ac127SKyle Ambroff-Kao {
330aa7ac127SKyle Ambroff-Kao 	Entry entry(123);
331aa7ac127SKyle Ambroff-Kao 
332aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
333aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(0), B_OK);
334aa7ac127SKyle Ambroff-Kao 
335aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Insert(&entry), B_OK);
336aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(123), &entry);
337aa7ac127SKyle Ambroff-Kao }
338aa7ac127SKyle Ambroff-Kao 
339aa7ac127SKyle Ambroff-Kao 
ClearTest()340aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::ClearTest()
341aa7ac127SKyle Ambroff-Kao {
342aa7ac127SKyle Ambroff-Kao 	const size_t kEntryCount = 3;
343aa7ac127SKyle Ambroff-Kao 
344aa7ac127SKyle Ambroff-Kao 	BObjectList<Entry> entries(20, true);
345aa7ac127SKyle Ambroff-Kao 
346aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
347aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(), B_OK);
348aa7ac127SKyle Ambroff-Kao 
349aa7ac127SKyle Ambroff-Kao 	for (uint32_t i = 0; i < kEntryCount; ++i) {
350aa7ac127SKyle Ambroff-Kao 		Entry* entry = new Entry(i);
351aa7ac127SKyle Ambroff-Kao 		entries.AddItem(entry);
352aa7ac127SKyle Ambroff-Kao 		CPPUNIT_ASSERT_EQUAL(table.Insert(entry), B_OK);
353aa7ac127SKyle Ambroff-Kao 	}
354aa7ac127SKyle Ambroff-Kao 
355aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.CountElements(), kEntryCount);
356aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT(table.Lookup(2) != NULL);
357aa7ac127SKyle Ambroff-Kao 
358aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Clear(false), NULL);
359aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.CountElements(), 0);
360aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(2), NULL);
361aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.GetIterator().HasNext(), false);
362aa7ac127SKyle Ambroff-Kao }
363aa7ac127SKyle Ambroff-Kao 
364aa7ac127SKyle Ambroff-Kao 
ClearAndReturnTest()365aa7ac127SKyle Ambroff-Kao void BOpenHashTableTest::ClearAndReturnTest()
366aa7ac127SKyle Ambroff-Kao {
367aa7ac127SKyle Ambroff-Kao 	// Same as ClearTest(), except that Clear(true) is called, which tells
368aa7ac127SKyle Ambroff-Kao 	// the BOpenHashTable<T> to return a linked list of entries before clearing
369aa7ac127SKyle Ambroff-Kao 	// the table.
370aa7ac127SKyle Ambroff-Kao 	const size_t kEntryCount = 3;
371aa7ac127SKyle Ambroff-Kao 	BOpenHashTable<EntryDefinition> table;
372aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Init(), B_OK);
373aa7ac127SKyle Ambroff-Kao 
374aa7ac127SKyle Ambroff-Kao 	for (uint32_t i = 0; i < kEntryCount; ++i) {
375aa7ac127SKyle Ambroff-Kao 		Entry* entry = new Entry(i);
376aa7ac127SKyle Ambroff-Kao 		CPPUNIT_ASSERT_EQUAL(table.Insert(entry), B_OK);
377aa7ac127SKyle Ambroff-Kao 	}
378aa7ac127SKyle Ambroff-Kao 
379aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.CountElements(), kEntryCount);
380aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT(table.Lookup(2) != NULL);
381aa7ac127SKyle Ambroff-Kao 
382aa7ac127SKyle Ambroff-Kao 	Entry* head = table.Clear(true);
383aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT(head != NULL);
384aa7ac127SKyle Ambroff-Kao 
385aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.CountElements(), 0);
386aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.Lookup(2), NULL);
387aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(table.GetIterator().HasNext(), false);
388aa7ac127SKyle Ambroff-Kao 
389aa7ac127SKyle Ambroff-Kao 	size_t items_returned = 0;
390aa7ac127SKyle Ambroff-Kao 	while (head != NULL) {
391aa7ac127SKyle Ambroff-Kao 		Entry* next = head->Next();
392aa7ac127SKyle Ambroff-Kao 		delete head;
393aa7ac127SKyle Ambroff-Kao 		head = next;
394aa7ac127SKyle Ambroff-Kao 
395aa7ac127SKyle Ambroff-Kao 		++items_returned;
396aa7ac127SKyle Ambroff-Kao 	}
397aa7ac127SKyle Ambroff-Kao 
398aa7ac127SKyle Ambroff-Kao 	CPPUNIT_ASSERT_EQUAL(items_returned, kEntryCount);
399aa7ac127SKyle Ambroff-Kao }
400