Skip to content

Commit

Permalink
add tests for pattern match
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan committed Dec 5, 2024
1 parent 840f68b commit 45089d2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/meta/test/meta_duplication_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
69 changes: 69 additions & 0 deletions src/utils/test/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<pattern_match_case>
{
};

const std::vector<pattern_match_case> 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 <typename Container>
Expand Down

0 comments on commit 45089d2

Please sign in to comment.