Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <stdio.h>
4 :
5 : #include "ordered-set.h"
6 : #include "string-util.h"
7 : #include "strv.h"
8 :
9 1 : static void test_set_steal_first(void) {
10 1 : _cleanup_ordered_set_free_ OrderedSet *m = NULL;
11 1 : int seen[3] = {};
12 : char *val;
13 :
14 1 : m = ordered_set_new(&string_hash_ops);
15 1 : assert_se(m);
16 :
17 1 : assert_se(ordered_set_put(m, (void*) "1") == 1);
18 1 : assert_se(ordered_set_put(m, (void*) "22") == 1);
19 1 : assert_se(ordered_set_put(m, (void*) "333") == 1);
20 :
21 1 : ordered_set_print(stdout, "SET=", m);
22 :
23 4 : while ((val = ordered_set_steal_first(m)))
24 3 : seen[strlen(val) - 1]++;
25 :
26 1 : assert_se(seen[0] == 1 && seen[1] == 1 && seen[2] == 1);
27 :
28 1 : assert_se(ordered_set_isempty(m));
29 :
30 1 : ordered_set_print(stdout, "SET=", m);
31 1 : }
32 :
33 : typedef struct Item {
34 : int seen;
35 : } Item;
36 3 : static void item_seen(Item *item) {
37 3 : item->seen++;
38 3 : }
39 :
40 3 : DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_hash_ops, void, trivial_hash_func, trivial_compare_func, Item, item_seen);
41 :
42 1 : static void test_set_free_with_hash_ops(void) {
43 : OrderedSet *m;
44 1 : struct Item items[4] = {};
45 : unsigned i;
46 :
47 1 : assert_se(m = ordered_set_new(&item_hash_ops));
48 4 : for (i = 0; i < ELEMENTSOF(items) - 1; i++)
49 3 : assert_se(ordered_set_put(m, items + i) == 1);
50 :
51 1 : m = ordered_set_free(m);
52 1 : assert_se(items[0].seen == 1);
53 1 : assert_se(items[1].seen == 1);
54 1 : assert_se(items[2].seen == 1);
55 1 : assert_se(items[3].seen == 0);
56 1 : }
57 :
58 1 : static void test_set_put(void) {
59 1 : _cleanup_ordered_set_free_ OrderedSet *m = NULL;
60 1 : _cleanup_free_ char **t = NULL;
61 :
62 1 : m = ordered_set_new(&string_hash_ops);
63 1 : assert_se(m);
64 :
65 1 : assert_se(ordered_set_put(m, (void*) "1") == 1);
66 1 : assert_se(ordered_set_put(m, (void*) "22") == 1);
67 1 : assert_se(ordered_set_put(m, (void*) "333") == 1);
68 1 : assert_se(ordered_set_put(m, (void*) "333") == 0);
69 1 : assert_se(ordered_set_remove(m, (void*) "333"));
70 1 : assert_se(ordered_set_put(m, (void*) "333") == 1);
71 1 : assert_se(ordered_set_put(m, (void*) "333") == 0);
72 1 : assert_se(ordered_set_put(m, (void*) "22") == 0);
73 :
74 1 : assert_se(t = ordered_set_get_strv(m));
75 1 : assert_se(streq(t[0], "1"));
76 1 : assert_se(streq(t[1], "22"));
77 1 : assert_se(streq(t[2], "333"));
78 1 : assert_se(!t[3]);
79 :
80 1 : ordered_set_print(stdout, "FOO=", m);
81 1 : }
82 :
83 1 : static void test_set_put_string_set(void) {
84 1 : _cleanup_ordered_set_free_free_ OrderedSet *m = NULL;
85 1 : _cleanup_ordered_set_free_ OrderedSet *q = NULL;
86 1 : _cleanup_free_ char **final = NULL; /* "just free" because the strings are in the set */
87 : void *t;
88 :
89 1 : m = ordered_set_new(&string_hash_ops);
90 1 : assert_se(m);
91 :
92 1 : q = ordered_set_new(&string_hash_ops);
93 1 : assert_se(q);
94 :
95 1 : assert_se(t = strdup("1"));
96 1 : assert_se(ordered_set_put(m, t) == 1);
97 1 : assert_se(t = strdup("22"));
98 1 : assert_se(ordered_set_put(m, t) == 1);
99 1 : assert_se(t = strdup("333"));
100 1 : assert_se(ordered_set_put(m, t) == 1);
101 :
102 1 : assert_se(ordered_set_put(q, (void*) "11") == 1);
103 1 : assert_se(ordered_set_put(q, (void*) "22") == 1);
104 1 : assert_se(ordered_set_put(q, (void*) "33") == 1);
105 :
106 1 : assert_se(ordered_set_put_string_set(m, q) == 2);
107 :
108 1 : assert_se(final = ordered_set_get_strv(m));
109 1 : assert_se(strv_equal(final, STRV_MAKE("1", "22", "333", "11", "33")));
110 :
111 1 : ordered_set_print(stdout, "BAR=", m);
112 1 : }
113 :
114 1 : int main(int argc, const char *argv[]) {
115 1 : test_set_steal_first();
116 1 : test_set_free_with_hash_ops();
117 1 : test_set_put();
118 1 : test_set_put_string_set();
119 :
120 1 : return 0;
121 : }
|