Imported Upstream version 1.33.1
[platform/upstream/grpc.git] / test / cpp / util / slice_test.cc
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 #include "test/core/util/test_config.h"
27
28 namespace grpc {
29
30 static internal::GrpcLibraryInitializer g_gli_initializer;
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());
47     EXPECT_EQ(content,
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(t->x, strlen(t->x),
98             [](void* p) {
99               auto* t = static_cast<stest*>(p);
100               delete[] t->x;
101               delete t;
102             },
103             t);
104   CheckSlice(spp, kContent);
105 }
106
107 TEST_F(SliceTest, SliceNewLen) {
108   Slice spp(const_cast<char*>(kContent), strlen(kContent),
109             [](void* /*p*/, size_t l) { EXPECT_EQ(l, strlen(kContent)); });
110   CheckSlice(spp, kContent);
111 }
112
113 TEST_F(SliceTest, Steal) {
114   grpc_slice s = grpc_slice_from_copied_string(kContent);
115   Slice spp(s, Slice::STEAL_REF);
116   CheckSlice(spp, kContent);
117 }
118
119 TEST_F(SliceTest, Add) {
120   grpc_slice s = grpc_slice_from_copied_string(kContent);
121   Slice spp(s, Slice::ADD_REF);
122   grpc_slice_unref(s);
123   CheckSlice(spp, kContent);
124 }
125
126 TEST_F(SliceTest, Cslice) {
127   grpc_slice s = grpc_slice_from_copied_string(kContent);
128   Slice spp(s, Slice::STEAL_REF);
129   CheckSlice(spp, kContent);
130   grpc_slice c_slice = spp.c_slice();
131   EXPECT_EQ(GRPC_SLICE_START_PTR(s), GRPC_SLICE_START_PTR(c_slice));
132   EXPECT_EQ(GRPC_SLICE_END_PTR(s), GRPC_SLICE_END_PTR(c_slice));
133   grpc_slice_unref(c_slice);
134 }
135
136 }  // namespace
137 }  // namespace grpc
138
139 int main(int argc, char** argv) {
140   grpc::testing::TestEnvironment env(argc, argv);
141   ::testing::InitGoogleTest(&argc, argv);
142   int ret = RUN_ALL_TESTS();
143   return ret;
144 }