HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/test/test-strv.c

45,913 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 33480a8a259518560496d1fe51d1c2c095ea1c35

Lines 1001–1374 of 1,374

        d = strv_new("foo", "bar", "waldo");
        assert_se(d);
        strv_reverse(d);
        ASSERT_STREQ(d[0], "waldo");
        ASSERT_STREQ(d[1], "bar");
        ASSERT_STREQ(d[2], "foo");
        ASSERT_STREQ(d[3], NULL);
}
 
TEST(strv_shell_escape) {
        _cleanup_strv_free_ char **v = NULL;
 
        v = strv_new("foo:bar", "bar,baz", "wal\\do");
        assert_se(v);
        assert_se(strv_shell_escape(v, ",:"));
        ASSERT_STREQ(v[0], "foo\\:bar");
        ASSERT_STREQ(v[1], "bar\\,baz");
        ASSERT_STREQ(v[2], "wal\\\\do");
        ASSERT_STREQ(v[3], NULL);
}
 
static void test_strv_skip_one(char **a, size_t n, char **b) {
        a = strv_skip(a, n);
        assert_se(strv_equal(a, b));
}
 
TEST(strv_skip) {
        test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
        test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
        test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
        test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, NULL);
        test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, NULL);
        test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, NULL);
 
        test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
        test_strv_skip_one(STRV_MAKE("quux"), 1, NULL);
        test_strv_skip_one(STRV_MAKE("quux"), 55, NULL);
 
        test_strv_skip_one(STRV_MAKE(NULL), 0, NULL);
        test_strv_skip_one(STRV_MAKE(NULL), 1, NULL);
        test_strv_skip_one(STRV_MAKE(NULL), 55, NULL);
 
        test_strv_skip_one(NULL, 0, NULL);
        test_strv_skip_one(NULL, 1, NULL);
        test_strv_skip_one(NULL, 55, NULL);
}
 
TEST(strv_extend_n) {
        _cleanup_strv_free_ char **v = NULL;
 
        v = strv_new("foo", "bar");
        assert_se(v);
 
        assert_se(strv_extend_n(&v, "waldo", 3) >= 0);
        assert_se(strv_extend_n(&v, "piep", 2) >= 0);
 
        ASSERT_STREQ(v[0], "foo");
        ASSERT_STREQ(v[1], "bar");
        ASSERT_STREQ(v[2], "waldo");
        ASSERT_STREQ(v[3], "waldo");
        ASSERT_STREQ(v[4], "waldo");
        ASSERT_STREQ(v[5], "piep");
        ASSERT_STREQ(v[6], "piep");
        ASSERT_NULL(v[7]);
 
        v = strv_free(v);
 
        assert_se(strv_extend_n(&v, "foo", 1) >= 0);
        assert_se(strv_extend_n(&v, "bar", 0) >= 0);
 
        ASSERT_STREQ(v[0], "foo");
        ASSERT_NULL(v[1]);
}
 
TEST(foreach_string) {
        const char * const t[] = {
                "foo",
                "bar",
                "waldo",
                NULL
        };
 
        unsigned i = 0;
        FOREACH_STRING(x, "foo", "bar", "waldo")
                ASSERT_STREQ(t[i++], x);
        assert_se(i == 3);
 
        FOREACH_STRING(x, "zzz")
                ASSERT_STREQ(x, "zzz");
}
 
TEST(strv_fnmatch) {
        _cleanup_strv_free_ char **v = NULL;
        size_t pos;
 
        assert_se(!strv_fnmatch(STRV_EMPTY, "a"));
 
        v = strv_new("xxx", "*\\*", "yyy");
        assert_se(!strv_fnmatch_full(v, "\\", 0, NULL));
        assert_se(strv_fnmatch_full(v, "\\", FNM_NOESCAPE, &pos));
        assert_se(pos == 1);
}
 
TEST(strv_extend_join) {
        _cleanup_strv_free_ char **v = NULL;
 
        assert_se(strv_extend_assignment(&v, "MESSAGE", "ABC") >= 0);
        assert_se(strv_extend_assignment(&v, "ABC", "QER") >= 0);
        assert_se(strv_extend_assignment(&v, "MISSING", NULL) >= 0);
 
        assert_se(strv_length(v) == 2);
        ASSERT_STREQ(v[0], "MESSAGE=ABC");
        ASSERT_STREQ(v[1], "ABC=QER");
}
 
TEST(strv_copy_n) {
        char **x = STRV_MAKE("a", "b", "c", "d", "e");
        _cleanup_strv_free_ char **l = NULL;
 
        l = strv_copy_n(x, 0);
        assert_se(strv_equal(l, NULL));
        strv_free(l);
 
        l = strv_copy_n(x, 0);
        assert_se(strv_equal(l, (char**) { NULL }));
        strv_free(l);
 
        l = strv_copy_n(x, 1);
        assert_se(strv_equal(l, STRV_MAKE("a")));
        strv_free(l);
 
        l = strv_copy_n(x, 2);
        assert_se(strv_equal(l, STRV_MAKE("a", "b")));
        strv_free(l);
 
        l = strv_copy_n(x, 3);
        assert_se(strv_equal(l, STRV_MAKE("a", "b", "c")));
        strv_free(l);
 
        l = strv_copy_n(x, 4);
        assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d")));
        strv_free(l);
 
        l = strv_copy_n(x, 5);
        assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
        strv_free(l);
 
        l = strv_copy_n(x, 6);
        assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
        strv_free(l);
 
        l = strv_copy_n(x, SIZE_MAX);
        assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
}
 
TEST(strv_find_first_field) {
        char **haystack = STRV_MAKE("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
 
        ASSERT_NULL(strv_find_first_field(NULL, NULL));
        ASSERT_NULL(strv_find_first_field(NULL, haystack));
        ASSERT_NULL(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "b"), NULL));
        ASSERT_NULL(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "b"), haystack));
        ASSERT_STREQ(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "a", "c"), haystack), "b");
        ASSERT_STREQ(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "c", "a"), haystack), "d");
        ASSERT_STREQ(strv_find_first_field(STRV_MAKE("i", "k", "l", "m", "d", "c", "a", "b"), haystack), "j");
}
 
TEST(endswith_strv) {
        ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "ldo", "zzz")), "ldo");
        ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "zzz")), NULL);
        ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("waldo")), "waldo");
        ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("w", "o", "ldo")), "o");
        ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("knurz", "", "waldo")), "");
}
 
TEST(strv_extend_many) {
        _cleanup_strv_free_ char **l = NULL;
 
        assert_se(strv_extend_many(&l, NULL) >= 0);
        assert_se(strv_isempty(l));
 
        assert_se(strv_extend_many(&l, NULL, NULL, NULL) >= 0);
        assert_se(strv_isempty(l));
 
        assert_se(strv_extend_many(&l, "foo") >= 0);
        assert_se(strv_equal(l, STRV_MAKE("foo")));
 
        assert_se(strv_extend_many(&l, NULL, "bar", NULL) >= 0);
        assert_se(strv_equal(l, STRV_MAKE("foo", "bar")));
 
        assert_se(strv_extend_many(&l, "waldo", "quux") >= 0);
        assert_se(strv_equal(l, STRV_MAKE("foo", "bar", "waldo", "quux")));
 
        assert_se(strv_extend_many(&l, "1", "2", "3", "4") >= 0);
        assert_se(strv_equal(l, STRV_MAKE("foo", "bar", "waldo", "quux", "1", "2", "3", "4")));
 
        assert_se(strv_extend_many(&l, "yes", NULL, "no") >= 0);
        assert_se(strv_equal(l, STRV_MAKE("foo", "bar", "waldo", "quux", "1", "2", "3", "4", "yes", "no")));
}
 
TEST(strv_rebreak_lines) {
        _cleanup_strv_free_ char **l = NULL;
 
        assert_se(strv_rebreak_lines(NULL, SIZE_MAX, &l) >= 0);
        assert_se(strv_equal(l, NULL));
        l = strv_free(l);
 
        assert_se(strv_rebreak_lines(STRV_MAKE(""), SIZE_MAX, &l) >= 0);
        assert_se(strv_equal(l, STRV_MAKE("")));
        l = strv_free(l);
 
        assert_se(strv_rebreak_lines(STRV_MAKE("", ""), SIZE_MAX, &l) >= 0);
        assert_se(strv_equal(l, STRV_MAKE("", "")));
        l = strv_free(l);
 
        assert_se(strv_rebreak_lines(STRV_MAKE("foo"), SIZE_MAX, &l) >= 0);
        assert_se(strv_equal(l, STRV_MAKE("foo")));
        l = strv_free(l);
 
        assert_se(strv_rebreak_lines(STRV_MAKE("foo", "bar"), SIZE_MAX, &l) >= 0);
        assert_se(strv_equal(l, STRV_MAKE("foo", "bar")));
        l = strv_free(l);
 
        assert_se(strv_rebreak_lines(STRV_MAKE("Foo fOo foO FOo", "bar Bar bAr baR BAr"), 10, &l) >= 0);
        assert_se(strv_equal(l, STRV_MAKE("Foo fOo", "foO FOo", "bar Bar", "bAr baR", "BAr")));
        l = strv_free(l);
 
        assert_se(strv_rebreak_lines(STRV_MAKE("           foo               ",
                                               "             foo bar               waldo quux         "),
                                     10, &l) >= 0);
        assert_se(strv_equal(l, STRV_MAKE("           foo",
                                          "             foo",
                                          "bar",
                                          "waldo quux")));
        l = strv_free(l);
 
        assert_se(strv_rebreak_lines(STRV_MAKE("            ",
                                               "\tfoo bar\t",
                                               "FOO\tBAR"),
                                     10, &l) >= 0);
        assert_se(strv_equal(l, STRV_MAKE("",
                                          "\tfoo",
                                          "bar",
                                          "FOO",
                                          "BAR")));
        l = strv_free(l);
 
        /* Now make sure that breaking the lines a 2nd time does not modify the output anymore */
        for (size_t i = 1; i < 100; i++) {
                _cleanup_strv_free_ char **a = NULL, **b = NULL;
 
                assert_se(strv_rebreak_lines(STRV_MAKE("foobar waldo waldo quux piep\tschnurz    pimm"), i, &a) >= 0);
                assert_se(strv_rebreak_lines(a, i, &b) >= 0);
 
                assert_se(strv_equal(a, b));
        }
 
        /* Invalid UTF-8 is refused rather than measured. Previously a truncated multibyte lead byte at the
         * end of a line was blind-skipped by its expected length, stepping over the NUL and reading past
         * the end of the string. */
        ASSERT_ERROR(strv_rebreak_lines(STRV_MAKE("foo\xF0"), 10, &l), EINVAL);
        ASSERT_NULL(l);
 
        ASSERT_ERROR(strv_rebreak_lines(STRV_MAKE("bar\xFC"), 10, &l), EINVAL);
        ASSERT_NULL(l);
 
        /* Same, but reached after a line break, i.e. with the scan restarted at the character following
         * the whitespace we broke at. */
        ASSERT_ERROR(strv_rebreak_lines(STRV_MAKE("a \xF0" "b"), 3, &l), EINVAL);
        ASSERT_NULL(l);
 
        /* Newlines embedded in the input are hard line breaks. Previously everything before such a newline
         * was silently dropped if nothing but whitespace followed it. */
        ASSERT_OK(strv_rebreak_lines(STRV_MAKE("foo\n"), 10, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("foo")));
        l = strv_free(l);
 
        ASSERT_OK(strv_rebreak_lines(STRV_MAKE("foo\n   "), 10, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("foo")));
        l = strv_free(l);
 
        ASSERT_OK(strv_rebreak_lines(STRV_MAKE("foo\nbar"), 10, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("foo", "bar")));
        l = strv_free(l);
 
        ASSERT_OK(strv_rebreak_lines(STRV_MAKE("foo   \n   bar   \n"), 10, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("foo", "   bar")));
        l = strv_free(l);
 
        ASSERT_OK(strv_rebreak_lines(STRV_MAKE("foo\n\nbar"), 10, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("foo", "", "bar")));
        l = strv_free(l);
 
        ASSERT_OK(strv_rebreak_lines(STRV_MAKE("foo\r\nbar\r\n"), 10, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("foo", "bar")));
        l = strv_free(l);
 
        /* And the resulting lines are properly broken to the requested width, rather than staying glued
         * together in a single, over-long entry. */
        ASSERT_OK(strv_rebreak_lines(STRV_MAKE("hello world\nfoo bar baz quux waldo"), 10, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("hello", "world", "foo bar", "baz quux", "waldo")));
        l = strv_free(l);
}
 
TEST(strv_find_closest) {
        char **l = STRV_MAKE("aaa", "aaaa", "bbb", "ccc");
 
        /* prefix match */
        ASSERT_STREQ(strv_find_closest(l, "a"),    "aaa");
        ASSERT_STREQ(strv_find_closest(l, "aa"),   "aaa");
        ASSERT_STREQ(strv_find_closest(l, "aaa"),  "aaa");
        ASSERT_STREQ(strv_find_closest(l, "aaaa"), "aaaa");
        ASSERT_STREQ(strv_find_closest(l, "b"),    "bbb");
        ASSERT_STREQ(strv_find_closest(l, "bb"),   "bbb");
        ASSERT_STREQ(strv_find_closest(l, "bbb"),  "bbb");
        ASSERT_STREQ(strv_find_closest(l, "c"),    "ccc");
        ASSERT_STREQ(strv_find_closest(l, "cc"),   "ccc");
        ASSERT_STREQ(strv_find_closest(l, "ccc"),  "ccc");
 
        /* levenshtein match */
        ASSERT_STREQ(strv_find_closest(l, "aab"),  "aaa");
        ASSERT_STREQ(strv_find_closest(l, "abb"),  "bbb");
        ASSERT_STREQ(strv_find_closest(l, "cbc"),  "ccc");
        ASSERT_STREQ(strv_find_closest(l, "aax"),  "aaa");
        ASSERT_STREQ(strv_find_closest(l, "bbbb"), "bbb");
        ASSERT_STREQ(strv_find_closest(l, "cbbb"), "bbb");
        ASSERT_STREQ(strv_find_closest(l, "bbbx"), "bbb");
 
        ASSERT_NULL(strv_find_closest(l, "sfajosajfosdjaofjdsaf"));
}
 
TEST(strv_equal_ignore_order) {
 
        ASSERT_TRUE(strv_equal_ignore_order(NULL, NULL));
        ASSERT_TRUE(strv_equal_ignore_order(NULL, STRV_MAKE(NULL)));
        ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE(NULL), NULL));
        ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE(NULL), STRV_MAKE(NULL)));
 
        ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("foo"), NULL));
        ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("foo"), STRV_MAKE(NULL)));
        ASSERT_FALSE(strv_equal_ignore_order(NULL, STRV_MAKE("foo")));
        ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE(NULL), STRV_MAKE("foo")));
        ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("foo"), STRV_MAKE("foo")));
        ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("foo"), STRV_MAKE("foo", "bar")));
        ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("foo", "bar"), STRV_MAKE("foo")));
        ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("foo", "bar"), STRV_MAKE("foo", "bar")));
        ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("bar", "foo"), STRV_MAKE("foo", "bar")));
        ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("bar", "foo"), STRV_MAKE("foo", "bar", "quux")));
        ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("bar", "foo", "quux"), STRV_MAKE("foo", "bar")));
        ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("bar", "foo", "quux"), STRV_MAKE("quux", "foo", "bar")));
        ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("bar", "foo"), STRV_MAKE("bar", "foo", "bar", "foo", "foo")));
}
 
TEST(strv_filter_prefix) {
        char **base = STRV_MAKE("foo", "bar", "baz", "foox", "zzz", "farb", "foerb");
 
        _cleanup_strv_free_ char **x = ASSERT_PTR(strv_filter_prefix(base, "fo"));
        ASSERT_TRUE(strv_equal(x, STRV_MAKE("foo", "foox", "foerb")));
        x = strv_free(x);
 
        x = ASSERT_PTR(strv_filter_prefix(base, ""));
        ASSERT_TRUE(strv_equal(x, base));
        x = strv_free(x);
 
        x = ASSERT_PTR(strv_filter_prefix(base, "z"));
        ASSERT_TRUE(strv_equal(x, STRV_MAKE("zzz")));
        x = strv_free(x);
 
        x = ASSERT_PTR(strv_filter_prefix(base, "zzz"));
        ASSERT_TRUE(strv_equal(x, STRV_MAKE("zzz")));
}
 
DEFINE_TEST_MAIN(LOG_INFO);