chiark / gitweb /
tree-wide: drop spurious newlines (#8764)
[elogind.git] / src / test / test-string-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2015 Lennart Poettering
6 ***/
7
8 #include "alloc-util.h"
9 #include "macro.h"
10 #include "string-util.h"
11 #include "strv.h"
12
13 static void test_string_erase(void) {
14         char *x;
15
16         x = strdupa("");
17         assert_se(streq(string_erase(x), ""));
18
19         x = strdupa("1");
20         assert_se(streq(string_erase(x), ""));
21
22         x = strdupa("123456789");
23         assert_se(streq(string_erase(x), ""));
24
25         assert_se(x[1] == '\0');
26         assert_se(x[2] == '\0');
27         assert_se(x[3] == '\0');
28         assert_se(x[4] == '\0');
29         assert_se(x[5] == '\0');
30         assert_se(x[6] == '\0');
31         assert_se(x[7] == '\0');
32         assert_se(x[8] == '\0');
33         assert_se(x[9] == '\0');
34 }
35
36 #if 0 /// UNNEEDED by elogind
37 static void test_ascii_strcasecmp_n(void) {
38
39         assert_se(ascii_strcasecmp_n("", "", 0) == 0);
40         assert_se(ascii_strcasecmp_n("", "", 1) == 0);
41         assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
42         assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
43         assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
44         assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
45         assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
46         assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
47         assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
48         assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
49         assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
50         assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
51         assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
52         assert_se(ascii_strcasecmp_n("xxxxxxxxx", "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
57         assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
58         assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 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 }
63
64 static void test_ascii_strcasecmp_nn(void) {
65         assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
66         assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
67         assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
68         assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
69
70         assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
71         assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
72         assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
73         assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
74         assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
75
76         assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
77         assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
78         assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
79 }
80 #endif // 0
81
82 static void test_streq_ptr(void) {
83         assert_se(streq_ptr(NULL, NULL));
84         assert_se(!streq_ptr("abc", "cdef"));
85 }
86
87 static void test_strstrip(void) {
88         char *r;
89         char input[] = "   hello, waldo.   ";
90
91         r = strstrip(input);
92         assert_se(streq(r, "hello, waldo."));
93 }
94
95 static void test_strextend(void) {
96         _cleanup_free_ char *str = NULL;
97
98         assert_se(strextend(&str, NULL));
99         assert_se(streq_ptr(str, ""));
100         assert_se(strextend(&str, "", "0", "", "", "123", NULL));
101         assert_se(streq_ptr(str, "0123"));
102         assert_se(strextend(&str, "456", "78", "9", NULL));
103         assert_se(streq_ptr(str, "0123456789"));
104 }
105
106 static void test_strextend_with_separator(void) {
107         _cleanup_free_ char *str = NULL;
108
109         assert_se(strextend_with_separator(&str, NULL, NULL));
110         assert_se(streq_ptr(str, ""));
111         str = mfree(str);
112
113         assert_se(strextend_with_separator(&str, "...", NULL));
114         assert_se(streq_ptr(str, ""));
115         assert_se(strextend_with_separator(&str, "...", NULL));
116         assert_se(streq_ptr(str, ""));
117         str = mfree(str);
118
119         assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc", NULL));
120         assert_se(streq_ptr(str, "axyzbbxyzccc"));
121         str = mfree(str);
122
123         assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234", NULL));
124         assert_se(streq_ptr(str, "start,,1,234"));
125         assert_se(strextend_with_separator(&str, ";", "more", "5", "678", NULL));
126         assert_se(streq_ptr(str, "start,,1,234;more;5;678"));
127 }
128
129 static void test_strrep(void) {
130         _cleanup_free_ char *one, *three, *zero;
131         one = strrep("waldo", 1);
132         three = strrep("waldo", 3);
133         zero = strrep("waldo", 0);
134
135         assert_se(streq(one, "waldo"));
136         assert_se(streq(three, "waldowaldowaldo"));
137         assert_se(streq(zero, ""));
138 }
139
140 static void test_strappend(void) {
141         _cleanup_free_ char *t1, *t2, *t3, *t4;
142
143         t1 = strappend(NULL, NULL);
144         assert_se(streq(t1, ""));
145
146         t2 = strappend(NULL, "suf");
147         assert_se(streq(t2, "suf"));
148
149         t3 = strappend("pre", NULL);
150         assert_se(streq(t3, "pre"));
151
152         t4 = strappend("pre", "suf");
153         assert_se(streq(t4, "presuf"));
154 }
155
156 static void test_string_has_cc(void) {
157         assert_se(string_has_cc("abc\1", NULL));
158         assert_se(string_has_cc("abc\x7f", NULL));
159         assert_se(string_has_cc("abc\x7f", NULL));
160         assert_se(string_has_cc("abc\t\x7f", "\t"));
161         assert_se(string_has_cc("abc\t\x7f", "\t"));
162         assert_se(string_has_cc("\x7f", "\t"));
163         assert_se(string_has_cc("\x7f", "\t\a"));
164
165         assert_se(!string_has_cc("abc\t\t", "\t"));
166         assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
167         assert_se(!string_has_cc("a\ab\tc", "\t\a"));
168 }
169
170 #if 0 /// UNNEEDED by elogind
171 static void test_ascii_strlower(void) {
172         char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
173         assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
174 }
175 #endif // 0
176
177 static void test_strshorten(void) {
178         char s[] = "foobar";
179
180         assert_se(strlen(strshorten(s, 6)) == 6);
181         assert_se(strlen(strshorten(s, 12)) == 6);
182         assert_se(strlen(strshorten(s, 2)) == 2);
183         assert_se(strlen(strshorten(s, 0)) == 0);
184 }
185
186 static void test_strjoina(void) {
187         char *actual;
188
189         actual = strjoina("", "foo", "bar");
190         assert_se(streq(actual, "foobar"));
191
192         actual = strjoina("foo", "bar", "baz");
193         assert_se(streq(actual, "foobarbaz"));
194
195         actual = strjoina("foo", "", "bar", "baz");
196         assert_se(streq(actual, "foobarbaz"));
197
198         actual = strjoina("foo");
199         assert_se(streq(actual, "foo"));
200
201         actual = strjoina(NULL);
202         assert_se(streq(actual, ""));
203
204         actual = strjoina(NULL, "foo");
205         assert_se(streq(actual, ""));
206
207         actual = strjoina("foo", NULL, "bar");
208         assert_se(streq(actual, "foo"));
209 }
210
211 static void test_strcmp_ptr(void) {
212         assert_se(strcmp_ptr(NULL, NULL) == 0);
213         assert_se(strcmp_ptr("", NULL) > 0);
214         assert_se(strcmp_ptr("foo", NULL) > 0);
215         assert_se(strcmp_ptr(NULL, "") < 0);
216         assert_se(strcmp_ptr(NULL, "bar") < 0);
217         assert_se(strcmp_ptr("foo", "bar") > 0);
218         assert_se(strcmp_ptr("bar", "baz") < 0);
219         assert_se(strcmp_ptr("foo", "foo") == 0);
220         assert_se(strcmp_ptr("", "") == 0);
221 }
222
223 static void test_foreach_word(void) {
224         const char *word, *state;
225         size_t l;
226         int i = 0;
227         const char test[] = "test abc d\te   f   ";
228         const char * const expected[] = {
229                 "test",
230                 "abc",
231                 "d",
232                 "e",
233                 "f",
234                 "",
235                 NULL
236         };
237
238         FOREACH_WORD(word, l, test, state)
239                 assert_se(strneq(expected[i++], word, l));
240 }
241
242 static void check(const char *test, char** expected, bool trailing) {
243         int i = 0, r;
244
245         printf("<<<%s>>>\n", test);
246         for (;;) {
247                 _cleanup_free_ char *word = NULL;
248
249                 r = extract_first_word(&test, &word, NULL, EXTRACT_QUOTES);
250                 if (r == 0) {
251                         assert_se(!trailing);
252                         break;
253                 } else if (r < 0) {
254                         assert_se(trailing);
255                         break;
256                 }
257
258                 assert_se(streq(word, expected[i++]));
259                 printf("<%s>\n", word);
260         }
261         assert_se(expected[i] == NULL);
262 }
263
264 static void test_foreach_word_quoted(void) {
265         check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
266               STRV_MAKE("test",
267                         "a",
268                         "b",
269                         "c",
270                         "d",
271                         "e",
272                         "",
273                         "",
274                         "hhh",
275                         "",
276                         "",
277                         "a b c"),
278               false);
279
280         check("test \"xxx",
281               STRV_MAKE("test"),
282               true);
283
284         check("test\\",
285               STRV_MAKE_EMPTY,
286               true);
287 }
288
289 static void test_endswith(void) {
290         assert_se(endswith("foobar", "bar"));
291         assert_se(endswith("foobar", ""));
292         assert_se(endswith("foobar", "foobar"));
293         assert_se(endswith("", ""));
294
295         assert_se(!endswith("foobar", "foo"));
296         assert_se(!endswith("foobar", "foobarfoofoo"));
297 }
298
299 static void test_endswith_no_case(void) {
300         assert_se(endswith_no_case("fooBAR", "bar"));
301         assert_se(endswith_no_case("foobar", ""));
302         assert_se(endswith_no_case("foobar", "FOOBAR"));
303         assert_se(endswith_no_case("", ""));
304
305         assert_se(!endswith_no_case("foobar", "FOO"));
306         assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
307 }
308
309 #if 0 /// UNNEEDED by elogind
310 static void test_delete_chars(void) {
311         char *s, input[] = "   hello, waldo.   abc";
312
313         s = delete_chars(input, WHITESPACE);
314         assert_se(streq(s, "hello,waldo.abc"));
315         assert_se(s == input);
316 }
317 #endif // 0
318
319 static void test_delete_trailing_chars(void) {
320
321         char *s,
322                 input1[] = " \n \r k \n \r ",
323                 input2[] = "kkkkthiskkkiskkkaktestkkk",
324                 input3[] = "abcdef";
325
326         s = delete_trailing_chars(input1, WHITESPACE);
327         assert_se(streq(s, " \n \r k"));
328         assert_se(s == input1);
329
330         s = delete_trailing_chars(input2, "kt");
331         assert_se(streq(s, "kkkkthiskkkiskkkaktes"));
332         assert_se(s == input2);
333
334         s = delete_trailing_chars(input3, WHITESPACE);
335         assert_se(streq(s, "abcdef"));
336         assert_se(s == input3);
337
338         s = delete_trailing_chars(input3, "fe");
339         assert_se(streq(s, "abcd"));
340         assert_se(s == input3);
341 }
342
343 static void test_delete_trailing_slashes(void) {
344         char s1[] = "foobar//",
345              s2[] = "foobar/",
346              s3[] = "foobar",
347              s4[] = "";
348
349         assert_se(streq(delete_trailing_chars(s1, "_"), "foobar//"));
350         assert_se(streq(delete_trailing_chars(s1, "/"), "foobar"));
351         assert_se(streq(delete_trailing_chars(s2, "/"), "foobar"));
352         assert_se(streq(delete_trailing_chars(s3, "/"), "foobar"));
353         assert_se(streq(delete_trailing_chars(s4, "/"), ""));
354 }
355
356 #if 0 /// UNNEEDED by elogind
357 static void test_skip_leading_chars(void) {
358         char input1[] = " \n \r k \n \r ",
359                 input2[] = "kkkkthiskkkiskkkaktestkkk",
360                 input3[] = "abcdef";
361
362         assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r "));
363         assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk"));
364         assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk"));
365         assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef"));
366         assert_se(streq(skip_leading_chars(input3, "bcaef"), "def"));
367 }
368 #endif // 0
369
370 static void test_in_charset(void) {
371         assert_se(in_charset("dddaaabbbcccc", "abcd"));
372         assert_se(!in_charset("dddaaabbbcccc", "abc f"));
373 }
374
375 static void test_split_pair(void) {
376         _cleanup_free_ char *a = NULL, *b = NULL;
377
378         assert_se(split_pair("", "", &a, &b) == -EINVAL);
379         assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
380         assert_se(split_pair("", "=", &a, &b) == -EINVAL);
381         assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
382         assert_se(streq(a, "foo"));
383         assert_se(streq(b, "bar"));
384         free(a);
385         free(b);
386         assert_se(split_pair("==", "==", &a, &b) >= 0);
387         assert_se(streq(a, ""));
388         assert_se(streq(b, ""));
389         free(a);
390         free(b);
391
392         assert_se(split_pair("===", "==", &a, &b) >= 0);
393         assert_se(streq(a, ""));
394         assert_se(streq(b, "="));
395 }
396
397 static void test_first_word(void) {
398         assert_se(first_word("Hello", ""));
399         assert_se(first_word("Hello", "Hello"));
400         assert_se(first_word("Hello world", "Hello"));
401         assert_se(first_word("Hello\tworld", "Hello"));
402         assert_se(first_word("Hello\nworld", "Hello"));
403         assert_se(first_word("Hello\rworld", "Hello"));
404         assert_se(first_word("Hello ", "Hello"));
405
406         assert_se(!first_word("Hello", "Hellooo"));
407         assert_se(!first_word("Hello", "xxxxx"));
408         assert_se(!first_word("Hellooo", "Hello"));
409 }
410
411 static void test_strlen_ptr(void) {
412         assert_se(strlen_ptr("foo") == 3);
413         assert_se(strlen_ptr("") == 0);
414         assert_se(strlen_ptr(NULL) == 0);
415 }
416
417 int main(int argc, char *argv[]) {
418         test_string_erase();
419 #if 0 /// UNNEEDED by elogind
420         test_ascii_strcasecmp_n();
421         test_ascii_strcasecmp_nn();
422 #endif // 0
423         test_streq_ptr();
424         test_strstrip();
425         test_strextend();
426         test_strextend_with_separator();
427         test_strrep();
428         test_strappend();
429         test_string_has_cc();
430 #if 0 /// UNNEEDED by elogind
431         test_ascii_strlower();
432 #endif // 0
433         test_strshorten();
434         test_strjoina();
435         test_strcmp_ptr();
436         test_foreach_word();
437         test_foreach_word_quoted();
438         test_endswith();
439         test_endswith_no_case();
440 #if 0 /// UNNEEDED by elogind
441         test_delete_chars();
442 #endif // 0
443         test_delete_trailing_chars();
444         test_delete_trailing_slashes();
445 #if 0 /// UNNEEDED by elogind
446         test_skip_leading_chars();
447 #endif // 0
448         test_in_charset();
449         test_split_pair();
450         test_first_word();
451         test_strlen_ptr();
452
453         return 0;
454 }