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