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 log_info("/* %s */", __func__
);
28 r
= specifier_printf("xxx a=%a b=%b yyy", table
, NULL
, &w
);
33 assert_se(streq(w
, "xxx a=AAAA b=BBBB yyy"));
36 r
= specifier_printf("machine=%m, boot=%B, host=%H, version=%v", table
, NULL
, &w
);
42 static void test_str_in_set(void) {
43 log_info("/* %s */", __func__
);
45 assert_se(STR_IN_SET("x", "x", "y", "z"));
46 assert_se(!STR_IN_SET("X", "x", "y", "z"));
47 assert_se(!STR_IN_SET("", "x", "y", "z"));
48 assert_se(STR_IN_SET("x", "w", "x"));
51 static void test_strptr_in_set(void) {
52 log_info("/* %s */", __func__
);
54 assert_se(STRPTR_IN_SET("x", "x", "y", "z"));
55 assert_se(!STRPTR_IN_SET("X", "x", "y", "z"));
56 assert_se(!STRPTR_IN_SET("", "x", "y", "z"));
57 assert_se(STRPTR_IN_SET("x", "w", "x"));
59 assert_se(!STRPTR_IN_SET(NULL
, "x", "y", "z"));
60 assert_se(!STRPTR_IN_SET(NULL
, ""));
61 /* strv cannot contain a null, hence the result below */
62 assert_se(!STRPTR_IN_SET(NULL
, NULL
));
65 static void test_startswith_set(void) {
66 log_info("/* %s */", __func__
);
68 assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo"));
69 assert_se(!STARTSWITH_SET("foo", "bar"));
71 assert_se(STARTSWITH_SET("abc", "a", "ab", "abc"));
72 assert_se(STARTSWITH_SET("abc", "ax", "ab", "abc"));
73 assert_se(STARTSWITH_SET("abc", "ax", "abx", "abc"));
74 assert_se(!STARTSWITH_SET("abc", "ax", "abx", "abcx"));
76 assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "foo", "zzz"), "bar"));
77 assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "", "zzz"), "foobar"));
78 assert_se(streq_ptr(STARTSWITH_SET("", "hhh", "kkk", "zzz", ""), ""));
81 static const char* const input_table_multiple
[] = {
88 static const char* const input_table_quoted
[] = {
95 static const char* const input_table_one
[] = {
100 static const char* const input_table_none
[] = {
104 static const char* const input_table_two_empties
[] = {
110 static const char* const input_table_one_empty
[] = {
115 static void test_strv_find(void) {
116 log_info("/* %s */", __func__
);
118 assert_se(strv_find((char **)input_table_multiple
, "three"));
119 assert_se(!strv_find((char **)input_table_multiple
, "four"));
122 static void test_strv_find_prefix(void) {
123 log_info("/* %s */", __func__
);
125 assert_se(strv_find_prefix((char **)input_table_multiple
, "o"));
126 assert_se(strv_find_prefix((char **)input_table_multiple
, "one"));
127 assert_se(strv_find_prefix((char **)input_table_multiple
, ""));
128 assert_se(!strv_find_prefix((char **)input_table_multiple
, "xxx"));
129 assert_se(!strv_find_prefix((char **)input_table_multiple
, "onee"));
132 static void test_strv_find_startswith(void) {
135 log_info("/* %s */", __func__
);
137 r
= strv_find_startswith((char **)input_table_multiple
, "o");
138 assert_se(r
&& streq(r
, "ne"));
140 r
= strv_find_startswith((char **)input_table_multiple
, "one");
141 assert_se(r
&& streq(r
, ""));
143 r
= strv_find_startswith((char **)input_table_multiple
, "");
144 assert_se(r
&& streq(r
, "one"));
146 assert_se(!strv_find_startswith((char **)input_table_multiple
, "xxx"));
147 assert_se(!strv_find_startswith((char **)input_table_multiple
, "onee"));
150 static void test_strv_join(void) {
151 _cleanup_free_
char *p
= NULL
, *q
= NULL
, *r
= NULL
, *s
= NULL
, *t
= NULL
, *v
= NULL
, *w
= NULL
;
153 log_info("/* %s */", __func__
);
155 p
= strv_join((char **)input_table_multiple
, ", ");
157 assert_se(streq(p
, "one, two, three"));
159 q
= strv_join((char **)input_table_multiple
, ";");
161 assert_se(streq(q
, "one;two;three"));
163 r
= strv_join((char **)input_table_multiple
, NULL
);
165 assert_se(streq(r
, "one two three"));
167 s
= strv_join((char **)input_table_one
, ", ");
169 assert_se(streq(s
, "one"));
171 t
= strv_join((char **)input_table_none
, ", ");
173 assert_se(streq(t
, ""));
175 v
= strv_join((char **)input_table_two_empties
, ", ");
177 assert_se(streq(v
, ", "));
179 w
= strv_join((char **)input_table_one_empty
, ", ");
181 assert_se(streq(w
, ""));
184 static void test_strv_join_prefix(void) {
185 _cleanup_free_
char *p
= NULL
, *q
= NULL
, *r
= NULL
, *s
= NULL
, *t
= NULL
, *v
= NULL
, *w
= NULL
;
187 log_info("/* %s */", __func__
);
189 p
= strv_join_prefix((char **)input_table_multiple
, ", ", "foo");
191 assert_se(streq(p
, "fooone, footwo, foothree"));
193 q
= strv_join_prefix((char **)input_table_multiple
, ";", "foo");
195 assert_se(streq(q
, "fooone;footwo;foothree"));
197 r
= strv_join_prefix((char **)input_table_multiple
, NULL
, "foo");
199 assert_se(streq(r
, "fooone footwo foothree"));
201 s
= strv_join_prefix((char **)input_table_one
, ", ", "foo");
203 assert_se(streq(s
, "fooone"));
205 t
= strv_join_prefix((char **)input_table_none
, ", ", "foo");
207 assert_se(streq(t
, ""));
209 v
= strv_join_prefix((char **)input_table_two_empties
, ", ", "foo");
211 assert_se(streq(v
, "foo, foo"));
213 w
= strv_join_prefix((char **)input_table_one_empty
, ", ", "foo");
215 assert_se(streq(w
, "foo"));
218 static void test_strv_unquote(const char *quoted
, char **list
) {
219 _cleanup_strv_free_
char **s
;
220 _cleanup_free_
char *j
;
225 log_info("/* %s */", __func__
);
227 r
= strv_split_extract(&s
, quoted
, WHITESPACE
, EXTRACT_UNQUOTE
);
228 assert_se(r
== (int) strv_length(list
));
230 j
= strv_join(s
, " | ");
235 assert_se(streq(list
[i
++], *t
));
237 assert_se(list
[i
] == NULL
);
240 static void test_invalid_unquote(const char *quoted
) {
244 log_info("/* %s */", __func__
);
246 r
= strv_split_extract(&s
, quoted
, WHITESPACE
, EXTRACT_UNQUOTE
);
247 assert_se(s
== NULL
);
248 assert_se(r
== -EINVAL
);
251 static void test_strv_split(void) {
252 _cleanup_(strv_free_erasep
) char **l
= NULL
;
253 const char str
[] = "one,two,three";
255 log_info("/* %s */", __func__
);
257 l
= strv_split(str
, ",");
259 assert_se(strv_equal(l
, (char**) input_table_multiple
));
263 l
= strv_split(" one two\t three", WHITESPACE
);
265 assert_se(strv_equal(l
, (char**) input_table_multiple
));
269 /* Setting NULL for separator is equivalent to WHITESPACE */
270 l
= strv_split(" one two\t three", NULL
);
272 assert_se(strv_equal(l
, (char**) input_table_multiple
));
276 l
= strv_split_full(" one two\t three", NULL
, 0);
278 assert_se(strv_equal(l
, (char**) input_table_multiple
));
282 l
= strv_split_full(" 'one' \" two\t three \" ' four five'", NULL
, SPLIT_QUOTES
);
284 assert_se(strv_equal(l
, (char**) input_table_quoted
));
288 /* missing last quote ignores the last element. */
289 l
= strv_split_full(" 'one' \" two\t three \" ' four five' ' ignored element ", NULL
, SPLIT_QUOTES
);
291 assert_se(strv_equal(l
, (char**) input_table_quoted
));
295 /* missing last quote, but the last element is _not_ ignored with SPLIT_RELAX. */
296 l
= strv_split_full(" 'one' \" two\t three \" ' four five", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
298 assert_se(strv_equal(l
, (char**) input_table_quoted
));
302 /* missing separator between */
303 l
= strv_split_full(" 'one' \" two\t three \"' four five'", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
305 assert_se(strv_equal(l
, (char**) input_table_quoted
));
309 l
= strv_split_full(" 'one' \" two\t three \"' four five", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
311 assert_se(strv_equal(l
, (char**) input_table_quoted
));
314 static void test_strv_split_empty(void) {
315 _cleanup_strv_free_
char **l
= NULL
;
317 log_info("/* %s */", __func__
);
319 l
= strv_split("", WHITESPACE
);
321 assert_se(strv_isempty(l
));
324 l
= strv_split("", NULL
);
326 assert_se(strv_isempty(l
));
329 l
= strv_split_full("", NULL
, 0);
331 assert_se(strv_isempty(l
));
334 l
= strv_split_full("", NULL
, SPLIT_QUOTES
);
336 assert_se(strv_isempty(l
));
339 l
= strv_split_full("", WHITESPACE
, SPLIT_QUOTES
);
341 assert_se(strv_isempty(l
));
344 l
= strv_split_full("", WHITESPACE
, SPLIT_QUOTES
| SPLIT_RELAX
);
346 assert_se(strv_isempty(l
));
349 l
= strv_split(" ", WHITESPACE
);
351 assert_se(strv_isempty(l
));
354 l
= strv_split(" ", NULL
);
356 assert_se(strv_isempty(l
));
359 l
= strv_split_full(" ", NULL
, 0);
361 assert_se(strv_isempty(l
));
364 l
= strv_split_full(" ", WHITESPACE
, SPLIT_QUOTES
);
366 assert_se(strv_isempty(l
));
369 l
= strv_split_full(" ", NULL
, SPLIT_QUOTES
);
371 assert_se(strv_isempty(l
));
374 l
= strv_split_full(" ", NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
376 assert_se(strv_isempty(l
));
379 static void test_strv_split_extract(void) {
380 _cleanup_strv_free_
char **l
= NULL
;
381 const char *str
= ":foo\\:bar::waldo:";
384 log_info("/* %s */", __func__
);
386 r
= strv_split_extract(&l
, str
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
387 assert_se(r
== (int) strv_length(l
));
388 assert_se(streq_ptr(l
[0], ""));
389 assert_se(streq_ptr(l
[1], "foo:bar"));
390 assert_se(streq_ptr(l
[2], ""));
391 assert_se(streq_ptr(l
[3], "waldo"));
392 assert_se(streq_ptr(l
[4], ""));
393 assert_se(streq_ptr(l
[5], NULL
));
396 static void test_strv_split_newlines(void) {
399 _cleanup_strv_free_
char **l
= NULL
;
400 const char str
[] = "one\ntwo\nthree";
402 log_info("/* %s */", __func__
);
404 l
= strv_split_newlines(str
);
408 assert_se(streq(*s
, input_table_multiple
[i
++]));
412 static void test_strv_split_nulstr(void) {
413 _cleanup_strv_free_
char **l
= NULL
;
414 const char nulstr
[] = "str0\0str1\0str2\0str3\0";
416 log_info("/* %s */", __func__
);
418 l
= strv_split_nulstr (nulstr
);
421 assert_se(streq(l
[0], "str0"));
422 assert_se(streq(l
[1], "str1"));
423 assert_se(streq(l
[2], "str2"));
424 assert_se(streq(l
[3], "str3"));
427 static void test_strv_parse_nulstr(void) {
428 _cleanup_strv_free_
char **l
= NULL
;
429 const char nulstr
[] = "hoge\0hoge2\0hoge3\0\0hoge5\0\0xxx";
431 log_info("/* %s */", __func__
);
433 l
= strv_parse_nulstr(nulstr
, sizeof(nulstr
)-1);
435 puts("Parse nulstr:");
438 assert_se(streq(l
[0], "hoge"));
439 assert_se(streq(l
[1], "hoge2"));
440 assert_se(streq(l
[2], "hoge3"));
441 assert_se(streq(l
[3], ""));
442 assert_se(streq(l
[4], "hoge5"));
443 assert_se(streq(l
[5], ""));
444 assert_se(streq(l
[6], "xxx"));
447 static void test_strv_overlap(void) {
448 const char * const input_table
[] = {
454 const char * const input_table_overlap
[] = {
458 const char * const input_table_unique
[] = {
465 log_info("/* %s */", __func__
);
467 assert_se(strv_overlap((char **)input_table
, (char**)input_table_overlap
));
468 assert_se(!strv_overlap((char **)input_table
, (char**)input_table_unique
));
471 static void test_strv_sort(void) {
472 const char* input_table
[] = {
476 "CAPITAL LETTERS FIRST",
481 log_info("/* %s */", __func__
);
483 strv_sort((char **)input_table
);
485 assert_se(streq(input_table
[0], "CAPITAL LETTERS FIRST"));
486 assert_se(streq(input_table
[1], "apple"));
487 assert_se(streq(input_table
[2], "banana"));
488 assert_se(streq(input_table
[3], "citrus"));
489 assert_se(streq(input_table
[4], "durian"));
492 static void test_strv_extend_strv_concat(void) {
493 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
495 log_info("/* %s */", __func__
);
497 a
= strv_new("without", "suffix");
498 b
= strv_new("with", "suffix");
502 assert_se(strv_extend_strv_concat(&a
, b
, "_suffix") >= 0);
504 assert_se(streq(a
[0], "without"));
505 assert_se(streq(a
[1], "suffix"));
506 assert_se(streq(a
[2], "with_suffix"));
507 assert_se(streq(a
[3], "suffix_suffix"));
510 static void test_strv_extend_strv(void) {
511 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **n
= NULL
;
513 log_info("/* %s */", __func__
);
515 a
= strv_new("abc", "def", "ghi");
516 b
= strv_new("jkl", "mno", "abc", "pqr");
520 assert_se(strv_extend_strv(&a
, b
, true) == 3);
522 assert_se(streq(a
[0], "abc"));
523 assert_se(streq(a
[1], "def"));
524 assert_se(streq(a
[2], "ghi"));
525 assert_se(streq(a
[3], "jkl"));
526 assert_se(streq(a
[4], "mno"));
527 assert_se(streq(a
[5], "pqr"));
528 assert_se(strv_length(a
) == 6);
530 assert_se(strv_extend_strv(&n
, b
, false) >= 0);
531 assert_se(streq(n
[0], "jkl"));
532 assert_se(streq(n
[1], "mno"));
533 assert_se(streq(n
[2], "abc"));
534 assert_se(streq(n
[3], "pqr"));
535 assert_se(strv_length(n
) == 4);
538 static void test_strv_extend(void) {
539 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
541 log_info("/* %s */", __func__
);
543 a
= strv_new("test", "test1");
545 assert_se(strv_extend(&a
, "test2") >= 0);
546 assert_se(strv_extend(&b
, "test3") >= 0);
548 assert_se(streq(a
[0], "test"));
549 assert_se(streq(a
[1], "test1"));
550 assert_se(streq(a
[2], "test2"));
551 assert_se(streq(b
[0], "test3"));
554 static void test_strv_extendf(void) {
555 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
557 log_info("/* %s */", __func__
);
559 a
= strv_new("test", "test1");
561 assert_se(strv_extendf(&a
, "test2 %s %d %s", "foo", 128, "bar") >= 0);
562 assert_se(strv_extendf(&b
, "test3 %s %s %d", "bar", "foo", 128) >= 0);
564 assert_se(streq(a
[0], "test"));
565 assert_se(streq(a
[1], "test1"));
566 assert_se(streq(a
[2], "test2 foo 128 bar"));
567 assert_se(streq(b
[0], "test3 bar foo 128"));
570 static void test_strv_foreach(void) {
571 _cleanup_strv_free_
char **a
;
575 log_info("/* %s */", __func__
);
577 a
= strv_new("one", "two", "three");
580 STRV_FOREACH(check
, a
)
581 assert_se(streq(*check
, input_table_multiple
[i
++]));
584 static void test_strv_foreach_backwards(void) {
585 _cleanup_strv_free_
char **a
;
589 log_info("/* %s */", __func__
);
591 a
= strv_new("one", "two", "three");
595 STRV_FOREACH_BACKWARDS(check
, a
)
596 assert_se(streq_ptr(*check
, input_table_multiple
[i
--]));
598 STRV_FOREACH_BACKWARDS(check
, (char**) NULL
)
599 assert_not_reached("Let's see that we check empty strv right, too.");
601 STRV_FOREACH_BACKWARDS(check
, (char**) { NULL
})
602 assert_not_reached("Let's see that we check empty strv right, too.");
605 static void test_strv_foreach_pair(void) {
606 _cleanup_strv_free_
char **a
= NULL
;
609 log_info("/* %s */", __func__
);
611 a
= strv_new("pair_one", "pair_one",
612 "pair_two", "pair_two",
613 "pair_three", "pair_three");
614 STRV_FOREACH_PAIR(x
, y
, a
)
615 assert_se(streq(*x
, *y
));
618 static void test_strv_from_stdarg_alloca_one(char **l
, const char *first
, ...) {
622 log_info("/* %s */", __func__
);
624 j
= strv_from_stdarg_alloca(first
);
627 assert_se(streq_ptr(l
[i
], j
[i
]));
634 static void test_strv_from_stdarg_alloca(void) {
635 log_info("/* %s */", __func__
);
637 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL
);
638 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL
);
639 test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY
, NULL
);
642 static void test_strv_insert(void) {
643 _cleanup_strv_free_
char **a
= NULL
;
645 log_info("/* %s */", __func__
);
647 assert_se(strv_insert(&a
, 0, strdup("first")) == 0);
648 assert_se(streq(a
[0], "first"));
651 assert_se(strv_insert(&a
, 0, NULL
) == 0);
652 assert_se(streq(a
[0], "first"));
655 assert_se(strv_insert(&a
, 1, strdup("two")) == 0);
656 assert_se(streq(a
[0], "first"));
657 assert_se(streq(a
[1], "two"));
660 assert_se(strv_insert(&a
, 4, strdup("tri")) == 0);
661 assert_se(streq(a
[0], "first"));
662 assert_se(streq(a
[1], "two"));
663 assert_se(streq(a
[2], "tri"));
666 assert_se(strv_insert(&a
, 1, strdup("duo")) == 0);
667 assert_se(streq(a
[0], "first"));
668 assert_se(streq(a
[1], "duo"));
669 assert_se(streq(a
[2], "two"));
670 assert_se(streq(a
[3], "tri"));
674 static void test_strv_push_prepend(void) {
675 _cleanup_strv_free_
char **a
= NULL
;
677 log_info("/* %s */", __func__
);
679 a
= strv_new("foo", "bar", "three");
681 assert_se(strv_push_prepend(&a
, strdup("first")) >= 0);
682 assert_se(streq(a
[0], "first"));
683 assert_se(streq(a
[1], "foo"));
684 assert_se(streq(a
[2], "bar"));
685 assert_se(streq(a
[3], "three"));
688 assert_se(strv_consume_prepend(&a
, strdup("first2")) >= 0);
689 assert_se(streq(a
[0], "first2"));
690 assert_se(streq(a
[1], "first"));
691 assert_se(streq(a
[2], "foo"));
692 assert_se(streq(a
[3], "bar"));
693 assert_se(streq(a
[4], "three"));
697 static void test_strv_push(void) {
698 _cleanup_strv_free_
char **a
= NULL
;
701 log_info("/* %s */", __func__
);
703 assert_se(i
= strdup("foo"));
704 assert_se(strv_push(&a
, i
) >= 0);
706 assert_se(i
= strdup("a"));
707 assert_se(j
= strdup("b"));
708 assert_se(strv_push_pair(&a
, i
, j
) >= 0);
710 assert_se(streq_ptr(a
[0], "foo"));
711 assert_se(streq_ptr(a
[1], "a"));
712 assert_se(streq_ptr(a
[2], "b"));
713 assert_se(streq_ptr(a
[3], NULL
));
716 static void test_strv_equal(void) {
717 _cleanup_strv_free_
char **a
= NULL
;
718 _cleanup_strv_free_
char **b
= NULL
;
719 _cleanup_strv_free_
char **c
= NULL
;
721 log_info("/* %s */", __func__
);
723 a
= strv_new("one", "two", "three");
725 b
= strv_new("one", "two", "three");
727 c
= strv_new("one", "two", "three", "four");
730 assert_se(strv_equal(a
, a
));
731 assert_se(strv_equal(a
, b
));
732 assert_se(strv_equal(NULL
, NULL
));
734 assert_se(!strv_equal(a
, c
));
735 assert_se(!strv_equal(b
, c
));
736 assert_se(!strv_equal(b
, NULL
));
739 static void test_strv_is_uniq(void) {
740 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
, **d
= NULL
;
742 log_info("/* %s */", __func__
);
746 assert_se(strv_is_uniq(a
));
750 assert_se(strv_is_uniq(b
));
752 c
= strv_new("foo", "bar");
754 assert_se(strv_is_uniq(c
));
756 d
= strv_new("foo", "bar", "waldo", "bar", "piep");
758 assert_se(!strv_is_uniq(d
));
761 static void test_strv_reverse(void) {
762 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
, **d
= NULL
;
764 log_info("/* %s */", __func__
);
770 assert_se(strv_isempty(a
));
775 assert_se(streq_ptr(b
[0], "foo"));
776 assert_se(streq_ptr(b
[1], NULL
));
778 c
= strv_new("foo", "bar");
781 assert_se(streq_ptr(c
[0], "bar"));
782 assert_se(streq_ptr(c
[1], "foo"));
783 assert_se(streq_ptr(c
[2], NULL
));
785 d
= strv_new("foo", "bar", "waldo");
788 assert_se(streq_ptr(d
[0], "waldo"));
789 assert_se(streq_ptr(d
[1], "bar"));
790 assert_se(streq_ptr(d
[2], "foo"));
791 assert_se(streq_ptr(d
[3], NULL
));
794 static void test_strv_shell_escape(void) {
795 _cleanup_strv_free_
char **v
= NULL
;
797 log_info("/* %s */", __func__
);
799 v
= strv_new("foo:bar", "bar,baz", "wal\\do");
801 assert_se(strv_shell_escape(v
, ",:"));
802 assert_se(streq_ptr(v
[0], "foo\\:bar"));
803 assert_se(streq_ptr(v
[1], "bar\\,baz"));
804 assert_se(streq_ptr(v
[2], "wal\\\\do"));
805 assert_se(streq_ptr(v
[3], NULL
));
808 static void test_strv_skip_one(char **a
, size_t n
, char **b
) {
810 assert_se(strv_equal(a
, b
));
813 static void test_strv_skip(void) {
814 log_info("/* %s */", __func__
);
816 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
817 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
818 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
819 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL
));
820 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL
));
821 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL
));
823 test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
824 test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL
));
825 test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL
));
827 test_strv_skip_one(STRV_MAKE(NULL
), 0, STRV_MAKE(NULL
));
828 test_strv_skip_one(STRV_MAKE(NULL
), 1, STRV_MAKE(NULL
));
829 test_strv_skip_one(STRV_MAKE(NULL
), 55, STRV_MAKE(NULL
));
832 static void test_strv_extend_n(void) {
833 _cleanup_strv_free_
char **v
= NULL
;
835 log_info("/* %s */", __func__
);
837 v
= strv_new("foo", "bar");
840 assert_se(strv_extend_n(&v
, "waldo", 3) >= 0);
841 assert_se(strv_extend_n(&v
, "piep", 2) >= 0);
843 assert_se(streq(v
[0], "foo"));
844 assert_se(streq(v
[1], "bar"));
845 assert_se(streq(v
[2], "waldo"));
846 assert_se(streq(v
[3], "waldo"));
847 assert_se(streq(v
[4], "waldo"));
848 assert_se(streq(v
[5], "piep"));
849 assert_se(streq(v
[6], "piep"));
850 assert_se(v
[7] == NULL
);
854 assert_se(strv_extend_n(&v
, "foo", 1) >= 0);
855 assert_se(strv_extend_n(&v
, "bar", 0) >= 0);
857 assert_se(streq(v
[0], "foo"));
858 assert_se(v
[1] == NULL
);
861 static void test_strv_make_nulstr_one(char **l
) {
862 _cleanup_free_
char *b
= NULL
, *c
= NULL
;
863 _cleanup_strv_free_
char **q
= NULL
;
864 const char *s
= NULL
;
868 log_info("/* %s */", __func__
);
870 assert_se(strv_make_nulstr(l
, &b
, &n
) >= 0);
871 assert_se(q
= strv_parse_nulstr(b
, n
));
872 assert_se(strv_equal(l
, q
));
874 assert_se(strv_make_nulstr(q
, &c
, &m
) >= 0);
876 assert_se(memcmp(b
, c
, m
) == 0);
879 assert_se(streq(s
, l
[i
++]));
880 assert_se(i
== strv_length(l
));
883 static void test_strv_make_nulstr(void) {
884 log_info("/* %s */", __func__
);
886 test_strv_make_nulstr_one(NULL
);
887 test_strv_make_nulstr_one(STRV_MAKE(NULL
));
888 test_strv_make_nulstr_one(STRV_MAKE("foo"));
889 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar"));
890 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
893 static void test_strv_free_free(void) {
896 log_info("/* %s */", __func__
);
898 assert_se(t
= new(char**, 3));
899 assert_se(t
[0] = strv_new("a", "b"));
900 assert_se(t
[1] = strv_new("c", "d", "e"));
903 t
= strv_free_free(t
);
906 static void test_foreach_string(void) {
907 const char * const t
[] = {
916 log_info("/* %s */", __func__
);
918 FOREACH_STRING(x
, "foo", "bar", "waldo")
919 assert_se(streq_ptr(t
[i
++], x
));
923 FOREACH_STRING(x
, "zzz")
924 assert_se(streq(x
, "zzz"));
927 static void test_strv_fnmatch(void) {
928 _cleanup_strv_free_
char **v
= NULL
;
930 log_info("/* %s */", __func__
);
932 assert_se(!strv_fnmatch(STRV_MAKE_EMPTY
, "a", 0));
934 v
= strv_new("*\\*");
935 assert_se(!strv_fnmatch(v
, "\\", 0));
936 assert_se(strv_fnmatch(v
, "\\", FNM_NOESCAPE
));
939 int main(int argc
, char *argv
[]) {
940 test_specifier_printf();
942 test_strptr_in_set();
943 test_startswith_set();
945 test_strv_foreach_backwards();
946 test_strv_foreach_pair();
948 test_strv_find_prefix();
949 test_strv_find_startswith();
951 test_strv_join_prefix();
953 test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
954 test_strv_unquote("", STRV_MAKE_EMPTY
);
955 test_strv_unquote(" ", STRV_MAKE_EMPTY
);
956 test_strv_unquote(" ", STRV_MAKE_EMPTY
);
957 test_strv_unquote(" x", STRV_MAKE("x"));
958 test_strv_unquote("x ", STRV_MAKE("x"));
959 test_strv_unquote(" x ", STRV_MAKE("x"));
960 test_strv_unquote(" \"x\" ", STRV_MAKE("x"));
961 test_strv_unquote(" 'x' ", STRV_MAKE("x"));
962 test_strv_unquote(" 'x\"' ", STRV_MAKE("x\""));
963 test_strv_unquote(" \"x'\" ", STRV_MAKE("x'"));
964 test_strv_unquote("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
966 /* trailing backslashes */
967 test_strv_unquote(" x\\\\", STRV_MAKE("x\\"));
968 test_invalid_unquote(" x\\");
970 test_invalid_unquote("a --b='c \"d e\"''");
971 test_invalid_unquote("a --b='c \"d e\" '\"");
972 test_invalid_unquote("a --b='c \"d e\"garbage");
973 test_invalid_unquote("'");
974 test_invalid_unquote("\"");
975 test_invalid_unquote("'x'y'g");
978 test_strv_split_empty();
979 test_strv_split_extract();
980 test_strv_split_newlines();
981 test_strv_split_nulstr();
982 test_strv_parse_nulstr();
985 test_strv_extend_strv();
986 test_strv_extend_strv_concat();
989 test_strv_from_stdarg_alloca();
991 test_strv_push_prepend();
996 test_strv_shell_escape();
998 test_strv_extend_n();
999 test_strv_make_nulstr();
1000 test_strv_free_free();
1002 test_foreach_string();
1003 test_strv_fnmatch();