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<grpc_core::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 grpc_core::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 = grpc_core::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 grpc_core::UniquePtr<char> & a,const grpc_core::UniquePtr<char> & b)126 int StringCmp(const grpc_core::UniquePtr<char>& a,
127 const grpc_core::UniquePtr<char>& b) {
128 return strcmp(a.get(), b.get());
129 }
130
PointerCmp(const grpc_core::UniquePtr<char> & a,const grpc_core::UniquePtr<char> & b)131 int PointerCmp(const grpc_core::UniquePtr<char>& a,
132 const grpc_core::UniquePtr<char>& b) {
133 return GPR_ICMP(a.get(), b.get());
134 }
135
TEST(SliceHashTable,CmpEqual)136 TEST(SliceHashTable, CmpEqual) {
137 const std::vector<TestEntry> test_entries_a = {
138 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
139 RefCountedPtr<TestHashTable> table_a =
140 CreateTableFromEntries(test_entries_a, StringCmp);
141 const std::vector<TestEntry> test_entries_b = {
142 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
143 RefCountedPtr<TestHashTable> table_b =
144 CreateTableFromEntries(test_entries_b, StringCmp);
145 // table_a equals itself.
146 EXPECT_EQ(0, TestHashTable::Cmp(*table_a, *table_a));
147 // table_a equals table_b.
148 EXPECT_EQ(0, TestHashTable::Cmp(*table_a, *table_b));
149 }
150
TEST(SliceHashTable,CmpDifferentSizes)151 TEST(SliceHashTable, CmpDifferentSizes) {
152 // table_a has 3 entries, table_b has only 2.
153 const std::vector<TestEntry> test_entries_a = {
154 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
155 RefCountedPtr<TestHashTable> table_a =
156 CreateTableFromEntries(test_entries_a, StringCmp);
157 const std::vector<TestEntry> test_entries_b = {{"key_0", "value_0"},
158 {"key_1", "value_1"}};
159 RefCountedPtr<TestHashTable> table_b =
160 CreateTableFromEntries(test_entries_b, StringCmp);
161 EXPECT_GT(TestHashTable::Cmp(*table_a, *table_b), 0);
162 EXPECT_LT(TestHashTable::Cmp(*table_b, *table_a), 0);
163 }
164
TEST(SliceHashTable,CmpDifferentKey)165 TEST(SliceHashTable, CmpDifferentKey) {
166 // One key doesn't match and is lexicographically "smaller".
167 const std::vector<TestEntry> test_entries_a = {
168 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
169 RefCountedPtr<TestHashTable> table_a =
170 CreateTableFromEntries(test_entries_a, StringCmp);
171 const std::vector<TestEntry> test_entries_b = {
172 {"key_zz", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
173 RefCountedPtr<TestHashTable> table_b =
174 CreateTableFromEntries(test_entries_b, StringCmp);
175 EXPECT_GT(TestHashTable::Cmp(*table_a, *table_b), 0);
176 EXPECT_LT(TestHashTable::Cmp(*table_b, *table_a), 0);
177 }
178
TEST(SliceHashTable,CmpDifferentValue)179 TEST(SliceHashTable, CmpDifferentValue) {
180 // One value doesn't match.
181 const std::vector<TestEntry> test_entries_a = {
182 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
183 RefCountedPtr<TestHashTable> table_a =
184 CreateTableFromEntries(test_entries_a, StringCmp);
185 const std::vector<TestEntry> test_entries_b = {
186 {"key_0", "value_z"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
187 RefCountedPtr<TestHashTable> table_b =
188 CreateTableFromEntries(test_entries_b, StringCmp);
189 EXPECT_LT(TestHashTable::Cmp(*table_a, *table_b), 0);
190 EXPECT_GT(TestHashTable::Cmp(*table_b, *table_a), 0);
191 }
192
TEST(SliceHashTable,CmpDifferentCmpFunctions)193 TEST(SliceHashTable, CmpDifferentCmpFunctions) {
194 // Same values but different "equals" functions.
195 const std::vector<TestEntry> test_entries_a = {
196 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
197 RefCountedPtr<TestHashTable> table_a =
198 CreateTableFromEntries(test_entries_a, StringCmp);
199 const std::vector<TestEntry> test_entries_b = {
200 {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}};
201 RefCountedPtr<TestHashTable> table_b =
202 CreateTableFromEntries(test_entries_b, PointerCmp);
203 EXPECT_NE(TestHashTable::Cmp(*table_a, *table_b), 0);
204 }
205
TEST(SliceHashTable,CmpEmptyKeysDifferentValue)206 TEST(SliceHashTable, CmpEmptyKeysDifferentValue) {
207 // Same (empty) key, different values.
208 const std::vector<TestEntry> test_entries_a = {{"", "value_0"}};
209 RefCountedPtr<TestHashTable> table_a =
210 CreateTableFromEntries(test_entries_a, StringCmp);
211 const std::vector<TestEntry> test_entries_b = {{"", "value_1"}};
212 RefCountedPtr<TestHashTable> table_b =
213 CreateTableFromEntries(test_entries_b, PointerCmp);
214 EXPECT_NE(TestHashTable::Cmp(*table_a, *table_b), 0);
215 }
216
217 } // namespace
218 } // namespace grpc_core
219
main(int argc,char ** argv)220 int main(int argc, char** argv) {
221 ::testing::InitGoogleTest(&argc, argv);
222 grpc::testing::TestEnvironment env(argc, argv);
223 grpc_core::ExecCtx::GlobalInit();
224 int result = RUN_ALL_TESTS();
225 grpc_core::ExecCtx::GlobalShutdown();
226 return result;
227 }
228