]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hashmap.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-hashmap.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd
4
5 Copyright 2013 Daniel Buch
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "hashmap.h"
22 #include "util.h"
23
24 void test_hashmap_funcs(void);
25 void test_ordered_hashmap_funcs(void);
26
27 static void test_ordered_hashmap_next(void) {
28 _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL;
29 int i;
30
31 assert_se(m = ordered_hashmap_new(NULL));
32 for (i = -2; i <= 2; i++)
33 assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1);
34 for (i = -2; i <= 1; i++)
35 assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11));
36 assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2)));
37 assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1)));
38 assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3)));
39 }
40
41 static void test_uint64_compare_func(void) {
42 const uint64_t a = 0x100, b = 0x101;
43
44 assert_se(uint64_compare_func(&a, &a) == 0);
45 assert_se(uint64_compare_func(&a, &b) == -1);
46 assert_se(uint64_compare_func(&b, &a) == 1);
47 }
48
49 static void test_trivial_compare_func(void) {
50 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
51 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
52 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
53 }
54
55 static void test_string_compare_func(void) {
56 assert_se(string_compare_func("fred", "wilma") != 0);
57 assert_se(string_compare_func("fred", "fred") == 0);
58 }
59
60 int main(int argc, const char *argv[]) {
61 test_hashmap_funcs();
62 test_ordered_hashmap_funcs();
63
64 test_ordered_hashmap_next();
65 test_uint64_compare_func();
66 test_trivial_compare_func();
67 test_string_compare_func();
68 }