chiark / gitweb /
tree-wide: drop license boilerplate
[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
141 static void test_strappend(void) {
142         _cleanup_free_ char *t1, *t2, *t3, *t4;
143
144         t1 = strappend(NULL, NULL);
145         assert_se(streq(t1, ""));
146
147         t2 = strappend(NULL, "suf");
148         assert_se(streq(t2, "suf"));
149
150         t3 = strappend("pre", NULL);
151         assert_se(streq(t3, "pre"));
152
153         t4 = strappend("pre", "suf");
154         assert_se(streq(t4, "presuf"));
155 }
156
157 static void test_string_has_cc(void) {
158         assert_se(string_has_cc("abc\1", NULL));
159         assert_se(string_has_cc("abc\x7f", NULL));
160         assert_se(string_has_cc("abc\x7f", NULL));
161         assert_se(string_has_cc("abc\t\x7f", "\t"));
162         assert_se(string_has_cc("abc\t\x7f", "\t"));
163         assert_se(string_has_cc("\x7f", "\t"));
164         assert_se(string_has_cc("\x7f", "\t\a"));
165
166         assert_se(!string_has_cc("abc\t\t", "\t"));
167         assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
168         assert_se(!string_has_cc("a\ab\tc", "\t\a"));
169 }
170
171 #if 0 /// UNNEEDED by elogind
172 static void test_ascii_strlower(void) {
173         char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
174         assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
175 }
176 #endif // 0
177
178 static void test_strshorten(void) {
179         char s[] = "foobar";
180
181         assert_se(strlen(strshorten(s, 6)) == 6);
182         assert_se(strlen(strshorten(s, 12)) == 6);
183         assert_se(strlen(strshorten(s, 2)) == 2);
184         assert_se(strlen(strshorten(s, 0)) == 0);
185 }
186
187 static void test_strjoina(void) {
188         char *actual;
189
190         actual = strjoina("", "foo", "bar");
191         assert_se(streq(actual, "foobar"));
192
193         actual = strjoina("foo", "bar", "baz");
194         assert_se(streq(actual, "foobarbaz"));
195
196         actual = strjoina("foo", "", "bar", "baz");
197         assert_se(streq(actual, "foobarbaz"));
198
199         actual = strjoina("foo");
200         assert_se(streq(actual, "foo"));
201
202         actual = strjoina(NULL);
203         assert_se(streq(actual, ""));
204
205         actual = strjoina(NULL, "foo");
206         assert_se(streq(actual, ""));
207
208         actual = strjoina("foo", NULL, "bar");
209         assert_se(streq(actual, "foo"));
210 }
211
212 static void test_strcmp_ptr(void) {
213         assert_se(strcmp_ptr(NULL, NULL) == 0);
214         assert_se(strcmp_ptr("", NULL) > 0);
215         assert_se(strcmp_ptr("foo", NULL) > 0);
216         assert_se(strcmp_ptr(NULL, "") < 0);
217         assert_se(strcmp_ptr(NULL, "bar") < 0);
218         assert_se(strcmp_ptr("foo", "bar") > 0);
219         assert_se(strcmp_ptr("bar", "baz") < 0);
220         assert_se(strcmp_ptr("foo", "foo") == 0);
221         assert_se(strcmp_ptr("", "") == 0);
222 }
223
224 static void test_foreach_word(void) {
225         const char *word, *state;
226         size_t l;
227         int i = 0;
228         const char test[] = "test abc d\te   f   ";
229         const char * const expected[] = {
230                 "test",
231                 "abc",
232                 "d",
233                 "e",
234                 "f",
235                 "",
236                 NULL
237         };
238
239         FOREACH_WORD(word, l, test, state)
240                 assert_se(strneq(expected[i++], word, l));
241 }
242
243 static void check(const char *test, char** expected, bool trailing) {
244         int i = 0, r;
245
246         printf("<<<%s>>>\n", test);
247         for (;;) {
248                 _cleanup_free_ char *word = NULL;
249
250                 r = extract_first_word(&test, &word, NULL, EXTRACT_QUOTES);
251                 if (r == 0) {
252                         assert_se(!trailing);
253                         break;
254                 } else if (r < 0) {
255                         assert_se(trailing);
256                         break;
257                 }
258
259                 assert_se(streq(word, expected[i++]));
260                 printf("<%s>\n", word);
261         }
262         assert_se(expected[i] == NULL);
263 }
264
265 static void test_foreach_word_quoted(void) {
266         check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
267               STRV_MAKE("test",
268                         "a",
269                         "b",
270                         "c",
271                         "d",
272                         "e",
273                         "",
274                         "",
275                         "hhh",
276                         "",
277                         "",
278                         "a b c"),
279               false);
280
281         check("test \"xxx",
282               STRV_MAKE("test"),
283               true);
284
285         check("test\\",
286               STRV_MAKE_EMPTY,
287               true);
288 }
289
290 static void test_endswith(void) {
291         assert_se(endswith("foobar", "bar"));
292         assert_se(endswith("foobar", ""));
293         assert_se(endswith("foobar", "foobar"));
294         assert_se(endswith("", ""));
295
296         assert_se(!endswith("foobar", "foo"));
297         assert_se(!endswith("foobar", "foobarfoofoo"));
298 }
299
300 static void test_endswith_no_case(void) {
301         assert_se(endswith_no_case("fooBAR", "bar"));
302         assert_se(endswith_no_case("foobar", ""));
303         assert_se(endswith_no_case("foobar", "FOOBAR"));
304         assert_se(endswith_no_case("", ""));
305
306         assert_se(!endswith_no_case("foobar", "FOO"));
307         assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
308 }
309
310 #if 0 /// UNNEEDED by elogind
311 static void test_delete_chars(void) {
312         char *s, input[] = "   hello, waldo.   abc";
313
314         s = delete_chars(input, WHITESPACE);
315         assert_se(streq(s, "hello,waldo.abc"));
316         assert_se(s == input);
317 }
318 #endif // 0
319
320 static void test_delete_trailing_chars(void) {
321
322         char *s,
323                 input1[] = " \n \r k \n \r ",
324                 input2[] = "kkkkthiskkkiskkkaktestkkk",
325                 input3[] = "abcdef";
326
327         s = delete_trailing_chars(input1, WHITESPACE);
328         assert_se(streq(s, " \n \r k"));
329         assert_se(s == input1);
330
331         s = delete_trailing_chars(input2, "kt");
332         assert_se(streq(s, "kkkkthiskkkiskkkaktes"));
333         assert_se(s == input2);
334
335         s = delete_trailing_chars(input3, WHITESPACE);
336         assert_se(streq(s, "abcdef"));
337         assert_se(s == input3);
338
339         s = delete_trailing_chars(input3, "fe");
340         assert_se(streq(s, "abcd"));
341         assert_se(s == input3);
342 }
343
344 static void test_delete_trailing_slashes(void) {
345         char s1[] = "foobar//",
346              s2[] = "foobar/",
347              s3[] = "foobar",
348              s4[] = "";
349
350         assert_se(streq(delete_trailing_chars(s1, "_"), "foobar//"));
351         assert_se(streq(delete_trailing_chars(s1, "/"), "foobar"));
352         assert_se(streq(delete_trailing_chars(s2, "/"), "foobar"));
353         assert_se(streq(delete_trailing_chars(s3, "/"), "foobar"));
354         assert_se(streq(delete_trailing_chars(s4, "/"), ""));
355 }
356
357 #if 0 /// UNNEEDED by elogind
358 static void test_skip_leading_chars(void) {
359         char input1[] = " \n \r k \n \r ",
360                 input2[] = "kkkkthiskkkiskkkaktestkkk",
361                 input3[] = "abcdef";
362
363         assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r "));
364         assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk"));
365         assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk"));
366         assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef"));
367         assert_se(streq(skip_leading_chars(input3, "bcaef"), "def"));
368 }
369 #endif // 0
370
371 static void test_in_charset(void) {
372         assert_se(in_charset("dddaaabbbcccc", "abcd"));
373         assert_se(!in_charset("dddaaabbbcccc", "abc f"));
374 }
375
376 static void test_split_pair(void) {
377         _cleanup_free_ char *a = NULL, *b = NULL;
378
379         assert_se(split_pair("", "", &a, &b) == -EINVAL);
380         assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
381         assert_se(split_pair("", "=", &a, &b) == -EINVAL);
382         assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
383         assert_se(streq(a, "foo"));
384         assert_se(streq(b, "bar"));
385         free(a);
386         free(b);
387         assert_se(split_pair("==", "==", &a, &b) >= 0);
388         assert_se(streq(a, ""));
389         assert_se(streq(b, ""));
390         free(a);
391         free(b);
392
393         assert_se(split_pair("===", "==", &a, &b) >= 0);
394         assert_se(streq(a, ""));
395         assert_se(streq(b, "="));
396 }
397
398 static void test_first_word(void) {
399         assert_se(first_word("Hello", ""));
400         assert_se(first_word("Hello", "Hello"));
401         assert_se(first_word("Hello world", "Hello"));
402         assert_se(first_word("Hello\tworld", "Hello"));
403         assert_se(first_word("Hello\nworld", "Hello"));
404         assert_se(first_word("Hello\rworld", "Hello"));
405         assert_se(first_word("Hello ", "Hello"));
406
407         assert_se(!first_word("Hello", "Hellooo"));
408         assert_se(!first_word("Hello", "xxxxx"));
409         assert_se(!first_word("Hellooo", "Hello"));
410 }
411
412 static void test_strlen_ptr(void) {
413         assert_se(strlen_ptr("foo") == 3);
414         assert_se(strlen_ptr("") == 0);
415         assert_se(strlen_ptr(NULL) == 0);
416 }
417
418 int main(int argc, char *argv[]) {
419         test_string_erase();
420 #if 0 /// UNNEEDED by elogind
421         test_ascii_strcasecmp_n();
422         test_ascii_strcasecmp_nn();
423 #endif // 0
424         test_streq_ptr();
425         test_strstrip();
426         test_strextend();
427         test_strextend_with_separator();
428         test_strrep();
429         test_strappend();
430         test_string_has_cc();
431 #if 0 /// UNNEEDED by elogind
432         test_ascii_strlower();
433 #endif // 0
434         test_strshorten();
435         test_strjoina();
436         test_strcmp_ptr();
437         test_foreach_word();
438         test_foreach_word_quoted();
439         test_endswith();
440         test_endswith_no_case();
441 #if 0 /// UNNEEDED by elogind
442         test_delete_chars();
443 #endif // 0
444         test_delete_trailing_chars();
445         test_delete_trailing_slashes();
446 #if 0 /// UNNEEDED by elogind
447         test_skip_leading_chars();
448 #endif // 0
449         test_in_charset();
450         test_split_pair();
451         test_first_word();
452         test_strlen_ptr();
453
454         return 0;
455 }