Debian Codegraph @

data/dictionary/DictionaryTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2024-2026 Carbo Kuo and contributors
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "gtest/gtest.h"
20
21 #include "src/Lexicon.hpp"
22 #include "src/MarisaDict.hpp"
23 #include "src/UTF8Util.hpp"
24 #include "tools/cpp/runfiles/runfiles.h"
25
26 using bazel::tools::cpp::runfiles::Runfiles;
27
28 namespace opencc {
29 namespace {
30
31 #ifndef OPENCC_DICTIONARY_TEST_FILE
32 #error "OPENCC_DICTIONARY_TEST_FILE must be defined"
33 #endif
34
35 static FILE* OpenFile(const std::string& path) {
36 #ifdef _MSC_VER
37 return _wfopen(UTF8Util::GetPlatformString(path).c_str(), L"rb");
38 #else
39 return fopen(UTF8Util::GetPlatformString(path).c_str(), "rb");
40 #endif
41 }
42
43 class DictionaryTest : public ::testing::Test {
44 protected:
45 static void SetUpTestSuite() {
46 runfiles_.reset(Runfiles::CreateForTest());
47 ASSERT_NE(nullptr, runfiles_);
48 }
49
50 static std::unique_ptr<Runfiles> runfiles_;
51 };
52
53 std::unique_ptr<Runfiles> DictionaryTest::runfiles_;
54
55 TEST_F(DictionaryTest, UniqueSortedTest) {
56 const std::string dictionary = OPENCC_DICTIONARY_TEST_FILE;
57 const std::string dictionaryFileName =
58 runfiles_->Rlocation("_main/data/dictionary/" + dictionary);
59 FILE* fp = OpenFile(dictionaryFileName);
60 ASSERT_NE(fp, nullptr);
61 LexiconPtr lexicon = Lexicon::ParseLexiconFromFile(fp);
62 EXPECT_TRUE(lexicon->IsUnique()) << dictionary << " has duplicated keys.";
63 EXPECT_TRUE(lexicon->IsSorted()) << dictionary << " is not sorted.";
64 }
65
66 TEST_F(DictionaryTest, BinaryTest) {
67 std::string dictionary = OPENCC_DICTIONARY_TEST_FILE;
68 const std::string suffix = ".txt";
69 ASSERT_TRUE(dictionary.size() >= suffix.size());
70 ASSERT_EQ(suffix, dictionary.substr(dictionary.size() - suffix.size()));
71 dictionary.erase(dictionary.size() - suffix.size());
72
73 const std::string binaryDictionaryFileName = runfiles_->Rlocation(
74 "_main/data/dictionary/" + dictionary + ".ocd2");
75 FILE* fp_bin = OpenFile(binaryDictionaryFileName);
76 ASSERT_NE(fp_bin, nullptr);
77 MarisaDictPtr dict = MarisaDict::NewFromFile(fp_bin);
78 ASSERT_NE(dict, nullptr);
79
80 const std::string textDictionaryFileName =
81 runfiles_->Rlocation("_main/data/dictionary/" + dictionary + ".txt");
82 FILE* fp_txt = OpenFile(textDictionaryFileName);
83 ASSERT_NE(fp_txt, nullptr);
84 LexiconPtr txt_lexicon = Lexicon::ParseLexiconFromFile(fp_txt);
85
86 EXPECT_EQ(dict->GetLexicon()->Length(), txt_lexicon->Length());
87 }
88
89 } // namespace
90 } // namespace opencc