1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "dictionary/suffix_dictionary.h"
31 
32 #include <memory>
33 
34 #include "base/util.h"
35 #include "data_manager/testing/mock_data_manager.h"
36 #include "dictionary/dictionary_interface.h"
37 #include "dictionary/dictionary_test_util.h"
38 #include "request/conversion_request.h"
39 #include "testing/base/public/gunit.h"
40 
41 namespace mozc {
42 namespace dictionary {
43 namespace {
44 
TEST(SuffixDictionaryTest,LookupPredictive)45 TEST(SuffixDictionaryTest, LookupPredictive) {
46   // Test SuffixDictionary with mock data.
47   std::unique_ptr<const SuffixDictionary> dic;
48   ConversionRequest convreq;
49   {
50     const testing::MockDataManager manager;
51     StringPiece key_array_data, value_arra_data;
52     const uint32 *token_array = nullptr;
53     manager.GetSuffixDictionaryData(&key_array_data, &value_arra_data,
54                                     &token_array);
55     dic.reset(new SuffixDictionary(key_array_data, value_arra_data,
56                                    token_array));
57     ASSERT_NE(nullptr, dic.get());
58   }
59 
60   {
61     // Lookup with empty key.  All tokens are looked up.  Here, just verify the
62     // result is nonempty and each token has valid data.
63     CollectTokenCallback callback;
64     dic->LookupPredictive("", convreq, &callback);
65     EXPECT_FALSE(callback.tokens().empty());
66     for (size_t i = 0; i < callback.tokens().size(); ++i) {
67       const Token &token = callback.tokens()[i];
68       EXPECT_FALSE(token.key.empty());
69       EXPECT_FALSE(token.value.empty());
70       EXPECT_LT(0, token.lid);
71       EXPECT_LT(0, token.rid);
72       EXPECT_EQ(Token::NONE, token.attributes);
73     }
74   }
75   {
76     // Non-empty prefix.
77     const string kPrefix = "た";
78     CollectTokenCallback callback;
79     dic->LookupPredictive(kPrefix, convreq, &callback);
80     EXPECT_FALSE(callback.tokens().empty());
81     for (size_t i = 0; i < callback.tokens().size(); ++i) {
82       const Token &token = callback.tokens()[i];
83       EXPECT_TRUE(Util::StartsWith(token.key, kPrefix));
84       EXPECT_FALSE(token.value.empty());
85       EXPECT_LT(0, token.lid);
86       EXPECT_LT(0, token.rid);
87       EXPECT_EQ(Token::NONE, token.attributes);
88     }
89   }
90 }
91 
92 }  // namespace
93 }  // namespace dictionary
94 }  // namespace mozc
95