chiark / gitweb /
sd-bus: reuse the KDBUS_CMD_FREE wrapper wherever appropriate
[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 <inttypes.h>
21 #include "strv.h"
22 #include "util.h"
23 #include "hashmap.h"
24
25 void test_hashmap_funcs(void);
26 void test_ordered_hashmap_funcs(void);
27
28 static void test_ordered_hashmap_next(void) {
29         OrderedHashmap *m;
30         char *val1, *val2, *val3, *val4, *r;
31
32         m = ordered_hashmap_new(&string_hash_ops);
33         val1 = strdup("val1");
34         assert_se(val1);
35         val2 = strdup("val2");
36         assert_se(val2);
37         val3 = strdup("val3");
38         assert_se(val3);
39         val4 = strdup("val4");
40         assert_se(val4);
41
42         ordered_hashmap_put(m, "key 1", val1);
43         ordered_hashmap_put(m, "key 2", val2);
44         ordered_hashmap_put(m, "key 3", val3);
45         ordered_hashmap_put(m, "key 4", val4);
46
47         r = ordered_hashmap_next(m, "key 1");
48         assert_se(streq(r, val2));
49         r = ordered_hashmap_next(m, "key 2");
50         assert_se(streq(r, val3));
51         r = ordered_hashmap_next(m, "key 3");
52         assert_se(streq(r, val4));
53         r = ordered_hashmap_next(m, "key 4");
54         assert_se(!r);
55         r = ordered_hashmap_next(NULL, "key 1");
56         assert_se(!r);
57         r = ordered_hashmap_next(m, "key 5");
58         assert_se(!r);
59
60         ordered_hashmap_free_free(m);
61 }
62
63 static void test_uint64_compare_func(void) {
64         const uint64_t a = 0x100, b = 0x101;
65
66         assert_se(uint64_compare_func(&a, &a) == 0);
67         assert_se(uint64_compare_func(&a, &b) == -1);
68         assert_se(uint64_compare_func(&b, &a) == 1);
69 }
70
71 static void test_trivial_compare_func(void) {
72         assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
73         assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
74         assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
75 }
76
77 static void test_string_compare_func(void) {
78         assert_se(!string_compare_func("fred", "wilma") == 0);
79         assert_se(string_compare_func("fred", "fred") == 0);
80 }
81
82 int main(int argc, const char *argv[]) {
83         test_hashmap_funcs();
84         test_ordered_hashmap_funcs();
85
86         test_ordered_hashmap_next();
87         test_uint64_compare_func();
88         test_trivial_compare_func();
89         test_string_compare_func();
90 }