1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
4 #include "locale-util.h"
6 #include "string-util.h"
15 assert_se(streq(string_erase(x
), ""));
17 x
= strdupa_safe("1");
18 assert_se(streq(string_erase(x
), ""));
20 x
= strdupa_safe("123456789");
21 assert_se(streq(string_erase(x
), ""));
23 assert_se(x
[1] == '\0');
24 assert_se(x
[2] == '\0');
25 assert_se(x
[3] == '\0');
26 assert_se(x
[4] == '\0');
27 assert_se(x
[5] == '\0');
28 assert_se(x
[6] == '\0');
29 assert_se(x
[7] == '\0');
30 assert_se(x
[8] == '\0');
31 assert_se(x
[9] == '\0');
34 static void test_free_and_strndup_one(char **t
, const char *src
, size_t l
, const char *expected
, bool change
) {
35 log_debug("%s: \"%s\", \"%s\", %zd (expect \"%s\", %s)",
36 __func__
, strnull(*t
), strnull(src
), l
, strnull(expected
), yes_no(change
));
38 int r
= free_and_strndup(t
, src
, l
);
39 assert_se(streq_ptr(*t
, expected
));
40 assert_se(r
== change
); /* check that change occurs only when necessary */
43 TEST(free_and_strndup
) {
44 static const struct test_case
{
76 {"foobar", 6, "foobar"},
79 _cleanup_free_
char *t
= NULL
;
80 const char *prev_expected
= t
;
82 for (unsigned i
= 0; i
< ELEMENTSOF(cases
); i
++) {
83 test_free_and_strndup_one(&t
,
84 cases
[i
].src
, cases
[i
].len
, cases
[i
].expected
,
85 !streq_ptr(cases
[i
].expected
, prev_expected
));
90 TEST(ascii_strcasecmp_n
) {
91 assert_se(ascii_strcasecmp_n("", "", 0) == 0);
92 assert_se(ascii_strcasecmp_n("", "", 1) == 0);
93 assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
94 assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
95 assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
96 assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
97 assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
98 assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
99 assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
100 assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
101 assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
102 assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
103 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
104 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
105 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
106 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
107 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
109 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
110 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
111 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
112 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
113 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
116 TEST(ascii_strcasecmp_nn
) {
117 assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
118 assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
119 assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
120 assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
122 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
123 assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
124 assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
125 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
126 assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
128 assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
129 assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
130 assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
136 assert_se(streq(cellescape(buf
, 1, ""), ""));
137 assert_se(streq(cellescape(buf
, 1, "1"), ""));
138 assert_se(streq(cellescape(buf
, 1, "12"), ""));
140 assert_se(streq(cellescape(buf
, 2, ""), ""));
141 assert_se(streq(cellescape(buf
, 2, "1"), "1"));
142 assert_se(streq(cellescape(buf
, 2, "12"), "."));
143 assert_se(streq(cellescape(buf
, 2, "123"), "."));
145 assert_se(streq(cellescape(buf
, 3, ""), ""));
146 assert_se(streq(cellescape(buf
, 3, "1"), "1"));
147 assert_se(streq(cellescape(buf
, 3, "12"), "12"));
148 assert_se(streq(cellescape(buf
, 3, "123"), ".."));
149 assert_se(streq(cellescape(buf
, 3, "1234"), ".."));
151 assert_se(streq(cellescape(buf
, 4, ""), ""));
152 assert_se(streq(cellescape(buf
, 4, "1"), "1"));
153 assert_se(streq(cellescape(buf
, 4, "12"), "12"));
154 assert_se(streq(cellescape(buf
, 4, "123"), "123"));
155 assert_se(streq(cellescape(buf
, 4, "1234"), is_locale_utf8() ? "…" : "..."));
156 assert_se(streq(cellescape(buf
, 4, "12345"), is_locale_utf8() ? "…" : "..."));
158 assert_se(streq(cellescape(buf
, 5, ""), ""));
159 assert_se(streq(cellescape(buf
, 5, "1"), "1"));
160 assert_se(streq(cellescape(buf
, 5, "12"), "12"));
161 assert_se(streq(cellescape(buf
, 5, "123"), "123"));
162 assert_se(streq(cellescape(buf
, 5, "1234"), "1234"));
163 assert_se(streq(cellescape(buf
, 5, "12345"), is_locale_utf8() ? "1…" : "1..."));
164 assert_se(streq(cellescape(buf
, 5, "123456"), is_locale_utf8() ? "1…" : "1..."));
166 assert_se(streq(cellescape(buf
, 1, "\020"), ""));
167 assert_se(streq(cellescape(buf
, 2, "\020"), "."));
168 assert_se(streq(cellescape(buf
, 3, "\020"), ".."));
169 assert_se(streq(cellescape(buf
, 4, "\020"), "…"));
170 assert_se(streq(cellescape(buf
, 5, "\020"), "\\020"));
172 assert_se(streq(cellescape(buf
, 5, "1234\020"), "1…"));
173 assert_se(streq(cellescape(buf
, 6, "1234\020"), "12…"));
174 assert_se(streq(cellescape(buf
, 7, "1234\020"), "123…"));
175 assert_se(streq(cellescape(buf
, 8, "1234\020"), "1234…"));
176 assert_se(streq(cellescape(buf
, 9, "1234\020"), "1234\\020"));
178 assert_se(streq(cellescape(buf
, 1, "\t\n"), ""));
179 assert_se(streq(cellescape(buf
, 2, "\t\n"), "."));
180 assert_se(streq(cellescape(buf
, 3, "\t\n"), ".."));
181 assert_se(streq(cellescape(buf
, 4, "\t\n"), "…"));
182 assert_se(streq(cellescape(buf
, 5, "\t\n"), "\\t\\n"));
184 assert_se(streq(cellescape(buf
, 5, "1234\t\n"), "1…"));
185 assert_se(streq(cellescape(buf
, 6, "1234\t\n"), "12…"));
186 assert_se(streq(cellescape(buf
, 7, "1234\t\n"), "123…"));
187 assert_se(streq(cellescape(buf
, 8, "1234\t\n"), "1234…"));
188 assert_se(streq(cellescape(buf
, 9, "1234\t\n"), "1234\\t\\n"));
190 assert_se(streq(cellescape(buf
, 4, "x\t\020\n"), "…"));
191 assert_se(streq(cellescape(buf
, 5, "x\t\020\n"), "x…"));
192 assert_se(streq(cellescape(buf
, 6, "x\t\020\n"), "x…"));
193 assert_se(streq(cellescape(buf
, 7, "x\t\020\n"), "x\\t…"));
194 assert_se(streq(cellescape(buf
, 8, "x\t\020\n"), "x\\t…"));
195 assert_se(streq(cellescape(buf
, 9, "x\t\020\n"), "x\\t…"));
196 assert_se(streq(cellescape(buf
, 10, "x\t\020\n"), "x\\t\\020\\n"));
198 assert_se(streq(cellescape(buf
, 6, "1\011"), "1\\t"));
199 assert_se(streq(cellescape(buf
, 6, "1\020"), "1\\020"));
200 assert_se(streq(cellescape(buf
, 6, "1\020x"), is_locale_utf8() ? "1…" : "1..."));
202 assert_se(streq(cellescape(buf
, 40, "1\020"), "1\\020"));
203 assert_se(streq(cellescape(buf
, 40, "1\020x"), "1\\020x"));
205 assert_se(streq(cellescape(buf
, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'"));
206 assert_se(streq(cellescape(buf
, 6, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
207 assert_se(streq(cellescape(buf
, 7, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
208 assert_se(streq(cellescape(buf
, 8, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b..."));
210 assert_se(streq(cellescape(buf
, sizeof buf
, "1\020"), "1\\020"));
211 assert_se(streq(cellescape(buf
, sizeof buf
, "1\020x"), "1\\020x"));
215 assert_se(streq_ptr(NULL
, NULL
));
216 assert_se(!streq_ptr("abc", "cdef"));
220 char *ret
, input
[] = " hello, waldo. ";
222 ret
= strstrip(input
);
223 assert_se(streq(ret
, "hello, waldo."));
227 _cleanup_free_
char *str
= NULL
;
229 assert_se(strextend(&str
, NULL
));
230 assert_se(streq_ptr(str
, ""));
231 assert_se(strextend(&str
, "", "0", "", "", "123"));
232 assert_se(streq_ptr(str
, "0123"));
233 assert_se(strextend(&str
, "456", "78", "9"));
234 assert_se(streq_ptr(str
, "0123456789"));
237 TEST(strextend_with_separator
) {
238 _cleanup_free_
char *str
= NULL
;
240 assert_se(strextend_with_separator(&str
, NULL
, NULL
));
241 assert_se(streq_ptr(str
, ""));
244 assert_se(strextend_with_separator(&str
, "...", NULL
));
245 assert_se(streq_ptr(str
, ""));
246 assert_se(strextend_with_separator(&str
, "...", NULL
));
247 assert_se(streq_ptr(str
, ""));
250 assert_se(strextend_with_separator(&str
, "xyz", "a", "bb", "ccc"));
251 assert_se(streq_ptr(str
, "axyzbbxyzccc"));
254 assert_se(strextend_with_separator(&str
, ",", "start", "", "1", "234"));
255 assert_se(streq_ptr(str
, "start,,1,234"));
256 assert_se(strextend_with_separator(&str
, ";", "more", "5", "678"));
257 assert_se(streq_ptr(str
, "start,,1,234;more;5;678"));
261 _cleanup_free_
char *one
, *three
, *zero
;
262 one
= strrep("waldo", 1);
263 three
= strrep("waldo", 3);
264 zero
= strrep("waldo", 0);
266 assert_se(streq(one
, "waldo"));
267 assert_se(streq(three
, "waldowaldowaldo"));
268 assert_se(streq(zero
, ""));
271 TEST(string_has_cc
) {
272 assert_se(string_has_cc("abc\1", NULL
));
273 assert_se(string_has_cc("abc\x7f", NULL
));
274 assert_se(string_has_cc("abc\x7f", NULL
));
275 assert_se(string_has_cc("abc\t\x7f", "\t"));
276 assert_se(string_has_cc("abc\t\x7f", "\t"));
277 assert_se(string_has_cc("\x7f", "\t"));
278 assert_se(string_has_cc("\x7f", "\t\a"));
280 assert_se(!string_has_cc("abc\t\t", "\t"));
281 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
282 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
285 TEST(ascii_strlower
) {
286 char a
[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
287 assert_se(streq(ascii_strlower(a
), "aabbcc jk ii od lkjjj kkd lk"));
293 assert_se(strlen(strshorten(s
, 6)) == 6);
294 assert_se(strlen(strshorten(s
, 12)) == 6);
295 assert_se(strlen(strshorten(s
, 2)) == 2);
296 assert_se(strlen(strshorten(s
, 0)) == 0);
302 actual
= strjoina("", "foo", "bar");
303 assert_se(streq(actual
, "foobar"));
305 actual
= strjoina("foo", "bar", "baz");
306 assert_se(streq(actual
, "foobarbaz"));
308 actual
= strjoina("foo", "", "bar", "baz");
309 assert_se(streq(actual
, "foobarbaz"));
311 actual
= strjoina("foo");
312 assert_se(streq(actual
, "foo"));
314 actual
= strjoina(NULL
);
315 assert_se(streq(actual
, ""));
317 actual
= strjoina(NULL
, "foo");
318 assert_se(streq(actual
, ""));
320 actual
= strjoina("foo", NULL
, "bar");
321 assert_se(streq(actual
, "foo"));
323 actual
= strjoina("/sys/fs/cgroup/", "dn", "/a/b/c", "/cgroup.procs");
324 assert_se(streq(actual
, "/sys/fs/cgroup/dn/a/b/c/cgroup.procs"));
326 actual
= strjoina("/sys/fs/cgroup/", "dn", NULL
, NULL
);
327 assert_se(streq(actual
, "/sys/fs/cgroup/dn"));
333 actual
= strjoin("", "foo", "bar");
334 assert_se(streq(actual
, "foobar"));
337 actual
= strjoin("foo", "bar", "baz");
338 assert_se(streq(actual
, "foobarbaz"));
341 actual
= strjoin("foo", "", "bar", "baz");
342 assert_se(streq(actual
, "foobarbaz"));
345 actual
= strjoin("foo", NULL
);
346 assert_se(streq(actual
, "foo"));
349 actual
= strjoin(NULL
, NULL
);
350 assert_se(streq(actual
, ""));
353 actual
= strjoin(NULL
, "foo");
354 assert_se(streq(actual
, ""));
357 actual
= strjoin("foo", NULL
, "bar");
358 assert_se(streq(actual
, "foo"));
363 assert_se(strcmp_ptr(NULL
, NULL
) == 0);
364 assert_se(strcmp_ptr("", NULL
) > 0);
365 assert_se(strcmp_ptr("foo", NULL
) > 0);
366 assert_se(strcmp_ptr(NULL
, "") < 0);
367 assert_se(strcmp_ptr(NULL
, "bar") < 0);
368 assert_se(strcmp_ptr("foo", "bar") > 0);
369 assert_se(strcmp_ptr("bar", "baz") < 0);
370 assert_se(strcmp_ptr("foo", "foo") == 0);
371 assert_se(strcmp_ptr("", "") == 0);
375 const char *test
= "test abc d\te f ";
376 const char * const expected
[] = {
386 for (const char *p
= test
;;) {
387 _cleanup_free_
char *word
= NULL
;
389 r
= extract_first_word(&p
, &word
, NULL
, 0);
391 assert_se(i
== ELEMENTSOF(expected
));
396 assert_se(streq(expected
[i
++], word
));
400 static void check(const char *test
, char** expected
, bool trailing
) {
404 printf("<<<%s>>>\n", test
);
406 _cleanup_free_
char *word
= NULL
;
408 r
= extract_first_word(&test
, &word
, NULL
, EXTRACT_UNQUOTE
);
410 assert_se(!trailing
);
417 assert_se(streq(word
, expected
[i
++]));
418 printf("<%s>\n", word
);
420 assert_se(expected
[i
] == NULL
);
423 TEST(foreach_word_quoted
) {
424 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
449 assert_se(endswith("foobar", "bar"));
450 assert_se(endswith("foobar", ""));
451 assert_se(endswith("foobar", "foobar"));
452 assert_se(endswith("", ""));
454 assert_se(!endswith("foobar", "foo"));
455 assert_se(!endswith("foobar", "foobarfoofoo"));
458 TEST(endswith_no_case
) {
459 assert_se(endswith_no_case("fooBAR", "bar"));
460 assert_se(endswith_no_case("foobar", ""));
461 assert_se(endswith_no_case("foobar", "FOOBAR"));
462 assert_se(endswith_no_case("", ""));
464 assert_se(!endswith_no_case("foobar", "FOO"));
465 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
469 char *s
, input
[] = " hello, waldo. abc";
471 s
= delete_chars(input
, WHITESPACE
);
472 assert_se(streq(s
, "hello,waldo.abc"));
473 assert_se(s
== input
);
476 TEST(delete_trailing_chars
) {
478 input1
[] = " \n \r k \n \r ",
479 input2
[] = "kkkkthiskkkiskkkaktestkkk",
482 s
= delete_trailing_chars(input1
, WHITESPACE
);
483 assert_se(streq(s
, " \n \r k"));
484 assert_se(s
== input1
);
486 s
= delete_trailing_chars(input2
, "kt");
487 assert_se(streq(s
, "kkkkthiskkkiskkkaktes"));
488 assert_se(s
== input2
);
490 s
= delete_trailing_chars(input3
, WHITESPACE
);
491 assert_se(streq(s
, "abcdef"));
492 assert_se(s
== input3
);
494 s
= delete_trailing_chars(input3
, "fe");
495 assert_se(streq(s
, "abcd"));
496 assert_se(s
== input3
);
499 TEST(delete_trailing_slashes
) {
500 char s1
[] = "foobar//",
505 assert_se(streq(delete_trailing_chars(s1
, "_"), "foobar//"));
506 assert_se(streq(delete_trailing_chars(s1
, "/"), "foobar"));
507 assert_se(streq(delete_trailing_chars(s2
, "/"), "foobar"));
508 assert_se(streq(delete_trailing_chars(s3
, "/"), "foobar"));
509 assert_se(streq(delete_trailing_chars(s4
, "/"), ""));
512 TEST(skip_leading_chars
) {
513 char input1
[] = " \n \r k \n \r ",
514 input2
[] = "kkkkthiskkkiskkkaktestkkk",
517 assert_se(streq(skip_leading_chars(input1
, WHITESPACE
), "k \n \r "));
518 assert_se(streq(skip_leading_chars(input2
, "k"), "thiskkkiskkkaktestkkk"));
519 assert_se(streq(skip_leading_chars(input2
, "tk"), "hiskkkiskkkaktestkkk"));
520 assert_se(streq(skip_leading_chars(input3
, WHITESPACE
), "abcdef"));
521 assert_se(streq(skip_leading_chars(input3
, "bcaef"), "def"));
525 assert_se(in_charset("dddaaabbbcccc", "abcd"));
526 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
530 _cleanup_free_
char *a
= NULL
, *b
= NULL
;
532 assert_se(split_pair("", "", &a
, &b
) == -EINVAL
);
533 assert_se(split_pair("foo=bar", "", &a
, &b
) == -EINVAL
);
534 assert_se(split_pair("", "=", &a
, &b
) == -EINVAL
);
535 assert_se(split_pair("foo=bar", "=", &a
, &b
) >= 0);
536 assert_se(streq(a
, "foo"));
537 assert_se(streq(b
, "bar"));
540 assert_se(split_pair("==", "==", &a
, &b
) >= 0);
541 assert_se(streq(a
, ""));
542 assert_se(streq(b
, ""));
546 assert_se(split_pair("===", "==", &a
, &b
) >= 0);
547 assert_se(streq(a
, ""));
548 assert_se(streq(b
, "="));
552 assert_se(first_word("Hello", ""));
553 assert_se(first_word("Hello", "Hello"));
554 assert_se(first_word("Hello world", "Hello"));
555 assert_se(first_word("Hello\tworld", "Hello"));
556 assert_se(first_word("Hello\nworld", "Hello"));
557 assert_se(first_word("Hello\rworld", "Hello"));
558 assert_se(first_word("Hello ", "Hello"));
560 assert_se(!first_word("Hello", "Hellooo"));
561 assert_se(!first_word("Hello", "xxxxx"));
562 assert_se(!first_word("Hellooo", "Hello"));
566 assert_se(strlen_ptr("foo") == 3);
567 assert_se(strlen_ptr("") == 0);
568 assert_se(strlen_ptr(NULL
) == 0);
571 TEST(memory_startswith
) {
572 assert_se(streq(memory_startswith("", 0, ""), ""));
573 assert_se(streq(memory_startswith("", 1, ""), ""));
574 assert_se(streq(memory_startswith("x", 2, ""), "x"));
575 assert_se(!memory_startswith("", 1, "x"));
576 assert_se(!memory_startswith("", 1, "xxxxxxxx"));
577 assert_se(streq(memory_startswith("xxx", 4, "x"), "xx"));
578 assert_se(streq(memory_startswith("xxx", 4, "xx"), "x"));
579 assert_se(streq(memory_startswith("xxx", 4, "xxx"), ""));
580 assert_se(!memory_startswith("xxx", 4, "xxxx"));
583 TEST(memory_startswith_no_case
) {
584 assert_se(streq(memory_startswith_no_case("", 0, ""), ""));
585 assert_se(streq(memory_startswith_no_case("", 1, ""), ""));
586 assert_se(streq(memory_startswith_no_case("x", 2, ""), "x"));
587 assert_se(streq(memory_startswith_no_case("X", 2, ""), "X"));
588 assert_se(!memory_startswith_no_case("", 1, "X"));
589 assert_se(!memory_startswith_no_case("", 1, "xxxxXXXX"));
590 assert_se(streq(memory_startswith_no_case("xxx", 4, "X"), "xx"));
591 assert_se(streq(memory_startswith_no_case("XXX", 4, "x"), "XX"));
592 assert_se(streq(memory_startswith_no_case("XXX", 4, "X"), "XX"));
593 assert_se(streq(memory_startswith_no_case("xxx", 4, "XX"), "x"));
594 assert_se(streq(memory_startswith_no_case("XXX", 4, "xx"), "X"));
595 assert_se(streq(memory_startswith_no_case("XXX", 4, "XX"), "X"));
596 assert_se(streq(memory_startswith_no_case("xxx", 4, "XXX"), ""));
597 assert_se(streq(memory_startswith_no_case("XXX", 4, "xxx"), ""));
598 assert_se(streq(memory_startswith_no_case("XXX", 4, "XXX"), ""));
600 assert_se(memory_startswith_no_case((char[2]){'x', 'x'}, 2, "xx"));
601 assert_se(memory_startswith_no_case((char[2]){'x', 'X'}, 2, "xX"));
602 assert_se(memory_startswith_no_case((char[2]){'X', 'x'}, 2, "Xx"));
603 assert_se(memory_startswith_no_case((char[2]){'X', 'X'}, 2, "XX"));
606 static void test_string_truncate_lines_one(const char *input
, size_t n_lines
, const char *output
, bool truncation
) {
607 _cleanup_free_
char *b
= NULL
;
610 assert_se((k
= string_truncate_lines(input
, n_lines
, &b
)) >= 0);
611 assert_se(streq(b
, output
));
612 assert_se(!!k
== truncation
);
615 TEST(string_truncate_lines
) {
616 test_string_truncate_lines_one("", 0, "", false);
617 test_string_truncate_lines_one("", 1, "", false);
618 test_string_truncate_lines_one("", 2, "", false);
619 test_string_truncate_lines_one("", 3, "", false);
621 test_string_truncate_lines_one("x", 0, "", true);
622 test_string_truncate_lines_one("x", 1, "x", false);
623 test_string_truncate_lines_one("x", 2, "x", false);
624 test_string_truncate_lines_one("x", 3, "x", false);
626 test_string_truncate_lines_one("x\n", 0, "", true);
627 test_string_truncate_lines_one("x\n", 1, "x", false);
628 test_string_truncate_lines_one("x\n", 2, "x", false);
629 test_string_truncate_lines_one("x\n", 3, "x", false);
631 test_string_truncate_lines_one("x\ny", 0, "", true);
632 test_string_truncate_lines_one("x\ny", 1, "x", true);
633 test_string_truncate_lines_one("x\ny", 2, "x\ny", false);
634 test_string_truncate_lines_one("x\ny", 3, "x\ny", false);
636 test_string_truncate_lines_one("x\ny\n", 0, "", true);
637 test_string_truncate_lines_one("x\ny\n", 1, "x", true);
638 test_string_truncate_lines_one("x\ny\n", 2, "x\ny", false);
639 test_string_truncate_lines_one("x\ny\n", 3, "x\ny", false);
641 test_string_truncate_lines_one("x\ny\nz", 0, "", true);
642 test_string_truncate_lines_one("x\ny\nz", 1, "x", true);
643 test_string_truncate_lines_one("x\ny\nz", 2, "x\ny", true);
644 test_string_truncate_lines_one("x\ny\nz", 3, "x\ny\nz", false);
646 test_string_truncate_lines_one("x\ny\nz\n", 0, "", true);
647 test_string_truncate_lines_one("x\ny\nz\n", 1, "x", true);
648 test_string_truncate_lines_one("x\ny\nz\n", 2, "x\ny", true);
649 test_string_truncate_lines_one("x\ny\nz\n", 3, "x\ny\nz", false);
651 test_string_truncate_lines_one("\n", 0, "", false);
652 test_string_truncate_lines_one("\n", 1, "", false);
653 test_string_truncate_lines_one("\n", 2, "", false);
654 test_string_truncate_lines_one("\n", 3, "", false);
656 test_string_truncate_lines_one("\n\n", 0, "", false);
657 test_string_truncate_lines_one("\n\n", 1, "", false);
658 test_string_truncate_lines_one("\n\n", 2, "", false);
659 test_string_truncate_lines_one("\n\n", 3, "", false);
661 test_string_truncate_lines_one("\n\n\n", 0, "", false);
662 test_string_truncate_lines_one("\n\n\n", 1, "", false);
663 test_string_truncate_lines_one("\n\n\n", 2, "", false);
664 test_string_truncate_lines_one("\n\n\n", 3, "", false);
666 test_string_truncate_lines_one("\nx\n\n", 0, "", true);
667 test_string_truncate_lines_one("\nx\n\n", 1, "", true);
668 test_string_truncate_lines_one("\nx\n\n", 2, "\nx", false);
669 test_string_truncate_lines_one("\nx\n\n", 3, "\nx", false);
671 test_string_truncate_lines_one("\n\nx\n", 0, "", true);
672 test_string_truncate_lines_one("\n\nx\n", 1, "", true);
673 test_string_truncate_lines_one("\n\nx\n", 2, "", true);
674 test_string_truncate_lines_one("\n\nx\n", 3, "\n\nx", false);
677 static void test_string_extract_lines_one(const char *input
, size_t i
, const char *output
, bool more
) {
678 _cleanup_free_
char *b
= NULL
;
681 assert_se((k
= string_extract_line(input
, i
, &b
)) >= 0);
682 assert_se(streq(b
?: input
, output
));
683 assert_se(!!k
== more
);
686 TEST(string_extract_line
) {
687 test_string_extract_lines_one("", 0, "", false);
688 test_string_extract_lines_one("", 1, "", false);
689 test_string_extract_lines_one("", 2, "", false);
690 test_string_extract_lines_one("", 3, "", false);
692 test_string_extract_lines_one("x", 0, "x", false);
693 test_string_extract_lines_one("x", 1, "", false);
694 test_string_extract_lines_one("x", 2, "", false);
695 test_string_extract_lines_one("x", 3, "", false);
697 test_string_extract_lines_one("x\n", 0, "x", false);
698 test_string_extract_lines_one("x\n", 1, "", false);
699 test_string_extract_lines_one("x\n", 2, "", false);
700 test_string_extract_lines_one("x\n", 3, "", false);
702 test_string_extract_lines_one("x\ny", 0, "x", true);
703 test_string_extract_lines_one("x\ny", 1, "y", false);
704 test_string_extract_lines_one("x\ny", 2, "", false);
705 test_string_extract_lines_one("x\ny", 3, "", false);
707 test_string_extract_lines_one("x\ny\n", 0, "x", true);
708 test_string_extract_lines_one("x\ny\n", 1, "y", false);
709 test_string_extract_lines_one("x\ny\n", 2, "", false);
710 test_string_extract_lines_one("x\ny\n", 3, "", false);
712 test_string_extract_lines_one("x\ny\nz", 0, "x", true);
713 test_string_extract_lines_one("x\ny\nz", 1, "y", true);
714 test_string_extract_lines_one("x\ny\nz", 2, "z", false);
715 test_string_extract_lines_one("x\ny\nz", 3, "", false);
717 test_string_extract_lines_one("\n", 0, "", false);
718 test_string_extract_lines_one("\n", 1, "", false);
719 test_string_extract_lines_one("\n", 2, "", false);
720 test_string_extract_lines_one("\n", 3, "", false);
722 test_string_extract_lines_one("\n\n", 0, "", true);
723 test_string_extract_lines_one("\n\n", 1, "", false);
724 test_string_extract_lines_one("\n\n", 2, "", false);
725 test_string_extract_lines_one("\n\n", 3, "", false);
727 test_string_extract_lines_one("\n\n\n", 0, "", true);
728 test_string_extract_lines_one("\n\n\n", 1, "", true);
729 test_string_extract_lines_one("\n\n\n", 2, "", false);
730 test_string_extract_lines_one("\n\n\n", 3, "", false);
732 test_string_extract_lines_one("\n\n\n\n", 0, "", true);
733 test_string_extract_lines_one("\n\n\n\n", 1, "", true);
734 test_string_extract_lines_one("\n\n\n\n", 2, "", true);
735 test_string_extract_lines_one("\n\n\n\n", 3, "", false);
737 test_string_extract_lines_one("\nx\n\n\n", 0, "", true);
738 test_string_extract_lines_one("\nx\n\n\n", 1, "x", true);
739 test_string_extract_lines_one("\nx\n\n\n", 2, "", true);
740 test_string_extract_lines_one("\nx\n\n\n", 3, "", false);
742 test_string_extract_lines_one("\n\nx\n\n", 0, "", true);
743 test_string_extract_lines_one("\n\nx\n\n", 1, "", true);
744 test_string_extract_lines_one("\n\nx\n\n", 2, "x", true);
745 test_string_extract_lines_one("\n\nx\n\n", 3, "", false);
747 test_string_extract_lines_one("\n\n\nx\n", 0, "", true);
748 test_string_extract_lines_one("\n\n\nx\n", 1, "", true);
749 test_string_extract_lines_one("\n\n\nx\n", 2, "", true);
750 test_string_extract_lines_one("\n\n\nx\n", 3, "x", false);
753 TEST(string_contains_word_strv
) {
756 assert_se(string_contains_word_strv("a b cc", NULL
, STRV_MAKE("a", "b"), NULL
));
758 assert_se(string_contains_word_strv("a b cc", NULL
, STRV_MAKE("a", "b"), &w
));
759 assert_se(streq(w
, "a"));
761 assert_se(!string_contains_word_strv("a b cc", NULL
, STRV_MAKE("d"), &w
));
762 assert_se(w
== NULL
);
764 assert_se(string_contains_word_strv("a b cc", NULL
, STRV_MAKE("b", "a"), &w
));
765 assert_se(streq(w
, "a"));
767 assert_se(string_contains_word_strv("b a b cc", NULL
, STRV_MAKE("b", "a", "b"), &w
));
768 assert_se(streq(w
, "b"));
770 assert_se(string_contains_word_strv("a b cc", NULL
, STRV_MAKE("b", ""), &w
));
771 assert_se(streq(w
, "b"));
773 assert_se(!string_contains_word_strv("a b cc", NULL
, STRV_MAKE(""), &w
));
774 assert_se(w
== NULL
);
776 assert_se(string_contains_word_strv("a b cc", " ", STRV_MAKE(""), &w
));
777 assert_se(streq(w
, ""));
780 TEST(string_contains_word
) {
781 assert_se( string_contains_word("a b cc", NULL
, "a"));
782 assert_se( string_contains_word("a b cc", NULL
, "b"));
783 assert_se(!string_contains_word("a b cc", NULL
, "c"));
784 assert_se( string_contains_word("a b cc", NULL
, "cc"));
785 assert_se(!string_contains_word("a b cc", NULL
, "d"));
786 assert_se(!string_contains_word("a b cc", NULL
, "a b"));
787 assert_se(!string_contains_word("a b cc", NULL
, "a b c"));
788 assert_se(!string_contains_word("a b cc", NULL
, "b c"));
789 assert_se(!string_contains_word("a b cc", NULL
, "b cc"));
790 assert_se(!string_contains_word("a b cc", NULL
, "a "));
791 assert_se(!string_contains_word("a b cc", NULL
, " b "));
792 assert_se(!string_contains_word("a b cc", NULL
, " cc"));
794 assert_se( string_contains_word(" a b\t\tcc", NULL
, "a"));
795 assert_se( string_contains_word(" a b\t\tcc", NULL
, "b"));
796 assert_se(!string_contains_word(" a b\t\tcc", NULL
, "c"));
797 assert_se( string_contains_word(" a b\t\tcc", NULL
, "cc"));
798 assert_se(!string_contains_word(" a b\t\tcc", NULL
, "d"));
799 assert_se(!string_contains_word(" a b\t\tcc", NULL
, "a b"));
800 assert_se(!string_contains_word(" a b\t\tcc", NULL
, "a b\t\tc"));
801 assert_se(!string_contains_word(" a b\t\tcc", NULL
, "b\t\tc"));
802 assert_se(!string_contains_word(" a b\t\tcc", NULL
, "b\t\tcc"));
803 assert_se(!string_contains_word(" a b\t\tcc", NULL
, "a "));
804 assert_se(!string_contains_word(" a b\t\tcc", NULL
, " b "));
805 assert_se(!string_contains_word(" a b\t\tcc", NULL
, " cc"));
807 assert_se(!string_contains_word(" a b\t\tcc", NULL
, ""));
808 assert_se(!string_contains_word(" a b\t\tcc", NULL
, " "));
809 assert_se(!string_contains_word(" a b\t\tcc", NULL
, " "));
810 assert_se( string_contains_word(" a b\t\tcc", " ", ""));
811 assert_se( string_contains_word(" a b\t\tcc", "\t", ""));
812 assert_se( string_contains_word(" a b\t\tcc", WHITESPACE
, ""));
814 assert_se( string_contains_word("a:b:cc", ":#", "a"));
815 assert_se( string_contains_word("a:b:cc", ":#", "b"));
816 assert_se(!string_contains_word("a:b:cc", ":#", "c"));
817 assert_se( string_contains_word("a:b:cc", ":#", "cc"));
818 assert_se(!string_contains_word("a:b:cc", ":#", "d"));
819 assert_se(!string_contains_word("a:b:cc", ":#", "a:b"));
820 assert_se(!string_contains_word("a:b:cc", ":#", "a:b:c"));
821 assert_se(!string_contains_word("a:b:cc", ":#", "b:c"));
822 assert_se(!string_contains_word("a#b#cc", ":#", "b:cc"));
823 assert_se( string_contains_word("a#b#cc", ":#", "b"));
824 assert_se( string_contains_word("a#b#cc", ":#", "cc"));
825 assert_se(!string_contains_word("a:b:cc", ":#", "a:"));
826 assert_se(!string_contains_word("a:b cc", ":#", "b"));
827 assert_se( string_contains_word("a:b cc", ":#", "b cc"));
828 assert_se(!string_contains_word("a:b:cc", ":#", ":cc"));
831 static void test_strverscmp_improved_one(const char *newer
, const char *older
) {
832 log_info("/* %s(%s, %s) */", __func__
, strnull(newer
), strnull(older
));
834 assert_se(strverscmp_improved(newer
, newer
) == 0);
835 assert_se(strverscmp_improved(newer
, older
) > 0);
836 assert_se(strverscmp_improved(older
, newer
) < 0);
837 assert_se(strverscmp_improved(older
, older
) == 0);
840 TEST(strverscmp_improved
) {
841 static const char * const versions
[] = {
868 const char * const *p
, * const *q
;
870 STRV_FOREACH(p
, versions
)
871 STRV_FOREACH(q
, p
+ 1)
872 test_strverscmp_improved_one(*q
, *p
);
874 test_strverscmp_improved_one("123.45-67.89", "123.45-67.88");
875 test_strverscmp_improved_one("123.45-67.89a", "123.45-67.89");
876 test_strverscmp_improved_one("123.45-67.89", "123.45-67.ab");
877 test_strverscmp_improved_one("123.45-67.89", "123.45-67.9");
878 test_strverscmp_improved_one("123.45-67.89", "123.45-67");
879 test_strverscmp_improved_one("123.45-67.89", "123.45-66.89");
880 test_strverscmp_improved_one("123.45-67.89", "123.45-9.99");
881 test_strverscmp_improved_one("123.45-67.89", "123.42-99.99");
882 test_strverscmp_improved_one("123.45-67.89", "123-99.99");
884 /* '~' : pre-releases */
885 test_strverscmp_improved_one("123.45-67.89", "123~rc1-99.99");
886 test_strverscmp_improved_one("123-45.67.89", "123~rc1-99.99");
887 test_strverscmp_improved_one("123~rc2-67.89", "123~rc1-99.99");
888 test_strverscmp_improved_one("123^aa2-67.89", "123~rc1-99.99");
889 test_strverscmp_improved_one("123aa2-67.89", "123~rc1-99.99");
891 /* '-' : separator between version and release. */
892 test_strverscmp_improved_one("123.45-67.89", "123-99.99");
893 test_strverscmp_improved_one("123^aa2-67.89", "123-99.99");
894 test_strverscmp_improved_one("123aa2-67.89", "123-99.99");
896 /* '^' : patch releases */
897 test_strverscmp_improved_one("123.45-67.89", "123^45-67.89");
898 test_strverscmp_improved_one("123^aa2-67.89", "123^aa1-99.99");
899 test_strverscmp_improved_one("123aa2-67.89", "123^aa2-67.89");
901 /* '.' : point release */
902 test_strverscmp_improved_one("123aa2-67.89", "123.aa2-67.89");
903 test_strverscmp_improved_one("123.ab2-67.89", "123.aa2-67.89");
905 /* invalid characters */
906 assert_se(strverscmp_improved("123_aa2-67.89", "123aa+2-67.89") == 0);
910 _cleanup_free_
char *p
= NULL
;
912 assert_se(strextendf(&p
, "<%i>", 77) >= 0);
913 assert_se(streq(p
, "<77>"));
915 assert_se(strextendf(&p
, "<%i>", 99) >= 0);
916 assert_se(streq(p
, "<77><99>"));
918 assert_se(strextendf(&p
, "<%80i>", 88) >= 0);
919 assert_se(streq(p
, "<77><99>< 88>"));
921 assert_se(strextendf(&p
, "<%08x>", 0x1234) >= 0);
922 assert_se(streq(p
, "<77><99>< 88><00001234>"));
926 assert_se(strextendf_with_separator(&p
, ",", "<%i>", 77) >= 0);
927 assert_se(streq(p
, "<77>"));
929 assert_se(strextendf_with_separator(&p
, ",", "<%i>", 99) >= 0);
930 assert_se(streq(p
, "<77>,<99>"));
932 assert_se(strextendf_with_separator(&p
, ",", "<%80i>", 88) >= 0);
933 assert_se(streq(p
, "<77>,<99>,< 88>"));
935 assert_se(strextendf_with_separator(&p
, ",", "<%08x>", 0x1234) >= 0);
936 assert_se(streq(p
, "<77>,<99>,< 88>,<00001234>"));
939 TEST(streq_skip_trailing_chars
) {
940 /* NULL is WHITESPACE by default*/
941 assert_se(streq_skip_trailing_chars("foo bar", "foo bar", NULL
));
942 assert_se(streq_skip_trailing_chars("foo", "foo", NULL
));
943 assert_se(streq_skip_trailing_chars("foo bar ", "foo bar", NULL
));
944 assert_se(streq_skip_trailing_chars("foo bar", "foo bar\t\t", NULL
));
945 assert_se(streq_skip_trailing_chars("foo bar ", "foo bar\t\t", NULL
));
946 assert_se(streq_skip_trailing_chars("foo\nbar", "foo\nbar", NULL
));
947 assert_se(streq_skip_trailing_chars("\t\tfoo bar", "\t\tfoo bar", NULL
));
948 assert_se(streq_skip_trailing_chars(" foo bar\t", " foo bar\n", NULL
));
950 assert_se(!streq_skip_trailing_chars("foobar", "foo bar", NULL
));
951 assert_se(!streq_skip_trailing_chars("foo\nbar", "foo\tbar", NULL
));
952 assert_se(!streq_skip_trailing_chars("\t\nfoo bar", "\t foo bar", NULL
));
954 assert_se(streq_skip_trailing_chars("foo bar ", "foo bar", WHITESPACE
));
955 assert_se(!streq_skip_trailing_chars("foo bar ", "foo bar", NEWLINE
));
957 assert_se(streq_skip_trailing_chars(NULL
, NULL
, NULL
));
958 assert_se(streq_skip_trailing_chars("", "", NULL
));
959 assert_se(!streq_skip_trailing_chars(NULL
, "foo bar", NULL
));
960 assert_se(!streq_skip_trailing_chars("foo", NULL
, NULL
));
961 assert_se(!streq_skip_trailing_chars("", "f", NULL
));
964 DEFINE_TEST_MAIN(LOG_DEBUG
);