39
39
#include "base/base.h"
40
40
#include "base/file_stream.h"
41
41
#include "base/util.h"
42
#include "converter/converter_data.h"
43
#include "converter/pos_mock.h"
42
#include "converter/node.h"
44
43
#include "dictionary/user_dictionary_storage.h"
45
44
#include "dictionary/user_dictionary_util.h"
45
#include "dictionary/user_pos.h"
46
46
#include "testing/base/public/googletest.h"
47
47
#include "testing/base/public/gunit.h"
49
49
DECLARE_string(test_tmpdir);
54
class TestNodeAllocator : public NodeAllocatorInterface {
56
TestNodeAllocator() {}
57
virtual ~TestNodeAllocator() {
58
for (size_t i = 0; i < nodes_.size(); ++i) {
65
Node *node = new Node;
68
nodes_.push_back(node);
73
vector<Node *> nodes_;
55
76
const char kUserDictionary0[] =
56
77
"start\tstart\tverb\n"
57
78
"star\tstar\tnoun\n"
76
97
const char kUserDictionary1[] = "end\tend\tverb\n";
99
void PushBackToken(const string &key,
102
vector<UserPOS::Token> *tokens) {
103
tokens->resize(tokens->size() + 1);
104
UserPOS::Token *t = &tokens->back();
111
// This class is a mock class for writing unit tests of a class that
112
// depends on POS. It accepts only two values for part-of-speech:
113
// "noun" as words without inflection and "verb" as words with
115
class UserPOSMock : public UserPOS::UserPOSInterface {
118
virtual ~UserPOSMock() {}
120
// This method returns true if the given pos is "noun" or "verb".
121
virtual bool IsValidPOS(const string &pos) const {
125
// Given a verb, this method expands it to three different forms,
126
// i.e. base form (the word itself), "-ed" form and "-ing" form. For
127
// example, if the given word is "play", the method returns "play",
128
// "played" and "playing". When a noun is passed, it returns only
129
// base form. The method set lid and rid of the word as following:
132
// ------------------+-----+-----
134
// verb (base form) | 200 | 200
135
// verb (-ed form) | 210 | 210
136
// verb (-ing form) | 220 | 220
137
virtual bool GetTokens(const string &key,
140
UserPOS::CostType cost_type,
141
vector<UserPOS::Token> *tokens) const {
151
PushBackToken(key, value, 100, tokens);
153
} else if (pos == "verb") {
154
PushBackToken(key, value, 200, tokens);
155
PushBackToken(key + "ed", value + "ed", 210, tokens);
156
PushBackToken(key + "ing", value + "ing", 220, tokens);
163
virtual void GetPOSList(vector<string> *pos_list) const {}
165
virtual bool GetPOSIDs(const string &pos, uint16 *id) const {
170
DISALLOW_COPY_AND_ASSIGN(UserPOSMock);
78
173
int Random(int size) {
79
174
return static_cast<int> (1.0 * size * rand() / (RAND_MAX + 1.0));
93
187
class UserDictionaryTest : public testing::Test {
95
189
static void SetUpTestCase() {
96
190
Util::SetUserProfileDirectory(FLAGS_test_tmpdir);
97
POS::SetHandler(new POSMockHandler);
191
UserPOS::SetUserPOSInterface(new UserPOSMock);
194
static void TearDownTestCase() {
195
UserPOS::SetUserPOSInterface(NULL);
100
198
// Workaround for the constructor of UserDictionary being protected.
116
214
const UserDictionary &dic) {
118
Node *node = dic.LookupPredictive(key, key_size, &data);
215
TestNodeAllocator allocator;
216
Node *node = dic.LookupPredictive(key, key_size, &allocator);
120
218
if (expected == NULL || expected_size == 0) {
121
219
EXPECT_TRUE(NULL == node);
132
230
const UserDictionary &dic) {
134
Node *node = dic.LookupPrefix(key, key_size, &data);
231
TestNodeAllocator allocator;
232
Node *node = dic.LookupPrefix(key, key_size, &allocator);
136
234
if (expected == NULL || expected_size == 0) {
137
235
EXPECT_TRUE(NULL == node);
445
UserDictionary *dic = UserDictionary::GetUserDictionary();
348
446
// Wait for async reload called from the constructor.
349
dic.WaitForReloader();
350
dic.SetUserDictionaryName(filename);
447
dic->WaitForReloader();
448
dic->SetUserDictionaryName(filename);
450
TestNodeAllocator allocator;
353
451
for (int i = 0; i < 32; ++i) {
354
452
random_shuffle(keys.begin(), keys.end());
356
454
for (int i = 0; i < 1000; ++i) {
357
dic.LookupPrefix(keys[i].c_str(),
358
keys[i].size(), &data);
455
dic->LookupPrefix(keys[i].c_str(),
456
keys[i].size(), &allocator);
363
462
} // namespace mozc