tests/test_map.cpp

Sun, 16 Apr 2023 21:09:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 16 Apr 2023 21:09:25 +0200
changeset 682
34120a385fc8
parent 677
b09aae58bba4
child 685
2dd841e364af
permissions
-rw-r--r--

work around some MSC problems

556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/hash_map.h"
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include "util_allocator.h"
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 #include <gtest/gtest.h>
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 #include <unordered_map>
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 #include <unordered_set>
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 struct map_operation {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 enum {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 put, rm
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 } op;
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 char const *key;
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 char const *value;
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 };
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 auto generate_map_operations() -> std::vector<map_operation> {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 return {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 {map_operation::put, "key 1", "test"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 {map_operation::put, "key 2", "blub"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 {map_operation::put, "key 3", "hallo"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 {map_operation::put, "key 2", "foobar"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 {map_operation::put, "key 4", "value 4"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 {map_operation::put, "key 5", "value 5"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 {map_operation::put, "key 6", "value 6"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 {map_operation::rm, "key 4", nullptr},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 {map_operation::put, "key 7", "value 7"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 {map_operation::put, "key 8", "value 8"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 {map_operation::rm, "does not exist", nullptr},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 {map_operation::put, "key 9", "value 9"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 {map_operation::put, "key 6", "other value"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 {map_operation::put, "key 7", "something else"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 {map_operation::rm, "key 8", nullptr},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 {map_operation::rm, "key 2", nullptr},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 {map_operation::put, "key 8", "new value"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 };
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 static void verify_map_contents(
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 CxMap *map,
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 std::unordered_map<std::string, std::string> const &refmap
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 ) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 // verify key iterator
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 auto keyiter = cxMapIteratorKeys(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
056e5f592d84 change hash functions
Mike Becker <universe@uap-core.de>
parents: 595
diff changeset
77 keys.insert(std::string(elem->data.cstr, elem->len));
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 ASSERT_EQ(keys.size(), map->size);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 for (auto &&k: keys) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 EXPECT_NE(refmap.find(k), refmap.end());
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 // verify value iterator
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 auto valiter = cxMapIteratorValues(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 std::unordered_set<std::string> values; // we use that the values in our test data are unique strings
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 cx_foreach(char const*, elem, valiter) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 values.insert(std::string(elem));
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 ASSERT_EQ(values.size(), map->size);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 for (auto &&v: values) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 EXPECT_NE(std::find_if(refmap.begin(), refmap.end(),
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 [v](auto const &e) { return e.second == v; }), refmap.end());
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 // verify pair iterator
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 auto pairiter = cxMapIterator(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 std::unordered_map<std::string, std::string> pairs;
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 cx_foreach(CxMapEntry*, entry, pairiter) {
604
056e5f592d84 change hash functions
Mike Becker <universe@uap-core.de>
parents: 595
diff changeset
106 pairs[std::string(entry->key->data.cstr, entry->key->len)] = std::string((char *) entry->value);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 ASSERT_EQ(pairs.size(), refmap.size());
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 for (auto &&p: pairs) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 ASSERT_EQ(p.second, refmap.at(p.first));
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 TEST(CxHashMap, Create) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 EXPECT_GT(hmap->bucket_count, 0);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 cx_for_n(i, hmap->bucket_count) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 EXPECT_EQ(hmap->buckets[i], nullptr);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 EXPECT_EQ(map->size, 0);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 EXPECT_EQ(map->allocator, &allocator);
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
127 EXPECT_FALSE(map->store_pointers);
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
128 cxMapStorePointers(map);
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
129 EXPECT_TRUE(map->store_pointers);
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 669
diff changeset
130 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
131 cxMapStoreObjects(map);
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
132 EXPECT_FALSE(map->store_pointers);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 cxMapDestroy(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 EXPECT_TRUE(allocator.verify());
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137
668
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
138 TEST(CxHashMap, CreateForStoringPointers) {
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
139 CxTestingAllocator allocator;
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
140 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
141 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
142 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
143 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
144 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
145 }
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
146 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
147 EXPECT_EQ(map->allocator, &allocator);
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
148 EXPECT_TRUE(map->store_pointers);
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 669
diff changeset
149 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
150
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
151 cxMapDestroy(map);
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
152 EXPECT_TRUE(allocator.verify());
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
153 }
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
154
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 TEST(CxHashMap, BasicOperations) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 // create the map
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
158 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 // create a reference map
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 std::unordered_map<std::string, std::string> refmap;
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 // generate operations
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 auto ops = generate_map_operations();
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 // verify iterators for empty map
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 verify_map_contents(map, refmap);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 // execute operations and verify results
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 for (auto &&op: ops) {
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
171 CxHashKey key = cx_hash_key_str(op.key);
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
172 key.hash = 0; // force the hash map to compute the hash
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 if (op.op == map_operation::put) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 // execute a put operation and verify that the exact value can be read back
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 refmap[std::string(op.key)] = std::string(op.value);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 int result = cxMapPut(map, key, (void *) op.value);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 EXPECT_EQ(result, 0);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 auto added = cxMapGet(map, key);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 EXPECT_EQ(memcmp(op.value, added, strlen(op.value)), 0);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 } else {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 // execute a remove and verify that the removed element was returned (or nullptr)
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 auto found = refmap.find(op.key);
659
4a06fd63909a split cxMapRemove() to cxMapRemoveAndGet()
Mike Becker <universe@uap-core.de>
parents: 658
diff changeset
183 auto removed = cxMapRemoveAndGet(map, key);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 if (found == refmap.end()) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 EXPECT_EQ(removed, nullptr);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 } else {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 EXPECT_EQ(std::string((char *) removed), found->second);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 refmap.erase(found);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 // compare the current map state with the reference map
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 verify_map_contents(map, refmap);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 // destroy the map and verify the memory (de)allocations
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 cxMapDestroy(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 EXPECT_TRUE(allocator.verify());
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 }
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
199
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
200 TEST(CxHashMap, RemoveViaIterator) {
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
201 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
202 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
203
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
204 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
205 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
206 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
207 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
208 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
209 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
210
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 604
diff changeset
211 auto iter = cxMapMutIterator(map);
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
212 cx_foreach(CxMapEntry*, entry, iter) {
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 604
diff changeset
213 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
214 }
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
215 EXPECT_EQ(map->size, 3);
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
216 EXPECT_EQ(iter.index, map->size);
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
217
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
218 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
219 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
220 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
221 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
222 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
223 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
224
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
225 cxMapDestroy(map);
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
226 EXPECT_TRUE(allocator.verify());
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
227 }
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
228
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
229 TEST(CxHashMap, RehashNotRequired) {
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
230 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
231 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
232
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
233 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
234 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
235 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
236 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
237 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
238 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
239
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
240 // 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
241 int result = cxMapRehash(map);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
242 EXPECT_EQ(result, 0);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
243 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
244
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
245 cxMapDestroy(map);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
246 EXPECT_TRUE(allocator.verify());
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
247 }
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 TEST(CxHashMap, Rehash) {
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
250 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
251 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
252
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
253 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
254 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
255 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
256 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
257 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
258 cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6");
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
259 cxMapPut(map, cx_hash_key_str("key 7"), (void *) "val 7");
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
260
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
261 int result = cxMapRehash(map);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
262 EXPECT_EQ(result, 0);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
263 EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 17);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
264 EXPECT_EQ(map->size, 7);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
265
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
266 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
267 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
268 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 3")), "val 3"), 0);
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
269 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 4")), "val 4"), 0);
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
270 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
271 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 6")), "val 6"), 0);
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
272 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 7")), "val 7"), 0);
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
273
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
274 cxMapDestroy(map);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
275 EXPECT_TRUE(allocator.verify());
594
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
276 }
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
277
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
278 TEST(CxHashMap, Clear) {
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
279 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
280 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0);
595
0da254bf23e6 remove unnecessary code
Mike Becker <universe@uap-core.de>
parents: 594
diff changeset
281
594
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
282 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
283 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
284 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
285
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
286 EXPECT_EQ(map->size, 3);
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 cxMapClear(map);
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
289
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
290 EXPECT_EQ(map->size, 0);
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
291 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
292 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
293 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
294
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
295 cxMapDestroy(map);
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
296 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
297 }
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
298
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
299 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
300 // 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
301 CxTestingAllocator allocator;
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
302 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
303
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
304 // define some strings
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
305 cxstring s1 = CX_STR("this");
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
306 cxstring s2 = CX_STR("is");
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
307 cxstring s3 = CX_STR("a");
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
308 cxstring s4 = CX_STR("test");
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
309 cxstring s5 = CX_STR("setup");
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
310
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
311 // 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
312 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
313 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
314 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
315 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
316
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
317 // 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
318 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
319
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
320 // 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
321 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
322 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
323 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
324 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
325
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
326 // remove a string
659
4a06fd63909a split cxMapRemove() to cxMapRemoveAndGet()
Mike Becker <universe@uap-core.de>
parents: 658
diff changeset
327 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
328
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
329 // iterate
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
330 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
331 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
332 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
333 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
334 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
335 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
336 }
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
337 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
338
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
339 cxMapDestroy(map);
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
340 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
341 }

mercurial