1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
7 #include "nulstr-util.h"
9 #include "string-util.h"
12 static void test_specifier_printf(void) {
13 static const Specifier table
[] = {
14 { 'a', specifier_string
, (char*) "AAAA" },
15 { 'b', specifier_string
, (char*) "BBBB" },
16 { 'm', specifier_machine_id
, NULL
},
17 { 'B', specifier_boot_id
, NULL
},
18 { 'H', specifier_host_name
, NULL
},
19 { 'v', specifier_kernel_release
, NULL
},
23 _cleanup_free_
char *w
= NULL
;
26 r
= specifier_printf("xxx a=%a b=%b yyy", table
, NULL
, &w
);
31 assert_se(streq(w
, "xxx a=AAAA b=BBBB yyy"));
34 r
= specifier_printf("machine=%m, boot=%B, host=%H, version=%v", table
, NULL
, &w
);
40 static void test_str_in_set(void) {
41 assert_se(STR_IN_SET("x", "x", "y", "z"));
42 assert_se(!STR_IN_SET("X", "x", "y", "z"));
43 assert_se(!STR_IN_SET("", "x", "y", "z"));
44 assert_se(STR_IN_SET("x", "w", "x"));
47 static void test_strptr_in_set(void) {
48 assert_se(STRPTR_IN_SET("x", "x", "y", "z"));
49 assert_se(!STRPTR_IN_SET("X", "x", "y", "z"));
50 assert_se(!STRPTR_IN_SET("", "x", "y", "z"));
51 assert_se(STRPTR_IN_SET("x", "w", "x"));
53 assert_se(!STRPTR_IN_SET(NULL
, "x", "y", "z"));
54 assert_se(!STRPTR_IN_SET(NULL
, ""));
55 /* strv cannot contain a null, hence the result below */
56 assert_se(!STRPTR_IN_SET(NULL
, NULL
));
59 static void test_startswith_set(void) {
60 assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo"));
61 assert_se(!STARTSWITH_SET("foo", "bar"));
63 assert_se(STARTSWITH_SET("abc", "a", "ab", "abc"));
64 assert_se(STARTSWITH_SET("abc", "ax", "ab", "abc"));
65 assert_se(STARTSWITH_SET("abc", "ax", "abx", "abc"));
66 assert_se(!STARTSWITH_SET("abc", "ax", "abx", "abcx"));
68 assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "foo", "zzz"), "bar"));
69 assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "", "zzz"), "foobar"));
70 assert_se(streq_ptr(STARTSWITH_SET("", "hhh", "kkk", "zzz", ""), ""));
73 static const char* const input_table_multiple
[] = {
80 static const char* const input_table_quoted
[] = {
87 static const char* const input_table_one
[] = {
92 static const char* const input_table_none
[] = {
96 static const char* const input_table_two_empties
[] = {
102 static const char* const input_table_one_empty
[] = {
107 static void test_strv_find(void) {
108 assert_se(strv_find((char **)input_table_multiple
, "three"));
109 assert_se(!strv_find((char **)input_table_multiple
, "four"));
112 static void test_strv_find_prefix(void) {
113 assert_se(strv_find_prefix((char **)input_table_multiple
, "o"));
114 assert_se(strv_find_prefix((char **)input_table_multiple
, "one"));
115 assert_se(strv_find_prefix((char **)input_table_multiple
, ""));
116 assert_se(!strv_find_prefix((char **)input_table_multiple
, "xxx"));
117 assert_se(!strv_find_prefix((char **)input_table_multiple
, "onee"));
120 static void test_strv_find_startswith(void) {
123 r
= strv_find_startswith((char **)input_table_multiple
, "o");
124 assert_se(r
&& streq(r
, "ne"));
126 r
= strv_find_startswith((char **)input_table_multiple
, "one");
127 assert_se(r
&& streq(r
, ""));
129 r
= strv_find_startswith((char **)input_table_multiple
, "");
130 assert_se(r
&& streq(r
, "one"));
132 assert_se(!strv_find_startswith((char **)input_table_multiple
, "xxx"));
133 assert_se(!strv_find_startswith((char **)input_table_multiple
, "onee"));
136 static void test_strv_join(void) {
137 _cleanup_free_
char *p
= NULL
, *q
= NULL
, *r
= NULL
, *s
= NULL
, *t
= NULL
, *v
= NULL
, *w
= NULL
;
139 p
= strv_join((char **)input_table_multiple
, ", ");
141 assert_se(streq(p
, "one, two, three"));
143 q
= strv_join((char **)input_table_multiple
, ";");
145 assert_se(streq(q
, "one;two;three"));
147 r
= strv_join((char **)input_table_multiple
, NULL
);
149 assert_se(streq(r
, "one two three"));
151 s
= strv_join((char **)input_table_one
, ", ");
153 assert_se(streq(s
, "one"));
155 t
= strv_join((char **)input_table_none
, ", ");
157 assert_se(streq(t
, ""));
159 v
= strv_join((char **)input_table_two_empties
, ", ");
161 assert_se(streq(v
, ", "));
163 w
= strv_join((char **)input_table_one_empty
, ", ");
165 assert_se(streq(w
, ""));
168 static void test_strv_join_prefix(void) {
169 _cleanup_free_
char *p
= NULL
, *q
= NULL
, *r
= NULL
, *s
= NULL
, *t
= NULL
, *v
= NULL
, *w
= NULL
;
171 p
= strv_join_prefix((char **)input_table_multiple
, ", ", "foo");
173 assert_se(streq(p
, "fooone, footwo, foothree"));
175 q
= strv_join_prefix((char **)input_table_multiple
, ";", "foo");
177 assert_se(streq(q
, "fooone;footwo;foothree"));
179 r
= strv_join_prefix((char **)input_table_multiple
, NULL
, "foo");
181 assert_se(streq(r
, "fooone footwo foothree"));
183 s
= strv_join_prefix((char **)input_table_one
, ", ", "foo");
185 assert_se(streq(s
, "fooone"));
187 t
= strv_join_prefix((char **)input_table_none
, ", ", "foo");
189 assert_se(streq(t
, ""));
191 v
= strv_join_prefix((char **)input_table_two_empties
, ", ", "foo");
193 assert_se(streq(v
, "foo, foo"));
195 w
= strv_join_prefix((char **)input_table_one_empty
, ", ", "foo");
197 assert_se(streq(w
, "foo"));
200 static void test_strv_unquote(const char *quoted
, char **list
) {
201 _cleanup_strv_free_
char **s
;
202 _cleanup_free_
char *j
;
207 r
= strv_split_extract(&s
, quoted
, WHITESPACE
, EXTRACT_QUOTES
);
208 assert_se(r
== (int) strv_length(list
));
210 j
= strv_join(s
, " | ");
215 assert_se(streq(list
[i
++], *t
));
217 assert_se(list
[i
] == NULL
);
220 static void test_invalid_unquote(const char *quoted
) {
224 r
= strv_split_extract(&s
, quoted
, WHITESPACE
, EXTRACT_QUOTES
);
225 assert_se(s
== NULL
);
226 assert_se(r
== -EINVAL
);
229 static void test_strv_split(void) {
230 _cleanup_strv_free_
char **l
= NULL
;
231 const char str
[] = "one,two,three";
233 l
= strv_split(str
, ",");
235 assert_se(strv_equal(l
, (char**) input_table_multiple
));
239 l
= strv_split(" one two\t three", WHITESPACE
);
241 assert_se(strv_equal(l
, (char**) input_table_multiple
));
245 /* Setting NULL for separator is equivalent to WHITESPACE */
246 l
= strv_split(" one two\t three", NULL
);
248 assert_se(strv_equal(l
, (char**) input_table_multiple
));
252 l
= strv_split_full(" one two\t three", NULL
, 0);
254 assert_se(strv_equal(l
, (char**) input_table_multiple
));
258 l
= strv_split_full(" 'one' \" two\t three \" ' four five'", NULL
, SPLIT_QUOTES
);
260 assert_se(strv_equal(l
, (char**) input_table_quoted
));
264 /* missing last quote ignores the last element. */
265 l
= strv_split_full(" 'one' \" two\t three \" ' four five' ' ignored element ", NULL
, SPLIT_QUOTES
);
267 assert_se(strv_equal(l
, (char**) input_table_quoted
));
271 /* missing last quote, but the last element is _not_ ignored with SPLIT_RELAX. */
272 l
= strv_split_full(" 'one' \" two\t three \" ' four five", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
274 assert_se(strv_equal(l
, (char**) input_table_quoted
));
278 /* missing separator between */
279 l
= strv_split_full(" 'one' \" two\t three \"' four five'", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
281 assert_se(strv_equal(l
, (char**) input_table_quoted
));
285 l
= strv_split_full(" 'one' \" two\t three \"' four five", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
287 assert_se(strv_equal(l
, (char**) input_table_quoted
));
290 static void test_strv_split_empty(void) {
291 _cleanup_strv_free_
char **l
= NULL
;
293 l
= strv_split("", WHITESPACE
);
295 assert_se(strv_isempty(l
));
298 l
= strv_split("", NULL
);
300 assert_se(strv_isempty(l
));
303 l
= strv_split_full("", NULL
, 0);
305 assert_se(strv_isempty(l
));
308 l
= strv_split_full("", NULL
, SPLIT_QUOTES
);
310 assert_se(strv_isempty(l
));
313 l
= strv_split_full("", WHITESPACE
, SPLIT_QUOTES
);
315 assert_se(strv_isempty(l
));
318 l
= strv_split_full("", WHITESPACE
, SPLIT_QUOTES
| SPLIT_RELAX
);
320 assert_se(strv_isempty(l
));
323 l
= strv_split(" ", WHITESPACE
);
325 assert_se(strv_isempty(l
));
328 l
= strv_split(" ", NULL
);
330 assert_se(strv_isempty(l
));
333 l
= strv_split_full(" ", NULL
, 0);
335 assert_se(strv_isempty(l
));
338 l
= strv_split_full(" ", WHITESPACE
, SPLIT_QUOTES
);
340 assert_se(strv_isempty(l
));
343 l
= strv_split_full(" ", NULL
, SPLIT_QUOTES
);
345 assert_se(strv_isempty(l
));
348 l
= strv_split_full(" ", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
350 assert_se(strv_isempty(l
));
353 static void test_strv_split_extract(void) {
354 _cleanup_strv_free_
char **l
= NULL
;
355 const char *str
= ":foo\\:bar::waldo:";
358 r
= strv_split_extract(&l
, str
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
359 assert_se(r
== (int) strv_length(l
));
360 assert_se(streq_ptr(l
[0], ""));
361 assert_se(streq_ptr(l
[1], "foo:bar"));
362 assert_se(streq_ptr(l
[2], ""));
363 assert_se(streq_ptr(l
[3], "waldo"));
364 assert_se(streq_ptr(l
[4], ""));
365 assert_se(streq_ptr(l
[5], NULL
));
368 static void test_strv_split_newlines(void) {
371 _cleanup_strv_free_
char **l
= NULL
;
372 const char str
[] = "one\ntwo\nthree";
374 l
= strv_split_newlines(str
);
379 assert_se(streq(*s
, input_table_multiple
[i
++]));
383 static void test_strv_split_nulstr(void) {
384 _cleanup_strv_free_
char **l
= NULL
;
385 const char nulstr
[] = "str0\0str1\0str2\0str3\0";
387 l
= strv_split_nulstr (nulstr
);
390 assert_se(streq(l
[0], "str0"));
391 assert_se(streq(l
[1], "str1"));
392 assert_se(streq(l
[2], "str2"));
393 assert_se(streq(l
[3], "str3"));
396 static void test_strv_parse_nulstr(void) {
397 _cleanup_strv_free_
char **l
= NULL
;
398 const char nulstr
[] = "hoge\0hoge2\0hoge3\0\0hoge5\0\0xxx";
400 l
= strv_parse_nulstr(nulstr
, sizeof(nulstr
)-1);
402 puts("Parse nulstr:");
405 assert_se(streq(l
[0], "hoge"));
406 assert_se(streq(l
[1], "hoge2"));
407 assert_se(streq(l
[2], "hoge3"));
408 assert_se(streq(l
[3], ""));
409 assert_se(streq(l
[4], "hoge5"));
410 assert_se(streq(l
[5], ""));
411 assert_se(streq(l
[6], "xxx"));
414 static void test_strv_overlap(void) {
415 const char * const input_table
[] = {
421 const char * const input_table_overlap
[] = {
425 const char * const input_table_unique
[] = {
432 assert_se(strv_overlap((char **)input_table
, (char**)input_table_overlap
));
433 assert_se(!strv_overlap((char **)input_table
, (char**)input_table_unique
));
436 static void test_strv_sort(void) {
437 const char* input_table
[] = {
441 "CAPITAL LETTERS FIRST",
446 strv_sort((char **)input_table
);
448 assert_se(streq(input_table
[0], "CAPITAL LETTERS FIRST"));
449 assert_se(streq(input_table
[1], "apple"));
450 assert_se(streq(input_table
[2], "banana"));
451 assert_se(streq(input_table
[3], "citrus"));
452 assert_se(streq(input_table
[4], "durian"));
455 static void test_strv_extend_strv_concat(void) {
456 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
458 a
= strv_new("without", "suffix");
459 b
= strv_new("with", "suffix");
463 assert_se(strv_extend_strv_concat(&a
, b
, "_suffix") >= 0);
465 assert_se(streq(a
[0], "without"));
466 assert_se(streq(a
[1], "suffix"));
467 assert_se(streq(a
[2], "with_suffix"));
468 assert_se(streq(a
[3], "suffix_suffix"));
471 static void test_strv_extend_strv(void) {
472 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **n
= NULL
;
474 a
= strv_new("abc", "def", "ghi");
475 b
= strv_new("jkl", "mno", "abc", "pqr");
479 assert_se(strv_extend_strv(&a
, b
, true) == 3);
481 assert_se(streq(a
[0], "abc"));
482 assert_se(streq(a
[1], "def"));
483 assert_se(streq(a
[2], "ghi"));
484 assert_se(streq(a
[3], "jkl"));
485 assert_se(streq(a
[4], "mno"));
486 assert_se(streq(a
[5], "pqr"));
487 assert_se(strv_length(a
) == 6);
489 assert_se(strv_extend_strv(&n
, b
, false) >= 0);
490 assert_se(streq(n
[0], "jkl"));
491 assert_se(streq(n
[1], "mno"));
492 assert_se(streq(n
[2], "abc"));
493 assert_se(streq(n
[3], "pqr"));
494 assert_se(strv_length(n
) == 4);
497 static void test_strv_extend(void) {
498 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
500 a
= strv_new("test", "test1");
502 assert_se(strv_extend(&a
, "test2") >= 0);
503 assert_se(strv_extend(&b
, "test3") >= 0);
505 assert_se(streq(a
[0], "test"));
506 assert_se(streq(a
[1], "test1"));
507 assert_se(streq(a
[2], "test2"));
508 assert_se(streq(b
[0], "test3"));
511 static void test_strv_extendf(void) {
512 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
514 a
= strv_new("test", "test1");
516 assert_se(strv_extendf(&a
, "test2 %s %d %s", "foo", 128, "bar") >= 0);
517 assert_se(strv_extendf(&b
, "test3 %s %s %d", "bar", "foo", 128) >= 0);
519 assert_se(streq(a
[0], "test"));
520 assert_se(streq(a
[1], "test1"));
521 assert_se(streq(a
[2], "test2 foo 128 bar"));
522 assert_se(streq(b
[0], "test3 bar foo 128"));
525 static void test_strv_foreach(void) {
526 _cleanup_strv_free_
char **a
;
530 a
= strv_new("one", "two", "three");
534 STRV_FOREACH(check
, a
) {
535 assert_se(streq(*check
, input_table_multiple
[i
++]));
539 static void test_strv_foreach_backwards(void) {
540 _cleanup_strv_free_
char **a
;
544 a
= strv_new("one", "two", "three");
548 STRV_FOREACH_BACKWARDS(check
, a
)
549 assert_se(streq_ptr(*check
, input_table_multiple
[i
--]));
551 STRV_FOREACH_BACKWARDS(check
, (char**) NULL
)
552 assert_not_reached("Let's see that we check empty strv right, too.");
554 STRV_FOREACH_BACKWARDS(check
, (char**) { NULL
})
555 assert_not_reached("Let's see that we check empty strv right, too.");
558 static void test_strv_foreach_pair(void) {
559 _cleanup_strv_free_
char **a
= NULL
;
562 a
= strv_new("pair_one", "pair_one",
563 "pair_two", "pair_two",
564 "pair_three", "pair_three");
566 STRV_FOREACH_PAIR(x
, y
, a
) {
567 assert_se(streq(*x
, *y
));
571 static void test_strv_from_stdarg_alloca_one(char **l
, const char *first
, ...) {
575 j
= strv_from_stdarg_alloca(first
);
578 assert_se(streq_ptr(l
[i
], j
[i
]));
585 static void test_strv_from_stdarg_alloca(void) {
586 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL
);
587 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL
);
588 test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY
, NULL
);
591 static void test_strv_insert(void) {
592 _cleanup_strv_free_
char **a
= NULL
;
594 assert_se(strv_insert(&a
, 0, strdup("first")) == 0);
595 assert_se(streq(a
[0], "first"));
598 assert_se(strv_insert(&a
, 0, NULL
) == 0);
599 assert_se(streq(a
[0], "first"));
602 assert_se(strv_insert(&a
, 1, strdup("two")) == 0);
603 assert_se(streq(a
[0], "first"));
604 assert_se(streq(a
[1], "two"));
607 assert_se(strv_insert(&a
, 4, strdup("tri")) == 0);
608 assert_se(streq(a
[0], "first"));
609 assert_se(streq(a
[1], "two"));
610 assert_se(streq(a
[2], "tri"));
613 assert_se(strv_insert(&a
, 1, strdup("duo")) == 0);
614 assert_se(streq(a
[0], "first"));
615 assert_se(streq(a
[1], "duo"));
616 assert_se(streq(a
[2], "two"));
617 assert_se(streq(a
[3], "tri"));
621 static void test_strv_push_prepend(void) {
622 _cleanup_strv_free_
char **a
= NULL
;
624 a
= strv_new("foo", "bar", "three");
626 assert_se(strv_push_prepend(&a
, strdup("first")) >= 0);
627 assert_se(streq(a
[0], "first"));
628 assert_se(streq(a
[1], "foo"));
629 assert_se(streq(a
[2], "bar"));
630 assert_se(streq(a
[3], "three"));
633 assert_se(strv_consume_prepend(&a
, strdup("first2")) >= 0);
634 assert_se(streq(a
[0], "first2"));
635 assert_se(streq(a
[1], "first"));
636 assert_se(streq(a
[2], "foo"));
637 assert_se(streq(a
[3], "bar"));
638 assert_se(streq(a
[4], "three"));
642 static void test_strv_push(void) {
643 _cleanup_strv_free_
char **a
= NULL
;
646 assert_se(i
= strdup("foo"));
647 assert_se(strv_push(&a
, i
) >= 0);
649 assert_se(i
= strdup("a"));
650 assert_se(j
= strdup("b"));
651 assert_se(strv_push_pair(&a
, i
, j
) >= 0);
653 assert_se(streq_ptr(a
[0], "foo"));
654 assert_se(streq_ptr(a
[1], "a"));
655 assert_se(streq_ptr(a
[2], "b"));
656 assert_se(streq_ptr(a
[3], NULL
));
659 static void test_strv_equal(void) {
660 _cleanup_strv_free_
char **a
= NULL
;
661 _cleanup_strv_free_
char **b
= NULL
;
662 _cleanup_strv_free_
char **c
= NULL
;
664 a
= strv_new("one", "two", "three");
666 b
= strv_new("one", "two", "three");
668 c
= strv_new("one", "two", "three", "four");
671 assert_se(strv_equal(a
, a
));
672 assert_se(strv_equal(a
, b
));
673 assert_se(strv_equal(NULL
, NULL
));
675 assert_se(!strv_equal(a
, c
));
676 assert_se(!strv_equal(b
, c
));
677 assert_se(!strv_equal(b
, NULL
));
680 static void test_strv_is_uniq(void) {
681 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
, **d
= NULL
;
685 assert_se(strv_is_uniq(a
));
689 assert_se(strv_is_uniq(b
));
691 c
= strv_new("foo", "bar");
693 assert_se(strv_is_uniq(c
));
695 d
= strv_new("foo", "bar", "waldo", "bar", "piep");
697 assert_se(!strv_is_uniq(d
));
700 static void test_strv_reverse(void) {
701 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
, **d
= NULL
;
707 assert_se(strv_isempty(a
));
712 assert_se(streq_ptr(b
[0], "foo"));
713 assert_se(streq_ptr(b
[1], NULL
));
715 c
= strv_new("foo", "bar");
718 assert_se(streq_ptr(c
[0], "bar"));
719 assert_se(streq_ptr(c
[1], "foo"));
720 assert_se(streq_ptr(c
[2], NULL
));
722 d
= strv_new("foo", "bar", "waldo");
725 assert_se(streq_ptr(d
[0], "waldo"));
726 assert_se(streq_ptr(d
[1], "bar"));
727 assert_se(streq_ptr(d
[2], "foo"));
728 assert_se(streq_ptr(d
[3], NULL
));
731 static void test_strv_shell_escape(void) {
732 _cleanup_strv_free_
char **v
= NULL
;
734 v
= strv_new("foo:bar", "bar,baz", "wal\\do");
736 assert_se(strv_shell_escape(v
, ",:"));
737 assert_se(streq_ptr(v
[0], "foo\\:bar"));
738 assert_se(streq_ptr(v
[1], "bar\\,baz"));
739 assert_se(streq_ptr(v
[2], "wal\\\\do"));
740 assert_se(streq_ptr(v
[3], NULL
));
743 static void test_strv_skip_one(char **a
, size_t n
, char **b
) {
745 assert_se(strv_equal(a
, b
));
748 static void test_strv_skip(void) {
749 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
750 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
751 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
752 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL
));
753 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL
));
754 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL
));
756 test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
757 test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL
));
758 test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL
));
760 test_strv_skip_one(STRV_MAKE(NULL
), 0, STRV_MAKE(NULL
));
761 test_strv_skip_one(STRV_MAKE(NULL
), 1, STRV_MAKE(NULL
));
762 test_strv_skip_one(STRV_MAKE(NULL
), 55, STRV_MAKE(NULL
));
765 static void test_strv_extend_n(void) {
766 _cleanup_strv_free_
char **v
= NULL
;
768 v
= strv_new("foo", "bar");
771 assert_se(strv_extend_n(&v
, "waldo", 3) >= 0);
772 assert_se(strv_extend_n(&v
, "piep", 2) >= 0);
774 assert_se(streq(v
[0], "foo"));
775 assert_se(streq(v
[1], "bar"));
776 assert_se(streq(v
[2], "waldo"));
777 assert_se(streq(v
[3], "waldo"));
778 assert_se(streq(v
[4], "waldo"));
779 assert_se(streq(v
[5], "piep"));
780 assert_se(streq(v
[6], "piep"));
781 assert_se(v
[7] == NULL
);
785 assert_se(strv_extend_n(&v
, "foo", 1) >= 0);
786 assert_se(strv_extend_n(&v
, "bar", 0) >= 0);
788 assert_se(streq(v
[0], "foo"));
789 assert_se(v
[1] == NULL
);
792 static void test_strv_make_nulstr_one(char **l
) {
793 _cleanup_free_
char *b
= NULL
, *c
= NULL
;
794 _cleanup_strv_free_
char **q
= NULL
;
795 const char *s
= NULL
;
799 assert_se(strv_make_nulstr(l
, &b
, &n
) >= 0);
800 assert_se(q
= strv_parse_nulstr(b
, n
));
801 assert_se(strv_equal(l
, q
));
803 assert_se(strv_make_nulstr(q
, &c
, &m
) >= 0);
805 assert_se(memcmp(b
, c
, m
) == 0);
808 assert_se(streq(s
, l
[i
++]));
809 assert_se(i
== strv_length(l
));
812 static void test_strv_make_nulstr(void) {
813 test_strv_make_nulstr_one(NULL
);
814 test_strv_make_nulstr_one(STRV_MAKE(NULL
));
815 test_strv_make_nulstr_one(STRV_MAKE("foo"));
816 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar"));
817 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
820 static void test_strv_free_free(void) {
823 assert_se(t
= new(char**, 3));
824 assert_se(t
[0] = strv_new("a", "b"));
825 assert_se(t
[1] = strv_new("c", "d", "e"));
828 t
= strv_free_free(t
);
831 static void test_foreach_string(void) {
832 const char * const t
[] = {
841 FOREACH_STRING(x
, "foo", "bar", "waldo")
842 assert_se(streq_ptr(t
[i
++], x
));
846 FOREACH_STRING(x
, "zzz")
847 assert_se(streq(x
, "zzz"));
850 static void test_strv_fnmatch(void) {
851 _cleanup_strv_free_
char **v
= NULL
;
853 assert_se(!strv_fnmatch(STRV_MAKE_EMPTY
, "a", 0));
855 v
= strv_new("*\\*");
856 assert_se(!strv_fnmatch(v
, "\\", 0));
857 assert_se(strv_fnmatch(v
, "\\", FNM_NOESCAPE
));
860 int main(int argc
, char *argv
[]) {
861 test_specifier_printf();
863 test_strptr_in_set();
864 test_startswith_set();
866 test_strv_foreach_backwards();
867 test_strv_foreach_pair();
869 test_strv_find_prefix();
870 test_strv_find_startswith();
872 test_strv_join_prefix();
874 test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
875 test_strv_unquote("", STRV_MAKE_EMPTY
);
876 test_strv_unquote(" ", STRV_MAKE_EMPTY
);
877 test_strv_unquote(" ", STRV_MAKE_EMPTY
);
878 test_strv_unquote(" x", STRV_MAKE("x"));
879 test_strv_unquote("x ", STRV_MAKE("x"));
880 test_strv_unquote(" x ", STRV_MAKE("x"));
881 test_strv_unquote(" \"x\" ", STRV_MAKE("x"));
882 test_strv_unquote(" 'x' ", STRV_MAKE("x"));
883 test_strv_unquote(" 'x\"' ", STRV_MAKE("x\""));
884 test_strv_unquote(" \"x'\" ", STRV_MAKE("x'"));
885 test_strv_unquote("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
887 /* trailing backslashes */
888 test_strv_unquote(" x\\\\", STRV_MAKE("x\\"));
889 test_invalid_unquote(" x\\");
891 test_invalid_unquote("a --b='c \"d e\"''");
892 test_invalid_unquote("a --b='c \"d e\" '\"");
893 test_invalid_unquote("a --b='c \"d e\"garbage");
894 test_invalid_unquote("'");
895 test_invalid_unquote("\"");
896 test_invalid_unquote("'x'y'g");
899 test_strv_split_empty();
900 test_strv_split_extract();
901 test_strv_split_newlines();
902 test_strv_split_nulstr();
903 test_strv_parse_nulstr();
906 test_strv_extend_strv();
907 test_strv_extend_strv_concat();
910 test_strv_from_stdarg_alloca();
912 test_strv_push_prepend();
917 test_strv_shell_escape();
919 test_strv_extend_n();
920 test_strv_make_nulstr();
921 test_strv_free_free();
923 test_foreach_string();