OLD | NEW |
1 // Copyright (C) 2014 Google Inc. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // found in the LICENSE file. |
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 | 4 |
15 #include "util/trie.h" | 5 #include "third_party/libaddressinput/chromium/trie.h" |
16 | 6 |
| 7 #include <stdint.h> |
17 #include <set> | 8 #include <set> |
18 #include <string> | 9 #include <string> |
19 | 10 |
20 #include <gtest/gtest.h> | 11 #include "testing/gtest/include/gtest/gtest.h" |
21 | 12 |
22 namespace i18n { | 13 namespace autofill { |
23 namespace addressinput { | |
24 | 14 |
25 namespace { | 15 namespace { |
26 | 16 |
| 17 std::vector<uint8_t> ToByteArray(const std::string& text) { |
| 18 std::vector<uint8_t> result(text.length() + 1, 0); |
| 19 result.assign(text.begin(), text.end()); |
| 20 return result; |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
27 TEST(TrieTest, EmptyTrieHasNoData) { | 25 TEST(TrieTest, EmptyTrieHasNoData) { |
28 Trie<std::string> trie; | 26 Trie<std::string> trie; |
29 std::set<std::string> result; | 27 std::set<std::string> result; |
30 trie.FindDataForKeyPrefix("key", &result); | 28 trie.FindDataForKeyPrefix(ToByteArray("key"), &result); |
31 EXPECT_TRUE(result.empty()); | 29 EXPECT_TRUE(result.empty()); |
32 } | 30 } |
33 | 31 |
34 TEST(TrieTest, CanGetDataByExactKey) { | 32 TEST(TrieTest, CanGetDataByExactKey) { |
35 Trie<std::string> trie; | 33 Trie<std::string> trie; |
36 trie.AddDataForKey("hello", "world"); | 34 trie.AddDataForKey(ToByteArray("hello"), "world"); |
37 std::set<std::string> result; | 35 std::set<std::string> result; |
38 trie.FindDataForKeyPrefix("hello", &result); | 36 trie.FindDataForKeyPrefix(ToByteArray("hello"), &result); |
39 std::set<std::string> expected; | 37 std::set<std::string> expected; |
40 expected.insert("world"); | 38 expected.insert("world"); |
41 EXPECT_EQ(expected, result); | 39 EXPECT_EQ(expected, result); |
42 } | 40 } |
43 | 41 |
44 TEST(TrieTest, CanGetDataByPrefix) { | 42 TEST(TrieTest, CanGetDataByPrefix) { |
45 Trie<std::string> trie; | 43 Trie<std::string> trie; |
46 trie.AddDataForKey("hello", "world"); | 44 trie.AddDataForKey(ToByteArray("hello"), "world"); |
47 std::set<std::string> result; | 45 std::set<std::string> result; |
48 trie.FindDataForKeyPrefix("he", &result); | 46 trie.FindDataForKeyPrefix(ToByteArray("he"), &result); |
49 std::set<std::string> expected; | 47 std::set<std::string> expected; |
50 expected.insert("world"); | 48 expected.insert("world"); |
51 EXPECT_EQ(expected, result); | 49 EXPECT_EQ(expected, result); |
52 } | 50 } |
53 | 51 |
54 TEST(TrieTest, KeyTooLongNoData) { | 52 TEST(TrieTest, KeyTooLongNoData) { |
55 Trie<std::string> trie; | 53 Trie<std::string> trie; |
56 trie.AddDataForKey("hello", "world"); | 54 trie.AddDataForKey(ToByteArray("hello"), "world"); |
57 std::set<std::string> result; | 55 std::set<std::string> result; |
58 trie.FindDataForKeyPrefix("helloo", &result); | 56 trie.FindDataForKeyPrefix(ToByteArray("helloo"), &result); |
59 EXPECT_TRUE(result.empty()); | 57 EXPECT_TRUE(result.empty()); |
60 } | 58 } |
61 | 59 |
62 TEST(TrieTest, CommonPrefixFindsMultipleData) { | 60 TEST(TrieTest, CommonPrefixFindsMultipleData) { |
63 Trie<std::string> trie; | 61 Trie<std::string> trie; |
64 trie.AddDataForKey("hello", "world"); | 62 trie.AddDataForKey(ToByteArray("hello"), "world"); |
65 trie.AddDataForKey("howdy", "buddy"); | 63 trie.AddDataForKey(ToByteArray("howdy"), "buddy"); |
66 trie.AddDataForKey("foo", "bar"); | 64 trie.AddDataForKey(ToByteArray("foo"), "bar"); |
67 std::set<std::string> results; | 65 std::set<std::string> results; |
68 trie.FindDataForKeyPrefix("h", &results); | 66 trie.FindDataForKeyPrefix(ToByteArray("h"), &results); |
69 std::set<std::string> expected; | 67 std::set<std::string> expected; |
70 expected.insert("world"); | 68 expected.insert("world"); |
71 expected.insert("buddy"); | 69 expected.insert("buddy"); |
72 EXPECT_EQ(expected, results); | 70 EXPECT_EQ(expected, results); |
73 } | 71 } |
74 | 72 |
75 TEST(TrieTest, KeyCanBePrefixOfOtherKey) { | 73 TEST(TrieTest, KeyCanBePrefixOfOtherKey) { |
76 Trie<std::string> trie; | 74 Trie<std::string> trie; |
77 trie.AddDataForKey("hello", "world"); | 75 trie.AddDataForKey(ToByteArray("hello"), "world"); |
78 trie.AddDataForKey("helloo", "woorld"); | 76 trie.AddDataForKey(ToByteArray("helloo"), "woorld"); |
79 trie.AddDataForKey("hella", "warld"); | 77 trie.AddDataForKey(ToByteArray("hella"), "warld"); |
80 std::set<std::string> results; | 78 std::set<std::string> results; |
81 trie.FindDataForKeyPrefix("hello", &results); | 79 trie.FindDataForKeyPrefix(ToByteArray("hello"), &results); |
82 std::set<std::string> expected; | 80 std::set<std::string> expected; |
83 expected.insert("world"); | 81 expected.insert("world"); |
84 expected.insert("woorld"); | 82 expected.insert("woorld"); |
85 EXPECT_EQ(expected, results); | 83 EXPECT_EQ(expected, results); |
86 } | 84 } |
87 | 85 |
88 TEST(TrieTest, AllowMutlipleKeys) { | 86 TEST(TrieTest, AllowMutlipleKeys) { |
89 Trie<std::string> trie; | 87 Trie<std::string> trie; |
90 trie.AddDataForKey("hello", "world"); | 88 trie.AddDataForKey(ToByteArray("hello"), "world"); |
91 trie.AddDataForKey("hello", "woorld"); | 89 trie.AddDataForKey(ToByteArray("hello"), "woorld"); |
92 std::set<std::string> results; | 90 std::set<std::string> results; |
93 trie.FindDataForKeyPrefix("hello", &results); | 91 trie.FindDataForKeyPrefix(ToByteArray("hello"), &results); |
94 std::set<std::string> expected; | 92 std::set<std::string> expected; |
95 expected.insert("world"); | 93 expected.insert("world"); |
96 expected.insert("woorld"); | 94 expected.insert("woorld"); |
97 EXPECT_EQ(expected, results); | 95 EXPECT_EQ(expected, results); |
98 } | 96 } |
99 | 97 |
100 TEST(TrieTest, CanFindVeryLongKey) { | 98 TEST(TrieTest, CanFindVeryLongKey) { |
101 Trie<std::string> trie; | 99 Trie<std::string> trie; |
102 static const char kVeryLongKey[] = "1234567890qwertyuioasdfghj"; | 100 static const char kVeryLongKey[] = "1234567890qwertyuioasdfghj"; |
103 trie.AddDataForKey(kVeryLongKey, "world"); | 101 trie.AddDataForKey(ToByteArray(kVeryLongKey), "world"); |
104 std::set<std::string> result; | 102 std::set<std::string> result; |
105 trie.FindDataForKeyPrefix(kVeryLongKey, &result); | 103 trie.FindDataForKeyPrefix(ToByteArray(kVeryLongKey), &result); |
106 std::set<std::string> expected; | 104 std::set<std::string> expected; |
107 expected.insert("world"); | 105 expected.insert("world"); |
108 EXPECT_EQ(expected, result); | 106 EXPECT_EQ(expected, result); |
109 } | 107 } |
110 | 108 |
111 } // namespace | 109 } // namespace autofill |
112 | |
113 } // namespace addressinput | |
114 } // namespace i18n | |
OLD | NEW |