32 /* |
32 /* |
33 * TODO: refactor tests |
33 * TODO: refactor tests |
34 * |
34 * |
35 * ucx_buffer_extend |
35 * ucx_buffer_extend |
36 * ucx_buffer_extract |
36 * ucx_buffer_extract |
37 * ucx_buffer_free |
|
38 * ucx_buffer_getc |
|
39 * ucx_buffer_new |
|
40 * ucx_buffer_puts |
|
41 * ucx_buffer_read |
|
42 * ucx_buffer_write |
37 * ucx_buffer_write |
43 * |
38 * |
44 */ |
39 */ |
|
40 |
|
41 UCX_TEST(test_ucx_buffer_new) { |
|
42 UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND); |
|
43 UcxBuffer *b2 = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
|
44 UCX_TEST_BEGIN |
|
45 |
|
46 UCX_TEST_ASSERT(b->capacity==16, "wrong capacity"); |
|
47 UCX_TEST_ASSERT(b2->capacity==32, "wrong capacity"); |
|
48 |
|
49 UCX_TEST_ASSERT(b->size==0, "wrong size"); |
|
50 UCX_TEST_ASSERT(b2->size==0, "wrong size"); |
|
51 |
|
52 UCX_TEST_ASSERT(b->pos==0, "wrong position"); |
|
53 UCX_TEST_ASSERT(b2->pos==0, "wrong position"); |
|
54 |
|
55 UCX_TEST_ASSERT(b->flags==(UCX_BUFFER_AUTOEXTEND|UCX_BUFFER_AUTOFREE), |
|
56 "wrong flags for autoextending buffer"); |
|
57 UCX_TEST_ASSERT(b2->flags==UCX_BUFFER_AUTOFREE, |
|
58 "wrong flags for default bufer"); |
|
59 |
|
60 UCX_TEST_END |
|
61 ucx_buffer_free(b2); |
|
62 ucx_buffer_free(b); |
|
63 } |
|
64 |
|
65 UCX_TEST(test_ucx_buffer_new_prealloc) { |
|
66 char* test = (char*) malloc(16); |
|
67 UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT); |
|
68 UCX_TEST_BEGIN |
|
69 |
|
70 UCX_TEST_ASSERT(b->capacity==16, "wrong capacity"); |
|
71 UCX_TEST_ASSERT(b->size==0, "wrong size"); |
|
72 UCX_TEST_ASSERT(b->pos==0, "wrong position"); |
|
73 |
|
74 UCX_TEST_ASSERT(b->flags==0, "wrong flags - all should be cleared"); |
|
75 |
|
76 UCX_TEST_END |
|
77 free(test); |
|
78 ucx_buffer_free(b); |
|
79 } |
45 |
80 |
46 UCX_TEST(test_ucx_buffer_eof) { |
81 UCX_TEST(test_ucx_buffer_eof) { |
47 char *test = "0123456789ABCDEF"; |
82 char *test = "0123456789ABCDEF"; |
48 UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT); |
83 UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT); |
49 UCX_TEST_BEGIN |
84 UCX_TEST_BEGIN |
313 |
348 |
314 UCX_TEST_BEGIN |
349 UCX_TEST_BEGIN |
315 |
350 |
316 char rb[16]; |
351 char rb[16]; |
317 for (size_t i = 0 ; i < 16 ; i++) { |
352 for (size_t i = 0 ; i < 16 ; i++) { |
318 UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop"); |
353 UCX_TEST_ASSERT(b->pos == i, "wrong position"); |
319 UCX_TEST_ASSERT(!ucx_buffer_eof(b), |
354 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "EOF false positive"); |
320 "EOF shall not be set during read loop"); |
|
321 rb[i] = ucx_buffer_getc(b); |
355 rb[i] = ucx_buffer_getc(b); |
322 } |
356 } |
323 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop"); |
|
324 UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set"); |
|
325 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, |
357 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, |
326 "read data incorrect"); |
358 "read data incorrect"); |
|
359 |
|
360 UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set after last possible read"); |
|
361 UCX_TEST_ASSERT(b->pos == 16, "wrong position after EOF"); |
|
362 |
|
363 UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF, |
|
364 "out of bounds read does not return EOF"); |
|
365 UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read"); |
327 |
366 |
328 UCX_TEST_END |
367 UCX_TEST_END |
329 |
368 |
330 ucx_buffer_free(b); |
369 ucx_buffer_free(b); |
331 free(buffer); |
370 free(buffer); |
397 ucx_buffer_free(b); |
436 ucx_buffer_free(b); |
398 } |
437 } |
399 |
438 |
400 UCX_TEST(test_ucx_buffer_read) { |
439 UCX_TEST(test_ucx_buffer_read) { |
401 UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); |
440 UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); |
|
441 ucx_buffer_write("01234567", 1, 8, b); |
|
442 b->pos = 0; |
402 |
443 |
403 char buf[32]; |
444 char buf[32]; |
404 memset(buf, 'X', 32); |
445 memset(buf, 'X', 32); |
405 int r; |
446 int r; |
406 |
447 |
407 UCX_TEST_BEGIN |
448 UCX_TEST_BEGIN |
408 |
449 |
|
450 ucx_buffer_seek(b, 2, SEEK_SET); |
|
451 r = ucx_buffer_read(buf, 1, 2, b); |
|
452 UCX_TEST_ASSERT(r == 2, "wrong number of bytes read (2 items)"); |
|
453 UCX_TEST_ASSERT(buf[0] == '2' && buf[1] == '3' && buf[2] == 'X', |
|
454 "buffer incorrect after read"); |
|
455 UCX_TEST_ASSERT(b->pos == 4, "wrong position after read (2 items)"); |
|
456 |
|
457 UCX_TEST_END |
|
458 |
|
459 ucx_buffer_free(b); |
|
460 } |
|
461 |
|
462 UCX_TEST(test_ucx_buffer_read_oob) { |
|
463 UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); |
409 ucx_buffer_write("01234567", 1, 8, b); |
464 ucx_buffer_write("01234567", 1, 8, b); |
410 UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled"); |
465 |
|
466 char buf[32]; |
|
467 memset(buf, 'X', 32); |
|
468 int r; |
|
469 |
|
470 UCX_TEST_BEGIN |
|
471 |
|
472 b->pos = 2; |
|
473 r = ucx_buffer_read(buf + 2, 1, 8, b); |
|
474 UCX_TEST_ASSERT(r == 6, "wrong number of bytes read (8 items)"); |
|
475 UCX_TEST_ASSERT(memcmp(buf, "XX234567XX", 10) == 0, |
|
476 "buffer incorrect after read"); |
|
477 UCX_TEST_ASSERT(b->pos == 8, |
|
478 "wrong position after read (8 items out of bound)"); |
|
479 |
411 b->pos = 0; |
480 b->pos = 0; |
412 |
|
413 r = ucx_buffer_read(buf, 1, 2, b); |
|
414 UCX_TEST_ASSERT(r == 2, "wrong number of bytes read"); |
|
415 UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X', |
|
416 "buffer incorrect after first read"); |
|
417 |
|
418 r = ucx_buffer_read(buf + 2, 1, 8, b); |
|
419 UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)"); |
|
420 UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0, |
|
421 "buffer incorrect after second read"); |
|
422 |
|
423 memset(buf, 'X', 32); |
481 memset(buf, 'X', 32); |
424 ucx_buffer_seek(b, 0, SEEK_SET); |
482 |
425 r = ucx_buffer_read(buf, 3, 3, b); |
483 r = ucx_buffer_read(buf, 3, 3, b); |
426 |
484 |
427 UCX_TEST_ASSERT(r == 2, "wrong number of blocks read"); |
485 UCX_TEST_ASSERT(r == 2, "wrong number of blocks read"); |
428 UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0, |
486 UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0, |
429 "buffer incorrect after three byte read"); |
487 "buffer incorrect after block out of bounds read"); |
430 |
488 |
|
489 r = ucx_buffer_read(buf+6, 1, 5, b); |
|
490 |
|
491 UCX_TEST_ASSERT(r == 2, "wrong number of remaining bytes read"); |
|
492 UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0, |
|
493 "buffer incorrect after remaining byte read"); |
431 |
494 |
432 UCX_TEST_END |
495 UCX_TEST_END |
433 |
496 |
434 ucx_buffer_free(b); |
497 ucx_buffer_free(b); |
435 } |
498 } |