|
40 | 40 | #include "utils/binary_reader.h"
|
41 | 41 | #include "utils/binary_writer.h"
|
42 | 42 | #include "utils/crc.h"
|
| 43 | +#include "utils/error_code.h" |
43 | 44 | #include "utils/link.h"
|
44 | 45 | #include "utils/rand.h"
|
45 | 46 | #include "utils/strings.h"
|
@@ -247,6 +248,74 @@ INSTANTIATE_TEST_SUITE_P(StringTest,
|
247 | 248 | CStringNBytesEqualityTest,
|
248 | 249 | testing::ValuesIn(c_string_n_bytes_equality_tests));
|
249 | 250 |
|
| 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 | + |
250 | 319 | // For containers such as std::unordered_set, the expected result will be deduplicated
|
251 | 320 | // at initialization. Therefore, it can be used to compare with actual result safely.
|
252 | 321 | template <typename Container>
|
|
0 commit comments