]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-strv.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / test / test-strv.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2013 Thomas H.P. Andersen
4 ***/
5
6 #include <string.h>
7
8 #include "alloc-util.h"
9 #include "escape.h"
10 #include "specifier.h"
11 #include "string-util.h"
12 #include "strv.h"
13 #include "util.h"
14
15 static void test_specifier_printf(void) {
16 static const Specifier table[] = {
17 { 'a', specifier_string, (char*) "AAAA" },
18 { 'b', specifier_string, (char*) "BBBB" },
19 { 'm', specifier_machine_id, NULL },
20 { 'B', specifier_boot_id, NULL },
21 { 'H', specifier_host_name, NULL },
22 { 'v', specifier_kernel_release, NULL },
23 {}
24 };
25
26 _cleanup_free_ char *w = NULL;
27 int r;
28
29 r = specifier_printf("xxx a=%a b=%b yyy", table, NULL, &w);
30 assert_se(r >= 0);
31 assert_se(w);
32
33 puts(w);
34 assert_se(streq(w, "xxx a=AAAA b=BBBB yyy"));
35
36 free(w);
37 r = specifier_printf("machine=%m, boot=%B, host=%H, version=%v", table, NULL, &w);
38 assert_se(r >= 0);
39 assert_se(w);
40 puts(w);
41 }
42
43 static void test_str_in_set(void) {
44 assert_se(STR_IN_SET("x", "x", "y", "z"));
45 assert_se(!STR_IN_SET("X", "x", "y", "z"));
46 assert_se(!STR_IN_SET("", "x", "y", "z"));
47 assert_se(STR_IN_SET("x", "w", "x"));
48 }
49
50 static void test_strptr_in_set(void) {
51 assert_se(STRPTR_IN_SET("x", "x", "y", "z"));
52 assert_se(!STRPTR_IN_SET("X", "x", "y", "z"));
53 assert_se(!STRPTR_IN_SET("", "x", "y", "z"));
54 assert_se(STRPTR_IN_SET("x", "w", "x"));
55
56 assert_se(!STRPTR_IN_SET(NULL, "x", "y", "z"));
57 assert_se(!STRPTR_IN_SET(NULL, ""));
58 /* strv cannot contain a null, hence the result below */
59 assert_se(!STRPTR_IN_SET(NULL, NULL));
60 }
61
62 static const char* const input_table_multiple[] = {
63 "one",
64 "two",
65 "three",
66 NULL,
67 };
68
69 static const char* const input_table_one[] = {
70 "one",
71 NULL,
72 };
73
74 static const char* const input_table_none[] = {
75 NULL,
76 };
77
78 static const char* const input_table_two_empties[] = {
79 "",
80 "",
81 NULL,
82 };
83
84 static const char* const input_table_one_empty[] = {
85 "",
86 NULL,
87 };
88
89 static void test_strv_find(void) {
90 assert_se(strv_find((char **)input_table_multiple, "three"));
91 assert_se(!strv_find((char **)input_table_multiple, "four"));
92 }
93
94 static void test_strv_find_prefix(void) {
95 assert_se(strv_find_prefix((char **)input_table_multiple, "o"));
96 assert_se(strv_find_prefix((char **)input_table_multiple, "one"));
97 assert_se(strv_find_prefix((char **)input_table_multiple, ""));
98 assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx"));
99 assert_se(!strv_find_prefix((char **)input_table_multiple, "onee"));
100 }
101
102 static void test_strv_find_startswith(void) {
103 char *r;
104
105 r = strv_find_startswith((char **)input_table_multiple, "o");
106 assert_se(r && streq(r, "ne"));
107
108 r = strv_find_startswith((char **)input_table_multiple, "one");
109 assert_se(r && streq(r, ""));
110
111 r = strv_find_startswith((char **)input_table_multiple, "");
112 assert_se(r && streq(r, "one"));
113
114 assert_se(!strv_find_startswith((char **)input_table_multiple, "xxx"));
115 assert_se(!strv_find_startswith((char **)input_table_multiple, "onee"));
116 }
117
118 static void test_strv_join(void) {
119 _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL, *v = NULL, *w = NULL;
120
121 p = strv_join((char **)input_table_multiple, ", ");
122 assert_se(p);
123 assert_se(streq(p, "one, two, three"));
124
125 q = strv_join((char **)input_table_multiple, ";");
126 assert_se(q);
127 assert_se(streq(q, "one;two;three"));
128
129 r = strv_join((char **)input_table_multiple, NULL);
130 assert_se(r);
131 assert_se(streq(r, "one two three"));
132
133 s = strv_join((char **)input_table_one, ", ");
134 assert_se(s);
135 assert_se(streq(s, "one"));
136
137 t = strv_join((char **)input_table_none, ", ");
138 assert_se(t);
139 assert_se(streq(t, ""));
140
141 v = strv_join((char **)input_table_two_empties, ", ");
142 assert_se(v);
143 assert_se(streq(v, ", "));
144
145 w = strv_join((char **)input_table_one_empty, ", ");
146 assert_se(w);
147 assert_se(streq(w, ""));
148 }
149
150 static void test_strv_unquote(const char *quoted, char **list) {
151 _cleanup_strv_free_ char **s;
152 _cleanup_free_ char *j;
153 unsigned i = 0;
154 char **t;
155 int r;
156
157 r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES);
158 assert_se(r == (int) strv_length(list));
159 assert_se(s);
160 j = strv_join(s, " | ");
161 assert_se(j);
162 puts(j);
163
164 STRV_FOREACH(t, s)
165 assert_se(streq(list[i++], *t));
166
167 assert_se(list[i] == NULL);
168 }
169
170 static void test_invalid_unquote(const char *quoted) {
171 char **s = NULL;
172 int r;
173
174 r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES);
175 assert_se(s == NULL);
176 assert_se(r == -EINVAL);
177 }
178
179 static void test_strv_split(void) {
180 char **s;
181 unsigned i = 0;
182 _cleanup_strv_free_ char **l = NULL;
183 const char str[] = "one,two,three";
184
185 l = strv_split(str, ",");
186
187 assert_se(l);
188
189 STRV_FOREACH(s, l) {
190 assert_se(streq(*s, input_table_multiple[i++]));
191 }
192 }
193
194 static void test_strv_split_extract(void) {
195 _cleanup_strv_free_ char **l = NULL;
196 const char *str = ":foo\\:bar::waldo:";
197 int r;
198
199 r = strv_split_extract(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
200 assert_se(r == (int) strv_length(l));
201 assert_se(streq_ptr(l[0], ""));
202 assert_se(streq_ptr(l[1], "foo:bar"));
203 assert_se(streq_ptr(l[2], ""));
204 assert_se(streq_ptr(l[3], "waldo"));
205 assert_se(streq_ptr(l[4], ""));
206 assert_se(streq_ptr(l[5], NULL));
207 }
208
209 static void test_strv_split_newlines(void) {
210 unsigned i = 0;
211 char **s;
212 _cleanup_strv_free_ char **l = NULL;
213 const char str[] = "one\ntwo\nthree";
214
215 l = strv_split_newlines(str);
216
217 assert_se(l);
218
219 STRV_FOREACH(s, l) {
220 assert_se(streq(*s, input_table_multiple[i++]));
221 }
222 }
223
224 static void test_strv_split_nulstr(void) {
225 _cleanup_strv_free_ char **l = NULL;
226 const char nulstr[] = "str0\0str1\0str2\0str3\0";
227
228 l = strv_split_nulstr (nulstr);
229 assert_se(l);
230
231 assert_se(streq(l[0], "str0"));
232 assert_se(streq(l[1], "str1"));
233 assert_se(streq(l[2], "str2"));
234 assert_se(streq(l[3], "str3"));
235 }
236
237 static void test_strv_parse_nulstr(void) {
238 _cleanup_strv_free_ char **l = NULL;
239 const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
240
241 l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1);
242 assert_se(l);
243 puts("Parse nulstr:");
244 strv_print(l);
245
246 assert_se(streq(l[0], "fuck"));
247 assert_se(streq(l[1], "fuck2"));
248 assert_se(streq(l[2], "fuck3"));
249 assert_se(streq(l[3], ""));
250 assert_se(streq(l[4], "fuck5"));
251 assert_se(streq(l[5], ""));
252 assert_se(streq(l[6], "xxx"));
253 }
254
255 static void test_strv_overlap(void) {
256 const char * const input_table[] = {
257 "one",
258 "two",
259 "three",
260 NULL
261 };
262 const char * const input_table_overlap[] = {
263 "two",
264 NULL
265 };
266 const char * const input_table_unique[] = {
267 "four",
268 "five",
269 "six",
270 NULL
271 };
272
273 assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap));
274 assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique));
275 }
276
277 static void test_strv_sort(void) {
278 const char* input_table[] = {
279 "durian",
280 "apple",
281 "citrus",
282 "CAPITAL LETTERS FIRST",
283 "banana",
284 NULL
285 };
286
287 strv_sort((char **)input_table);
288
289 assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST"));
290 assert_se(streq(input_table[1], "apple"));
291 assert_se(streq(input_table[2], "banana"));
292 assert_se(streq(input_table[3], "citrus"));
293 assert_se(streq(input_table[4], "durian"));
294 }
295
296 static void test_strv_extend_strv_concat(void) {
297 _cleanup_strv_free_ char **a = NULL, **b = NULL;
298
299 a = strv_new("without", "suffix", NULL);
300 b = strv_new("with", "suffix", NULL);
301 assert_se(a);
302 assert_se(b);
303
304 assert_se(strv_extend_strv_concat(&a, b, "_suffix") >= 0);
305
306 assert_se(streq(a[0], "without"));
307 assert_se(streq(a[1], "suffix"));
308 assert_se(streq(a[2], "with_suffix"));
309 assert_se(streq(a[3], "suffix_suffix"));
310 }
311
312 static void test_strv_extend_strv(void) {
313 _cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL;
314
315 a = strv_new("abc", "def", "ghi", NULL);
316 b = strv_new("jkl", "mno", "abc", "pqr", NULL);
317 assert_se(a);
318 assert_se(b);
319
320 assert_se(strv_extend_strv(&a, b, true) == 3);
321
322 assert_se(streq(a[0], "abc"));
323 assert_se(streq(a[1], "def"));
324 assert_se(streq(a[2], "ghi"));
325 assert_se(streq(a[3], "jkl"));
326 assert_se(streq(a[4], "mno"));
327 assert_se(streq(a[5], "pqr"));
328 assert_se(strv_length(a) == 6);
329
330 assert_se(strv_extend_strv(&n, b, false) >= 0);
331 assert_se(streq(n[0], "jkl"));
332 assert_se(streq(n[1], "mno"));
333 assert_se(streq(n[2], "abc"));
334 assert_se(streq(n[3], "pqr"));
335 assert_se(strv_length(n) == 4);
336 }
337
338 static void test_strv_extend(void) {
339 _cleanup_strv_free_ char **a = NULL, **b = NULL;
340
341 a = strv_new("test", "test1", NULL);
342 assert_se(a);
343 assert_se(strv_extend(&a, "test2") >= 0);
344 assert_se(strv_extend(&b, "test3") >= 0);
345
346 assert_se(streq(a[0], "test"));
347 assert_se(streq(a[1], "test1"));
348 assert_se(streq(a[2], "test2"));
349 assert_se(streq(b[0], "test3"));
350 }
351
352 static void test_strv_extendf(void) {
353 _cleanup_strv_free_ char **a = NULL, **b = NULL;
354
355 a = strv_new("test", "test1", NULL);
356 assert_se(a);
357 assert_se(strv_extendf(&a, "test2 %s %d %s", "foo", 128, "bar") >= 0);
358 assert_se(strv_extendf(&b, "test3 %s %s %d", "bar", "foo", 128) >= 0);
359
360 assert_se(streq(a[0], "test"));
361 assert_se(streq(a[1], "test1"));
362 assert_se(streq(a[2], "test2 foo 128 bar"));
363 assert_se(streq(b[0], "test3 bar foo 128"));
364 }
365
366 static void test_strv_foreach(void) {
367 _cleanup_strv_free_ char **a;
368 unsigned i = 0;
369 char **check;
370
371 a = strv_new("one", "two", "three", NULL);
372
373 assert_se(a);
374
375 STRV_FOREACH(check, a) {
376 assert_se(streq(*check, input_table_multiple[i++]));
377 }
378 }
379
380 static void test_strv_foreach_backwards(void) {
381 _cleanup_strv_free_ char **a;
382 unsigned i = 2;
383 char **check;
384
385 a = strv_new("one", "two", "three", NULL);
386
387 assert_se(a);
388
389 STRV_FOREACH_BACKWARDS(check, a)
390 assert_se(streq_ptr(*check, input_table_multiple[i--]));
391
392 STRV_FOREACH_BACKWARDS(check, (char**) NULL)
393 assert_not_reached("Let's see that we check empty strv right, too.");
394
395 STRV_FOREACH_BACKWARDS(check, (char**) { NULL })
396 assert_not_reached("Let's see that we check empty strv right, too.");
397 }
398
399 static void test_strv_foreach_pair(void) {
400 _cleanup_strv_free_ char **a = NULL;
401 char **x, **y;
402
403 a = strv_new("pair_one", "pair_one",
404 "pair_two", "pair_two",
405 "pair_three", "pair_three",
406 NULL);
407
408 STRV_FOREACH_PAIR(x, y, a) {
409 assert_se(streq(*x, *y));
410 }
411 }
412
413 static void test_strv_from_stdarg_alloca_one(char **l, const char *first, ...) {
414 char **j;
415 unsigned i;
416
417 j = strv_from_stdarg_alloca(first);
418
419 for (i = 0;; i++) {
420 assert_se(streq_ptr(l[i], j[i]));
421
422 if (!l[i])
423 break;
424 }
425 }
426
427 static void test_strv_from_stdarg_alloca(void) {
428 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL);
429 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL);
430 test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL);
431 }
432
433 static void test_strv_insert(void) {
434 _cleanup_strv_free_ char **a = NULL;
435
436 assert_se(strv_insert(&a, 0, strdup("first")) == 0);
437 assert_se(streq(a[0], "first"));
438 assert_se(!a[1]);
439
440 assert_se(strv_insert(&a, 0, NULL) == 0);
441 assert_se(streq(a[0], "first"));
442 assert_se(!a[1]);
443
444 assert_se(strv_insert(&a, 1, strdup("two")) == 0);
445 assert_se(streq(a[0], "first"));
446 assert_se(streq(a[1], "two"));
447 assert_se(!a[2]);
448
449 assert_se(strv_insert(&a, 4, strdup("tri")) == 0);
450 assert_se(streq(a[0], "first"));
451 assert_se(streq(a[1], "two"));
452 assert_se(streq(a[2], "tri"));
453 assert_se(!a[3]);
454
455 assert_se(strv_insert(&a, 1, strdup("duo")) == 0);
456 assert_se(streq(a[0], "first"));
457 assert_se(streq(a[1], "duo"));
458 assert_se(streq(a[2], "two"));
459 assert_se(streq(a[3], "tri"));
460 assert_se(!a[4]);
461 }
462
463 static void test_strv_push_prepend(void) {
464 _cleanup_strv_free_ char **a = NULL;
465
466 a = strv_new("foo", "bar", "three", NULL);
467
468 assert_se(strv_push_prepend(&a, strdup("first")) >= 0);
469 assert_se(streq(a[0], "first"));
470 assert_se(streq(a[1], "foo"));
471 assert_se(streq(a[2], "bar"));
472 assert_se(streq(a[3], "three"));
473 assert_se(!a[4]);
474
475 assert_se(strv_consume_prepend(&a, strdup("first2")) >= 0);
476 assert_se(streq(a[0], "first2"));
477 assert_se(streq(a[1], "first"));
478 assert_se(streq(a[2], "foo"));
479 assert_se(streq(a[3], "bar"));
480 assert_se(streq(a[4], "three"));
481 assert_se(!a[5]);
482 }
483
484 static void test_strv_push(void) {
485 _cleanup_strv_free_ char **a = NULL;
486 char *i, *j;
487
488 assert_se(i = strdup("foo"));
489 assert_se(strv_push(&a, i) >= 0);
490
491 assert_se(i = strdup("a"));
492 assert_se(j = strdup("b"));
493 assert_se(strv_push_pair(&a, i, j) >= 0);
494
495 assert_se(streq_ptr(a[0], "foo"));
496 assert_se(streq_ptr(a[1], "a"));
497 assert_se(streq_ptr(a[2], "b"));
498 assert_se(streq_ptr(a[3], NULL));
499 }
500
501 static void test_strv_equal(void) {
502 _cleanup_strv_free_ char **a = NULL;
503 _cleanup_strv_free_ char **b = NULL;
504 _cleanup_strv_free_ char **c = NULL;
505
506 a = strv_new("one", "two", "three", NULL);
507 assert_se(a);
508 b = strv_new("one", "two", "three", NULL);
509 assert_se(a);
510 c = strv_new("one", "two", "three", "four", NULL);
511 assert_se(a);
512
513 assert_se(strv_equal(a, a));
514 assert_se(strv_equal(a, b));
515 assert_se(strv_equal(NULL, NULL));
516
517 assert_se(!strv_equal(a, c));
518 assert_se(!strv_equal(b, c));
519 assert_se(!strv_equal(b, NULL));
520 }
521
522 static void test_strv_is_uniq(void) {
523 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
524
525 a = strv_new(NULL, NULL);
526 assert_se(a);
527 assert_se(strv_is_uniq(a));
528
529 b = strv_new("foo", NULL);
530 assert_se(b);
531 assert_se(strv_is_uniq(b));
532
533 c = strv_new("foo", "bar", NULL);
534 assert_se(c);
535 assert_se(strv_is_uniq(c));
536
537 d = strv_new("foo", "bar", "waldo", "bar", "piep", NULL);
538 assert_se(d);
539 assert_se(!strv_is_uniq(d));
540 }
541
542 static void test_strv_reverse(void) {
543 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
544
545 a = strv_new(NULL, NULL);
546 assert_se(a);
547
548 strv_reverse(a);
549 assert_se(strv_isempty(a));
550
551 b = strv_new("foo", NULL);
552 assert_se(b);
553 strv_reverse(b);
554 assert_se(streq_ptr(b[0], "foo"));
555 assert_se(streq_ptr(b[1], NULL));
556
557 c = strv_new("foo", "bar", NULL);
558 assert_se(c);
559 strv_reverse(c);
560 assert_se(streq_ptr(c[0], "bar"));
561 assert_se(streq_ptr(c[1], "foo"));
562 assert_se(streq_ptr(c[2], NULL));
563
564 d = strv_new("foo", "bar", "waldo", NULL);
565 assert_se(d);
566 strv_reverse(d);
567 assert_se(streq_ptr(d[0], "waldo"));
568 assert_se(streq_ptr(d[1], "bar"));
569 assert_se(streq_ptr(d[2], "foo"));
570 assert_se(streq_ptr(d[3], NULL));
571 }
572
573 static void test_strv_shell_escape(void) {
574 _cleanup_strv_free_ char **v = NULL;
575
576 v = strv_new("foo:bar", "bar,baz", "wal\\do", NULL);
577 assert_se(v);
578 assert_se(strv_shell_escape(v, ",:"));
579 assert_se(streq_ptr(v[0], "foo\\:bar"));
580 assert_se(streq_ptr(v[1], "bar\\,baz"));
581 assert_se(streq_ptr(v[2], "wal\\\\do"));
582 assert_se(streq_ptr(v[3], NULL));
583 }
584
585 static void test_strv_skip_one(char **a, size_t n, char **b) {
586 a = strv_skip(a, n);
587 assert_se(strv_equal(a, b));
588 }
589
590 static void test_strv_skip(void) {
591 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
592 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
593 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
594 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL));
595 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL));
596 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL));
597
598 test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
599 test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL));
600 test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL));
601
602 test_strv_skip_one(STRV_MAKE(NULL), 0, STRV_MAKE(NULL));
603 test_strv_skip_one(STRV_MAKE(NULL), 1, STRV_MAKE(NULL));
604 test_strv_skip_one(STRV_MAKE(NULL), 55, STRV_MAKE(NULL));
605 }
606
607 static void test_strv_extend_n(void) {
608 _cleanup_strv_free_ char **v = NULL;
609
610 v = strv_new("foo", "bar", NULL);
611 assert_se(v);
612
613 assert_se(strv_extend_n(&v, "waldo", 3) >= 0);
614 assert_se(strv_extend_n(&v, "piep", 2) >= 0);
615
616 assert_se(streq(v[0], "foo"));
617 assert_se(streq(v[1], "bar"));
618 assert_se(streq(v[2], "waldo"));
619 assert_se(streq(v[3], "waldo"));
620 assert_se(streq(v[4], "waldo"));
621 assert_se(streq(v[5], "piep"));
622 assert_se(streq(v[6], "piep"));
623 assert_se(v[7] == NULL);
624
625 v = strv_free(v);
626
627 assert_se(strv_extend_n(&v, "foo", 1) >= 0);
628 assert_se(strv_extend_n(&v, "bar", 0) >= 0);
629
630 assert_se(streq(v[0], "foo"));
631 assert_se(v[1] == NULL);
632 }
633
634 static void test_strv_make_nulstr_one(char **l) {
635 _cleanup_free_ char *b = NULL, *c = NULL;
636 _cleanup_strv_free_ char **q = NULL;
637 const char *s = NULL;
638 size_t n, m;
639 unsigned i = 0;
640
641 assert_se(strv_make_nulstr(l, &b, &n) >= 0);
642 assert_se(q = strv_parse_nulstr(b, n));
643 assert_se(strv_equal(l, q));
644
645 assert_se(strv_make_nulstr(q, &c, &m) >= 0);
646 assert_se(m == n);
647 assert_se(memcmp(b, c, m) == 0);
648
649 NULSTR_FOREACH(s, b)
650 assert_se(streq(s, l[i++]));
651 assert_se(i == strv_length(l));
652 }
653
654 static void test_strv_make_nulstr(void) {
655 test_strv_make_nulstr_one(NULL);
656 test_strv_make_nulstr_one(STRV_MAKE(NULL));
657 test_strv_make_nulstr_one(STRV_MAKE("foo"));
658 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar"));
659 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
660 }
661
662 static void test_strv_free_free(void) {
663 char ***t;
664
665 assert_se(t = new(char**, 3));
666 assert_se(t[0] = strv_new("a", "b", NULL));
667 assert_se(t[1] = strv_new("c", "d", "e", NULL));
668 t[2] = NULL;
669
670 t = strv_free_free(t);
671 }
672
673 static void test_foreach_string(void) {
674 const char * const t[] = {
675 "foo",
676 "bar",
677 "waldo",
678 NULL
679 };
680 const char *x;
681 unsigned i = 0;
682
683 FOREACH_STRING(x, "foo", "bar", "waldo")
684 assert_se(streq_ptr(t[i++], x));
685
686 assert_se(i == 3);
687
688 FOREACH_STRING(x, "zzz")
689 assert_se(streq(x, "zzz"));
690 }
691
692 static void test_strv_fnmatch(void) {
693 _cleanup_strv_free_ char **v = NULL;
694
695 assert_se(!strv_fnmatch(STRV_MAKE_EMPTY, "a", 0));
696
697 v = strv_new("*\\*", NULL);
698 assert_se(!strv_fnmatch(v, "\\", 0));
699 assert_se(strv_fnmatch(v, "\\", FNM_NOESCAPE));
700 }
701
702 int main(int argc, char *argv[]) {
703 test_specifier_printf();
704 test_str_in_set();
705 test_strptr_in_set();
706 test_strv_foreach();
707 test_strv_foreach_backwards();
708 test_strv_foreach_pair();
709 test_strv_find();
710 test_strv_find_prefix();
711 test_strv_find_startswith();
712 test_strv_join();
713
714 test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
715 test_strv_unquote("", STRV_MAKE_EMPTY);
716 test_strv_unquote(" ", STRV_MAKE_EMPTY);
717 test_strv_unquote(" ", STRV_MAKE_EMPTY);
718 test_strv_unquote(" x", STRV_MAKE("x"));
719 test_strv_unquote("x ", STRV_MAKE("x"));
720 test_strv_unquote(" x ", STRV_MAKE("x"));
721 test_strv_unquote(" \"x\" ", STRV_MAKE("x"));
722 test_strv_unquote(" 'x' ", STRV_MAKE("x"));
723 test_strv_unquote(" 'x\"' ", STRV_MAKE("x\""));
724 test_strv_unquote(" \"x'\" ", STRV_MAKE("x'"));
725 test_strv_unquote("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
726
727 /* trailing backslashes */
728 test_strv_unquote(" x\\\\", STRV_MAKE("x\\"));
729 test_invalid_unquote(" x\\");
730
731 test_invalid_unquote("a --b='c \"d e\"''");
732 test_invalid_unquote("a --b='c \"d e\" '\"");
733 test_invalid_unquote("a --b='c \"d e\"garbage");
734 test_invalid_unquote("'");
735 test_invalid_unquote("\"");
736 test_invalid_unquote("'x'y'g");
737
738 test_strv_split();
739 test_strv_split_extract();
740 test_strv_split_newlines();
741 test_strv_split_nulstr();
742 test_strv_parse_nulstr();
743 test_strv_overlap();
744 test_strv_sort();
745 test_strv_extend_strv();
746 test_strv_extend_strv_concat();
747 test_strv_extend();
748 test_strv_extendf();
749 test_strv_from_stdarg_alloca();
750 test_strv_insert();
751 test_strv_push_prepend();
752 test_strv_push();
753 test_strv_equal();
754 test_strv_is_uniq();
755 test_strv_reverse();
756 test_strv_shell_escape();
757 test_strv_skip();
758 test_strv_extend_n();
759 test_strv_make_nulstr();
760 test_strv_free_free();
761
762 test_foreach_string();
763 test_strv_fnmatch();
764
765 return 0;
766 }