]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-hashmap.c
update TODO
[thirdparty/systemd.git] / src / test / test-hashmap.c
CommitLineData
9341a4a1
DB
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
32a4456c
MS
25void test_hashmap_funcs(void);
26void test_ordered_hashmap_funcs(void);
9341a4a1 27
32a4456c
MS
28static void test_ordered_hashmap_next(void) {
29 OrderedHashmap *m;
9341a4a1
DB
30 char *val1, *val2, *val3, *val4, *r;
31
32a4456c 32 m = ordered_hashmap_new(&string_hash_ops);
9341a4a1
DB
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
32a4456c
MS
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);
9341a4a1 46
32a4456c 47 r = ordered_hashmap_next(m, "key 1");
9341a4a1 48 assert_se(streq(r, val2));
32a4456c 49 r = ordered_hashmap_next(m, "key 2");
9341a4a1 50 assert_se(streq(r, val3));
32a4456c 51 r = ordered_hashmap_next(m, "key 3");
9341a4a1 52 assert_se(streq(r, val4));
32a4456c
MS
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");
9341a4a1
DB
58 assert_se(!r);
59
32a4456c 60 ordered_hashmap_free_free(m);
d06b3a9d
RC
61}
62
9341a4a1 63static void test_uint64_compare_func(void) {
8097ab4f
ZJS
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);
9341a4a1
DB
69}
70
71static 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
77static 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
45fa9e29 82int main(int argc, const char *argv[]) {
32a4456c
MS
83 test_hashmap_funcs();
84 test_ordered_hashmap_funcs();
85
86 test_ordered_hashmap_next();
9341a4a1
DB
87 test_uint64_compare_func();
88 test_trivial_compare_func();
89 test_string_compare_func();
90}