Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "hashmap.h"
4 : #include "util.h"
5 :
6 : unsigned custom_counter = 0;
7 960 : static void custom_destruct(void* p) {
8 960 : custom_counter--;
9 960 : free(p);
10 960 : }
11 :
12 960 : DEFINE_HASH_OPS_FULL(boring_hash_ops, char, string_hash_func, string_compare_func, free, char, free);
13 960 : DEFINE_HASH_OPS_FULL(custom_hash_ops, char, string_hash_func, string_compare_func, custom_destruct, char, custom_destruct);
14 :
15 : void test_hashmap_funcs(void);
16 : void test_ordered_hashmap_funcs(void);
17 :
18 1 : static void test_ordered_hashmap_next(void) {
19 1 : _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL;
20 : int i;
21 :
22 1 : log_info("/* %s */", __func__);
23 :
24 1 : assert_se(m = ordered_hashmap_new(NULL));
25 6 : for (i = -2; i <= 2; i++)
26 5 : assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1);
27 5 : for (i = -2; i <= 1; i++)
28 4 : assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11));
29 1 : assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2)));
30 1 : assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1)));
31 1 : assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3)));
32 1 : }
33 :
34 1 : static void test_uint64_compare_func(void) {
35 1 : const uint64_t a = 0x100, b = 0x101;
36 :
37 1 : assert_se(uint64_compare_func(&a, &a) == 0);
38 1 : assert_se(uint64_compare_func(&a, &b) == -1);
39 1 : assert_se(uint64_compare_func(&b, &a) == 1);
40 1 : }
41 :
42 1 : static void test_trivial_compare_func(void) {
43 1 : assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
44 1 : assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
45 1 : assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
46 1 : }
47 :
48 1 : static void test_string_compare_func(void) {
49 : assert_se(string_compare_func("fred", "wilma") != 0);
50 : assert_se(string_compare_func("fred", "fred") == 0);
51 1 : }
52 :
53 112 : static void compare_cache(Hashmap *map, IteratedCache *cache) {
54 112 : const void **keys = NULL, **values = NULL;
55 : unsigned num, idx;
56 : Iterator iter;
57 : void *k, *v;
58 :
59 112 : assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0);
60 112 : assert_se(num == 0 || keys);
61 112 : assert_se(num == 0 || values);
62 :
63 112 : idx = 0;
64 491612 : HASHMAP_FOREACH_KEY(v, k, map, iter) {
65 491500 : assert_se(v == values[idx]);
66 491500 : assert_se(k == keys[idx]);
67 :
68 491500 : idx++;
69 : }
70 :
71 112 : assert_se(idx == num);
72 112 : }
73 :
74 1 : static void test_iterated_cache(void) {
75 : Hashmap *m;
76 : IteratedCache *c;
77 :
78 1 : log_info("/* %s */", __func__);
79 :
80 1 : assert_se(m = hashmap_new(NULL));
81 1 : assert_se(c = hashmap_iterated_cache_new(m));
82 1 : compare_cache(m, c);
83 :
84 101 : for (int stage = 0; stage < 100; stage++) {
85 :
86 10100 : for (int i = 0; i < 100; i++) {
87 10000 : int foo = stage * 1000 + i;
88 :
89 10000 : assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1);
90 : }
91 :
92 100 : compare_cache(m, c);
93 :
94 100 : if (!(stage % 10)) {
95 1010 : for (int i = 0; i < 100; i++) {
96 1000 : int foo = stage * 1000 + i;
97 :
98 1000 : assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777));
99 : }
100 :
101 10 : compare_cache(m, c);
102 : }
103 : }
104 :
105 1 : hashmap_clear(m);
106 1 : compare_cache(m, c);
107 :
108 1 : assert_se(hashmap_free(m) == NULL);
109 1 : assert_se(iterated_cache_free(c) == NULL);
110 1 : }
111 :
112 1 : int main(int argc, const char *argv[]) {
113 : /* This file tests in test-hashmap-plain.c, and tests in test-hashmap-ordered.c, which is generated
114 : * from test-hashmap-plain.c. Hashmap tests should be added to test-hashmap-plain.c, and here only if
115 : * they don't apply to ordered hashmaps. */
116 :
117 1 : log_parse_environment();
118 1 : log_open();
119 :
120 1 : test_hashmap_funcs();
121 1 : test_ordered_hashmap_funcs();
122 :
123 1 : log_info("/************ non-shared tests ************/");
124 :
125 1 : test_ordered_hashmap_next();
126 1 : test_uint64_compare_func();
127 1 : test_trivial_compare_func();
128 1 : test_string_compare_func();
129 1 : test_iterated_cache();
130 :
131 1 : return 0;
132 : }
|