• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "test/core/util/test_config.h"
23 
test_slice_buffer_add()24 void test_slice_buffer_add() {
25   grpc_slice_buffer buf;
26   grpc_slice aaa = grpc_slice_from_copied_string("aaa");
27   grpc_slice bb = grpc_slice_from_copied_string("bb");
28   size_t i;
29 
30   grpc_slice_buffer_init(&buf);
31   for (i = 0; i < 10; i++) {
32     grpc_slice_ref(aaa);
33     grpc_slice_ref(bb);
34     grpc_slice_buffer_add(&buf, aaa);
35     grpc_slice_buffer_add(&buf, bb);
36   }
37   GPR_ASSERT(buf.count > 0);
38   GPR_ASSERT(buf.length == 50);
39   grpc_slice_buffer_reset_and_unref(&buf);
40   GPR_ASSERT(buf.count == 0);
41   GPR_ASSERT(buf.length == 0);
42   for (i = 0; i < 10; i++) {
43     grpc_slice_ref(aaa);
44     grpc_slice_ref(bb);
45     grpc_slice_buffer_add(&buf, aaa);
46     grpc_slice_buffer_add(&buf, bb);
47   }
48   GPR_ASSERT(buf.count > 0);
49   GPR_ASSERT(buf.length == 50);
50   for (i = 0; i < 10; i++) {
51     grpc_slice_buffer_pop(&buf);
52     grpc_slice_unref(aaa);
53     grpc_slice_unref(bb);
54   }
55   GPR_ASSERT(buf.count == 0);
56   GPR_ASSERT(buf.length == 0);
57   grpc_slice_buffer_destroy(&buf);
58 }
59 
test_slice_buffer_move_first()60 void test_slice_buffer_move_first() {
61   grpc_slice slices[3];
62   grpc_slice_buffer src;
63   grpc_slice_buffer dst;
64   int idx = 0;
65   size_t src_len = 0;
66   size_t dst_len = 0;
67 
68   slices[0] = grpc_slice_from_copied_string("aaa");
69   slices[1] = grpc_slice_from_copied_string("bbbb");
70   slices[2] = grpc_slice_from_copied_string("ccc");
71 
72   grpc_slice_buffer_init(&src);
73   grpc_slice_buffer_init(&dst);
74   for (idx = 0; idx < 3; idx++) {
75     grpc_slice_ref(slices[idx]);
76     /* For this test, it is important that we add each slice at a new
77        slice index */
78     grpc_slice_buffer_add_indexed(&src, slices[idx]);
79     grpc_slice_buffer_add_indexed(&dst, slices[idx]);
80   }
81 
82   /* Case 1: Move more than the first slice's length from src to dst */
83   src_len = src.length;
84   dst_len = dst.length;
85   grpc_slice_buffer_move_first(&src, 4, &dst);
86   src_len -= 4;
87   dst_len += 4;
88   GPR_ASSERT(src.length == src_len);
89   GPR_ASSERT(dst.length == dst_len);
90 
91   /* src now has two slices ["bbb"] and  ["ccc"] */
92   /* Case 2: Move the first slice from src to dst */
93   grpc_slice_buffer_move_first(&src, 3, &dst);
94   src_len -= 3;
95   dst_len += 3;
96   GPR_ASSERT(src.length == src_len);
97   GPR_ASSERT(dst.length == dst_len);
98 
99   /* src now has one slice ["ccc"] */
100   /* Case 3: Move less than the first slice's length from src to dst*/
101   grpc_slice_buffer_move_first(&src, 2, &dst);
102   src_len -= 2;
103   dst_len += 2;
104   GPR_ASSERT(src.length == src_len);
105   GPR_ASSERT(dst.length == dst_len);
106 }
107 
main(int argc,char ** argv)108 int main(int argc, char** argv) {
109   grpc_test_init(argc, argv);
110   grpc_init();
111 
112   test_slice_buffer_add();
113   test_slice_buffer_move_first();
114 
115   grpc_shutdown();
116   return 0;
117 }
118