chiark / gitweb /
Remove src/sysv-generator
[elogind.git] / src / test / test-hashmap.c
1 /***
2   This file is part of systemd
3
4   Copyright 2013 Daniel Buch
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "util.h"
21 #include "hashmap.h"
22
23 void test_hashmap_funcs(void);
24 void test_ordered_hashmap_funcs(void);
25
26 static void test_ordered_hashmap_next(void) {
27         OrderedHashmap *m;
28         char *val1, *val2, *val3, *val4, *r;
29
30         m = ordered_hashmap_new(&string_hash_ops);
31         val1 = strdup("val1");
32         assert_se(val1);
33         val2 = strdup("val2");
34         assert_se(val2);
35         val3 = strdup("val3");
36         assert_se(val3);
37         val4 = strdup("val4");
38         assert_se(val4);
39
40         ordered_hashmap_put(m, "key 1", val1);
41         ordered_hashmap_put(m, "key 2", val2);
42         ordered_hashmap_put(m, "key 3", val3);
43         ordered_hashmap_put(m, "key 4", val4);
44
45         r = ordered_hashmap_next(m, "key 1");
46         assert_se(streq(r, val2));
47         r = ordered_hashmap_next(m, "key 2");
48         assert_se(streq(r, val3));
49         r = ordered_hashmap_next(m, "key 3");
50         assert_se(streq(r, val4));
51         r = ordered_hashmap_next(m, "key 4");
52         assert_se(!r);
53         r = ordered_hashmap_next(NULL, "key 1");
54         assert_se(!r);
55         r = ordered_hashmap_next(m, "key 5");
56         assert_se(!r);
57
58         ordered_hashmap_free_free(m);
59 }
60
61 static void test_uint64_compare_func(void) {
62         const uint64_t a = 0x100, b = 0x101;
63
64         assert_se(uint64_compare_func(&a, &a) == 0);
65         assert_se(uint64_compare_func(&a, &b) == -1);
66         assert_se(uint64_compare_func(&b, &a) == 1);
67 }
68
69 static void test_trivial_compare_func(void) {
70         assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
71         assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
72         assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
73 }
74
75 static void test_string_compare_func(void) {
76         assert_se(string_compare_func("fred", "wilma") != 0);
77         assert_se(string_compare_func("fred", "fred") == 0);
78 }
79
80 int main(int argc, const char *argv[]) {
81         test_hashmap_funcs();
82         test_ordered_hashmap_funcs();
83
84         test_ordered_hashmap_next();
85         test_uint64_compare_func();
86         test_trivial_compare_func();
87         test_string_compare_func();
88 }