diff options
author | Mark Hindley <mark@hindley.org.uk> | 2018-11-12 09:10:28 +0000 |
---|---|---|
committer | Mark Hindley <mark@hindley.org.uk> | 2018-11-12 09:21:21 +0000 |
commit | 2cc17d30309a1db16cccbf376a59ae40e47b6959 (patch) | |
tree | cd51ee95799631af348ebae8630a69219bee99cd /src/test/test-string-util.c | |
parent | ae65e91a5439f395e0da0cd8ffda95d6289849e1 (diff) | |
parent | d4a3f291e3955648ea1d29e674b0f8f9b1556257 (diff) |
Merge remote-tracking branch 'upstream/v239-stable' into merge_upstream.
Diffstat (limited to 'src/test/test-string-util.c')
-rw-r--r-- | src/test/test-string-util.c | 116 |
1 files changed, 115 insertions, 1 deletions
diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index 202af8bcc..54f9b29ef 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -5,6 +5,7 @@ #include "macro.h" #include "string-util.h" #include "strv.h" +//#include "tests.h" #include "utf8.h" static void test_string_erase(void) { @@ -30,6 +31,64 @@ static void test_string_erase(void) { assert_se(x[9] == '\0'); } +static void test_free_and_strndup_one(char **t, const char *src, size_t l, const char *expected, bool change) { + int r; + + log_debug("%s: \"%s\", \"%s\", %zd (expect \"%s\", %s)", + __func__, strnull(*t), strnull(src), l, strnull(expected), yes_no(change)); + + r = free_and_strndup(t, src, l); + assert_se(streq_ptr(*t, expected)); + assert_se(r == change); /* check that change occurs only when necessary */ +} + +static void test_free_and_strndup(void) { + static const struct test_case { + const char *src; + size_t len; + const char *expected; + } cases[] = { + {"abc", 0, ""}, + {"abc", 0, ""}, + {"abc", 1, "a"}, + {"abc", 2, "ab"}, + {"abc", 3, "abc"}, + {"abc", 4, "abc"}, + {"abc", 5, "abc"}, + {"abc", 5, "abc"}, + {"abc", 4, "abc"}, + {"abc", 3, "abc"}, + {"abc", 2, "ab"}, + {"abc", 1, "a"}, + {"abc", 0, ""}, + + {"", 0, ""}, + {"", 1, ""}, + {"", 2, ""}, + {"", 0, ""}, + {"", 1, ""}, + {"", 2, ""}, + {"", 2, ""}, + {"", 1, ""}, + {"", 0, ""}, + + {NULL, 0, NULL}, + + {"foo", 3, "foo"}, + {"foobar", 6, "foobar"}, + }; + + _cleanup_free_ char *t = NULL; + const char *prev_expected = t; + + for (unsigned i = 0; i < ELEMENTSOF(cases); i++) { + test_free_and_strndup_one(&t, + cases[i].src, cases[i].len, cases[i].expected, + !streq_ptr(cases[i].expected, prev_expected)); + prev_expected = t; + } +} + #if 0 /// UNNEEDED by elogind static void test_ascii_strcasecmp_n(void) { @@ -112,6 +171,9 @@ static void test_cellescape(void) { assert_se(streq(cellescape(buf, 1, "\020"), "")); assert_se(streq(cellescape(buf, 2, "\020"), ".")); assert_se(streq(cellescape(buf, 3, "\020"), "..")); +#if 1 /// elogind supports systems with non-UTF-8 locales, the next would fail there + if (is_locale_utf8()) { +#endif // 1 assert_se(streq(cellescape(buf, 4, "\020"), "…")); assert_se(streq(cellescape(buf, 5, "\020"), "\\020")); @@ -119,26 +181,47 @@ static void test_cellescape(void) { assert_se(streq(cellescape(buf, 6, "1234\020"), "12…")); assert_se(streq(cellescape(buf, 7, "1234\020"), "123…")); assert_se(streq(cellescape(buf, 8, "1234\020"), "1234…")); +#if 1 /// elogind supports systems with non-UTF-8 locales, the previous would fail there + } +#endif // 1 assert_se(streq(cellescape(buf, 9, "1234\020"), "1234\\020")); assert_se(streq(cellescape(buf, 1, "\t\n"), "")); assert_se(streq(cellescape(buf, 2, "\t\n"), ".")); assert_se(streq(cellescape(buf, 3, "\t\n"), "..")); +#if 1 /// elogind supports systems with non-UTF-8 locales, the next would fail there + if (is_locale_utf8()) { +#endif // 1 assert_se(streq(cellescape(buf, 4, "\t\n"), "…")); +#if 1 /// elogind supports systems with non-UTF-8 locales, the previous would fail there + } +#endif // 1 assert_se(streq(cellescape(buf, 5, "\t\n"), "\\t\\n")); +#if 1 /// elogind supports systems with non-UTF-8 locales, the next would fail there + if (is_locale_utf8()) { +#endif // 1 assert_se(streq(cellescape(buf, 5, "1234\t\n"), "1…")); assert_se(streq(cellescape(buf, 6, "1234\t\n"), "12…")); assert_se(streq(cellescape(buf, 7, "1234\t\n"), "123…")); assert_se(streq(cellescape(buf, 8, "1234\t\n"), "1234…")); +#if 1 /// elogind supports systems with non-UTF-8 locales, the previous would fail there + } +#endif // 1 assert_se(streq(cellescape(buf, 9, "1234\t\n"), "1234\\t\\n")); +#if 1 /// elogind supports systems with non-UTF-8 locales, the next would fail there + if (is_locale_utf8()) { +#endif // 1 assert_se(streq(cellescape(buf, 4, "x\t\020\n"), "…")); assert_se(streq(cellescape(buf, 5, "x\t\020\n"), "x…")); assert_se(streq(cellescape(buf, 6, "x\t\020\n"), "x…")); assert_se(streq(cellescape(buf, 7, "x\t\020\n"), "x\\t…")); assert_se(streq(cellescape(buf, 8, "x\t\020\n"), "x\\t…")); assert_se(streq(cellescape(buf, 9, "x\t\020\n"), "x\\t…")); +#if 1 /// elogind supports systems with non-UTF-8 locales, the previous would fail there + } +#endif // 1 assert_se(streq(cellescape(buf, 10, "x\t\020\n"), "x\\t\\020\\n")); assert_se(streq(cellescape(buf, 6, "1\011"), "1\\t")); @@ -497,11 +580,39 @@ static void test_memory_startswith(void) { assert_se(streq(memory_startswith("xxx", 4, "x"), "xx")); assert_se(streq(memory_startswith("xxx", 4, "xx"), "x")); assert_se(streq(memory_startswith("xxx", 4, "xxx"), "")); +#if 0 /// This is not used anywhere in elogind and thus masked. assert_se(!memory_startswith("xxx", 4, "xxxx")); +#endif // 0 } +#if 0 /// This is not used anywhere in elogind and thus masked. +static void test_memory_startswith_no_case(void) { + assert_se(streq(memory_startswith_no_case("", 0, ""), "")); + assert_se(streq(memory_startswith_no_case("", 1, ""), "")); + assert_se(streq(memory_startswith_no_case("x", 2, ""), "x")); + assert_se(streq(memory_startswith_no_case("X", 2, ""), "X")); + assert_se(!memory_startswith_no_case("", 1, "X")); + assert_se(!memory_startswith_no_case("", 1, "xxxxXXXX")); + assert_se(streq(memory_startswith_no_case("xxx", 4, "X"), "xx")); + assert_se(streq(memory_startswith_no_case("XXX", 4, "x"), "XX")); + assert_se(streq(memory_startswith_no_case("XXX", 4, "X"), "XX")); + assert_se(streq(memory_startswith_no_case("xxx", 4, "XX"), "x")); + assert_se(streq(memory_startswith_no_case("XXX", 4, "xx"), "X")); + assert_se(streq(memory_startswith_no_case("XXX", 4, "XX"), "X")); + assert_se(streq(memory_startswith_no_case("xxx", 4, "XXX"), "")); + assert_se(streq(memory_startswith_no_case("XXX", 4, "xxx"), "")); + assert_se(streq(memory_startswith_no_case("XXX", 4, "XXX"), "")); + + assert_se(memory_startswith_no_case((char[2]){'x', 'x'}, 2, "xx")); + assert_se(memory_startswith_no_case((char[2]){'x', 'X'}, 2, "xX")); + assert_se(memory_startswith_no_case((char[2]){'X', 'x'}, 2, "Xx")); + assert_se(memory_startswith_no_case((char[2]){'X', 'X'}, 2, "XX")); +} +#endif // 0 + int main(int argc, char *argv[]) { test_string_erase(); + test_free_and_strndup(); #if 0 /// UNNEEDED by elogind test_ascii_strcasecmp_n(); test_ascii_strcasecmp_nn(); @@ -533,6 +644,9 @@ int main(int argc, char *argv[]) { test_first_word(); test_strlen_ptr(); test_memory_startswith(); - +#if 0 /// This is not used anywhere in elogind and thus masked. + test_memory_startswith_no_case(); +#endif // 0 + return 0; } |