Esempio n. 1
0
bool TestKeepIf() {
    BEGIN_TEST;
    StringList list;
    const char* original[] = {"",     "foo",   "bar",    "baz",    "qux",
                              "quux", "corge", "grault", "garply", "waldo",
                              "fred", "plugh", "xyzzy",  "thud",   ""};

    const char* expected1[] = {"bar", "corge", "grault", "garply", "plugh"};

    const char* expected2[] = {"corge", "grault", "garply", "plugh"};

    const char* expected3[] = {"garply"};

    for (size_t i = 0; i < arraysize(original); ++i) {
        list.push_back(original[i]);
    }

    // Null string has no effect
    list.keep_if(nullptr);
    EXPECT_TRUE(Match(&list, original, 0, arraysize(original)));

    // Empty string matches everything
    list.keep_if("");
    EXPECT_TRUE(Match(&list, original, 0, arraysize(original)));

    // Match a string
    list.keep_if("g");
    EXPECT_TRUE(Match(&list, expected2, 0, arraysize(expected2)));

    // Match a string that would have matched elements in the original list
    list.keep_if("ar");
    EXPECT_TRUE(Match(&list, expected3, 0, arraysize(expected3)));

    // Use a string that doesn't match anything
    list.keep_if("zzz");
    EXPECT_TRUE(list.is_empty());

    // Reset and apply both matches at once with logical-or
    StringList substrs;
    substrs.push_back("g");
    substrs.push_back("ar");

    list.clear();
    for (size_t i = 0; i < arraysize(original); ++i) {
        list.push_back(original[i]);
    }
    list.keep_if_any(&substrs);
    EXPECT_TRUE(Match(&list, expected1, 0, arraysize(expected1)));

    // Reset and apply both matches at once with logical-and
    list.clear();
    for (size_t i = 0; i < arraysize(original); ++i) {
        list.push_back(original[i]);
    }
    list.keep_if_all(&substrs);
    EXPECT_TRUE(Match(&list, expected3, 0, arraysize(expected3)));

    END_TEST;
}