]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-hashmap.c
hwdb: add support for Alienware graphics amplifier
[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
9341a4a1
DB
20#include "util.h"
21#include "hashmap.h"
22
32a4456c
MS
23void test_hashmap_funcs(void);
24void test_ordered_hashmap_funcs(void);
9341a4a1 25
32a4456c
MS
26static void test_ordered_hashmap_next(void) {
27 OrderedHashmap *m;
9341a4a1
DB
28 char *val1, *val2, *val3, *val4, *r;
29
32a4456c 30 m = ordered_hashmap_new(&string_hash_ops);
9341a4a1
DB
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
32a4456c
MS
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);
9341a4a1 44
32a4456c 45 r = ordered_hashmap_next(m, "key 1");
9341a4a1 46 assert_se(streq(r, val2));
32a4456c 47 r = ordered_hashmap_next(m, "key 2");
9341a4a1 48 assert_se(streq(r, val3));
32a4456c 49 r = ordered_hashmap_next(m, "key 3");
9341a4a1 50 assert_se(streq(r, val4));
32a4456c
MS
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");
9341a4a1
DB
56 assert_se(!r);
57
32a4456c 58 ordered_hashmap_free_free(m);
d06b3a9d
RC
59}
60
9341a4a1 61static void test_uint64_compare_func(void) {
8097ab4f
ZJS
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);
9341a4a1
DB
67}
68
69static 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
75static void test_string_compare_func(void) {
4b3eff61 76 assert_se(string_compare_func("fred", "wilma") != 0);
9341a4a1
DB
77 assert_se(string_compare_func("fred", "fred") == 0);
78}
79
45fa9e29 80int main(int argc, const char *argv[]) {
32a4456c
MS
81 test_hashmap_funcs();
82 test_ordered_hashmap_funcs();
83
84 test_ordered_hashmap_next();
9341a4a1
DB
85 test_uint64_compare_func();
86 test_trivial_compare_func();
87 test_string_compare_func();
88}