1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/lib/slice/slice_hash_table.h"
20
21 #include <string.h>
22
23 #include <vector>
24
25 #include <gtest/gtest.h>
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/string_util.h>
30
31 #include "src/core/lib/iomgr/exec_ctx.h"
32 #include "src/core/lib/slice/slice_internal.h"
33 #include "test/core/util/test_config.h"
34
35 namespace grpc_core {
36 namespace {
37
38 typedef SliceHashTable<UniquePtr<char>> TestHashTable;
39
40 struct TestEntry {
41 const char* key;
42 const char* value;
43 };
44
CheckValues(const std::vector<TestEntry> & input,const TestHashTable & table)45 void CheckValues(const std::vector<TestEntry>& input,
46 const TestHashTable& table) {
47 for (const TestEntry& expected : input) {
48 grpc_slice key = grpc_slice_from_static_string(expected.key);
49 const UniquePtr<char>* actual = table.Get(key);
50 ASSERT_NE(actual, nullptr);
51 EXPECT_STREQ(expected.value, actual->get());
52 grpc_slice_unref(key);
53 }
54 }
55
CheckNonExistentValue(const char * key_string,const TestHashTable & table)56 void CheckNonExistentValue(const char* key_string, const TestHashTable& table) {
57 grpc_slice key = grpc_slice_from_static_string(key_string);
58 ASSERT_EQ(nullptr, table.Get(key));
59 grpc_slice_unref(key);
60 }
61
PopulateEntries(const std::vector<TestEntry> & input,TestHashTable::Entry * output)62 void PopulateEntries(const std::vector<TestEntry>& input,
63 TestHashTable::Entry* output) {
64 for (size_t i = 0; i < input.size(); ++i) {
65 output[i].key = grpc_slice_from_copied_string(input[i].key);
66 output[i].value = UniquePtr<char>(gpr_strdup(input[i].value));
67 }
68 }
69
CreateTableFromEntries(const std::vector<TestEntry> & test_entries,TestHashTable::ValueCmp value_cmp)70 RefCountedPtr<TestHashTable> CreateTableFromEntries(
71 const std::vector<TestEntry>& test_entries,
72 TestHashTable::ValueCmp value_cmp) {
73 TestHashTable::Entry* entries = static_cast<TestHashTable::Entry*>(
74 gpr_zalloc(sizeof(*entries) * test_entries.size()));
75 PopulateEntries(test_entries, entries);
76 RefCountedPtr<TestHashTable> table =
77 TestHashTable::Create(test_entries.size(), entries, value_cmp);
78 gpr_free(entries);
79 return table;
80 }
81
TEST(SliceHashTable,Basic)82 TEST(SliceHashTable, Basic) {
83 const std::vector<TestEntry> test_entries = {
84 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"},
85 {"key_3", "value_3"}, {"key_4", "value_4"}, {"key_5", "value_5"},
86 {"key_6", "value_6"}, {"key_7", "value_7"}, {"key_8", "value_8"},
87 {"key_9", "value_9"}, {"key_10", "value_10"}, {"key_11", "value_11"},
88 {"key_12", "value_12"}, {"key_13", "value_13"}, {"key_14", "value_14"},
89 {"key_15", "value_15"}, {"key_16", "value_16"}, {"key_17", "value_17"},
90 {"key_18", "value_18"}, {"key_19", "value_19"}, {"key_20", "value_20"},
91 {"key_21", "value_21"}, {"key_22", "value_22"}, {"key_23", "value_23"},
92 {"key_24", "value_24"}, {"key_25", "value_25"}, {"key_26", "value_26"},
93 {"key_27", "value_27"}, {"key_28", "value_28"}, {"key_29", "value_29"},
94 {"key_30", "value_30"}, {"key_31", "value_31"}, {"key_32", "value_32"},
95 {"key_33", "value_33"}, {"key_34", "value_34"}, {"key_35", "value_35"},
96 {"key_36", "value_36"}, {"key_37", "value_37"}, {"key_38", "value_38"},
97 {"key_39", "value_39"}, {"key_40", "value_40"}, {"key_41", "value_41"},
98 {"key_42", "value_42"}, {"key_43", "value_43"}, {"key_44", "value_44"},
99 {"key_45", "value_45"}, {"key_46", "value_46"}, {"key_47", "value_47"},
100 {"key_48", "value_48"}, {"key_49", "value_49"}, {"key_50", "value_50"},
101 {"key_51", "value_51"}, {"key_52", "value_52"}, {"key_53", "value_53"},
102 {"key_54", "value_54"}, {"key_55", "value_55"}, {"key_56", "value_56"},
103 {"key_57", "value_57"}, {"key_58", "value_58"}, {"key_59", "value_59"},
104 {"key_60", "value_60"}, {"key_61", "value_61"}, {"key_62", "value_62"},
105 {"key_63", "value_63"}, {"key_64", "value_64"}, {"key_65", "value_65"},
106 {"key_66", "value_66"}, {"key_67", "value_67"}, {"key_68", "value_68"},
107 {"key_69", "value_69"}, {"key_70", "value_70"}, {"key_71", "value_71"},
108 {"key_72", "value_72"}, {"key_73", "value_73"}, {"key_74", "value_74"},
109 {"key_75", "value_75"}, {"key_76", "value_76"}, {"key_77", "value_77"},
110 {"key_78", "value_78"}, {"key_79", "value_79"}, {"key_80", "value_80"},
111 {"key_81", "value_81"}, {"key_82", "value_82"}, {"key_83", "value_83"},
112 {"key_84", "value_84"}, {"key_85", "value_85"}, {"key_86", "value_86"},
113 {"key_87", "value_87"}, {"key_88", "value_88"}, {"key_89", "value_89"},
114 {"key_90", "value_90"}, {"key_91", "value_91"}, {"key_92", "value_92"},
115 {"key_93", "value_93"}, {"key_94", "value_94"}, {"key_95", "value_95"},
116 {"key_96", "value_96"}, {"key_97", "value_97"}, {"key_98", "value_98"},
117 {"key_99", "value_99"},
118 };
119 RefCountedPtr<TestHashTable> table =
120 CreateTableFromEntries(test_entries, nullptr);
121 // Check contents of table.
122 CheckValues(test_entries, *table);
123 CheckNonExistentValue("XX", *table);
124 }
125
StringCmp(const UniquePtr<char> & a,const UniquePtr<char> & b)126 int StringCmp(const UniquePtr<char>& a, const UniquePtr<char>& b) {
127 return strcmp(a.get(), b.get());
128 }
129
PointerCmp(const UniquePtr<char> & a,const UniquePtr<char> & b)130 int PointerCmp(const UniquePtr<char>& a, const UniquePtr<char>& b) {
131 return GPR_ICMP(a.get(), b.get());
132 }
133
TEST(SliceHashTable,CmpEqual)134 TEST(SliceHashTable, CmpEqual) {
135 const std::vector<TestEntry> test_entries_a = {
136 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
137 RefCountedPtr<TestHashTable> table_a =
138 CreateTableFromEntries(test_entries_a, StringCmp);
139 const std::vector<TestEntry> test_entries_b = {
140 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
141 RefCountedPtr<TestHashTable> table_b =
142 CreateTableFromEntries(test_entries_b, StringCmp);
143 // table_a equals itself.
144 EXPECT_EQ(0, TestHashTable::Cmp(*table_a, *table_a));
145 // table_a equals table_b.
146 EXPECT_EQ(0, TestHashTable::Cmp(*table_a, *table_b));
147 }
148
TEST(SliceHashTable,CmpDifferentSizes)149 TEST(SliceHashTable, CmpDifferentSizes) {
150 // table_a has 3 entries, table_b has only 2.
151 const std::vector<TestEntry> test_entries_a = {
152 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
153 RefCountedPtr<TestHashTable> table_a =
154 CreateTableFromEntries(test_entries_a, StringCmp);
155 const std::vector<TestEntry> test_entries_b = {{"key_0", "value_0"},
156 {"key_1", "value_1"}};
157 RefCountedPtr<TestHashTable> table_b =
158 CreateTableFromEntries(test_entries_b, StringCmp);
159 EXPECT_GT(TestHashTable::Cmp(*table_a, *table_b), 0);
160 EXPECT_LT(TestHashTable::Cmp(*table_b, *table_a), 0);
161 }
162
TEST(SliceHashTable,CmpDifferentKey)163 TEST(SliceHashTable, CmpDifferentKey) {
164 // One key doesn't match and is lexicographically "smaller".
165 const std::vector<TestEntry> test_entries_a = {
166 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
167 RefCountedPtr<TestHashTable> table_a =
168 CreateTableFromEntries(test_entries_a, StringCmp);
169 const std::vector<TestEntry> test_entries_b = {
170 {"key_zz", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
171 RefCountedPtr<TestHashTable> table_b =
172 CreateTableFromEntries(test_entries_b, StringCmp);
173 EXPECT_GT(TestHashTable::Cmp(*table_a, *table_b), 0);
174 EXPECT_LT(TestHashTable::Cmp(*table_b, *table_a), 0);
175 }
176
TEST(SliceHashTable,CmpDifferentValue)177 TEST(SliceHashTable, CmpDifferentValue) {
178 // One value doesn't match.
179 const std::vector<TestEntry> test_entries_a = {
180 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
181 RefCountedPtr<TestHashTable> table_a =
182 CreateTableFromEntries(test_entries_a, StringCmp);
183 const std::vector<TestEntry> test_entries_b = {
184 {"key_0", "value_z"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
185 RefCountedPtr<TestHashTable> table_b =
186 CreateTableFromEntries(test_entries_b, StringCmp);
187 EXPECT_LT(TestHashTable::Cmp(*table_a, *table_b), 0);
188 EXPECT_GT(TestHashTable::Cmp(*table_b, *table_a), 0);
189 }
190
TEST(SliceHashTable,CmpDifferentCmpFunctions)191 TEST(SliceHashTable, CmpDifferentCmpFunctions) {
192 // Same values but different "equals" functions.
193 const std::vector<TestEntry> test_entries_a = {
194 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
195 RefCountedPtr<TestHashTable> table_a =
196 CreateTableFromEntries(test_entries_a, StringCmp);
197 const std::vector<TestEntry> test_entries_b = {
198 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
199 RefCountedPtr<TestHashTable> table_b =
200 CreateTableFromEntries(test_entries_b, PointerCmp);
201 EXPECT_NE(TestHashTable::Cmp(*table_a, *table_b), 0);
202 }
203
TEST(SliceHashTable,CmpEmptyKeysDifferentValue)204 TEST(SliceHashTable, CmpEmptyKeysDifferentValue) {
205 // Same (empty) key, different values.
206 const std::vector<TestEntry> test_entries_a = {{"", "value_0"}};
207 RefCountedPtr<TestHashTable> table_a =
208 CreateTableFromEntries(test_entries_a, StringCmp);
209 const std::vector<TestEntry> test_entries_b = {{"", "value_1"}};
210 RefCountedPtr<TestHashTable> table_b =
211 CreateTableFromEntries(test_entries_b, PointerCmp);
212 EXPECT_NE(TestHashTable::Cmp(*table_a, *table_b), 0);
213 }
214
215 } // namespace
216 } // namespace grpc_core
217
main(int argc,char ** argv)218 int main(int argc, char** argv) {
219 ::testing::InitGoogleTest(&argc, argv);
220 grpc_test_init(argc, argv);
221 grpc_core::ExecCtx::GlobalInit();
222 int result = RUN_ALL_TESTS();
223 grpc_core::ExecCtx::GlobalShutdown();
224 return result;
225 }
226