| 1 | // |
| 2 | // HashTableTest.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 "HashTableTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/HashTable.h" |
| 15 | #include "Poco/NumberFormatter.h" |
| 16 | |
| 17 | |
| 18 | using namespace Poco; |
| 19 | |
| 20 | |
| 21 | HashTableTest::HashTableTest(const std::string& rName): CppUnit::TestCase(rName) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | |
| 26 | HashTableTest::~HashTableTest() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | |
| 31 | void HashTableTest::testInsert() |
| 32 | { |
| 33 | std::string s1("str1" ); |
| 34 | std::string s2("str2" ); |
| 35 | HashTable<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 HashTableTest::testUpdate() |
| 64 | { |
| 65 | // add code for second test here |
| 66 | std::string s1("str1" ); |
| 67 | std::string s2("str2" ); |
| 68 | HashTable<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 HashTableTest::testOverflow() |
| 85 | { |
| 86 | HashTable<std::string, int> hashTable(13); |
| 87 | for (int i = 0; i < 1024; ++i) |
| 88 | { |
| 89 | hashTable.insert(Poco::NumberFormatter::format(i), i*i); |
| 90 | } |
| 91 | |
| 92 | for (int i = 0; i < 1024; ++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 HashTableTest::testSize() |
| 102 | { |
| 103 | HashTable<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.remove("1" ); |
| 110 | assertTrue (hashTable.size() == 1); |
| 111 | hashTable.remove("3" ); |
| 112 | assertTrue (hashTable.size() == 1); |
| 113 | hashTable.removeRaw("2" , h2); |
| 114 | assertTrue (hashTable.size() == 0); |
| 115 | hashTable.insert("1" , 1); |
| 116 | hashTable.insert("2" , 2); |
| 117 | assertTrue (hashTable.size() == 2); |
| 118 | hashTable.clear(); |
| 119 | assertTrue (hashTable.size() == 0); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void HashTableTest::testResize() |
| 124 | { |
| 125 | HashTable<std::string, int> hashTable(13); |
| 126 | assertTrue (hashTable.size() == 0); |
| 127 | hashTable.resize(19); |
| 128 | for (int i = 0; i < 1024; ++i) |
| 129 | { |
| 130 | hashTable.insert(Poco::NumberFormatter::format(i), i*i); |
| 131 | } |
| 132 | hashTable.resize(1009); |
| 133 | |
| 134 | for (int i = 0; i < 1024; ++i) |
| 135 | { |
| 136 | std::string tmp = Poco::NumberFormatter::format(i); |
| 137 | assertTrue (hashTable.exists(tmp)); |
| 138 | assertTrue (hashTable.get(tmp) == i*i); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | void HashTableTest::testStatistic() |
| 144 | { |
| 145 | double relax = 0.001; |
| 146 | HashTable<std::string, int> hashTable(13); |
| 147 | assertTrue (hashTable.size() == 0); |
| 148 | HashStatistic stat1(hashTable.currentState()); |
| 149 | assertTrue (stat1.avgEntriesPerHash() < relax && stat1.avgEntriesPerHash() > -relax); |
| 150 | assertTrue (stat1.maxPositionsOfTable() == 13); |
| 151 | assertTrue (stat1.maxEntriesPerHash() == 0); |
| 152 | |
| 153 | hashTable.resize(19); |
| 154 | stat1 = hashTable.currentState(true); |
| 155 | assertTrue (stat1.avgEntriesPerHash() < relax && stat1.avgEntriesPerHash() > -relax); |
| 156 | assertTrue (stat1.maxPositionsOfTable() == 19); |
| 157 | assertTrue (stat1.maxEntriesPerHash() == 0); |
| 158 | assertTrue (stat1.detailedEntriesPerHash().size() == 19); |
| 159 | |
| 160 | for (int i = 0; i < 1024; ++i) |
| 161 | { |
| 162 | hashTable.insert(Poco::NumberFormatter::format(i), i*i); |
| 163 | } |
| 164 | stat1 = hashTable.currentState(true); |
| 165 | double expAvg = 1024.0/ 19; |
| 166 | assertTrue (stat1.avgEntriesPerHash() < (expAvg + relax) && stat1.avgEntriesPerHash() > (expAvg - relax)); |
| 167 | assertTrue (stat1.maxPositionsOfTable() == 19); |
| 168 | assertTrue (stat1.maxEntriesPerHash() > expAvg); |
| 169 | hashTable.resize(1009); |
| 170 | stat1 = hashTable.currentState(true); |
| 171 | |
| 172 | expAvg = 1024.0/ 1009; |
| 173 | |
| 174 | assertTrue (stat1.avgEntriesPerHash() < (expAvg + relax) && stat1.avgEntriesPerHash() > (expAvg - relax)); |
| 175 | assertTrue (stat1.maxPositionsOfTable() == 1009); |
| 176 | assertTrue (stat1.maxEntriesPerHash() > expAvg); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | void HashTableTest::setUp() |
| 181 | { |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void HashTableTest::tearDown() |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | |
| 190 | CppUnit::Test* HashTableTest::suite() |
| 191 | { |
| 192 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HashTableTest" ); |
| 193 | |
| 194 | CppUnit_addTest(pSuite, HashTableTest, testInsert); |
| 195 | CppUnit_addTest(pSuite, HashTableTest, testUpdate); |
| 196 | CppUnit_addTest(pSuite, HashTableTest, testOverflow); |
| 197 | CppUnit_addTest(pSuite, HashTableTest, testSize); |
| 198 | CppUnit_addTest(pSuite, HashTableTest, testResize); |
| 199 | CppUnit_addTest(pSuite, HashTableTest, testStatistic); |
| 200 | |
| 201 | return pSuite; |
| 202 | } |
| 203 | |