]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-set.c
Merge pull request #7492 from keszybz/coverity-fixes
[thirdparty/systemd.git] / src / test / test-set.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
21 #include "set.h"
22
23 static 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
38 assert_se(seen[0] == 1 && seen[1] == 1 && seen[2] == 1);
39
40 assert_se(set_isempty(m));
41 }
42
43 typedef struct Item {
44 int seen;
45 } Item;
46 static void item_seen(Item *item) {
47 item->seen++;
48 }
49
50 static void test_set_free_with_destructor(void) {
51 Set *m;
52 struct Item items[4] = {};
53 unsigned i;
54
55 assert_se(m = set_new(NULL));
56 for (i = 0; i < ELEMENTSOF(items) - 1; i++)
57 assert_se(set_put(m, items + i) == 1);
58
59 m = set_free_with_destructor(m, item_seen);
60 assert_se(items[0].seen == 1);
61 assert_se(items[1].seen == 1);
62 assert_se(items[2].seen == 1);
63 assert_se(items[3].seen == 0);
64 }
65
66 static void test_set_put(void) {
67 _cleanup_set_free_ Set *m = NULL;
68
69 m = set_new(&string_hash_ops);
70 assert_se(m);
71
72 assert_se(set_put(m, (void*) "1") == 1);
73 assert_se(set_put(m, (void*) "22") == 1);
74 assert_se(set_put(m, (void*) "333") == 1);
75 assert_se(set_put(m, (void*) "333") == 0);
76 assert_se(set_remove(m, (void*) "333"));
77 assert_se(set_put(m, (void*) "333") == 1);
78 assert_se(set_put(m, (void*) "333") == 0);
79 assert_se(set_put(m, (void*) "22") == 0);
80 }
81
82 static void test_set_make(void) {
83 _cleanup_set_free_ Set *s = NULL;
84
85 assert_se(set_make(&s, NULL, UINT_TO_PTR(4), UINT_TO_PTR(6), UINT_TO_PTR(8), NULL) == 0);
86 assert_se(set_size(s) == 3);
87 assert_se(!set_contains(s, UINT_TO_PTR(0)));
88 assert_se(!set_contains(s, UINT_TO_PTR(1)));
89 assert_se(!set_contains(s, UINT_TO_PTR(2)));
90 assert_se(!set_contains(s, UINT_TO_PTR(3)));
91 assert_se(set_contains(s, UINT_TO_PTR(4)));
92 assert_se(!set_contains(s, UINT_TO_PTR(5)));
93 assert_se(set_contains(s, UINT_TO_PTR(6)));
94 assert_se(!set_contains(s, UINT_TO_PTR(7)));
95 assert_se(set_contains(s, UINT_TO_PTR(8)));
96 assert_se(!set_contains(s, UINT_TO_PTR(9)));
97 s = set_free(s);
98
99 assert_se(set_make(&s, NULL, NULL) == 0);
100 assert_se(set_size(s) == 0);
101 assert_se(!set_contains(s, UINT_TO_PTR(0)));
102 assert_se(!set_contains(s, UINT_TO_PTR(4)));
103 assert_se(!set_contains(s, UINT_TO_PTR(6)));
104 assert_se(!set_contains(s, UINT_TO_PTR(8)));
105 s = set_free(s);
106
107 assert_se(set_make(&s, NULL, UINT_TO_PTR(3), NULL) == 0);
108 assert_se(set_size(s) == 1);
109 assert_se(!set_contains(s, UINT_TO_PTR(0)));
110 assert_se(!set_contains(s, UINT_TO_PTR(1)));
111 assert_se(!set_contains(s, UINT_TO_PTR(2)));
112 assert_se(set_contains(s, UINT_TO_PTR(3)));
113 assert_se(!set_contains(s, UINT_TO_PTR(4)));
114
115 assert_se(set_make(&s, NULL, UINT_TO_PTR(2), UINT_TO_PTR(5), NULL) == 0);
116 assert_se(set_size(s) == 2);
117 assert_se(!set_contains(s, UINT_TO_PTR(0)));
118 assert_se(!set_contains(s, UINT_TO_PTR(1)));
119 assert_se(set_contains(s, UINT_TO_PTR(2)));
120 assert_se(!set_contains(s, UINT_TO_PTR(3)));
121 assert_se(!set_contains(s, UINT_TO_PTR(4)));
122 assert_se(set_contains(s, UINT_TO_PTR(5)));
123 assert_se(!set_contains(s, UINT_TO_PTR(6)));
124 }
125
126 int main(int argc, const char *argv[]) {
127 test_set_steal_first();
128 test_set_free_with_destructor();
129 test_set_put();
130 test_set_make();
131
132 return 0;
133 }