| 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 "DictGroupTestBase.hpp" |
| 20 | |
| 21 | namespace opencc { |
| 22 | |
| 23 | class DictGroupTest : public DictGroupTestBase { |
| 24 | protected: |
| 25 | DictGroupTest() {} |
| 26 | }; |
| 27 | |
| 28 | TEST_F(DictGroupTest, KeyMaxLength) { |
| 29 | const DictGroupPtr& dictGroup = CreateDictGroupForConversion(); |
| 30 | EXPECT_EQ(6, dictGroup->KeyMaxLength()); |
| 31 | EXPECT_EQ(6, dictGroup->GetDicts().front()->KeyMaxLength()); |
| 32 | EXPECT_EQ(3, dictGroup->GetDicts().back()->KeyMaxLength()); |
| 33 | } |
| 34 | |
| 35 | TEST_F(DictGroupTest, MatchPolicyDefaultsToShortCircuit) { |
| 36 | const DictGroupPtr& dictGroup = CreateDictGroupForConversion(); |
| 37 | EXPECT_EQ(DictGroupMatchPolicy::ShortCircuit, dictGroup->GetMatchPolicy()); |
| 38 | } |
| 39 | |
| 40 | TEST_F(DictGroupTest, ExplicitMatchPolicy) { |
| 41 | const DictPtr phrasesDict = CreateDictForPhrases(); |
| 42 | const DictPtr charactersDict = CreateDictForCharacters(); |
| 43 | const DictGroupPtr dictGroup(new DictGroup( |
| 44 | std::list<DictPtr>{phrasesDict, charactersDict}, |
| 45 | DictGroupMatchPolicy::ShortCircuit)); |
| 46 | EXPECT_EQ(DictGroupMatchPolicy::ShortCircuit, dictGroup->GetMatchPolicy()); |
| 47 | } |
| 48 | |
| 49 | TEST_F(DictGroupTest, UnionMatchPolicy) { |
| 50 | const DictPtr phrasesDict = CreateDictForPhrases(); |
| 51 | const DictPtr charactersDict = CreateDictForCharacters(); |
| 52 | const DictGroupPtr dictGroup( |
| 53 | new UnionDictGroup(std::list<DictPtr>{phrasesDict, charactersDict})); |
| 54 | EXPECT_EQ(DictGroupMatchPolicy::Union, dictGroup->GetMatchPolicy()); |
| 55 | } |
| 56 | |
| 57 | TEST_F(DictGroupTest, UnionMatchPrefixPrefersLaterLongerMatch) { |
| 58 | LexiconPtr firstLexicon(new Lexicon); |
| 59 | firstLexicon->Add(DictEntryFactory::New(utf8("意大利"), utf8("義大利"))); |
| 60 | firstLexicon->Sort(); |
| 61 | DictPtr firstDict(new TextDict(firstLexicon)); |
| 62 | |
| 63 | LexiconPtr secondLexicon(new Lexicon); |
| 64 | secondLexicon->Add(DictEntryFactory::New(utf8("意大利面"), utf8("義大利麵"))); |
| 65 | secondLexicon->Sort(); |
| 66 | DictPtr secondDict(new TextDict(secondLexicon)); |
| 67 | |
| 68 | const DictGroupPtr dictGroup( |
| 69 | new UnionDictGroup(std::list<DictPtr>{firstDict, secondDict})); |
| 70 | const std::string query = utf8("意大利面"); |
| 71 | const auto& entry = dictGroup->MatchPrefix(query.c_str(), query.length()); |
| 72 | EXPECT_FALSE(entry.IsNull()); |
| 73 | EXPECT_EQ(utf8("意大利面"), entry.Get()->Key()); |
| 74 | EXPECT_EQ(utf8("義大利麵"), entry.Get()->GetDefault()); |
| 75 | } |
| 76 | |
| 77 | TEST_F(DictGroupTest, SimpleGroupTest) { |
| 78 | const DictGroupPtr& dictGroup = CreateDictGroupForConversion(); |
| 79 | { |
| 80 | const auto& entry = dictGroup->Dict::MatchPrefix(utf8("Unknown")); |
| 81 | EXPECT_TRUE(entry.IsNull()); |
| 82 | } |
| 83 | { |
| 84 | const auto& entry = dictGroup->Dict::MatchPrefix(utf8("里面")); |
| 85 | EXPECT_FALSE(entry.IsNull()); |
| 86 | EXPECT_EQ(3, entry.Get()->KeyLength()); |
| 87 | EXPECT_EQ(utf8("裏"), entry.Get()->GetDefault()); |
| 88 | } |
| 89 | { |
| 90 | const auto& matches = dictGroup->Dict::MatchAllPrefixes(utf8("干燥")); |
| 91 | EXPECT_EQ(2, matches.size()); |
| 92 | EXPECT_EQ(utf8("乾燥"), matches.at(0)->GetDefault()); |
| 93 | EXPECT_EQ(utf8("幹"), matches.at(1)->GetDefault()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | TEST_F(DictGroupTest, TaiwanPhraseGroupTest) { |
| 98 | const DictGroupPtr dictGroup(new DictGroup( |
| 99 | std::list<DictPtr>{CreateDictForPhrases(), CreateTaiwanPhraseDict()})); |
| 100 | { |
| 101 | const auto& entry = dictGroup->Dict::MatchPrefix(utf8("鼠标")); |
| 102 | EXPECT_EQ(utf8("鼠標"), entry.Get()->GetDefault()); |
| 103 | } |
| 104 | { |
| 105 | const auto& entry = dictGroup->Dict::MatchPrefix(utf8("克罗地亚")); |
| 106 | EXPECT_EQ(utf8("克羅埃西亞"), entry.Get()->GetDefault()); |
| 107 | } |
| 108 | { |
| 109 | const auto& matches = dictGroup->Dict::MatchAllPrefixes(utf8("鼠标")); |
| 110 | EXPECT_EQ(1, matches.size()); |
| 111 | EXPECT_EQ(utf8("鼠標"), matches[0]->GetDefault()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | } // namespace opencc |