~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to dictionary/user_dictionary_test.cc

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
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"
48
48
 
49
49
DECLARE_string(test_tmpdir);
50
50
 
51
51
namespace mozc {
52
 
 
53
52
namespace {
54
53
 
 
54
class TestNodeAllocator : public NodeAllocatorInterface {
 
55
 public:
 
56
  TestNodeAllocator() {}
 
57
  virtual ~TestNodeAllocator() {
 
58
    for (size_t i = 0; i < nodes_.size(); ++i) {
 
59
      delete nodes_[i];
 
60
    }
 
61
    nodes_.clear();
 
62
  }
 
63
 
 
64
  Node *NewNode() {
 
65
    Node *node = new Node;
 
66
    CHECK(node);
 
67
    node->Init();
 
68
    nodes_.push_back(node);
 
69
    return node;
 
70
  }
 
71
 
 
72
 private:
 
73
  vector<Node *> nodes_;
 
74
};
 
75
 
55
76
const char kUserDictionary0[] =
56
77
    "start\tstart\tverb\n"
57
78
    "star\tstar\tnoun\n"
75
96
 
76
97
const char kUserDictionary1[] = "end\tend\tverb\n";
77
98
 
 
99
void PushBackToken(const string &key,
 
100
                   const string &value,
 
101
                   uint16 id,
 
102
                   vector<UserPOS::Token> *tokens) {
 
103
  tokens->resize(tokens->size() + 1);
 
104
  UserPOS::Token *t = &tokens->back();
 
105
  t->key = key;
 
106
  t->value = value;
 
107
  t->id = id;
 
108
  t->cost = 0;
 
109
}
 
110
 
 
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
 
114
// inflection.
 
115
class UserPOSMock : public UserPOS::UserPOSInterface {
 
116
 public:
 
117
  UserPOSMock() {}
 
118
  virtual ~UserPOSMock() {}
 
119
 
 
120
  // This method returns true if the given pos is "noun" or "verb".
 
121
  virtual bool IsValidPOS(const string &pos) const {
 
122
    return true;
 
123
  }
 
124
 
 
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:
 
130
  //
 
131
  //  POS              | lid | rid
 
132
  // ------------------+-----+-----
 
133
  //  noun             | 100 | 100
 
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,
 
138
                         const string &value,
 
139
                         const string &pos,
 
140
                         UserPOS::CostType cost_type,
 
141
                         vector<UserPOS::Token> *tokens) const {
 
142
    if (key.empty() ||
 
143
        value.empty() ||
 
144
        pos.empty() ||
 
145
        tokens == NULL) {
 
146
      return false;
 
147
    }
 
148
 
 
149
    tokens->clear();
 
150
    if (pos == "noun") {
 
151
      PushBackToken(key, value, 100, tokens);
 
152
      return true;
 
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);
 
157
      return true;
 
158
    } else {
 
159
      return false;
 
160
    }
 
161
  }
 
162
 
 
163
  virtual void GetPOSList(vector<string> *pos_list) const {}
 
164
 
 
165
  virtual bool GetPOSIDs(const string &pos, uint16 *id) const {
 
166
    return false;
 
167
  }
 
168
 
 
169
 private:
 
170
  DISALLOW_COPY_AND_ASSIGN(UserPOSMock);
 
171
};
 
172
 
78
173
int Random(int size) {
79
174
  return static_cast<int> (1.0 * size * rand() / (RAND_MAX + 1.0));
80
175
}
88
183
  }
89
184
  return result;
90
185
}
91
 
}  // namespace
92
186
 
93
187
class UserDictionaryTest : public testing::Test {
94
188
 protected:
95
189
  static void SetUpTestCase() {
96
190
    Util::SetUserProfileDirectory(FLAGS_test_tmpdir);
97
 
    POS::SetHandler(new POSMockHandler);
 
191
    UserPOS::SetUserPOSInterface(new UserPOSMock);
 
192
  }
 
193
 
 
194
  static void TearDownTestCase() {
 
195
    UserPOS::SetUserPOSInterface(NULL);
98
196
  }
99
197
 
100
198
  // Workaround for the constructor of UserDictionary being protected.
114
212
                                         const char *key,
115
213
                                         size_t key_size,
116
214
                                         const UserDictionary &dic) {
117
 
    ConverterData data;
118
 
    Node *node = dic.LookupPredictive(key, key_size, &data);
 
215
    TestNodeAllocator allocator;
 
216
    Node *node = dic.LookupPredictive(key, key_size, &allocator);
119
217
 
120
218
    if (expected == NULL || expected_size == 0) {
121
219
      EXPECT_TRUE(NULL == node);
130
228
                                     const char *key,
131
229
                                     size_t key_size,
132
230
                                     const UserDictionary &dic) {
133
 
    ConverterData data;
134
 
    Node *node = dic.LookupPrefix(key, key_size, &data);
 
231
    TestNodeAllocator allocator;
 
232
    Node *node = dic.LookupPrefix(key, key_size, &allocator);
135
233
 
136
234
    if (expected == NULL || expected_size == 0) {
137
235
      EXPECT_TRUE(NULL == node);
344
442
  }
345
443
 
346
444
  {
347
 
    UserDictionary dic;
 
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);
351
449
 
352
 
    ConverterData data;
 
450
    TestNodeAllocator allocator;
353
451
    for (int i = 0; i < 32; ++i) {
354
452
      random_shuffle(keys.begin(), keys.end());
355
 
      dic.AsyncReload();
 
453
      dic->AsyncReload();
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);
359
457
      }
360
458
    }
361
459
  }
362
460
}
 
461
}  // namespace
363
462
}  // namespace mozc