c_slice_buffer_test.cc
Go to the documentation of this file.
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/grpc.h>
20 #include <grpc/slice_buffer.h>
21 #include <grpc/support/log.h>
22 
25 
26 static constexpr size_t kTotalDataLength = 4096;
27 
32  size_t i;
33 
35  for (i = 0; i < 10; i++) {
36  grpc_slice_ref(aaa);
37  grpc_slice_ref(bb);
40  }
41  GPR_ASSERT(buf.count > 0);
42  GPR_ASSERT(buf.length == 50);
44  GPR_ASSERT(buf.count == 0);
45  GPR_ASSERT(buf.length == 0);
46  for (i = 0; i < 10; i++) {
47  grpc_slice_ref(aaa);
48  grpc_slice_ref(bb);
51  }
52  GPR_ASSERT(buf.count > 0);
53  GPR_ASSERT(buf.length == 50);
54  for (i = 0; i < 10; i++) {
56  grpc_slice_unref(aaa);
57  grpc_slice_unref(bb);
58  }
59  GPR_ASSERT(buf.count == 0);
60  GPR_ASSERT(buf.length == 0);
62 }
63 
64 static void free_data(void* data, size_t len) {
66  gpr_free(data);
67 }
68 
72  char* data = reinterpret_cast<char*>(gpr_malloc(kTotalDataLength));
73  GPR_ASSERT(data != nullptr);
79  GPR_ASSERT(buf.count == 1);
80  GPR_ASSERT(buf.length == kTotalDataLength / 4);
82  GPR_ASSERT(buf.count == 1);
83  GPR_ASSERT(buf.length == kTotalDataLength / 2);
85  GPR_ASSERT(buf.count == 1);
86  GPR_ASSERT(buf.length == 3 * kTotalDataLength / 4);
88  GPR_ASSERT(buf.count == 1);
89  GPR_ASSERT(buf.length == kTotalDataLength);
91 }
92 
94  grpc_slice slices[3];
97  int idx = 0;
98  size_t src_len = 0;
99  size_t dst_len = 0;
100 
104 
107  for (idx = 0; idx < 3; idx++) {
109  /* For this test, it is important that we add each slice at a new
110  slice index */
113  }
114 
115  /* Case 1: Move more than the first slice's length from src to dst */
116  src_len = src.length;
117  dst_len = dst.length;
119  src_len -= 4;
120  dst_len += 4;
121  GPR_ASSERT(src.length == src_len);
122  GPR_ASSERT(dst.length == dst_len);
123 
124  /* src now has two slices ["bbb"] and ["ccc"] */
125  /* Case 2: Move the first slice from src to dst */
127  src_len -= 3;
128  dst_len += 3;
129  GPR_ASSERT(src.length == src_len);
130  GPR_ASSERT(dst.length == dst_len);
131 
132  /* src now has one slice ["ccc"] */
133  /* Case 3: Move less than the first slice's length from src to dst*/
135  src_len -= 2;
136  dst_len += 2;
137  GPR_ASSERT(src.length == src_len);
138  GPR_ASSERT(dst.length == dst_len);
139 }
140 
142  grpc_slice slices[3];
146 
149  for (int idx = 0; idx < 3; ++idx) {
152  }
153 
155  GPR_ASSERT(GPR_SLICE_LENGTH(*first) == GPR_SLICE_LENGTH(slices[0]));
156  GPR_ASSERT(buf.count == 3);
157  GPR_ASSERT(buf.length == 12);
158 
161  GPR_ASSERT(GPR_SLICE_LENGTH(*first) == 1);
162  GPR_ASSERT(buf.count == 3);
163  GPR_ASSERT(buf.length == 10);
164 
167  GPR_ASSERT(GPR_SLICE_LENGTH(*first) == GPR_SLICE_LENGTH(slices[1]));
168  GPR_ASSERT(buf.count == 2);
169  GPR_ASSERT(buf.length == 9);
170 
173  GPR_ASSERT(GPR_SLICE_LENGTH(*first) == GPR_SLICE_LENGTH(slices[2]));
174  GPR_ASSERT(buf.count == 1);
175  GPR_ASSERT(buf.length == 5);
176 
178  GPR_ASSERT(buf.count == 0);
179  GPR_ASSERT(buf.length == 0);
180 }
181 
182 int main(int argc, char** argv) {
183  grpc::testing::TestEnvironment env(&argc, argv);
184  grpc_init();
185 
190 
191  grpc_shutdown();
192  return 0;
193 }
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
grpc_slice_buffer_destroy
GPRAPI void grpc_slice_buffer_destroy(grpc_slice_buffer *sb)
Definition: slice_buffer_api.cc:27
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
test_slice_buffer_first
void test_slice_buffer_first()
Definition: c_slice_buffer_test.cc:141
log.h
generate.env
env
Definition: generate.py:37
grpc_slice_from_copied_string
GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source)
Definition: slice/slice.cc:177
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
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
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc_slice_buffer_pop
GPRAPI void grpc_slice_buffer_pop(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:231
main
int main(int argc, char **argv)
Definition: c_slice_buffer_test.cc:182
test_slice_buffer_move_first
void test_slice_buffer_move_first()
Definition: c_slice_buffer_test.cc:93
test_slice_buffer_add
void test_slice_buffer_add()
Definition: c_slice_buffer_test.cc:28
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_slice_buffer_reset_and_unref
GPRAPI void grpc_slice_buffer_reset_and_unref(grpc_slice_buffer *sb)
Definition: slice_buffer_api.cc:32
grpc.h
grpc_slice_buffer::length
size_t length
Definition: include/grpc/impl/codegen/slice.h:96
slice_buffer.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
slice_internal.h
grpc_slice_buffer_peek_first
grpc_slice * grpc_slice_buffer_peek_first(grpc_slice_buffer *sb)
Definition: slice_internal.h:45
grpc_slice_buffer_init
GPRAPI void grpc_slice_buffer_init(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:116
setup.idx
idx
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:197
test_config.h
grpc_slice_buffer_add
GPRAPI void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:170
kTotalDataLength
static constexpr size_t kTotalDataLength
Definition: c_slice_buffer_test.cc:26
first
StrT first
Definition: cxa_demangle.cpp:4884
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
slices
SliceBuffer * slices
Definition: retry_filter.cc:631
grpc_slice_split_head
GPRAPI grpc_slice grpc_slice_split_head(grpc_slice *s, size_t split)
Definition: slice/slice.cc:347
grpc_slice_buffer_move_first
GPRAPI void grpc_slice_buffer_move_first(grpc_slice_buffer *src, size_t n, grpc_slice_buffer *dst)
Definition: slice/slice_buffer.cc:348
test_slice_buffer_add_contiguous_slices
void test_slice_buffer_add_contiguous_slices()
Definition: c_slice_buffer_test.cc:69
free_data
static void free_data(void *data, size_t len)
Definition: c_slice_buffer_test.cc:64
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_slice_buffer_remove_first
void grpc_slice_buffer_remove_first(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:449
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_slice_buffer_sub_first
void grpc_slice_buffer_sub_first(grpc_slice_buffer *sb, size_t begin, size_t end)
Definition: slice/slice_buffer.cc:459
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_slice_new_with_len
GPRAPI grpc_slice grpc_slice_new_with_len(void *p, size_t len, void(*destroy)(void *, size_t))
Definition: slice/slice.cc:161
grpc_slice_buffer_add_indexed
GPRAPI size_t grpc_slice_buffer_add_indexed(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:161
grpc_slice_ref
GPRAPI grpc_slice grpc_slice_ref(grpc_slice s)
Definition: slice_api.cc:27


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:42