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 #if 0 /// UNNEEDED by elogind
49 static void test_ascii_strcasecmp_n(void) {
51 assert_se(ascii_strcasecmp_n("", "", 0) == 0);
52 assert_se(ascii_strcasecmp_n("", "", 1) == 0);
53 assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
54 assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
55 assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
56 assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
57 assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
58 assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
59 assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
60 assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
61 assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
62 assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
63 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "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);
67 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
69 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 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);
73 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
76 static void test_ascii_strcasecmp_nn(void) {
77 assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
78 assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
79 assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
80 assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
82 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
83 assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
84 assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
85 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
86 assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
88 assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
89 assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
90 assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
94 static void test_streq_ptr(void) {
95 assert_se(streq_ptr(NULL, NULL));
96 assert_se(!streq_ptr("abc", "cdef"));
99 static void test_strstrip(void) {
101 char input[] = " hello, waldo. ";
104 assert_se(streq(r, "hello, waldo."));
107 static void test_strextend(void) {
108 _cleanup_free_ char *str = strdup("0123");
109 strextend(&str, "456", "78", "9", NULL);
110 assert_se(streq(str, "0123456789"));
113 static void test_strrep(void) {
114 _cleanup_free_ char *one, *three, *zero;
115 one = strrep("waldo", 1);
116 three = strrep("waldo", 3);
117 zero = strrep("waldo", 0);
119 assert_se(streq(one, "waldo"));
120 assert_se(streq(three, "waldowaldowaldo"));
121 assert_se(streq(zero, ""));
125 static void test_strappend(void) {
126 _cleanup_free_ char *t1, *t2, *t3, *t4;
128 t1 = strappend(NULL, NULL);
129 assert_se(streq(t1, ""));
131 t2 = strappend(NULL, "suf");
132 assert_se(streq(t2, "suf"));
134 t3 = strappend("pre", NULL);
135 assert_se(streq(t3, "pre"));
137 t4 = strappend("pre", "suf");
138 assert_se(streq(t4, "presuf"));
141 static void test_string_has_cc(void) {
142 assert_se(string_has_cc("abc\1", NULL));
143 assert_se(string_has_cc("abc\x7f", NULL));
144 assert_se(string_has_cc("abc\x7f", NULL));
145 assert_se(string_has_cc("abc\t\x7f", "\t"));
146 assert_se(string_has_cc("abc\t\x7f", "\t"));
147 assert_se(string_has_cc("\x7f", "\t"));
148 assert_se(string_has_cc("\x7f", "\t\a"));
150 assert_se(!string_has_cc("abc\t\t", "\t"));
151 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
152 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
155 #if 0 /// UNNEEDED by elogind
156 static void test_ascii_strlower(void) {
157 char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
158 assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
162 static void test_strshorten(void) {
165 assert_se(strlen(strshorten(s, 6)) == 6);
166 assert_se(strlen(strshorten(s, 12)) == 6);
167 assert_se(strlen(strshorten(s, 2)) == 2);
168 assert_se(strlen(strshorten(s, 0)) == 0);
171 static void test_strjoina(void) {
174 actual = strjoina("", "foo", "bar");
175 assert_se(streq(actual, "foobar"));
177 actual = strjoina("foo", "bar", "baz");
178 assert_se(streq(actual, "foobarbaz"));
180 actual = strjoina("foo", "", "bar", "baz");
181 assert_se(streq(actual, "foobarbaz"));
183 actual = strjoina("foo");
184 assert_se(streq(actual, "foo"));
186 actual = strjoina(NULL);
187 assert_se(streq(actual, ""));
189 actual = strjoina(NULL, "foo");
190 assert_se(streq(actual, ""));
192 actual = strjoina("foo", NULL, "bar");
193 assert_se(streq(actual, "foo"));
196 static void test_strcmp_ptr(void) {
197 assert_se(strcmp_ptr(NULL, NULL) == 0);
198 assert_se(strcmp_ptr("", NULL) > 0);
199 assert_se(strcmp_ptr("foo", NULL) > 0);
200 assert_se(strcmp_ptr(NULL, "") < 0);
201 assert_se(strcmp_ptr(NULL, "bar") < 0);
202 assert_se(strcmp_ptr("foo", "bar") > 0);
203 assert_se(strcmp_ptr("bar", "baz") < 0);
204 assert_se(strcmp_ptr("foo", "foo") == 0);
205 assert_se(strcmp_ptr("", "") == 0);
208 static void test_foreach_word(void) {
209 const char *word, *state;
212 const char test[] = "test abc d\te f ";
213 const char * const expected[] = {
223 FOREACH_WORD(word, l, test, state)
224 assert_se(strneq(expected[i++], word, l));
227 static void check(const char *test, char** expected, bool trailing) {
230 printf("<<<%s>>>\n", test);
232 _cleanup_free_ char *word = NULL;
234 r = extract_first_word(&test, &word, NULL, EXTRACT_QUOTES);
236 assert_se(!trailing);
243 assert_se(streq(word, expected[i++]));
244 printf("<%s>\n", word);
246 assert_se(expected[i] == NULL);
249 static void test_foreach_word_quoted(void) {
250 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
274 static void test_endswith(void) {
275 assert_se(endswith("foobar", "bar"));
276 assert_se(endswith("foobar", ""));
277 assert_se(endswith("foobar", "foobar"));
278 assert_se(endswith("", ""));
280 assert_se(!endswith("foobar", "foo"));
281 assert_se(!endswith("foobar", "foobarfoofoo"));
284 static void test_endswith_no_case(void) {
285 assert_se(endswith_no_case("fooBAR", "bar"));
286 assert_se(endswith_no_case("foobar", ""));
287 assert_se(endswith_no_case("foobar", "FOOBAR"));
288 assert_se(endswith_no_case("", ""));
290 assert_se(!endswith_no_case("foobar", "FOO"));
291 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
294 #if 0 /// UNNEEDED by elogind
295 static void test_delete_chars(void) {
297 char input[] = " hello, waldo. abc";
299 r = delete_chars(input, WHITESPACE);
300 assert_se(streq(r, "hello,waldo.abc"));
304 static void test_in_charset(void) {
305 assert_se(in_charset("dddaaabbbcccc", "abcd"));
306 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
309 static void test_split_pair(void) {
310 _cleanup_free_ char *a = NULL, *b = NULL;
312 assert_se(split_pair("", "", &a, &b) == -EINVAL);
313 assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
314 assert_se(split_pair("", "=", &a, &b) == -EINVAL);
315 assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
316 assert_se(streq(a, "foo"));
317 assert_se(streq(b, "bar"));
320 assert_se(split_pair("==", "==", &a, &b) >= 0);
321 assert_se(streq(a, ""));
322 assert_se(streq(b, ""));
326 assert_se(split_pair("===", "==", &a, &b) >= 0);
327 assert_se(streq(a, ""));
328 assert_se(streq(b, "="));
331 static void test_first_word(void) {
332 assert_se(first_word("Hello", ""));
333 assert_se(first_word("Hello", "Hello"));
334 assert_se(first_word("Hello world", "Hello"));
335 assert_se(first_word("Hello\tworld", "Hello"));
336 assert_se(first_word("Hello\nworld", "Hello"));
337 assert_se(first_word("Hello\rworld", "Hello"));
338 assert_se(first_word("Hello ", "Hello"));
340 assert_se(!first_word("Hello", "Hellooo"));
341 assert_se(!first_word("Hello", "xxxxx"));
342 assert_se(!first_word("Hellooo", "Hello"));
345 int main(int argc, char *argv[]) {
347 #if 0 /// UNNEEDED by elogind
348 test_ascii_strcasecmp_n();
349 test_ascii_strcasecmp_nn();
356 test_string_has_cc();
357 #if 0 /// UNNEEDED by elogind
358 test_ascii_strlower();
364 test_foreach_word_quoted();
366 test_endswith_no_case();
367 #if 0 /// UNNEEDED by elogind