| 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 "TestUtilsUTF8.hpp" |
| 20 | #include "TextDictTestBase.hpp" |
| 21 | |
| 22 | namespace opencc { |
| 23 | |
| 24 | class TextDictTest : public TextDictTestBase { |
| 25 | protected: |
| 26 | TextDictTest() : fileName("dict.txt"){}; |
| 27 | |
| 28 | const std::string fileName; |
| 29 | }; |
| 30 | |
| 31 | TEST_F(TextDictTest, DictTest) { TestDict(textDict); } |
| 32 | |
| 33 | TEST_F(TextDictTest, Serialization) { |
| 34 | textDict->opencc::SerializableDict::SerializeToFile(fileName); |
| 35 | } |
| 36 | |
| 37 | TEST_F(TextDictTest, Deserialization) { |
| 38 | const TextDictPtr& deserialized = |
| 39 | SerializableDict::NewFromFile<TextDict>(fileName); |
| 40 | TestDict(deserialized); |
| 41 | } |
| 42 | |
| 43 | TEST_F(TextDictTest, DeserializationEmptyLine) { |
| 44 | FILE* fp = fopen(fileName.c_str(), "w"); |
| 45 | fprintf(fp, "A\tB\n\n\nC\tD E\n\n"); |
| 46 | fclose(fp); |
| 47 | const TextDictPtr& dict = SerializableDict::NewFromFile<TextDict>(fileName); |
| 48 | EXPECT_EQ(dict->GetLexicon()->Length(), 2); |
| 49 | |
| 50 | Optional<const DictEntry*> entry = dict->opencc::Dict::Match("A"); |
| 51 | EXPECT_TRUE(!entry.IsNull()); |
| 52 | EXPECT_EQ(utf8("A"), entry.Get()->Key()); |
| 53 | } |
| 54 | |
| 55 | TEST_F(TextDictTest, ExactMatch) { |
| 56 | auto there = textDict->Match("積羽沉舟", 12); |
| 57 | EXPECT_FALSE(there.IsNull()); |
| 58 | auto dictEntry = there.Get(); |
| 59 | EXPECT_EQ(1, dictEntry->NumValues()); |
| 60 | EXPECT_EQ(utf8("羣輕折軸"), dictEntry->GetDefault()); |
| 61 | |
| 62 | auto nowhere = textDict->Match("積羽沉舟衆口鑠金", 24); |
| 63 | EXPECT_TRUE(nowhere.IsNull()); |
| 64 | } |
| 65 | |
| 66 | TEST_F(TextDictTest, MatchPrefix) { |
| 67 | { |
| 68 | auto there = textDict->MatchPrefix("清華", 3); |
| 69 | EXPECT_FALSE(there.IsNull()); |
| 70 | auto dictEntry = there.Get(); |
| 71 | EXPECT_EQ(utf8("Tsing"), dictEntry->GetDefault()); |
| 72 | } |
| 73 | { |
| 74 | auto there = textDict->MatchPrefix("清華", 5); |
| 75 | EXPECT_FALSE(there.IsNull()); |
| 76 | auto dictEntry = there.Get(); |
| 77 | EXPECT_EQ(utf8("Tsing"), dictEntry->GetDefault()); |
| 78 | } |
| 79 | { |
| 80 | auto there = textDict->MatchPrefix("清華", 6); |
| 81 | EXPECT_FALSE(there.IsNull()); |
| 82 | auto dictEntry = there.Get(); |
| 83 | EXPECT_EQ(utf8("Tsinghua"), dictEntry->GetDefault()); |
| 84 | } |
| 85 | { |
| 86 | auto there = textDict->MatchPrefix("清華", 100); |
| 87 | EXPECT_FALSE(there.IsNull()); |
| 88 | auto dictEntry = there.Get(); |
| 89 | EXPECT_EQ(utf8("Tsinghua"), dictEntry->GetDefault()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | } // namespace opencc |