]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-strv.c
test-path-util,test-sched-prio: uninitialize manager to appease valgrind
[thirdparty/systemd.git] / src / test / test-strv.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Thomas H.P. Andersen
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <string.h>
24
25 #include "util.h"
26 #include "specifier.h"
27 #include "strv.h"
28
29 static void test_specifier_printf(void) {
30 _cleanup_free_ char *w = NULL;
31
32 const Specifier table[] = {
33 { 'a', specifier_string, (char*) "AAAA" },
34 { 'b', specifier_string, (char*) "BBBB" },
35 { 0, NULL, NULL }
36 };
37
38 w = specifier_printf("xxx a=%a b=%b yyy", table, NULL);
39 puts(w);
40
41 assert_se(w);
42 assert_se(streq(w, "xxx a=AAAA b=BBBB yyy"));
43 }
44
45 static void test_strv_find(void) {
46 const char * const input_table[] = {
47 "one",
48 "two",
49 "three",
50 NULL
51 };
52
53 assert_se(strv_find((char **)input_table, "three"));
54 assert_se(!strv_find((char **)input_table, "four"));
55 }
56
57 static void test_strv_find_prefix(void) {
58 const char * const input_table[] = {
59 "one",
60 "two",
61 "three",
62 NULL
63 };
64
65 assert_se(strv_find_prefix((char **)input_table, "o"));
66 assert_se(strv_find_prefix((char **)input_table, "one"));
67 assert_se(strv_find_prefix((char **)input_table, ""));
68 assert_se(!strv_find_prefix((char **)input_table, "xxx"));
69 assert_se(!strv_find_prefix((char **)input_table, "onee"));
70 }
71
72 static void test_strv_join(void) {
73 _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL;
74
75 const char * const input_table_multiple[] = {
76 "one",
77 "two",
78 "three",
79 NULL
80 };
81 const char * const input_table_one[] = {
82 "one",
83 NULL
84 };
85 const char * const input_table_none[] = {
86 NULL
87 };
88
89 p = strv_join((char **)input_table_multiple, ", ");
90 assert_se(p);
91 assert_se(streq(p, "one, two, three"));
92
93 q = strv_join((char **)input_table_multiple, ";");
94 assert_se(q);
95 assert_se(streq(q, "one;two;three"));
96
97 r = strv_join((char **)input_table_multiple, NULL);
98 assert_se(r);
99 assert_se(streq(r, "one two three"));
100
101 s = strv_join((char **)input_table_one, ", ");
102 assert_se(s);
103 assert_se(streq(s, "one"));
104
105 t = strv_join((char **)input_table_none, ", ");
106 assert_se(t);
107 assert_se(streq(t, ""));
108 }
109
110 static void test_strv_split_nulstr(void) {
111 _cleanup_strv_free_ char **l = NULL;
112 const char nulstr[] = "str0\0str1\0str2\0str3\0";
113
114 l = strv_split_nulstr (nulstr);
115 assert_se(l);
116
117 assert_se(streq(l[0], "str0"));
118 assert_se(streq(l[1], "str1"));
119 assert_se(streq(l[2], "str2"));
120 assert_se(streq(l[3], "str3"));
121 }
122
123 static void test_strv_parse_nulstr(void) {
124 _cleanup_strv_free_ char **l = NULL;
125 const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
126
127 l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1);
128 assert_se(l);
129 puts("Parse nulstr:");
130 strv_print(l);
131
132 assert_se(streq(l[0], "fuck"));
133 assert_se(streq(l[1], "fuck2"));
134 assert_se(streq(l[2], "fuck3"));
135 assert_se(streq(l[3], ""));
136 assert_se(streq(l[4], "fuck5"));
137 assert_se(streq(l[5], ""));
138 assert_se(streq(l[6], "xxx"));
139 }
140
141 static void test_strv_overlap(void) {
142 const char * const input_table[] = {
143 "one",
144 "two",
145 "three",
146 NULL
147 };
148 const char * const input_table_overlap[] = {
149 "two",
150 NULL
151 };
152 const char * const input_table_unique[] = {
153 "four",
154 "five",
155 "six",
156 NULL
157 };
158
159 assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap));
160 assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique));
161 }
162
163 static void test_strv_sort(void) {
164 const char* input_table[] = {
165 "durian",
166 "apple",
167 "citrus",
168 "CAPITAL LETTERS FIRST",
169 "banana",
170 NULL
171 };
172
173 strv_sort((char **)input_table);
174
175 assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST"));
176 assert_se(streq(input_table[1], "apple"));
177 assert_se(streq(input_table[2], "banana"));
178 assert_se(streq(input_table[3], "citrus"));
179 assert_se(streq(input_table[4], "durian"));
180 }
181
182 static void test_strv_merge_concat(void) {
183 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
184
185 a = strv_new("without", "suffix", NULL);
186 b = strv_new("with", "suffix", NULL);
187 assert_se(a);
188 assert_se(b);
189
190 c = strv_merge_concat(a, b, "_suffix");
191 assert_se(c);
192
193 assert_se(streq(c[0], "without"));
194 assert_se(streq(c[1], "suffix"));
195 assert_se(streq(c[2], "with_suffix"));
196 assert_se(streq(c[3], "suffix_suffix"));
197 }
198
199 static void test_strv_merge(void) {
200 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
201
202 a = strv_new("abc", "def", "ghi", NULL);
203 b = strv_new("jkl", "mno", "pqr", NULL);
204 assert_se(a);
205 assert_se(b);
206
207 c = strv_merge(a, b);
208 assert_se(c);
209
210 assert_se(streq(c[0], "abc"));
211 assert_se(streq(c[1], "def"));
212 assert_se(streq(c[2], "ghi"));
213 assert_se(streq(c[3], "jkl"));
214 assert_se(streq(c[4], "mno"));
215 assert_se(streq(c[5], "pqr"));
216
217 assert_se(strv_length(c) == 6);
218 }
219
220 static void test_strv_append(void) {
221 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
222
223 a = strv_new("test", "test1", NULL);
224 assert_se(a);
225 b = strv_append(a, "test2");
226 c = strv_append(NULL, "test3");
227 assert_se(b);
228 assert_se(c);
229
230 assert_se(streq(b[0], "test"));
231 assert_se(streq(b[1], "test1"));
232 assert_se(streq(b[2], "test2"));
233 assert_se(streq(c[0], "test3"));
234 }
235
236 static void test_strv_foreach_pair(void) {
237 _cleanup_strv_free_ char **a = NULL;
238 char **x, **y;
239
240 a = strv_new("pair_one", "pair_one",
241 "pair_two", "pair_two",
242 "pair_three", "pair_three",
243 NULL);
244
245 STRV_FOREACH_PAIR(x, y, a) {
246 assert_se(streq(*x, *y));
247 }
248 }
249
250 int main(int argc, char *argv[]) {
251 test_specifier_printf();
252 test_strv_foreach_pair();
253 test_strv_find();
254 test_strv_find_prefix();
255 test_strv_join();
256 test_strv_split_nulstr();
257 test_strv_parse_nulstr();
258 test_strv_overlap();
259 test_strv_sort();
260 test_strv_merge();
261 test_strv_merge_concat();
262 test_strv_append();
263
264 return 0;
265 }