Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_mapping_test.cc
Go to the documentation of this file.
1#include <gtest/gtest.h>
2#include <ah-mapping.H>
3
4using namespace Aleph;
5
6class AHMappingTest : public ::testing::Test
7{
8protected:
9 void SetUp() override {}
10 void TearDown() override {}
11};
12
13// =============================================================================
14// Basic Operations Tests
15// =============================================================================
16
24
26{
28
29 map.insert(1, "one");
30 map.insert(2, "two");
31 map.insert(3, "three");
32
33 EXPECT_FALSE(map.empty());
34 EXPECT_EQ(map.size(), 3u);
35 EXPECT_EQ(map[1], "one");
36 EXPECT_EQ(map[2], "two");
37 EXPECT_EQ(map[3], "three");
38}
39
41{
43
44 map["one"] = 1;
45 map["two"] = 2;
46 map["three"] = 3;
47
48 EXPECT_EQ(map.size(), 3u);
49 EXPECT_EQ(map["one"], 1);
50 EXPECT_EQ(map["two"], 2);
51 EXPECT_EQ(map["three"], 3);
52}
53
55{
57
58 map.insert(10, "ten");
59
60 EXPECT_TRUE(map.valid_key(10));
61 EXPECT_FALSE(map.valid_key(20));
62 EXPECT_FALSE(map.valid_key(0));
63}
64
66{
68
69 map.insert(1, "one");
70 map.insert(2, "two");
71 map.insert(3, "three");
72
73 EXPECT_TRUE(map.remove(2));
74 EXPECT_EQ(map.size(), 2u);
75 EXPECT_FALSE(map.valid_key(2));
76 EXPECT_TRUE(map.valid_key(1));
77 EXPECT_TRUE(map.valid_key(3));
78
79 EXPECT_FALSE(map.remove(2)); // Already removed
80}
81
83{
85
86 map.insert(1, "one");
87 map.insert(2, "two");
88 map.insert(3, "three");
89
90 map.clear();
91
92 EXPECT_TRUE(map.empty());
93 EXPECT_EQ(map.size(), 0u);
94}
95
97{
99
100 map.insert(1, "one");
101 EXPECT_EQ(map[1], "one");
102
103 // Use bracket operator to update value
104 map[1] = "ONE";
105 EXPECT_EQ(map[1], "ONE");
106 EXPECT_EQ(map.size(), 1u); // Still just one entry
107}
108
109// =============================================================================
110// Inverse Mapping Tests
111// =============================================================================
112
114{
116
117 map.insert(1, "one");
118 map.insert(2, "two");
119 map.insert(3, "three");
120
121 auto inv = map.inverse();
122
123 EXPECT_EQ(inv.size(), 3u);
124 EXPECT_EQ(inv["one"], 1);
125 EXPECT_EQ(inv["two"], 2);
126 EXPECT_EQ(inv["three"], 3);
127}
128
130{
132
133 auto inv = map.inverse();
134
136 EXPECT_EQ(inv.size(), 0u);
137}
138
140{
142
143 map.insert(1, "one");
144 map.insert(2, "two");
145
146 auto inv1 = map.inverse();
147 auto inv2 = inv1.inverse();
148
149 EXPECT_EQ(inv2.size(), 2u);
150 EXPECT_EQ(inv2[1], "one");
151 EXPECT_EQ(inv2[2], "two");
152}
153
154// =============================================================================
155// Value Search Tests
156// =============================================================================
157
159{
161
162 map.insert(1, "one");
163 map.insert(2, "two");
164 map.insert(3, "three");
165
166 EXPECT_TRUE(map.contains_value("one"));
167 EXPECT_TRUE(map.contains_value("two"));
168 EXPECT_TRUE(map.contains_value("three"));
169 EXPECT_FALSE(map.contains_value("four"));
171}
172
173// =============================================================================
174// Iteration Tests
175// =============================================================================
176
178{
180
181 map.insert(1, "one");
182 map.insert(2, "two");
183 map.insert(3, "three");
184
185 int count = 0;
186 map.for_each([&count](const int& k, const std::string& v) {
187 count++;
188 EXPECT_GT(k, 0);
189 EXPECT_FALSE(v.empty());
190 });
191
192 EXPECT_EQ(count, 3);
193}
194
195// =============================================================================
196// Copy and Move Tests
197// =============================================================================
198
200{
202 map1.insert(1, "one");
203 map1.insert(2, "two");
204
206
207 EXPECT_EQ(map2.size(), 2u);
208 EXPECT_EQ(map2[1], "one");
209 EXPECT_EQ(map2[2], "two");
210
211 // Verify independence
212 map1.insert(3, "three");
213 EXPECT_EQ(map1.size(), 3u);
214 EXPECT_EQ(map2.size(), 2u);
215}
216
218{
220 map1.insert(1, "one");
221 map1.insert(2, "two");
222
224
225 EXPECT_EQ(map2.size(), 2u);
226 EXPECT_EQ(map2[1], "one");
227 EXPECT_EQ(map2[2], "two");
228}
229
230// =============================================================================
231// Different Types Tests
232// =============================================================================
233
235{
237
238 map["zero"] = 0;
239 map["one"] = 1;
240 map["two"] = 2;
241
242 EXPECT_EQ(map["zero"], 0);
243 EXPECT_EQ(map["one"], 1);
244 EXPECT_EQ(map["two"], 2);
245}
246
248{
250
251 map[1.5] = "one-half";
252 map[2.5] = "two-half";
253
254 EXPECT_EQ(map[1.5], "one-half");
255 EXPECT_EQ(map[2.5], "two-half");
256}
257
258// =============================================================================
259// Edge Cases Tests
260// =============================================================================
261
263{
265
266 map.insert(1, "");
267
268 EXPECT_TRUE(map.valid_key(1));
269 EXPECT_EQ(map[1], "");
270}
271
273{
275
276 map.insert(0, "zero");
277
278 EXPECT_TRUE(map.valid_key(0));
279 EXPECT_EQ(map[0], "zero");
280}
281
283{
285
286 map.insert(-1, "minus-one");
287 map.insert(-10, "minus-ten");
288
289 EXPECT_EQ(map[-1], "minus-one");
290 EXPECT_EQ(map[-10], "minus-ten");
291}
292
293// =============================================================================
294// Stress Tests
295// =============================================================================
296
298{
300
301 const int N = 1000;
302 for (int i = 0; i < N; ++i)
303 map.insert(i, i * 2);
304
305 EXPECT_EQ(map.size(), static_cast<size_t>(N));
306
307 for (int i = 0; i < N; ++i)
308 {
309 EXPECT_TRUE(map.valid_key(i));
310 EXPECT_EQ(map[i], i * 2);
311 }
312}
313
315{
317
318 for (int i = 0; i < 500; ++i)
319 map.insert(i, std::to_string(i));
320
321 EXPECT_EQ(map.size(), 500u);
322
323 for (int i = 0; i < 500; i += 10)
324 EXPECT_EQ(map[i], std::to_string(i));
325}
326
328{
330
331 for (int i = 0; i < 100; ++i)
332 map.insert(i, std::to_string(i));
333
334 for (int i = 0; i < 100; i += 2)
335 EXPECT_TRUE(map.remove(i));
336
337 EXPECT_EQ(map.size(), 50u);
338
339 for (int i = 1; i < 100; i += 2)
340 EXPECT_TRUE(map.valid_key(i));
341}
342
344{
346
347 for (int i = 0; i < 200; ++i)
348 map.insert(i, "val_" + std::to_string(i));
349
350 auto inv = map.inverse();
351
352 EXPECT_EQ(inv.size(), 200u);
353
354 for (int i = 0; i < 200; i += 10)
355 {
356 std::string key = "val_" + std::to_string(i);
357 EXPECT_TRUE(inv.valid_key(key));
358 EXPECT_EQ(inv[key], i);
359 }
360}
Bidirectional mapping between two types.
TEST_F(AHMappingTest, DefaultConstructor)
void SetUp() override
void TearDown() override
A generic key-value mapping container with inverse operation support.
Definition ah-mapping.H:70
bool remove(const Key &key) noexcept
Removes the key-value pair with the given key.
Definition ah-mapping.H:182
size_t size() const noexcept
Gets the number of key-value pairs in the mapping.
Definition ah-mapping.H:230
bool valid_key(const Key &key) const noexcept
Checks if a key exists in the mapping.
Definition ah-mapping.H:200
AHMapping< ValueType, Key > inverse() const
Creates a new mapping with keys and values swapped.
Definition ah-mapping.H:166
bool contains_value(const ValueType &value) const
Checks if the mapping contains a specific value.
Definition ah-mapping.H:257
void insert(const Key &key, const ValueType &value)
Inserts or updates a key-value pair in the mapping.
Definition ah-mapping.H:131
void for_each(F f) const
Applies a function to each key-value pair.
Definition ah-mapping.H:271
void clear() noexcept
Removes all key-value pairs from the mapping.
Definition ah-mapping.H:247
bool empty() const noexcept
Checks if the mapping is empty.
Definition ah-mapping.H:239
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
void empty() noexcept
empty the list
Definition htlist.H:1689
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127