1 | // |
2 | // SimpleHashTableTest.cpp |
3 | // |
4 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "SimpleHashTableTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/SimpleHashTable.h" |
15 | #include "Poco/NumberFormatter.h" |
16 | |
17 | |
18 | using namespace Poco; |
19 | |
20 | |
21 | SimpleHashTableTest::SimpleHashTableTest(const std::string& rName): CppUnit::TestCase(rName) |
22 | { |
23 | } |
24 | |
25 | |
26 | SimpleHashTableTest::~SimpleHashTableTest() |
27 | { |
28 | } |
29 | |
30 | |
31 | void SimpleHashTableTest::testInsert() |
32 | { |
33 | std::string s1("str1" ); |
34 | std::string s2("str2" ); |
35 | SimpleHashTable<std::string, int> hashTable; |
36 | assertTrue (!hashTable.exists(s1)); |
37 | hashTable.insert(s1, 13); |
38 | assertTrue (hashTable.exists(s1)); |
39 | assertTrue (hashTable.get(s1) == 13); |
40 | int retVal = 0; |
41 | |
42 | assertTrue (hashTable.get(s1, retVal)); |
43 | assertTrue (retVal == 13); |
44 | try |
45 | { |
46 | hashTable.insert(s1, 22); |
47 | failmsg ("duplicate insert must fail" ); |
48 | } |
49 | catch (Exception&){} |
50 | try |
51 | { |
52 | hashTable.get(s2); |
53 | failmsg ("getting a non inserted item must fail" ); |
54 | } |
55 | catch (Exception&){} |
56 | |
57 | assertTrue (!hashTable.exists(s2)); |
58 | hashTable.insert(s2, 13); |
59 | assertTrue (hashTable.exists(s2)); |
60 | } |
61 | |
62 | |
63 | void SimpleHashTableTest::testUpdate() |
64 | { |
65 | // add code for second test here |
66 | std::string s1("str1" ); |
67 | std::string s2("str2" ); |
68 | SimpleHashTable<std::string, int> hashTable; |
69 | hashTable.insert(s1, 13); |
70 | hashTable.update(s1, 14); |
71 | assertTrue (hashTable.exists(s1)); |
72 | assertTrue (hashTable.get(s1) == 14); |
73 | int retVal = 0; |
74 | |
75 | assertTrue (hashTable.get(s1, retVal)); |
76 | assertTrue (retVal == 14); |
77 | |
78 | // updating a non existing item must work too |
79 | hashTable.update(s2, 15); |
80 | assertTrue (hashTable.get(s2) == 15); |
81 | } |
82 | |
83 | |
84 | void SimpleHashTableTest::testOverflow() |
85 | { |
86 | SimpleHashTable<std::string, int> hashTable(31); |
87 | for (int i = 0; i < 31; ++i) |
88 | { |
89 | hashTable.insert(Poco::NumberFormatter::format(i), i*i); |
90 | } |
91 | |
92 | for (int i = 0; i < 31; ++i) |
93 | { |
94 | std::string tmp = Poco::NumberFormatter::format(i); |
95 | assertTrue (hashTable.exists(tmp)); |
96 | assertTrue (hashTable.get(tmp) == i*i); |
97 | } |
98 | } |
99 | |
100 | |
101 | void SimpleHashTableTest::testSize() |
102 | { |
103 | SimpleHashTable<std::string, int> hashTable(13); |
104 | assertTrue (hashTable.size() == 0); |
105 | Poco::UInt32 h1 = hashTable.insert("1" , 1); |
106 | assertTrue (hashTable.size() == 1); |
107 | Poco::UInt32 h2 = hashTable.update("2" , 2); |
108 | assertTrue (hashTable.size() == 2); |
109 | hashTable.clear(); |
110 | assertTrue (hashTable.size() == 0); |
111 | } |
112 | |
113 | |
114 | void SimpleHashTableTest::testResize() |
115 | { |
116 | SimpleHashTable<std::string, int> hashTable(13); |
117 | assertTrue (hashTable.size() == 0); |
118 | hashTable.resize(2467); |
119 | for (int i = 0; i < 1024; ++i) |
120 | { |
121 | hashTable.insert(Poco::NumberFormatter::format(i), i*i); |
122 | } |
123 | hashTable.resize(3037); |
124 | |
125 | for (int i = 0; i < 1024; ++i) |
126 | { |
127 | std::string tmp = Poco::NumberFormatter::format(i); |
128 | assertTrue (hashTable.exists(tmp)); |
129 | assertTrue (hashTable.get(tmp) == i*i); |
130 | } |
131 | } |
132 | |
133 | |
134 | void SimpleHashTableTest::setUp() |
135 | { |
136 | } |
137 | |
138 | |
139 | void SimpleHashTableTest::tearDown() |
140 | { |
141 | } |
142 | |
143 | |
144 | CppUnit::Test* SimpleHashTableTest::suite() |
145 | { |
146 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SimpleHashTableTest" ); |
147 | |
148 | CppUnit_addTest(pSuite, SimpleHashTableTest, testInsert); |
149 | CppUnit_addTest(pSuite, SimpleHashTableTest, testUpdate); |
150 | CppUnit_addTest(pSuite, SimpleHashTableTest, testOverflow); |
151 | CppUnit_addTest(pSuite, SimpleHashTableTest, testSize); |
152 | CppUnit_addTest(pSuite, SimpleHashTableTest, testResize); |
153 | |
154 | return pSuite; |
155 | } |
156 | |