chiark / gitweb /
Fix service file to match installed elogind binary location
[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 "hashmap.h"
21 #include "util.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         _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL;
28         int i;
29
30         assert_se(m = ordered_hashmap_new(NULL));
31         for (i = -2; i <= 2; i++)
32                 assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1);
33         for (i = -2; i <= 1; i++)
34                 assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11));
35         assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2)));
36         assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1)));
37         assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3)));
38 }
39
40 static void test_uint64_compare_func(void) {
41         const uint64_t a = 0x100, b = 0x101;
42
43         assert_se(uint64_compare_func(&a, &a) == 0);
44         assert_se(uint64_compare_func(&a, &b) == -1);
45         assert_se(uint64_compare_func(&b, &a) == 1);
46 }
47
48 static void test_trivial_compare_func(void) {
49         assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
50         assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
51         assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
52 }
53
54 static void test_string_compare_func(void) {
55         assert_se(string_compare_func("fred", "wilma") != 0);
56         assert_se(string_compare_func("fred", "fred") == 0);
57 }
58
59 int main(int argc, const char *argv[]) {
60         test_hashmap_funcs();
61         test_ordered_hashmap_funcs();
62
63         test_ordered_hashmap_next();
64         test_uint64_compare_func();
65         test_trivial_compare_func();
66         test_string_compare_func();
67 }