diff --git a/src/meta/test/meta_duplication_service_test.cpp b/src/meta/test/meta_duplication_service_test.cpp index 95fc9c160d..6284c21531 100644 --- a/src/meta/test/meta_duplication_service_test.cpp +++ b/src/meta/test/meta_duplication_service_test.cpp @@ -553,10 +553,6 @@ class meta_duplication_service_test : public meta_test_base { const auto &resp = list_dup_info(app_name_pattern, match_type); - for (const auto &[app_name, _] : resp.app_states) { - std::cout << app_name << std::endl; - } - ASSERT_EQ(ERR_OK, resp.err); ASSERT_EQ(app_names.size(), resp.app_states.size()); diff --git a/src/utils/test/utils.cpp b/src/utils/test/utils.cpp index cd57087507..0014acdcb2 100644 --- a/src/utils/test/utils.cpp +++ b/src/utils/test/utils.cpp @@ -40,6 +40,7 @@ #include "utils/binary_reader.h" #include "utils/binary_writer.h" #include "utils/crc.h" +#include "utils/error_code.h" #include "utils/link.h" #include "utils/rand.h" #include "utils/strings.h" @@ -247,6 +248,74 @@ INSTANTIATE_TEST_SUITE_P(StringTest, CStringNBytesEqualityTest, testing::ValuesIn(c_string_n_bytes_equality_tests)); +struct pattern_match_case +{ + std::string str; + std::string pattern; + pattern_match_type::type match_type; + error_code expected_err; +}; + +class PatternMatchTest : public testing::TestWithParam +{ +}; + +const std::vector pattern_match_tests = { + // Everything would be matched even if pattern is empty. + {"abc", "", pattern_match_type::PMT_MATCH_ALL, ERR_OK}, + // Everything would be matched even if it is not matched completely. + {"abc", "xyz", pattern_match_type::PMT_MATCH_ALL, ERR_OK}, + // It is matched exactly. + {"abc", "abc", pattern_match_type::PMT_MATCH_EXACT, ERR_OK}, + // Non-empty string cannot be matched exactly with empty pattern. + {"abc", "", pattern_match_type::PMT_MATCH_EXACT, ERR_NOT_MATCHED}, + // The string whose content is different from pattern would not be matched. + {"abc", "xyz", pattern_match_type::PMT_MATCH_EXACT, ERR_NOT_MATCHED}, + // The pattern as a sub string would not be matched. + {"abc", "ab", pattern_match_type::PMT_MATCH_EXACT, ERR_NOT_MATCHED}, + // It is matched with same prefix for anywhere. + {"abcdef", "ab", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_OK}, + // It is matched with same middle for anywhere. + {"abcdef", "cd", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_OK}, + // It is matched with same postfix for anywhere. + {"abcdef", "ef", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_OK}, + // It is matched with empty content for anywhere. + {"abcdef", "", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_OK}, + // It is not matched with different content for anywhere. + {"abcdef", "xyz", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_NOT_MATCHED}, + // It is matched for prefix. + {"abcdef", "ab", pattern_match_type::PMT_MATCH_PREFIX, ERR_OK}, + // It is not matched with same middle for prefix. + {"abcdef", "cd", pattern_match_type::PMT_MATCH_PREFIX, ERR_NOT_MATCHED}, + // It is not matched with same postfix for prefix. + {"abcdef", "ef", pattern_match_type::PMT_MATCH_PREFIX, ERR_NOT_MATCHED}, + // It is not matched with different content for prefix. + {"abcdef", "xyz", pattern_match_type::PMT_MATCH_PREFIX, ERR_NOT_MATCHED}, + // It is matched with empty content for prefix. + {"abcdef", "", pattern_match_type::PMT_MATCH_PREFIX, ERR_OK}, + // It is matched for postfix. + {"abcdef", "ef", pattern_match_type::PMT_MATCH_POSTFIX, ERR_OK}, + // It is not matched with same prefix for postfix. + {"abcdef", "ab", pattern_match_type::PMT_MATCH_POSTFIX, ERR_NOT_MATCHED}, + // It is not matched with same middle for postfix. + {"abcdef", "cd", pattern_match_type::PMT_MATCH_POSTFIX, ERR_NOT_MATCHED}, + // It is not matched with different content for postfix. + {"abcdef", "xyz", pattern_match_type::PMT_MATCH_PREFIX, ERR_NOT_MATCHED}, + // It is matched with empty content for postfix. + {"abcdef", "", pattern_match_type::PMT_MATCH_POSTFIX, ERR_OK}, + // PMT_MATCH_REGEX is still not supported. + {"unsupported", ".*", pattern_match_type::PMT_MATCH_REGEX, ERR_NOT_IMPLEMENTED}, +}; + +TEST_P(PatternMatchTest, PatternMatch) +{ + const auto &test_case = GetParam(); + const auto actual_err = pattern_match(test_case.str, test_case.pattern, test_case.match_type); + EXPECT_EQ(test_case.expected_err, actual_err); +} + +INSTANTIATE_TEST_SUITE_P(StringTest, PatternMatchTest, testing::ValuesIn(pattern_match_tests)); + // For containers such as std::unordered_set, the expected result will be deduplicated // at initialization. Therefore, it can be used to compare with actual result safely. template