]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-hashmap.c
tree-wide: add missing includes
[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 26static void test_ordered_hashmap_next(void) {
8f8a5213
MS
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)));
d06b3a9d
RC
38}
39
9341a4a1 40static void test_uint64_compare_func(void) {
8097ab4f
ZJS
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);
9341a4a1
DB
46}
47
48static 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
54static void test_string_compare_func(void) {
4b3eff61 55 assert_se(string_compare_func("fred", "wilma") != 0);
9341a4a1
DB
56 assert_se(string_compare_func("fred", "fred") == 0);
57}
58
45fa9e29 59int main(int argc, const char *argv[]) {
32a4456c
MS
60 test_hashmap_funcs();
61 test_ordered_hashmap_funcs();
62
63 test_ordered_hashmap_next();
9341a4a1
DB
64 test_uint64_compare_func();
65 test_trivial_compare_func();
66 test_string_compare_func();
67}