1 | // |
2 | // ListMapTest.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 "ListMapTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/ListMap.h" |
15 | #include "Poco/Exception.h" |
16 | #include <map> |
17 | |
18 | |
19 | using Poco::ListMap; |
20 | |
21 | |
22 | ListMapTest::ListMapTest(const std::string& rName): CppUnit::TestCase(rName) |
23 | { |
24 | } |
25 | |
26 | |
27 | ListMapTest::~ListMapTest() |
28 | { |
29 | } |
30 | |
31 | |
32 | void ListMapTest::testInsert() |
33 | { |
34 | const int N = 1000; |
35 | |
36 | typedef ListMap<int, int> IntMap; |
37 | IntMap hm; |
38 | |
39 | assertTrue (hm.empty()); |
40 | |
41 | for (int i = 0; i < N; ++i) |
42 | { |
43 | IntMap::Iterator res = hm.insert(IntMap::ValueType(i, i*2)); |
44 | assertTrue (res->first == i); |
45 | assertTrue (res->second == i*2); |
46 | IntMap::Iterator it = hm.find(i); |
47 | assertTrue (it != hm.end()); |
48 | assertTrue (it->first == i); |
49 | assertTrue (it->second == i*2); |
50 | assertTrue (hm.size() == i + 1); |
51 | } |
52 | |
53 | assertTrue (!hm.empty()); |
54 | |
55 | for (int i = 0; i < N; ++i) |
56 | { |
57 | IntMap::Iterator it = hm.find(i); |
58 | assertTrue (it != hm.end()); |
59 | assertTrue (it->first == i); |
60 | assertTrue (it->second == i*2); |
61 | } |
62 | |
63 | hm.clear(); |
64 | for (int i = 0; i < N; ++i) |
65 | { |
66 | IntMap::Iterator res = hm.insert(IntMap::ValueType(i, 0)); |
67 | assertTrue (res->first == i); |
68 | assertTrue (res->second == 0); |
69 | } |
70 | } |
71 | |
72 | |
73 | void ListMapTest::testInsertOrder() |
74 | { |
75 | const int N = 1000; |
76 | |
77 | typedef ListMap<std::string, int> StrToIntMap; |
78 | StrToIntMap lm; |
79 | |
80 | lm.insert(StrToIntMap::ValueType("foo" , 42)); |
81 | lm.insert(StrToIntMap::ValueType("bar" , 43)); |
82 | |
83 | StrToIntMap::Iterator it = lm.begin(); |
84 | assertTrue (it != lm.end() && it->first == "foo" && it->second == 42); |
85 | |
86 | ++it; |
87 | assertTrue (it != lm.end() && it->first == "bar" && it->second == 43); |
88 | |
89 | ++it; |
90 | assertTrue (it == lm.end()); |
91 | |
92 | lm.insert(StrToIntMap::ValueType("foo" , 44)); |
93 | |
94 | it = lm.begin(); |
95 | assertTrue (it != lm.end() && it->first == "foo" && it->second == 42); |
96 | |
97 | ++it; |
98 | assertTrue (it != lm.end() && it->first == "foo" && it->second == 44); |
99 | |
100 | ++it; |
101 | assertTrue (it != lm.end() && it->first == "bar" && it->second == 43); |
102 | |
103 | ++it; |
104 | assertTrue (it == lm.end()); |
105 | } |
106 | |
107 | |
108 | void ListMapTest::testErase() |
109 | { |
110 | const int N = 1000; |
111 | |
112 | typedef ListMap<int, int> IntMap; |
113 | IntMap hm; |
114 | |
115 | for (int i = 0; i < N; ++i) |
116 | { |
117 | hm.insert(IntMap::ValueType(i, i*2)); |
118 | } |
119 | assertTrue (hm.size() == N); |
120 | |
121 | for (int i = 0; i < N; i += 2) |
122 | { |
123 | hm.erase(i); |
124 | IntMap::Iterator it = hm.find(i); |
125 | assertTrue (it == hm.end()); |
126 | } |
127 | assertTrue (hm.size() == N/2); |
128 | |
129 | for (int i = 0; i < N; i += 2) |
130 | { |
131 | IntMap::Iterator it = hm.find(i); |
132 | assertTrue (it == hm.end()); |
133 | } |
134 | |
135 | for (int i = 1; i < N; i += 2) |
136 | { |
137 | IntMap::Iterator it = hm.find(i); |
138 | assertTrue (it != hm.end()); |
139 | assertTrue (it->first == i); |
140 | } |
141 | |
142 | for (int i = 0; i < N; i += 2) |
143 | { |
144 | hm.insert(IntMap::ValueType(i, i*2)); |
145 | } |
146 | |
147 | for (int i = 0; i < N; ++i) |
148 | { |
149 | IntMap::Iterator it = hm.find(i); |
150 | assertTrue (it != hm.end()); |
151 | assertTrue (it->first == i); |
152 | assertTrue (it->second == i*2); |
153 | } |
154 | } |
155 | |
156 | |
157 | void ListMapTest::testIterator() |
158 | { |
159 | const int N = 1000; |
160 | |
161 | typedef ListMap<int, int> IntMap; |
162 | IntMap hm; |
163 | |
164 | for (int i = 0; i < N; ++i) |
165 | { |
166 | hm.insert(IntMap::ValueType(i, i*2)); |
167 | } |
168 | |
169 | std::map<int, int> values; |
170 | IntMap::Iterator it; // do not initialize here to test proper behavior of uninitialized iterators |
171 | it = hm.begin(); |
172 | while (it != hm.end()) |
173 | { |
174 | assertTrue (values.find(it->first) == values.end()); |
175 | values[it->first] = it->second; |
176 | ++it; |
177 | } |
178 | |
179 | assertTrue (values.size() == N); |
180 | } |
181 | |
182 | |
183 | void ListMapTest::testConstIterator() |
184 | { |
185 | const int N = 1000; |
186 | |
187 | typedef ListMap<int, int> IntMap; |
188 | IntMap hm; |
189 | |
190 | for (int i = 0; i < N; ++i) |
191 | { |
192 | hm.insert(IntMap::ValueType(i, i*2)); |
193 | } |
194 | |
195 | std::map<int, int> values; |
196 | IntMap::ConstIterator it = hm.begin(); |
197 | while (it != hm.end()) |
198 | { |
199 | assertTrue (values.find(it->first) == values.end()); |
200 | values[it->first] = it->second; |
201 | ++it; |
202 | } |
203 | |
204 | assertTrue (values.size() == N); |
205 | } |
206 | |
207 | |
208 | void ListMapTest::testIntIndex() |
209 | { |
210 | typedef ListMap<int, int> IntMap; |
211 | IntMap hm; |
212 | |
213 | hm[1] = 2; |
214 | hm[2] = 4; |
215 | hm[3] = 6; |
216 | |
217 | assertTrue (hm.size() == 3); |
218 | assertTrue (hm[1] == 2); |
219 | assertTrue (hm[2] == 4); |
220 | assertTrue (hm[3] == 6); |
221 | |
222 | try |
223 | { |
224 | const IntMap& im = hm; |
225 | int x = im[4]; |
226 | fail("no such key - must throw" ); |
227 | } |
228 | catch (Poco::NotFoundException&) |
229 | { |
230 | } |
231 | } |
232 | |
233 | |
234 | void ListMapTest::testStringIndex() |
235 | { |
236 | typedef ListMap<const char*, std::string> StringMap; |
237 | StringMap hm; |
238 | |
239 | hm["index1" ] = "value2" ; |
240 | hm["index2" ] = "value4" ; |
241 | hm["index3" ] = "value6" ; |
242 | |
243 | assertTrue (hm.size() == 3); |
244 | assertTrue (hm["index1" ] == "value2" ); |
245 | assertTrue (hm["Index2" ] == "value4" ); |
246 | assertTrue (hm["inDeX3" ] == "value6" ); |
247 | |
248 | try |
249 | { |
250 | const StringMap& im = hm; |
251 | std::string x = im["index4" ]; |
252 | fail("no such key - must throw" ); |
253 | } |
254 | catch (Poco::NotFoundException&) |
255 | { |
256 | } |
257 | } |
258 | |
259 | |
260 | void ListMapTest::setUp() |
261 | { |
262 | } |
263 | |
264 | |
265 | void ListMapTest::tearDown() |
266 | { |
267 | } |
268 | |
269 | |
270 | CppUnit::Test* ListMapTest::suite() |
271 | { |
272 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ListMapTest" ); |
273 | |
274 | CppUnit_addTest(pSuite, ListMapTest, testInsert); |
275 | CppUnit_addTest(pSuite, ListMapTest, testInsertOrder); |
276 | CppUnit_addTest(pSuite, ListMapTest, testErase); |
277 | CppUnit_addTest(pSuite, ListMapTest, testIterator); |
278 | CppUnit_addTest(pSuite, ListMapTest, testConstIterator); |
279 | CppUnit_addTest(pSuite, ListMapTest, testIntIndex); |
280 | CppUnit_addTest(pSuite, ListMapTest, testStringIndex); |
281 | |
282 | return pSuite; |
283 | } |
284 | |