diff options
author | Muxi Yan <mxyan@google.com> | 2017-02-08 15:27:31 -0800 |
---|---|---|
committer | Muxi Yan <mxyan@google.com> | 2017-02-08 15:27:31 -0800 |
commit | acbc5dd6ece72a1eb1032568a0061ef867b8b800 (patch) | |
tree | d991968fe7c2bf8154bef1b798b89bda68dd5719 /test/core/slice/slice_test.c | |
parent | 40d7c627bde4c08d40568f62e6f284a6a615fb71 (diff) | |
parent | da7b06c2f8d0dd89ab92589c55d5233f329083c5 (diff) |
Merge remote-tracking branch 'upstream/master' into packet-coalescing-objc
Diffstat (limited to 'test/core/slice/slice_test.c')
-rw-r--r-- | test/core/slice/slice_test.c | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/test/core/slice/slice_test.c b/test/core/slice/slice_test.c index ca44becfd0..f68a953261 100644 --- a/test/core/slice/slice_test.c +++ b/test/core/slice/slice_test.c @@ -35,8 +35,12 @@ #include <string.h> +#include <grpc/grpc.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> + +#include "src/core/lib/slice/slice_internal.h" +#include "src/core/lib/transport/static_metadata.h" #include "test/core/util/test_config.h" #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x); @@ -61,8 +65,10 @@ static void test_slice_malloc_returns_something_sensible(void) { GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == length); /* If the slice has a refcount, it must be destroyable. */ if (slice.refcount) { - GPR_ASSERT(slice.refcount->ref != NULL); - GPR_ASSERT(slice.refcount->unref != NULL); + GPR_ASSERT(slice.refcount->vtable != NULL); + GPR_ASSERT(slice.refcount->vtable->ref != NULL); + GPR_ASSERT(slice.refcount->vtable->unref != NULL); + GPR_ASSERT(slice.refcount->vtable->hash != NULL); } /* We must be able to write to every byte of the data */ for (i = 0; i < length; i++) { @@ -249,6 +255,55 @@ static void test_slice_from_copied_string_works(void) { grpc_slice_unref(slice); } +static void test_slice_interning(void) { + LOG_TEST_NAME("test_slice_interning"); + + grpc_init(); + grpc_slice src1 = grpc_slice_from_copied_string("hello123456789123456789"); + grpc_slice src2 = grpc_slice_from_copied_string("hello123456789123456789"); + GPR_ASSERT(GRPC_SLICE_START_PTR(src1) != GRPC_SLICE_START_PTR(src2)); + grpc_slice interned1 = grpc_slice_intern(src1); + grpc_slice interned2 = grpc_slice_intern(src2); + GPR_ASSERT(GRPC_SLICE_START_PTR(interned1) == + GRPC_SLICE_START_PTR(interned2)); + GPR_ASSERT(GRPC_SLICE_START_PTR(interned1) != GRPC_SLICE_START_PTR(src1)); + GPR_ASSERT(GRPC_SLICE_START_PTR(interned2) != GRPC_SLICE_START_PTR(src2)); + grpc_slice_unref(src1); + grpc_slice_unref(src2); + grpc_slice_unref(interned1); + grpc_slice_unref(interned2); + grpc_shutdown(); +} + +static void test_static_slice_interning(void) { + LOG_TEST_NAME("test_static_slice_interning"); + + // grpc_init/grpc_shutdown deliberately omitted: they should not be necessary + // to intern a static slice + + for (size_t i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) { + GPR_ASSERT(grpc_slice_is_equivalent( + grpc_static_slice_table[i], + grpc_slice_intern(grpc_static_slice_table[i]))); + } +} + +static void test_static_slice_copy_interning(void) { + LOG_TEST_NAME("test_static_slice_copy_interning"); + + grpc_init(); + + for (size_t i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) { + grpc_slice copy = grpc_slice_dup(grpc_static_slice_table[i]); + GPR_ASSERT(grpc_static_slice_table[i].refcount != copy.refcount); + GPR_ASSERT(grpc_static_slice_table[i].refcount == + grpc_slice_intern(copy).refcount); + grpc_slice_unref(copy); + } + + grpc_shutdown(); +} + int main(int argc, char **argv) { unsigned length; grpc_test_init(argc, argv); @@ -262,5 +317,8 @@ int main(int argc, char **argv) { test_slice_split_tail_works(length); } test_slice_from_copied_string_works(); + test_slice_interning(); + test_static_slice_interning(); + test_static_slice_copy_interning(); return 0; } |