Debian Codegraph @

src/LexiconAnnotationTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert (OpenCC) LexiconAnnotationTest
3 *
4 * Copyright 2026 Frank Lin <[email protected]>
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 * https://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 "Lexicon.hpp"
20 #include "SerializableDict.hpp"
21 #include "TestUtils.hpp"
22 #include "TestUtilsUTF8.hpp"
23 #include "TextDict.hpp"
24
25 namespace opencc {
26
27 class LexiconAnnotationTest : public ::testing::Test {
28 protected:
29 const std::string testFileName = "test_annotation_dict.txt";
30
31 void TearDown() override { remove(testFileName.c_str()); }
32 };
33
34 TEST_F(LexiconAnnotationTest, ParseCommentLines) {
35 FILE* fp = fopen(testFileName.c_str(), "w");
36 fprintf(fp, "# This is a header comment\n");
37 fprintf(fp, "# Line 2 of header\n");
38 fprintf(fp, "\n");
39 fprintf(fp, "A\tB\n");
40 fprintf(fp, "C\tD\n");
41 fclose(fp);
42
43 FILE* readFp = fopen(testFileName.c_str(), "r");
44 const TextDictPtr& dict = TextDict::NewFromFile(readFp);
45 fclose(readFp);
46 EXPECT_EQ(dict->GetLexicon()->Length(), 2);
47 }
48
49 TEST_F(LexiconAnnotationTest, ParseAttachedComment) {
50 FILE* fp = fopen(testFileName.c_str(), "w");
51 fprintf(fp, "# Header\n");
52 fprintf(fp, "\n");
53 fprintf(fp, "# Comment for A\n");
54 fprintf(fp, "A\tB\n");
55 fprintf(fp, "C\tD\n");
56 fclose(fp);
57
58 FILE* readFp = fopen(testFileName.c_str(), "r");
59 const TextDictPtr& dict = TextDict::NewFromFile(readFp);
60 fclose(readFp);
61 EXPECT_EQ(dict->GetLexicon()->Length(), 2);
62 }
63
64 TEST_F(LexiconAnnotationTest, ParseFloatingComment) {
65 FILE* fp = fopen(testFileName.c_str(), "w");
66 fprintf(fp, "A\tB\n");
67 fprintf(fp, "\n");
68 fprintf(fp, "# This is a floating comment\n");
69 fprintf(fp, "\n");
70 fprintf(fp, "C\tD\n");
71 fclose(fp);
72
73 FILE* readFp = fopen(testFileName.c_str(), "r");
74 const TextDictPtr& dict = TextDict::NewFromFile(readFp);
75 fclose(readFp);
76 EXPECT_EQ(dict->GetLexicon()->Length(), 2);
77 }
78
79 TEST_F(LexiconAnnotationTest, ParseFooterComment) {
80 FILE* fp = fopen(testFileName.c_str(), "w");
81 fprintf(fp, "A\tB\n");
82 fprintf(fp, "C\tD\n");
83 fprintf(fp, "\n");
84 fprintf(fp, "# Footer comment\n");
85 fprintf(fp, "# Line 2 of footer\n");
86 fclose(fp);
87
88 FILE* readFp = fopen(testFileName.c_str(), "r");
89 const TextDictPtr& dict = TextDict::NewFromFile(readFp);
90 fclose(readFp);
91 EXPECT_EQ(dict->GetLexicon()->Length(), 2);
92 }
93
94 TEST_F(LexiconAnnotationTest, SerializeIgnoresComments) {
95 FILE* fp = fopen(testFileName.c_str(), "w");
96 fprintf(fp, "# Header\n");
97 fprintf(fp, "\n");
98 fprintf(fp, "# Comment for B\n");
99 fprintf(fp, "B\tBB\n");
100 fprintf(fp, "A\tAA\n");
101 fprintf(fp, "\n");
102 fprintf(fp, "# Footer\n");
103 fclose(fp);
104
105 FILE* readFp = fopen(testFileName.c_str(), "r");
106 const TextDictPtr& dict = TextDict::NewFromFile(readFp);
107 fclose(readFp);
108
109 // Serialize back
110 const std::string outputFileName = "test_annotation_dict_output.txt";
111 FILE* outFp = fopen(outputFileName.c_str(), "w");
112 dict->SerializeToFile(outFp);
113 fclose(outFp);
114
115 // Read back and verify
116 FILE* outputFp = fopen(outputFileName.c_str(), "r");
117 char buff[1024];
118 std::vector<std::string> lines;
119 while (fgets(buff, sizeof(buff), outputFp)) {
120 std::string line(buff);
121 while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) {
122 line.pop_back();
123 }
124 lines.push_back(line);
125 }
126 fclose(outputFp);
127 remove(outputFileName.c_str());
128
129 EXPECT_EQ(lines.size(), 2);
130 EXPECT_EQ(lines[0], "A\tAA");
131 EXPECT_EQ(lines[1], "B\tBB");
132 }
133
134 TEST_F(LexiconAnnotationTest, SortIgnoresComments) {
135 FILE* fp = fopen(testFileName.c_str(), "w");
136 fprintf(fp, "# Header\n");
137 fprintf(fp, "\n");
138 fprintf(fp, "# Comment for C\n");
139 fprintf(fp, "C\tCC\n");
140 fprintf(fp, "# Comment for A\n");
141 fprintf(fp, "A\tAA\n");
142 fprintf(fp, "B\tBB\n");
143 fclose(fp);
144
145 FILE* readFp = fopen(testFileName.c_str(), "r");
146 const TextDictPtr& dict = TextDict::NewFromFile(readFp);
147 fclose(readFp);
148
149 EXPECT_EQ(dict->GetLexicon()->Length(), 3);
150 }
151
152 TEST_F(LexiconAnnotationTest, DefaultBehaviorIgnoresComments) {
153 FILE* fp = fopen(testFileName.c_str(), "w");
154 fprintf(fp, "A\tB\n");
155 fprintf(fp, "C\tD\n");
156 fclose(fp);
157
158 // Default behavior should ignore comments
159 FILE* readFp = fopen(testFileName.c_str(), "r");
160 const TextDictPtr& dict = TextDict::NewFromFile(readFp);
161 fclose(readFp);
162
163 EXPECT_EQ(dict->GetLexicon()->Length(), 2);
164 }
165
166 TEST_F(LexiconAnnotationTest, DefaultBehaviorAcceptsCommentLines) {
167 FILE* fp = fopen(testFileName.c_str(), "w");
168 fprintf(fp, "# This is a comment\n");
169 fprintf(fp, "A\tB\n");
170 fclose(fp);
171
172 FILE* readFp = fopen(testFileName.c_str(), "r");
173 const TextDictPtr& dict = TextDict::NewFromFile(readFp);
174 fclose(readFp);
175
176 EXPECT_EQ(dict->GetLexicon()->Length(), 1);
177 }
178
179 } // namespace opencc