File: DictionaryTest.cpp

package info (click to toggle)
opencc 1.1.9%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,380 kB
  • sloc: cpp: 4,730; python: 361; javascript: 133; makefile: 126; sh: 59
file content (90 lines) | stat: -rw-r--r-- 3,224 bytes parent folder | download | duplicates (2)
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
/*
 * Open Chinese Convert
 *
 * Copyright 2024-2024 Carbo Kuo <byvoid@byvoid.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gtest/gtest.h"

#include "src/Lexicon.hpp"
#include "src/MarisaDict.hpp"
#include "src/UTF8Util.hpp"

namespace opencc {

const char* RUNFILE_SUFFIX = ".runfiles/_main";

class DictionaryTest : public ::testing::Test,
                       public ::testing::WithParamInterface<std::string> {
protected:
  static void SetUpTestSuite() {

    std::string program_filename = ::testing::internal::GetArgvs().front();
    size_t suffix_pos = program_filename.find(RUNFILE_SUFFIX);
    ASSERT_NE(suffix_pos, std::string::npos);

    runfile_dir_ =
        program_filename.substr(0, suffix_pos + strlen(RUNFILE_SUFFIX));
  }

  static std::string runfile_dir_;
};

std::string DictionaryTest::runfile_dir_;

INSTANTIATE_TEST_SUITE_P(
    , DictionaryTest,
    ::testing::Values("HKVariants", "HKVariantsRevPhrases",
                      "JPShinjitaiCharacters", "JPShinjitaiPhrases",
                      "JPVariants", "STCharacters", "STPhrases", "TSCharacters",
                      "TSPhrases", "TWPhrasesIT", "TWPhrasesName",
                      "TWPhrasesOther", "TWVariants", "TWVariantsRevPhrases",
                      "TWPhrases", "TWVariantsRev", "TWPhrasesRev",
                      "HKVariantsRev", "JPVariantsRev"),
    [](const testing::TestParamInfo<DictionaryTest::ParamType>& info) {
      return info.param;
    });

TEST_P(DictionaryTest, UniqueSortedTest) {
  const std::string dictionaryFileName =
      runfile_dir_ + "/data/dictionary/" + GetParam() + ".txt";
  FILE* fp =
      fopen(UTF8Util::GetPlatformString(dictionaryFileName).c_str(), "rb");
  ASSERT_NE(fp, nullptr);
  LexiconPtr lexicon = Lexicon::ParseLexiconFromFile(fp);
  EXPECT_TRUE(lexicon->IsUnique()) << GetParam() << " has duplicated keys.";
  EXPECT_TRUE(lexicon->IsSorted()) << GetParam() << " is not sorted.";
}

TEST_P(DictionaryTest, BinaryTest) {
  const std::string binaryDictionaryFileName =
      runfile_dir_ + "/data/dictionary/" + GetParam() + ".ocd2";
  FILE* fp_bin = fopen(
      UTF8Util::GetPlatformString(binaryDictionaryFileName).c_str(), "rb");
  ASSERT_NE(fp_bin, nullptr);
  MarisaDictPtr dict = MarisaDict::NewFromFile(fp_bin);
  ASSERT_NE(dict, nullptr);

  const std::string textDictionaryFileName =
      runfile_dir_ + "/data/dictionary/" + GetParam() + ".txt";
  FILE* fp_txt =
      fopen(UTF8Util::GetPlatformString(textDictionaryFileName).c_str(), "rb");
  ASSERT_NE(fp_txt, nullptr);
  LexiconPtr txt_lexicon = Lexicon::ParseLexiconFromFile(fp_txt);

  EXPECT_EQ(dict->GetLexicon()->Length(), txt_lexicon->Length());
}

} // namespace opencc