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 <gtest/gtest.h>
21 #include <grpc++/support/slice.h>
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
24 #include <grpcpp/impl/grpc_library.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);
100 auto* t = static_cast<stest*>(p);
105 CheckSlice(spp, kContent);
108 TEST_F(SliceTest, SliceNewLen) {
109 Slice spp(const_cast<char*>(kContent), strlen(kContent),
110 [](void* /*p*/, size_t l) { EXPECT_EQ(l, strlen(kContent)); });
111 CheckSlice(spp, kContent);
114 TEST_F(SliceTest, Steal) {
115 grpc_slice s = grpc_slice_from_copied_string(kContent);
116 Slice spp(s, Slice::STEAL_REF);
117 CheckSlice(spp, kContent);
120 TEST_F(SliceTest, Add) {
121 grpc_slice s = grpc_slice_from_copied_string(kContent);
122 Slice spp(s, Slice::ADD_REF);
124 CheckSlice(spp, kContent);
127 TEST_F(SliceTest, Sub) {
128 Slice spp("0123456789");
129 Slice sub = spp.sub(1, 9);
130 CheckSlice(sub, "12345678");
133 TEST_F(SliceTest, Cslice) {
134 grpc_slice s = grpc_slice_from_copied_string(kContent);
135 Slice spp(s, Slice::STEAL_REF);
136 CheckSlice(spp, kContent);
137 grpc_slice c_slice = spp.c_slice();
138 EXPECT_EQ(GRPC_SLICE_START_PTR(s), GRPC_SLICE_START_PTR(c_slice));
139 EXPECT_EQ(GRPC_SLICE_END_PTR(s), GRPC_SLICE_END_PTR(c_slice));
140 grpc_slice_unref(c_slice);
146 int main(int argc, char** argv) {
147 grpc::testing::TestEnvironment env(argc, argv);
148 ::testing::InitGoogleTest(&argc, argv);
149 int ret = RUN_ALL_TESTS();