]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-string-util.c
process-util: slightly optimize querying of our own process metadata
[thirdparty/systemd.git] / src / test / test-string-util.c
CommitLineData
9fe4ea21
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2015 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
6571a64e
RC
20#include "alloc-util.h"
21#include "macro.h"
9fe4ea21 22#include "string-util.h"
6571a64e 23#include "strv.h"
9fe4ea21
LP
24
25static void test_string_erase(void) {
26 char *x;
27
28 x = strdupa("");
29 assert_se(streq(string_erase(x), ""));
30
31 x = strdupa("1");
2d26d8e0 32 assert_se(streq(string_erase(x), ""));
9fe4ea21
LP
33
34 x = strdupa("123456789");
2d26d8e0
ZJS
35 assert_se(streq(string_erase(x), ""));
36
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');
9fe4ea21
LP
46}
47
522d85ae
LP
48static void test_ascii_strcasecmp_n(void) {
49
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);
67
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);
73}
74
c1749834
LP
75static 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);
80
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);
86
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);
90}
91
6571a64e
RC
92static void test_streq_ptr(void) {
93 assert_se(streq_ptr(NULL, NULL));
94 assert_se(!streq_ptr("abc", "cdef"));
95}
96
97static void test_strstrip(void) {
98 char *r;
99 char input[] = " hello, waldo. ";
100
101 r = strstrip(input);
102 assert_se(streq(r, "hello, waldo."));
103}
104
105static void test_strextend(void) {
106 _cleanup_free_ char *str = strdup("0123");
107 strextend(&str, "456", "78", "9", NULL);
108 assert_se(streq(str, "0123456789"));
109}
110
111static 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);
116
117 assert_se(streq(one, "waldo"));
118 assert_se(streq(three, "waldowaldowaldo"));
119 assert_se(streq(zero, ""));
120}
121
122
123static void test_strappend(void) {
124 _cleanup_free_ char *t1, *t2, *t3, *t4;
125
126 t1 = strappend(NULL, NULL);
127 assert_se(streq(t1, ""));
128
129 t2 = strappend(NULL, "suf");
130 assert_se(streq(t2, "suf"));
131
132 t3 = strappend("pre", NULL);
133 assert_se(streq(t3, "pre"));
134
135 t4 = strappend("pre", "suf");
136 assert_se(streq(t4, "presuf"));
137}
138
139static 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"));
147
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"));
151}
152
153static 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"));
156}
157
158static void test_strshorten(void) {
159 char s[] = "foobar";
160
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);
165}
166
167static void test_strjoina(void) {
168 char *actual;
169
170 actual = strjoina("", "foo", "bar");
171 assert_se(streq(actual, "foobar"));
172
173 actual = strjoina("foo", "bar", "baz");
174 assert_se(streq(actual, "foobarbaz"));
175
176 actual = strjoina("foo", "", "bar", "baz");
177 assert_se(streq(actual, "foobarbaz"));
178
179 actual = strjoina("foo");
180 assert_se(streq(actual, "foo"));
181
182 actual = strjoina(NULL);
183 assert_se(streq(actual, ""));
184
185 actual = strjoina(NULL, "foo");
186 assert_se(streq(actual, ""));
187
188 actual = strjoina("foo", NULL, "bar");
189 assert_se(streq(actual, "foo"));
190}
191
192static 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);
202}
203
204static void test_foreach_word(void) {
205 const char *word, *state;
206 size_t l;
207 int i = 0;
208 const char test[] = "test abc d\te f ";
209 const char * const expected[] = {
210 "test",
211 "abc",
212 "d",
213 "e",
214 "f",
215 "",
216 NULL
217 };
218
219 FOREACH_WORD(word, l, test, state)
220 assert_se(strneq(expected[i++], word, l));
221}
222
223static void check(const char *test, char** expected, bool trailing) {
bc8ec170 224 int i = 0, r;
6571a64e
RC
225
226 printf("<<<%s>>>\n", test);
bc8ec170
ZJS
227 for (;;) {
228 _cleanup_free_ char *word = NULL;
229
230 r = extract_first_word(&test, &word, NULL, EXTRACT_QUOTES);
231 if (r == 0) {
232 assert_se(!trailing);
233 break;
234 } else if (r < 0) {
235 assert_se(trailing);
236 break;
237 }
238
239 assert_se(streq(word, expected[i++]));
240 printf("<%s>\n", word);
6571a64e 241 }
6571a64e 242 assert_se(expected[i] == NULL);
6571a64e
RC
243}
244
245static void test_foreach_word_quoted(void) {
246 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
247 STRV_MAKE("test",
248 "a",
249 "b",
250 "c",
251 "d",
252 "e",
253 "",
254 "",
255 "hhh",
256 "",
257 "",
258 "a b c"),
259 false);
260
261 check("test \"xxx",
262 STRV_MAKE("test"),
263 true);
264
265 check("test\\",
266 STRV_MAKE_EMPTY,
267 true);
268}
269
270static 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("", ""));
275
276 assert_se(!endswith("foobar", "foo"));
277 assert_se(!endswith("foobar", "foobarfoofoo"));
278}
279
280static 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("", ""));
285
286 assert_se(!endswith_no_case("foobar", "FOO"));
287 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
288}
289
290static void test_delete_chars(void) {
291 char *r;
292 char input[] = " hello, waldo. abc";
293
294 r = delete_chars(input, WHITESPACE);
295 assert_se(streq(r, "hello,waldo.abc"));
296}
297
298static void test_in_charset(void) {
299 assert_se(in_charset("dddaaabbbcccc", "abcd"));
300 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
301}
302
303static void test_split_pair(void) {
304 _cleanup_free_ char *a = NULL, *b = NULL;
305
306 assert_se(split_pair("", "", &a, &b) == -EINVAL);
307 assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
308 assert_se(split_pair("", "=", &a, &b) == -EINVAL);
309 assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
310 assert_se(streq(a, "foo"));
311 assert_se(streq(b, "bar"));
312 free(a);
313 free(b);
314 assert_se(split_pair("==", "==", &a, &b) >= 0);
315 assert_se(streq(a, ""));
316 assert_se(streq(b, ""));
317 free(a);
318 free(b);
319
320 assert_se(split_pair("===", "==", &a, &b) >= 0);
321 assert_se(streq(a, ""));
322 assert_se(streq(b, "="));
323}
324
325static void test_first_word(void) {
326 assert_se(first_word("Hello", ""));
327 assert_se(first_word("Hello", "Hello"));
328 assert_se(first_word("Hello world", "Hello"));
329 assert_se(first_word("Hello\tworld", "Hello"));
330 assert_se(first_word("Hello\nworld", "Hello"));
331 assert_se(first_word("Hello\rworld", "Hello"));
332 assert_se(first_word("Hello ", "Hello"));
333
334 assert_se(!first_word("Hello", "Hellooo"));
335 assert_se(!first_word("Hello", "xxxxx"));
336 assert_se(!first_word("Hellooo", "Hello"));
337}
338
9fe4ea21
LP
339int main(int argc, char *argv[]) {
340 test_string_erase();
522d85ae 341 test_ascii_strcasecmp_n();
c1749834 342 test_ascii_strcasecmp_nn();
6571a64e
RC
343 test_streq_ptr();
344 test_strstrip();
345 test_strextend();
346 test_strrep();
347 test_strappend();
348 test_string_has_cc();
349 test_ascii_strlower();
350 test_strshorten();
351 test_strjoina();
352 test_strcmp_ptr();
353 test_foreach_word();
354 test_foreach_word_quoted();
355 test_endswith();
356 test_endswith_no_case();
357 test_delete_chars();
358 test_in_charset();
359 test_split_pair();
360 test_first_word();
361
9fe4ea21
LP
362 return 0;
363}