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