tests/test_buffer.cpp

changeset 789
9b2f5661bebd
parent 761
61d5197d612b
child 792
3ca984931e1d
equal deleted inserted replaced
788:b34ff44e6433 789:9b2f5661bebd
32 #include "util_allocator.h" 32 #include "util_allocator.h"
33 33
34 class BufferFixture : public ::testing::Test { 34 class BufferFixture : public ::testing::Test {
35 protected: 35 protected:
36 void SetUp() override { 36 void SetUp() override {
37 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 37 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
38 buf.size = 6; 38 buf.size = 6;
39 buf.pos = 3; 39 buf.pos = 3;
40 } 40 }
41 41
42 void TearDown() override { 42 void TearDown() override {
43 cxBufferDestroy(&buf); 43 cxBufferDestroy(&buf);
44 } 44 }
45 45
46 CxBuffer buf{}; 46 CxBuffer buf{};
47 }; 47 };
48
49 static void expect_default_flush_config(CxBuffer *buf) {
50 EXPECT_EQ(buf->flush_blkmax, 0);
51 EXPECT_EQ(buf->flush_blksize, 4096);
52 EXPECT_EQ(buf->flush_threshold, SIZE_MAX);
53 EXPECT_EQ(buf->flush_func, nullptr);
54 EXPECT_EQ(buf->flush_target, nullptr);
55 }
56
57 TEST(BufferInit, WrapSpace) {
58 CxTestingAllocator alloc;
59 CxBuffer buf;
60 void *space = cxMalloc(&alloc, 16);
61 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_DEFAULT);
62 expect_default_flush_config(&buf);
63 EXPECT_EQ(buf.space, space);
64 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
65 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0);
66 EXPECT_EQ(buf.pos, 0);
67 EXPECT_EQ(buf.size, 0);
68 EXPECT_EQ(buf.capacity, 16);
69 EXPECT_EQ(buf.allocator, &alloc);
70 cxBufferDestroy(&buf);
71 EXPECT_FALSE(alloc.verify());
72 cxFree(&alloc, space);
73 EXPECT_TRUE(alloc.verify());
74 }
75
76 TEST(BufferInit, WrapSpaceAutoExtend) {
77 CxTestingAllocator alloc;
78 CxBuffer buf;
79 void *space = cxMalloc(&alloc, 16);
80 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_AUTO_EXTEND);
81 expect_default_flush_config(&buf);
82 EXPECT_EQ(buf.space, space);
83 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, CX_BUFFER_AUTO_EXTEND);
84 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0);
85 EXPECT_EQ(buf.pos, 0);
86 EXPECT_EQ(buf.size, 0);
87 EXPECT_EQ(buf.capacity, 16);
88 EXPECT_EQ(buf.allocator, &alloc);
89 cxBufferDestroy(&buf);
90 EXPECT_FALSE(alloc.verify());
91 cxFree(&alloc, space);
92 EXPECT_TRUE(alloc.verify());
93 }
94
95 TEST(BufferInit, WrapSpaceAutoFree) {
96 CxTestingAllocator alloc;
97 CxBuffer buf;
98 void *space = cxMalloc(&alloc, 16);
99 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_FREE_CONTENTS);
100 expect_default_flush_config(&buf);
101 EXPECT_EQ(buf.space, space);
102 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
103 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
104 EXPECT_EQ(buf.pos, 0);
105 EXPECT_EQ(buf.size, 0);
106 EXPECT_EQ(buf.capacity, 16);
107 EXPECT_EQ(buf.allocator, &alloc);
108 EXPECT_FALSE(alloc.verify());
109 cxBufferDestroy(&buf);
110 EXPECT_TRUE(alloc.verify());
111 }
112
113 TEST(BufferInit, FreshSpace) {
114 CxTestingAllocator alloc;
115 CxBuffer buf;
116 cxBufferInit(&buf, nullptr, 8, &alloc, CX_BUFFER_DEFAULT);
117 expect_default_flush_config(&buf);
118 EXPECT_NE(buf.space, nullptr);
119 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
120 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
121 EXPECT_EQ(buf.pos, 0);
122 EXPECT_EQ(buf.size, 0);
123 EXPECT_EQ(buf.capacity, 8);
124 EXPECT_EQ(buf.allocator, &alloc);
125 EXPECT_FALSE(alloc.verify()); // space is still allocated
126 cxBufferDestroy(&buf);
127 EXPECT_TRUE(alloc.verify());
128 }
129
130 TEST(BufferInit, OnHeap) {
131 CxTestingAllocator alloc;
132 CxBuffer *buf;
133 void *space = cxMalloc(&alloc, 16);
134 buf = cxBufferCreate(space, 16, &alloc, CX_BUFFER_FREE_CONTENTS);
135 EXPECT_NE(buf, nullptr);
136 expect_default_flush_config(buf);
137 EXPECT_EQ(buf->space, space);
138 EXPECT_EQ(buf->flags & CX_BUFFER_AUTO_EXTEND, 0);
139 EXPECT_EQ(buf->flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
140 EXPECT_EQ(buf->pos, 0);
141 EXPECT_EQ(buf->size, 0);
142 EXPECT_EQ(buf->capacity, 16);
143 EXPECT_EQ(buf->allocator, &alloc);
144 cxBufferFree(buf);
145 EXPECT_TRUE(alloc.verify());
146 }
147 48
148 class BufferShiftFixture : public ::testing::Test { 49 class BufferShiftFixture : public ::testing::Test {
149 protected: 50 protected:
150 void SetUp() override { 51 void SetUp() override {
151 ASSERT_TRUE(alloc.verify()); 52 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
152 cxBufferInit(&buf, nullptr, 16, &alloc, CX_BUFFER_DEFAULT); 53 cxBufferInit(&buf, NULL, 16, &alloc, CX_BUFFER_DEFAULT);
153 memcpy(buf.space, "test____________", 16); 54 memcpy(buf.space, "test____________", 16);
154 buf.capacity = 8; // purposely pretend that the buffer has less capacity s.t. we can test beyond the range 55 buf.capacity = 8; // purposely pretend that the buffer has less capacity s.t. we can test beyond the range
155 buf.pos = 4; 56 buf.pos = 4;
156 buf.size = 4; 57 buf.size = 4;
157 } 58 }
158 59
159 void TearDown() override { 60 void TearDown() override {
160 cxBufferDestroy(&buf); 61 cxBufferDestroy(&buf);
161 EXPECT_TRUE(alloc.verify()); 62 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
162 } 63 }
163 64
164 CxTestingAllocator alloc; 65 CxTestingAllocator alloc;
165 CxBuffer buf{}; 66 CxBuffer buf{};
166 }; 67 };
167 68
168 class BufferShiftLeft : public BufferShiftFixture { 69 class BufferShiftLeft : public BufferShiftFixture {
169 }; 70 };
170 71
171 TEST_F(BufferShiftLeft, Zero) { 72 TEST_F(BufferShiftLeft, Zero) {
172 ASSERT_EQ(buf.pos, 4); 73 CX_TEST_ASSERT(buf.pos == 4);
173 ASSERT_EQ(buf.size, 4); 74 CX_TEST_ASSERT(buf.size == 4);
174 int ret = cxBufferShiftLeft(&buf, 0); 75 int ret = cxBufferShiftLeft(&buf, 0);
175 EXPECT_EQ(ret, 0); 76 CX_TEST_ASSERT(ret == 0);
176 EXPECT_EQ(buf.pos, 4); 77 CX_TEST_ASSERT(buf.pos == 4);
177 EXPECT_EQ(buf.size, 4); 78 CX_TEST_ASSERT(buf.size == 4);
178 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); 79 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
179 } 80 }
180 81
181 TEST_F(BufferShiftLeft, ZeroOffsetInterface) { 82 TEST_F(BufferShiftLeft, ZeroOffsetInterface) {
182 ASSERT_EQ(buf.pos, 4); 83 CX_TEST_ASSERT(buf.pos == 4);
183 ASSERT_EQ(buf.size, 4); 84 CX_TEST_ASSERT(buf.size == 4);
184 int ret = cxBufferShift(&buf, -0); 85 int ret = cxBufferShift(&buf, -0);
185 EXPECT_EQ(ret, 0); 86 CX_TEST_ASSERT(ret == 0);
186 EXPECT_EQ(buf.pos, 4); 87 CX_TEST_ASSERT(buf.pos == 4);
187 EXPECT_EQ(buf.size, 4); 88 CX_TEST_ASSERT(buf.size == 4);
188 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); 89 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
189 } 90 }
190 91
191 TEST_F(BufferShiftLeft, Standard) { 92 TEST_F(BufferShiftLeft, Standard) {
192 ASSERT_EQ(buf.pos, 4); 93 CX_TEST_ASSERT(buf.pos == 4);
193 ASSERT_EQ(buf.size, 4); 94 CX_TEST_ASSERT(buf.size == 4);
194 int ret = cxBufferShiftLeft(&buf, 2); 95 int ret = cxBufferShiftLeft(&buf, 2);
195 EXPECT_EQ(ret, 0); 96 CX_TEST_ASSERT(ret == 0);
196 EXPECT_EQ(buf.pos, 2); 97 CX_TEST_ASSERT(buf.pos == 2);
197 EXPECT_EQ(buf.size, 2); 98 CX_TEST_ASSERT(buf.size == 2);
198 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0); 99 CX_TEST_ASSERT(memcmp(buf.space, "stst________", 8) == 0);
199 } 100 }
200 101
201 TEST_F(BufferShiftLeft, Overshift) { 102 TEST_F(BufferShiftLeft, Overshift) {
202 ASSERT_LT(buf.pos, 6); 103 ASSERT_LT(buf.pos, 6);
203 ASSERT_LT(buf.size, 6); 104 ASSERT_LT(buf.size, 6);
204 int ret = cxBufferShiftLeft(&buf, 6); 105 int ret = cxBufferShiftLeft(&buf, 6);
205 EXPECT_EQ(ret, 0); 106 CX_TEST_ASSERT(ret == 0);
206 EXPECT_EQ(buf.pos, 0); 107 CX_TEST_ASSERT(buf.pos == 0);
207 EXPECT_EQ(buf.size, 0); 108 CX_TEST_ASSERT(buf.size == 0);
208 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); 109 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
209 } 110 }
210 111
211 TEST_F(BufferShiftLeft, OvershiftPosOnly) { 112 TEST_F(BufferShiftLeft, OvershiftPosOnly) {
212 buf.pos = 2; 113 buf.pos = 2;
213 ASSERT_EQ(buf.size, 4); 114 CX_TEST_ASSERT(buf.size == 4);
214 int ret = cxBufferShiftLeft(&buf, 3); 115 int ret = cxBufferShiftLeft(&buf, 3);
215 EXPECT_EQ(ret, 0); 116 CX_TEST_ASSERT(ret == 0);
216 EXPECT_EQ(buf.pos, 0); 117 CX_TEST_ASSERT(buf.pos == 0);
217 EXPECT_EQ(buf.size, 1); 118 CX_TEST_ASSERT(buf.size == 1);
218 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); 119 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
219 } 120 }
220 121
221 TEST_F(BufferShiftLeft, OffsetInterface) { 122 TEST_F(BufferShiftLeft, OffsetInterface) {
222 buf.pos = 3; 123 buf.pos = 3;
223 ASSERT_EQ(buf.size, 4); 124 CX_TEST_ASSERT(buf.size == 4);
224 int ret = cxBufferShift(&buf, -2); 125 int ret = cxBufferShift(&buf, -2);
225 EXPECT_EQ(ret, 0); 126 CX_TEST_ASSERT(ret == 0);
226 EXPECT_EQ(buf.pos, 1); 127 CX_TEST_ASSERT(buf.pos == 1);
227 EXPECT_EQ(buf.size, 2); 128 CX_TEST_ASSERT(buf.size == 2);
228 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0); 129 CX_TEST_ASSERT(memcmp(buf.space, "stst________", 8) == 0);
229 } 130 }
230
231 class BufferShiftRight : public BufferShiftFixture {
232 };
233 131
234 TEST_F(BufferShiftRight, Zero) { 132 TEST_F(BufferShiftRight, Zero) {
235 ASSERT_EQ(buf.pos, 4); 133 CX_TEST_ASSERT(buf.pos == 4);
236 ASSERT_EQ(buf.size, 4); 134 CX_TEST_ASSERT(buf.size == 4);
237 int ret = cxBufferShiftRight(&buf, 0); 135 int ret = cxBufferShiftRight(&buf, 0);
238 EXPECT_EQ(ret, 0); 136 CX_TEST_ASSERT(ret == 0);
239 EXPECT_EQ(buf.pos, 4); 137 CX_TEST_ASSERT(buf.pos == 4);
240 EXPECT_EQ(buf.size, 4); 138 CX_TEST_ASSERT(buf.size == 4);
241 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); 139 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
242 } 140 }
243 141
244 TEST_F(BufferShiftRight, ZeroOffsetInterface) { 142 TEST_F(BufferShiftRight, ZeroOffsetInterface) {
245 ASSERT_EQ(buf.pos, 4); 143 CX_TEST_ASSERT(buf.pos == 4);
246 ASSERT_EQ(buf.size, 4); 144 CX_TEST_ASSERT(buf.size == 4);
247 int ret = cxBufferShift(&buf, +0); 145 int ret = cxBufferShift(&buf, +0);
248 EXPECT_EQ(ret, 0); 146 CX_TEST_ASSERT(ret == 0);
249 EXPECT_EQ(buf.pos, 4); 147 CX_TEST_ASSERT(buf.pos == 4);
250 EXPECT_EQ(buf.size, 4); 148 CX_TEST_ASSERT(buf.size == 4);
251 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); 149 CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
252 } 150 }
253 151
254 TEST_F(BufferShiftRight, Standard) { 152 TEST_F(BufferShiftRight, Standard) {
255 ASSERT_EQ(buf.pos, 4); 153 CX_TEST_ASSERT(buf.pos == 4);
256 ASSERT_EQ(buf.size, 4); 154 CX_TEST_ASSERT(buf.size == 4);
257 int ret = cxBufferShiftRight(&buf, 3); 155 int ret = cxBufferShiftRight(&buf, 3);
258 EXPECT_EQ(ret, 0); 156 CX_TEST_ASSERT(ret == 0);
259 EXPECT_EQ(buf.pos, 7); 157 CX_TEST_ASSERT(buf.pos == 7);
260 EXPECT_EQ(buf.size, 7); 158 CX_TEST_ASSERT(buf.size == 7);
261 EXPECT_TRUE(memcmp(buf.space, "testest_____", 8) == 0); 159 CX_TEST_ASSERT(memcmp(buf.space, "testest_____", 8) == 0);
262 } 160 }
263 161
264 TEST_F(BufferShiftRight, OvershiftDiscard) { 162 TEST_F(BufferShiftRight, OvershiftDiscard) {
265 ASSERT_EQ(buf.pos, 4); 163 CX_TEST_ASSERT(buf.pos == 4);
266 ASSERT_EQ(buf.size, 4); 164 CX_TEST_ASSERT(buf.size == 4);
267 ASSERT_EQ(buf.capacity, 8); 165 CX_TEST_ASSERT(buf.capacity == 8);
268 int ret = cxBufferShiftRight(&buf, 6); 166 int ret = cxBufferShiftRight(&buf, 6);
269 EXPECT_EQ(ret, 0); 167 CX_TEST_ASSERT(ret == 0);
270 EXPECT_EQ(buf.pos, 8); 168 CX_TEST_ASSERT(buf.pos == 8);
271 EXPECT_EQ(buf.size, 8); 169 CX_TEST_ASSERT(buf.size == 8);
272 EXPECT_EQ(buf.capacity, 8); 170 CX_TEST_ASSERT(buf.capacity == 8);
273 EXPECT_TRUE(memcmp(buf.space, "test__te____", 8) == 0); 171 CX_TEST_ASSERT(memcmp(buf.space, "test__te____", 8) == 0);
274 } 172 }
275 173
276 TEST_F(BufferShiftRight, OvershiftExtend) { 174 TEST_F(BufferShiftRight, OvershiftExtend) {
277 ASSERT_EQ(buf.pos, 4); 175 CX_TEST_ASSERT(buf.pos == 4);
278 ASSERT_EQ(buf.size, 4); 176 CX_TEST_ASSERT(buf.size == 4);
279 ASSERT_EQ(buf.capacity, 8); 177 CX_TEST_ASSERT(buf.capacity == 8);
280 buf.flags |= CX_BUFFER_AUTO_EXTEND; 178 buf.flags |= CX_BUFFER_AUTO_EXTEND;
281 int ret = cxBufferShiftRight(&buf, 6); 179 int ret = cxBufferShiftRight(&buf, 6);
282 EXPECT_EQ(ret, 0); 180 CX_TEST_ASSERT(ret == 0);
283 EXPECT_EQ(buf.pos, 10); 181 CX_TEST_ASSERT(buf.pos == 10);
284 EXPECT_EQ(buf.size, 10); 182 CX_TEST_ASSERT(buf.size == 10);
285 EXPECT_GE(buf.capacity, 10); 183 EXPECT_GE(buf.capacity, 10);
286 EXPECT_TRUE(memcmp(buf.space, "test__test__", 8) == 0); 184 CX_TEST_ASSERT(memcmp(buf.space, "test__test__", 8) == 0);
287 } 185 }
288 186
289 TEST_F(BufferShiftRight, OffsetInterface) { 187 TEST_F(BufferShiftRight, OffsetInterface) {
290 buf.pos = 3; 188 buf.pos = 3;
291 ASSERT_EQ(buf.size, 4); 189 CX_TEST_ASSERT(buf.size == 4);
292 int ret = cxBufferShift(&buf, 2); 190 int ret = cxBufferShift(&buf, 2);
293 EXPECT_EQ(ret, 0); 191 CX_TEST_ASSERT(ret == 0);
294 EXPECT_EQ(buf.pos, 5); 192 CX_TEST_ASSERT(buf.pos == 5);
295 EXPECT_EQ(buf.size, 6); 193 CX_TEST_ASSERT(buf.size == 6);
296 EXPECT_TRUE(memcmp(buf.space, "tetest______", 8) == 0); 194 CX_TEST_ASSERT(memcmp(buf.space, "tetest______", 8) == 0);
297 }
298
299 TEST(BufferMinimumCapacity, Sufficient) {
300 CxTestingAllocator alloc;
301 auto space = cxMalloc(&alloc, 8);
302 CxBuffer buf;
303 cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS);
304 memcpy(space, "Testing", 8);
305 buf.size = 8;
306 cxBufferMinimumCapacity(&buf, 6);
307 EXPECT_EQ(buf.capacity, 8);
308 EXPECT_EQ(buf.size, 8);
309 EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
310 cxBufferDestroy(&buf);
311 EXPECT_TRUE(alloc.verify());
312 }
313
314 TEST(BufferMinimumCapacity, Extend) {
315 CxTestingAllocator alloc;
316 auto space = cxMalloc(&alloc, 8);
317 CxBuffer buf;
318 cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS); // NO auto extend!
319 memcpy(space, "Testing", 8);
320 buf.size = 8;
321 cxBufferMinimumCapacity(&buf, 16);
322 EXPECT_EQ(buf.capacity, 16);
323 EXPECT_EQ(buf.size, 8);
324 EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
325 cxBufferDestroy(&buf);
326 EXPECT_TRUE(alloc.verify());
327 }
328
329 TEST(BufferClear, Test) {
330 char space[16];
331 strcpy(space, "clear test");
332 CxBuffer buf;
333 cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
334 ASSERT_EQ(buf.size, 0);
335 // only clear the used part of the buffer
336 cxBufferClear(&buf);
337 EXPECT_EQ(memcmp(space, "clear test", 10), 0);
338 buf.size = 5;
339 buf.pos = 3;
340 cxBufferClear(&buf);
341 EXPECT_EQ(memcmp(space, "\0\0\0\0\0 test", 10), 0);
342 EXPECT_EQ(buf.size, 0);
343 EXPECT_EQ(buf.pos, 0);
344 cxBufferDestroy(&buf);
345 }
346
347 TEST(BufferReset, Test) {
348 char space[16];
349 strcpy(space, "reset test");
350 CxBuffer buf;
351 cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
352 buf.size = 5;
353 buf.pos = 3;
354 cxBufferReset(&buf);
355 EXPECT_EQ(memcmp(space, "reset test", 10), 0);
356 EXPECT_EQ(buf.size, 0);
357 EXPECT_EQ(buf.pos, 0);
358 cxBufferDestroy(&buf);
359 } 195 }
360 196
361 class BufferWrite : public ::testing::Test { 197 class BufferWrite : public ::testing::Test {
362 protected: 198 protected:
363 CxBuffer buf{}, target{}; 199 CxBuffer buf{}, target{};
364 200
365 void SetUp() override { 201 void SetUp() override {
366 cxBufferInit(&target, nullptr, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); 202 cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
367 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 203 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
368 buf.capacity = 8; // artificially reduce capacity to check OOB writes 204 buf.capacity = 8; // artificially reduce capacity to check OOB writes
369 memset(buf.space, 0, 16); 205 memset(buf.space, 0, 16);
370 memcpy(buf.space, "prep", 4); 206 memcpy(buf.space, "prep", 4);
371 buf.size = buf.pos = 4; 207 buf.size = buf.pos = 4;
372 } 208 }
400 } 236 }
401 } 237 }
402 238
403 TEST_F(BufferWrite, SizeOneFit) { 239 TEST_F(BufferWrite, SizeOneFit) {
404 const char *data = "test"; 240 const char *data = "test";
405 ASSERT_EQ(buf.capacity, 8); 241 CX_TEST_ASSERT(buf.capacity == 8);
406 ASSERT_EQ(buf.pos, 4); 242 CX_TEST_ASSERT(buf.pos == 4);
407 ASSERT_EQ(buf.size, 4); 243 CX_TEST_ASSERT(buf.size == 4);
408 size_t written = cxBufferWrite(data, 1, 4, &buf); 244 size_t written = cxBufferWrite(data, 1, 4, &buf);
409 EXPECT_EQ(written, 4); 245 CX_TEST_ASSERT(written == 4);
410 EXPECT_EQ(buf.size, 8); 246 CX_TEST_ASSERT(buf.size == 8);
411 EXPECT_EQ(buf.pos, 8); 247 CX_TEST_ASSERT(buf.pos == 8);
412 EXPECT_EQ(buf.capacity, 8); 248 CX_TEST_ASSERT(buf.capacity == 8);
413 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0); 249 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
414 } 250 }
415 251
416 TEST_F(BufferWrite, SizeOneDiscard) { 252 TEST_F(BufferWrite, SizeOneDiscard) {
417 const char *data = "testing"; 253 const char *data = "testing";
418 ASSERT_EQ(buf.capacity, 8); 254 CX_TEST_ASSERT(buf.capacity == 8);
419 ASSERT_EQ(buf.pos, 4); 255 CX_TEST_ASSERT(buf.pos == 4);
420 ASSERT_EQ(buf.size, 4); 256 CX_TEST_ASSERT(buf.size == 4);
421 size_t written = cxBufferWrite(data, 1, 7, &buf); 257 size_t written = cxBufferWrite(data, 1, 7, &buf);
422 EXPECT_EQ(written, 4); 258 CX_TEST_ASSERT(written == 4);
423 EXPECT_EQ(buf.size, 8); 259 CX_TEST_ASSERT(buf.size == 8);
424 EXPECT_EQ(buf.pos, 8); 260 CX_TEST_ASSERT(buf.pos == 8);
425 EXPECT_EQ(buf.capacity, 8); 261 CX_TEST_ASSERT(buf.capacity == 8);
426 EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0); 262 EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0);
427 } 263 }
428 264
429 TEST_F(BufferWrite, SizeOneExtend) { 265 TEST_F(BufferWrite, SizeOneExtend) {
430 buf.flags |= CX_BUFFER_AUTO_EXTEND; 266 buf.flags |= CX_BUFFER_AUTO_EXTEND;
431 const char *data = "testing"; 267 const char *data = "testing";
432 ASSERT_EQ(buf.capacity, 8); 268 CX_TEST_ASSERT(buf.capacity == 8);
433 ASSERT_EQ(buf.pos, 4); 269 CX_TEST_ASSERT(buf.pos == 4);
434 ASSERT_EQ(buf.size, 4); 270 CX_TEST_ASSERT(buf.size == 4);
435 size_t written = cxBufferWrite(data, 1, 7, &buf); 271 size_t written = cxBufferWrite(data, 1, 7, &buf);
436 EXPECT_EQ(written, 7); 272 CX_TEST_ASSERT(written == 7);
437 EXPECT_EQ(buf.size, 11); 273 CX_TEST_ASSERT(buf.size == 11);
438 EXPECT_EQ(buf.pos, 11); 274 CX_TEST_ASSERT(buf.pos == 11);
439 EXPECT_GE(buf.capacity, 11); 275 EXPECT_GE(buf.capacity, 11);
440 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0); 276 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
441 } 277 }
442 278
443 TEST_F(BufferWrite, MultibyteFit) { 279 TEST_F(BufferWrite, MultibyteFit) {
444 const char *data = "test"; 280 const char *data = "test";
445 ASSERT_EQ(buf.capacity, 8); 281 CX_TEST_ASSERT(buf.capacity == 8);
446 ASSERT_EQ(buf.pos, 4); 282 CX_TEST_ASSERT(buf.pos == 4);
447 ASSERT_EQ(buf.size, 4); 283 CX_TEST_ASSERT(buf.size == 4);
448 size_t written = cxBufferWrite(data, 2, 2, &buf); 284 size_t written = cxBufferWrite(data, 2, 2, &buf);
449 EXPECT_EQ(written, 2); 285 CX_TEST_ASSERT(written == 2);
450 EXPECT_EQ(buf.size, 8); 286 CX_TEST_ASSERT(buf.size == 8);
451 EXPECT_EQ(buf.pos, 8); 287 CX_TEST_ASSERT(buf.pos == 8);
452 EXPECT_EQ(buf.capacity, 8); 288 CX_TEST_ASSERT(buf.capacity == 8);
453 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0); 289 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
454 } 290 }
455 291
456 TEST_F(BufferWrite, MultibyteDiscard) { 292 TEST_F(BufferWrite, MultibyteDiscard) {
457 const char *data = "testing"; 293 const char *data = "testing";
458 ASSERT_EQ(buf.capacity, 8); 294 CX_TEST_ASSERT(buf.capacity == 8);
459 ASSERT_EQ(buf.size, 4); 295 CX_TEST_ASSERT(buf.size == 4);
460 buf.pos = 3; 296 buf.pos = 3;
461 size_t written = cxBufferWrite(data, 2, 4, &buf); 297 size_t written = cxBufferWrite(data, 2, 4, &buf);
462 // remember: whole elements are discarded if they do not fit 298 // remember: whole elements are discarded if they do not fit
463 EXPECT_EQ(written, 2); 299 CX_TEST_ASSERT(written == 2);
464 EXPECT_EQ(buf.size, 7); 300 CX_TEST_ASSERT(buf.size == 7);
465 EXPECT_EQ(buf.pos, 7); 301 CX_TEST_ASSERT(buf.pos == 7);
466 EXPECT_EQ(buf.capacity, 8); 302 CX_TEST_ASSERT(buf.capacity == 8);
467 EXPECT_EQ(memcmp(buf.space, "pretest\0", 8), 0); 303 EXPECT_EQ(memcmp(buf.space, "pretest\0", 8), 0);
468 } 304 }
469 305
470 TEST_F(BufferWrite, MultibyteExtend) { 306 TEST_F(BufferWrite, MultibyteExtend) {
471 buf.flags |= CX_BUFFER_AUTO_EXTEND; 307 buf.flags |= CX_BUFFER_AUTO_EXTEND;
472 const char *data = "tester"; 308 const char *data = "tester";
473 ASSERT_EQ(buf.capacity, 8); 309 CX_TEST_ASSERT(buf.capacity == 8);
474 ASSERT_EQ(buf.size, 4); 310 CX_TEST_ASSERT(buf.size == 4);
475 buf.pos = 3; 311 buf.pos = 3;
476 size_t written = cxBufferWrite(data, 2, 3, &buf); 312 size_t written = cxBufferWrite(data, 2, 3, &buf);
477 // remember: whole elements are discarded if they do not fit 313 // remember: whole elements are discarded if they do not fit
478 EXPECT_EQ(written, 3); 314 CX_TEST_ASSERT(written == 3);
479 EXPECT_EQ(buf.size, 9); 315 CX_TEST_ASSERT(buf.size == 9);
480 EXPECT_EQ(buf.pos, 9); 316 CX_TEST_ASSERT(buf.pos == 9);
481 EXPECT_GE(buf.capacity, 9); 317 EXPECT_GE(buf.capacity, 9);
482 EXPECT_EQ(memcmp(buf.space, "pretester", 9), 0); 318 EXPECT_EQ(memcmp(buf.space, "pretester", 9), 0);
483 } 319 }
484 320
485 TEST_F(BufferWrite, PutcWrapperFit) { 321 TEST_F(BufferWrite, PutcWrapperFit) {
486 ASSERT_EQ(buf.capacity, 8); 322 CX_TEST_ASSERT(buf.capacity == 8);
487 ASSERT_EQ(buf.pos, 4); 323 CX_TEST_ASSERT(buf.pos == 4);
488 ASSERT_EQ(buf.size, 4); 324 CX_TEST_ASSERT(buf.size == 4);
489 int c = cxBufferPut(&buf, 0x200 | 'a'); 325 int c = cxBufferPut(&buf, 0x200 | 'a');
490 EXPECT_EQ(c, 'a'); 326 CX_TEST_ASSERT(c == 'a');
491 EXPECT_EQ(buf.size, 5); 327 CX_TEST_ASSERT(buf.size == 5);
492 EXPECT_EQ(buf.pos, 5); 328 CX_TEST_ASSERT(buf.pos == 5);
493 EXPECT_EQ(buf.capacity, 8); 329 CX_TEST_ASSERT(buf.capacity == 8);
494 EXPECT_EQ(memcmp(buf.space, "prepa\0", 6), 0); 330 EXPECT_EQ(memcmp(buf.space, "prepa\0", 6), 0);
495 } 331 }
496 332
497 TEST_F(BufferWrite, PutcWrapperDiscard) { 333 TEST_F(BufferWrite, PutcWrapperDiscard) {
498 ASSERT_EQ(buf.capacity, 8); 334 CX_TEST_ASSERT(buf.capacity == 8);
499 ASSERT_EQ(buf.size, 4); 335 CX_TEST_ASSERT(buf.size == 4);
500 buf.pos = 8; 336 buf.pos = 8;
501 int c = cxBufferPut(&buf, 0x200 | 'a'); 337 int c = cxBufferPut(&buf, 0x200 | 'a');
502 EXPECT_EQ(c, EOF); 338 CX_TEST_ASSERT(c == EOF);
503 EXPECT_EQ(buf.size, 4); 339 CX_TEST_ASSERT(buf.size == 4);
504 EXPECT_EQ(buf.pos, 8); 340 CX_TEST_ASSERT(buf.pos == 8);
505 EXPECT_EQ(buf.capacity, 8); 341 CX_TEST_ASSERT(buf.capacity == 8);
506 EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0\0", 9), 0); 342 EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0\0", 9), 0);
507 } 343 }
508 344
509 TEST_F(BufferWrite, PutcWrapperExtend) { 345 TEST_F(BufferWrite, PutcWrapperExtend) {
510 buf.flags |= CX_BUFFER_AUTO_EXTEND; 346 buf.flags |= CX_BUFFER_AUTO_EXTEND;
511 ASSERT_EQ(buf.capacity, 8); 347 CX_TEST_ASSERT(buf.capacity == 8);
512 ASSERT_EQ(buf.size, 4); 348 CX_TEST_ASSERT(buf.size == 4);
513 buf.pos = 8; 349 buf.pos = 8;
514 int c = cxBufferPut(&buf, 0x200 | 'a'); 350 int c = cxBufferPut(&buf, 0x200 | 'a');
515 EXPECT_EQ(c, 'a'); 351 CX_TEST_ASSERT(c == 'a');
516 EXPECT_EQ(buf.size, 9); 352 CX_TEST_ASSERT(buf.size == 9);
517 EXPECT_EQ(buf.pos, 9); 353 CX_TEST_ASSERT(buf.pos == 9);
518 EXPECT_GE(buf.capacity, 9); 354 EXPECT_GE(buf.capacity, 9);
519 EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0a", 9), 0); 355 EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0a", 9), 0);
520 } 356 }
521 357
522 TEST_F(BufferWrite, PutStringWrapperFit) { 358 TEST_F(BufferWrite, PutStringWrapperFit) {
523 const char *data = "test"; 359 const char *data = "test";
524 ASSERT_EQ(buf.capacity, 8); 360 CX_TEST_ASSERT(buf.capacity == 8);
525 ASSERT_EQ(buf.pos, 4); 361 CX_TEST_ASSERT(buf.pos == 4);
526 ASSERT_EQ(buf.size, 4); 362 CX_TEST_ASSERT(buf.size == 4);
527 size_t written = cxBufferPutString(&buf, data); 363 size_t written = cxBufferPutString(&buf, data);
528 EXPECT_EQ(written, 4); 364 CX_TEST_ASSERT(written == 4);
529 EXPECT_EQ(buf.size, 8); 365 CX_TEST_ASSERT(buf.size == 8);
530 EXPECT_EQ(buf.pos, 8); 366 CX_TEST_ASSERT(buf.pos == 8);
531 EXPECT_EQ(buf.capacity, 8); 367 CX_TEST_ASSERT(buf.capacity == 8);
532 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0); 368 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
533 } 369 }
534 370
535 TEST_F(BufferWrite, PutStringWrapperDiscard) { 371 TEST_F(BufferWrite, PutStringWrapperDiscard) {
536 const char *data = "testing"; 372 const char *data = "testing";
537 ASSERT_EQ(buf.capacity, 8); 373 CX_TEST_ASSERT(buf.capacity == 8);
538 ASSERT_EQ(buf.pos, 4); 374 CX_TEST_ASSERT(buf.pos == 4);
539 ASSERT_EQ(buf.size, 4); 375 CX_TEST_ASSERT(buf.size == 4);
540 size_t written = cxBufferPutString(&buf, data); 376 size_t written = cxBufferPutString(&buf, data);
541 EXPECT_EQ(written, 4); 377 CX_TEST_ASSERT(written == 4);
542 EXPECT_EQ(buf.size, 8); 378 CX_TEST_ASSERT(buf.size == 8);
543 EXPECT_EQ(buf.pos, 8); 379 CX_TEST_ASSERT(buf.pos == 8);
544 EXPECT_EQ(buf.capacity, 8); 380 CX_TEST_ASSERT(buf.capacity == 8);
545 EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0); 381 EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0);
546 } 382 }
547 383
548 TEST_F(BufferWrite, PutStringWrapperExtend) { 384 TEST_F(BufferWrite, PutStringWrapperExtend) {
549 buf.flags |= CX_BUFFER_AUTO_EXTEND; 385 buf.flags |= CX_BUFFER_AUTO_EXTEND;
550 const char *data = "testing"; 386 const char *data = "testing";
551 ASSERT_EQ(buf.capacity, 8); 387 CX_TEST_ASSERT(buf.capacity == 8);
552 ASSERT_EQ(buf.pos, 4); 388 CX_TEST_ASSERT(buf.pos == 4);
553 ASSERT_EQ(buf.size, 4); 389 CX_TEST_ASSERT(buf.size == 4);
554 size_t written = cxBufferPutString(&buf, data); 390 size_t written = cxBufferPutString(&buf, data);
555 EXPECT_EQ(written, 7); 391 CX_TEST_ASSERT(written == 7);
556 EXPECT_EQ(buf.size, 11); 392 CX_TEST_ASSERT(buf.size == 11);
557 EXPECT_EQ(buf.pos, 11); 393 CX_TEST_ASSERT(buf.pos == 11);
558 EXPECT_GE(buf.capacity, 11); 394 EXPECT_GE(buf.capacity, 11);
559 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0); 395 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
560 } 396 }
561 397
562 TEST_F(BufferWrite, MultOverflow) { 398 TEST_F(BufferWrite, MultOverflow) {
563 const char *data = "testing"; 399 const char *data = "testing";
564 ASSERT_EQ(buf.capacity, 8); 400 CX_TEST_ASSERT(buf.capacity == 8);
565 ASSERT_EQ(buf.pos, 4); 401 CX_TEST_ASSERT(buf.pos == 4);
566 ASSERT_EQ(buf.size, 4); 402 CX_TEST_ASSERT(buf.size == 4);
567 size_t written = cxBufferWrite(data, 8, SIZE_MAX / 4, &buf); 403 size_t written = cxBufferWrite(data, 8, SIZE_MAX / 4, &buf);
568 EXPECT_EQ(written, 0); 404 CX_TEST_ASSERT(written == 0);
569 EXPECT_EQ(buf.capacity, 8); 405 CX_TEST_ASSERT(buf.capacity == 8);
570 EXPECT_EQ(buf.pos, 4); 406 CX_TEST_ASSERT(buf.pos == 4);
571 EXPECT_EQ(buf.size, 4); 407 CX_TEST_ASSERT(buf.size == 4);
572 EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0); 408 EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0);
573 } 409 }
574 410
575 TEST_F(BufferWrite, MaxCapaOverflow) { 411 TEST_F(BufferWrite, MaxCapaOverflow) {
576 buf.flags |= CX_BUFFER_AUTO_EXTEND; 412 buf.flags |= CX_BUFFER_AUTO_EXTEND;
577 const char *data = "testing"; 413 const char *data = "testing";
578 ASSERT_EQ(buf.capacity, 8); 414 CX_TEST_ASSERT(buf.capacity == 8);
579 ASSERT_EQ(buf.pos, 4); 415 CX_TEST_ASSERT(buf.pos == 4);
580 ASSERT_EQ(buf.size, 4); 416 CX_TEST_ASSERT(buf.size == 4);
581 size_t written = cxBufferWrite(data, 1, SIZE_MAX - 2, &buf); 417 size_t written = cxBufferWrite(data, 1, SIZE_MAX - 2, &buf);
582 EXPECT_EQ(written, 0); 418 CX_TEST_ASSERT(written == 0);
583 EXPECT_EQ(buf.capacity, 8); 419 CX_TEST_ASSERT(buf.capacity == 8);
584 EXPECT_EQ(buf.pos, 4); 420 CX_TEST_ASSERT(buf.pos == 4);
585 EXPECT_EQ(buf.size, 4); 421 CX_TEST_ASSERT(buf.size == 4);
586 EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0); 422 EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0);
587 } 423 }
588 424
589 TEST_F(BufferWrite, OnlyOverwrite) { 425 TEST_F(BufferWrite, OnlyOverwrite) {
590 buf.flags |= CX_BUFFER_AUTO_EXTEND; 426 buf.flags |= CX_BUFFER_AUTO_EXTEND;
591 ASSERT_EQ(buf.capacity, 8); 427 CX_TEST_ASSERT(buf.capacity == 8);
592 memcpy(buf.space, "preptest", 8); 428 memcpy(buf.space, "preptest", 8);
593 buf.pos = 3; 429 buf.pos = 3;
594 buf.size = 8; 430 buf.size = 8;
595 size_t written = cxBufferWrite("XXX", 2, 2, &buf); 431 size_t written = cxBufferWrite("XXX", 2, 2, &buf);
596 EXPECT_EQ(written, 2); 432 CX_TEST_ASSERT(written == 2);
597 EXPECT_EQ(buf.capacity, 8); 433 CX_TEST_ASSERT(buf.capacity == 8);
598 EXPECT_EQ(buf.size, 8); 434 CX_TEST_ASSERT(buf.size == 8);
599 EXPECT_EQ(buf.pos, 7); 435 CX_TEST_ASSERT(buf.pos == 7);
600 EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0); 436 EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0);
601 } 437 }
602 438
603 TEST_F(BufferWrite, FlushAtCapacity) { 439 TEST_F(BufferWrite, FlushAtCapacity) {
604 enableFlushing(); 440 enableFlushing();
605 ASSERT_EQ(buf.capacity, 8); 441 CX_TEST_ASSERT(buf.capacity == 8);
606 ASSERT_EQ(buf.pos, 4); 442 CX_TEST_ASSERT(buf.pos == 4);
607 size_t written = cxBufferWrite("foo", 1, 3, &buf); 443 size_t written = cxBufferWrite("foo", 1, 3, &buf);
608 EXPECT_EQ(written, 3); 444 CX_TEST_ASSERT(written == 3);
609 ASSERT_EQ(buf.pos, 7); 445 CX_TEST_ASSERT(buf.pos == 7);
610 ASSERT_EQ(buf.size, 7); 446 CX_TEST_ASSERT(buf.size == 7);
611 ASSERT_EQ(target.pos, 0); 447 CX_TEST_ASSERT(target.pos == 0);
612 ASSERT_EQ(target.size, 0); 448 CX_TEST_ASSERT(target.size == 0);
613 written = cxBufferWrite("hello", 1, 5, &buf); 449 written = cxBufferWrite("hello", 1, 5, &buf);
614 EXPECT_EQ(written, 5); 450 CX_TEST_ASSERT(written == 5);
615 EXPECT_EQ(buf.pos, 0); 451 CX_TEST_ASSERT(buf.pos == 0);
616 EXPECT_EQ(buf.size, 0); 452 CX_TEST_ASSERT(buf.size == 0);
617 EXPECT_EQ(buf.capacity, 8); 453 CX_TEST_ASSERT(buf.capacity == 8);
618 EXPECT_EQ(target.pos, 12); 454 CX_TEST_ASSERT(target.pos == 12);
619 ASSERT_EQ(target.size, 12); 455 CX_TEST_ASSERT(target.size == 12);
620 EXPECT_EQ(memcmp(target.space, "prepfoohello", 12), 0); 456 EXPECT_EQ(memcmp(target.space, "prepfoohello", 12), 0);
621 } 457 }
622 458
623 TEST_F(BufferWrite, FlushAtThreshold) { 459 TEST_F(BufferWrite, FlushAtThreshold) {
624 enableFlushing(); 460 enableFlushing();
625 buf.flush_threshold = 12; 461 buf.flush_threshold = 12;
626 buf.flags |= CX_BUFFER_AUTO_EXTEND; 462 buf.flags |= CX_BUFFER_AUTO_EXTEND;
627 ASSERT_EQ(buf.capacity, 8); 463 CX_TEST_ASSERT(buf.capacity == 8);
628 ASSERT_EQ(buf.pos, 4); 464 CX_TEST_ASSERT(buf.pos == 4);
629 size_t written = cxBufferWrite("foobar", 1, 6, &buf); 465 size_t written = cxBufferWrite("foobar", 1, 6, &buf);
630 EXPECT_EQ(written, 6); 466 CX_TEST_ASSERT(written == 6);
631 ASSERT_EQ(buf.pos, 10); 467 CX_TEST_ASSERT(buf.pos == 10);
632 ASSERT_EQ(buf.size, 10); 468 CX_TEST_ASSERT(buf.size == 10);
633 ASSERT_GE(buf.capacity, 10); 469 ASSERT_GE(buf.capacity, 10);
634 ASSERT_LE(buf.capacity, 12); 470 ASSERT_LE(buf.capacity, 12);
635 ASSERT_EQ(target.pos, 0); 471 CX_TEST_ASSERT(target.pos == 0);
636 ASSERT_EQ(target.size, 0); 472 CX_TEST_ASSERT(target.size == 0);
637 written = cxBufferWrite("hello", 1, 5, &buf); 473 written = cxBufferWrite("hello", 1, 5, &buf);
638 EXPECT_EQ(written, 5); 474 CX_TEST_ASSERT(written == 5);
639 EXPECT_EQ(buf.pos, 0); 475 CX_TEST_ASSERT(buf.pos == 0);
640 EXPECT_EQ(buf.size, 0); 476 CX_TEST_ASSERT(buf.size == 0);
641 EXPECT_LE(buf.capacity, 12); 477 EXPECT_LE(buf.capacity, 12);
642 EXPECT_EQ(target.pos, 15); 478 CX_TEST_ASSERT(target.pos == 15);
643 ASSERT_EQ(target.size, 15); 479 CX_TEST_ASSERT(target.size == 15);
644 EXPECT_EQ(memcmp(target.space, "prepfoobarhello", 15), 0); 480 EXPECT_EQ(memcmp(target.space, "prepfoobarhello", 15), 0);
645 } 481 }
646 482
647 TEST_F(BufferWrite, FlushRateLimited) { 483 TEST_F(BufferWrite, FlushRateLimited) {
648 enableFlushing(); 484 enableFlushing();
649 // limit the rate of the flush function and the capacity of the target 485 // limit the rate of the flush function and the capacity of the target
650 target.capacity = 16; 486 target.capacity = 16;
651 target.flags &= ~CX_BUFFER_AUTO_EXTEND; 487 target.flags &= ~CX_BUFFER_AUTO_EXTEND;
652 buf.flush_func = (cx_write_func) mock_write_limited_rate; 488 buf.flush_func = (cx_write_func) mock_write_limited_rate;
653 ASSERT_EQ(buf.capacity, 8); 489 CX_TEST_ASSERT(buf.capacity == 8);
654 ASSERT_EQ(buf.pos, 4); 490 CX_TEST_ASSERT(buf.pos == 4);
655 size_t written = cxBufferWrite("foo", 1, 3, &buf); 491 size_t written = cxBufferWrite("foo", 1, 3, &buf);
656 EXPECT_EQ(written, 3); 492 CX_TEST_ASSERT(written == 3);
657 ASSERT_EQ(buf.pos, 7); 493 CX_TEST_ASSERT(buf.pos == 7);
658 ASSERT_EQ(buf.size, 7); 494 CX_TEST_ASSERT(buf.size == 7);
659 ASSERT_EQ(target.pos, 0); 495 CX_TEST_ASSERT(target.pos == 0);
660 ASSERT_EQ(target.size, 0); 496 CX_TEST_ASSERT(target.size == 0);
661 written = cxBufferWrite("hello, world!", 1, 13, &buf); 497 written = cxBufferWrite("hello, world!", 1, 13, &buf);
662 // " world!" fits into this buffer, the remaining stuff is flushed out 498 // " world!" fits into this buffer, the remaining stuff is flushed out
663 EXPECT_EQ(written, 13); 499 CX_TEST_ASSERT(written == 13);
664 EXPECT_EQ(buf.pos, 7); 500 CX_TEST_ASSERT(buf.pos == 7);
665 EXPECT_EQ(buf.size, 7); 501 CX_TEST_ASSERT(buf.size == 7);
666 EXPECT_EQ(buf.capacity, 8); 502 CX_TEST_ASSERT(buf.capacity == 8);
667 EXPECT_EQ(memcmp(buf.space, " world!", 7), 0); 503 EXPECT_EQ(memcmp(buf.space, " world!", 7), 0);
668 EXPECT_EQ(target.pos, 13); 504 CX_TEST_ASSERT(target.pos == 13);
669 ASSERT_EQ(target.size, 13); 505 CX_TEST_ASSERT(target.size == 13);
670 EXPECT_EQ(target.capacity, 16); 506 CX_TEST_ASSERT(target.capacity == 16);
671 EXPECT_EQ(memcmp(target.space, "prepfoohello,", 13), 0); 507 EXPECT_EQ(memcmp(target.space, "prepfoohello,", 13), 0);
672 } 508 }
673 509
674 class BufferSeek : public BufferFixture {
675 };
676
677 TEST_F(BufferSeek, SetZero) {
678 int result = cxBufferSeek(&buf, 0, SEEK_SET);
679 EXPECT_EQ(result, 0);
680 EXPECT_EQ(buf.pos, 0);
681 }
682
683 TEST_F(BufferSeek, SetValid) {
684 int result = cxBufferSeek(&buf, 5, SEEK_SET);
685 EXPECT_EQ(result, 0);
686 EXPECT_EQ(buf.pos, 5);
687 }
688
689 TEST_F(BufferSeek, SetInvalid) {
690 ASSERT_EQ(buf.pos, 3);
691 int result = cxBufferSeek(&buf, 6, SEEK_SET);
692 EXPECT_NE(result, 0);
693 EXPECT_EQ(buf.pos, 3);
694 }
695
696 TEST_F(BufferSeek, CurZero) {
697 ASSERT_EQ(buf.pos, 3);
698 int result = cxBufferSeek(&buf, 0, SEEK_CUR);
699 EXPECT_EQ(result, 0);
700 EXPECT_EQ(buf.pos, 3);
701 }
702
703 TEST_F(BufferSeek, CurValidPositive) {
704 ASSERT_EQ(buf.pos, 3);
705 int result = cxBufferSeek(&buf, 2, SEEK_CUR);
706 EXPECT_EQ(result, 0);
707 EXPECT_EQ(buf.pos, 5);
708 }
709
710 TEST_F(BufferSeek, CurValidNegative) {
711 ASSERT_EQ(buf.pos, 3);
712 int result = cxBufferSeek(&buf, -3, SEEK_CUR);
713 EXPECT_EQ(result, 0);
714 EXPECT_EQ(buf.pos, 0);
715 }
716
717 TEST_F(BufferSeek, CurInvalidPositive) {
718 ASSERT_EQ(buf.pos, 3);
719 int result = cxBufferSeek(&buf, 3, SEEK_CUR);
720 EXPECT_NE(result, 0);
721 EXPECT_EQ(buf.pos, 3);
722 }
723
724 TEST_F(BufferSeek, CurInvalidNegative) {
725 ASSERT_EQ(buf.pos, 3);
726 int result = cxBufferSeek(&buf, -4, SEEK_CUR);
727 EXPECT_NE(result, 0);
728 EXPECT_EQ(buf.pos, 3);
729 }
730
731 TEST_F(BufferSeek, EndZero) {
732 ASSERT_EQ(buf.size, 6);
733 int result = cxBufferSeek(&buf, 0, SEEK_END);
734 // the (past-the-)end position is always invalid
735 EXPECT_NE(result, 0);
736 EXPECT_EQ(buf.pos, 3);
737 }
738
739 TEST_F(BufferSeek, EndValid) {
740 ASSERT_EQ(buf.size, 6);
741 int result = cxBufferSeek(&buf, -6, SEEK_END);
742 EXPECT_EQ(result, 0);
743 EXPECT_EQ(buf.pos, 0);
744 }
745
746 TEST_F(BufferSeek, EndInvalid) {
747 ASSERT_EQ(buf.size, 6);
748 int result = cxBufferSeek(&buf, 1, SEEK_END);
749 EXPECT_NE(result, 0);
750 EXPECT_EQ(buf.pos, 3);
751 }
752
753 TEST_F(BufferSeek, WhenceInvalid) {
754 ASSERT_EQ(buf.size, 6);
755 ASSERT_EQ(buf.pos, 3);
756 int result = cxBufferSeek(&buf, 2, 9000);
757 EXPECT_NE(result, 0);
758 EXPECT_EQ(buf.size, 6);
759 EXPECT_EQ(buf.pos, 3);
760 }
761
762 class BufferEof : public BufferFixture {
763 };
764 510
765 TEST_F(BufferEof, Reached) { 511 TEST_F(BufferEof, Reached) {
766 buf.pos = buf.size; 512 buf.pos = buf.size;
767 EXPECT_TRUE(cxBufferEof(&buf)); 513 CX_TEST_ASSERT(cxBufferEof(&buf));
768 buf.pos = buf.size - 1; 514 buf.pos = buf.size - 1;
769 ASSERT_FALSE(cxBufferEof(&buf)); 515 CX_TEST_ASSERT(!cxBufferEof(&buf));
770 cxBufferPut(&buf, 'a'); 516 cxBufferPut(&buf, 'a');
771 EXPECT_TRUE(cxBufferEof(&buf)); 517 CX_TEST_ASSERT(cxBufferEof(&buf));
772 } 518 }
773 519
774 TEST_F(BufferEof, NotReached) { 520 TEST_F(BufferEof, NotReached) {
775 buf.pos = buf.size - 1; 521 buf.pos = buf.size - 1;
776 EXPECT_FALSE(cxBufferEof(&buf)); 522 CX_TEST_ASSERT(!cxBufferEof(&buf));
777 buf.pos = 0; 523 buf.pos = 0;
778 cxBufferWrite("test", 1, 5, &buf); 524 cxBufferWrite("test", 1, 5, &buf);
779 EXPECT_FALSE(cxBufferEof(&buf)); 525 CX_TEST_ASSERT(!cxBufferEof(&buf));
780 } 526 }
781 527
782 class BufferRead : public ::testing::Test { 528 class BufferRead : public ::testing::Test {
783 protected: 529 protected:
784 CxBuffer buf{}; 530 CxBuffer buf{};
785 531
786 void SetUp() override { 532 void SetUp() override {
787 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 533 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
788 buf.capacity = 8; // artificially reduce capacity to check OOB writes 534 buf.capacity = 8; // artificially reduce capacity to check OOB writes
789 memset(buf.space, 0, 16); 535 memset(buf.space, 0, 16);
790 memcpy(buf.space, "some data", 9); 536 memcpy(buf.space, "some data", 9);
791 buf.size = 9; 537 buf.size = 9;
792 } 538 }
800 buf.pos = 2; 546 buf.pos = 2;
801 EXPECT_EQ(cxBufferGet(&buf), 'm'); 547 EXPECT_EQ(cxBufferGet(&buf), 'm');
802 EXPECT_EQ(cxBufferGet(&buf), 'e'); 548 EXPECT_EQ(cxBufferGet(&buf), 'e');
803 EXPECT_EQ(cxBufferGet(&buf), ' '); 549 EXPECT_EQ(cxBufferGet(&buf), ' ');
804 EXPECT_EQ(cxBufferGet(&buf), 'd'); 550 EXPECT_EQ(cxBufferGet(&buf), 'd');
805 EXPECT_EQ(buf.pos, 6); 551 CX_TEST_ASSERT(buf.pos == 6);
806 } 552 }
807 553
808 TEST_F(BufferRead, GetEof) { 554 TEST_F(BufferRead, GetEof) {
809 buf.pos = buf.size; 555 buf.pos = buf.size;
810 EXPECT_EQ(cxBufferGet(&buf), EOF); 556 EXPECT_EQ(cxBufferGet(&buf), EOF);
812 558
813 TEST_F(BufferRead, ReadWithinBounds) { 559 TEST_F(BufferRead, ReadWithinBounds) {
814 buf.pos = 2; 560 buf.pos = 2;
815 char target[4]; 561 char target[4];
816 auto read = cxBufferRead(&target, 1, 4, &buf); 562 auto read = cxBufferRead(&target, 1, 4, &buf);
817 ASSERT_EQ(read, 4); 563 CX_TEST_ASSERT(read == 4);
818 EXPECT_EQ(memcmp(&target, "me d", 4), 0); 564 EXPECT_EQ(memcmp(&target, "me d", 4), 0);
819 EXPECT_EQ(buf.pos, 6); 565 CX_TEST_ASSERT(buf.pos == 6);
820 } 566 }
821 567
822 TEST_F(BufferRead, ReadOutOfBounds) { 568 TEST_F(BufferRead, ReadOutOfBounds) {
823 buf.pos = 6; 569 buf.pos = 6;
824 char target[4]; 570 char target[4];
825 auto read = cxBufferRead(&target, 1, 4, &buf); 571 auto read = cxBufferRead(&target, 1, 4, &buf);
826 ASSERT_EQ(read, 3); 572 CX_TEST_ASSERT(read == 3);
827 EXPECT_EQ(memcmp(&target, "ata", 3), 0); 573 EXPECT_EQ(memcmp(&target, "ata", 3), 0);
828 EXPECT_EQ(buf.pos, 9); 574 CX_TEST_ASSERT(buf.pos == 9);
829 } 575 }
830 576
831 TEST_F(BufferRead, ReadOutOfBoundsMultibyte) { 577 TEST_F(BufferRead, ReadOutOfBoundsMultibyte) {
832 buf.pos = 6; 578 buf.pos = 6;
833 char target[4]; 579 char target[4];
834 target[2] = '\0'; 580 target[2] = '\0';
835 auto read = cxBufferRead(&target, 2, 2, &buf); 581 auto read = cxBufferRead(&target, 2, 2, &buf);
836 ASSERT_EQ(read, 1); 582 CX_TEST_ASSERT(read == 1);
837 EXPECT_EQ(memcmp(&target, "at\0", 3), 0); 583 EXPECT_EQ(memcmp(&target, "at\0", 3), 0);
838 EXPECT_EQ(buf.pos, 8); 584 CX_TEST_ASSERT(buf.pos == 8);
839 } 585 }
840 586
841 TEST_F(BufferRead, ReadEof) { 587 TEST_F(BufferRead, ReadEof) {
842 buf.pos = 9; 588 buf.pos = 9;
843 char target[4]; 589 char target[4];
844 auto read = cxBufferRead(&target, 1, 1, &buf); 590 auto read = cxBufferRead(&target, 1, 1, &buf);
845 ASSERT_EQ(read, 0); 591 CX_TEST_ASSERT(read == 0);
846 EXPECT_EQ(buf.pos, 9); 592 CX_TEST_ASSERT(buf.pos == 9);
847 } 593 }

mercurial