blob: 546080deb904d4d1dc9a8883f15d9a3174f85924 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/transport/stream_op.h"
35
36#include <string.h>
37
38#include <grpc/support/log.h>
39#include "test/core/util/test_config.h"
40
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041static void assert_slices_equal(gpr_slice a, gpr_slice b) {
42 GPR_ASSERT(a.refcount == b.refcount);
43 if (a.refcount) {
44 GPR_ASSERT(a.data.refcounted.bytes == b.data.refcounted.bytes);
45 GPR_ASSERT(a.data.refcounted.length == b.data.refcounted.length);
46 } else {
47 GPR_ASSERT(a.data.inlined.length == b.data.inlined.length);
48 GPR_ASSERT(0 == memcmp(a.data.inlined.bytes, b.data.inlined.bytes,
49 a.data.inlined.length));
50 }
51}
52
53int main(int argc, char **argv) {
54 /* some basic test data */
55 gpr_slice test_slice_1 = gpr_slice_malloc(1);
56 gpr_slice test_slice_2 = gpr_slice_malloc(2);
57 gpr_slice test_slice_3 = gpr_slice_malloc(3);
58 gpr_slice test_slice_4 = gpr_slice_malloc(4);
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +010059 unsigned i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060
61 grpc_stream_op_buffer buf;
62 grpc_stream_op_buffer buf2;
63
64 grpc_test_init(argc, argv);
65 /* initialize one of our buffers */
66 grpc_sopb_init(&buf);
67 /* it should start out empty */
68 GPR_ASSERT(buf.nops == 0);
69
70 /* add some data to the buffer */
71 grpc_sopb_add_begin_message(&buf, 1, 2);
72 grpc_sopb_add_slice(&buf, test_slice_1);
73 grpc_sopb_add_slice(&buf, test_slice_2);
74 grpc_sopb_add_slice(&buf, test_slice_3);
75 grpc_sopb_add_slice(&buf, test_slice_4);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076 grpc_sopb_add_no_op(&buf);
77
78 /* verify that the data went in ok */
Craig Tillerf87b6092015-04-24 10:13:05 -070079 GPR_ASSERT(buf.nops == 6);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080 GPR_ASSERT(buf.ops[0].type == GRPC_OP_BEGIN_MESSAGE);
81 GPR_ASSERT(buf.ops[0].data.begin_message.length == 1);
82 GPR_ASSERT(buf.ops[0].data.begin_message.flags == 2);
83 GPR_ASSERT(buf.ops[1].type == GRPC_OP_SLICE);
84 assert_slices_equal(buf.ops[1].data.slice, test_slice_1);
85 GPR_ASSERT(buf.ops[2].type == GRPC_OP_SLICE);
86 assert_slices_equal(buf.ops[2].data.slice, test_slice_2);
87 GPR_ASSERT(buf.ops[3].type == GRPC_OP_SLICE);
88 assert_slices_equal(buf.ops[3].data.slice, test_slice_3);
89 GPR_ASSERT(buf.ops[4].type == GRPC_OP_SLICE);
90 assert_slices_equal(buf.ops[4].data.slice, test_slice_4);
Craig Tiller6e84aba2015-04-23 15:08:17 -070091 GPR_ASSERT(buf.ops[5].type == GRPC_NO_OP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092
93 /* initialize the second buffer */
94 grpc_sopb_init(&buf2);
95 /* add a no-op, and then the original buffer */
96 grpc_sopb_add_no_op(&buf2);
97 grpc_sopb_append(&buf2, buf.ops, buf.nops);
98 /* should be one element bigger than the original */
99 GPR_ASSERT(buf2.nops == buf.nops + 1);
100 GPR_ASSERT(buf2.ops[0].type == GRPC_NO_OP);
101 /* and the tail should be the same */
102 for (i = 0; i < buf.nops; i++) {
103 GPR_ASSERT(buf2.ops[i + 1].type == buf.ops[i].type);
104 }
105
106 /* destroy the buffers */
107 grpc_sopb_destroy(&buf);
108 grpc_sopb_destroy(&buf2);
109
110 gpr_slice_unref(test_slice_1);
111 gpr_slice_unref(test_slice_2);
112 gpr_slice_unref(test_slice_3);
113 gpr_slice_unref(test_slice_4);
114
115 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800116}