]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-set.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-set.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
647f6824
ZJS
2/***
3 This file is part of systemd
4
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
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
647f6824
ZJS
21#include "set.h"
22
23static void test_set_steal_first(void) {
24 _cleanup_set_free_ Set *m = NULL;
25 int seen[3] = {};
26 char *val;
27
28 m = set_new(&string_hash_ops);
29 assert_se(m);
30
31 assert_se(set_put(m, (void*) "1") == 1);
32 assert_se(set_put(m, (void*) "22") == 1);
33 assert_se(set_put(m, (void*) "333") == 1);
34
35 while ((val = set_steal_first(m)))
36 seen[strlen(val) - 1]++;
37
bdf7026e 38 assert_se(seen[0] == 1 && seen[1] == 1 && seen[2] == 1);
647f6824
ZJS
39
40 assert_se(set_isempty(m));
41}
42
756c09e6
RC
43static void test_set_put(void) {
44 _cleanup_set_free_ Set *m = NULL;
45
46 m = set_new(&string_hash_ops);
47 assert_se(m);
48
49 assert_se(set_put(m, (void*) "1") == 1);
50 assert_se(set_put(m, (void*) "22") == 1);
51 assert_se(set_put(m, (void*) "333") == 1);
52 assert_se(set_put(m, (void*) "333") == 0);
53 assert_se(set_remove(m, (void*) "333"));
54 assert_se(set_put(m, (void*) "333") == 1);
55 assert_se(set_put(m, (void*) "333") == 0);
56 assert_se(set_put(m, (void*) "22") == 0);
57}
58
ca543871
LP
59static void test_set_make(void) {
60 _cleanup_set_free_ Set *s = NULL;
61
62 assert_se(set_make(&s, NULL, UINT_TO_PTR(4), UINT_TO_PTR(6), UINT_TO_PTR(8), NULL) == 0);
63 assert_se(set_size(s) == 3);
64 assert_se(!set_contains(s, UINT_TO_PTR(0)));
65 assert_se(!set_contains(s, UINT_TO_PTR(1)));
66 assert_se(!set_contains(s, UINT_TO_PTR(2)));
67 assert_se(!set_contains(s, UINT_TO_PTR(3)));
68 assert_se(set_contains(s, UINT_TO_PTR(4)));
69 assert_se(!set_contains(s, UINT_TO_PTR(5)));
70 assert_se(set_contains(s, UINT_TO_PTR(6)));
71 assert_se(!set_contains(s, UINT_TO_PTR(7)));
72 assert_se(set_contains(s, UINT_TO_PTR(8)));
73 assert_se(!set_contains(s, UINT_TO_PTR(9)));
74 s = set_free(s);
75
76 assert_se(set_make(&s, NULL, NULL) == 0);
77 assert_se(set_size(s) == 0);
78 assert_se(!set_contains(s, UINT_TO_PTR(0)));
79 assert_se(!set_contains(s, UINT_TO_PTR(4)));
80 assert_se(!set_contains(s, UINT_TO_PTR(6)));
81 assert_se(!set_contains(s, UINT_TO_PTR(8)));
82 s = set_free(s);
83
84 assert_se(set_make(&s, NULL, UINT_TO_PTR(3), NULL) == 0);
85 assert_se(set_size(s) == 1);
86 assert_se(!set_contains(s, UINT_TO_PTR(0)));
87 assert_se(!set_contains(s, UINT_TO_PTR(1)));
88 assert_se(!set_contains(s, UINT_TO_PTR(2)));
89 assert_se(set_contains(s, UINT_TO_PTR(3)));
90 assert_se(!set_contains(s, UINT_TO_PTR(4)));
91
92 assert_se(set_make(&s, NULL, UINT_TO_PTR(2), UINT_TO_PTR(5), NULL) == 0);
93 assert_se(set_size(s) == 2);
94 assert_se(!set_contains(s, UINT_TO_PTR(0)));
95 assert_se(!set_contains(s, UINT_TO_PTR(1)));
96 assert_se(set_contains(s, UINT_TO_PTR(2)));
97 assert_se(!set_contains(s, UINT_TO_PTR(3)));
98 assert_se(!set_contains(s, UINT_TO_PTR(4)));
99 assert_se(set_contains(s, UINT_TO_PTR(5)));
100 assert_se(!set_contains(s, UINT_TO_PTR(6)));
101}
102
647f6824
ZJS
103int main(int argc, const char *argv[]) {
104 test_set_steal_first();
756c09e6 105 test_set_put();
ca543871 106 test_set_make();
647f6824
ZJS
107
108 return 0;
109}