event_engine/slice_buffer_test.cc
Go to the documentation of this file.
1 // Copyright 2022 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
21 #include <grpc/grpc.h>
22 
25 
26 using ::grpc_event_engine::experimental::Slice;
27 using ::grpc_event_engine::experimental::SliceBuffer;
28 
29 static constexpr int kNewSliceLength = 100;
30 
31 Slice MakeSlice(size_t len) {
32  GPR_ASSERT(len > 0);
33  unsigned char* contents = static_cast<unsigned char*>(gpr_malloc(len));
34  memset(contents, 'a', len);
35  return Slice(grpc_slice_new(contents, len, gpr_free));
36 }
37 
38 TEST(SliceBufferTest, AddAndRemoveTest) {
39  SliceBuffer sb;
40  Slice first_slice = MakeSlice(kNewSliceLength);
41  Slice second_slice = MakeSlice(kNewSliceLength);
42  Slice first_slice_copy = first_slice.Copy();
43  sb.Append(std::move(first_slice));
44  sb.Append(std::move(second_slice));
45  ASSERT_EQ(sb.Count(), 2);
46  ASSERT_EQ(sb.Length(), 2 * kNewSliceLength);
47  Slice popped = sb.TakeFirst();
48  ASSERT_EQ(popped, first_slice_copy);
49  ASSERT_EQ(sb.Count(), 1);
50  ASSERT_EQ(sb.Length(), kNewSliceLength);
51  sb.Prepend(std::move(popped));
52  ASSERT_EQ(sb.Count(), 2);
53  ASSERT_EQ(sb.Length(), 2 * kNewSliceLength);
54  sb.Clear();
55  ASSERT_EQ(sb.Count(), 0);
56  ASSERT_EQ(sb.Length(), 0);
57 }
58 
59 TEST(SliceBufferTest, SliceRefTest) {
60  SliceBuffer sb;
61  Slice first_slice = MakeSlice(kNewSliceLength);
62  Slice second_slice = MakeSlice(kNewSliceLength + 1);
63  Slice first_slice_copy = first_slice.Copy();
64  Slice second_slice_copy = second_slice.Copy();
65  ASSERT_EQ(sb.AppendIndexed(std::move(first_slice)), 0);
66  ASSERT_EQ(sb.AppendIndexed(std::move(second_slice)), 1);
67  Slice first_reffed = sb.RefSlice(0);
68  Slice second_reffed = sb.RefSlice(1);
69  ASSERT_EQ(first_reffed, first_slice_copy);
70  ASSERT_EQ(second_reffed, second_slice_copy);
71  ASSERT_EQ(sb.Count(), 2);
72  ASSERT_EQ(sb.Length(), 2 * kNewSliceLength + 1);
73  sb.Clear();
74 }
75 
76 int main(int argc, char** argv) {
77  testing::InitGoogleTest(&argc, argv);
78  return RUN_ALL_TESTS();
79 }
slice.h
kNewSliceLength
static constexpr int kNewSliceLength
Definition: event_engine/slice_buffer_test.cc:29
memset
return memset(p, 0, total)
grpc_slice_new
GPRAPI grpc_slice grpc_slice_new(void *p, size_t len, void(*destroy)(void *))
Definition: slice/slice.cc:103
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
TEST
TEST(SliceBufferTest, AddAndRemoveTest)
Definition: event_engine/slice_buffer_test.cc:38
MakeSlice
Slice MakeSlice(size_t len)
Definition: event_engine/slice_buffer_test.cc:31
grpc.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
test_config.h
main
int main(int argc, char **argv)
Definition: event_engine/slice_buffer_test.cc:76
contents
string_view contents
Definition: elf.cc:597
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
slice_buffer.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:13