Skip to content

Commit 45089d2

Browse files
committed
add tests for pattern match
1 parent 840f68b commit 45089d2

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

src/meta/test/meta_duplication_service_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,6 @@ class meta_duplication_service_test : public meta_test_base
553553
{
554554
const auto &resp = list_dup_info(app_name_pattern, match_type);
555555

556-
for (const auto &[app_name, _] : resp.app_states) {
557-
std::cout << app_name << std::endl;
558-
}
559-
560556
ASSERT_EQ(ERR_OK, resp.err);
561557
ASSERT_EQ(app_names.size(), resp.app_states.size());
562558

src/utils/test/utils.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "utils/binary_reader.h"
4141
#include "utils/binary_writer.h"
4242
#include "utils/crc.h"
43+
#include "utils/error_code.h"
4344
#include "utils/link.h"
4445
#include "utils/rand.h"
4546
#include "utils/strings.h"
@@ -247,6 +248,74 @@ INSTANTIATE_TEST_SUITE_P(StringTest,
247248
CStringNBytesEqualityTest,
248249
testing::ValuesIn(c_string_n_bytes_equality_tests));
249250

251+
struct pattern_match_case
252+
{
253+
std::string str;
254+
std::string pattern;
255+
pattern_match_type::type match_type;
256+
error_code expected_err;
257+
};
258+
259+
class PatternMatchTest : public testing::TestWithParam<pattern_match_case>
260+
{
261+
};
262+
263+
const std::vector<pattern_match_case> pattern_match_tests = {
264+
// Everything would be matched even if pattern is empty.
265+
{"abc", "", pattern_match_type::PMT_MATCH_ALL, ERR_OK},
266+
// Everything would be matched even if it is not matched completely.
267+
{"abc", "xyz", pattern_match_type::PMT_MATCH_ALL, ERR_OK},
268+
// It is matched exactly.
269+
{"abc", "abc", pattern_match_type::PMT_MATCH_EXACT, ERR_OK},
270+
// Non-empty string cannot be matched exactly with empty pattern.
271+
{"abc", "", pattern_match_type::PMT_MATCH_EXACT, ERR_NOT_MATCHED},
272+
// The string whose content is different from pattern would not be matched.
273+
{"abc", "xyz", pattern_match_type::PMT_MATCH_EXACT, ERR_NOT_MATCHED},
274+
// The pattern as a sub string would not be matched.
275+
{"abc", "ab", pattern_match_type::PMT_MATCH_EXACT, ERR_NOT_MATCHED},
276+
// It is matched with same prefix for anywhere.
277+
{"abcdef", "ab", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_OK},
278+
// It is matched with same middle for anywhere.
279+
{"abcdef", "cd", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_OK},
280+
// It is matched with same postfix for anywhere.
281+
{"abcdef", "ef", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_OK},
282+
// It is matched with empty content for anywhere.
283+
{"abcdef", "", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_OK},
284+
// It is not matched with different content for anywhere.
285+
{"abcdef", "xyz", pattern_match_type::PMT_MATCH_ANYWHERE, ERR_NOT_MATCHED},
286+
// It is matched for prefix.
287+
{"abcdef", "ab", pattern_match_type::PMT_MATCH_PREFIX, ERR_OK},
288+
// It is not matched with same middle for prefix.
289+
{"abcdef", "cd", pattern_match_type::PMT_MATCH_PREFIX, ERR_NOT_MATCHED},
290+
// It is not matched with same postfix for prefix.
291+
{"abcdef", "ef", pattern_match_type::PMT_MATCH_PREFIX, ERR_NOT_MATCHED},
292+
// It is not matched with different content for prefix.
293+
{"abcdef", "xyz", pattern_match_type::PMT_MATCH_PREFIX, ERR_NOT_MATCHED},
294+
// It is matched with empty content for prefix.
295+
{"abcdef", "", pattern_match_type::PMT_MATCH_PREFIX, ERR_OK},
296+
// It is matched for postfix.
297+
{"abcdef", "ef", pattern_match_type::PMT_MATCH_POSTFIX, ERR_OK},
298+
// It is not matched with same prefix for postfix.
299+
{"abcdef", "ab", pattern_match_type::PMT_MATCH_POSTFIX, ERR_NOT_MATCHED},
300+
// It is not matched with same middle for postfix.
301+
{"abcdef", "cd", pattern_match_type::PMT_MATCH_POSTFIX, ERR_NOT_MATCHED},
302+
// It is not matched with different content for postfix.
303+
{"abcdef", "xyz", pattern_match_type::PMT_MATCH_PREFIX, ERR_NOT_MATCHED},
304+
// It is matched with empty content for postfix.
305+
{"abcdef", "", pattern_match_type::PMT_MATCH_POSTFIX, ERR_OK},
306+
// PMT_MATCH_REGEX is still not supported.
307+
{"unsupported", ".*", pattern_match_type::PMT_MATCH_REGEX, ERR_NOT_IMPLEMENTED},
308+
};
309+
310+
TEST_P(PatternMatchTest, PatternMatch)
311+
{
312+
const auto &test_case = GetParam();
313+
const auto actual_err = pattern_match(test_case.str, test_case.pattern, test_case.match_type);
314+
EXPECT_EQ(test_case.expected_err, actual_err);
315+
}
316+
317+
INSTANTIATE_TEST_SUITE_P(StringTest, PatternMatchTest, testing::ValuesIn(pattern_match_tests));
318+
250319
// For containers such as std::unordered_set, the expected result will be deduplicated
251320
// at initialization. Therefore, it can be used to compare with actual result safely.
252321
template <typename Container>

0 commit comments

Comments
 (0)