cpp/util/slice_test.cc
Go to the documentation of this file.
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 
19 #include <gtest/gtest.h>
20 
21 #include <grpc++/support/slice.h>
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
25 
27 
28 namespace grpc {
29 
31 
32 namespace {
33 
34 const char* kContent = "hello xxxxxxxxxxxxxxxxxxxx world";
35 
36 class SliceTest : public ::testing::Test {
37  protected:
38  static void SetUpTestCase() { grpc_init(); }
39 
40  static void TearDownTestCase() { grpc_shutdown(); }
41 
42  void CheckSliceSize(const Slice& s, const std::string& content) {
43  EXPECT_EQ(content.size(), s.size());
44  }
45  void CheckSlice(const Slice& s, const std::string& content) {
46  EXPECT_EQ(content.size(), s.size());
48  std::string(reinterpret_cast<const char*>(s.begin()), s.size()));
49  }
50 };
51 
52 TEST_F(SliceTest, Empty) {
53  Slice empty_slice;
54  CheckSlice(empty_slice, "");
55 }
56 
57 TEST_F(SliceTest, Sized) {
58  Slice sized_slice(strlen(kContent));
59  CheckSliceSize(sized_slice, kContent);
60 }
61 
62 TEST_F(SliceTest, String) {
63  Slice spp(kContent);
64  CheckSlice(spp, kContent);
65 }
66 
67 TEST_F(SliceTest, Buf) {
68  Slice spp(kContent, strlen(kContent));
69  CheckSlice(spp, kContent);
70 }
71 
72 TEST_F(SliceTest, StaticBuf) {
73  Slice spp(kContent, strlen(kContent), Slice::STATIC_SLICE);
74  CheckSlice(spp, kContent);
75 }
76 
77 TEST_F(SliceTest, SliceNew) {
78  char* x = new char[strlen(kContent) + 1];
79  strcpy(x, kContent);
80  Slice spp(x, strlen(x), [](void* p) { delete[] static_cast<char*>(p); });
81  CheckSlice(spp, kContent);
82 }
83 
84 TEST_F(SliceTest, SliceNewDoNothing) {
85  Slice spp(const_cast<char*>(kContent), strlen(kContent), [](void* /*p*/) {});
86  CheckSlice(spp, kContent);
87 }
88 
89 TEST_F(SliceTest, SliceNewWithUserData) {
90  struct stest {
91  char* x;
92  int y;
93  };
94  auto* t = new stest;
95  t->x = new char[strlen(kContent) + 1];
96  strcpy(t->x, kContent);
97  Slice spp(
98  t->x, strlen(t->x),
99  [](void* p) {
100  auto* t = static_cast<stest*>(p);
101  delete[] t->x;
102  delete t;
103  },
104  t);
105  CheckSlice(spp, kContent);
106 }
107 
108 TEST_F(SliceTest, SliceNewLen) {
109  Slice spp(const_cast<char*>(kContent), strlen(kContent),
110  [](void* /*p*/, size_t l) { EXPECT_EQ(l, strlen(kContent)); });
111  CheckSlice(spp, kContent);
112 }
113 
114 TEST_F(SliceTest, Steal) {
116  Slice spp(s, Slice::STEAL_REF);
117  CheckSlice(spp, kContent);
118 }
119 
120 TEST_F(SliceTest, Add) {
122  Slice spp(s, Slice::ADD_REF);
123  grpc_slice_unref(s);
124  CheckSlice(spp, kContent);
125 }
126 
127 TEST_F(SliceTest, Sub) {
128  Slice spp("0123456789");
129  Slice sub = spp.sub(1, 9);
130  CheckSlice(sub, "12345678");
131 }
132 
133 TEST_F(SliceTest, Cslice) {
135  Slice spp(s, Slice::STEAL_REF);
136  CheckSlice(spp, kContent);
137  grpc_slice c_slice = spp.c_slice();
140  grpc_slice_unref(c_slice);
141 }
142 
143 } // namespace
144 } // namespace grpc
145 
146 int main(int argc, char** argv) {
147  grpc::testing::TestEnvironment env(&argc, argv);
148  ::testing::InitGoogleTest(&argc, argv);
149  int ret = RUN_ALL_TESTS();
150  return ret;
151 }
check_grpcio_tools.content
content
Definition: check_grpcio_tools.py:26
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
grpc::Slice::STATIC_SLICE
@ STATIC_SLICE
Definition: include/grpcpp/impl/codegen/slice.h:67
grpc::Slice::sub
Slice sub(size_t begin, size_t end) const
Returns a substring of the slice as another slice.
Definition: include/grpcpp/impl/codegen/slice.h:117
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
grpc_slice_from_copied_string
GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source)
Definition: slice/slice.cc:177
slice.h
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
grpc::g_gli_initializer
static grpc::internal::GrpcLibraryInitializer g_gli_initializer
Definition: channel_cc.cc:52
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::python::cdescriptor_pool::Add
static PyObject * Add(PyObject *self, PyObject *file_descriptor_proto)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_pool.cc:621
grpc::Slice::ADD_REF
@ ADD_REF
Definition: include/grpcpp/impl/codegen/slice.h:43
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
grpc::Slice::STEAL_REF
@ STEAL_REF
Definition: include/grpcpp/impl/codegen/slice.h:48
grpc::internal::GrpcLibraryInitializer
Instantiating this class ensures the proper initialization of gRPC.
Definition: grpcpp/impl/grpc_library.h:39
grpc.h
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
GRPC_SLICE_END_PTR
#define GRPC_SLICE_END_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:110
Empty
Definition: abseil-cpp/absl/container/internal/compressed_tuple_test.cc:33
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_library.h
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::EXPECT_EQ
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code())
grpc::Slice::c_slice
grpc_slice c_slice() const
Raw C slice. Caller needs to call grpc_slice_unref when done.
Definition: include/grpcpp/impl/codegen/slice.h:123
slice.h
grpc::Slice
Definition: include/grpcpp/impl/codegen/slice.h:36
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
main
int main(int argc, char **argv)
Definition: cpp/util/slice_test.cc:146
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2367


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:19