forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableTest.cpp
More file actions
executable file
·207 lines (167 loc) · 4.74 KB
/
HashTableTest.cpp
File metadata and controls
executable file
·207 lines (167 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// HashTableTest.cpp
//
// $Id: //poco/1.4/Foundation/testsuite/src/HashTableTest.cpp#1 $
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HashTableTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/HashTable.h"
#include "Poco/NumberFormatter.h"
GCC_DIAG_OFF(unused-variable)
using namespace Poco;
HashTableTest::HashTableTest(const std::string& name): CppUnit::TestCase(name)
{
}
HashTableTest::~HashTableTest()
{
}
void HashTableTest::testInsert()
{
std::string s1("str1");
std::string s2("str2");
HashTable<std::string, int> hashTable;
assert (!hashTable.exists(s1));
hashTable.insert(s1, 13);
assert (hashTable.exists(s1));
assert (hashTable.get(s1) == 13);
int retVal = 0;
assert (hashTable.get(s1, retVal));
assert (retVal == 13);
try
{
hashTable.insert(s1, 22);
failmsg ("duplicate insert must fail");
}
catch (Exception&){}
try
{
hashTable.get(s2);
failmsg ("getting a non inserted item must fail");
}
catch (Exception&){}
assert (!hashTable.exists(s2));
hashTable.insert(s2, 13);
assert (hashTable.exists(s2));
}
void HashTableTest::testUpdate()
{
// add code for second test here
std::string s1("str1");
std::string s2("str2");
HashTable<std::string, int> hashTable;
hashTable.insert(s1, 13);
hashTable.update(s1, 14);
assert (hashTable.exists(s1));
assert (hashTable.get(s1) == 14);
int retVal = 0;
assert (hashTable.get(s1, retVal));
assert (retVal == 14);
// updating a non existing item must work too
hashTable.update(s2, 15);
assert (hashTable.get(s2) == 15);
}
void HashTableTest::testOverflow()
{
HashTable<std::string, int> hashTable(13);
for (int i = 0; i < 1024; ++i)
{
hashTable.insert(Poco::NumberFormatter::format(i), i*i);
}
for (int i = 0; i < 1024; ++i)
{
std::string tmp = Poco::NumberFormatter::format(i);
assert (hashTable.exists(tmp));
assert (hashTable.get(tmp) == i*i);
}
}
void HashTableTest::testSize()
{
HashTable<std::string, int> hashTable(13);
assert (hashTable.size() == 0);
Poco::UInt32 h1 = hashTable.insert("1", 1);
assert (hashTable.size() == 1);
Poco::UInt32 h2 = hashTable.update("2", 2);
assert (hashTable.size() == 2);
hashTable.remove("1");
assert (hashTable.size() == 1);
hashTable.remove("3");
assert (hashTable.size() == 1);
hashTable.removeRaw("2", h2);
assert (hashTable.size() == 0);
hashTable.insert("1", 1);
hashTable.insert("2", 2);
assert (hashTable.size() == 2);
hashTable.clear();
assert (hashTable.size() == 0);
}
void HashTableTest::testResize()
{
HashTable<std::string, int> hashTable(13);
assert (hashTable.size() == 0);
hashTable.resize(19);
for (int i = 0; i < 1024; ++i)
{
hashTable.insert(Poco::NumberFormatter::format(i), i*i);
}
hashTable.resize(1009);
for (int i = 0; i < 1024; ++i)
{
std::string tmp = Poco::NumberFormatter::format(i);
assert (hashTable.exists(tmp));
assert (hashTable.get(tmp) == i*i);
}
}
void HashTableTest::testStatistic()
{
double relax = 0.001;
HashTable<std::string, int> hashTable(13);
assert (hashTable.size() == 0);
HashStatistic stat1(hashTable.currentState());
assert (stat1.avgEntriesPerHash() < relax && stat1.avgEntriesPerHash() > -relax);
assert (stat1.maxPositionsOfTable() == 13);
assert (stat1.maxEntriesPerHash() == 0);
hashTable.resize(19);
stat1 = hashTable.currentState(true);
assert (stat1.avgEntriesPerHash() < relax && stat1.avgEntriesPerHash() > -relax);
assert (stat1.maxPositionsOfTable() == 19);
assert (stat1.maxEntriesPerHash() == 0);
assert (stat1.detailedEntriesPerHash().size() == 19);
for (int i = 0; i < 1024; ++i)
{
hashTable.insert(Poco::NumberFormatter::format(i), i*i);
}
stat1 = hashTable.currentState(true);
double expAvg = 1024.0/ 19;
assert (stat1.avgEntriesPerHash() < (expAvg + relax) && stat1.avgEntriesPerHash() > (expAvg - relax));
assert (stat1.maxPositionsOfTable() == 19);
assert (stat1.maxEntriesPerHash() > expAvg);
hashTable.resize(1009);
stat1 = hashTable.currentState(true);
expAvg = 1024.0/ 1009;
assert (stat1.avgEntriesPerHash() < (expAvg + relax) && stat1.avgEntriesPerHash() > (expAvg - relax));
assert (stat1.maxPositionsOfTable() == 1009);
assert (stat1.maxEntriesPerHash() > expAvg);
}
void HashTableTest::setUp()
{
}
void HashTableTest::tearDown()
{
}
CppUnit::Test* HashTableTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HashTableTest");
CppUnit_addTest(pSuite, HashTableTest, testInsert);
CppUnit_addTest(pSuite, HashTableTest, testUpdate);
CppUnit_addTest(pSuite, HashTableTest, testOverflow);
CppUnit_addTest(pSuite, HashTableTest, testSize);
CppUnit_addTest(pSuite, HashTableTest, testResize);
CppUnit_addTest(pSuite, HashTableTest, testStatistic);
return pSuite;
}