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