Fix a bug in the calculation of DictGroup::keyMaxLength.
authorCarbo Kuo <byvoid@byvoid.com>
Thu, 25 Feb 2021 11:48:50 +0000 (20:48 +0900)
committerShengjing Zhu <zhsj@debian.org>
Sun, 7 Mar 2021 06:20:40 +0000 (06:20 +0000)
The length should be the maximum of all sub-dictionaries in the dictionary group.

Gbp-Pq: Name 0006-Fix-a-bug-in-the-calculation-of-DictGroup-keyMaxLeng.patch

src/DictGroup.cpp
src/DictGroupTest.cpp

index 4ca9e3363da387b9e264c4c1e6d6d98992ea60ad..c682e9619487c7ba4b52f36ca44eb81924a511f5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Open Chinese Convert
  *
- * Copyright 2010-2014 Carbo Kuo <byvoid@byvoid.com>
+ * Copyright 2010-2021 Carbo Kuo <byvoid@byvoid.com>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 using namespace opencc;
 
+namespace {
+
+size_t GetKeyMaxLength(const std::list<DictPtr>& dicts) {
+  size_t keyMaxLength = 0;
+  for (const DictPtr& dict : dicts) {
+    keyMaxLength = std::max(keyMaxLength, dict->KeyMaxLength());
+  }
+  return keyMaxLength;
+}
+
+} // namespace
+
 DictGroup::DictGroup(const std::list<DictPtr>& _dicts)
-    : keyMaxLength(0), dicts(_dicts) {}
+    : keyMaxLength(GetKeyMaxLength(_dicts)), dicts(_dicts) {}
 
 DictGroup::~DictGroup() {}
 
index 7003506c291a2cf576e34f92e65c4e9dcc72272b..c91731ed18e112e5523644fd503212ccf9a8f385 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Open Chinese Convert
  *
- * Copyright 2015 Carbo Kuo <byvoid@byvoid.com>
+ * Copyright 2015-2021 Carbo Kuo <byvoid@byvoid.com>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,15 +25,31 @@ protected:
   DictGroupTest() {}
 };
 
-TEST_F(DictGroupTest, SimpleGroupTest) {
+TEST_F(DictGroupTest, KeyMaxLength) {
   const DictGroupPtr& dictGroup = CreateDictGroupForConversion();
-  const auto& entry = dictGroup->Dict::MatchPrefix(utf8("Unknown"));
-  EXPECT_TRUE(entry.IsNull());
+  EXPECT_EQ(6, dictGroup->KeyMaxLength());
+  EXPECT_EQ(6, dictGroup->GetDicts().front()->KeyMaxLength());
+  EXPECT_EQ(3, dictGroup->GetDicts().back()->KeyMaxLength());
+}
 
-  const auto& matches = dictGroup->Dict::MatchAllPrefixes(utf8("干燥"));
-  EXPECT_EQ(2, matches.size());
-  EXPECT_EQ(utf8("乾燥"), matches.at(0)->GetDefault());
-  EXPECT_EQ(utf8("幹"), matches.at(1)->GetDefault());
+TEST_F(DictGroupTest, SimpleGroupTest) {
+  const DictGroupPtr& dictGroup = CreateDictGroupForConversion();
+  {
+    const auto& entry = dictGroup->Dict::MatchPrefix(utf8("Unknown"));
+    EXPECT_TRUE(entry.IsNull());
+  }
+  {
+    const auto& entry = dictGroup->Dict::MatchPrefix(utf8("里面"));
+    EXPECT_FALSE(entry.IsNull());
+    EXPECT_EQ(3, entry.Get()->KeyLength());
+    EXPECT_EQ(utf8("裏"), entry.Get()->GetDefault());
+  }
+  {
+    const auto& matches = dictGroup->Dict::MatchAllPrefixes(utf8("干燥"));
+    EXPECT_EQ(2, matches.size());
+    EXPECT_EQ(utf8("乾燥"), matches.at(0)->GetDefault());
+    EXPECT_EQ(utf8("幹"), matches.at(1)->GetDefault());
+  }
 }
 
 TEST_F(DictGroupTest, TaiwanPhraseGroupTest) {