OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "third_party/libaddressinput/chromium/trie.h" |
| 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace autofill { |
| 13 |
| 14 TEST(TrieTest, EmptyTrieHasNoData) { |
| 15 Trie<std::string> trie; |
| 16 std::set<std::string> result; |
| 17 |
| 18 static const uint8_t kKey[] = {0, 1, 2}; |
| 19 trie.FindDataForKeyPrefix(kKey, 3, &result); |
| 20 |
| 21 EXPECT_TRUE(result.empty()); |
| 22 } |
| 23 |
| 24 TEST(TrieTest, CanGetDataByExactKey) { |
| 25 Trie<std::string> trie; |
| 26 std::set<std::string> result; |
| 27 |
| 28 static const uint8_t kKey[] = {1, 2, 3}; |
| 29 static const int32_t kKeySize = 3; |
| 30 trie.AddDataForKey(kKey, kKeySize, "world"); |
| 31 trie.FindDataForKeyPrefix(kKey, kKeySize, &result); |
| 32 |
| 33 std::set<std::string> expected; |
| 34 expected.insert("world"); |
| 35 EXPECT_EQ(expected, result); |
| 36 } |
| 37 |
| 38 TEST(TrieTest, CanGetDataByPrefix) { |
| 39 Trie<std::string> trie; |
| 40 std::set<std::string> result; |
| 41 |
| 42 static const uint8_t kKey[] = {1, 2, 3, 4, 5}; |
| 43 trie.AddDataForKey(kKey, 5, "world"); |
| 44 |
| 45 static const uint8_t kPrefix[] = {1, 2, 3}; |
| 46 trie.FindDataForKeyPrefix(kPrefix, 3, &result); |
| 47 |
| 48 std::set<std::string> expected; |
| 49 expected.insert("world"); |
| 50 EXPECT_EQ(expected, result); |
| 51 } |
| 52 |
| 53 TEST(TrieTest, KeyTooLongNoData) { |
| 54 Trie<std::string> trie; |
| 55 std::set<std::string> result; |
| 56 |
| 57 static const uint8_t kKey[] = {1, 2, 3}; |
| 58 trie.AddDataForKey(kKey, 3, "world"); |
| 59 |
| 60 static const uint8_t kTooLong[] = {1, 2, 3, 4}; |
| 61 trie.FindDataForKeyPrefix(kTooLong, 4, &result); |
| 62 |
| 63 EXPECT_TRUE(result.empty()); |
| 64 } |
| 65 |
| 66 TEST(TrieTest, CommonPrefixFindsMultipleData) { |
| 67 Trie<std::string> trie; |
| 68 std::set<std::string> result; |
| 69 |
| 70 static const uint8_t kHello[] = {1, 2, 3}; |
| 71 trie.AddDataForKey(kHello, 3, "world"); |
| 72 |
| 73 static const uint8_t kHowdy[] = {1, 4, 5}; |
| 74 trie.AddDataForKey(kHowdy, 3, "buddy"); |
| 75 |
| 76 static const uint8_t kFoo[] = {3, 2, 1}; |
| 77 trie.AddDataForKey(kFoo, 3, "bar"); |
| 78 |
| 79 static const uint8_t kPrefix[] = {1}; |
| 80 trie.FindDataForKeyPrefix(kPrefix, 1, &result); |
| 81 |
| 82 std::set<std::string> expected; |
| 83 expected.insert("world"); |
| 84 expected.insert("buddy"); |
| 85 EXPECT_EQ(expected, result); |
| 86 } |
| 87 |
| 88 TEST(TrieTest, KeyCanBePrefixOfOtherKey) { |
| 89 Trie<std::string> trie; |
| 90 std::set<std::string> result; |
| 91 |
| 92 static const uint8_t kHello[] = {1, 2}; |
| 93 trie.AddDataForKey(kHello, 2, "world"); |
| 94 |
| 95 static const uint8_t kHelloo[] = {1, 2, 3}; |
| 96 trie.AddDataForKey(kHelloo, 3, "woorld"); |
| 97 |
| 98 static const uint8_t kHella[] = {1, 3, 4}; |
| 99 trie.AddDataForKey(kHella, 3, "warld"); |
| 100 |
| 101 trie.FindDataForKeyPrefix(kHello, 2, &result); |
| 102 |
| 103 std::set<std::string> expected; |
| 104 expected.insert("world"); |
| 105 expected.insert("woorld"); |
| 106 EXPECT_EQ(expected, result); |
| 107 } |
| 108 |
| 109 TEST(TrieTest, AllowMutlipleKeys) { |
| 110 Trie<std::string> trie; |
| 111 std::set<std::string> result; |
| 112 |
| 113 static const uint8_t kHello[] = {1, 2}; |
| 114 trie.AddDataForKey(kHello, 2, "world"); |
| 115 trie.AddDataForKey(kHello, 2, "woorld"); |
| 116 |
| 117 trie.FindDataForKeyPrefix(kHello, 2, &result); |
| 118 |
| 119 std::set<std::string> expected; |
| 120 expected.insert("world"); |
| 121 expected.insert("woorld"); |
| 122 EXPECT_EQ(expected, result); |
| 123 } |
| 124 |
| 125 TEST(TrieTest, CanFindVeryLongKey) { |
| 126 Trie<std::string> trie; |
| 127 std::set<std::string> result; |
| 128 |
| 129 static const uint8_t kVeryLongKey[] = { |
| 130 9, 3, 2, 6, 7, 0, 2, 7, 9, 3, 6, 8, 9, 2, 1, 6, 1, 5, 0, 3, 5}; |
| 131 static const size_t kKeySize = sizeof kVeryLongKey; |
| 132 trie.AddDataForKey(kVeryLongKey, kKeySize, "world"); |
| 133 |
| 134 trie.FindDataForKeyPrefix(kVeryLongKey, kKeySize, &result); |
| 135 |
| 136 std::set<std::string> expected; |
| 137 expected.insert("world"); |
| 138 EXPECT_EQ(expected, result); |
| 139 } |
| 140 |
| 141 } // namespace autofill |
OLD | NEW |