aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp
diff options
context:
space:
mode:
authorAlexey Chernyshev <aleksei@spotify.com>2022-04-04 16:23:07 +0200
committerAlexey Chernyshev <aleksei@spotify.com>2022-04-07 14:44:30 +0200
commit7e9b33401201db9a9e22971dd419247e268bbfaa (patch)
treef5032a82e9fa74247b2fdeb3dcde4dc6cf98ce89 /vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp
parentad7cc1d11f0c19baa2344a643377576c559555f7 (diff)
Propagating annotations for fuzzy query
Diffstat (limited to 'vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp')
-rw-r--r--vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp44
1 files changed, 37 insertions, 7 deletions
diff --git a/vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp b/vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp
index 60a4eab3f57..1395915fa5f 100644
--- a/vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp
+++ b/vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp
@@ -6,12 +6,34 @@
using namespace vespalib;
-FuzzyMatcher from_term(std::string_view term, uint8_t threshold, uint8_t prefix_size) {
- return {LowerCase::convert_to_ucs4(term), threshold, prefix_size};
+
+template <typename T>
+void assert_span(std::span<const T> left, std::vector<T> right) {
+ EXPECT_TRUE(std::equal(left.begin(), left.end(), right.begin(), right.end()));
+}
+
+TEST(FuzzyMatcherTest, get_prefix_edge_cases) {
+ assert_span(FuzzyMatcher::get_prefix({1, 2, 3}, 0), {});
+ assert_span(FuzzyMatcher::get_prefix({1, 2, 3}, 1), {1 });
+ assert_span(FuzzyMatcher::get_prefix({1, 2, 3}, 2), {1, 2});
+ assert_span(FuzzyMatcher::get_prefix({1, 2, 3}, 3), {1, 2, 3});
+ assert_span(FuzzyMatcher::get_prefix({1, 2, 3}, 10),{1, 2, 3});
+ assert_span(FuzzyMatcher::get_prefix({}, 0), {});
+ assert_span(FuzzyMatcher::get_prefix({}, 10), {});
+}
+
+TEST(FuzzyMatcherTest, get_suffix_edge_cases) {
+ assert_span(FuzzyMatcher::get_suffix({1, 2, 3}, 0), {1, 2, 3});
+ assert_span(FuzzyMatcher::get_suffix({1, 2, 3}, 1), {2, 3});
+ assert_span(FuzzyMatcher::get_suffix({1, 2, 3}, 2), {3});
+ assert_span(FuzzyMatcher::get_suffix({1, 2, 3}, 3), {});
+ assert_span(FuzzyMatcher::get_suffix({1, 2, 3}, 10), {});
+ assert_span(FuzzyMatcher::get_suffix({}, 0), {});
+ assert_span(FuzzyMatcher::get_suffix({}, 10),{});
}
TEST(FuzzyMatcherTest, fuzzy_match_empty_prefix) {
- FuzzyMatcher fuzzy = from_term("abc", 2, 0);
+ FuzzyMatcher fuzzy = FuzzyMatcher::from_term("abc", 2, 0);
EXPECT_TRUE(fuzzy.isMatch("abc"));
EXPECT_TRUE(fuzzy.isMatch("ABC"));
EXPECT_TRUE(fuzzy.isMatch("ab1"));
@@ -20,22 +42,30 @@ TEST(FuzzyMatcherTest, fuzzy_match_empty_prefix) {
}
TEST(FuzzyMatcherTest, fuzzy_match_with_prefix) {
- FuzzyMatcher fuzzy = from_term("abcdef", 2, 2);
+ FuzzyMatcher fuzzy = FuzzyMatcher::from_term("abcdef", 2, 2);
EXPECT_TRUE(fuzzy.isMatch("abcdef"));
EXPECT_TRUE(fuzzy.isMatch("ABCDEF"));
EXPECT_TRUE(fuzzy.isMatch("abcde1"));
EXPECT_TRUE(fuzzy.isMatch("abcd12"));
EXPECT_FALSE(fuzzy.isMatch("abc123"));
- EXPECT_TRUE(fuzzy.isMatch("12cdef")); // prefix match is not enforced
+ EXPECT_FALSE(fuzzy.isMatch("12cdef"));
}
TEST(FuzzyMatcherTest, get_prefix_is_empty) {
- FuzzyMatcher fuzzy = from_term("whatever", 2, 0);
+ FuzzyMatcher fuzzy = FuzzyMatcher::from_term("whatever", 2, 0);
EXPECT_EQ(fuzzy.getPrefix(), "");
}
+TEST(FuzzyMatcherTest, term_is_empty) {
+ FuzzyMatcher fuzzy = FuzzyMatcher::from_term("", 2, 0);
+ EXPECT_TRUE(fuzzy.isMatch(""));
+ EXPECT_TRUE(fuzzy.isMatch("a"));
+ EXPECT_TRUE(fuzzy.isMatch("aa"));
+ EXPECT_FALSE(fuzzy.isMatch("aaa"));
+}
+
TEST(FuzzyMatcherTest, get_prefix_non_empty) {
- FuzzyMatcher fuzzy = from_term("abcd", 2, 2);
+ FuzzyMatcher fuzzy = FuzzyMatcher::from_term("abcd", 2, 2);
EXPECT_EQ(fuzzy.getPrefix(), "ab");
}