3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc++/support/slice.h>
20 #include <grpcpp/impl/grpc_library.h>
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
24 #include <gtest/gtest.h>
26 #include "test/core/util/test_config.h"
30 static internal::GrpcLibraryInitializer g_gli_initializer;
34 const char* kContent = "hello xxxxxxxxxxxxxxxxxxxx world";
36 class SliceTest : public ::testing::Test {
38 static void SetUpTestCase() { grpc_init(); }
40 static void TearDownTestCase() { grpc_shutdown(); }
42 void CheckSliceSize(const Slice& s, const std::string& content) {
43 EXPECT_EQ(content.size(), s.size());
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()));
52 TEST_F(SliceTest, Empty) {
54 CheckSlice(empty_slice, "");
57 TEST_F(SliceTest, Sized) {
58 Slice sized_slice(strlen(kContent));
59 CheckSliceSize(sized_slice, kContent);
62 TEST_F(SliceTest, String) {
64 CheckSlice(spp, kContent);
67 TEST_F(SliceTest, Buf) {
68 Slice spp(kContent, strlen(kContent));
69 CheckSlice(spp, kContent);
72 TEST_F(SliceTest, StaticBuf) {
73 Slice spp(kContent, strlen(kContent), Slice::STATIC_SLICE);
74 CheckSlice(spp, kContent);
77 TEST_F(SliceTest, SliceNew) {
78 char* x = new char[strlen(kContent) + 1];
80 Slice spp(x, strlen(x), [](void* p) { delete[] static_cast<char*>(p); });
81 CheckSlice(spp, kContent);
84 TEST_F(SliceTest, SliceNewDoNothing) {
85 Slice spp(const_cast<char*>(kContent), strlen(kContent), [](void* /*p*/) {});
86 CheckSlice(spp, kContent);
89 TEST_F(SliceTest, SliceNewWithUserData) {
95 t->x = new char[strlen(kContent) + 1];
96 strcpy(t->x, kContent);
97 Slice spp(t->x, strlen(t->x),
99 auto* t = static_cast<stest*>(p);
104 CheckSlice(spp, kContent);
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);
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);
119 TEST_F(SliceTest, Add) {
120 grpc_slice s = grpc_slice_from_copied_string(kContent);
121 Slice spp(s, Slice::ADD_REF);
123 CheckSlice(spp, kContent);
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);
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();