aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/fuzzy/fuzzy_matcher_test.cpp
blob: d94120e5bcf2c1699509b5cb0dfec2223f9cf85e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/fuzzy/fuzzy_matcher.h>
#include <vespa/vespalib/text/lowercase.h>
#include <vespa/vespalib/gtest/gtest.h>

using namespace vespalib;


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("abc", 2, 0, false);
    EXPECT_TRUE(fuzzy.isMatch("abc"));
    EXPECT_TRUE(fuzzy.isMatch("ABC"));
    EXPECT_TRUE(fuzzy.isMatch("ab1"));
    EXPECT_TRUE(fuzzy.isMatch("a12"));
    EXPECT_FALSE(fuzzy.isMatch("123"));
}

TEST(FuzzyMatcherTest, fuzzy_match_cased) {
    FuzzyMatcher fuzzy("abc", 2, 0, true);
    EXPECT_TRUE(fuzzy.isMatch("abc"));
    EXPECT_TRUE(fuzzy.isMatch("abC"));
    EXPECT_TRUE(fuzzy.isMatch("aBC"));
    EXPECT_FALSE(fuzzy.isMatch("ABC"));
}

TEST(FuzzyMatcherTest, fuzzy_match_with_prefix) {
    FuzzyMatcher fuzzy("abcdef", 2, 2, false);
    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_FALSE(fuzzy.isMatch("12cdef"));
}

TEST(FuzzyMatcherTest, get_prefix_is_empty) {
    FuzzyMatcher fuzzy("whatever", 2, 0, false);
    EXPECT_EQ(fuzzy.getPrefix(), "");
}

TEST(FuzzyMatcherTest, term_is_empty) {
    FuzzyMatcher fuzzy("", 2, 0, false);
    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("abcd", 2, 2, false);
    EXPECT_EQ(fuzzy.getPrefix(), "ab");
}

GTEST_MAIN_RUN_ALL_TESTS()