• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/sorted-strings-table.h"
18 
19 #include <vector>
20 
21 #include "utils/base/integral_types.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 
25 namespace libtextclassifier3 {
26 namespace {
27 
TEST(SortedStringsTest,Lookup)28 TEST(SortedStringsTest, Lookup) {
29   const char pieces[] = "hell\0hello\0o\0there\0";
30   const uint32 offsets[] = {0, 5, 11, 13};
31 
32   SortedStringsTable table(/*num_pieces=*/4, offsets, StringPiece(pieces, 18),
33                            /*use_linear_scan_threshold=*/1);
34 
35   {
36     std::vector<StringSet::Match> matches;
37     EXPECT_TRUE(table.FindAllPrefixMatches("hello there", &matches));
38     EXPECT_EQ(matches.size(), 2);
39     EXPECT_EQ(matches[0].id, 0 /*hell*/);
40     EXPECT_EQ(matches[0].match_length, 4 /*hell*/);
41     EXPECT_EQ(matches[1].id, 1 /*hello*/);
42     EXPECT_EQ(matches[1].match_length, 5 /*hello*/);
43   }
44 
45   {
46     std::vector<StringSet::Match> matches;
47     EXPECT_TRUE(table.FindAllPrefixMatches("he", &matches));
48     EXPECT_THAT(matches, testing::IsEmpty());
49   }
50 
51   {
52     std::vector<StringSet::Match> matches;
53     EXPECT_TRUE(table.FindAllPrefixMatches("he", &matches));
54     EXPECT_THAT(matches, testing::IsEmpty());
55   }
56 
57   {
58     std::vector<StringSet::Match> matches;
59     EXPECT_TRUE(table.FindAllPrefixMatches("abcd", &matches));
60     EXPECT_THAT(matches, testing::IsEmpty());
61   }
62 
63   {
64     std::vector<StringSet::Match> matches;
65     EXPECT_TRUE(table.FindAllPrefixMatches("", &matches));
66     EXPECT_THAT(matches, testing::IsEmpty());
67   }
68 
69   {
70     std::vector<StringSet::Match> matches;
71     EXPECT_TRUE(table.FindAllPrefixMatches("hi there", &matches));
72     EXPECT_THAT(matches, testing::IsEmpty());
73   }
74 
75   {
76     std::vector<StringSet::Match> matches;
77     EXPECT_TRUE(table.FindAllPrefixMatches(StringPiece("\0", 1), &matches));
78     EXPECT_THAT(matches, testing::IsEmpty());
79   }
80 
81   {
82     std::vector<StringSet::Match> matches;
83     EXPECT_TRUE(
84         table.FindAllPrefixMatches(StringPiece("\xff, \xfe", 2), &matches));
85     EXPECT_THAT(matches, testing::IsEmpty());
86   }
87 
88   {
89     StringSet::Match match;
90     EXPECT_TRUE(table.LongestPrefixMatch("hella there", &match));
91     EXPECT_EQ(match.id, 0 /*hell*/);
92   }
93 
94   {
95     StringSet::Match match;
96     EXPECT_TRUE(table.LongestPrefixMatch("hello there", &match));
97     EXPECT_EQ(match.id, 1 /*hello*/);
98   }
99 
100   {
101     StringSet::Match match;
102     EXPECT_TRUE(table.LongestPrefixMatch("abcd", &match));
103     EXPECT_EQ(match.id, -1);
104   }
105 
106   {
107     StringSet::Match match;
108     EXPECT_TRUE(table.LongestPrefixMatch("", &match));
109     EXPECT_EQ(match.id, -1);
110   }
111 
112   {
113     int value;
114     EXPECT_TRUE(table.Find("hell", &value));
115     EXPECT_EQ(value, 0);
116   }
117 
118   {
119     int value;
120     EXPECT_FALSE(table.Find("hella", &value));
121   }
122 
123   {
124     int value;
125     EXPECT_TRUE(table.Find("hello", &value));
126     EXPECT_EQ(value, 1 /*hello*/);
127   }
128 }
129 
130 }  // namespace
131 }  // namespace libtextclassifier3
132