1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
8 #include "alloc-util.h"
9 //#include "locale-util.h"
11 #include "string-util.h"
14 static void test_string_erase(void) {
18 assert_se(streq(string_erase(x), ""));
21 assert_se(streq(string_erase(x), ""));
23 x = strdupa("123456789");
24 assert_se(streq(string_erase(x), ""));
26 assert_se(x[1] == '\0');
27 assert_se(x[2] == '\0');
28 assert_se(x[3] == '\0');
29 assert_se(x[4] == '\0');
30 assert_se(x[5] == '\0');
31 assert_se(x[6] == '\0');
32 assert_se(x[7] == '\0');
33 assert_se(x[8] == '\0');
34 assert_se(x[9] == '\0');
37 #if 0 /// UNNEEDED by elogind
38 static void test_ascii_strcasecmp_n(void) {
40 assert_se(ascii_strcasecmp_n("", "", 0) == 0);
41 assert_se(ascii_strcasecmp_n("", "", 1) == 0);
42 assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
43 assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
44 assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
45 assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
46 assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
47 assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
48 assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
49 assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
50 assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
51 assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
52 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
53 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
54 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
55 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
56 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
58 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
59 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
60 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
61 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
62 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
65 static void test_ascii_strcasecmp_nn(void) {
66 assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
67 assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
68 assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
69 assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
71 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
72 assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
73 assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
74 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
75 assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
77 assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
78 assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
79 assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
83 static void test_cellescape(void) {
86 assert_se(streq(cellescape(buf, 10, "1"), "1"));
87 assert_se(streq(cellescape(buf, 10, "12"), "12"));
88 assert_se(streq(cellescape(buf, 10, "123"), is_locale_utf8() ? "1…" : "1..."));
90 assert_se(streq(cellescape(buf, 10, "1\011"), "1\\t"));
91 assert_se(streq(cellescape(buf, 10, "1\020"), "1\\020"));
92 assert_se(streq(cellescape(buf, 10, "1\020x"), is_locale_utf8() ? "1…" : "1..."));
94 assert_se(streq(cellescape(buf, 40, "1\020"), "1\\020"));
95 assert_se(streq(cellescape(buf, 40, "1\020x"), "1\\020x"));
97 assert_se(streq(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'"));
98 assert_se(streq(cellescape(buf, 10, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
99 assert_se(streq(cellescape(buf, 11, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
100 assert_se(streq(cellescape(buf, 12, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b..."));
102 assert_se(streq(cellescape(buf, sizeof buf, "1\020"), "1\\020"));
103 assert_se(streq(cellescape(buf, sizeof buf, "1\020x"), "1\\020x"));
106 static void test_streq_ptr(void) {
107 assert_se(streq_ptr(NULL, NULL));
108 assert_se(!streq_ptr("abc", "cdef"));
111 static void test_strstrip(void) {
113 char input[] = " hello, waldo. ";
116 assert_se(streq(r, "hello, waldo."));
119 static void test_strextend(void) {
120 _cleanup_free_ char *str = NULL;
122 assert_se(strextend(&str, NULL));
123 assert_se(streq_ptr(str, ""));
124 assert_se(strextend(&str, "", "0", "", "", "123", NULL));
125 assert_se(streq_ptr(str, "0123"));
126 assert_se(strextend(&str, "456", "78", "9", NULL));
127 assert_se(streq_ptr(str, "0123456789"));
130 static void test_strextend_with_separator(void) {
131 _cleanup_free_ char *str = NULL;
133 assert_se(strextend_with_separator(&str, NULL, NULL));
134 assert_se(streq_ptr(str, ""));
137 assert_se(strextend_with_separator(&str, "...", NULL));
138 assert_se(streq_ptr(str, ""));
139 assert_se(strextend_with_separator(&str, "...", NULL));
140 assert_se(streq_ptr(str, ""));
143 assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc", NULL));
144 assert_se(streq_ptr(str, "axyzbbxyzccc"));
147 assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234", NULL));
148 assert_se(streq_ptr(str, "start,,1,234"));
149 assert_se(strextend_with_separator(&str, ";", "more", "5", "678", NULL));
150 assert_se(streq_ptr(str, "start,,1,234;more;5;678"));
153 static void test_strrep(void) {
154 _cleanup_free_ char *one, *three, *zero;
155 one = strrep("waldo", 1);
156 three = strrep("waldo", 3);
157 zero = strrep("waldo", 0);
159 assert_se(streq(one, "waldo"));
160 assert_se(streq(three, "waldowaldowaldo"));
161 assert_se(streq(zero, ""));
164 static void test_strappend(void) {
165 _cleanup_free_ char *t1, *t2, *t3, *t4;
167 t1 = strappend(NULL, NULL);
168 assert_se(streq(t1, ""));
170 t2 = strappend(NULL, "suf");
171 assert_se(streq(t2, "suf"));
173 t3 = strappend("pre", NULL);
174 assert_se(streq(t3, "pre"));
176 t4 = strappend("pre", "suf");
177 assert_se(streq(t4, "presuf"));
180 static void test_string_has_cc(void) {
181 assert_se(string_has_cc("abc\1", NULL));
182 assert_se(string_has_cc("abc\x7f", NULL));
183 assert_se(string_has_cc("abc\x7f", NULL));
184 assert_se(string_has_cc("abc\t\x7f", "\t"));
185 assert_se(string_has_cc("abc\t\x7f", "\t"));
186 assert_se(string_has_cc("\x7f", "\t"));
187 assert_se(string_has_cc("\x7f", "\t\a"));
189 assert_se(!string_has_cc("abc\t\t", "\t"));
190 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
191 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
194 #if 0 /// UNNEEDED by elogind
195 static void test_ascii_strlower(void) {
196 char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
197 assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
201 static void test_strshorten(void) {
204 assert_se(strlen(strshorten(s, 6)) == 6);
205 assert_se(strlen(strshorten(s, 12)) == 6);
206 assert_se(strlen(strshorten(s, 2)) == 2);
207 assert_se(strlen(strshorten(s, 0)) == 0);
210 static void test_strjoina(void) {
213 actual = strjoina("", "foo", "bar");
214 assert_se(streq(actual, "foobar"));
216 actual = strjoina("foo", "bar", "baz");
217 assert_se(streq(actual, "foobarbaz"));
219 actual = strjoina("foo", "", "bar", "baz");
220 assert_se(streq(actual, "foobarbaz"));
222 actual = strjoina("foo");
223 assert_se(streq(actual, "foo"));
225 actual = strjoina(NULL);
226 assert_se(streq(actual, ""));
228 actual = strjoina(NULL, "foo");
229 assert_se(streq(actual, ""));
231 actual = strjoina("foo", NULL, "bar");
232 assert_se(streq(actual, "foo"));
235 static void test_strcmp_ptr(void) {
236 assert_se(strcmp_ptr(NULL, NULL) == 0);
237 assert_se(strcmp_ptr("", NULL) > 0);
238 assert_se(strcmp_ptr("foo", NULL) > 0);
239 assert_se(strcmp_ptr(NULL, "") < 0);
240 assert_se(strcmp_ptr(NULL, "bar") < 0);
241 assert_se(strcmp_ptr("foo", "bar") > 0);
242 assert_se(strcmp_ptr("bar", "baz") < 0);
243 assert_se(strcmp_ptr("foo", "foo") == 0);
244 assert_se(strcmp_ptr("", "") == 0);
247 static void test_foreach_word(void) {
248 const char *word, *state;
251 const char test[] = "test abc d\te f ";
252 const char * const expected[] = {
262 FOREACH_WORD(word, l, test, state)
263 assert_se(strneq(expected[i++], word, l));
266 static void check(const char *test, char** expected, bool trailing) {
269 printf("<<<%s>>>\n", test);
271 _cleanup_free_ char *word = NULL;
273 r = extract_first_word(&test, &word, NULL, EXTRACT_QUOTES);
275 assert_se(!trailing);
282 assert_se(streq(word, expected[i++]));
283 printf("<%s>\n", word);
285 assert_se(expected[i] == NULL);
288 static void test_foreach_word_quoted(void) {
289 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
313 static void test_endswith(void) {
314 assert_se(endswith("foobar", "bar"));
315 assert_se(endswith("foobar", ""));
316 assert_se(endswith("foobar", "foobar"));
317 assert_se(endswith("", ""));
319 assert_se(!endswith("foobar", "foo"));
320 assert_se(!endswith("foobar", "foobarfoofoo"));
323 static void test_endswith_no_case(void) {
324 assert_se(endswith_no_case("fooBAR", "bar"));
325 assert_se(endswith_no_case("foobar", ""));
326 assert_se(endswith_no_case("foobar", "FOOBAR"));
327 assert_se(endswith_no_case("", ""));
329 assert_se(!endswith_no_case("foobar", "FOO"));
330 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
333 #if 0 /// UNNEEDED by elogind
334 static void test_delete_chars(void) {
335 char *s, input[] = " hello, waldo. abc";
337 s = delete_chars(input, WHITESPACE);
338 assert_se(streq(s, "hello,waldo.abc"));
339 assert_se(s == input);
343 static void test_delete_trailing_chars(void) {
346 input1[] = " \n \r k \n \r ",
347 input2[] = "kkkkthiskkkiskkkaktestkkk",
350 s = delete_trailing_chars(input1, WHITESPACE);
351 assert_se(streq(s, " \n \r k"));
352 assert_se(s == input1);
354 s = delete_trailing_chars(input2, "kt");
355 assert_se(streq(s, "kkkkthiskkkiskkkaktes"));
356 assert_se(s == input2);
358 s = delete_trailing_chars(input3, WHITESPACE);
359 assert_se(streq(s, "abcdef"));
360 assert_se(s == input3);
362 s = delete_trailing_chars(input3, "fe");
363 assert_se(streq(s, "abcd"));
364 assert_se(s == input3);
367 static void test_delete_trailing_slashes(void) {
368 char s1[] = "foobar//",
373 assert_se(streq(delete_trailing_chars(s1, "_"), "foobar//"));
374 assert_se(streq(delete_trailing_chars(s1, "/"), "foobar"));
375 assert_se(streq(delete_trailing_chars(s2, "/"), "foobar"));
376 assert_se(streq(delete_trailing_chars(s3, "/"), "foobar"));
377 assert_se(streq(delete_trailing_chars(s4, "/"), ""));
380 #if 0 /// UNNEEDED by elogind
381 static void test_skip_leading_chars(void) {
382 char input1[] = " \n \r k \n \r ",
383 input2[] = "kkkkthiskkkiskkkaktestkkk",
386 assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r "));
387 assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk"));
388 assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk"));
389 assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef"));
390 assert_se(streq(skip_leading_chars(input3, "bcaef"), "def"));
394 static void test_in_charset(void) {
395 assert_se(in_charset("dddaaabbbcccc", "abcd"));
396 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
399 static void test_split_pair(void) {
400 _cleanup_free_ char *a = NULL, *b = NULL;
402 assert_se(split_pair("", "", &a, &b) == -EINVAL);
403 assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
404 assert_se(split_pair("", "=", &a, &b) == -EINVAL);
405 assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
406 assert_se(streq(a, "foo"));
407 assert_se(streq(b, "bar"));
410 assert_se(split_pair("==", "==", &a, &b) >= 0);
411 assert_se(streq(a, ""));
412 assert_se(streq(b, ""));
416 assert_se(split_pair("===", "==", &a, &b) >= 0);
417 assert_se(streq(a, ""));
418 assert_se(streq(b, "="));
421 static void test_first_word(void) {
422 assert_se(first_word("Hello", ""));
423 assert_se(first_word("Hello", "Hello"));
424 assert_se(first_word("Hello world", "Hello"));
425 assert_se(first_word("Hello\tworld", "Hello"));
426 assert_se(first_word("Hello\nworld", "Hello"));
427 assert_se(first_word("Hello\rworld", "Hello"));
428 assert_se(first_word("Hello ", "Hello"));
430 assert_se(!first_word("Hello", "Hellooo"));
431 assert_se(!first_word("Hello", "xxxxx"));
432 assert_se(!first_word("Hellooo", "Hello"));
435 static void test_strlen_ptr(void) {
436 assert_se(strlen_ptr("foo") == 3);
437 assert_se(strlen_ptr("") == 0);
438 assert_se(strlen_ptr(NULL) == 0);
441 static void test_memory_startswith(void) {
442 assert_se(streq(memory_startswith("", 0, ""), ""));
443 assert_se(streq(memory_startswith("", 1, ""), ""));
444 assert_se(streq(memory_startswith("x", 2, ""), "x"));
445 assert_se(!memory_startswith("", 1, "x"));
446 assert_se(!memory_startswith("", 1, "xxxxxxxx"));
447 assert_se(streq(memory_startswith("xxx", 4, "x"), "xx"));
448 assert_se(streq(memory_startswith("xxx", 4, "xx"), "x"));
449 assert_se(streq(memory_startswith("xxx", 4, "xxx"), ""));
450 assert_se(!memory_startswith("xxx", 4, "xxxx"));
453 int main(int argc, char *argv[]) {
455 #if 0 /// UNNEEDED by elogind
456 test_ascii_strcasecmp_n();
457 test_ascii_strcasecmp_nn();
463 test_strextend_with_separator();
466 test_string_has_cc();
467 #if 0 /// UNNEEDED by elogind
468 test_ascii_strlower();
474 test_foreach_word_quoted();
476 test_endswith_no_case();
477 #if 0 /// UNNEEDED by elogind
480 test_delete_trailing_chars();
481 test_delete_trailing_slashes();
482 #if 0 /// UNNEEDED by elogind
483 test_skip_leading_chars();
489 test_memory_startswith();