]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-string-util.c
2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
22 #include "string-util.h"
25 static void test_string_erase(void) {
29 assert_se(streq(string_erase(x
), ""));
32 assert_se(streq(string_erase(x
), ""));
34 x
= strdupa("123456789");
35 assert_se(streq(string_erase(x
), ""));
37 assert_se(x
[1] == '\0');
38 assert_se(x
[2] == '\0');
39 assert_se(x
[3] == '\0');
40 assert_se(x
[4] == '\0');
41 assert_se(x
[5] == '\0');
42 assert_se(x
[6] == '\0');
43 assert_se(x
[7] == '\0');
44 assert_se(x
[8] == '\0');
45 assert_se(x
[9] == '\0');
48 static void test_ascii_strcasecmp_n(void) {
50 assert_se(ascii_strcasecmp_n("", "", 0) == 0);
51 assert_se(ascii_strcasecmp_n("", "", 1) == 0);
52 assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
53 assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
54 assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
55 assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
56 assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
57 assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
58 assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
59 assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
60 assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
61 assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
62 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
63 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
64 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
65 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
66 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
68 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
69 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
70 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
71 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
72 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
75 static void test_ascii_strcasecmp_nn(void) {
76 assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
77 assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
78 assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
79 assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
81 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
82 assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
83 assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
84 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
85 assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
87 assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
88 assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
89 assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
92 static void test_streq_ptr(void) {
93 assert_se(streq_ptr(NULL
, NULL
));
94 assert_se(!streq_ptr("abc", "cdef"));
97 static void test_strstrip(void) {
99 char input
[] = " hello, waldo. ";
102 assert_se(streq(r
, "hello, waldo."));
105 static void test_strextend(void) {
106 _cleanup_free_
char *str
= strdup("0123");
107 strextend(&str
, "456", "78", "9", NULL
);
108 assert_se(streq(str
, "0123456789"));
111 static void test_strrep(void) {
112 _cleanup_free_
char *one
, *three
, *zero
;
113 one
= strrep("waldo", 1);
114 three
= strrep("waldo", 3);
115 zero
= strrep("waldo", 0);
117 assert_se(streq(one
, "waldo"));
118 assert_se(streq(three
, "waldowaldowaldo"));
119 assert_se(streq(zero
, ""));
123 static void test_strappend(void) {
124 _cleanup_free_
char *t1
, *t2
, *t3
, *t4
;
126 t1
= strappend(NULL
, NULL
);
127 assert_se(streq(t1
, ""));
129 t2
= strappend(NULL
, "suf");
130 assert_se(streq(t2
, "suf"));
132 t3
= strappend("pre", NULL
);
133 assert_se(streq(t3
, "pre"));
135 t4
= strappend("pre", "suf");
136 assert_se(streq(t4
, "presuf"));
139 static void test_string_has_cc(void) {
140 assert_se(string_has_cc("abc\1", NULL
));
141 assert_se(string_has_cc("abc\x7f", NULL
));
142 assert_se(string_has_cc("abc\x7f", NULL
));
143 assert_se(string_has_cc("abc\t\x7f", "\t"));
144 assert_se(string_has_cc("abc\t\x7f", "\t"));
145 assert_se(string_has_cc("\x7f", "\t"));
146 assert_se(string_has_cc("\x7f", "\t\a"));
148 assert_se(!string_has_cc("abc\t\t", "\t"));
149 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
150 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
153 static void test_ascii_strlower(void) {
154 char a
[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
155 assert_se(streq(ascii_strlower(a
), "aabbcc jk ii od lkjjj kkd lk"));
158 static void test_strshorten(void) {
161 assert_se(strlen(strshorten(s
, 6)) == 6);
162 assert_se(strlen(strshorten(s
, 12)) == 6);
163 assert_se(strlen(strshorten(s
, 2)) == 2);
164 assert_se(strlen(strshorten(s
, 0)) == 0);
167 static void test_strjoina(void) {
170 actual
= strjoina("", "foo", "bar");
171 assert_se(streq(actual
, "foobar"));
173 actual
= strjoina("foo", "bar", "baz");
174 assert_se(streq(actual
, "foobarbaz"));
176 actual
= strjoina("foo", "", "bar", "baz");
177 assert_se(streq(actual
, "foobarbaz"));
179 actual
= strjoina("foo");
180 assert_se(streq(actual
, "foo"));
182 actual
= strjoina(NULL
);
183 assert_se(streq(actual
, ""));
185 actual
= strjoina(NULL
, "foo");
186 assert_se(streq(actual
, ""));
188 actual
= strjoina("foo", NULL
, "bar");
189 assert_se(streq(actual
, "foo"));
192 static void test_strcmp_ptr(void) {
193 assert_se(strcmp_ptr(NULL
, NULL
) == 0);
194 assert_se(strcmp_ptr("", NULL
) > 0);
195 assert_se(strcmp_ptr("foo", NULL
) > 0);
196 assert_se(strcmp_ptr(NULL
, "") < 0);
197 assert_se(strcmp_ptr(NULL
, "bar") < 0);
198 assert_se(strcmp_ptr("foo", "bar") > 0);
199 assert_se(strcmp_ptr("bar", "baz") < 0);
200 assert_se(strcmp_ptr("foo", "foo") == 0);
201 assert_se(strcmp_ptr("", "") == 0);
204 static void test_foreach_word(void) {
205 const char *word
, *state
;
208 const char test
[] = "test abc d\te f ";
209 const char * const expected
[] = {
219 FOREACH_WORD(word
, l
, test
, state
)
220 assert_se(strneq(expected
[i
++], word
, l
));
223 static void check(const char *test
, char** expected
, bool trailing
) {
226 printf("<<<%s>>>\n", test
);
228 _cleanup_free_
char *word
= NULL
;
230 r
= extract_first_word(&test
, &word
, NULL
, EXTRACT_QUOTES
);
232 assert_se(!trailing
);
239 assert_se(streq(word
, expected
[i
++]));
240 printf("<%s>\n", word
);
242 assert_se(expected
[i
] == NULL
);
245 static void test_foreach_word_quoted(void) {
246 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
270 static void test_endswith(void) {
271 assert_se(endswith("foobar", "bar"));
272 assert_se(endswith("foobar", ""));
273 assert_se(endswith("foobar", "foobar"));
274 assert_se(endswith("", ""));
276 assert_se(!endswith("foobar", "foo"));
277 assert_se(!endswith("foobar", "foobarfoofoo"));
280 static void test_endswith_no_case(void) {
281 assert_se(endswith_no_case("fooBAR", "bar"));
282 assert_se(endswith_no_case("foobar", ""));
283 assert_se(endswith_no_case("foobar", "FOOBAR"));
284 assert_se(endswith_no_case("", ""));
286 assert_se(!endswith_no_case("foobar", "FOO"));
287 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
290 static void test_delete_chars(void) {
291 char *s
, input
[] = " hello, waldo. abc";
293 s
= delete_chars(input
, WHITESPACE
);
294 assert_se(streq(s
, "hello,waldo.abc"));
295 assert_se(s
== input
);
298 static void test_delete_trailing_chars(void) {
301 input1
[] = " \n \r k \n \r ",
302 input2
[] = "kkkkthiskkkiskkkaktestkkk",
305 s
= delete_trailing_chars(input1
, WHITESPACE
);
306 assert_se(streq(s
, " \n \r k"));
307 assert_se(s
== input1
);
309 s
= delete_trailing_chars(input2
, "kt");
310 assert_se(streq(s
, "kkkkthiskkkiskkkaktes"));
311 assert_se(s
== input2
);
313 s
= delete_trailing_chars(input3
, WHITESPACE
);
314 assert_se(streq(s
, "abcdef"));
315 assert_se(s
== input3
);
317 s
= delete_trailing_chars(input3
, "fe");
318 assert_se(streq(s
, "abcd"));
319 assert_se(s
== input3
);
322 static void test_skip_leading_chars(void) {
323 char input1
[] = " \n \r k \n \r ",
324 input2
[] = "kkkkthiskkkiskkkaktestkkk",
327 assert_se(streq(skip_leading_chars(input1
, WHITESPACE
), "k \n \r "));
328 assert_se(streq(skip_leading_chars(input2
, "k"), "thiskkkiskkkaktestkkk"));
329 assert_se(streq(skip_leading_chars(input2
, "tk"), "hiskkkiskkkaktestkkk"));
330 assert_se(streq(skip_leading_chars(input3
, WHITESPACE
), "abcdef"));
331 assert_se(streq(skip_leading_chars(input3
, "bcaef"), "def"));
334 static void test_in_charset(void) {
335 assert_se(in_charset("dddaaabbbcccc", "abcd"));
336 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
339 static void test_split_pair(void) {
340 _cleanup_free_
char *a
= NULL
, *b
= NULL
;
342 assert_se(split_pair("", "", &a
, &b
) == -EINVAL
);
343 assert_se(split_pair("foo=bar", "", &a
, &b
) == -EINVAL
);
344 assert_se(split_pair("", "=", &a
, &b
) == -EINVAL
);
345 assert_se(split_pair("foo=bar", "=", &a
, &b
) >= 0);
346 assert_se(streq(a
, "foo"));
347 assert_se(streq(b
, "bar"));
350 assert_se(split_pair("==", "==", &a
, &b
) >= 0);
351 assert_se(streq(a
, ""));
352 assert_se(streq(b
, ""));
356 assert_se(split_pair("===", "==", &a
, &b
) >= 0);
357 assert_se(streq(a
, ""));
358 assert_se(streq(b
, "="));
361 static void test_first_word(void) {
362 assert_se(first_word("Hello", ""));
363 assert_se(first_word("Hello", "Hello"));
364 assert_se(first_word("Hello world", "Hello"));
365 assert_se(first_word("Hello\tworld", "Hello"));
366 assert_se(first_word("Hello\nworld", "Hello"));
367 assert_se(first_word("Hello\rworld", "Hello"));
368 assert_se(first_word("Hello ", "Hello"));
370 assert_se(!first_word("Hello", "Hellooo"));
371 assert_se(!first_word("Hello", "xxxxx"));
372 assert_se(!first_word("Hellooo", "Hello"));
375 static void test_strlen_ptr(void) {
376 assert_se(strlen_ptr("foo") == 3);
377 assert_se(strlen_ptr("") == 0);
378 assert_se(strlen_ptr(NULL
) == 0);
381 int main(int argc
, char *argv
[]) {
383 test_ascii_strcasecmp_n();
384 test_ascii_strcasecmp_nn();
390 test_string_has_cc();
391 test_ascii_strlower();
396 test_foreach_word_quoted();
398 test_endswith_no_case();
400 test_delete_trailing_chars();
401 test_skip_leading_chars();