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