tests/test_buffer.cpp

changeset 792
3ca984931e1d
parent 789
9b2f5661bebd
equal deleted inserted replaced
791:945ee9bf2dd1 792:3ca984931e1d
29 #include "cx/buffer.h" 29 #include "cx/buffer.h"
30 30
31 #include <gtest/gtest.h> 31 #include <gtest/gtest.h>
32 #include "util_allocator.h" 32 #include "util_allocator.h"
33 33
34 class BufferFixture : public ::testing::Test {
35 protected:
36 void SetUp() override {
37 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
38 buf.size = 6;
39 buf.pos = 3;
40 }
41
42 void TearDown() override {
43 cxBufferDestroy(&buf);
44 }
45
46 CxBuffer buf{};
47 };
48
49 class BufferShiftFixture : public ::testing::Test {
50 protected:
51 void SetUp() override {
52 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
53 cxBufferInit(&buf, NULL, 16, &alloc, CX_BUFFER_DEFAULT);
54 memcpy(buf.space, "test____________", 16);
55 buf.capacity = 8; // purposely pretend that the buffer has less capacity s.t. we can test beyond the range
56 buf.pos = 4;
57 buf.size = 4;
58 }
59
60 void TearDown() override {
61 cxBufferDestroy(&buf);
62 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
63 }
64
65 CxTestingAllocator alloc;
66 CxBuffer buf{};
67 };
68
69 class BufferShiftLeft : public BufferShiftFixture {
70 };
71
72 TEST_F(BufferShiftLeft, Zero) {
73 CX_TEST_ASSERT(buf.pos == 4);
74 CX_TEST_ASSERT(buf.size == 4);
75 int ret = cxBufferShiftLeft(&buf, 0);
76 CX_TEST_ASSERT(ret == 0);
77 CX_TEST_ASSERT(buf.pos == 4);
78 CX_TEST_ASSERT(buf.size == 4);
79 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
80 }
81
82 TEST_F(BufferShiftLeft, ZeroOffsetInterface) {
83 CX_TEST_ASSERT(buf.pos == 4);
84 CX_TEST_ASSERT(buf.size == 4);
85 int ret = cxBufferShift(&buf, -0);
86 CX_TEST_ASSERT(ret == 0);
87 CX_TEST_ASSERT(buf.pos == 4);
88 CX_TEST_ASSERT(buf.size == 4);
89 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
90 }
91
92 TEST_F(BufferShiftLeft, Standard) {
93 CX_TEST_ASSERT(buf.pos == 4);
94 CX_TEST_ASSERT(buf.size == 4);
95 int ret = cxBufferShiftLeft(&buf, 2);
96 CX_TEST_ASSERT(ret == 0);
97 CX_TEST_ASSERT(buf.pos == 2);
98 CX_TEST_ASSERT(buf.size == 2);
99 CX_TEST_ASSERT(memcmp(buf.space, "stst________", 8) == 0);
100 }
101
102 TEST_F(BufferShiftLeft, Overshift) {
103 ASSERT_LT(buf.pos, 6);
104 ASSERT_LT(buf.size, 6);
105 int ret = cxBufferShiftLeft(&buf, 6);
106 CX_TEST_ASSERT(ret == 0);
107 CX_TEST_ASSERT(buf.pos == 0);
108 CX_TEST_ASSERT(buf.size == 0);
109 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
110 }
111
112 TEST_F(BufferShiftLeft, OvershiftPosOnly) {
113 buf.pos = 2;
114 CX_TEST_ASSERT(buf.size == 4);
115 int ret = cxBufferShiftLeft(&buf, 3);
116 CX_TEST_ASSERT(ret == 0);
117 CX_TEST_ASSERT(buf.pos == 0);
118 CX_TEST_ASSERT(buf.size == 1);
119 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
120 }
121
122 TEST_F(BufferShiftLeft, OffsetInterface) {
123 buf.pos = 3;
124 CX_TEST_ASSERT(buf.size == 4);
125 int ret = cxBufferShift(&buf, -2);
126 CX_TEST_ASSERT(ret == 0);
127 CX_TEST_ASSERT(buf.pos == 1);
128 CX_TEST_ASSERT(buf.size == 2);
129 CX_TEST_ASSERT(memcmp(buf.space, "stst________", 8) == 0);
130 }
131
132 TEST_F(BufferShiftRight, Zero) {
133 CX_TEST_ASSERT(buf.pos == 4);
134 CX_TEST_ASSERT(buf.size == 4);
135 int ret = cxBufferShiftRight(&buf, 0);
136 CX_TEST_ASSERT(ret == 0);
137 CX_TEST_ASSERT(buf.pos == 4);
138 CX_TEST_ASSERT(buf.size == 4);
139 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
140 }
141
142 TEST_F(BufferShiftRight, ZeroOffsetInterface) {
143 CX_TEST_ASSERT(buf.pos == 4);
144 CX_TEST_ASSERT(buf.size == 4);
145 int ret = cxBufferShift(&buf, +0);
146 CX_TEST_ASSERT(ret == 0);
147 CX_TEST_ASSERT(buf.pos == 4);
148 CX_TEST_ASSERT(buf.size == 4);
149 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
150 }
151
152 TEST_F(BufferShiftRight, Standard) {
153 CX_TEST_ASSERT(buf.pos == 4);
154 CX_TEST_ASSERT(buf.size == 4);
155 int ret = cxBufferShiftRight(&buf, 3);
156 CX_TEST_ASSERT(ret == 0);
157 CX_TEST_ASSERT(buf.pos == 7);
158 CX_TEST_ASSERT(buf.size == 7);
159 CX_TEST_ASSERT(memcmp(buf.space, "testest_____", 8) == 0);
160 }
161
162 TEST_F(BufferShiftRight, OvershiftDiscard) {
163 CX_TEST_ASSERT(buf.pos == 4);
164 CX_TEST_ASSERT(buf.size == 4);
165 CX_TEST_ASSERT(buf.capacity == 8);
166 int ret = cxBufferShiftRight(&buf, 6);
167 CX_TEST_ASSERT(ret == 0);
168 CX_TEST_ASSERT(buf.pos == 8);
169 CX_TEST_ASSERT(buf.size == 8);
170 CX_TEST_ASSERT(buf.capacity == 8);
171 CX_TEST_ASSERT(memcmp(buf.space, "test__te____", 8) == 0);
172 }
173
174 TEST_F(BufferShiftRight, OvershiftExtend) {
175 CX_TEST_ASSERT(buf.pos == 4);
176 CX_TEST_ASSERT(buf.size == 4);
177 CX_TEST_ASSERT(buf.capacity == 8);
178 buf.flags |= CX_BUFFER_AUTO_EXTEND;
179 int ret = cxBufferShiftRight(&buf, 6);
180 CX_TEST_ASSERT(ret == 0);
181 CX_TEST_ASSERT(buf.pos == 10);
182 CX_TEST_ASSERT(buf.size == 10);
183 EXPECT_GE(buf.capacity, 10);
184 CX_TEST_ASSERT(memcmp(buf.space, "test__test__", 8) == 0);
185 }
186
187 TEST_F(BufferShiftRight, OffsetInterface) {
188 buf.pos = 3;
189 CX_TEST_ASSERT(buf.size == 4);
190 int ret = cxBufferShift(&buf, 2);
191 CX_TEST_ASSERT(ret == 0);
192 CX_TEST_ASSERT(buf.pos == 5);
193 CX_TEST_ASSERT(buf.size == 6);
194 CX_TEST_ASSERT(memcmp(buf.space, "tetest______", 8) == 0);
195 }
196
197 class BufferWrite : public ::testing::Test { 34 class BufferWrite : public ::testing::Test {
198 protected: 35 protected:
199 CxBuffer buf{}, target{}; 36 CxBuffer buf{}, target{};
200 37
201 void SetUp() override { 38 void SetUp() override {
505 CX_TEST_ASSERT(target.size == 13); 342 CX_TEST_ASSERT(target.size == 13);
506 CX_TEST_ASSERT(target.capacity == 16); 343 CX_TEST_ASSERT(target.capacity == 16);
507 EXPECT_EQ(memcmp(target.space, "prepfoohello,", 13), 0); 344 EXPECT_EQ(memcmp(target.space, "prepfoohello,", 13), 0);
508 } 345 }
509 346
510
511 TEST_F(BufferEof, Reached) {
512 buf.pos = buf.size;
513 CX_TEST_ASSERT(cxBufferEof(&buf));
514 buf.pos = buf.size - 1;
515 CX_TEST_ASSERT(!cxBufferEof(&buf));
516 cxBufferPut(&buf, 'a');
517 CX_TEST_ASSERT(cxBufferEof(&buf));
518 }
519
520 TEST_F(BufferEof, NotReached) {
521 buf.pos = buf.size - 1;
522 CX_TEST_ASSERT(!cxBufferEof(&buf));
523 buf.pos = 0;
524 cxBufferWrite("test", 1, 5, &buf);
525 CX_TEST_ASSERT(!cxBufferEof(&buf));
526 }
527
528 class BufferRead : public ::testing::Test { 347 class BufferRead : public ::testing::Test {
529 protected: 348 protected:
530 CxBuffer buf{}; 349 CxBuffer buf{};
531 350
532 void SetUp() override { 351 void SetUp() override {

mercurial