chiark / gitweb /
hash-func: add generic hash_ops implementation for hashing paths
[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   systemd is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   systemd is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "hashmap.h"
22 #include "util.h"
23
24 void test_hashmap_funcs(void);
25 void test_ordered_hashmap_funcs(void);
26
27 static void test_ordered_hashmap_next(void) {
28         _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL;
29         int i;
30
31         assert_se(m = ordered_hashmap_new(NULL));
32         for (i = -2; i <= 2; i++)
33                 assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1);
34         for (i = -2; i <= 1; i++)
35                 assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11));
36         assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2)));
37         assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1)));
38         assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3)));
39 }
40
41 typedef struct Item {
42         int seen;
43 } Item;
44 static void item_seen(Item *item) {
45         item->seen++;
46 }
47
48 static void test_hashmap_free_with_destructor(void) {
49         Hashmap *m;
50         struct Item items[4] = {};
51         unsigned i;
52
53         assert_se(m = hashmap_new(NULL));
54         for (i = 0; i < ELEMENTSOF(items) - 1; i++)
55                 assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1);
56
57         m = hashmap_free_with_destructor(m, item_seen);
58         assert_se(items[0].seen == 1);
59         assert_se(items[1].seen == 1);
60         assert_se(items[2].seen == 1);
61         assert_se(items[3].seen == 0);
62 }
63
64 static void test_uint64_compare_func(void) {
65         const uint64_t a = 0x100, b = 0x101;
66
67         assert_se(uint64_compare_func(&a, &a) == 0);
68         assert_se(uint64_compare_func(&a, &b) == -1);
69         assert_se(uint64_compare_func(&b, &a) == 1);
70 }
71
72 static void test_trivial_compare_func(void) {
73         assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
74         assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
75         assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
76 }
77
78 static void test_string_compare_func(void) {
79         assert_se(string_compare_func("fred", "wilma") != 0);
80         assert_se(string_compare_func("fred", "fred") == 0);
81 }
82
83 static void test_path_hashmap(void) {
84         _cleanup_(hashmap_freep) Hashmap *h = NULL;
85
86         assert_se(h = hashmap_new(&path_hash_ops));
87
88         assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0);
89         assert_se(hashmap_put(h, "/foo", INT_TO_PTR(2)) >= 0);
90         assert_se(hashmap_put(h, "//foo", INT_TO_PTR(3)) == -EEXIST);
91         assert_se(hashmap_put(h, "//foox/", INT_TO_PTR(4)) >= 0);
92         assert_se(hashmap_put(h, "/foox////", INT_TO_PTR(5)) == -EEXIST);
93         assert_se(hashmap_put(h, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0);
94         assert_se(hashmap_put(h, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST);
95
96         assert_se(hashmap_get(h, "foo") == INT_TO_PTR(1));
97         assert_se(hashmap_get(h, "foo/") == INT_TO_PTR(1));
98         assert_se(hashmap_get(h, "foo////") == INT_TO_PTR(1));
99         assert_se(hashmap_get(h, "/foo") == INT_TO_PTR(2));
100         assert_se(hashmap_get(h, "//foo") == INT_TO_PTR(2));
101         assert_se(hashmap_get(h, "/////foo////") == INT_TO_PTR(2));
102         assert_se(hashmap_get(h, "/////foox////") == INT_TO_PTR(4));
103         assert_se(hashmap_get(h, "/foox/") == INT_TO_PTR(4));
104         assert_se(hashmap_get(h, "/foox") == INT_TO_PTR(4));
105         assert_se(!hashmap_get(h, "foox"));
106         assert_se(hashmap_get(h, "foo/bar/quux") == INT_TO_PTR(6));
107         assert_se(hashmap_get(h, "foo////bar////quux/////") == INT_TO_PTR(6));
108         assert_se(!hashmap_get(h, "/foo////bar////quux/////"));
109 }
110
111 int main(int argc, const char *argv[]) {
112         test_hashmap_funcs();
113         test_ordered_hashmap_funcs();
114
115         test_ordered_hashmap_next();
116         test_hashmap_free_with_destructor();
117         test_uint64_compare_func();
118         test_trivial_compare_func();
119         test_string_compare_func();
120         test_path_hashmap();
121
122         return 0;
123 }