1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2017 Mike Becker, Olaf Wintermann 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 met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "buffer_tests.h" |
|
30 #include <stdint.h> |
|
31 |
|
32 UCX_TEST(test_ucx_buffer_new) { |
|
33 UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND); |
|
34 UcxBuffer *b2 = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
|
35 UCX_TEST_BEGIN |
|
36 |
|
37 UCX_TEST_ASSERT(b->capacity==16, "wrong capacity"); |
|
38 UCX_TEST_ASSERT(b2->capacity==32, "wrong capacity"); |
|
39 |
|
40 UCX_TEST_ASSERT(b->size==0, "wrong size"); |
|
41 UCX_TEST_ASSERT(b2->size==0, "wrong size"); |
|
42 |
|
43 UCX_TEST_ASSERT(b->pos==0, "wrong position"); |
|
44 UCX_TEST_ASSERT(b2->pos==0, "wrong position"); |
|
45 |
|
46 UCX_TEST_ASSERT(b->flags==(UCX_BUFFER_AUTOEXTEND|UCX_BUFFER_AUTOFREE), |
|
47 "wrong flags for autoextending buffer"); |
|
48 UCX_TEST_ASSERT(b2->flags==UCX_BUFFER_AUTOFREE, |
|
49 "wrong flags for default bufer"); |
|
50 |
|
51 UCX_TEST_END |
|
52 ucx_buffer_free(b2); |
|
53 ucx_buffer_free(b); |
|
54 } |
|
55 |
|
56 UCX_TEST(test_ucx_buffer_new_prealloc) { |
|
57 char* test = (char*) malloc(16); |
|
58 UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT); |
|
59 UCX_TEST_BEGIN |
|
60 |
|
61 UCX_TEST_ASSERT(b->capacity==16, "wrong capacity"); |
|
62 UCX_TEST_ASSERT(b->size==0, "wrong size"); |
|
63 UCX_TEST_ASSERT(b->pos==0, "wrong position"); |
|
64 |
|
65 UCX_TEST_ASSERT(b->flags==0, "wrong flags - all should be cleared"); |
|
66 |
|
67 UCX_TEST_END |
|
68 free(test); |
|
69 ucx_buffer_free(b); |
|
70 } |
|
71 |
|
72 UCX_TEST(test_ucx_buffer_eof) { |
|
73 char *test = (char*)"0123456789ABCDEF"; |
|
74 UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT); |
|
75 UCX_TEST_BEGIN |
|
76 b->pos = 9; b->size = 10; |
|
77 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive"); |
|
78 b->pos = 10; b->size = 10; |
|
79 UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF"); |
|
80 b->pos = 11; b->size = 10; |
|
81 UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative"); |
|
82 UCX_TEST_END |
|
83 ucx_buffer_free(b); |
|
84 } |
|
85 |
|
86 UCX_TEST(test_ucx_buffer_seek_overflow) { |
|
87 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
|
88 b->size = 32; |
|
89 int r; |
|
90 |
|
91 UCX_TEST_BEGIN |
|
92 const size_t sizemax = (size_t)-1; |
|
93 size_t bigpos = sizemax - 5000; |
|
94 b->pos = bigpos; |
|
95 r = ucx_buffer_seek(b, 5016, SEEK_CUR); |
|
96 UCX_TEST_ASSERT(r != 0, "seek cur overflow"); |
|
97 UCX_TEST_ASSERT(b->pos == bigpos, |
|
98 "failed seek shall leave pos unchanged"); |
|
99 |
|
100 b->pos = 0; |
|
101 b->size = (sizemax >> 1) + 32; |
|
102 |
|
103 // we don't want to risk overflows in implicit constant casts |
|
104 const size_t bigoff_comp = (sizemax >> 1) - 16; |
|
105 off_t bigoff = (off_t)bigoff_comp; |
|
106 |
|
107 r = ucx_buffer_seek(b, -bigoff, SEEK_CUR); |
|
108 UCX_TEST_ASSERT(r != 0, "seek cur underflow"); |
|
109 UCX_TEST_ASSERT(b->pos == 0, |
|
110 "failed seek shall leave pos unchanged"); |
|
111 |
|
112 UCX_TEST_END |
|
113 |
|
114 ucx_buffer_free(b); |
|
115 } |
|
116 |
|
117 UCX_TEST(test_ucx_buffer_seek_invalid) { |
|
118 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
|
119 b->pos = 7; |
|
120 int r; |
|
121 |
|
122 UCX_TEST_BEGIN |
|
123 r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END)); |
|
124 UCX_TEST_ASSERT(r != 0, "invalid whence shall fail"); |
|
125 UCX_TEST_ASSERT(b->pos == 7, |
|
126 "failed seek shall leave pos unchanged"); |
|
127 UCX_TEST_END |
|
128 |
|
129 ucx_buffer_free(b); |
|
130 } |
|
131 |
|
132 UCX_TEST(test_ucx_buffer_seek_oob) { |
|
133 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
|
134 b->size = 16; // less than capacity |
|
135 b->pos = 7; |
|
136 int r; |
|
137 |
|
138 UCX_TEST_BEGIN |
|
139 |
|
140 r = ucx_buffer_seek(b, -1, SEEK_SET); |
|
141 UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail"); |
|
142 UCX_TEST_ASSERT(b->pos == 7, |
|
143 "failed seek shall leave pos unchanged"); |
|
144 |
|
145 r = ucx_buffer_seek(b, 16, SEEK_SET); |
|
146 UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail"); |
|
147 UCX_TEST_ASSERT(b->pos == 7, |
|
148 "failed seek shall leave pos unchanged"); |
|
149 |
|
150 r = ucx_buffer_seek(b, -8, SEEK_CUR); |
|
151 UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail"); |
|
152 UCX_TEST_ASSERT(b->pos == 7, |
|
153 "failed seek shall leave pos unchanged"); |
|
154 |
|
155 r = ucx_buffer_seek(b, 9, SEEK_CUR); |
|
156 UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail"); |
|
157 UCX_TEST_ASSERT(b->pos == 7, |
|
158 "failed seek shall leave pos unchanged"); |
|
159 |
|
160 r = ucx_buffer_seek(b, -17, SEEK_END); |
|
161 UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail"); |
|
162 UCX_TEST_ASSERT(b->pos == 7, |
|
163 "failed seek shall leave pos unchanged"); |
|
164 |
|
165 r = ucx_buffer_seek(b, 1, SEEK_END); |
|
166 UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail"); |
|
167 UCX_TEST_ASSERT(b->pos == 7, |
|
168 "failed seek shall leave pos unchanged"); |
|
169 |
|
170 UCX_TEST_END |
|
171 |
|
172 ucx_buffer_free(b); |
|
173 } |
|
174 |
|
175 UCX_TEST(test_ucx_buffer_seek_set) { |
|
176 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
|
177 b->size = 16; |
|
178 int r; |
|
179 |
|
180 UCX_TEST_BEGIN |
|
181 |
|
182 r = ucx_buffer_seek(b, 5, SEEK_SET); |
|
183 UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); |
|
184 UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position"); |
|
185 |
|
186 |
|
187 r = ucx_buffer_seek(b, 10, SEEK_SET); |
|
188 UCX_TEST_ASSERT(r == 0, "seek SET+10 failed"); |
|
189 UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position"); |
|
190 |
|
191 UCX_TEST_END |
|
192 |
|
193 ucx_buffer_free(b); |
|
194 } |
|
195 |
|
196 UCX_TEST(test_ucx_buffer_seek_cur) { |
|
197 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
|
198 b->size = 16; |
|
199 int r; |
|
200 |
|
201 UCX_TEST_BEGIN |
|
202 |
|
203 b->pos = 7; |
|
204 r = ucx_buffer_seek(b, 5, SEEK_CUR); |
|
205 UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); |
|
206 UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position"); |
|
207 |
|
208 UCX_TEST_END |
|
209 |
|
210 ucx_buffer_free(b); |
|
211 } |
|
212 |
|
213 UCX_TEST(test_ucx_buffer_seek_end) { |
|
214 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
|
215 b->size = 16; |
|
216 int r; |
|
217 |
|
218 UCX_TEST_BEGIN |
|
219 |
|
220 r = ucx_buffer_seek(b, -5, SEEK_END); |
|
221 UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); |
|
222 UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position"); |
|
223 |
|
224 |
|
225 UCX_TEST_END |
|
226 |
|
227 ucx_buffer_free(b); |
|
228 } |
|
229 |
|
230 UCX_TEST(test_ucx_buffer_putc) { |
|
231 char *buffer = (char*) malloc(16); |
|
232 memset(buffer, 32, 16); |
|
233 |
|
234 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
|
235 b->size = b->capacity; |
|
236 |
|
237 UCX_TEST_BEGIN |
|
238 |
|
239 ucx_buffer_seek(b, 0, SEEK_SET); |
|
240 UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0', |
|
241 "putc shall return (arg & 0xFF)"); |
|
242 ucx_buffer_putc(b, '0'); |
|
243 ucx_buffer_putc(b, '0'); |
|
244 |
|
245 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts"); |
|
246 ucx_buffer_seek(b, 10, SEEK_CUR); |
|
247 |
|
248 ucx_buffer_putc(b, '0'); |
|
249 ucx_buffer_putc(b, '0'); |
|
250 ucx_buffer_putc(b, '0'); |
|
251 |
|
252 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts"); |
|
253 |
|
254 UCX_TEST_ASSERT(!memcmp(b->space, "000 000", 16), |
|
255 "buffer content wrong") |
|
256 UCX_TEST_END |
|
257 ucx_buffer_free(b); |
|
258 free(buffer); |
|
259 } |
|
260 |
|
261 UCX_TEST(test_ucx_buffer_putc_oob) { |
|
262 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT); |
|
263 |
|
264 UCX_TEST_BEGIN |
|
265 b->pos = b->size = b->capacity = 1; |
|
266 b->space[1] = 'X'; |
|
267 |
|
268 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF " |
|
269 "when buffer is full and auto extend is disabled"); |
|
270 UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2), |
|
271 "wrong buffer content after failed putc"); |
|
272 |
|
273 UCX_TEST_END |
|
274 |
|
275 ucx_buffer_free(b); |
|
276 } |
|
277 |
|
278 |
|
279 UCX_TEST(test_ucx_buffer_putc_ae) { |
|
280 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
|
281 ucx_buffer_putc(b, '0'); |
|
282 ucx_buffer_putc(b, '1'); |
|
283 |
|
284 UCX_TEST_BEGIN |
|
285 |
|
286 UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts"); |
|
287 UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts"); |
|
288 UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended"); |
|
289 UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content"); |
|
290 |
|
291 UCX_TEST_END |
|
292 |
|
293 ucx_buffer_free(b); |
|
294 } |
|
295 |
|
296 UCX_TEST(test_ucx_buffer_putc_oobae) { |
|
297 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
|
298 ucx_buffer_putc(b, '0'); |
|
299 ucx_buffer_putc(b, '1'); |
|
300 |
|
301 UCX_TEST_BEGIN |
|
302 |
|
303 ucx_buffer_putc(b, 'a'); |
|
304 |
|
305 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put"); |
|
306 UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended"); |
|
307 UCX_TEST_ASSERT(b->size == 3, "wrong buffer size"); |
|
308 |
|
309 UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content"); |
|
310 |
|
311 UCX_TEST_END |
|
312 |
|
313 ucx_buffer_free(b); |
|
314 } |
|
315 |
|
316 UCX_TEST(test_ucx_buffer_putc_size) { |
|
317 UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT); |
|
318 |
|
319 UCX_TEST_BEGIN |
|
320 |
|
321 UCX_TEST_ASSERT(b->size == 0, "wrong initial size"); |
|
322 ucx_buffer_putc(b, 'a'); |
|
323 ucx_buffer_putc(b, 'b'); |
|
324 ucx_buffer_putc(b, 'c'); |
|
325 UCX_TEST_ASSERT(b->size == 3, "size does not increase"); |
|
326 ucx_buffer_seek(b, 1, SEEK_SET); |
|
327 ucx_buffer_putc(b, 'd'); |
|
328 UCX_TEST_ASSERT(b->size == 3, "size shall not decrease"); |
|
329 UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc"); |
|
330 |
|
331 UCX_TEST_END |
|
332 |
|
333 ucx_buffer_free(b); |
|
334 } |
|
335 |
|
336 UCX_TEST(test_ucx_buffer_getc) { |
|
337 char *buffer = (char*) malloc(16); |
|
338 memset(buffer, 32, 8); |
|
339 for (int i = 8; i < 16 ; i++) { |
|
340 buffer[i] = 40+i; |
|
341 } |
|
342 |
|
343 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
|
344 b->size = b->capacity; |
|
345 |
|
346 unsigned char ubuffer[] = {127, 128, 129, 130}; |
|
347 UcxBuffer *ub = ucx_buffer_new(ubuffer, 4, UCX_BUFFER_DEFAULT); |
|
348 ub->size = 4; |
|
349 |
|
350 UCX_TEST_BEGIN |
|
351 |
|
352 char rb[16]; |
|
353 for (size_t i = 0 ; i < 16 ; i++) { |
|
354 UCX_TEST_ASSERT(b->pos == i, "wrong position"); |
|
355 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "EOF false positive"); |
|
356 rb[i] = ucx_buffer_getc(b); |
|
357 } |
|
358 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, |
|
359 "read data incorrect"); |
|
360 |
|
361 UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set after last possible read"); |
|
362 UCX_TEST_ASSERT(b->pos == 16, "wrong position after EOF"); |
|
363 |
|
364 UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF, |
|
365 "out of bounds read does not return EOF"); |
|
366 UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read"); |
|
367 |
|
368 int uc[5]; |
|
369 for(int i=0;i<5;i++) { |
|
370 uc[i] = ucx_buffer_getc(ub); |
|
371 } |
|
372 UCX_TEST_ASSERT(uc[0] == 127, "wrong unsigned value(0)"); |
|
373 UCX_TEST_ASSERT(uc[1] == 128, "wrong unsigned value(0)"); |
|
374 UCX_TEST_ASSERT(uc[2] == 129, "wrong unsigned value(0)"); |
|
375 UCX_TEST_ASSERT(uc[3] == 130, "wrong unsigned value(0)"); |
|
376 UCX_TEST_ASSERT(uc[4] == EOF, "EOF not set after last ub read"); |
|
377 |
|
378 UCX_TEST_END |
|
379 |
|
380 ucx_buffer_free(b); |
|
381 ucx_buffer_free(ub); |
|
382 free(buffer); |
|
383 } |
|
384 |
|
385 UCX_TEST(test_ucx_buffer_write) { |
|
386 char *buffer = (char*) malloc(32); |
|
387 memset(buffer, 0, 32); |
|
388 |
|
389 memset(buffer, 32, 8); |
|
390 for (int i = 8; i < 16 ; i++) { |
|
391 buffer[i] = 40+i; |
|
392 } |
|
393 |
|
394 UcxBuffer *b = ucx_buffer_new(buffer, 32, UCX_BUFFER_DEFAULT); |
|
395 int r; |
|
396 |
|
397 UCX_TEST_BEGIN |
|
398 b->pos = 4; |
|
399 r = ucx_buffer_write("test string", 1, 11, b); |
|
400 UCX_TEST_ASSERT(r == 11, "returned incorrect number of written bytes"); |
|
401 UCX_TEST_ASSERT(b->pos == 15, "incorrect position"); |
|
402 UCX_TEST_ASSERT(memcmp(buffer, " test string7\0\0", 18) == 0, |
|
403 "incorrect buffer content (test string)"); |
|
404 |
|
405 r = ucx_buffer_write(".", 1, 1, b); |
|
406 UCX_TEST_ASSERT(r == 1, "returned incorrect number of written elements"); |
|
407 UCX_TEST_ASSERT(b->pos == 16, "incorrect position"); |
|
408 |
|
409 int32_t testarr[4] = {0x09abcdef, 0x05fedcba, 0x01abefcd, 0x3cd07ab}; |
|
410 r = ucx_buffer_write(testarr, 4, 4, b); |
|
411 UCX_TEST_ASSERT(r = 4, "returned incorrect number of written elements"); |
|
412 UCX_TEST_ASSERT(b->pos == 32, "incorrect position"); |
|
413 |
|
414 char cmp[32]; |
|
415 memset(cmp, 0, 32); |
|
416 memcpy(cmp, " test string.", 16); |
|
417 int32_t *ptr = (int32_t*) (cmp+16); |
|
418 ptr[0] = testarr[0]; |
|
419 ptr[1] = testarr[1]; |
|
420 ptr[2] = testarr[2]; |
|
421 ptr[3] = testarr[3]; |
|
422 UCX_TEST_ASSERT(memcmp(buffer, cmp, 32) == 0, |
|
423 "incorrect buffer content (int array)"); |
|
424 |
|
425 UCX_TEST_END |
|
426 |
|
427 ucx_buffer_free(b); |
|
428 free(buffer); |
|
429 } |
|
430 |
|
431 UCX_TEST(test_ucx_buffer_write_oob) { |
|
432 char *buffer = (char*) malloc(32); |
|
433 memset(buffer, 0, 32); |
|
434 |
|
435 UcxBuffer *b = ucx_buffer_new(buffer, 15, UCX_BUFFER_DEFAULT); |
|
436 int r; |
|
437 |
|
438 UCX_TEST_BEGIN |
|
439 r = ucx_buffer_write("a very long string", 1, 18, b); |
|
440 UCX_TEST_ASSERT(r == 15, "incorrect number of written bytes"); |
|
441 UCX_TEST_ASSERT(memcmp(buffer, "a very long str\0\0\0", 18) == 0, |
|
442 "invalid buffer content (test string)"); |
|
443 |
|
444 b->size = b->pos = 0; |
|
445 int32_t intarr[4] = {0,-1,0,-1}; |
|
446 memset(buffer, 0, 32); |
|
447 |
|
448 r = ucx_buffer_write(intarr, 4, 4, b); |
|
449 UCX_TEST_ASSERT(r == 3, "incorrect number of written elements"); |
|
450 UCX_TEST_ASSERT(memcmp(buffer, |
|
451 "\0\0\0\0\xff\xff\xff\xff\0\0\0\0\0\0\0\0", 16) == 0, |
|
452 "invalid buffer content (test string)"); |
|
453 |
|
454 UCX_TEST_END |
|
455 |
|
456 ucx_buffer_free(b); |
|
457 free(buffer); |
|
458 } |
|
459 |
|
460 UCX_TEST(test_ucx_buffer_write_ax) { |
|
461 char *buffer = (char*) malloc(16); |
|
462 memset(buffer, 0, 16); |
|
463 |
|
464 UcxBuffer *b = ucx_buffer_new(buffer, 16, |
|
465 UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE); |
|
466 int r; |
|
467 |
|
468 UCX_TEST_BEGIN |
|
469 |
|
470 const char* teststring = "this is way too much"; |
|
471 r = ucx_buffer_write((void*)teststring, 1, 20, b); |
|
472 buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/ |
|
473 UCX_TEST_ASSERT(r == 20, "not all characters written"); |
|
474 UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended"); |
|
475 UCX_TEST_ASSERT(b->pos == 20, "position incorrect"); |
|
476 UCX_TEST_ASSERT(memcmp(buffer, |
|
477 "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0, |
|
478 "incorrect buffer content"); |
|
479 |
|
480 UCX_TEST_END |
|
481 |
|
482 ucx_buffer_free(b); |
|
483 } |
|
484 |
|
485 UCX_TEST(test_ucx_buffer_read) { |
|
486 UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); |
|
487 ucx_buffer_write("01234567", 1, 8, b); |
|
488 b->pos = 0; |
|
489 |
|
490 char buf[32]; |
|
491 memset(buf, 'X', 32); |
|
492 int r; |
|
493 |
|
494 UCX_TEST_BEGIN |
|
495 |
|
496 ucx_buffer_seek(b, 2, SEEK_SET); |
|
497 r = ucx_buffer_read(buf, 1, 2, b); |
|
498 UCX_TEST_ASSERT(r == 2, "wrong number of bytes read (2 items)"); |
|
499 UCX_TEST_ASSERT(buf[0] == '2' && buf[1] == '3' && buf[2] == 'X', |
|
500 "buffer incorrect after read"); |
|
501 UCX_TEST_ASSERT(b->pos == 4, "wrong position after read (2 items)"); |
|
502 |
|
503 UCX_TEST_END |
|
504 |
|
505 ucx_buffer_free(b); |
|
506 } |
|
507 |
|
508 UCX_TEST(test_ucx_buffer_read_oob) { |
|
509 UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); |
|
510 ucx_buffer_write("01234567", 1, 8, b); |
|
511 |
|
512 char buf[32]; |
|
513 memset(buf, 'X', 32); |
|
514 int r; |
|
515 |
|
516 UCX_TEST_BEGIN |
|
517 |
|
518 b->pos = 2; |
|
519 r = ucx_buffer_read(buf + 2, 1, 8, b); |
|
520 UCX_TEST_ASSERT(r == 6, "wrong number of bytes read (8 items)"); |
|
521 UCX_TEST_ASSERT(memcmp(buf, "XX234567XX", 10) == 0, |
|
522 "buffer incorrect after read"); |
|
523 UCX_TEST_ASSERT(b->pos == 8, |
|
524 "wrong position after read (8 items out of bound)"); |
|
525 |
|
526 b->pos = 0; |
|
527 memset(buf, 'X', 32); |
|
528 |
|
529 r = ucx_buffer_read(buf, 3, 3, b); |
|
530 |
|
531 UCX_TEST_ASSERT(r == 2, "wrong number of blocks read"); |
|
532 UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0, |
|
533 "buffer incorrect after block out of bounds read"); |
|
534 |
|
535 r = ucx_buffer_read(buf+6, 1, 5, b); |
|
536 |
|
537 UCX_TEST_ASSERT(r == 2, "wrong number of remaining bytes read"); |
|
538 UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0, |
|
539 "buffer incorrect after remaining byte read"); |
|
540 |
|
541 UCX_TEST_END |
|
542 |
|
543 ucx_buffer_free(b); |
|
544 } |
|
545 |
|
546 UCX_TEST(test_ucx_buffer_extract) { |
|
547 char *buffer = (char*) malloc(16); |
|
548 strcpy(buffer, "this is a test!"); |
|
549 |
|
550 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
|
551 src->size = 16; |
|
552 UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND); |
|
553 |
|
554 UCX_TEST_BEGIN |
|
555 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); |
|
556 UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE), |
|
557 "autofree flag shall be enforced"); |
|
558 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); |
|
559 UCX_TEST_ASSERT(dst->capacity == 5, "wrong capacity for new buffer"); |
|
560 UCX_TEST_ASSERT(dst->pos == 0, "wrong position for new buffer"); |
|
561 char rb[5]; |
|
562 ucx_buffer_read(rb, 1, 5, dst); |
|
563 UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0, |
|
564 "new buffer has incorrect content"); |
|
565 |
|
566 UCX_TEST_END |
|
567 |
|
568 ucx_buffer_free(dst); |
|
569 ucx_buffer_free(src); |
|
570 } |
|
571 |
|
572 UCX_TEST(test_ucx_buffer_extract_oob) { |
|
573 char *buffer = (char*) malloc(16); |
|
574 strcpy(buffer, "this is a test!"); |
|
575 |
|
576 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
|
577 src->size = 16; |
|
578 |
|
579 UCX_TEST_BEGIN |
|
580 |
|
581 UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL, |
|
582 "extract shall fail on zero length"); |
|
583 UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL, |
|
584 "extract shall fail on invalid bounds (size exceeds limits)"); |
|
585 UCX_TEST_ASSERT(ucx_buffer_extract(src, 20, -7, UCX_BUFFER_DEFAULT) == NULL, |
|
586 "extract shall fail on invalid bounds (start exceeds limits)"); |
|
587 |
|
588 UCX_TEST_END |
|
589 |
|
590 ucx_buffer_free(src); |
|
591 } |
|
592 |
|
593 UCX_TEST(test_ucx_buffer_extract_overflow) { |
|
594 char *buffer = (char*) malloc(16); |
|
595 strcpy(buffer, "this is a test!"); |
|
596 |
|
597 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
|
598 src->size = 16; |
|
599 |
|
600 UCX_TEST_BEGIN |
|
601 |
|
602 UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4, |
|
603 UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow"); |
|
604 |
|
605 UCX_TEST_END |
|
606 |
|
607 ucx_buffer_free(src); |
|
608 } |
|
609 |
|
610 UCX_TEST(test_ucx_buffer_extend) { |
|
611 |
|
612 UcxBuffer *b = ucx_buffer_new(NULL, 10, UCX_BUFFER_DEFAULT); |
|
613 |
|
614 UCX_TEST_BEGIN |
|
615 |
|
616 UCX_TEST_ASSERT(ucx_buffer_extend(b, 15) == 0, "shall return 0 on success"); |
|
617 UCX_TEST_ASSERT(b->capacity = 40, "wrong capacity"); |
|
618 UCX_TEST_ASSERT((b->size == 0 && b->pos == 0), |
|
619 "pos and size shall remain unchanged"); |
|
620 |
|
621 UCX_TEST_ASSERT(ucx_buffer_extend(b, (size_t) - 61) != 0, |
|
622 "shall fail and return a non-zero value on overflow"); |
|
623 |
|
624 UCX_TEST_END |
|
625 |
|
626 ucx_buffer_free(b); |
|
627 } |
|
628 |
|
629 UCX_TEST(test_ucx_buffer_shl) { |
|
630 |
|
631 const char* hw = "Shift the World!"; |
|
632 |
|
633 UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_DEFAULT); |
|
634 ucx_buffer_puts(b, hw); |
|
635 b->pos = 5; |
|
636 |
|
637 UCX_TEST_BEGIN |
|
638 char* expected; |
|
639 |
|
640 ucx_buffer_shift_left(b, 2); |
|
641 expected = "ift the World!"; |
|
642 |
|
643 UCX_TEST_ASSERT(b->pos == 3, "position after normal shl wrong"); |
|
644 UCX_TEST_ASSERT(b->size == strlen(expected), "size after normal shl wrong"); |
|
645 UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), |
|
646 "contents after normal shl wrong"); |
|
647 |
|
648 |
|
649 ucx_buffer_shift_left(b, 5); |
|
650 expected = "he World!"; |
|
651 |
|
652 UCX_TEST_ASSERT(b->pos == 0, "position after overshift left wrong"); |
|
653 UCX_TEST_ASSERT(b->size == strlen(expected), |
|
654 "size after overshift left wrong"); |
|
655 UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), |
|
656 "contents after overshift left wrong"); |
|
657 |
|
658 ucx_buffer_shift_left(b, 10); |
|
659 UCX_TEST_ASSERT(b->pos == 0, "position after 'shl everything away' wrong"); |
|
660 UCX_TEST_ASSERT(b->size == 0, "size after 'shl everything away' wrong"); |
|
661 |
|
662 UCX_TEST_END |
|
663 |
|
664 ucx_buffer_free(b); |
|
665 } |
|
666 |
|
667 UCX_TEST(test_ucx_buffer_shr) { |
|
668 |
|
669 const char* hw = "Shift the World!"; |
|
670 |
|
671 UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_DEFAULT); |
|
672 ucx_buffer_puts(b, hw); |
|
673 b->pos = 12; |
|
674 |
|
675 UCX_TEST_BEGIN |
|
676 char* expected; |
|
677 |
|
678 ucx_buffer_shift_right(b, 2); |
|
679 expected = "ShShift the World!"; |
|
680 |
|
681 UCX_TEST_ASSERT(b->pos == 14, "position after normal shr wrong"); |
|
682 UCX_TEST_ASSERT(b->size == strlen(expected), "size after normal shr wrong"); |
|
683 UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), |
|
684 "contents after normal shr wrong"); |
|
685 |
|
686 |
|
687 ucx_buffer_shift_right(b, 5); |
|
688 expected = "ShShiShShift the Wor"; |
|
689 UCX_TEST_ASSERT(strlen(expected) == b->capacity, |
|
690 "Test data is wrong, please fix the test."); |
|
691 |
|
692 UCX_TEST_ASSERT(b->pos == 19, |
|
693 "position after overshift right w/o auto-extend wrong"); |
|
694 UCX_TEST_ASSERT(b->size == 20, |
|
695 "size after overshift right w/o auto-extend wrong"); |
|
696 UCX_TEST_ASSERT(b->capacity == 20, |
|
697 "capacity after overshift right w/o auto-extend wrong"); |
|
698 UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), |
|
699 "contents after overshift right w/o auto-extend wrong"); |
|
700 |
|
701 ucx_buffer_shift_right(b, 15); |
|
702 UCX_TEST_ASSERT(b->pos == b->capacity, "position after 'shr to eof' wrong"); |
|
703 UCX_TEST_ASSERT(b->size == b->capacity, "size after 'shr to eof' wrong"); |
|
704 UCX_TEST_ASSERT(ucx_buffer_eof(b), "buffer not eof after 'shr to eof'"); |
|
705 |
|
706 UCX_TEST_END |
|
707 |
|
708 ucx_buffer_free(b); |
|
709 } |
|
710 |
|
711 UCX_TEST(test_ucx_buffer_shr_ax) { |
|
712 |
|
713 const char* hw = "Shift the World!"; |
|
714 |
|
715 UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_AUTOEXTEND); |
|
716 ucx_buffer_puts(b, hw); |
|
717 b->pos = 12; |
|
718 |
|
719 UCX_TEST_BEGIN |
|
720 |
|
721 const char* expected = "Shift the Shift the World!"; |
|
722 |
|
723 ucx_buffer_shift_right(b, 10); |
|
724 UCX_TEST_ASSERT(b->pos == 22, "position after shr w/ auto-extend wrong"); |
|
725 UCX_TEST_ASSERT(b->size == strlen(expected), |
|
726 "size after shr w/ auto-extend wrong"); |
|
727 UCX_TEST_ASSERT(b->capacity >= b->size, |
|
728 "auto-extension of capacity after shr w/ auto-extend failed"); |
|
729 UCX_TEST_ASSERT(!ucx_buffer_eof(b), |
|
730 "buffer should not be eof after shr w/ auto-extend"); |
|
731 UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), |
|
732 "contents wrong after shr w/ auto-extend"); |
|
733 |
|
734 UCX_TEST_END |
|
735 |
|
736 ucx_buffer_free(b); |
|
737 } |
|
738 |
|