1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "utils/container/double-array-trie.h"
18
19 #include <fstream>
20 #include <string>
21 #include <vector>
22
23 #include "utils/test-data-test-utils.h"
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26
27 namespace libtextclassifier3 {
28 namespace {
29
GetTestConfigPath()30 std::string GetTestConfigPath() {
31 return GetTestDataPath("utils/container/test_data/test_trie.bin");
32 }
33
TEST(DoubleArrayTest,Lookup)34 TEST(DoubleArrayTest, Lookup) {
35 // Test trie that contains pieces "hell", "hello", "o", "there".
36 std::ifstream test_config_stream(GetTestConfigPath());
37 std::string config((std::istreambuf_iterator<char>(test_config_stream)),
38 (std::istreambuf_iterator<char>()));
39 DoubleArrayTrie trie(reinterpret_cast<const TrieNode*>(config.data()),
40 config.size() / sizeof(TrieNode));
41
42 {
43 std::vector<StringSet::Match> matches;
44 EXPECT_TRUE(trie.FindAllPrefixMatches("hello there", &matches));
45 EXPECT_EQ(matches.size(), 2);
46 EXPECT_EQ(matches[0].id, 0 /*hell*/);
47 EXPECT_EQ(matches[0].match_length, 4 /*hell*/);
48 EXPECT_EQ(matches[1].id, 1 /*hello*/);
49 EXPECT_EQ(matches[1].match_length, 5 /*hello*/);
50 }
51
52 {
53 std::vector<StringSet::Match> matches;
54 EXPECT_TRUE(trie.FindAllPrefixMatches("he", &matches));
55 EXPECT_THAT(matches, testing::IsEmpty());
56 }
57
58 {
59 std::vector<StringSet::Match> matches;
60 EXPECT_TRUE(trie.FindAllPrefixMatches("abcd", &matches));
61 EXPECT_THAT(matches, testing::IsEmpty());
62 }
63
64 {
65 std::vector<StringSet::Match> matches;
66 EXPECT_TRUE(trie.FindAllPrefixMatches("", &matches));
67 EXPECT_THAT(matches, testing::IsEmpty());
68 }
69
70 {
71 std::vector<StringSet::Match> matches;
72 EXPECT_TRUE(trie.FindAllPrefixMatches("hi there", &matches));
73 EXPECT_THAT(matches, testing::IsEmpty());
74 }
75
76 {
77 std::vector<StringSet::Match> matches;
78 EXPECT_TRUE(trie.FindAllPrefixMatches(StringPiece("\0", 1), &matches));
79 EXPECT_THAT(matches, testing::IsEmpty());
80 }
81
82 {
83 std::vector<StringSet::Match> matches;
84 EXPECT_TRUE(
85 trie.FindAllPrefixMatches(StringPiece("\xff, \xfe", 2), &matches));
86 EXPECT_THAT(matches, testing::IsEmpty());
87 }
88
89 {
90 StringSet::Match match;
91 EXPECT_TRUE(trie.LongestPrefixMatch("hella there", &match));
92 EXPECT_EQ(match.id, 0 /*hell*/);
93 }
94
95 {
96 StringSet::Match match;
97 EXPECT_TRUE(trie.LongestPrefixMatch("hello there", &match));
98 EXPECT_EQ(match.id, 1 /*hello*/);
99 }
100
101 {
102 StringSet::Match match;
103 EXPECT_TRUE(trie.LongestPrefixMatch("abcd", &match));
104 EXPECT_EQ(match.id, -1);
105 }
106
107 {
108 StringSet::Match match;
109 EXPECT_TRUE(trie.LongestPrefixMatch("", &match));
110 EXPECT_EQ(match.id, -1);
111 }
112
113 {
114 int value;
115 EXPECT_TRUE(trie.Find("hell", &value));
116 EXPECT_EQ(value, 0);
117 }
118
119 {
120 int value;
121 EXPECT_FALSE(trie.Find("hella", &value));
122 }
123
124 {
125 int value;
126 EXPECT_TRUE(trie.Find("hello", &value));
127 EXPECT_EQ(value, 1 /*hello*/);
128 }
129 }
130
131 } // namespace
132 } // namespace libtextclassifier3
133