Debian Codegraph @

src/MarisaDictTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2020-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 <cstring>
20
21 #include "MarisaDict.hpp"
22 #include "TestUtilsUTF8.hpp"
23 #include "TextDictTestBase.hpp"
24
25 namespace opencc {
26
27 class MarisaDictTest : public TextDictTestBase {
28 protected:
29 MarisaDictTest()
30 : dict(MarisaDict::NewFromDict(*textDict)), fileName("dict.ocd2"){};
31
32 // Write a crafted OCD2 file with a valid header but corrupt trie data.
33 static std::string WriteMalformedMarisaFile() {
34 const std::string path = "malformed_marisa.ocd2";
35 FILE* fp = fopen(path.c_str(), "wb");
36 const char* header = "OPENCC_MARISA_0.2.5";
37 fwrite(header, sizeof(char), strlen(header), fp);
38 // Write garbage trie data
39 char garbage[128];
40 memset(garbage, 0xFF, sizeof(garbage));
41 fwrite(garbage, 1, sizeof(garbage), fp);
42 fclose(fp);
43 return path;
44 }
45
46 const MarisaDictPtr dict;
47 const std::string fileName;
48 };
49
50 TEST_F(MarisaDictTest, DictTest) { TestDict(dict); }
51
52 TEST_F(MarisaDictTest, Serialization) {
53 dict->opencc::SerializableDict::SerializeToFile(fileName);
54 }
55
56 TEST_F(MarisaDictTest, Deserialization) {
57 const MarisaDictPtr& deserialized =
58 SerializableDict::NewFromFile<MarisaDict>(fileName);
59 TestDict(deserialized);
60
61 const LexiconPtr& lex1 = dict->GetLexicon();
62 const LexiconPtr& lex2 = deserialized->GetLexicon();
63
64 // Compare every entry
65 EXPECT_EQ(lex1->Length(), lex2->Length());
66 for (size_t i = 0; i < lex1->Length(); i++) {
67 EXPECT_EQ(lex1->At(i)->Key(), lex2->At(i)->Key());
68 EXPECT_EQ(lex1->At(i)->NumValues(), lex2->At(i)->NumValues());
69 }
70 }
71
72 TEST_F(MarisaDictTest, ExactMatch) {
73 auto there = dict->Match("積羽沉舟", 12);
74 EXPECT_FALSE(there.IsNull());
75 auto dictEntry = there.Get();
76 EXPECT_EQ(1, dictEntry->NumValues());
77 EXPECT_EQ(utf8("羣輕折軸"), dictEntry->GetDefault());
78
79 auto nowhere = dict->Match("積羽沉舟衆口鑠金", 24);
80 EXPECT_TRUE(nowhere.IsNull());
81 }
82
83 TEST_F(MarisaDictTest, MatchPrefix) {
84 {
85 auto there = dict->MatchPrefix("清華", 3);
86 EXPECT_FALSE(there.IsNull());
87 auto dictEntry = there.Get();
88 EXPECT_EQ(utf8("Tsing"), dictEntry->GetDefault());
89 }
90 {
91 auto there = dict->MatchPrefix("清華", 5);
92 EXPECT_FALSE(there.IsNull());
93 auto dictEntry = there.Get();
94 EXPECT_EQ(utf8("Tsing"), dictEntry->GetDefault());
95 }
96 {
97 auto there = dict->MatchPrefix("清華", 6);
98 EXPECT_FALSE(there.IsNull());
99 auto dictEntry = there.Get();
100 EXPECT_EQ(utf8("Tsinghua"), dictEntry->GetDefault());
101 }
102 {
103 auto there = dict->MatchPrefix("清華", 100);
104 EXPECT_FALSE(there.IsNull());
105 auto dictEntry = there.Get();
106 EXPECT_EQ(utf8("Tsinghua"), dictEntry->GetDefault());
107 }
108 }
109
110 // Test that corrupt marisa trie data triggers InvalidFormat (#814, #817).
111 TEST_F(MarisaDictTest, RejectsCorruptTrieData) {
112 std::string path = WriteMalformedMarisaFile();
113 EXPECT_THROW(SerializableDict::NewFromFile<MarisaDict>(path), InvalidFormat);
114 std::remove(path.c_str());
115 }
116
117 } // namespace opencc