slice_test.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. *
  3. * Copyright 2015 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. #include <gtest/gtest.h>
  19. #include <grpc++/support/slice.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/slice.h>
  22. #include <grpcpp/impl/grpc_library.h>
  23. #include "test/core/util/test_config.h"
  24. namespace grpc {
  25. static internal::GrpcLibraryInitializer g_gli_initializer;
  26. namespace {
  27. const char* kContent = "hello xxxxxxxxxxxxxxxxxxxx world";
  28. class SliceTest : public ::testing::Test {
  29. protected:
  30. static void SetUpTestCase() { grpc_init(); }
  31. static void TearDownTestCase() { grpc_shutdown(); }
  32. void CheckSliceSize(const Slice& s, const std::string& content) {
  33. EXPECT_EQ(content.size(), s.size());
  34. }
  35. void CheckSlice(const Slice& s, const std::string& content) {
  36. EXPECT_EQ(content.size(), s.size());
  37. EXPECT_EQ(content,
  38. std::string(reinterpret_cast<const char*>(s.begin()), s.size()));
  39. }
  40. };
  41. TEST_F(SliceTest, Empty) {
  42. Slice empty_slice;
  43. CheckSlice(empty_slice, "");
  44. }
  45. TEST_F(SliceTest, Sized) {
  46. Slice sized_slice(strlen(kContent));
  47. CheckSliceSize(sized_slice, kContent);
  48. }
  49. TEST_F(SliceTest, String) {
  50. Slice spp(kContent);
  51. CheckSlice(spp, kContent);
  52. }
  53. TEST_F(SliceTest, Buf) {
  54. Slice spp(kContent, strlen(kContent));
  55. CheckSlice(spp, kContent);
  56. }
  57. TEST_F(SliceTest, StaticBuf) {
  58. Slice spp(kContent, strlen(kContent), Slice::STATIC_SLICE);
  59. CheckSlice(spp, kContent);
  60. }
  61. TEST_F(SliceTest, SliceNew) {
  62. char* x = new char[strlen(kContent) + 1];
  63. strcpy(x, kContent);
  64. Slice spp(x, strlen(x), [](void* p) { delete[] static_cast<char*>(p); });
  65. CheckSlice(spp, kContent);
  66. }
  67. TEST_F(SliceTest, SliceNewDoNothing) {
  68. Slice spp(const_cast<char*>(kContent), strlen(kContent), [](void* /*p*/) {});
  69. CheckSlice(spp, kContent);
  70. }
  71. TEST_F(SliceTest, SliceNewWithUserData) {
  72. struct stest {
  73. char* x;
  74. int y;
  75. };
  76. auto* t = new stest;
  77. t->x = new char[strlen(kContent) + 1];
  78. strcpy(t->x, kContent);
  79. Slice spp(
  80. t->x, strlen(t->x),
  81. [](void* p) {
  82. auto* t = static_cast<stest*>(p);
  83. delete[] t->x;
  84. delete t;
  85. },
  86. t);
  87. CheckSlice(spp, kContent);
  88. }
  89. TEST_F(SliceTest, SliceNewLen) {
  90. Slice spp(const_cast<char*>(kContent), strlen(kContent),
  91. [](void* /*p*/, size_t l) { EXPECT_EQ(l, strlen(kContent)); });
  92. CheckSlice(spp, kContent);
  93. }
  94. TEST_F(SliceTest, Steal) {
  95. grpc_slice s = grpc_slice_from_copied_string(kContent);
  96. Slice spp(s, Slice::STEAL_REF);
  97. CheckSlice(spp, kContent);
  98. }
  99. TEST_F(SliceTest, Add) {
  100. grpc_slice s = grpc_slice_from_copied_string(kContent);
  101. Slice spp(s, Slice::ADD_REF);
  102. grpc_slice_unref(s);
  103. CheckSlice(spp, kContent);
  104. }
  105. TEST_F(SliceTest, Sub) {
  106. Slice spp("0123456789");
  107. Slice sub = spp.sub(1, 9);
  108. CheckSlice(sub, "12345678");
  109. }
  110. TEST_F(SliceTest, Cslice) {
  111. grpc_slice s = grpc_slice_from_copied_string(kContent);
  112. Slice spp(s, Slice::STEAL_REF);
  113. CheckSlice(spp, kContent);
  114. grpc_slice c_slice = spp.c_slice();
  115. EXPECT_EQ(GRPC_SLICE_START_PTR(s), GRPC_SLICE_START_PTR(c_slice));
  116. EXPECT_EQ(GRPC_SLICE_END_PTR(s), GRPC_SLICE_END_PTR(c_slice));
  117. grpc_slice_unref(c_slice);
  118. }
  119. } // namespace
  120. } // namespace grpc
  121. int main(int argc, char** argv) {
  122. grpc::testing::TestEnvironment env(argc, argv);
  123. ::testing::InitGoogleTest(&argc, argv);
  124. int ret = RUN_ALL_TESTS();
  125. return ret;
  126. }