summaryrefslogtreecommitdiffstats
path: root/chrome/tools/convert_dict/convert_dict_unittest.cc
blob: a7995d7c4e3ad99a2e54547854e9208cf70375fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <map>
#include <string>

#include "base/file_util.h"
#include "base/format_macros.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
#include "chrome/tools/convert_dict/aff_reader.h"
#include "chrome/tools/convert_dict/dic_reader.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/hunspell/google/bdict_reader.h"
#include "third_party/hunspell/google/bdict_writer.h"

namespace {

// Compares the given word list with the serialized trie to make sure they
// are the same.
// (This function is copied from "chrome/tools/convert_dict/convert_dict.cc").
bool VerifyWords(const convert_dict::DicReader::WordList& org_words,
                 const std::string& serialized) {
  hunspell::BDictReader reader;
  EXPECT_TRUE(
      reader.Init(reinterpret_cast<const unsigned char*>(serialized.data()),
      serialized.size()));

  hunspell::WordIterator iter = reader.GetAllWordIterator();

  int affix_ids[hunspell::BDict::MAX_AFFIXES_PER_WORD];

  static const int kBufSize = 128;
  char buf[kBufSize];
  for (size_t i = 0; i < org_words.size(); i++) {
    SCOPED_TRACE(base::StringPrintf(
        "org_words[%" PRIuS "]: %s", i, org_words[i].first.c_str()));

    int affix_matches = iter.Advance(buf, kBufSize, affix_ids);
    EXPECT_NE(0, affix_matches);
    EXPECT_EQ(org_words[i].first, std::string(buf));
    EXPECT_EQ(affix_matches, static_cast<int>(org_words[i].second.size()));

    // Check the individual affix indices.
    for (size_t affix_index = 0; affix_index < org_words[i].second.size();
         affix_index++) {
      EXPECT_EQ(affix_ids[affix_index], org_words[i].second[affix_index]);
    }
  }

  return true;
}

// Implements the test process used by ConvertDictTest.
// This function encapsulates all complicated operations used by
// ConvertDictTest so we can conceal them from the tests themselves.
// This function consists of the following parts:
// * Creates a dummy affix file and a dictionary file.
// * Reads the dummy files.
// * Creates bdict data.
// * Verify the bdict data.
void RunDictionaryTest(const char* codepage,
                       const std::map<string16, bool>& word_list) {
  // Create an affix data and a dictionary data.
  std::string aff_data(base::StringPrintf("SET %s\n", codepage));

  std::string dic_data(base::StringPrintf("%" PRIuS "\n", word_list.size()));
  for (std::map<string16, bool>::const_iterator it = word_list.begin();
       it != word_list.end(); ++it) {
    std::string encoded_word;
    EXPECT_TRUE(UTF16ToCodepage(it->first,
                                codepage,
                                base::OnStringConversionError::FAIL,
                                &encoded_word));
    dic_data += encoded_word;
    dic_data += "\n";
  }

  // Create a temporary affix file and a dictionary file from the test data.
  base::FilePath aff_file;
  file_util::CreateTemporaryFile(&aff_file);
  file_util::WriteFile(aff_file, aff_data.c_str(), aff_data.length());

  base::FilePath dic_file;
  file_util::CreateTemporaryFile(&dic_file);
  file_util::WriteFile(dic_file, dic_data.c_str(), dic_data.length());

  {
    // Read the above affix file with AffReader and read the dictionary file
    // with DicReader, respectively.
    convert_dict::AffReader aff_reader(aff_file);
    EXPECT_TRUE(aff_reader.Read());

    convert_dict::DicReader dic_reader(dic_file);
    EXPECT_TRUE(dic_reader.Read(&aff_reader));

    // Verify this DicReader includes all the input words.
    EXPECT_EQ(word_list.size(), dic_reader.words().size());
    for (size_t i = 0; i < dic_reader.words().size(); ++i) {
      SCOPED_TRACE(base::StringPrintf("dic_reader.words()[%" PRIuS "]: %s",
                                      i, dic_reader.words()[i].first.c_str()));
      string16 word(UTF8ToUTF16(dic_reader.words()[i].first));
      EXPECT_TRUE(word_list.find(word) != word_list.end());
    }

    // Create BDICT data and verify it.
    hunspell::BDictWriter writer;
    writer.SetComment(aff_reader.comments());
    writer.SetAffixRules(aff_reader.affix_rules());
    writer.SetAffixGroups(aff_reader.GetAffixGroups());
    writer.SetReplacements(aff_reader.replacements());
    writer.SetOtherCommands(aff_reader.other_commands());
    writer.SetWords(dic_reader.words());

    std::string bdict_data = writer.GetBDict();
    VerifyWords(dic_reader.words(), bdict_data);
    EXPECT_TRUE(hunspell::BDict::Verify(bdict_data.data(), bdict_data.size()));

    // Trim the end of this BDICT and verify our verifier tells these trimmed
    // BDICTs are corrupted.
    for (size_t i = 1; i < bdict_data.size(); ++i) {
      SCOPED_TRACE(base::StringPrintf("i = %" PRIuS, i));
      EXPECT_FALSE(hunspell::BDict::Verify(bdict_data.data(),
                                           bdict_data.size() - i));
    }
  }

  // Deletes the temporary files.
  // We need to delete them after the above AffReader and DicReader are deleted
  // since they close the input files in their destructors.
  file_util::Delete(aff_file, false);
  file_util::Delete(dic_file, false);
}

}  // namespace

// Tests whether or not our DicReader can read all the input English words
TEST(ConvertDictTest, English) {
  const char kCodepage[] = "UTF-8";
  const wchar_t* kWords[] = {
    L"I",
    L"he",
    L"she",
    L"it",
    L"we",
    L"you",
    L"they",
  };

  std::map<string16, bool> word_list;
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kWords); ++i)
    word_list.insert(std::make_pair<string16, bool>(
        base::WideToUTF16(kWords[i]), true));

  RunDictionaryTest(kCodepage, word_list);
}

// Tests whether or not our DicReader can read all the input Russian words.
TEST(ConvertDictTest, Russian) {
  const char kCodepage[] = "KOI8-R";
  const wchar_t* kWords[] = {
    L"\x044f",
    L"\x0442\x044b",
    L"\x043e\x043d",
    L"\x043e\x043d\x0430",
    L"\x043e\x043d\x043e",
    L"\x043c\x044b",
    L"\x0432\x044b",
    L"\x043e\x043d\x0438",
  };

  std::map<string16, bool> word_list;
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kWords); ++i)
    word_list.insert(std::make_pair<string16, bool>(
        base::WideToUTF16(kWords[i]), true));

  RunDictionaryTest(kCodepage, word_list);
}

// Tests whether or not our DicReader can read all the input Hungarian words.
TEST(ConvertDictTest, Hungarian) {
  const char kCodepage[] = "ISO8859-2";
  const wchar_t* kWords[] = {
    L"\x00e9\x006e",
    L"\x0074\x0065",
    L"\x0151",
    L"\x00f6\x006e",
    L"\x006d\x0061\x0067\x0061",
    L"\x006d\x0069",
    L"\x0074\x0069",
    L"\x0151\x006b",
    L"\x00f6\x006e\x00f6\x006b",
    L"\x006d\x0061\x0067\x0075\x006b",
  };

  std::map<string16, bool> word_list;
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kWords); ++i)
    word_list.insert(std::make_pair<string16, bool>(
        base::WideToUTF16(kWords[i]), true));

  RunDictionaryTest(kCodepage, word_list);
}