| 1 | // | 
|---|
| 2 | // HashMapTest.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 "HashMapTest.h" | 
|---|
| 12 | #include "Poco/CppUnit/TestCaller.h" | 
|---|
| 13 | #include "Poco/CppUnit/TestSuite.h" | 
|---|
| 14 | #include "Poco/HashMap.h" | 
|---|
| 15 | #include "Poco/Exception.h" | 
|---|
| 16 | #include <map> | 
|---|
| 17 |  | 
|---|
| 18 |  | 
|---|
| 19 | using Poco::HashMap; | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | HashMapTest::HashMapTest(const std::string& rName): CppUnit::TestCase(rName) | 
|---|
| 23 | { | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | HashMapTest::~HashMapTest() | 
|---|
| 28 | { | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | void HashMapTest::testInsert() | 
|---|
| 33 | { | 
|---|
| 34 | const int N = 1000; | 
|---|
| 35 |  | 
|---|
| 36 | typedef HashMap<int, int> IntMap; | 
|---|
| 37 | IntMap hm; | 
|---|
| 38 |  | 
|---|
| 39 | assertTrue (hm.empty()); | 
|---|
| 40 |  | 
|---|
| 41 | for (int i = 0; i < N; ++i) | 
|---|
| 42 | { | 
|---|
| 43 | std::pair<IntMap::Iterator, bool> res = hm.insert(IntMap::ValueType(i, i*2)); | 
|---|
| 44 | assertTrue (res.first->first == i); | 
|---|
| 45 | assertTrue (res.first->second == i*2); | 
|---|
| 46 | assertTrue (res.second); | 
|---|
| 47 | IntMap::Iterator it = hm.find(i); | 
|---|
| 48 | assertTrue (it != hm.end()); | 
|---|
| 49 | assertTrue (it->first == i); | 
|---|
| 50 | assertTrue (it->second == i*2); | 
|---|
| 51 | assertTrue (hm.count(i) == 1); | 
|---|
| 52 | assertTrue (hm.size() == i + 1); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | assertTrue (!hm.empty()); | 
|---|
| 56 |  | 
|---|
| 57 | for (int i = 0; i < N; ++i) | 
|---|
| 58 | { | 
|---|
| 59 | IntMap::Iterator it = hm.find(i); | 
|---|
| 60 | assertTrue (it != hm.end()); | 
|---|
| 61 | assertTrue (it->first == i); | 
|---|
| 62 | assertTrue (it->second == i*2); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | for (int i = 0; i < N; ++i) | 
|---|
| 66 | { | 
|---|
| 67 | std::pair<IntMap::Iterator, bool> res = hm.insert(IntMap::ValueType(i, 0)); | 
|---|
| 68 | assertTrue (res.first->first == i); | 
|---|
| 69 | assertTrue (res.first->second == i*2); | 
|---|
| 70 | assertTrue (!res.second); | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | void HashMapTest::testErase() | 
|---|
| 76 | { | 
|---|
| 77 | const int N = 1000; | 
|---|
| 78 |  | 
|---|
| 79 | typedef HashMap<int, int> IntMap; | 
|---|
| 80 | IntMap hm; | 
|---|
| 81 |  | 
|---|
| 82 | for (int i = 0; i < N; ++i) | 
|---|
| 83 | { | 
|---|
| 84 | hm.insert(IntMap::ValueType(i, i*2)); | 
|---|
| 85 | } | 
|---|
| 86 | assertTrue (hm.size() == N); | 
|---|
| 87 |  | 
|---|
| 88 | for (int i = 0; i < N; i += 2) | 
|---|
| 89 | { | 
|---|
| 90 | hm.erase(i); | 
|---|
| 91 | IntMap::Iterator it = hm.find(i); | 
|---|
| 92 | assertTrue (it == hm.end()); | 
|---|
| 93 | } | 
|---|
| 94 | assertTrue (hm.size() == N/2); | 
|---|
| 95 |  | 
|---|
| 96 | for (int i = 0; i < N; i += 2) | 
|---|
| 97 | { | 
|---|
| 98 | IntMap::Iterator it = hm.find(i); | 
|---|
| 99 | assertTrue (it == hm.end()); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | for (int i = 1; i < N; i += 2) | 
|---|
| 103 | { | 
|---|
| 104 | IntMap::Iterator it = hm.find(i); | 
|---|
| 105 | assertTrue (it != hm.end()); | 
|---|
| 106 | assertTrue (*it == i); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | for (int i = 0; i < N; i += 2) | 
|---|
| 110 | { | 
|---|
| 111 | hm.insert(IntMap::ValueType(i, i*2)); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | for (int i = 0; i < N; ++i) | 
|---|
| 115 | { | 
|---|
| 116 | IntMap::Iterator it = hm.find(i); | 
|---|
| 117 | assertTrue (it != hm.end()); | 
|---|
| 118 | assertTrue (it->first == i); | 
|---|
| 119 | assertTrue (it->second == i*2); | 
|---|
| 120 | } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 | void HashMapTest::testIterator() | 
|---|
| 125 | { | 
|---|
| 126 | const int N = 1000; | 
|---|
| 127 |  | 
|---|
| 128 | typedef HashMap<int, int> IntMap; | 
|---|
| 129 | IntMap hm; | 
|---|
| 130 |  | 
|---|
| 131 | for (int i = 0; i < N; ++i) | 
|---|
| 132 | { | 
|---|
| 133 | hm.insert(IntMap::ValueType(i, i*2)); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | std::map<int, int> values; | 
|---|
| 137 | IntMap::Iterator it; // do not initialize here to test proper behavior of uninitialized iterators | 
|---|
| 138 | it = hm.begin(); | 
|---|
| 139 | while (it != hm.end()) | 
|---|
| 140 | { | 
|---|
| 141 | assertTrue (values.find(it->first) == values.end()); | 
|---|
| 142 | values[it->first] = it->second; | 
|---|
| 143 | ++it; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | assertTrue (values.size() == N); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 | void HashMapTest::testConstIterator() | 
|---|
| 151 | { | 
|---|
| 152 | const int N = 1000; | 
|---|
| 153 |  | 
|---|
| 154 | typedef HashMap<int, int> IntMap; | 
|---|
| 155 | IntMap hm; | 
|---|
| 156 |  | 
|---|
| 157 | for (int i = 0; i < N; ++i) | 
|---|
| 158 | { | 
|---|
| 159 | hm.insert(IntMap::ValueType(i, i*2)); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | std::map<int, int> values; | 
|---|
| 163 | IntMap::ConstIterator it = hm.begin(); | 
|---|
| 164 | while (it != hm.end()) | 
|---|
| 165 | { | 
|---|
| 166 | assertTrue (values.find(it->first) == values.end()); | 
|---|
| 167 | values[it->first] = it->second; | 
|---|
| 168 | ++it; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | assertTrue (values.size() == N); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | void HashMapTest::testIndex() | 
|---|
| 176 | { | 
|---|
| 177 | typedef HashMap<int, int> IntMap; | 
|---|
| 178 | IntMap hm; | 
|---|
| 179 |  | 
|---|
| 180 | hm[1] = 2; | 
|---|
| 181 | hm[2] = 4; | 
|---|
| 182 | hm[3] = 6; | 
|---|
| 183 |  | 
|---|
| 184 | assertTrue (hm.size() == 3); | 
|---|
| 185 | assertTrue (hm[1] == 2); | 
|---|
| 186 | assertTrue (hm[2] == 4); | 
|---|
| 187 | assertTrue (hm[3] == 6); | 
|---|
| 188 |  | 
|---|
| 189 | try | 
|---|
| 190 | { | 
|---|
| 191 | const IntMap& im = hm; | 
|---|
| 192 | int x = im[4]; | 
|---|
| 193 | fail( "no such key - must throw"); | 
|---|
| 194 | } | 
|---|
| 195 | catch (Poco::NotFoundException&) | 
|---|
| 196 | { | 
|---|
| 197 | } | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 |  | 
|---|
| 201 | void HashMapTest::setUp() | 
|---|
| 202 | { | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 |  | 
|---|
| 206 | void HashMapTest::tearDown() | 
|---|
| 207 | { | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 | CppUnit::Test* HashMapTest::suite() | 
|---|
| 212 | { | 
|---|
| 213 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite( "HashMapTest"); | 
|---|
| 214 |  | 
|---|
| 215 | CppUnit_addTest(pSuite, HashMapTest, testInsert); | 
|---|
| 216 | CppUnit_addTest(pSuite, HashMapTest, testErase); | 
|---|
| 217 | CppUnit_addTest(pSuite, HashMapTest, testIterator); | 
|---|
| 218 | CppUnit_addTest(pSuite, HashMapTest, testConstIterator); | 
|---|
| 219 | CppUnit_addTest(pSuite, HashMapTest, testIndex); | 
|---|
| 220 |  | 
|---|
| 221 | return pSuite; | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|