diff options
Diffstat (limited to 'vespalib/src/tests/regex/regex.cpp')
-rw-r--r-- | vespalib/src/tests/regex/regex.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/vespalib/src/tests/regex/regex.cpp b/vespalib/src/tests/regex/regex.cpp index 471ba84a68f..1ab4a24a1b3 100644 --- a/vespalib/src/tests/regex/regex.cpp +++ b/vespalib/src/tests/regex/regex.cpp @@ -150,4 +150,35 @@ TEST("Test that default constructed regex is invalid.") { ASSERT_FALSE(dummy.valid()); } +TEST("Can extract min/max prefix range from anchored regex") { + auto min_max = Regex::from_pattern("^.*").possible_anchored_match_prefix_range(); + EXPECT_EQUAL(min_max.first, ""); + EXPECT_EQUAL(min_max.second, "\xf4\x8f\xbf\xc0"); // Highest possible Unicode char (U+10FFFF) as UTF-8, plus 1 + + min_max = Regex::from_pattern("^hello").possible_anchored_match_prefix_range(); + EXPECT_EQUAL(min_max.first, "hello"); + EXPECT_EQUAL(min_max.second, "hello"); + + min_max = Regex::from_pattern("^hello|^world").possible_anchored_match_prefix_range(); + EXPECT_EQUAL(min_max.first, "hello"); + EXPECT_EQUAL(min_max.second, "world"); + + min_max = Regex::from_pattern("(^hello|^world|^zoidberg)").possible_anchored_match_prefix_range(); + EXPECT_EQUAL(min_max.first, "hello"); + EXPECT_EQUAL(min_max.second, "zoidberg"); + + min_max = Regex::from_pattern("^hello (foo|bar|zoo)").possible_anchored_match_prefix_range(); + EXPECT_EQUAL(min_max.first, "hello bar"); + EXPECT_EQUAL(min_max.second, "hello zoo"); + + min_max = Regex::from_pattern("^(hello|world)+").possible_anchored_match_prefix_range(); + EXPECT_EQUAL(min_max.first, "hello"); + EXPECT_EQUAL(min_max.second, "worldwp"); + + // Bad regex; no range + min_max = Regex::from_pattern("*hello").possible_anchored_match_prefix_range(); + EXPECT_EQUAL(min_max.first, ""); + EXPECT_EQUAL(min_max.second, ""); +} + TEST_MAIN() { TEST_RUN_ALL(); } |