]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hashmap.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-hashmap.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "hashmap.h"
4 #include "util.h"
5
6 void test_hashmap_funcs(void);
7 void test_ordered_hashmap_funcs(void);
8
9 static void test_ordered_hashmap_next(void) {
10 _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL;
11 int i;
12
13 assert_se(m = ordered_hashmap_new(NULL));
14 for (i = -2; i <= 2; i++)
15 assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1);
16 for (i = -2; i <= 1; i++)
17 assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11));
18 assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2)));
19 assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1)));
20 assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3)));
21 }
22
23 typedef struct Item {
24 int seen;
25 } Item;
26 static void item_seen(Item *item) {
27 item->seen++;
28 }
29
30 static void test_hashmap_free_with_destructor(void) {
31 Hashmap *m;
32 struct Item items[4] = {};
33 unsigned i;
34
35 assert_se(m = hashmap_new(NULL));
36 for (i = 0; i < ELEMENTSOF(items) - 1; i++)
37 assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1);
38
39 m = hashmap_free_with_destructor(m, item_seen);
40 assert_se(items[0].seen == 1);
41 assert_se(items[1].seen == 1);
42 assert_se(items[2].seen == 1);
43 assert_se(items[3].seen == 0);
44 }
45
46 static void test_uint64_compare_func(void) {
47 const uint64_t a = 0x100, b = 0x101;
48
49 assert_se(uint64_compare_func(&a, &a) == 0);
50 assert_se(uint64_compare_func(&a, &b) == -1);
51 assert_se(uint64_compare_func(&b, &a) == 1);
52 }
53
54 static void test_trivial_compare_func(void) {
55 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
56 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
57 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
58 }
59
60 static void test_string_compare_func(void) {
61 assert_se(string_compare_func("fred", "wilma") != 0);
62 assert_se(string_compare_func("fred", "fred") == 0);
63 }
64
65 static void compare_cache(Hashmap *map, IteratedCache *cache) {
66 const void **keys = NULL, **values = NULL;
67 unsigned num, idx;
68 Iterator iter;
69 void *k, *v;
70
71 assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0);
72 assert_se(num == 0 || keys);
73 assert_se(num == 0 || values);
74
75 idx = 0;
76 HASHMAP_FOREACH_KEY(v, k, map, iter) {
77 assert_se(v == values[idx]);
78 assert_se(k == keys[idx]);
79
80 idx++;
81 }
82
83 assert_se(idx == num);
84 }
85
86 static void test_iterated_cache(void) {
87 Hashmap *m;
88 IteratedCache *c;
89
90 assert_se(m = hashmap_new(NULL));
91 assert_se(c = hashmap_iterated_cache_new(m));
92 compare_cache(m, c);
93
94 for (int stage = 0; stage < 100; stage++) {
95
96 for (int i = 0; i < 100; i++) {
97 int foo = stage * 1000 + i;
98
99 assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1);
100 }
101
102 compare_cache(m, c);
103
104 if (!(stage % 10)) {
105 for (int i = 0; i < 100; i++) {
106 int foo = stage * 1000 + i;
107
108 assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777));
109 }
110
111 compare_cache(m, c);
112 }
113 }
114
115 hashmap_clear(m);
116 compare_cache(m, c);
117
118 assert_se(hashmap_free(m) == NULL);
119 assert_se(iterated_cache_free(c) == NULL);
120 }
121
122 static void test_path_hashmap(void) {
123 _cleanup_hashmap_free_ Hashmap *h = NULL;
124
125 assert_se(h = hashmap_new(&path_hash_ops));
126
127 assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0);
128 assert_se(hashmap_put(h, "/foo", INT_TO_PTR(2)) >= 0);
129 assert_se(hashmap_put(h, "//foo", INT_TO_PTR(3)) == -EEXIST);
130 assert_se(hashmap_put(h, "//foox/", INT_TO_PTR(4)) >= 0);
131 assert_se(hashmap_put(h, "/foox////", INT_TO_PTR(5)) == -EEXIST);
132 assert_se(hashmap_put(h, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0);
133 assert_se(hashmap_put(h, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST);
134
135 assert_se(hashmap_get(h, "foo") == INT_TO_PTR(1));
136 assert_se(hashmap_get(h, "foo/") == INT_TO_PTR(1));
137 assert_se(hashmap_get(h, "foo////") == INT_TO_PTR(1));
138 assert_se(hashmap_get(h, "/foo") == INT_TO_PTR(2));
139 assert_se(hashmap_get(h, "//foo") == INT_TO_PTR(2));
140 assert_se(hashmap_get(h, "/////foo////") == INT_TO_PTR(2));
141 assert_se(hashmap_get(h, "/////foox////") == INT_TO_PTR(4));
142 assert_se(hashmap_get(h, "/foox/") == INT_TO_PTR(4));
143 assert_se(hashmap_get(h, "/foox") == INT_TO_PTR(4));
144 assert_se(!hashmap_get(h, "foox"));
145 assert_se(hashmap_get(h, "foo/bar/quux") == INT_TO_PTR(6));
146 assert_se(hashmap_get(h, "foo////bar////quux/////") == INT_TO_PTR(6));
147 assert_se(!hashmap_get(h, "/foo////bar////quux/////"));
148 }
149
150 int main(int argc, const char *argv[]) {
151 test_hashmap_funcs();
152 test_ordered_hashmap_funcs();
153
154 test_ordered_hashmap_next();
155 test_hashmap_free_with_destructor();
156 test_uint64_compare_func();
157 test_trivial_compare_func();
158 test_string_compare_func();
159 test_iterated_cache();
160 test_path_hashmap();
161
162 return 0;
163 }