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 "rewriter/user_dictionary_rewriter.h"
31
32 #include <cstddef>
33 #include <string>
34 #include <vector>
35
36 #include "base/logging.h"
37 #include "base/system_util.h"
38 #include "base/util.h"
39 #include "converter/segments.h"
40 #include "request/conversion_request.h"
41 #include "testing/base/public/gunit.h"
42
43 DECLARE_string(test_tmpdir);
44
45 namespace mozc {
46
47 namespace {
AddCandidate(const string & value,bool is_user_dictionary,Segments * segments)48 void AddCandidate(const string &value,
49 bool is_user_dictionary,
50 Segments *segments) {
51 segments->set_request_type(Segments::CONVERSION);
52 Segment *seg = NULL;
53 if (segments->segments_size() == 0) {
54 seg = segments->push_back_segment();
55 seg->set_key("test");
56 } else {
57 seg = segments->mutable_segment(0);
58 }
59 Segment::Candidate *candidate = seg->add_candidate();
60 candidate->Init();
61 candidate->key = value;
62 candidate->content_key = value;
63 candidate->value = value;
64 candidate->content_value = value;
65 if (is_user_dictionary) {
66 candidate->attributes |= Segment::Candidate::USER_DICTIONARY;
67 }
68 }
69
GetCandidates(const Segments & segments)70 string GetCandidates(const Segments &segments) {
71 CHECK_EQ(1, segments.segments_size());
72 const Segment &seg = segments.segment(0);
73 std::vector<string> results;
74 for (size_t i = 0; i < seg.candidates_size(); ++i) {
75 results.push_back(seg.candidate(i).value);
76 }
77 string result;
78 Util::JoinStrings(results, " ", &result);
79 return result;
80 }
81 } // namespace
82
83 class UserDictionaryRewriterTest : public testing::Test {
84 protected:
UserDictionaryRewriterTest()85 UserDictionaryRewriterTest() {}
~UserDictionaryRewriterTest()86 ~UserDictionaryRewriterTest() {}
87
SetUp()88 virtual void SetUp() {
89 SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
90 }
91
TearDown()92 virtual void TearDown() {}
93 };
94
TEST_F(UserDictionaryRewriterTest,RewriteTest)95 TEST_F(UserDictionaryRewriterTest, RewriteTest) {
96 UserDictionaryRewriter rewriter;
97 const ConversionRequest request;
98
99 {
100 Segments segments;
101 AddCandidate("1", false, &segments);
102 AddCandidate("2", false, &segments);
103 AddCandidate("3", false, &segments);
104 AddCandidate("4", false, &segments);
105 AddCandidate("5", false, &segments);
106 EXPECT_FALSE(rewriter.Rewrite(request, &segments));
107 EXPECT_EQ("1 2 3 4 5", GetCandidates(segments));
108 }
109
110 {
111 Segments segments;
112 AddCandidate("1", true, &segments);
113 AddCandidate("2", false, &segments);
114 AddCandidate("3", false, &segments);
115 AddCandidate("4", false, &segments);
116 AddCandidate("5", false, &segments);
117 EXPECT_FALSE(rewriter.Rewrite(request, &segments));
118 EXPECT_EQ("1 2 3 4 5", GetCandidates(segments));
119 }
120
121 {
122 Segments segments;
123 AddCandidate("1", false, &segments);
124 AddCandidate("2", true, &segments);
125 AddCandidate("3", false, &segments);
126 AddCandidate("4", false, &segments);
127 AddCandidate("5", false, &segments);
128 EXPECT_FALSE(rewriter.Rewrite(request, &segments));
129 EXPECT_EQ("1 2 3 4 5", GetCandidates(segments));
130 }
131
132 {
133 Segments segments;
134 AddCandidate("1", false, &segments);
135 AddCandidate("2", false, &segments);
136 AddCandidate("3", true, &segments);
137 AddCandidate("4", false, &segments);
138 AddCandidate("5", false, &segments);
139 EXPECT_TRUE(rewriter.Rewrite(request, &segments));
140 EXPECT_EQ("1 3 2 4 5", GetCandidates(segments));
141 }
142 {
143 Segments segments;
144 AddCandidate("1", false, &segments);
145 AddCandidate("2", false, &segments);
146 AddCandidate("3", true, &segments);
147 AddCandidate("4", true, &segments);
148 AddCandidate("5", false, &segments);
149 EXPECT_TRUE(rewriter.Rewrite(request, &segments));
150 EXPECT_EQ("1 3 4 2 5", GetCandidates(segments));
151 }
152 {
153 Segments segments;
154 AddCandidate("1", false, &segments);
155 AddCandidate("2", true, &segments);
156 AddCandidate("3", true, &segments);
157 AddCandidate("4", true, &segments);
158 AddCandidate("5", false, &segments);
159 EXPECT_FALSE(rewriter.Rewrite(request, &segments));
160 EXPECT_EQ("1 2 3 4 5", GetCandidates(segments));
161 }
162 {
163 Segments segments;
164 AddCandidate("1", true, &segments);
165 AddCandidate("2", true, &segments);
166 AddCandidate("3", true, &segments);
167 AddCandidate("4", true, &segments);
168 AddCandidate("5", true, &segments);
169 EXPECT_FALSE(rewriter.Rewrite(request, &segments));
170 EXPECT_EQ("1 2 3 4 5", GetCandidates(segments));
171 }
172 {
173 Segments segments;
174 AddCandidate("1", true, &segments);
175 AddCandidate("2", false, &segments);
176 AddCandidate("3", false, &segments);
177 AddCandidate("4", true, &segments);
178 AddCandidate("5", false, &segments);
179 EXPECT_TRUE(rewriter.Rewrite(request, &segments));
180 EXPECT_EQ("1 4 2 3 5", GetCandidates(segments));
181 }
182 }
183 } // namespace mozc
184