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