]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
9fe4ea21 | 2 | |
6571a64e | 3 | #include "alloc-util.h" |
8409f688 | 4 | #include "locale-util.h" |
6571a64e | 5 | #include "macro.h" |
9fe4ea21 | 6 | #include "string-util.h" |
6571a64e | 7 | #include "strv.h" |
7f546026 | 8 | #include "tests.h" |
9924aef6 | 9 | #include "utf8.h" |
ca78ad1d | 10 | #include "util.h" |
9fe4ea21 | 11 | |
53caaffd | 12 | static void test_string_erase(void) { |
0176728a | 13 | log_info("/* %s */", __func__); |
53caaffd | 14 | |
0176728a | 15 | char *x; |
53caaffd LP |
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 | ||
7f546026 | 36 | static void test_free_and_strndup_one(char **t, const char *src, size_t l, const char *expected, bool change) { |
7f546026 ZJS |
37 | log_debug("%s: \"%s\", \"%s\", %zd (expect \"%s\", %s)", |
38 | __func__, strnull(*t), strnull(src), l, strnull(expected), yes_no(change)); | |
39 | ||
0176728a | 40 | int r = free_and_strndup(t, src, l); |
7f546026 ZJS |
41 | assert_se(streq_ptr(*t, expected)); |
42 | assert_se(r == change); /* check that change occurs only when necessary */ | |
43 | } | |
44 | ||
45 | static void test_free_and_strndup(void) { | |
0176728a ZJS |
46 | log_info("/* %s */", __func__); |
47 | ||
7f546026 ZJS |
48 | static const struct test_case { |
49 | const char *src; | |
50 | size_t len; | |
51 | const char *expected; | |
52 | } cases[] = { | |
53 | {"abc", 0, ""}, | |
54 | {"abc", 0, ""}, | |
55 | {"abc", 1, "a"}, | |
56 | {"abc", 2, "ab"}, | |
57 | {"abc", 3, "abc"}, | |
58 | {"abc", 4, "abc"}, | |
59 | {"abc", 5, "abc"}, | |
60 | {"abc", 5, "abc"}, | |
61 | {"abc", 4, "abc"}, | |
62 | {"abc", 3, "abc"}, | |
63 | {"abc", 2, "ab"}, | |
64 | {"abc", 1, "a"}, | |
65 | {"abc", 0, ""}, | |
66 | ||
67 | {"", 0, ""}, | |
68 | {"", 1, ""}, | |
69 | {"", 2, ""}, | |
70 | {"", 0, ""}, | |
71 | {"", 1, ""}, | |
72 | {"", 2, ""}, | |
73 | {"", 2, ""}, | |
74 | {"", 1, ""}, | |
75 | {"", 0, ""}, | |
76 | ||
77 | {NULL, 0, NULL}, | |
78 | ||
79 | {"foo", 3, "foo"}, | |
80 | {"foobar", 6, "foobar"}, | |
81 | }; | |
82 | ||
83 | _cleanup_free_ char *t = NULL; | |
84 | const char *prev_expected = t; | |
85 | ||
86 | for (unsigned i = 0; i < ELEMENTSOF(cases); i++) { | |
87 | test_free_and_strndup_one(&t, | |
88 | cases[i].src, cases[i].len, cases[i].expected, | |
89 | !streq_ptr(cases[i].expected, prev_expected)); | |
90 | prev_expected = t; | |
91 | } | |
92 | } | |
93 | ||
522d85ae | 94 | static void test_ascii_strcasecmp_n(void) { |
0176728a | 95 | log_info("/* %s */", __func__); |
522d85ae LP |
96 | |
97 | assert_se(ascii_strcasecmp_n("", "", 0) == 0); | |
98 | assert_se(ascii_strcasecmp_n("", "", 1) == 0); | |
99 | assert_se(ascii_strcasecmp_n("", "a", 1) < 0); | |
100 | assert_se(ascii_strcasecmp_n("", "a", 2) < 0); | |
101 | assert_se(ascii_strcasecmp_n("a", "", 1) > 0); | |
102 | assert_se(ascii_strcasecmp_n("a", "", 2) > 0); | |
103 | assert_se(ascii_strcasecmp_n("a", "a", 1) == 0); | |
104 | assert_se(ascii_strcasecmp_n("a", "a", 2) == 0); | |
105 | assert_se(ascii_strcasecmp_n("a", "b", 1) < 0); | |
106 | assert_se(ascii_strcasecmp_n("a", "b", 2) < 0); | |
107 | assert_se(ascii_strcasecmp_n("b", "a", 1) > 0); | |
108 | assert_se(ascii_strcasecmp_n("b", "a", 2) > 0); | |
109 | assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0); | |
110 | assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0); | |
111 | assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0); | |
112 | assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0); | |
113 | assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0); | |
114 | ||
115 | assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0); | |
116 | assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0); | |
117 | assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0); | |
118 | assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0); | |
119 | assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0); | |
120 | } | |
121 | ||
c1749834 | 122 | static void test_ascii_strcasecmp_nn(void) { |
0176728a ZJS |
123 | log_info("/* %s */", __func__); |
124 | ||
c1749834 LP |
125 | assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0); |
126 | assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0); | |
127 | assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0); | |
128 | assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0); | |
129 | ||
130 | assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0); | |
131 | assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0); | |
132 | assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0); | |
133 | assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0); | |
134 | assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0); | |
135 | ||
136 | assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0); | |
137 | assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0); | |
138 | assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0); | |
139 | } | |
140 | ||
8409f688 ZJS |
141 | static void test_cellescape(void) { |
142 | char buf[40]; | |
143 | ||
0176728a ZJS |
144 | log_info("/* %s */", __func__); |
145 | ||
61f6e276 LP |
146 | assert_se(streq(cellescape(buf, 1, ""), "")); |
147 | assert_se(streq(cellescape(buf, 1, "1"), "")); | |
148 | assert_se(streq(cellescape(buf, 1, "12"), "")); | |
149 | ||
150 | assert_se(streq(cellescape(buf, 2, ""), "")); | |
151 | assert_se(streq(cellescape(buf, 2, "1"), "1")); | |
152 | assert_se(streq(cellescape(buf, 2, "12"), ".")); | |
153 | assert_se(streq(cellescape(buf, 2, "123"), ".")); | |
154 | ||
155 | assert_se(streq(cellescape(buf, 3, ""), "")); | |
156 | assert_se(streq(cellescape(buf, 3, "1"), "1")); | |
157 | assert_se(streq(cellescape(buf, 3, "12"), "12")); | |
158 | assert_se(streq(cellescape(buf, 3, "123"), "..")); | |
159 | assert_se(streq(cellescape(buf, 3, "1234"), "..")); | |
160 | ||
161 | assert_se(streq(cellescape(buf, 4, ""), "")); | |
162 | assert_se(streq(cellescape(buf, 4, "1"), "1")); | |
163 | assert_se(streq(cellescape(buf, 4, "12"), "12")); | |
164 | assert_se(streq(cellescape(buf, 4, "123"), "123")); | |
165 | assert_se(streq(cellescape(buf, 4, "1234"), is_locale_utf8() ? "…" : "...")); | |
166 | assert_se(streq(cellescape(buf, 4, "12345"), is_locale_utf8() ? "…" : "...")); | |
167 | ||
168 | assert_se(streq(cellescape(buf, 5, ""), "")); | |
169 | assert_se(streq(cellescape(buf, 5, "1"), "1")); | |
170 | assert_se(streq(cellescape(buf, 5, "12"), "12")); | |
171 | assert_se(streq(cellescape(buf, 5, "123"), "123")); | |
172 | assert_se(streq(cellescape(buf, 5, "1234"), "1234")); | |
173 | assert_se(streq(cellescape(buf, 5, "12345"), is_locale_utf8() ? "1…" : "1...")); | |
174 | assert_se(streq(cellescape(buf, 5, "123456"), is_locale_utf8() ? "1…" : "1...")); | |
175 | ||
176 | assert_se(streq(cellescape(buf, 1, "\020"), "")); | |
177 | assert_se(streq(cellescape(buf, 2, "\020"), ".")); | |
178 | assert_se(streq(cellescape(buf, 3, "\020"), "..")); | |
179 | assert_se(streq(cellescape(buf, 4, "\020"), "…")); | |
180 | assert_se(streq(cellescape(buf, 5, "\020"), "\\020")); | |
181 | ||
182 | assert_se(streq(cellescape(buf, 5, "1234\020"), "1…")); | |
183 | assert_se(streq(cellescape(buf, 6, "1234\020"), "12…")); | |
184 | assert_se(streq(cellescape(buf, 7, "1234\020"), "123…")); | |
185 | assert_se(streq(cellescape(buf, 8, "1234\020"), "1234…")); | |
186 | assert_se(streq(cellescape(buf, 9, "1234\020"), "1234\\020")); | |
187 | ||
188 | assert_se(streq(cellescape(buf, 1, "\t\n"), "")); | |
189 | assert_se(streq(cellescape(buf, 2, "\t\n"), ".")); | |
190 | assert_se(streq(cellescape(buf, 3, "\t\n"), "..")); | |
191 | assert_se(streq(cellescape(buf, 4, "\t\n"), "…")); | |
192 | assert_se(streq(cellescape(buf, 5, "\t\n"), "\\t\\n")); | |
193 | ||
194 | assert_se(streq(cellescape(buf, 5, "1234\t\n"), "1…")); | |
195 | assert_se(streq(cellescape(buf, 6, "1234\t\n"), "12…")); | |
196 | assert_se(streq(cellescape(buf, 7, "1234\t\n"), "123…")); | |
197 | assert_se(streq(cellescape(buf, 8, "1234\t\n"), "1234…")); | |
198 | assert_se(streq(cellescape(buf, 9, "1234\t\n"), "1234\\t\\n")); | |
199 | ||
200 | assert_se(streq(cellescape(buf, 4, "x\t\020\n"), "…")); | |
201 | assert_se(streq(cellescape(buf, 5, "x\t\020\n"), "x…")); | |
202 | assert_se(streq(cellescape(buf, 6, "x\t\020\n"), "x…")); | |
203 | assert_se(streq(cellescape(buf, 7, "x\t\020\n"), "x\\t…")); | |
204 | assert_se(streq(cellescape(buf, 8, "x\t\020\n"), "x\\t…")); | |
205 | assert_se(streq(cellescape(buf, 9, "x\t\020\n"), "x\\t…")); | |
206 | assert_se(streq(cellescape(buf, 10, "x\t\020\n"), "x\\t\\020\\n")); | |
207 | ||
208 | assert_se(streq(cellescape(buf, 6, "1\011"), "1\\t")); | |
209 | assert_se(streq(cellescape(buf, 6, "1\020"), "1\\020")); | |
210 | assert_se(streq(cellescape(buf, 6, "1\020x"), is_locale_utf8() ? "1…" : "1...")); | |
8409f688 ZJS |
211 | |
212 | assert_se(streq(cellescape(buf, 40, "1\020"), "1\\020")); | |
213 | assert_se(streq(cellescape(buf, 40, "1\020x"), "1\\020x")); | |
214 | ||
215 | assert_se(streq(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'")); | |
61f6e276 LP |
216 | assert_se(streq(cellescape(buf, 6, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a...")); |
217 | assert_se(streq(cellescape(buf, 7, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a...")); | |
218 | assert_se(streq(cellescape(buf, 8, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b...")); | |
8409f688 ZJS |
219 | |
220 | assert_se(streq(cellescape(buf, sizeof buf, "1\020"), "1\\020")); | |
221 | assert_se(streq(cellescape(buf, sizeof buf, "1\020x"), "1\\020x")); | |
222 | } | |
223 | ||
6571a64e | 224 | static void test_streq_ptr(void) { |
0176728a ZJS |
225 | log_info("/* %s */", __func__); |
226 | ||
6571a64e RC |
227 | assert_se(streq_ptr(NULL, NULL)); |
228 | assert_se(!streq_ptr("abc", "cdef")); | |
229 | } | |
230 | ||
231 | static void test_strstrip(void) { | |
0176728a ZJS |
232 | log_info("/* %s */", __func__); |
233 | ||
234 | char *ret, input[] = " hello, waldo. "; | |
6571a64e | 235 | |
0176728a ZJS |
236 | ret = strstrip(input); |
237 | assert_se(streq(ret, "hello, waldo.")); | |
6571a64e RC |
238 | } |
239 | ||
240 | static void test_strextend(void) { | |
0176728a ZJS |
241 | log_info("/* %s */", __func__); |
242 | ||
bb8ad9ea LP |
243 | _cleanup_free_ char *str = NULL; |
244 | ||
245 | assert_se(strextend(&str, NULL)); | |
246 | assert_se(streq_ptr(str, "")); | |
c2bc710b | 247 | assert_se(strextend(&str, "", "0", "", "", "123")); |
bb8ad9ea | 248 | assert_se(streq_ptr(str, "0123")); |
c2bc710b | 249 | assert_se(strextend(&str, "456", "78", "9")); |
bb8ad9ea LP |
250 | assert_se(streq_ptr(str, "0123456789")); |
251 | } | |
252 | ||
253 | static void test_strextend_with_separator(void) { | |
0176728a ZJS |
254 | log_info("/* %s */", __func__); |
255 | ||
bb8ad9ea LP |
256 | _cleanup_free_ char *str = NULL; |
257 | ||
258 | assert_se(strextend_with_separator(&str, NULL, NULL)); | |
259 | assert_se(streq_ptr(str, "")); | |
260 | str = mfree(str); | |
261 | ||
262 | assert_se(strextend_with_separator(&str, "...", NULL)); | |
263 | assert_se(streq_ptr(str, "")); | |
264 | assert_se(strextend_with_separator(&str, "...", NULL)); | |
265 | assert_se(streq_ptr(str, "")); | |
266 | str = mfree(str); | |
267 | ||
c2bc710b | 268 | assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc")); |
bb8ad9ea LP |
269 | assert_se(streq_ptr(str, "axyzbbxyzccc")); |
270 | str = mfree(str); | |
271 | ||
c2bc710b | 272 | assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234")); |
bb8ad9ea | 273 | assert_se(streq_ptr(str, "start,,1,234")); |
c2bc710b | 274 | assert_se(strextend_with_separator(&str, ";", "more", "5", "678")); |
bb8ad9ea | 275 | assert_se(streq_ptr(str, "start,,1,234;more;5;678")); |
6571a64e RC |
276 | } |
277 | ||
278 | static void test_strrep(void) { | |
0176728a ZJS |
279 | log_info("/* %s */", __func__); |
280 | ||
6571a64e RC |
281 | _cleanup_free_ char *one, *three, *zero; |
282 | one = strrep("waldo", 1); | |
283 | three = strrep("waldo", 3); | |
284 | zero = strrep("waldo", 0); | |
285 | ||
286 | assert_se(streq(one, "waldo")); | |
287 | assert_se(streq(three, "waldowaldowaldo")); | |
288 | assert_se(streq(zero, "")); | |
289 | } | |
290 | ||
6571a64e RC |
291 | static void test_string_has_cc(void) { |
292 | assert_se(string_has_cc("abc\1", NULL)); | |
293 | assert_se(string_has_cc("abc\x7f", NULL)); | |
294 | assert_se(string_has_cc("abc\x7f", NULL)); | |
295 | assert_se(string_has_cc("abc\t\x7f", "\t")); | |
296 | assert_se(string_has_cc("abc\t\x7f", "\t")); | |
297 | assert_se(string_has_cc("\x7f", "\t")); | |
298 | assert_se(string_has_cc("\x7f", "\t\a")); | |
299 | ||
300 | assert_se(!string_has_cc("abc\t\t", "\t")); | |
301 | assert_se(!string_has_cc("abc\t\t\a", "\t\a")); | |
302 | assert_se(!string_has_cc("a\ab\tc", "\t\a")); | |
303 | } | |
304 | ||
305 | static void test_ascii_strlower(void) { | |
0176728a ZJS |
306 | log_info("/* %s */", __func__); |
307 | ||
6571a64e RC |
308 | char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK"; |
309 | assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk")); | |
310 | } | |
311 | ||
312 | static void test_strshorten(void) { | |
0176728a ZJS |
313 | log_info("/* %s */", __func__); |
314 | ||
6571a64e RC |
315 | char s[] = "foobar"; |
316 | ||
317 | assert_se(strlen(strshorten(s, 6)) == 6); | |
318 | assert_se(strlen(strshorten(s, 12)) == 6); | |
319 | assert_se(strlen(strshorten(s, 2)) == 2); | |
320 | assert_se(strlen(strshorten(s, 0)) == 0); | |
321 | } | |
322 | ||
323 | static void test_strjoina(void) { | |
0176728a ZJS |
324 | log_info("/* %s */", __func__); |
325 | ||
6571a64e RC |
326 | char *actual; |
327 | ||
328 | actual = strjoina("", "foo", "bar"); | |
329 | assert_se(streq(actual, "foobar")); | |
330 | ||
331 | actual = strjoina("foo", "bar", "baz"); | |
332 | assert_se(streq(actual, "foobarbaz")); | |
333 | ||
334 | actual = strjoina("foo", "", "bar", "baz"); | |
335 | assert_se(streq(actual, "foobarbaz")); | |
336 | ||
337 | actual = strjoina("foo"); | |
338 | assert_se(streq(actual, "foo")); | |
339 | ||
340 | actual = strjoina(NULL); | |
341 | assert_se(streq(actual, "")); | |
342 | ||
343 | actual = strjoina(NULL, "foo"); | |
344 | assert_se(streq(actual, "")); | |
345 | ||
346 | actual = strjoina("foo", NULL, "bar"); | |
347 | assert_se(streq(actual, "foo")); | |
e4645ca5 ZJS |
348 | |
349 | actual = strjoina("/sys/fs/cgroup/", "dn", "/a/b/c", "/cgroup.procs"); | |
350 | assert_se(streq(actual, "/sys/fs/cgroup/dn/a/b/c/cgroup.procs")); | |
351 | ||
352 | actual = strjoina("/sys/fs/cgroup/", "dn", NULL, NULL); | |
353 | assert_se(streq(actual, "/sys/fs/cgroup/dn")); | |
6571a64e RC |
354 | } |
355 | ||
7eac7b4c ZJS |
356 | static void test_strjoin(void) { |
357 | char *actual; | |
358 | ||
359 | actual = strjoin("", "foo", "bar"); | |
360 | assert_se(streq(actual, "foobar")); | |
361 | mfree(actual); | |
362 | ||
363 | actual = strjoin("foo", "bar", "baz"); | |
364 | assert_se(streq(actual, "foobarbaz")); | |
365 | mfree(actual); | |
366 | ||
367 | actual = strjoin("foo", "", "bar", "baz"); | |
368 | assert_se(streq(actual, "foobarbaz")); | |
369 | mfree(actual); | |
370 | ||
371 | actual = strjoin("foo", NULL); | |
372 | assert_se(streq(actual, "foo")); | |
373 | mfree(actual); | |
374 | ||
375 | actual = strjoin(NULL, NULL); | |
376 | assert_se(streq(actual, "")); | |
377 | mfree(actual); | |
378 | ||
379 | actual = strjoin(NULL, "foo"); | |
380 | assert_se(streq(actual, "")); | |
381 | mfree(actual); | |
382 | ||
383 | actual = strjoin("foo", NULL, "bar"); | |
384 | assert_se(streq(actual, "foo")); | |
385 | mfree(actual); | |
386 | } | |
387 | ||
6571a64e | 388 | static void test_strcmp_ptr(void) { |
0176728a ZJS |
389 | log_info("/* %s */", __func__); |
390 | ||
6571a64e RC |
391 | assert_se(strcmp_ptr(NULL, NULL) == 0); |
392 | assert_se(strcmp_ptr("", NULL) > 0); | |
393 | assert_se(strcmp_ptr("foo", NULL) > 0); | |
394 | assert_se(strcmp_ptr(NULL, "") < 0); | |
395 | assert_se(strcmp_ptr(NULL, "bar") < 0); | |
396 | assert_se(strcmp_ptr("foo", "bar") > 0); | |
397 | assert_se(strcmp_ptr("bar", "baz") < 0); | |
398 | assert_se(strcmp_ptr("foo", "foo") == 0); | |
399 | assert_se(strcmp_ptr("", "") == 0); | |
400 | } | |
401 | ||
402 | static void test_foreach_word(void) { | |
0176728a ZJS |
403 | log_info("/* %s */", __func__); |
404 | ||
d59d954d | 405 | const char *test = "test abc d\te f "; |
6571a64e RC |
406 | const char * const expected[] = { |
407 | "test", | |
408 | "abc", | |
409 | "d", | |
410 | "e", | |
411 | "f", | |
6571a64e RC |
412 | }; |
413 | ||
d59d954d ZJS |
414 | size_t i = 0; |
415 | int r; | |
416 | for (const char *p = test;;) { | |
417 | _cleanup_free_ char *word = NULL; | |
418 | ||
419 | r = extract_first_word(&p, &word, NULL, 0); | |
420 | if (r == 0) { | |
421 | assert_se(i == ELEMENTSOF(expected)); | |
422 | break; | |
423 | } | |
424 | assert_se(r > 0); | |
425 | ||
426 | assert_se(streq(expected[i++], word)); | |
427 | } | |
6571a64e RC |
428 | } |
429 | ||
430 | static void check(const char *test, char** expected, bool trailing) { | |
d59d954d ZJS |
431 | size_t i = 0; |
432 | int r; | |
6571a64e RC |
433 | |
434 | printf("<<<%s>>>\n", test); | |
bc8ec170 ZJS |
435 | for (;;) { |
436 | _cleanup_free_ char *word = NULL; | |
437 | ||
4ec85141 | 438 | r = extract_first_word(&test, &word, NULL, EXTRACT_UNQUOTE); |
bc8ec170 ZJS |
439 | if (r == 0) { |
440 | assert_se(!trailing); | |
441 | break; | |
442 | } else if (r < 0) { | |
443 | assert_se(trailing); | |
444 | break; | |
445 | } | |
446 | ||
447 | assert_se(streq(word, expected[i++])); | |
448 | printf("<%s>\n", word); | |
6571a64e | 449 | } |
6571a64e | 450 | assert_se(expected[i] == NULL); |
6571a64e RC |
451 | } |
452 | ||
453 | static void test_foreach_word_quoted(void) { | |
0176728a ZJS |
454 | log_info("/* %s */", __func__); |
455 | ||
6571a64e RC |
456 | check("test a b c 'd' e '' '' hhh '' '' \"a b c\"", |
457 | STRV_MAKE("test", | |
458 | "a", | |
459 | "b", | |
460 | "c", | |
461 | "d", | |
462 | "e", | |
463 | "", | |
464 | "", | |
465 | "hhh", | |
466 | "", | |
467 | "", | |
468 | "a b c"), | |
469 | false); | |
470 | ||
471 | check("test \"xxx", | |
472 | STRV_MAKE("test"), | |
473 | true); | |
474 | ||
475 | check("test\\", | |
476 | STRV_MAKE_EMPTY, | |
477 | true); | |
478 | } | |
479 | ||
480 | static void test_endswith(void) { | |
0176728a ZJS |
481 | log_info("/* %s */", __func__); |
482 | ||
6571a64e RC |
483 | assert_se(endswith("foobar", "bar")); |
484 | assert_se(endswith("foobar", "")); | |
485 | assert_se(endswith("foobar", "foobar")); | |
486 | assert_se(endswith("", "")); | |
487 | ||
488 | assert_se(!endswith("foobar", "foo")); | |
489 | assert_se(!endswith("foobar", "foobarfoofoo")); | |
490 | } | |
491 | ||
492 | static void test_endswith_no_case(void) { | |
0176728a ZJS |
493 | log_info("/* %s */", __func__); |
494 | ||
6571a64e RC |
495 | assert_se(endswith_no_case("fooBAR", "bar")); |
496 | assert_se(endswith_no_case("foobar", "")); | |
497 | assert_se(endswith_no_case("foobar", "FOOBAR")); | |
498 | assert_se(endswith_no_case("", "")); | |
499 | ||
500 | assert_se(!endswith_no_case("foobar", "FOO")); | |
501 | assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO")); | |
502 | } | |
503 | ||
504 | static void test_delete_chars(void) { | |
0176728a ZJS |
505 | log_info("/* %s */", __func__); |
506 | ||
7546145e LP |
507 | char *s, input[] = " hello, waldo. abc"; |
508 | ||
509 | s = delete_chars(input, WHITESPACE); | |
510 | assert_se(streq(s, "hello,waldo.abc")); | |
511 | assert_se(s == input); | |
512 | } | |
513 | ||
514 | static void test_delete_trailing_chars(void) { | |
0176728a | 515 | log_info("/* %s */", __func__); |
7546145e LP |
516 | |
517 | char *s, | |
518 | input1[] = " \n \r k \n \r ", | |
519 | input2[] = "kkkkthiskkkiskkkaktestkkk", | |
520 | input3[] = "abcdef"; | |
521 | ||
522 | s = delete_trailing_chars(input1, WHITESPACE); | |
523 | assert_se(streq(s, " \n \r k")); | |
524 | assert_se(s == input1); | |
525 | ||
526 | s = delete_trailing_chars(input2, "kt"); | |
527 | assert_se(streq(s, "kkkkthiskkkiskkkaktes")); | |
528 | assert_se(s == input2); | |
529 | ||
530 | s = delete_trailing_chars(input3, WHITESPACE); | |
531 | assert_se(streq(s, "abcdef")); | |
532 | assert_se(s == input3); | |
533 | ||
534 | s = delete_trailing_chars(input3, "fe"); | |
535 | assert_se(streq(s, "abcd")); | |
536 | assert_se(s == input3); | |
537 | } | |
538 | ||
ca4d708d | 539 | static void test_delete_trailing_slashes(void) { |
0176728a ZJS |
540 | log_info("/* %s */", __func__); |
541 | ||
ca4d708d ZJS |
542 | char s1[] = "foobar//", |
543 | s2[] = "foobar/", | |
544 | s3[] = "foobar", | |
545 | s4[] = ""; | |
546 | ||
547 | assert_se(streq(delete_trailing_chars(s1, "_"), "foobar//")); | |
548 | assert_se(streq(delete_trailing_chars(s1, "/"), "foobar")); | |
549 | assert_se(streq(delete_trailing_chars(s2, "/"), "foobar")); | |
550 | assert_se(streq(delete_trailing_chars(s3, "/"), "foobar")); | |
551 | assert_se(streq(delete_trailing_chars(s4, "/"), "")); | |
552 | } | |
553 | ||
7546145e | 554 | static void test_skip_leading_chars(void) { |
0176728a ZJS |
555 | log_info("/* %s */", __func__); |
556 | ||
7546145e LP |
557 | char input1[] = " \n \r k \n \r ", |
558 | input2[] = "kkkkthiskkkiskkkaktestkkk", | |
559 | input3[] = "abcdef"; | |
6571a64e | 560 | |
7546145e LP |
561 | assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r ")); |
562 | assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk")); | |
563 | assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk")); | |
564 | assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef")); | |
565 | assert_se(streq(skip_leading_chars(input3, "bcaef"), "def")); | |
6571a64e RC |
566 | } |
567 | ||
568 | static void test_in_charset(void) { | |
0176728a ZJS |
569 | log_info("/* %s */", __func__); |
570 | ||
6571a64e RC |
571 | assert_se(in_charset("dddaaabbbcccc", "abcd")); |
572 | assert_se(!in_charset("dddaaabbbcccc", "abc f")); | |
573 | } | |
574 | ||
575 | static void test_split_pair(void) { | |
0176728a ZJS |
576 | log_info("/* %s */", __func__); |
577 | ||
6571a64e RC |
578 | _cleanup_free_ char *a = NULL, *b = NULL; |
579 | ||
580 | assert_se(split_pair("", "", &a, &b) == -EINVAL); | |
581 | assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL); | |
582 | assert_se(split_pair("", "=", &a, &b) == -EINVAL); | |
583 | assert_se(split_pair("foo=bar", "=", &a, &b) >= 0); | |
584 | assert_se(streq(a, "foo")); | |
585 | assert_se(streq(b, "bar")); | |
586 | free(a); | |
587 | free(b); | |
588 | assert_se(split_pair("==", "==", &a, &b) >= 0); | |
589 | assert_se(streq(a, "")); | |
590 | assert_se(streq(b, "")); | |
591 | free(a); | |
592 | free(b); | |
593 | ||
594 | assert_se(split_pair("===", "==", &a, &b) >= 0); | |
595 | assert_se(streq(a, "")); | |
596 | assert_se(streq(b, "=")); | |
597 | } | |
598 | ||
599 | static void test_first_word(void) { | |
0176728a ZJS |
600 | log_info("/* %s */", __func__); |
601 | ||
6571a64e RC |
602 | assert_se(first_word("Hello", "")); |
603 | assert_se(first_word("Hello", "Hello")); | |
604 | assert_se(first_word("Hello world", "Hello")); | |
605 | assert_se(first_word("Hello\tworld", "Hello")); | |
606 | assert_se(first_word("Hello\nworld", "Hello")); | |
607 | assert_se(first_word("Hello\rworld", "Hello")); | |
608 | assert_se(first_word("Hello ", "Hello")); | |
609 | ||
610 | assert_se(!first_word("Hello", "Hellooo")); | |
611 | assert_se(!first_word("Hello", "xxxxx")); | |
612 | assert_se(!first_word("Hellooo", "Hello")); | |
613 | } | |
614 | ||
7bf7ce28 | 615 | static void test_strlen_ptr(void) { |
0176728a ZJS |
616 | log_info("/* %s */", __func__); |
617 | ||
7bf7ce28 LP |
618 | assert_se(strlen_ptr("foo") == 3); |
619 | assert_se(strlen_ptr("") == 0); | |
620 | assert_se(strlen_ptr(NULL) == 0); | |
621 | } | |
622 | ||
9b8ff183 | 623 | static void test_memory_startswith(void) { |
0176728a ZJS |
624 | log_info("/* %s */", __func__); |
625 | ||
9b8ff183 LP |
626 | assert_se(streq(memory_startswith("", 0, ""), "")); |
627 | assert_se(streq(memory_startswith("", 1, ""), "")); | |
628 | assert_se(streq(memory_startswith("x", 2, ""), "x")); | |
629 | assert_se(!memory_startswith("", 1, "x")); | |
630 | assert_se(!memory_startswith("", 1, "xxxxxxxx")); | |
631 | assert_se(streq(memory_startswith("xxx", 4, "x"), "xx")); | |
632 | assert_se(streq(memory_startswith("xxx", 4, "xx"), "x")); | |
633 | assert_se(streq(memory_startswith("xxx", 4, "xxx"), "")); | |
634 | assert_se(!memory_startswith("xxx", 4, "xxxx")); | |
635 | } | |
636 | ||
21224070 | 637 | static void test_memory_startswith_no_case(void) { |
0176728a ZJS |
638 | log_info("/* %s */", __func__); |
639 | ||
21224070 DP |
640 | assert_se(streq(memory_startswith_no_case("", 0, ""), "")); |
641 | assert_se(streq(memory_startswith_no_case("", 1, ""), "")); | |
642 | assert_se(streq(memory_startswith_no_case("x", 2, ""), "x")); | |
643 | assert_se(streq(memory_startswith_no_case("X", 2, ""), "X")); | |
644 | assert_se(!memory_startswith_no_case("", 1, "X")); | |
645 | assert_se(!memory_startswith_no_case("", 1, "xxxxXXXX")); | |
646 | assert_se(streq(memory_startswith_no_case("xxx", 4, "X"), "xx")); | |
647 | assert_se(streq(memory_startswith_no_case("XXX", 4, "x"), "XX")); | |
648 | assert_se(streq(memory_startswith_no_case("XXX", 4, "X"), "XX")); | |
649 | assert_se(streq(memory_startswith_no_case("xxx", 4, "XX"), "x")); | |
650 | assert_se(streq(memory_startswith_no_case("XXX", 4, "xx"), "X")); | |
651 | assert_se(streq(memory_startswith_no_case("XXX", 4, "XX"), "X")); | |
652 | assert_se(streq(memory_startswith_no_case("xxx", 4, "XXX"), "")); | |
653 | assert_se(streq(memory_startswith_no_case("XXX", 4, "xxx"), "")); | |
654 | assert_se(streq(memory_startswith_no_case("XXX", 4, "XXX"), "")); | |
655 | ||
656 | assert_se(memory_startswith_no_case((char[2]){'x', 'x'}, 2, "xx")); | |
657 | assert_se(memory_startswith_no_case((char[2]){'x', 'X'}, 2, "xX")); | |
658 | assert_se(memory_startswith_no_case((char[2]){'X', 'x'}, 2, "Xx")); | |
659 | assert_se(memory_startswith_no_case((char[2]){'X', 'X'}, 2, "XX")); | |
660 | } | |
661 | ||
8dd6491e LP |
662 | static void test_string_truncate_lines_one(const char *input, size_t n_lines, const char *output, bool truncation) { |
663 | _cleanup_free_ char *b = NULL; | |
664 | int k; | |
665 | ||
666 | assert_se((k = string_truncate_lines(input, n_lines, &b)) >= 0); | |
667 | assert_se(streq(b, output)); | |
668 | assert_se(!!k == truncation); | |
669 | } | |
670 | ||
671 | static void test_string_truncate_lines(void) { | |
0176728a ZJS |
672 | log_info("/* %s */", __func__); |
673 | ||
8dd6491e LP |
674 | test_string_truncate_lines_one("", 0, "", false); |
675 | test_string_truncate_lines_one("", 1, "", false); | |
676 | test_string_truncate_lines_one("", 2, "", false); | |
677 | test_string_truncate_lines_one("", 3, "", false); | |
678 | ||
679 | test_string_truncate_lines_one("x", 0, "", true); | |
680 | test_string_truncate_lines_one("x", 1, "x", false); | |
681 | test_string_truncate_lines_one("x", 2, "x", false); | |
682 | test_string_truncate_lines_one("x", 3, "x", false); | |
683 | ||
684 | test_string_truncate_lines_one("x\n", 0, "", true); | |
685 | test_string_truncate_lines_one("x\n", 1, "x", false); | |
686 | test_string_truncate_lines_one("x\n", 2, "x", false); | |
687 | test_string_truncate_lines_one("x\n", 3, "x", false); | |
688 | ||
689 | test_string_truncate_lines_one("x\ny", 0, "", true); | |
690 | test_string_truncate_lines_one("x\ny", 1, "x", true); | |
691 | test_string_truncate_lines_one("x\ny", 2, "x\ny", false); | |
692 | test_string_truncate_lines_one("x\ny", 3, "x\ny", false); | |
693 | ||
694 | test_string_truncate_lines_one("x\ny\n", 0, "", true); | |
695 | test_string_truncate_lines_one("x\ny\n", 1, "x", true); | |
696 | test_string_truncate_lines_one("x\ny\n", 2, "x\ny", false); | |
697 | test_string_truncate_lines_one("x\ny\n", 3, "x\ny", false); | |
698 | ||
699 | test_string_truncate_lines_one("x\ny\nz", 0, "", true); | |
700 | test_string_truncate_lines_one("x\ny\nz", 1, "x", true); | |
701 | test_string_truncate_lines_one("x\ny\nz", 2, "x\ny", true); | |
702 | test_string_truncate_lines_one("x\ny\nz", 3, "x\ny\nz", false); | |
703 | ||
704 | test_string_truncate_lines_one("x\ny\nz\n", 0, "", true); | |
705 | test_string_truncate_lines_one("x\ny\nz\n", 1, "x", true); | |
706 | test_string_truncate_lines_one("x\ny\nz\n", 2, "x\ny", true); | |
707 | test_string_truncate_lines_one("x\ny\nz\n", 3, "x\ny\nz", false); | |
708 | ||
709 | test_string_truncate_lines_one("\n", 0, "", false); | |
710 | test_string_truncate_lines_one("\n", 1, "", false); | |
711 | test_string_truncate_lines_one("\n", 2, "", false); | |
712 | test_string_truncate_lines_one("\n", 3, "", false); | |
713 | ||
714 | test_string_truncate_lines_one("\n\n", 0, "", false); | |
715 | test_string_truncate_lines_one("\n\n", 1, "", false); | |
716 | test_string_truncate_lines_one("\n\n", 2, "", false); | |
717 | test_string_truncate_lines_one("\n\n", 3, "", false); | |
718 | ||
719 | test_string_truncate_lines_one("\n\n\n", 0, "", false); | |
720 | test_string_truncate_lines_one("\n\n\n", 1, "", false); | |
721 | test_string_truncate_lines_one("\n\n\n", 2, "", false); | |
722 | test_string_truncate_lines_one("\n\n\n", 3, "", false); | |
723 | ||
724 | test_string_truncate_lines_one("\nx\n\n", 0, "", true); | |
725 | test_string_truncate_lines_one("\nx\n\n", 1, "", true); | |
726 | test_string_truncate_lines_one("\nx\n\n", 2, "\nx", false); | |
727 | test_string_truncate_lines_one("\nx\n\n", 3, "\nx", false); | |
728 | ||
729 | test_string_truncate_lines_one("\n\nx\n", 0, "", true); | |
730 | test_string_truncate_lines_one("\n\nx\n", 1, "", true); | |
731 | test_string_truncate_lines_one("\n\nx\n", 2, "", true); | |
732 | test_string_truncate_lines_one("\n\nx\n", 3, "\n\nx", false); | |
733 | } | |
734 | ||
f6857fa6 LP |
735 | static void test_string_extract_lines_one(const char *input, size_t i, const char *output, bool more) { |
736 | _cleanup_free_ char *b = NULL; | |
737 | int k; | |
738 | ||
739 | assert_se((k = string_extract_line(input, i, &b)) >= 0); | |
740 | assert_se(streq(b ?: input, output)); | |
741 | assert_se(!!k == more); | |
742 | } | |
743 | ||
744 | static void test_string_extract_line(void) { | |
0176728a ZJS |
745 | log_info("/* %s */", __func__); |
746 | ||
f6857fa6 LP |
747 | test_string_extract_lines_one("", 0, "", false); |
748 | test_string_extract_lines_one("", 1, "", false); | |
749 | test_string_extract_lines_one("", 2, "", false); | |
750 | test_string_extract_lines_one("", 3, "", false); | |
751 | ||
752 | test_string_extract_lines_one("x", 0, "x", false); | |
753 | test_string_extract_lines_one("x", 1, "", false); | |
754 | test_string_extract_lines_one("x", 2, "", false); | |
755 | test_string_extract_lines_one("x", 3, "", false); | |
756 | ||
757 | test_string_extract_lines_one("x\n", 0, "x", false); | |
758 | test_string_extract_lines_one("x\n", 1, "", false); | |
759 | test_string_extract_lines_one("x\n", 2, "", false); | |
760 | test_string_extract_lines_one("x\n", 3, "", false); | |
761 | ||
762 | test_string_extract_lines_one("x\ny", 0, "x", true); | |
763 | test_string_extract_lines_one("x\ny", 1, "y", false); | |
764 | test_string_extract_lines_one("x\ny", 2, "", false); | |
765 | test_string_extract_lines_one("x\ny", 3, "", false); | |
766 | ||
767 | test_string_extract_lines_one("x\ny\n", 0, "x", true); | |
768 | test_string_extract_lines_one("x\ny\n", 1, "y", false); | |
769 | test_string_extract_lines_one("x\ny\n", 2, "", false); | |
770 | test_string_extract_lines_one("x\ny\n", 3, "", false); | |
771 | ||
772 | test_string_extract_lines_one("x\ny\nz", 0, "x", true); | |
773 | test_string_extract_lines_one("x\ny\nz", 1, "y", true); | |
774 | test_string_extract_lines_one("x\ny\nz", 2, "z", false); | |
775 | test_string_extract_lines_one("x\ny\nz", 3, "", false); | |
776 | ||
777 | test_string_extract_lines_one("\n", 0, "", false); | |
778 | test_string_extract_lines_one("\n", 1, "", false); | |
779 | test_string_extract_lines_one("\n", 2, "", false); | |
780 | test_string_extract_lines_one("\n", 3, "", false); | |
781 | ||
782 | test_string_extract_lines_one("\n\n", 0, "", true); | |
783 | test_string_extract_lines_one("\n\n", 1, "", false); | |
784 | test_string_extract_lines_one("\n\n", 2, "", false); | |
785 | test_string_extract_lines_one("\n\n", 3, "", false); | |
786 | ||
787 | test_string_extract_lines_one("\n\n\n", 0, "", true); | |
788 | test_string_extract_lines_one("\n\n\n", 1, "", true); | |
789 | test_string_extract_lines_one("\n\n\n", 2, "", false); | |
790 | test_string_extract_lines_one("\n\n\n", 3, "", false); | |
791 | ||
792 | test_string_extract_lines_one("\n\n\n\n", 0, "", true); | |
793 | test_string_extract_lines_one("\n\n\n\n", 1, "", true); | |
794 | test_string_extract_lines_one("\n\n\n\n", 2, "", true); | |
795 | test_string_extract_lines_one("\n\n\n\n", 3, "", false); | |
796 | ||
797 | test_string_extract_lines_one("\nx\n\n\n", 0, "", true); | |
798 | test_string_extract_lines_one("\nx\n\n\n", 1, "x", true); | |
799 | test_string_extract_lines_one("\nx\n\n\n", 2, "", true); | |
800 | test_string_extract_lines_one("\nx\n\n\n", 3, "", false); | |
801 | ||
802 | test_string_extract_lines_one("\n\nx\n\n", 0, "", true); | |
803 | test_string_extract_lines_one("\n\nx\n\n", 1, "", true); | |
804 | test_string_extract_lines_one("\n\nx\n\n", 2, "x", true); | |
805 | test_string_extract_lines_one("\n\nx\n\n", 3, "", false); | |
806 | ||
807 | test_string_extract_lines_one("\n\n\nx\n", 0, "", true); | |
808 | test_string_extract_lines_one("\n\n\nx\n", 1, "", true); | |
809 | test_string_extract_lines_one("\n\n\nx\n", 2, "", true); | |
810 | test_string_extract_lines_one("\n\n\nx\n", 3, "x", false); | |
811 | } | |
812 | ||
46bf625a ZJS |
813 | static void test_string_contains_word_strv(void) { |
814 | log_info("/* %s */", __func__); | |
815 | ||
816 | const char *w; | |
817 | ||
818 | assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), NULL)); | |
819 | ||
820 | assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), &w)); | |
821 | assert_se(streq(w, "a")); | |
822 | ||
823 | assert_se(!string_contains_word_strv("a b cc", NULL, STRV_MAKE("d"), &w)); | |
824 | assert_se(w == NULL); | |
825 | ||
826 | assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("b", "a"), &w)); | |
827 | assert_se(streq(w, "a")); | |
828 | ||
829 | assert_se(string_contains_word_strv("b a b cc", NULL, STRV_MAKE("b", "a", "b"), &w)); | |
830 | assert_se(streq(w, "b")); | |
831 | ||
832 | assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("b", ""), &w)); | |
833 | assert_se(streq(w, "b")); | |
834 | ||
835 | assert_se(!string_contains_word_strv("a b cc", NULL, STRV_MAKE(""), &w)); | |
836 | assert_se(w == NULL); | |
837 | ||
838 | assert_se(string_contains_word_strv("a b cc", " ", STRV_MAKE(""), &w)); | |
839 | assert_se(streq(w, "")); | |
840 | } | |
841 | ||
53cd7f33 ZJS |
842 | static void test_string_contains_word(void) { |
843 | log_info("/* %s */", __func__); | |
844 | ||
845 | assert_se( string_contains_word("a b cc", NULL, "a")); | |
846 | assert_se( string_contains_word("a b cc", NULL, "b")); | |
847 | assert_se(!string_contains_word("a b cc", NULL, "c")); | |
848 | assert_se( string_contains_word("a b cc", NULL, "cc")); | |
849 | assert_se(!string_contains_word("a b cc", NULL, "d")); | |
850 | assert_se(!string_contains_word("a b cc", NULL, "a b")); | |
851 | assert_se(!string_contains_word("a b cc", NULL, "a b c")); | |
852 | assert_se(!string_contains_word("a b cc", NULL, "b c")); | |
853 | assert_se(!string_contains_word("a b cc", NULL, "b cc")); | |
854 | assert_se(!string_contains_word("a b cc", NULL, "a ")); | |
855 | assert_se(!string_contains_word("a b cc", NULL, " b ")); | |
856 | assert_se(!string_contains_word("a b cc", NULL, " cc")); | |
857 | ||
858 | assert_se( string_contains_word(" a b\t\tcc", NULL, "a")); | |
859 | assert_se( string_contains_word(" a b\t\tcc", NULL, "b")); | |
860 | assert_se(!string_contains_word(" a b\t\tcc", NULL, "c")); | |
861 | assert_se( string_contains_word(" a b\t\tcc", NULL, "cc")); | |
862 | assert_se(!string_contains_word(" a b\t\tcc", NULL, "d")); | |
863 | assert_se(!string_contains_word(" a b\t\tcc", NULL, "a b")); | |
864 | assert_se(!string_contains_word(" a b\t\tcc", NULL, "a b\t\tc")); | |
865 | assert_se(!string_contains_word(" a b\t\tcc", NULL, "b\t\tc")); | |
866 | assert_se(!string_contains_word(" a b\t\tcc", NULL, "b\t\tcc")); | |
867 | assert_se(!string_contains_word(" a b\t\tcc", NULL, "a ")); | |
868 | assert_se(!string_contains_word(" a b\t\tcc", NULL, " b ")); | |
869 | assert_se(!string_contains_word(" a b\t\tcc", NULL, " cc")); | |
870 | ||
871 | assert_se(!string_contains_word(" a b\t\tcc", NULL, "")); | |
872 | assert_se(!string_contains_word(" a b\t\tcc", NULL, " ")); | |
873 | assert_se(!string_contains_word(" a b\t\tcc", NULL, " ")); | |
874 | assert_se( string_contains_word(" a b\t\tcc", " ", "")); | |
875 | assert_se( string_contains_word(" a b\t\tcc", "\t", "")); | |
876 | assert_se( string_contains_word(" a b\t\tcc", WHITESPACE, "")); | |
877 | ||
878 | assert_se( string_contains_word("a:b:cc", ":#", "a")); | |
879 | assert_se( string_contains_word("a:b:cc", ":#", "b")); | |
880 | assert_se(!string_contains_word("a:b:cc", ":#", "c")); | |
881 | assert_se( string_contains_word("a:b:cc", ":#", "cc")); | |
882 | assert_se(!string_contains_word("a:b:cc", ":#", "d")); | |
883 | assert_se(!string_contains_word("a:b:cc", ":#", "a:b")); | |
884 | assert_se(!string_contains_word("a:b:cc", ":#", "a:b:c")); | |
885 | assert_se(!string_contains_word("a:b:cc", ":#", "b:c")); | |
886 | assert_se(!string_contains_word("a#b#cc", ":#", "b:cc")); | |
887 | assert_se( string_contains_word("a#b#cc", ":#", "b")); | |
888 | assert_se( string_contains_word("a#b#cc", ":#", "cc")); | |
889 | assert_se(!string_contains_word("a:b:cc", ":#", "a:")); | |
890 | assert_se(!string_contains_word("a:b cc", ":#", "b")); | |
891 | assert_se( string_contains_word("a:b cc", ":#", "b cc")); | |
892 | assert_se(!string_contains_word("a:b:cc", ":#", ":cc")); | |
893 | } | |
894 | ||
87b7d9b6 YW |
895 | static void test_strverscmp_improved_one(const char *newer, const char *older) { |
896 | log_info("/* %s(%s, %s) */", __func__, strnull(newer), strnull(older)); | |
897 | ||
898 | assert_se(strverscmp_improved(newer, newer) == 0); | |
899 | assert_se(strverscmp_improved(newer, older) > 0); | |
900 | assert_se(strverscmp_improved(older, newer) < 0); | |
901 | assert_se(strverscmp_improved(older, older) == 0); | |
902 | } | |
903 | ||
904 | static void test_strverscmp_improved(void) { | |
905 | static const char * const versions[] = { | |
906 | "", | |
907 | "~1", | |
908 | "ab", | |
909 | "abb", | |
910 | "abc", | |
911 | "0001", | |
912 | "002", | |
913 | "12", | |
914 | "122", | |
915 | "122.9", | |
916 | "123~rc1", | |
917 | "123", | |
918 | "123-a", | |
919 | "123-a.1", | |
920 | "123-a1", | |
921 | "123-a1.1", | |
922 | "123-3", | |
923 | "123-3.1", | |
924 | "123^patch1", | |
925 | "123^1", | |
8aa35d09 | 926 | "123.a-1", |
87b7d9b6 YW |
927 | "123.1-1", |
928 | "123a-1", | |
929 | "124", | |
930 | NULL, | |
931 | }; | |
932 | const char * const *p, * const *q; | |
933 | ||
934 | STRV_FOREACH(p, versions) | |
935 | STRV_FOREACH(q, p + 1) | |
936 | test_strverscmp_improved_one(*q, *p); | |
937 | ||
938 | test_strverscmp_improved_one("123.45-67.89", "123.45-67.88"); | |
939 | test_strverscmp_improved_one("123.45-67.89a", "123.45-67.89"); | |
940 | test_strverscmp_improved_one("123.45-67.89", "123.45-67.ab"); | |
941 | test_strverscmp_improved_one("123.45-67.89", "123.45-67.9"); | |
942 | test_strverscmp_improved_one("123.45-67.89", "123.45-67"); | |
943 | test_strverscmp_improved_one("123.45-67.89", "123.45-66.89"); | |
944 | test_strverscmp_improved_one("123.45-67.89", "123.45-9.99"); | |
945 | test_strverscmp_improved_one("123.45-67.89", "123.42-99.99"); | |
946 | test_strverscmp_improved_one("123.45-67.89", "123-99.99"); | |
947 | ||
948 | /* '~' : pre-releases */ | |
949 | test_strverscmp_improved_one("123.45-67.89", "123~rc1-99.99"); | |
950 | test_strverscmp_improved_one("123-45.67.89", "123~rc1-99.99"); | |
951 | test_strverscmp_improved_one("123~rc2-67.89", "123~rc1-99.99"); | |
952 | test_strverscmp_improved_one("123^aa2-67.89", "123~rc1-99.99"); | |
953 | test_strverscmp_improved_one("123aa2-67.89", "123~rc1-99.99"); | |
954 | ||
955 | /* '-' : separator between version and release. */ | |
956 | test_strverscmp_improved_one("123.45-67.89", "123-99.99"); | |
957 | test_strverscmp_improved_one("123^aa2-67.89", "123-99.99"); | |
958 | test_strverscmp_improved_one("123aa2-67.89", "123-99.99"); | |
959 | ||
960 | /* '^' : patch releases */ | |
961 | test_strverscmp_improved_one("123.45-67.89", "123^45-67.89"); | |
962 | test_strverscmp_improved_one("123^aa2-67.89", "123^aa1-99.99"); | |
963 | test_strverscmp_improved_one("123aa2-67.89", "123^aa2-67.89"); | |
964 | ||
965 | /* '.' : point release */ | |
966 | test_strverscmp_improved_one("123aa2-67.89", "123.aa2-67.89"); | |
967 | test_strverscmp_improved_one("123.ab2-67.89", "123.aa2-67.89"); | |
968 | ||
969 | /* invalid characters */ | |
970 | assert_se(strverscmp_improved("123_aa2-67.89", "123aa+2-67.89") == 0); | |
971 | } | |
972 | ||
9fe4ea21 | 973 | int main(int argc, char *argv[]) { |
7f546026 ZJS |
974 | test_setup_logging(LOG_DEBUG); |
975 | ||
53caaffd | 976 | test_string_erase(); |
7f546026 | 977 | test_free_and_strndup(); |
522d85ae | 978 | test_ascii_strcasecmp_n(); |
c1749834 | 979 | test_ascii_strcasecmp_nn(); |
8409f688 | 980 | test_cellescape(); |
6571a64e RC |
981 | test_streq_ptr(); |
982 | test_strstrip(); | |
983 | test_strextend(); | |
bb8ad9ea | 984 | test_strextend_with_separator(); |
6571a64e | 985 | test_strrep(); |
6571a64e RC |
986 | test_string_has_cc(); |
987 | test_ascii_strlower(); | |
988 | test_strshorten(); | |
989 | test_strjoina(); | |
7eac7b4c | 990 | test_strjoin(); |
6571a64e RC |
991 | test_strcmp_ptr(); |
992 | test_foreach_word(); | |
993 | test_foreach_word_quoted(); | |
994 | test_endswith(); | |
995 | test_endswith_no_case(); | |
996 | test_delete_chars(); | |
7546145e | 997 | test_delete_trailing_chars(); |
ca4d708d | 998 | test_delete_trailing_slashes(); |
7546145e | 999 | test_skip_leading_chars(); |
6571a64e RC |
1000 | test_in_charset(); |
1001 | test_split_pair(); | |
1002 | test_first_word(); | |
7bf7ce28 | 1003 | test_strlen_ptr(); |
9b8ff183 | 1004 | test_memory_startswith(); |
21224070 | 1005 | test_memory_startswith_no_case(); |
8dd6491e | 1006 | test_string_truncate_lines(); |
f6857fa6 | 1007 | test_string_extract_line(); |
46bf625a | 1008 | test_string_contains_word_strv(); |
53cd7f33 | 1009 | test_string_contains_word(); |
87b7d9b6 | 1010 | test_strverscmp_improved(); |
6571a64e | 1011 | |
9fe4ea21 LP |
1012 | return 0; |
1013 | } |