aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/slice
diff options
context:
space:
mode:
authorGravatar David Garcia Quintas <dgq@google.com>2018-02-28 16:06:30 -0800
committerGravatar David Garcia Quintas <dgq@google.com>2018-02-28 16:06:30 -0800
commit396d49d95842b42ee2537f54a12919aff47a0401 (patch)
tree898c75ea8c5ac0e997258a768f84d44d35cff8d2 /test/core/slice
parentbaf1a412624d20d1c11f60ddd66d47e58b3e3f35 (diff)
Introduce a 'weak' and mutable version of slice hash table
Diffstat (limited to 'test/core/slice')
-rw-r--r--test/core/slice/BUILD40
-rw-r--r--test/core/slice/slice_weak_hash_table_test.cc105
2 files changed, 140 insertions, 5 deletions
diff --git a/test/core/slice/BUILD b/test/core/slice/BUILD
index 226aed1db9..9a1a506a43 100644
--- a/test/core/slice/BUILD
+++ b/test/core/slice/BUILD
@@ -23,8 +23,8 @@ load("//test/core/util:grpc_fuzzer.bzl", "grpc_fuzzer")
grpc_fuzzer(
name = "percent_encode_fuzzer",
srcs = ["percent_encode_fuzzer.cc"],
- language = "C++",
corpus = "percent_encode_corpus",
+ language = "C++",
deps = [
"//:gpr",
"//:grpc",
@@ -35,8 +35,8 @@ grpc_fuzzer(
grpc_fuzzer(
name = "percent_decode_fuzzer",
srcs = ["percent_decode_fuzzer.cc"],
- language = "C++",
corpus = "percent_decode_corpus",
+ language = "C++",
deps = [
"//:gpr",
"//:grpc",
@@ -59,8 +59,13 @@ grpc_cc_test(
grpc_cc_test(
name = "slice_test",
srcs = ["slice_test.cc"],
- deps = ["//:grpc", "//test/core/util:grpc_test_util", "//:gpr", "//test/core/util:gpr_test_util"],
language = "C++",
+ deps = [
+ "//:gpr",
+ "//:grpc",
+ "//test/core/util:gpr_test_util",
+ "//test/core/util:grpc_test_util",
+ ],
)
grpc_cc_test(
@@ -78,18 +83,43 @@ grpc_cc_test(
grpc_cc_test(
name = "slice_buffer_test",
srcs = ["slice_buffer_test.cc"],
- deps = ["//:grpc", "//test/core/util:grpc_test_util", "//:gpr", "//test/core/util:gpr_test_util"],
language = "C++",
+ deps = [
+ "//:gpr",
+ "//:grpc",
+ "//test/core/util:gpr_test_util",
+ "//test/core/util:grpc_test_util",
+ ],
)
grpc_cc_test(
name = "slice_hash_table_test",
srcs = ["slice_hash_table_test.cc"],
- deps = ["//:grpc", "//test/core/util:grpc_test_util", "//:gpr", "//test/core/util:gpr_test_util"],
+ external_deps = [
+ "gtest",
+ ],
language = "C++",
+ deps = [
+ "//:gpr",
+ "//:grpc",
+ "//test/core/util:gpr_test_util",
+ "//test/core/util:grpc_test_util",
+ ],
+)
+
+grpc_cc_test(
+ name = "slice_weak_hash_table_test",
+ srcs = ["slice_weak_hash_table_test.cc"],
external_deps = [
"gtest",
],
+ language = "C++",
+ deps = [
+ "//:gpr",
+ "//:grpc",
+ "//test/core/util:gpr_test_util",
+ "//test/core/util:grpc_test_util",
+ ],
)
grpc_cc_test(
diff --git a/test/core/slice/slice_weak_hash_table_test.cc b/test/core/slice/slice_weak_hash_table_test.cc
new file mode 100644
index 0000000000..4711d2fd26
--- /dev/null
+++ b/test/core/slice/slice_weak_hash_table_test.cc
@@ -0,0 +1,105 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "src/core/lib/slice/slice_weak_hash_table.h"
+
+#include <cstring>
+#include <sstream>
+
+#include <gtest/gtest.h>
+
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+
+#include "src/core/lib/gpr/useful.h"
+#include "src/core/lib/slice/slice_internal.h"
+#include "test/core/util/test_config.h"
+
+namespace grpc_core {
+namespace {
+
+grpc_slice BuildRefCountedKey(const char* key_str) {
+ const size_t key_length = strlen(key_str);
+ grpc_slice key = grpc_slice_malloc_large(key_length);
+ memcpy(GRPC_SLICE_START_PTR(key), key_str, key_length);
+ return key;
+}
+
+TEST(SliceWeakHashTable, Basic) {
+ auto table = SliceWeakHashTable<UniquePtr<char>, 10>::Create();
+ // Single key-value insertion.
+ grpc_slice key = BuildRefCountedKey("key");
+ grpc_slice_ref(key); // Get doesn't own.
+ table->Add(key, UniquePtr<char>(gpr_strdup("value")));
+ ASSERT_NE(table->Get(key), nullptr);
+ ASSERT_STREQ(table->Get(key)->get(), "value");
+ grpc_slice_unref(key);
+ // Unknown key.
+ ASSERT_EQ(table->Get(grpc_slice_from_static_string("unknown_key")), nullptr);
+}
+
+TEST(SliceWeakHashTable, ValueTypeConstructor) {
+ struct Value {
+ Value() : a(123) {}
+ int a;
+ };
+ auto table = SliceWeakHashTable<Value, 1>::Create();
+ grpc_slice key = BuildRefCountedKey("key");
+ grpc_slice_ref(key); // Get doesn't own.
+ table->Add(key, Value());
+ ASSERT_EQ(table->Get(key)->a, 123);
+ grpc_slice_unref(key);
+}
+
+TEST(SliceWeakHashTable, ForceOverload) {
+ constexpr int kTableSize = 10;
+ auto table = SliceWeakHashTable<UniquePtr<char>, kTableSize>::Create();
+ // Insert a multiple of the maximum size table.
+ for (int i = 0; i < kTableSize * 2; ++i) {
+ std::ostringstream oss;
+ oss << "key-" << i;
+ grpc_slice key = BuildRefCountedKey(oss.str().c_str());
+ oss.clear();
+ oss << "value-" << i;
+ table->Add(key, UniquePtr<char>(gpr_strdup(oss.str().c_str())));
+ }
+ // Verify that some will have been replaced.
+ int num_missing = 0;
+ for (int i = 0; i < kTableSize * 2; ++i) {
+ std::ostringstream oss;
+ oss << "key-" << i;
+ grpc_slice key = BuildRefCountedKey(oss.str().c_str());
+ if (table->Get(key) == nullptr) num_missing++;
+ grpc_slice_unref(key);
+ }
+ // At least kTableSize elements will be missing.
+ ASSERT_GE(num_missing, kTableSize);
+}
+
+} // namespace
+} // namespace grpc_core
+
+int main(int argc, char** argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ grpc_test_init(argc, argv);
+ grpc_core::ExecCtx::GlobalInit();
+ int result = RUN_ALL_TESTS();
+ grpc_core::ExecCtx::GlobalShutdown();
+ return result;
+}