]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9341a4a1 DB |
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 | ||
9341a4a1 | 21 | #include "hashmap.h" |
cf0fbc49 | 22 | #include "util.h" |
9341a4a1 | 23 | |
32a4456c MS |
24 | void test_hashmap_funcs(void); |
25 | void test_ordered_hashmap_funcs(void); | |
9341a4a1 | 26 | |
32a4456c | 27 | static void test_ordered_hashmap_next(void) { |
8f8a5213 MS |
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))); | |
d06b3a9d RC |
39 | } |
40 | ||
224b0e7a ZJS |
41 | typedef struct Item { |
42 | int seen; | |
43 | } Item; | |
44 | static void item_seen(Item *item) { | |
45 | item->seen++; | |
46 | } | |
47 | ||
48 | static void test_hashmap_free_with_destructor(void) { | |
49 | Hashmap *m; | |
50 | struct Item items[4] = {}; | |
51 | unsigned i; | |
52 | ||
53 | assert_se(m = hashmap_new(NULL)); | |
54 | for (i = 0; i < ELEMENTSOF(items) - 1; i++) | |
55 | assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1); | |
56 | ||
57 | m = hashmap_free_with_destructor(m, item_seen); | |
58 | assert_se(items[0].seen == 1); | |
59 | assert_se(items[1].seen == 1); | |
60 | assert_se(items[2].seen == 1); | |
61 | assert_se(items[3].seen == 0); | |
62 | } | |
63 | ||
9341a4a1 | 64 | static void test_uint64_compare_func(void) { |
8097ab4f ZJS |
65 | const uint64_t a = 0x100, b = 0x101; |
66 | ||
67 | assert_se(uint64_compare_func(&a, &a) == 0); | |
68 | assert_se(uint64_compare_func(&a, &b) == -1); | |
69 | assert_se(uint64_compare_func(&b, &a) == 1); | |
9341a4a1 DB |
70 | } |
71 | ||
72 | static void test_trivial_compare_func(void) { | |
73 | assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0); | |
74 | assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1); | |
75 | assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1); | |
76 | } | |
77 | ||
78 | static void test_string_compare_func(void) { | |
4b3eff61 | 79 | assert_se(string_compare_func("fred", "wilma") != 0); |
9341a4a1 DB |
80 | assert_se(string_compare_func("fred", "fred") == 0); |
81 | } | |
82 | ||
45fa9e29 | 83 | int main(int argc, const char *argv[]) { |
32a4456c MS |
84 | test_hashmap_funcs(); |
85 | test_ordered_hashmap_funcs(); | |
86 | ||
87 | test_ordered_hashmap_next(); | |
224b0e7a | 88 | test_hashmap_free_with_destructor(); |
9341a4a1 DB |
89 | test_uint64_compare_func(); |
90 | test_trivial_compare_func(); | |
91 | test_string_compare_func(); | |
92 | } |