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