tests/test_map.cpp

Wed, 20 Dec 2023 16:46:14 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 20 Dec 2023 16:46:14 +0100
changeset 766
e59b76889f00
parent 707
87eb4bdb2d0e
permissions
-rw-r--r--

bring back UCX test - fixes #341

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"
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
33 #include "test_map_generics.h"
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 #include <gtest/gtest.h>
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 #include <unordered_map>
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 #include <unordered_set>
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 struct map_operation {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 enum {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 put, rm
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 } op;
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 char const *key;
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 char const *value;
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 auto generate_map_operations() -> std::vector<map_operation> {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 return {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 {map_operation::put, "key 1", "test"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 {map_operation::put, "key 2", "blub"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 {map_operation::put, "key 3", "hallo"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 {map_operation::put, "key 2", "foobar"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 {map_operation::put, "key 4", "value 4"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 {map_operation::put, "key 5", "value 5"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 {map_operation::put, "key 6", "value 6"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 {map_operation::rm, "key 4", nullptr},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 {map_operation::put, "key 7", "value 7"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 {map_operation::put, "key 8", "value 8"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 {map_operation::rm, "does not exist", nullptr},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 {map_operation::put, "key 9", "value 9"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 {map_operation::put, "key 6", "other value"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 {map_operation::put, "key 7", "something else"},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 {map_operation::rm, "key 8", nullptr},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 {map_operation::rm, "key 2", nullptr},
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 {map_operation::put, "key 8", "new value"},
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 static void verify_map_contents(
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 CxMap *map,
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 std::unordered_map<std::string, std::string> const &refmap
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 ) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 // verify key iterator
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 auto keyiter = cxMapIteratorKeys(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 std::unordered_set<std::string> keys;
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
77 cx_foreach(CxHashKey*, elem, keyiter) {
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
78 keys.insert(std::string(reinterpret_cast<char const *>(elem->data), elem->len));
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 }
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
80 EXPECT_EQ(keyiter.index, map->size);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 ASSERT_EQ(keys.size(), map->size);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 for (auto &&k: keys) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 EXPECT_NE(refmap.find(k), refmap.end());
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 // verify value iterator
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 auto valiter = cxMapIteratorValues(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 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
91 cx_foreach(char const*, elem, valiter) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 values.insert(std::string(elem));
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 }
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
94 EXPECT_EQ(valiter.index, map->size);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 ASSERT_EQ(values.size(), map->size);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 for (auto &&v: values) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 EXPECT_NE(std::find_if(refmap.begin(), refmap.end(),
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 [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
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
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 // verify pair iterator
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 auto pairiter = cxMapIterator(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 std::unordered_map<std::string, std::string> pairs;
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 cx_foreach(CxMapEntry*, entry, pairiter) {
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
107 pairs[std::string(reinterpret_cast<char const *>(entry->key->data), entry->key->len)] = std::string(
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
108 (char *) entry->value);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 }
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
110 EXPECT_EQ(pairiter.index, map->size);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 ASSERT_EQ(pairs.size(), refmap.size());
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 for (auto &&p: pairs) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 ASSERT_EQ(p.second, refmap.at(p.first));
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 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 TEST(CxHashMap, Create) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 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
120 auto map = cxHashMapCreate(&allocator, 1, 0);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 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
122 EXPECT_GT(hmap->bucket_count, 0);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 cx_for_n(i, hmap->bucket_count) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 EXPECT_EQ(hmap->buckets[i], nullptr);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 }
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 669
diff changeset
126 EXPECT_EQ(map->item_size, 1);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 EXPECT_EQ(map->size, 0);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 EXPECT_EQ(map->allocator, &allocator);
685
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
129 EXPECT_FALSE(map->store_pointer);
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
130 EXPECT_EQ(map->cmpfunc, nullptr);
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
131 EXPECT_EQ(map->simple_destructor, nullptr);
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
132 EXPECT_EQ(map->advanced_destructor, nullptr);
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
133 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
134 cxMapStorePointers(map);
685
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
135 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
136 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
137 cxMapStoreObjects(map);
685
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
138 EXPECT_FALSE(map->store_pointer);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 cxMapDestroy(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 EXPECT_TRUE(allocator.verify());
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143
668
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
144 TEST(CxHashMap, CreateForStoringPointers) {
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
145 CxTestingAllocator allocator;
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
146 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
147 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
148 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
149 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
150 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
151 }
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
152 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
153 EXPECT_EQ(map->allocator, &allocator);
685
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
154 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
155 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
156
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
157 cxMapDestroy(map);
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
158 EXPECT_TRUE(allocator.verify());
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
159 }
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 659
diff changeset
160
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 TEST(CxHashMap, BasicOperations) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 // create the map
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
164 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8);
556
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 // create a reference map
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 std::unordered_map<std::string, std::string> 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 // generate operations
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 auto ops = generate_map_operations();
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 // verify iterators for empty map
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 verify_map_contents(map, refmap);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 // execute operations and verify results
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 for (auto &&op: ops) {
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
177 CxHashKey key = cx_hash_key_str(op.key);
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
178 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
179 if (op.op == map_operation::put) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 // 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
181 refmap[std::string(op.key)] = std::string(op.value);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 int result = cxMapPut(map, key, (void *) op.value);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 EXPECT_EQ(result, 0);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 auto added = cxMapGet(map, key);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 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
186 } else {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 // 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
188 auto found = refmap.find(op.key);
659
4a06fd63909a split cxMapRemove() to cxMapRemoveAndGet()
Mike Becker <universe@uap-core.de>
parents: 658
diff changeset
189 auto removed = cxMapRemoveAndGet(map, key);
556
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 if (found == refmap.end()) {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 EXPECT_EQ(removed, nullptr);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 } else {
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 EXPECT_EQ(std::string((char *) removed), found->second);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 refmap.erase(found);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 // compare the current map state with the reference map
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 verify_map_contents(map, refmap);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 }
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 // destroy the map and verify the memory (de)allocations
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 cxMapDestroy(map);
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 EXPECT_TRUE(allocator.verify());
3d19cae7e924 #199 tests for hash map
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 }
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
205
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
206 TEST(CxHashMap, RemoveViaIterator) {
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
207 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
208 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
209
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
210 cxMapPut(map, "key 1", (void *) "val 1");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
211 cxMapPut(map, "key 2", (void *) "val 2");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
212 cxMapPut(map, "key 3", (void *) "val 3");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
213 cxMapPut(map, "key 4", (void *) "val 4");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
214 cxMapPut(map, "key 5", (void *) "val 5");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
215 cxMapPut(map, "key 6", (void *) "val 6");
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
216
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 604
diff changeset
217 auto iter = cxMapMutIterator(map);
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
218 cx_foreach(CxMapEntry*, entry, iter) {
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
219 if (reinterpret_cast<char const *>(entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter);
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
220 }
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
221 EXPECT_EQ(map->size, 3);
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
222 EXPECT_EQ(iter.index, map->size);
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
223
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
224 EXPECT_EQ(cxMapGet(map, "key 1"), nullptr);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
225 EXPECT_NE(cxMapGet(map, "key 2"), nullptr);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
226 EXPECT_EQ(cxMapGet(map, "key 3"), nullptr);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
227 EXPECT_NE(cxMapGet(map, "key 4"), nullptr);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
228 EXPECT_EQ(cxMapGet(map, "key 5"), nullptr);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
229 EXPECT_NE(cxMapGet(map, "key 6"), nullptr);
561
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
230
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
231 cxMapDestroy(map);
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
232 EXPECT_TRUE(allocator.verify());
bb17790af41e #199 test removing via iterator
Mike Becker <universe@uap-core.de>
parents: 556
diff changeset
233 }
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
234
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
235 TEST(CxHashMap, RehashNotRequired) {
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
236 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
237 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
238
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
239 cxMapPut(map, "key 1", (void *) "val 1");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
240 cxMapPut(map, "key 2", (void *) "val 2");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
241 cxMapPut(map, "key 3", (void *) "val 3");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
242 cxMapPut(map, "key 4", (void *) "val 4");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
243 cxMapPut(map, "key 5", (void *) "val 5");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
244 cxMapPut(map, "key 6", (void *) "val 6");
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
245
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
246 // 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
247 int result = cxMapRehash(map);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
248 EXPECT_EQ(result, 0);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
249 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
250
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
251 cxMapDestroy(map);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
252 EXPECT_TRUE(allocator.verify());
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
253 }
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
254
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
255 TEST(CxHashMap, Rehash) {
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
256 CxTestingAllocator allocator;
687
612ed521b1c5 tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
257 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
258
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
259 cxMapPut(map, "key 1", (void *) "val 1");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
260 cxMapPut(map, "key 2", (void *) "val 2");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
261 cxMapPut(map, "key 3", (void *) "val 3");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
262 cxMapPut(map, "foo 4", (void *) "val 4");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
263 cxMapPut(map, "key 5", (void *) "val 5");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
264 cxMapPut(map, "key 6", (void *) "val 6");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
265 cxMapPut(map, "bar 7", (void *) "val 7");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
266 cxMapPut(map, "key 8", (void *) "val 8");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
267 cxMapPut(map, "key 9", (void *) "val 9");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
268 cxMapPut(map, "key 10", (void *) "val 10");
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
269
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
270 int result = cxMapRehash(map);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
271 EXPECT_EQ(result, 0);
687
612ed521b1c5 tweak rehash test to achieve missing coverage
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
272 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
273 EXPECT_EQ(map->size, 10);
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
274
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
275 EXPECT_STREQ((char *) cxMapGet(map, "key 1"), "val 1");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
276 EXPECT_STREQ((char *) cxMapGet(map, "key 2"), "val 2");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
277 EXPECT_STREQ((char *) cxMapGet(map, "key 3"), "val 3");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
278 EXPECT_STREQ((char *) cxMapGet(map, "foo 4"), "val 4");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
279 EXPECT_STREQ((char *) cxMapGet(map, "key 5"), "val 5");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
280 EXPECT_STREQ((char *) cxMapGet(map, "key 6"), "val 6");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
281 EXPECT_STREQ((char *) cxMapGet(map, "bar 7"), "val 7");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
282 EXPECT_STREQ((char *) cxMapGet(map, "key 8"), "val 8");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
283 EXPECT_STREQ((char *) cxMapGet(map, "key 9"), "val 9");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
284 EXPECT_STREQ((char *) cxMapGet(map, "key 10"), "val 10");
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
285
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
286 cxMapDestroy(map);
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 561
diff changeset
287 EXPECT_TRUE(allocator.verify());
594
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
288 }
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 TEST(CxHashMap, Clear) {
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
291 CxTestingAllocator allocator;
669
dce9b8450656 add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
Mike Becker <universe@uap-core.de>
parents: 668
diff changeset
292 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
293
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
294 cxMapPut(map, "key 1", (void *) "val 1");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
295 cxMapPut(map, "key 2", (void *) "val 2");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
296 cxMapPut(map, "key 3", (void *) "val 3");
594
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 EXPECT_EQ(map->size, 3);
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 cxMapClear(map);
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
301
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
302 EXPECT_EQ(map->size, 0);
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
303 EXPECT_EQ(cxMapGet(map, "key 1"), nullptr);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
304 EXPECT_EQ(cxMapGet(map, "key 2"), nullptr);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
305 EXPECT_EQ(cxMapGet(map, "key 3"), nullptr);
594
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
306
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
307 cxMapDestroy(map);
d90cfa6721f9 add test for cxMapClear()
Mike Becker <universe@uap-core.de>
parents: 563
diff changeset
308 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
309 }
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 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
312 // 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
313 CxTestingAllocator allocator;
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
314 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
315
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
316 // define some strings
685
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
317 auto s1 = CX_STR("this");
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
318 auto s2 = CX_STR("is");
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
319 auto s3 = CX_STR("a");
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
320 auto s4 = CX_STR("test");
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
321 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
322
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
323 // put them into the map
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
324 cxMapPut(map, "s1", &s1);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
325 cxMapPut(map, "s2", &s2);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
326 cxMapPut(map, "s3", &s3);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
327 cxMapPut(map, "s4", &s4);
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 // overwrite a value
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
330 cxMapPut(map, "s1", &s5);
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
331
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
332 // look up a string
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
333 auto s3p = reinterpret_cast<cxstring *>(cxMapGet(map, "s3"));
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
334 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
335 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
336 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
337
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
338 // remove a string
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
339 cxMapRemove(map, "s2");
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
340
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
341 // iterate
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
342 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
343 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
344 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
345 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
346 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
347 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
348 }
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
349 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
350
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
351 cxMapDestroy(map);
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
352 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
353 }
685
2dd841e364af add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
354
686
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
355 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
356 strcpy((char *) data, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
357 }
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
358
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
359 static void test_advanced_destructor(
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
360 [[maybe_unused]] void *unused,
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
361 void *data
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 strcpy((char *) data, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
364 }
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
365
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
366 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
367 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
368 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
369 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
370 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
371 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
372
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
373 char v1[] = "val 1";
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
374 char v2[] = "val 2";
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
375 char v3[] = "val 3";
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
376 char v4[] = "val 4";
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
377 char v5[] = "val 5";
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
378
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
379 cxMapPut(map, k1, (void *) v1);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
380 cxMapPut(map, k2, (void *) v2);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
381 cxMapPut(map, k3, (void *) v3);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
382 cxMapPut(map, k4, (void *) v4);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
383
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
384 cxMapRemove(map, k2);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
385 auto r = cxMapRemoveAndGet(map, k3);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
386 cxMapDetach(map, k1);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
387
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
388 EXPECT_STREQ(v1, "val 1");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
389 EXPECT_STREQ(v2, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
390 EXPECT_STREQ(v3, "val 3");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
391 EXPECT_STREQ(v4, "val 4");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
392 EXPECT_STREQ(v5, "val 5");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
393 EXPECT_EQ(r, v3);
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 cxMapClear(map);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
396
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
397 EXPECT_STREQ(v1, "val 1");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
398 EXPECT_STREQ(v2, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
399 EXPECT_STREQ(v3, "val 3");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
400 EXPECT_STREQ(v4, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
401 EXPECT_STREQ(v5, "val 5");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
402
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
403 cxMapPut(map, k1, (void *) v1);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
404 cxMapPut(map, k3, (void *) v3);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
405 cxMapPut(map, k5, (void *) v5);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
406
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
407 {
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
408 auto iter = cxMapMutIteratorKeys(map);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
409 cx_foreach(CxHashKey*, key, iter) {
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
410 if (reinterpret_cast<char const *>(key->data)[4] == '1') cxIteratorFlagRemoval(iter);
686
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 }
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
413 {
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
414 auto iter = cxMapMutIteratorValues(map);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
415 cx_foreach(char*, v, iter) {
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
416 if (v[4] == '5') cxIteratorFlagRemoval(iter);
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 }
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
419
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
420 EXPECT_STREQ(v1, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
421 EXPECT_STREQ(v2, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
422 EXPECT_STREQ(v3, "val 3");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
423 EXPECT_STREQ(v4, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
424 EXPECT_STREQ(v5, "OK");
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 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
427
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
428 cxMapDestroy(map);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
429
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
430 EXPECT_STREQ(v1, "cK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
431 EXPECT_STREQ(v2, "cK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
432 EXPECT_STREQ(v3, "OK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
433 EXPECT_STREQ(v4, "cK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
434 EXPECT_STREQ(v5, "cK");
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
435 }
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
436
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
437 TEST(CxHashMap, SimpleDestructor) {
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
438 CxTestingAllocator allocator;
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
439 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
440 map->simple_destructor = test_simple_destructor;
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
441 verify_any_destructor(map);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
442 EXPECT_TRUE(allocator.verify());
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
443 }
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
444
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
445 TEST(CxHashMap, AdvancedDestructor) {
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
446 CxTestingAllocator allocator;
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
447 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
448 map->advanced_destructor = test_advanced_destructor;
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
449 verify_any_destructor(map);
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
450 EXPECT_TRUE(allocator.verify());
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
451 }
691
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
452
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
453 TEST(CxHashMap, Generics) {
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
454 CxTestingAllocator allocator;
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
455 auto map = test_map_generics_step_1(&allocator);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
456
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
457 EXPECT_EQ(map->size, 3);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
458 EXPECT_STREQ((char *) cxMapGet(map, "test"), "test");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
459 EXPECT_STREQ((char *) cxMapGet(map, "foo"), "bar");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
460 EXPECT_STREQ((char *) cxMapGet(map, "hallo"), "welt");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
461
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
462 test_map_generics_step_2(map);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
463
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
464 EXPECT_EQ(map->size, 2);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
465 EXPECT_STREQ((char *) cxMapGet(map, "key"), "value");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
466 EXPECT_STREQ((char *) cxMapGet(map, "foo"), "bar");
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
467
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
468 test_map_generics_step_3(map);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
469
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
470 EXPECT_EQ(map->size, 0);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
471
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
472 cxMapDestroy(map);
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
473 EXPECT_TRUE(allocator.verify());
65baf7f45ac8 bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents: 690
diff changeset
474 }
706
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
475
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
476 TEST(EmptyMap, Size) {
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
477 auto map = cxEmptyMap;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
478
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
479 EXPECT_EQ(map->size, 0);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
480 }
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
481
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
482 TEST(EmptyMap, Iterator) {
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
483 auto map = cxEmptyMap;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
484
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
485 auto it1 = cxMapIterator(map);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
486 auto it2 = cxMapIteratorValues(map);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
487 auto it3 = cxMapIteratorKeys(map);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
488 auto it4 = cxMapMutIterator(map);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
489 auto it5 = cxMapMutIteratorValues(map);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
490 auto it6 = cxMapMutIteratorKeys(map);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
491
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
492 EXPECT_FALSE(cxIteratorValid(it1));
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
493 EXPECT_FALSE(cxIteratorValid(it2));
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
494 EXPECT_FALSE(cxIteratorValid(it3));
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
495 EXPECT_FALSE(cxIteratorValid(it4));
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
496 EXPECT_FALSE(cxIteratorValid(it5));
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
497 EXPECT_FALSE(cxIteratorValid(it6));
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
498
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
499 int c = 0;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
500 cx_foreach(void*, data, it1) c++;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
501 cx_foreach(void*, data, it2) c++;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
502 cx_foreach(void*, data, it3) c++;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
503 cx_foreach(void*, data, it4) c++;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
504 cx_foreach(void*, data, it5) c++;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
505 cx_foreach(void*, data, it6) c++;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
506 EXPECT_EQ(c, 0);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
507 }
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
508
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
509 TEST(EmptyMap, NoOps) {
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
510 auto map = cxEmptyMap;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
511
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
512 ASSERT_NO_FATAL_FAILURE(cxMapClear(map));
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
513 ASSERT_NO_FATAL_FAILURE(cxMapDestroy(map));
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
514 }
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
515
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
516 TEST(EmptyMap, Get) {
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
517 auto map = cxEmptyMap;
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
518
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
519 CxHashKey key = cx_hash_key_str("test");
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
520 EXPECT_EQ(cxMapGet(map, key), nullptr);
8c6edaccaef1 add empty map implementation - fixes #259
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
521 }

mercurial