]>
git.proxmox.com Git - systemd.git/blob - src/test/test-hashmap.c
2 This file is part of systemd
4 Copyright 2013 Daniel Buch
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.
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.
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/>.
25 void test_hashmap_funcs(void);
26 void test_ordered_hashmap_funcs(void);
28 static void test_ordered_hashmap_next(void) {
30 char *val1
, *val2
, *val3
, *val4
, *r
;
32 m
= ordered_hashmap_new(&string_hash_ops
);
33 val1
= strdup("val1");
35 val2
= strdup("val2");
37 val3
= strdup("val3");
39 val4
= strdup("val4");
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
);
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");
55 r
= ordered_hashmap_next(NULL
, "key 1");
57 r
= ordered_hashmap_next(m
, "key 5");
60 ordered_hashmap_free_free(m
);
63 static void test_uint64_compare_func(void) {
64 const uint64_t a
= 0x100, b
= 0x101;
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);
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);
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);
82 int main(int argc
, const char *argv
[]) {
84 test_ordered_hashmap_funcs();
86 test_ordered_hashmap_next();
87 test_uint64_compare_func();
88 test_trivial_compare_func();
89 test_string_compare_func();