| 28 | } |
| 29 | |
| 30 | void ExpectRevComplete(const std::string& phrasesName) { |
| 31 | std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest()); |
| 32 | ASSERT_NE(nullptr, runfiles); |
| 33 | |
| 34 | const std::string phrasesFile = |
| 35 | runfiles->Rlocation("_main/data/dictionary/" + phrasesName + ".txt"); |
| 36 | const std::string phrasesRevFile = |
| 37 | runfiles->Rlocation("_main/data/dictionary/" + phrasesName + "Rev" + ".txt"); |
| 38 | |
| 39 | auto loadLexicon = [](const std::string& path) -> LexiconPtr { |
| 40 | FILE* fp = OpenFile(path); |
| 41 | EXPECT_NE(fp, nullptr) << path; |
| 42 | if (fp == nullptr) { |
| 43 | return LexiconPtr(); |
| 44 | } |
| 45 | return Lexicon::ParseLexiconFromFile(fp); |
| 46 | }; |
| 47 | |
| 48 | auto buildMap = [](const LexiconPtr& lexicon) |
| 49 | -> std::unordered_map<std::string, std::unordered_set<std::string>> { |
| 50 | std::unordered_map<std::string, std::unordered_set<std::string>> map; |
| 51 | if (!lexicon) { |
| 52 | return map; |
| 53 | } |
| 54 | for (size_t i = 0; i < lexicon->Length(); ++i) { |
| 55 | const DictEntry* entry = lexicon->At(i); |
| 56 | auto& values = map[entry->Key()]; |
| 57 | for (const auto& value : entry->Values()) { |
| 58 | values.insert(value); |
| 59 | } |
| 60 | } |
| 61 | return map; |
| 62 | }; |
| 63 | |
| 64 | try { |
| 65 | LexiconPtr twPhrases = loadLexicon(phrasesFile); |
| 66 | LexiconPtr twPhrasesRev = loadLexicon(phrasesRevFile); |
| 67 | ASSERT_NE(twPhrases, nullptr); |
| 68 | ASSERT_NE(twPhrasesRev, nullptr); |
| 69 | |
| 70 | auto phrasesMap = buildMap(twPhrases); |
| 71 | auto phrasesRevMap = buildMap(twPhrasesRev); |
| 72 | |
| 73 | for (const auto& entry : phrasesMap) { |
| 74 | const std::string& key = entry.first; |
| 75 | for (const auto& value : entry.second) { |
| 76 | auto it = phrasesRevMap.find(value); |
| 77 | EXPECT_TRUE(it != phrasesRevMap.end() && it->second.count(key) > 0) |
| 78 | << "Missing reverse mapping: " << key << " -> " << value; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | for (const auto& entry : phrasesRevMap) { |
| 83 | const std::string& key = entry.first; |
| 84 | for (const auto& value : entry.second) { |
| 85 | auto it = phrasesMap.find(value); |
| 86 | EXPECT_TRUE(it != phrasesMap.end() && it->second.count(key) > 0) |
| 87 | << "Missing reverse mapping: " << key << " -> " << value; |