Tue, 18 Apr 2023 19:15:50 +0200
tweak rehash test to achieve missing coverage
556 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "cx/hash_map.h" | |
30 | #include "cx/utils.h" | |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
31 | #include "cx/string.h" |
556 | 32 | #include "util_allocator.h" |
33 | ||
34 | #include <gtest/gtest.h> | |
35 | #include <unordered_map> | |
36 | #include <unordered_set> | |
37 | ||
38 | struct map_operation { | |
39 | enum { | |
40 | put, rm | |
41 | } op; | |
42 | char const *key; | |
43 | char const *value; | |
44 | }; | |
45 | ||
46 | auto generate_map_operations() -> std::vector<map_operation> { | |
47 | return { | |
48 | {map_operation::put, "key 1", "test"}, | |
49 | {map_operation::put, "key 2", "blub"}, | |
50 | {map_operation::put, "key 3", "hallo"}, | |
51 | {map_operation::put, "key 2", "foobar"}, | |
52 | {map_operation::put, "key 4", "value 4"}, | |
53 | {map_operation::put, "key 5", "value 5"}, | |
54 | {map_operation::put, "key 6", "value 6"}, | |
55 | {map_operation::rm, "key 4", nullptr}, | |
56 | {map_operation::put, "key 7", "value 7"}, | |
57 | {map_operation::put, "key 8", "value 8"}, | |
58 | {map_operation::rm, "does not exist", nullptr}, | |
59 | {map_operation::put, "key 9", "value 9"}, | |
60 | {map_operation::put, "key 6", "other value"}, | |
61 | {map_operation::put, "key 7", "something else"}, | |
62 | {map_operation::rm, "key 8", nullptr}, | |
63 | {map_operation::rm, "key 2", nullptr}, | |
64 | {map_operation::put, "key 8", "new value"}, | |
65 | }; | |
66 | } | |
67 | ||
68 | static void verify_map_contents( | |
69 | CxMap *map, | |
70 | std::unordered_map<std::string, std::string> const &refmap | |
71 | ) { | |
72 | // verify key iterator | |
73 | { | |
74 | auto keyiter = cxMapIteratorKeys(map); | |
75 | std::unordered_set<std::string> keys; | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
76 | cx_foreach(CxHashKey*, elem, keyiter) { |
604 | 77 | keys.insert(std::string(elem->data.cstr, elem->len)); |
556 | 78 | } |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
79 | EXPECT_EQ(keyiter.index, map->size); |
556 | 80 | ASSERT_EQ(keys.size(), map->size); |
81 | for (auto &&k: keys) { | |
82 | EXPECT_NE(refmap.find(k), refmap.end()); | |
83 | } | |
84 | } | |
85 | ||
86 | // verify value iterator | |
87 | { | |
88 | auto valiter = cxMapIteratorValues(map); | |
89 | std::unordered_set<std::string> values; // we use that the values in our test data are unique strings | |
90 | cx_foreach(char const*, elem, valiter) { | |
91 | values.insert(std::string(elem)); | |
92 | } | |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
93 | EXPECT_EQ(valiter.index, map->size); |
556 | 94 | ASSERT_EQ(values.size(), map->size); |
95 | for (auto &&v: values) { | |
96 | EXPECT_NE(std::find_if(refmap.begin(), refmap.end(), | |
97 | [v](auto const &e) { return e.second == v; }), refmap.end()); | |
98 | } | |
99 | } | |
100 | ||
101 | // verify pair iterator | |
102 | { | |
103 | auto pairiter = cxMapIterator(map); | |
104 | std::unordered_map<std::string, std::string> pairs; | |
105 | cx_foreach(CxMapEntry*, entry, pairiter) { | |
604 | 106 | pairs[std::string(entry->key->data.cstr, entry->key->len)] = std::string((char *) entry->value); |
556 | 107 | } |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
108 | EXPECT_EQ(pairiter.index, map->size); |
556 | 109 | ASSERT_EQ(pairs.size(), refmap.size()); |
110 | for (auto &&p: pairs) { | |
111 | ASSERT_EQ(p.second, refmap.at(p.first)); | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | TEST(CxHashMap, Create) { | |
117 | CxTestingAllocator allocator; | |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
118 | auto map = cxHashMapCreate(&allocator, 1, 0); |
556 | 119 | auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map); |
120 | EXPECT_GT(hmap->bucket_count, 0); | |
121 | cx_for_n(i, hmap->bucket_count) { | |
122 | EXPECT_EQ(hmap->buckets[i], nullptr); | |
123 | } | |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
669
diff
changeset
|
124 | EXPECT_EQ(map->item_size, 1); |
556 | 125 | EXPECT_EQ(map->size, 0); |
126 | EXPECT_EQ(map->allocator, &allocator); | |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
127 | EXPECT_FALSE(map->store_pointer); |
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
128 | EXPECT_EQ(map->cmpfunc, nullptr); |
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
129 | EXPECT_EQ(map->simple_destructor, nullptr); |
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
130 | EXPECT_EQ(map->advanced_destructor, nullptr); |
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
131 | EXPECT_EQ(map->destructor_data, nullptr); |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
132 | cxMapStorePointers(map); |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
133 | EXPECT_TRUE(map->store_pointer); |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
669
diff
changeset
|
134 | EXPECT_EQ(map->item_size, sizeof(void *)); |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
135 | cxMapStoreObjects(map); |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
136 | EXPECT_FALSE(map->store_pointer); |
556 | 137 | |
138 | cxMapDestroy(map); | |
139 | EXPECT_TRUE(allocator.verify()); | |
140 | } | |
141 | ||
668
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
142 | TEST(CxHashMap, CreateForStoringPointers) { |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
143 | CxTestingAllocator allocator; |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
144 | auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
145 | auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map); |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
146 | EXPECT_GT(hmap->bucket_count, 0); |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
147 | cx_for_n(i, hmap->bucket_count) { |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
148 | EXPECT_EQ(hmap->buckets[i], nullptr); |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
149 | } |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
150 | EXPECT_EQ(map->size, 0); |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
151 | EXPECT_EQ(map->allocator, &allocator); |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
152 | EXPECT_TRUE(map->store_pointer); |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
669
diff
changeset
|
153 | EXPECT_EQ(map->item_size, sizeof(void *)); |
668
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
154 | |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
155 | cxMapDestroy(map); |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
156 | EXPECT_TRUE(allocator.verify()); |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
157 | } |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
659
diff
changeset
|
158 | |
556 | 159 | TEST(CxHashMap, BasicOperations) { |
160 | // create the map | |
161 | CxTestingAllocator allocator; | |
669
dce9b8450656
add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents:
668
diff
changeset
|
162 | auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); |
556 | 163 | |
164 | // create a reference map | |
165 | std::unordered_map<std::string, std::string> refmap; | |
166 | ||
167 | // generate operations | |
168 | auto ops = generate_map_operations(); | |
169 | ||
170 | // verify iterators for empty map | |
171 | verify_map_contents(map, refmap); | |
172 | ||
173 | // execute operations and verify results | |
174 | for (auto &&op: ops) { | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
175 | CxHashKey key = cx_hash_key_str(op.key); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
176 | key.hash = 0; // force the hash map to compute the hash |
556 | 177 | if (op.op == map_operation::put) { |
178 | // execute a put operation and verify that the exact value can be read back | |
179 | refmap[std::string(op.key)] = std::string(op.value); | |
180 | int result = cxMapPut(map, key, (void *) op.value); | |
181 | EXPECT_EQ(result, 0); | |
182 | auto added = cxMapGet(map, key); | |
183 | EXPECT_EQ(memcmp(op.value, added, strlen(op.value)), 0); | |
184 | } else { | |
185 | // execute a remove and verify that the removed element was returned (or nullptr) | |
186 | auto found = refmap.find(op.key); | |
659
4a06fd63909a
split cxMapRemove() to cxMapRemoveAndGet()
Mike Becker <universe@uap-core.de>
parents:
658
diff
changeset
|
187 | auto removed = cxMapRemoveAndGet(map, key); |
556 | 188 | if (found == refmap.end()) { |
189 | EXPECT_EQ(removed, nullptr); | |
190 | } else { | |
191 | EXPECT_EQ(std::string((char *) removed), found->second); | |
192 | refmap.erase(found); | |
193 | } | |
194 | } | |
195 | // compare the current map state with the reference map | |
196 | verify_map_contents(map, refmap); | |
197 | } | |
198 | ||
199 | // destroy the map and verify the memory (de)allocations | |
200 | cxMapDestroy(map); | |
201 | EXPECT_TRUE(allocator.verify()); | |
202 | } | |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
203 | |
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
204 | TEST(CxHashMap, RemoveViaIterator) { |
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
205 | CxTestingAllocator allocator; |
669
dce9b8450656
add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents:
668
diff
changeset
|
206 | auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 4); |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
207 | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
208 | cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
209 | cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
210 | cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
211 | cxMapPut(map, cx_hash_key_str("key 4"), (void *) "val 4"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
212 | cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
213 | cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
214 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
604
diff
changeset
|
215 | auto iter = cxMapMutIterator(map); |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
216 | cx_foreach(CxMapEntry*, entry, iter) { |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
604
diff
changeset
|
217 | if (entry->key->data.cstr[4] % 2 == 1) cxIteratorFlagRemoval(iter); |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
218 | } |
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
219 | EXPECT_EQ(map->size, 3); |
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
220 | EXPECT_EQ(iter.index, map->size); |
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
221 | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
222 | EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 1")), nullptr); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
223 | EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 2")), nullptr); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
224 | EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 3")), nullptr); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
225 | EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 4")), nullptr); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
226 | EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 5")), nullptr); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
227 | EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 6")), nullptr); |
561
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
228 | |
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
229 | cxMapDestroy(map); |
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
230 | EXPECT_TRUE(allocator.verify()); |
bb17790af41e
#199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents:
556
diff
changeset
|
231 | } |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
232 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
233 | TEST(CxHashMap, RehashNotRequired) { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
234 | CxTestingAllocator allocator; |
669
dce9b8450656
add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents:
668
diff
changeset
|
235 | auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
236 | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
237 | cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
238 | cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
239 | cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
240 | cxMapPut(map, cx_hash_key_str("key 4"), (void *) "val 4"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
241 | cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
242 | cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
243 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
244 | // 6/8 does not exceed 0.75, therefore the function should not rehash |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
245 | int result = cxMapRehash(map); |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
246 | EXPECT_EQ(result, 0); |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
247 | EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 8); |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
248 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
249 | cxMapDestroy(map); |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
250 | EXPECT_TRUE(allocator.verify()); |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
251 | } |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
252 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
253 | TEST(CxHashMap, Rehash) { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
254 | CxTestingAllocator allocator; |
687
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
255 | auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 7); |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
256 | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
257 | cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
258 | cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
259 | cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); |
687
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
260 | cxMapPut(map, cx_hash_key_str("foo 4"), (void *) "val 4"); |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
261 | cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
262 | cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); |
687
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
263 | cxMapPut(map, cx_hash_key_str("bar 7"), (void *) "val 7"); |
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
264 | cxMapPut(map, cx_hash_key_str("key 8"), (void *) "val 8"); |
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
265 | cxMapPut(map, cx_hash_key_str("key 9"), (void *) "val 9"); |
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
266 | cxMapPut(map, cx_hash_key_str("key 10"), (void *) "val 10"); |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
267 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
268 | int result = cxMapRehash(map); |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
269 | EXPECT_EQ(result, 0); |
687
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
270 | EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 25); |
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
271 | EXPECT_EQ(map->size, 10); |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
272 | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
273 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 1")), "val 1"), 0); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
274 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 2")), "val 2"), 0); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
275 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 3")), "val 3"), 0); |
687
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
276 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("foo 4")), "val 4"), 0); |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
277 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 5")), "val 5"), 0); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
278 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 6")), "val 6"), 0); |
687
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
279 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("bar 7")), "val 7"), 0); |
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
280 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 8")), "val 8"), 0); |
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
281 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 9")), "val 9"), 0); |
612ed521b1c5
tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
282 | EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 10")), "val 10"), 0); |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
283 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
284 | cxMapDestroy(map); |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
561
diff
changeset
|
285 | EXPECT_TRUE(allocator.verify()); |
594
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
286 | } |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
287 | |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
288 | TEST(CxHashMap, Clear) { |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
289 | CxTestingAllocator allocator; |
669
dce9b8450656
add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents:
668
diff
changeset
|
290 | auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
291 | |
594
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
292 | cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
293 | cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
294 | cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
295 | |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
296 | EXPECT_EQ(map->size, 3); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
297 | |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
298 | cxMapClear(map); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
299 | |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
300 | EXPECT_EQ(map->size, 0); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
301 | EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 1")), nullptr); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
302 | EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 2")), nullptr); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
303 | EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 3")), nullptr); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
304 | |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
305 | cxMapDestroy(map); |
d90cfa6721f9
add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
306 | EXPECT_TRUE(allocator.verify()); |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
307 | } |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
308 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
309 | TEST(CxHashMap, StoreUcxStrings) { |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
310 | // create the map |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
311 | CxTestingAllocator allocator; |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
312 | auto map = cxHashMapCreate(&allocator, sizeof(cxstring), 8); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
313 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
314 | // define some strings |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
315 | auto s1 = CX_STR("this"); |
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
316 | auto s2 = CX_STR("is"); |
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
317 | auto s3 = CX_STR("a"); |
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
318 | auto s4 = CX_STR("test"); |
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
319 | auto s5 = CX_STR("setup"); |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
320 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
321 | // put them into the map |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
322 | cxMapPut(map, cx_hash_key_str("s1"), &s1); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
323 | cxMapPut(map, cx_hash_key_str("s2"), &s2); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
324 | cxMapPut(map, cx_hash_key_str("s3"), &s3); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
325 | cxMapPut(map, cx_hash_key_str("s4"), &s4); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
326 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
327 | // overwrite a value |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
328 | cxMapPut(map, cx_hash_key_str("s1"), &s5); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
329 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
330 | // look up a string |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
331 | auto s3p = reinterpret_cast<cxstring *>(cxMapGet(map, cx_hash_key_str("s3"))); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
332 | EXPECT_EQ(s3p->length, s3.length); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
333 | EXPECT_EQ(s3p->ptr, s3.ptr); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
334 | EXPECT_NE(s3p, &s3); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
335 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
336 | // remove a string |
659
4a06fd63909a
split cxMapRemove() to cxMapRemoveAndGet()
Mike Becker <universe@uap-core.de>
parents:
658
diff
changeset
|
337 | cxMapRemove(map, cx_hash_key_str("s2")); |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
338 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
339 | // iterate |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
340 | auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr}; |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
341 | auto iter = cxMapIteratorValues(map); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
342 | cx_foreach(cxstring*, s, iter) { |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
343 | auto found = std::find(ref.begin(), ref.end(), s->ptr); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
344 | ASSERT_NE(found, ref.end()); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
345 | ref.erase(found); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
346 | } |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
347 | EXPECT_EQ(ref.size(), 0); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
348 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
349 | cxMapDestroy(map); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
350 | EXPECT_TRUE(allocator.verify()); |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
351 | } |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
352 | |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
353 | static void test_simple_destructor(void *data) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
354 | strcpy((char *) data, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
355 | } |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
356 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
357 | static void test_advanced_destructor( |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
358 | [[maybe_unused]] void *unused, |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
359 | void *data |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
360 | ) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
361 | strcpy((char *) data, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
362 | } |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
363 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
364 | static void verify_any_destructor(CxMap *map) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
365 | auto k1 = cx_hash_key_str("key 1"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
366 | auto k2 = cx_hash_key_str("key 2"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
367 | auto k3 = cx_hash_key_str("key 3"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
368 | auto k4 = cx_hash_key_str("key 4"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
369 | auto k5 = cx_hash_key_str("key 5"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
370 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
371 | char v1[] = "val 1"; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
372 | char v2[] = "val 2"; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
373 | char v3[] = "val 3"; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
374 | char v4[] = "val 4"; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
375 | char v5[] = "val 5"; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
376 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
377 | cxMapPut(map, k1, (void *) v1); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
378 | cxMapPut(map, k2, (void *) v2); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
379 | cxMapPut(map, k3, (void *) v3); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
380 | cxMapPut(map, k4, (void *) v4); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
381 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
382 | cxMapRemove(map, k2); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
383 | auto r = cxMapRemoveAndGet(map, k3); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
384 | cxMapDetach(map, k1); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
385 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
386 | EXPECT_STREQ(v1, "val 1"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
387 | EXPECT_STREQ(v2, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
388 | EXPECT_STREQ(v3, "val 3"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
389 | EXPECT_STREQ(v4, "val 4"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
390 | EXPECT_STREQ(v5, "val 5"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
391 | EXPECT_EQ(r, v3); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
392 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
393 | cxMapClear(map); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
394 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
395 | EXPECT_STREQ(v1, "val 1"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
396 | EXPECT_STREQ(v2, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
397 | EXPECT_STREQ(v3, "val 3"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
398 | EXPECT_STREQ(v4, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
399 | EXPECT_STREQ(v5, "val 5"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
400 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
401 | cxMapPut(map, k1, (void *) v1); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
402 | cxMapPut(map, k3, (void *) v3); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
403 | cxMapPut(map, k5, (void *) v5); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
404 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
405 | { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
406 | auto iter = cxMapMutIteratorKeys(map); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
407 | cx_foreach(CxHashKey*, key, iter) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
408 | if (key->data.cstr[4] == '1') cxIteratorFlagRemoval(iter); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
409 | } |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
410 | } |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
411 | { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
412 | auto iter = cxMapMutIteratorValues(map); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
413 | cx_foreach(char*, v, iter) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
414 | if (v[4] == '5') cxIteratorFlagRemoval(iter); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
415 | } |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
416 | } |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
417 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
418 | EXPECT_STREQ(v1, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
419 | EXPECT_STREQ(v2, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
420 | EXPECT_STREQ(v3, "val 3"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
421 | EXPECT_STREQ(v4, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
422 | EXPECT_STREQ(v5, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
423 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
424 | v1[0] = v2[0] = v4[0] = v5[0] = 'c'; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
425 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
426 | cxMapDestroy(map); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
427 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
428 | EXPECT_STREQ(v1, "cK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
429 | EXPECT_STREQ(v2, "cK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
430 | EXPECT_STREQ(v3, "OK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
431 | EXPECT_STREQ(v4, "cK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
432 | EXPECT_STREQ(v5, "cK"); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
433 | } |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
434 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
435 | TEST(CxHashMap, SimpleDestructor) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
436 | CxTestingAllocator allocator; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
437 | auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
438 | map->simple_destructor = test_simple_destructor; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
439 | verify_any_destructor(map); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
440 | EXPECT_TRUE(allocator.verify()); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
441 | } |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
442 | |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
443 | TEST(CxHashMap, AdvancedDestructor) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
444 | CxTestingAllocator allocator; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
445 | auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
446 | map->advanced_destructor = test_advanced_destructor; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
447 | verify_any_destructor(map); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
448 | EXPECT_TRUE(allocator.verify()); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
449 | } |