1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
8 #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 const char* const input_table_multiple
[] = {
66 static const char* const input_table_quoted
[] = {
73 static const char* const input_table_one
[] = {
78 static const char* const input_table_none
[] = {
82 static const char* const input_table_two_empties
[] = {
88 static const char* const input_table_one_empty
[] = {
93 static void test_strv_find(void) {
94 assert_se(strv_find((char **)input_table_multiple
, "three"));
95 assert_se(!strv_find((char **)input_table_multiple
, "four"));
98 static void test_strv_find_prefix(void) {
99 assert_se(strv_find_prefix((char **)input_table_multiple
, "o"));
100 assert_se(strv_find_prefix((char **)input_table_multiple
, "one"));
101 assert_se(strv_find_prefix((char **)input_table_multiple
, ""));
102 assert_se(!strv_find_prefix((char **)input_table_multiple
, "xxx"));
103 assert_se(!strv_find_prefix((char **)input_table_multiple
, "onee"));
106 static void test_strv_find_startswith(void) {
109 r
= strv_find_startswith((char **)input_table_multiple
, "o");
110 assert_se(r
&& streq(r
, "ne"));
112 r
= strv_find_startswith((char **)input_table_multiple
, "one");
113 assert_se(r
&& streq(r
, ""));
115 r
= strv_find_startswith((char **)input_table_multiple
, "");
116 assert_se(r
&& streq(r
, "one"));
118 assert_se(!strv_find_startswith((char **)input_table_multiple
, "xxx"));
119 assert_se(!strv_find_startswith((char **)input_table_multiple
, "onee"));
122 static void test_strv_join(void) {
123 _cleanup_free_
char *p
= NULL
, *q
= NULL
, *r
= NULL
, *s
= NULL
, *t
= NULL
, *v
= NULL
, *w
= NULL
;
125 p
= strv_join((char **)input_table_multiple
, ", ");
127 assert_se(streq(p
, "one, two, three"));
129 q
= strv_join((char **)input_table_multiple
, ";");
131 assert_se(streq(q
, "one;two;three"));
133 r
= strv_join((char **)input_table_multiple
, NULL
);
135 assert_se(streq(r
, "one two three"));
137 s
= strv_join((char **)input_table_one
, ", ");
139 assert_se(streq(s
, "one"));
141 t
= strv_join((char **)input_table_none
, ", ");
143 assert_se(streq(t
, ""));
145 v
= strv_join((char **)input_table_two_empties
, ", ");
147 assert_se(streq(v
, ", "));
149 w
= strv_join((char **)input_table_one_empty
, ", ");
151 assert_se(streq(w
, ""));
154 static void test_strv_join_prefix(void) {
155 _cleanup_free_
char *p
= NULL
, *q
= NULL
, *r
= NULL
, *s
= NULL
, *t
= NULL
, *v
= NULL
, *w
= NULL
;
157 p
= strv_join_prefix((char **)input_table_multiple
, ", ", "foo");
159 assert_se(streq(p
, "fooone, footwo, foothree"));
161 q
= strv_join_prefix((char **)input_table_multiple
, ";", "foo");
163 assert_se(streq(q
, "fooone;footwo;foothree"));
165 r
= strv_join_prefix((char **)input_table_multiple
, NULL
, "foo");
167 assert_se(streq(r
, "fooone footwo foothree"));
169 s
= strv_join_prefix((char **)input_table_one
, ", ", "foo");
171 assert_se(streq(s
, "fooone"));
173 t
= strv_join_prefix((char **)input_table_none
, ", ", "foo");
175 assert_se(streq(t
, ""));
177 v
= strv_join_prefix((char **)input_table_two_empties
, ", ", "foo");
179 assert_se(streq(v
, "foo, foo"));
181 w
= strv_join_prefix((char **)input_table_one_empty
, ", ", "foo");
183 assert_se(streq(w
, "foo"));
186 static void test_strv_unquote(const char *quoted
, char **list
) {
187 _cleanup_strv_free_
char **s
;
188 _cleanup_free_
char *j
;
193 r
= strv_split_extract(&s
, quoted
, WHITESPACE
, EXTRACT_QUOTES
);
194 assert_se(r
== (int) strv_length(list
));
196 j
= strv_join(s
, " | ");
201 assert_se(streq(list
[i
++], *t
));
203 assert_se(list
[i
] == NULL
);
206 static void test_invalid_unquote(const char *quoted
) {
210 r
= strv_split_extract(&s
, quoted
, WHITESPACE
, EXTRACT_QUOTES
);
211 assert_se(s
== NULL
);
212 assert_se(r
== -EINVAL
);
215 static void test_strv_split(void) {
216 _cleanup_strv_free_
char **l
= NULL
;
217 const char str
[] = "one,two,three";
219 l
= strv_split(str
, ",");
221 assert_se(strv_equal(l
, (char**) input_table_multiple
));
225 l
= strv_split(" one two\t three", WHITESPACE
);
227 assert_se(strv_equal(l
, (char**) input_table_multiple
));
231 /* Setting NULL for separator is equivalent to WHITESPACE */
232 l
= strv_split(" one two\t three", NULL
);
234 assert_se(strv_equal(l
, (char**) input_table_multiple
));
238 l
= strv_split_full(" one two\t three", NULL
, 0);
240 assert_se(strv_equal(l
, (char**) input_table_multiple
));
244 l
= strv_split_full(" 'one' \" two\t three \" ' four five'", NULL
, SPLIT_QUOTES
);
246 assert_se(strv_equal(l
, (char**) input_table_quoted
));
250 /* missing last quote ignores the last element. */
251 l
= strv_split_full(" 'one' \" two\t three \" ' four five' ' ignored element ", NULL
, SPLIT_QUOTES
);
253 assert_se(strv_equal(l
, (char**) input_table_quoted
));
257 /* missing last quote, but the last element is _not_ ignored with SPLIT_RELAX. */
258 l
= strv_split_full(" 'one' \" two\t three \" ' four five", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
260 assert_se(strv_equal(l
, (char**) input_table_quoted
));
264 /* missing separator between */
265 l
= strv_split_full(" 'one' \" two\t three \"' four five'", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
267 assert_se(strv_equal(l
, (char**) input_table_quoted
));
271 l
= strv_split_full(" 'one' \" two\t three \"' four five", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
273 assert_se(strv_equal(l
, (char**) input_table_quoted
));
276 static void test_strv_split_empty(void) {
277 _cleanup_strv_free_
char **l
= NULL
;
279 l
= strv_split("", WHITESPACE
);
281 assert_se(strv_isempty(l
));
284 l
= strv_split("", NULL
);
286 assert_se(strv_isempty(l
));
289 l
= strv_split_full("", NULL
, 0);
291 assert_se(strv_isempty(l
));
294 l
= strv_split_full("", NULL
, SPLIT_QUOTES
);
296 assert_se(strv_isempty(l
));
299 l
= strv_split_full("", WHITESPACE
, SPLIT_QUOTES
);
301 assert_se(strv_isempty(l
));
304 l
= strv_split_full("", WHITESPACE
, SPLIT_QUOTES
| SPLIT_RELAX
);
306 assert_se(strv_isempty(l
));
309 l
= strv_split(" ", WHITESPACE
);
311 assert_se(strv_isempty(l
));
314 l
= strv_split(" ", NULL
);
316 assert_se(strv_isempty(l
));
319 l
= strv_split_full(" ", NULL
, 0);
321 assert_se(strv_isempty(l
));
324 l
= strv_split_full(" ", WHITESPACE
, SPLIT_QUOTES
);
326 assert_se(strv_isempty(l
));
329 l
= strv_split_full(" ", NULL
, SPLIT_QUOTES
);
331 assert_se(strv_isempty(l
));
334 l
= strv_split_full(" ", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
336 assert_se(strv_isempty(l
));
339 static void test_strv_split_extract(void) {
340 _cleanup_strv_free_
char **l
= NULL
;
341 const char *str
= ":foo\\:bar::waldo:";
344 r
= strv_split_extract(&l
, str
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
345 assert_se(r
== (int) strv_length(l
));
346 assert_se(streq_ptr(l
[0], ""));
347 assert_se(streq_ptr(l
[1], "foo:bar"));
348 assert_se(streq_ptr(l
[2], ""));
349 assert_se(streq_ptr(l
[3], "waldo"));
350 assert_se(streq_ptr(l
[4], ""));
351 assert_se(streq_ptr(l
[5], NULL
));
354 static void test_strv_split_newlines(void) {
357 _cleanup_strv_free_
char **l
= NULL
;
358 const char str
[] = "one\ntwo\nthree";
360 l
= strv_split_newlines(str
);
365 assert_se(streq(*s
, input_table_multiple
[i
++]));
369 static void test_strv_split_nulstr(void) {
370 _cleanup_strv_free_
char **l
= NULL
;
371 const char nulstr
[] = "str0\0str1\0str2\0str3\0";
373 l
= strv_split_nulstr (nulstr
);
376 assert_se(streq(l
[0], "str0"));
377 assert_se(streq(l
[1], "str1"));
378 assert_se(streq(l
[2], "str2"));
379 assert_se(streq(l
[3], "str3"));
382 static void test_strv_parse_nulstr(void) {
383 _cleanup_strv_free_
char **l
= NULL
;
384 const char nulstr
[] = "hoge\0hoge2\0hoge3\0\0hoge5\0\0xxx";
386 l
= strv_parse_nulstr(nulstr
, sizeof(nulstr
)-1);
388 puts("Parse nulstr:");
391 assert_se(streq(l
[0], "hoge"));
392 assert_se(streq(l
[1], "hoge2"));
393 assert_se(streq(l
[2], "hoge3"));
394 assert_se(streq(l
[3], ""));
395 assert_se(streq(l
[4], "hoge5"));
396 assert_se(streq(l
[5], ""));
397 assert_se(streq(l
[6], "xxx"));
400 static void test_strv_overlap(void) {
401 const char * const input_table
[] = {
407 const char * const input_table_overlap
[] = {
411 const char * const input_table_unique
[] = {
418 assert_se(strv_overlap((char **)input_table
, (char**)input_table_overlap
));
419 assert_se(!strv_overlap((char **)input_table
, (char**)input_table_unique
));
422 static void test_strv_sort(void) {
423 const char* input_table
[] = {
427 "CAPITAL LETTERS FIRST",
432 strv_sort((char **)input_table
);
434 assert_se(streq(input_table
[0], "CAPITAL LETTERS FIRST"));
435 assert_se(streq(input_table
[1], "apple"));
436 assert_se(streq(input_table
[2], "banana"));
437 assert_se(streq(input_table
[3], "citrus"));
438 assert_se(streq(input_table
[4], "durian"));
441 static void test_strv_extend_strv_concat(void) {
442 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
444 a
= strv_new("without", "suffix");
445 b
= strv_new("with", "suffix");
449 assert_se(strv_extend_strv_concat(&a
, b
, "_suffix") >= 0);
451 assert_se(streq(a
[0], "without"));
452 assert_se(streq(a
[1], "suffix"));
453 assert_se(streq(a
[2], "with_suffix"));
454 assert_se(streq(a
[3], "suffix_suffix"));
457 static void test_strv_extend_strv(void) {
458 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **n
= NULL
;
460 a
= strv_new("abc", "def", "ghi");
461 b
= strv_new("jkl", "mno", "abc", "pqr");
465 assert_se(strv_extend_strv(&a
, b
, true) == 3);
467 assert_se(streq(a
[0], "abc"));
468 assert_se(streq(a
[1], "def"));
469 assert_se(streq(a
[2], "ghi"));
470 assert_se(streq(a
[3], "jkl"));
471 assert_se(streq(a
[4], "mno"));
472 assert_se(streq(a
[5], "pqr"));
473 assert_se(strv_length(a
) == 6);
475 assert_se(strv_extend_strv(&n
, b
, false) >= 0);
476 assert_se(streq(n
[0], "jkl"));
477 assert_se(streq(n
[1], "mno"));
478 assert_se(streq(n
[2], "abc"));
479 assert_se(streq(n
[3], "pqr"));
480 assert_se(strv_length(n
) == 4);
483 static void test_strv_extend(void) {
484 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
486 a
= strv_new("test", "test1");
488 assert_se(strv_extend(&a
, "test2") >= 0);
489 assert_se(strv_extend(&b
, "test3") >= 0);
491 assert_se(streq(a
[0], "test"));
492 assert_se(streq(a
[1], "test1"));
493 assert_se(streq(a
[2], "test2"));
494 assert_se(streq(b
[0], "test3"));
497 static void test_strv_extendf(void) {
498 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
500 a
= strv_new("test", "test1");
502 assert_se(strv_extendf(&a
, "test2 %s %d %s", "foo", 128, "bar") >= 0);
503 assert_se(strv_extendf(&b
, "test3 %s %s %d", "bar", "foo", 128) >= 0);
505 assert_se(streq(a
[0], "test"));
506 assert_se(streq(a
[1], "test1"));
507 assert_se(streq(a
[2], "test2 foo 128 bar"));
508 assert_se(streq(b
[0], "test3 bar foo 128"));
511 static void test_strv_foreach(void) {
512 _cleanup_strv_free_
char **a
;
516 a
= strv_new("one", "two", "three");
520 STRV_FOREACH(check
, a
) {
521 assert_se(streq(*check
, input_table_multiple
[i
++]));
525 static void test_strv_foreach_backwards(void) {
526 _cleanup_strv_free_
char **a
;
530 a
= strv_new("one", "two", "three");
534 STRV_FOREACH_BACKWARDS(check
, a
)
535 assert_se(streq_ptr(*check
, input_table_multiple
[i
--]));
537 STRV_FOREACH_BACKWARDS(check
, (char**) NULL
)
538 assert_not_reached("Let's see that we check empty strv right, too.");
540 STRV_FOREACH_BACKWARDS(check
, (char**) { NULL
})
541 assert_not_reached("Let's see that we check empty strv right, too.");
544 static void test_strv_foreach_pair(void) {
545 _cleanup_strv_free_
char **a
= NULL
;
548 a
= strv_new("pair_one", "pair_one",
549 "pair_two", "pair_two",
550 "pair_three", "pair_three");
552 STRV_FOREACH_PAIR(x
, y
, a
) {
553 assert_se(streq(*x
, *y
));
557 static void test_strv_from_stdarg_alloca_one(char **l
, const char *first
, ...) {
561 j
= strv_from_stdarg_alloca(first
);
564 assert_se(streq_ptr(l
[i
], j
[i
]));
571 static void test_strv_from_stdarg_alloca(void) {
572 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL
);
573 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL
);
574 test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY
, NULL
);
577 static void test_strv_insert(void) {
578 _cleanup_strv_free_
char **a
= NULL
;
580 assert_se(strv_insert(&a
, 0, strdup("first")) == 0);
581 assert_se(streq(a
[0], "first"));
584 assert_se(strv_insert(&a
, 0, NULL
) == 0);
585 assert_se(streq(a
[0], "first"));
588 assert_se(strv_insert(&a
, 1, strdup("two")) == 0);
589 assert_se(streq(a
[0], "first"));
590 assert_se(streq(a
[1], "two"));
593 assert_se(strv_insert(&a
, 4, strdup("tri")) == 0);
594 assert_se(streq(a
[0], "first"));
595 assert_se(streq(a
[1], "two"));
596 assert_se(streq(a
[2], "tri"));
599 assert_se(strv_insert(&a
, 1, strdup("duo")) == 0);
600 assert_se(streq(a
[0], "first"));
601 assert_se(streq(a
[1], "duo"));
602 assert_se(streq(a
[2], "two"));
603 assert_se(streq(a
[3], "tri"));
607 static void test_strv_push_prepend(void) {
608 _cleanup_strv_free_
char **a
= NULL
;
610 a
= strv_new("foo", "bar", "three");
612 assert_se(strv_push_prepend(&a
, strdup("first")) >= 0);
613 assert_se(streq(a
[0], "first"));
614 assert_se(streq(a
[1], "foo"));
615 assert_se(streq(a
[2], "bar"));
616 assert_se(streq(a
[3], "three"));
619 assert_se(strv_consume_prepend(&a
, strdup("first2")) >= 0);
620 assert_se(streq(a
[0], "first2"));
621 assert_se(streq(a
[1], "first"));
622 assert_se(streq(a
[2], "foo"));
623 assert_se(streq(a
[3], "bar"));
624 assert_se(streq(a
[4], "three"));
628 static void test_strv_push(void) {
629 _cleanup_strv_free_
char **a
= NULL
;
632 assert_se(i
= strdup("foo"));
633 assert_se(strv_push(&a
, i
) >= 0);
635 assert_se(i
= strdup("a"));
636 assert_se(j
= strdup("b"));
637 assert_se(strv_push_pair(&a
, i
, j
) >= 0);
639 assert_se(streq_ptr(a
[0], "foo"));
640 assert_se(streq_ptr(a
[1], "a"));
641 assert_se(streq_ptr(a
[2], "b"));
642 assert_se(streq_ptr(a
[3], NULL
));
645 static void test_strv_equal(void) {
646 _cleanup_strv_free_
char **a
= NULL
;
647 _cleanup_strv_free_
char **b
= NULL
;
648 _cleanup_strv_free_
char **c
= NULL
;
650 a
= strv_new("one", "two", "three");
652 b
= strv_new("one", "two", "three");
654 c
= strv_new("one", "two", "three", "four");
657 assert_se(strv_equal(a
, a
));
658 assert_se(strv_equal(a
, b
));
659 assert_se(strv_equal(NULL
, NULL
));
661 assert_se(!strv_equal(a
, c
));
662 assert_se(!strv_equal(b
, c
));
663 assert_se(!strv_equal(b
, NULL
));
666 static void test_strv_is_uniq(void) {
667 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
, **d
= NULL
;
671 assert_se(strv_is_uniq(a
));
675 assert_se(strv_is_uniq(b
));
677 c
= strv_new("foo", "bar");
679 assert_se(strv_is_uniq(c
));
681 d
= strv_new("foo", "bar", "waldo", "bar", "piep");
683 assert_se(!strv_is_uniq(d
));
686 static void test_strv_reverse(void) {
687 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
, **d
= NULL
;
693 assert_se(strv_isempty(a
));
698 assert_se(streq_ptr(b
[0], "foo"));
699 assert_se(streq_ptr(b
[1], NULL
));
701 c
= strv_new("foo", "bar");
704 assert_se(streq_ptr(c
[0], "bar"));
705 assert_se(streq_ptr(c
[1], "foo"));
706 assert_se(streq_ptr(c
[2], NULL
));
708 d
= strv_new("foo", "bar", "waldo");
711 assert_se(streq_ptr(d
[0], "waldo"));
712 assert_se(streq_ptr(d
[1], "bar"));
713 assert_se(streq_ptr(d
[2], "foo"));
714 assert_se(streq_ptr(d
[3], NULL
));
717 static void test_strv_shell_escape(void) {
718 _cleanup_strv_free_
char **v
= NULL
;
720 v
= strv_new("foo:bar", "bar,baz", "wal\\do");
722 assert_se(strv_shell_escape(v
, ",:"));
723 assert_se(streq_ptr(v
[0], "foo\\:bar"));
724 assert_se(streq_ptr(v
[1], "bar\\,baz"));
725 assert_se(streq_ptr(v
[2], "wal\\\\do"));
726 assert_se(streq_ptr(v
[3], NULL
));
729 static void test_strv_skip_one(char **a
, size_t n
, char **b
) {
731 assert_se(strv_equal(a
, b
));
734 static void test_strv_skip(void) {
735 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
736 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
737 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
738 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL
));
739 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL
));
740 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL
));
742 test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
743 test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL
));
744 test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL
));
746 test_strv_skip_one(STRV_MAKE(NULL
), 0, STRV_MAKE(NULL
));
747 test_strv_skip_one(STRV_MAKE(NULL
), 1, STRV_MAKE(NULL
));
748 test_strv_skip_one(STRV_MAKE(NULL
), 55, STRV_MAKE(NULL
));
751 static void test_strv_extend_n(void) {
752 _cleanup_strv_free_
char **v
= NULL
;
754 v
= strv_new("foo", "bar");
757 assert_se(strv_extend_n(&v
, "waldo", 3) >= 0);
758 assert_se(strv_extend_n(&v
, "piep", 2) >= 0);
760 assert_se(streq(v
[0], "foo"));
761 assert_se(streq(v
[1], "bar"));
762 assert_se(streq(v
[2], "waldo"));
763 assert_se(streq(v
[3], "waldo"));
764 assert_se(streq(v
[4], "waldo"));
765 assert_se(streq(v
[5], "piep"));
766 assert_se(streq(v
[6], "piep"));
767 assert_se(v
[7] == NULL
);
771 assert_se(strv_extend_n(&v
, "foo", 1) >= 0);
772 assert_se(strv_extend_n(&v
, "bar", 0) >= 0);
774 assert_se(streq(v
[0], "foo"));
775 assert_se(v
[1] == NULL
);
778 static void test_strv_make_nulstr_one(char **l
) {
779 _cleanup_free_
char *b
= NULL
, *c
= NULL
;
780 _cleanup_strv_free_
char **q
= NULL
;
781 const char *s
= NULL
;
785 assert_se(strv_make_nulstr(l
, &b
, &n
) >= 0);
786 assert_se(q
= strv_parse_nulstr(b
, n
));
787 assert_se(strv_equal(l
, q
));
789 assert_se(strv_make_nulstr(q
, &c
, &m
) >= 0);
791 assert_se(memcmp(b
, c
, m
) == 0);
794 assert_se(streq(s
, l
[i
++]));
795 assert_se(i
== strv_length(l
));
798 static void test_strv_make_nulstr(void) {
799 test_strv_make_nulstr_one(NULL
);
800 test_strv_make_nulstr_one(STRV_MAKE(NULL
));
801 test_strv_make_nulstr_one(STRV_MAKE("foo"));
802 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar"));
803 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
806 static void test_strv_free_free(void) {
809 assert_se(t
= new(char**, 3));
810 assert_se(t
[0] = strv_new("a", "b"));
811 assert_se(t
[1] = strv_new("c", "d", "e"));
814 t
= strv_free_free(t
);
817 static void test_foreach_string(void) {
818 const char * const t
[] = {
827 FOREACH_STRING(x
, "foo", "bar", "waldo")
828 assert_se(streq_ptr(t
[i
++], x
));
832 FOREACH_STRING(x
, "zzz")
833 assert_se(streq(x
, "zzz"));
836 static void test_strv_fnmatch(void) {
837 _cleanup_strv_free_
char **v
= NULL
;
839 assert_se(!strv_fnmatch(STRV_MAKE_EMPTY
, "a", 0));
841 v
= strv_new("*\\*");
842 assert_se(!strv_fnmatch(v
, "\\", 0));
843 assert_se(strv_fnmatch(v
, "\\", FNM_NOESCAPE
));
846 int main(int argc
, char *argv
[]) {
847 test_specifier_printf();
849 test_strptr_in_set();
851 test_strv_foreach_backwards();
852 test_strv_foreach_pair();
854 test_strv_find_prefix();
855 test_strv_find_startswith();
857 test_strv_join_prefix();
859 test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
860 test_strv_unquote("", STRV_MAKE_EMPTY
);
861 test_strv_unquote(" ", STRV_MAKE_EMPTY
);
862 test_strv_unquote(" ", STRV_MAKE_EMPTY
);
863 test_strv_unquote(" x", STRV_MAKE("x"));
864 test_strv_unquote("x ", STRV_MAKE("x"));
865 test_strv_unquote(" x ", STRV_MAKE("x"));
866 test_strv_unquote(" \"x\" ", STRV_MAKE("x"));
867 test_strv_unquote(" 'x' ", STRV_MAKE("x"));
868 test_strv_unquote(" 'x\"' ", STRV_MAKE("x\""));
869 test_strv_unquote(" \"x'\" ", STRV_MAKE("x'"));
870 test_strv_unquote("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
872 /* trailing backslashes */
873 test_strv_unquote(" x\\\\", STRV_MAKE("x\\"));
874 test_invalid_unquote(" x\\");
876 test_invalid_unquote("a --b='c \"d e\"''");
877 test_invalid_unquote("a --b='c \"d e\" '\"");
878 test_invalid_unquote("a --b='c \"d e\"garbage");
879 test_invalid_unquote("'");
880 test_invalid_unquote("\"");
881 test_invalid_unquote("'x'y'g");
884 test_strv_split_empty();
885 test_strv_split_extract();
886 test_strv_split_newlines();
887 test_strv_split_nulstr();
888 test_strv_parse_nulstr();
891 test_strv_extend_strv();
892 test_strv_extend_strv_concat();
895 test_strv_from_stdarg_alloca();
897 test_strv_push_prepend();
902 test_strv_shell_escape();
904 test_strv_extend_n();
905 test_strv_make_nulstr();
906 test_strv_free_free();
908 test_foreach_string();