Debian Codegraph @

src/ConversionChainTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2015-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 "ConversionChain.hpp"
20 #include "Converter.hpp"
21 #include "DictGroupTestBase.hpp"
22 #include "MaxMatchSegmentation.hpp"
23 #include "PipelineConverter.hpp"
24 #include "SingleStageConverter.hpp"
25
26 namespace opencc {
27
28 class ConversionChainTest : public DictGroupTestBase {
29 protected:
30 ConversionChainTest() {}
31
32 virtual void SetUp() {
33 dict = CreateDictGroupForConversion();
34 conversion = ConversionPtr(new Conversion(dict));
35 }
36
37 void SegmentsAssertEquals(const SegmentsPtr& expected,
38 const SegmentsPtr& actual) {
39 const size_t length = expected->Length();
40 EXPECT_TRUE(length == actual->Length());
41 for (size_t i = 0; i < length; i++) {
42 EXPECT_EQ(std::string(expected->At(i)), std::string(actual->At(i)))
43 << "i = " << i;
44 }
45 }
46
47 DictPtr dict;
48 ConversionPtr conversion;
49 };
50
51 TEST_F(ConversionChainTest, Convert) {
52 // Variants
53 const DictPtr& dictVariants = CreateDictForTaiwanVariants();
54 const ConversionPtr& conversionVariants =
55 ConversionPtr(new Conversion(dictVariants));
56 const std::list<ConversionPtr> conversions{conversion, conversionVariants};
57 const ConversionChainPtr& conversionChain =
58 ConversionChainPtr(new ConversionChain(conversions));
59 const SegmentsPtr& converted =
60 conversionChain->Convert(SegmentsPtr(new Segments{utf8("里面")}));
61 SegmentsAssertEquals(SegmentsPtr(new Segments{utf8("裡面")}), converted);
62 }
63
64 TEST_F(ConversionChainTest, StreamKeepsIncompleteIdeographicDescriptionSequence) {
65 LexiconPtr lexicon(new Lexicon);
66 lexicon->Add(DictEntryFactory::New(utf8("钅"), utf8("釒")));
67 lexicon->Add(DictEntryFactory::New(utf8("只"), utf8("隻")));
68 lexicon->Sort();
69
70 DictPtr componentDict(new TextDict(lexicon));
71 SegmentationPtr segmentation(new MaxMatchSegmentation(componentDict));
72 ConversionPtr componentConversion(new Conversion(componentDict));
73 ConversionChainPtr chain(
74 new ConversionChain(std::list<ConversionPtr>{componentConversion}));
75 ConverterPtr converter(new SingleStageConverter(segmentation, chain));
76 ConverterStream stream(converter, 1);
77
78 const std::string firstChunk = utf8("prefix⿰钅");
79 const std::string secondChunk = utf8("只只");
80 std::string output;
81 output += stream.ConvertChunk(firstChunk);
82 output += stream.Finish(secondChunk);
83
84 EXPECT_EQ(utf8("prefix⿰钅只隻"), output);
85 }
86
87 TEST_F(ConversionChainTest,
88 StreamKeepsIncompleteIdeographicDescriptionSequenceBeforeKeepLimit) {
89 LexiconPtr lexicon(new Lexicon);
90 lexicon->Add(DictEntryFactory::New(utf8("只"), utf8("隻")));
91 lexicon->Sort();
92
93 DictPtr componentDict(new TextDict(lexicon));
94 SegmentationPtr segmentation(new MaxMatchSegmentation(componentDict));
95 ConversionPtr componentConversion(new Conversion(componentDict));
96 ConversionChainPtr chain(
97 new ConversionChain(std::list<ConversionPtr>{componentConversion}));
98 ConverterPtr converter(new SingleStageConverter(segmentation, chain));
99 ConverterStream stream(converter, 16);
100
101 std::string nestedPrefix = utf8("prefix");
102 std::string expected = utf8("prefix");
103 for (size_t i = 0; i < 10; i++) {
104 nestedPrefix += utf8("⿰");
105 expected += utf8("⿰");
106 }
107 for (size_t i = 0; i < 10; i++) {
108 nestedPrefix += utf8("木");
109 expected += utf8("木");
110 }
111 expected += utf8("只隻");
112
113 const std::string secondChunk = utf8("只只");
114 std::string output;
115 output += stream.ConvertChunk(nestedPrefix);
116 output += stream.Finish(secondChunk);
117
118 EXPECT_EQ(expected, output);
119 }
120
121 class PipelineConverterTest : public TextDictTestBase {
122 protected:
123 // stage1: 钅 → 釒 stage2: 釒 → 金
124 void SetUp() override {
125 LexiconPtr lex1(new Lexicon);
126 lex1->Add(DictEntryFactory::New(utf8("钅"), utf8("釒")));
127 lex1->Sort();
128 DictPtr dict1(new TextDict(lex1));
129 stage1 = ConverterPtr(new SingleStageConverter(
130 SegmentationPtr(new MaxMatchSegmentation(dict1)),
131 ConversionChainPtr(
132 new ConversionChain({ConversionPtr(new Conversion(dict1))}))));
133 seg1 = stage1->GetSegmentation();
134
135 LexiconPtr lex2(new Lexicon);
136 lex2->Add(DictEntryFactory::New(utf8("釒"), utf8("金")));
137 lex2->Sort();
138 DictPtr dict2(new TextDict(lex2));
139 stage2 = ConverterPtr(new SingleStageConverter(
140 SegmentationPtr(new MaxMatchSegmentation(dict2)),
141 ConversionChainPtr(
142 new ConversionChain({ConversionPtr(new Conversion(dict2))}))));
143 seg2 = stage2->GetSegmentation();
144 }
145
146 ConverterPtr stage1, stage2;
147 SegmentationPtr seg1, seg2;
148 };
149
150 TEST_F(PipelineConverterTest, ConvertChainsStagesInOrder) {
151 PipelineConverter pipeline({stage1, stage2});
152 EXPECT_EQ(utf8("金"), pipeline.Convert(utf8("钅")));
153 }
154
155 TEST_F(PipelineConverterTest, ConvertEmptyPipelineIsIdentity) {
156 PipelineConverter pipeline({});
157 EXPECT_EQ(utf8("钅"), pipeline.Convert(utf8("钅")));
158 }
159
160 TEST_F(PipelineConverterTest, GetSegmentationReturnsLastStage) {
161 PipelineConverter pipeline({stage1, stage2});
162 EXPECT_EQ(seg2, pipeline.GetSegmentation());
163 }
164
165 TEST_F(PipelineConverterTest, GetSegmentationEmptyPipelineReturnsNullptr) {
166 PipelineConverter pipeline({});
167 EXPECT_EQ(nullptr, pipeline.GetSegmentation());
168 }
169
170 TEST_F(PipelineConverterTest, InspectPopulatesPipelineStages) {
171 PipelineConverter pipeline({stage1, stage2});
172 const ConversionInspectionResult result = pipeline.Inspect(utf8("钅"));
173
174 // Pipeline-level fields
175 EXPECT_EQ(utf8("钅"), result.input);
176 EXPECT_EQ(utf8("金"), result.output);
177 // segments/stages are SingleStageConverter concepts; empty at pipeline level
178 EXPECT_TRUE(result.segments.empty());
179 EXPECT_TRUE(result.stages.empty());
180
181 // Each child stage is fully inspected
182 ASSERT_EQ(2u, result.pipelineStages.size());
183
184 // Stage 1: 钅 → 釒
185 EXPECT_EQ(utf8("钅"), result.pipelineStages[0].input);
186 EXPECT_EQ(utf8("釒"), result.pipelineStages[0].output);
187 EXPECT_FALSE(result.pipelineStages[0].segments.empty());
188 EXPECT_FALSE(result.pipelineStages[0].stages.empty());
189 EXPECT_TRUE(result.pipelineStages[0].pipelineStages.empty());
190
191 // Stage 2: 釒 → 金, and its input must equal stage 1's output
192 EXPECT_EQ(result.pipelineStages[0].output, result.pipelineStages[1].input);
193 EXPECT_EQ(utf8("金"), result.pipelineStages[1].output);
194 EXPECT_FALSE(result.pipelineStages[1].stages.empty());
195 EXPECT_TRUE(result.pipelineStages[1].pipelineStages.empty());
196 }
197
198 TEST_F(PipelineConverterTest, InspectEmptyPipelineReturnsTrivialResult) {
199 PipelineConverter pipeline({});
200 const ConversionInspectionResult result = pipeline.Inspect(utf8("钅"));
201 EXPECT_EQ(utf8("钅"), result.input);
202 EXPECT_EQ(utf8("钅"), result.output);
203 EXPECT_TRUE(result.pipelineStages.empty());
204 }
205
206 TEST_F(PipelineConverterTest, InspectOutputMatchesConvert) {
207 PipelineConverter pipeline({stage1, stage2});
208 const ConversionInspectionResult result = pipeline.Inspect(utf8("钅"));
209 EXPECT_EQ(pipeline.Convert(utf8("钅")), result.output);
210 }
211
212 } // namespace opencc