| OLD | NEW |
| (Empty) |
| 1 // Copyright (C) 2014 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 #include "util/trie.h" | |
| 16 | |
| 17 #include <set> | |
| 18 #include <string> | |
| 19 | |
| 20 #include <gtest/gtest.h> | |
| 21 | |
| 22 namespace i18n { | |
| 23 namespace addressinput { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 TEST(TrieTest, EmptyTrieHasNoData) { | |
| 28 Trie<std::string> trie; | |
| 29 std::set<std::string> result; | |
| 30 trie.FindDataForKeyPrefix("key", &result); | |
| 31 EXPECT_TRUE(result.empty()); | |
| 32 } | |
| 33 | |
| 34 TEST(TrieTest, CanGetDataByExactKey) { | |
| 35 Trie<std::string> trie; | |
| 36 trie.AddDataForKey("hello", "world"); | |
| 37 std::set<std::string> result; | |
| 38 trie.FindDataForKeyPrefix("hello", &result); | |
| 39 std::set<std::string> expected; | |
| 40 expected.insert("world"); | |
| 41 EXPECT_EQ(expected, result); | |
| 42 } | |
| 43 | |
| 44 TEST(TrieTest, CanGetDataByPrefix) { | |
| 45 Trie<std::string> trie; | |
| 46 trie.AddDataForKey("hello", "world"); | |
| 47 std::set<std::string> result; | |
| 48 trie.FindDataForKeyPrefix("he", &result); | |
| 49 std::set<std::string> expected; | |
| 50 expected.insert("world"); | |
| 51 EXPECT_EQ(expected, result); | |
| 52 } | |
| 53 | |
| 54 TEST(TrieTest, KeyTooLongNoData) { | |
| 55 Trie<std::string> trie; | |
| 56 trie.AddDataForKey("hello", "world"); | |
| 57 std::set<std::string> result; | |
| 58 trie.FindDataForKeyPrefix("helloo", &result); | |
| 59 EXPECT_TRUE(result.empty()); | |
| 60 } | |
| 61 | |
| 62 TEST(TrieTest, CommonPrefixFindsMultipleData) { | |
| 63 Trie<std::string> trie; | |
| 64 trie.AddDataForKey("hello", "world"); | |
| 65 trie.AddDataForKey("howdy", "buddy"); | |
| 66 trie.AddDataForKey("foo", "bar"); | |
| 67 std::set<std::string> results; | |
| 68 trie.FindDataForKeyPrefix("h", &results); | |
| 69 std::set<std::string> expected; | |
| 70 expected.insert("world"); | |
| 71 expected.insert("buddy"); | |
| 72 EXPECT_EQ(expected, results); | |
| 73 } | |
| 74 | |
| 75 TEST(TrieTest, KeyCanBePrefixOfOtherKey) { | |
| 76 Trie<std::string> trie; | |
| 77 trie.AddDataForKey("hello", "world"); | |
| 78 trie.AddDataForKey("helloo", "woorld"); | |
| 79 trie.AddDataForKey("hella", "warld"); | |
| 80 std::set<std::string> results; | |
| 81 trie.FindDataForKeyPrefix("hello", &results); | |
| 82 std::set<std::string> expected; | |
| 83 expected.insert("world"); | |
| 84 expected.insert("woorld"); | |
| 85 EXPECT_EQ(expected, results); | |
| 86 } | |
| 87 | |
| 88 TEST(TrieTest, AllowMutlipleKeys) { | |
| 89 Trie<std::string> trie; | |
| 90 trie.AddDataForKey("hello", "world"); | |
| 91 trie.AddDataForKey("hello", "woorld"); | |
| 92 std::set<std::string> results; | |
| 93 trie.FindDataForKeyPrefix("hello", &results); | |
| 94 std::set<std::string> expected; | |
| 95 expected.insert("world"); | |
| 96 expected.insert("woorld"); | |
| 97 EXPECT_EQ(expected, results); | |
| 98 } | |
| 99 | |
| 100 TEST(TrieTest, CanFindVeryLongKey) { | |
| 101 Trie<std::string> trie; | |
| 102 static const char kVeryLongKey[] = "1234567890qwertyuioasdfghj"; | |
| 103 trie.AddDataForKey(kVeryLongKey, "world"); | |
| 104 std::set<std::string> result; | |
| 105 trie.FindDataForKeyPrefix(kVeryLongKey, &result); | |
| 106 std::set<std::string> expected; | |
| 107 expected.insert("world"); | |
| 108 EXPECT_EQ(expected, result); | |
| 109 } | |
| 110 | |
| 111 } // namespace | |
| 112 | |
| 113 } // namespace addressinput | |
| 114 } // namespace i18n | |
| OLD | NEW |