Debian Codegraph @

src/test/test-hashmap.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "hashmap.h"
4 #include "tests.h"
5
6 // NOLINTNEXTLINE(misc-use-internal-linkage)
7 unsigned custom_counter = 0;
8 static void custom_destruct(void* p) {
9 custom_counter--;
10 free(p);
11 }
12
13 // NOLINTBEGIN(misc-use-internal-linkage)
14 DEFINE_HASH_OPS_FULL(boring_hash_ops, char, string_hash_func, string_compare_func, free, char, free);
15 DEFINE_HASH_OPS_FULL(custom_hash_ops, char, string_hash_func, string_compare_func, custom_destruct, char, custom_destruct);
16 // NOLINTEND(misc-use-internal-linkage)
17
18 TEST(ordered_hashmap_next) {
19 _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL;
20 int i;
21
22 assert_se(m = ordered_hashmap_new(NULL));
23 for (i = -2; i <= 2; i++)
24 assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1);
25 for (i = -2; i <= 1; i++)
26 assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11));
27 assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2)));
28 assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1)));
29 assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3)));
30 }
31
32 TEST(uint64_compare_func) {
33 const uint64_t a = 0x100, b = 0x101;
34
35 assert_se(uint64_compare_func(&a, &a) == 0);
36 assert_se(uint64_compare_func(&a, &b) == -1);
37 assert_se(uint64_compare_func(&b, &a) == 1);
38 }
39
40 TEST(trivial_compare_func) {
41 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
42 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
43 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
44 }
45
46 TEST(string_compare_func) {
47 ASSERT_NE(string_compare_func("fred", "wilma"), 0);
48 assert_se(string_compare_func("fred", "fred") == 0);
49 }
50
51 static void compare_cache(Hashmap *map, IteratedCache *cache) {
52 const void **keys = NULL, **values = NULL;
53 unsigned num, idx;
54 void *k, *v;
55
56 assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0);
57 assert_se(num == 0 || keys);
58 assert_se(num == 0 || values);
59
60 idx = 0;
61 HASHMAP_FOREACH_KEY(v, k, map) {
62 assert_se(v == values[idx]);
63 assert_se(k == keys[idx]);
64
65 idx++;
66 }
67
68 assert_se(idx == num);
69 }
70
71 TEST(iterated_cache) {
72 Hashmap *m;
73 IteratedCache *c;
74
75 assert_se(m = hashmap_new(NULL));
76 assert_se(c = hashmap_iterated_cache_new(m));
77 compare_cache(m, c);
78
79 for (int stage = 0; stage < 100; stage++) {
80
81 for (int i = 0; i < 100; i++) {
82 int foo = stage * 1000 + i;
83
84 assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1);
85 }
86
87 compare_cache(m, c);
88
89 if (!(stage % 10)) {
90 for (int i = 0; i < 100; i++) {
91 int foo = stage * 1000 + i;
92
93 assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777));
94 }
95
96 compare_cache(m, c);
97 }
98 }
99
100 hashmap_clear(m);
101 compare_cache(m, c);
102
103 ASSERT_NULL(hashmap_free(m));
104 ASSERT_NULL(iterated_cache_free(c));
105 }
106
107 TEST(hashmap_put_strdup) {
108 _cleanup_hashmap_free_ Hashmap *m = NULL;
109 char *s;
110
111 /* We don't have ordered_hashmap_put_strdup() yet. If it is added,
112 * these tests should be moved to test-hashmap-plain.c. */
113
114 assert_se(hashmap_put_strdup(&m, "foo", "bar") == 1);
115 assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0);
116 assert_se(hashmap_put_strdup(&m, "foo", "BAR") == -EEXIST);
117 assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0);
118 assert_se(hashmap_contains(m, "foo"));
119
120 s = hashmap_get(m, "foo");
121 ASSERT_STREQ(s, "bar");
122
123 assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 1);
124 assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 0);
125 assert_se(hashmap_put_strdup(&m, "xxx", "BAR") == -EEXIST);
126 assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 0);
127 assert_se(hashmap_contains(m, "xxx"));
128
129 s = hashmap_get(m, "xxx");
130 ASSERT_STREQ(s, "bar");
131 }
132
133 TEST(hashmap_put_strdup_null) {
134 _cleanup_hashmap_free_ Hashmap *m = NULL;
135 char *s;
136
137 assert_se(hashmap_put_strdup(&m, "foo", "bar") == 1);
138 assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0);
139 assert_se(hashmap_put_strdup(&m, "foo", NULL) == -EEXIST);
140 assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0);
141 assert_se(hashmap_contains(m, "foo"));
142
143 s = hashmap_get(m, "foo");
144 ASSERT_STREQ(s, "bar");
145
146 assert_se(hashmap_put_strdup(&m, "xxx", NULL) == 1);
147 assert_se(hashmap_put_strdup(&m, "xxx", "bar") == -EEXIST);
148 assert_se(hashmap_put_strdup(&m, "xxx", NULL) == 0);
149 assert_se(hashmap_contains(m, "xxx"));
150
151 s = hashmap_get(m, "xxx");
152 ASSERT_NULL(s);
153 }
154
155 /* This file tests in test-hashmap-plain.c, and tests in test-hashmap-ordered.c, which is generated
156 * from test-hashmap-plain.c. Hashmap tests should be added to test-hashmap-plain.c, and here only if
157 * they don't apply to ordered hashmaps. */
158
159 /* This variable allows us to assert that the tests from different compilation units were actually run. */
160 // NOLINTNEXTLINE(misc-use-internal-linkage)
161 int n_extern_tests_run = 0;
162
163 static int intro(void) {
164 assert_se(n_extern_tests_run == 0);
165 return EXIT_SUCCESS;
166 }
167
168 static int outro(void) {
169 /* Ensure hashmap and ordered_hashmap were tested. */
170 assert_se(n_extern_tests_run == 2);
171 return EXIT_SUCCESS;
172 }
173
174 DEFINE_TEST_MAIN_FULL(LOG_INFO, intro, outro);