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