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_weak_hash_table.h"
20
21 #include <cstring>
22 #include <sstream>
23
24 #include <gtest/gtest.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29
30 #include "src/core/lib/gpr/useful.h"
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
BuildRefCountedKey(const char * key_str)38 grpc_slice BuildRefCountedKey(const char* key_str) {
39 const size_t key_length = strlen(key_str);
40 grpc_slice key = grpc_slice_malloc_large(key_length);
41 memcpy(GRPC_SLICE_START_PTR(key), key_str, key_length);
42 return key;
43 }
44
TEST(SliceWeakHashTable,Basic)45 TEST(SliceWeakHashTable, Basic) {
46 auto table = SliceWeakHashTable<UniquePtr<char>, 10>::Create();
47 // Single key-value insertion.
48 grpc_slice key = BuildRefCountedKey("key");
49 grpc_slice_ref(key); // Get doesn't own.
50 table->Add(key, UniquePtr<char>(gpr_strdup("value")));
51 ASSERT_NE(table->Get(key), nullptr);
52 ASSERT_STREQ(table->Get(key)->get(), "value");
53 grpc_slice_unref(key);
54 // Unknown key.
55 ASSERT_EQ(table->Get(grpc_slice_from_static_string("unknown_key")), nullptr);
56 }
57
TEST(SliceWeakHashTable,ValueTypeConstructor)58 TEST(SliceWeakHashTable, ValueTypeConstructor) {
59 struct Value {
60 Value() : a(123) {}
61 int a;
62 };
63 auto table = SliceWeakHashTable<Value, 1>::Create();
64 grpc_slice key = BuildRefCountedKey("key");
65 grpc_slice_ref(key); // Get doesn't own.
66 table->Add(key, Value());
67 ASSERT_EQ(table->Get(key)->a, 123);
68 grpc_slice_unref(key);
69 }
70
TEST(SliceWeakHashTable,ForceOverload)71 TEST(SliceWeakHashTable, ForceOverload) {
72 constexpr int kTableSize = 10;
73 auto table = SliceWeakHashTable<UniquePtr<char>, kTableSize>::Create();
74 // Insert a multiple of the maximum size table.
75 for (int i = 0; i < kTableSize * 2; ++i) {
76 std::ostringstream oss;
77 oss << "key-" << i;
78 grpc_slice key = BuildRefCountedKey(oss.str().c_str());
79 oss.clear();
80 oss << "value-" << i;
81 table->Add(key, UniquePtr<char>(gpr_strdup(oss.str().c_str())));
82 }
83 // Verify that some will have been replaced.
84 int num_missing = 0;
85 for (int i = 0; i < kTableSize * 2; ++i) {
86 std::ostringstream oss;
87 oss << "key-" << i;
88 grpc_slice key = BuildRefCountedKey(oss.str().c_str());
89 if (table->Get(key) == nullptr) num_missing++;
90 grpc_slice_unref(key);
91 }
92 // At least kTableSize elements will be missing.
93 ASSERT_GE(num_missing, kTableSize);
94 }
95
96 } // namespace
97 } // namespace grpc_core
98
main(int argc,char ** argv)99 int main(int argc, char** argv) {
100 ::testing::InitGoogleTest(&argc, argv);
101 grpc_test_init(argc, argv);
102 grpc_core::ExecCtx::GlobalInit();
103 int result = RUN_ALL_TESTS();
104 grpc_core::ExecCtx::GlobalShutdown();
105 return result;
106 }
107