369 ucx_buffer_free(b); |
360 ucx_buffer_free(b); |
370 free(buffer); |
361 free(buffer); |
371 } |
362 } |
372 |
363 |
373 UCX_TEST(test_ucx_buffer_write) { |
364 UCX_TEST(test_ucx_buffer_write) { |
374 char *buffer = (char*) malloc(16); |
365 char *buffer = (char*) malloc(32); |
375 memset(buffer, 32, 8); |
366 memset(buffer, 32, 8); |
376 for (int i = 8; i < 16 ; i++) { |
367 for (int i = 8; i < 16 ; i++) { |
377 buffer[i] = 40+i; |
368 buffer[i] = 40+i; |
378 } |
369 } |
379 |
370 |
380 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
371 UcxBuffer *b = ucx_buffer_new(buffer, 32, UCX_BUFFER_DEFAULT); |
381 int r; |
372 int r; |
382 |
373 |
383 UCX_TEST_BEGIN |
374 UCX_TEST_BEGIN |
384 |
375 b->pos = 4; |
385 const char* teststring = "this is way too much"; |
376 r = ucx_buffer_write("test string", 1, 11, b); |
386 r = ucx_buffer_write((void*)teststring, 1, 20, b); |
377 UCX_TEST_ASSERT(r == 11, "returned incorrect number of written bytes"); |
387 UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); |
378 UCX_TEST_ASSERT(b->pos == 15, "incorrect position"); |
388 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, |
379 UCX_TEST_ASSERT(memcmp(buffer, " test string7\0\0", 18) == 0, |
389 "buffer data incorrect"); |
380 "incorrect buffer content (test string)"); |
390 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set"); |
381 |
391 |
382 int32_t testarr[4] = {0x09abcdef, 0x05fedcba, 0x01abefcd, 0x3cd07ab}; |
392 ucx_buffer_seek(b, 8, SEEK_SET); |
383 r = ucx_buffer_write(testarr, 4, 4, b); |
393 r = ucx_buffer_write((void*)"not", 1, 3, b); |
384 UCX_TEST_ASSERT(r = 4, "returned incorrect number of written elements"); |
394 UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); |
385 UCX_TEST_ASSERT(b->pos == 31, "incorrect position"); |
395 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, |
386 |
396 "modified buffer is incorrect"); |
387 char cmp[32]; |
397 |
388 memset(cmp, 0, 32); |
398 const char* threebytestring = " t h r e e "; |
389 memcpy(cmp, " test string", 15); |
399 memset(buffer, 49, 16); |
390 int32_t *ptr = (int32_t*) (cmp+15); |
400 ucx_buffer_seek(b, 0, SEEK_SET); |
391 ptr[0] = testarr[0]; |
401 r = ucx_buffer_write((void*)threebytestring, 3, 6, b); |
392 ptr[1] = testarr[1]; |
402 UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed"); |
393 ptr[2] = testarr[2]; |
403 UCX_TEST_ASSERT(b->pos == 15, |
394 ptr[3] = testarr[3]; |
404 "position after write of three byte string incorrect"); |
395 UCX_TEST_ASSERT(memcmp(buffer, cmp, 32) == 0, |
405 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); |
396 "incorrect buffer content (int array)"); |
406 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
397 |
407 "bufer is incorrect after three byte string has been written"); |
398 UCX_TEST_END |
408 |
399 |
|
400 ucx_buffer_free(b); |
|
401 free(buffer); |
|
402 } |
|
403 |
|
404 UCX_TEST(test_ucx_buffer_write_oob) { |
|
405 char *buffer = (char*) malloc(32); |
|
406 memset(buffer, 0, 32); |
|
407 |
|
408 UcxBuffer *b = ucx_buffer_new(buffer, 15, UCX_BUFFER_DEFAULT); |
|
409 int r; |
|
410 |
|
411 UCX_TEST_BEGIN |
|
412 r = ucx_buffer_write("a very long string", 1, 18, b); |
|
413 UCX_TEST_ASSERT(r == 15, "incorrect number of written bytes"); |
|
414 UCX_TEST_ASSERT(memcmp(buffer, "a very long str\0\0\0", 18) == 0, |
|
415 "invalid buffer content (test string)"); |
|
416 |
|
417 b->size = b->pos = 0; |
|
418 int32_t intarr[4] = {0,-1,0,-1}; |
|
419 memset(buffer, 0, 32); |
|
420 |
|
421 r = ucx_buffer_write(intarr, 4, 4, b); |
|
422 UCX_TEST_ASSERT(r == 3, "incorrect number of written elements"); |
|
423 UCX_TEST_ASSERT(memcmp(buffer, |
|
424 "\0\0\0\0\xff\xff\xff\xff\0\0\0\0\0\0\0\0", 16) == 0, |
|
425 "invalid buffer content (test string)"); |
|
426 |
409 UCX_TEST_END |
427 UCX_TEST_END |
410 |
428 |
411 ucx_buffer_free(b); |
429 ucx_buffer_free(b); |
412 free(buffer); |
430 free(buffer); |
413 } |
431 } |
500 UCX_TEST(test_ucx_buffer_extract) { |
518 UCX_TEST(test_ucx_buffer_extract) { |
501 char *buffer = (char*) malloc(16); |
519 char *buffer = (char*) malloc(16); |
502 strcpy(buffer, "this is a test!"); |
520 strcpy(buffer, "this is a test!"); |
503 |
521 |
504 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
522 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
505 src->size = 15; |
523 src->size = 16; |
506 UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND); |
524 UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND); |
507 |
525 |
508 UCX_TEST_BEGIN |
526 UCX_TEST_BEGIN |
509 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); |
527 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); |
510 UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE), |
528 UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE), |
526 UCX_TEST(test_ucx_buffer_extract_oob) { |
544 UCX_TEST(test_ucx_buffer_extract_oob) { |
527 char *buffer = (char*) malloc(16); |
545 char *buffer = (char*) malloc(16); |
528 strcpy(buffer, "this is a test!"); |
546 strcpy(buffer, "this is a test!"); |
529 |
547 |
530 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
548 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
|
549 src->size = 16; |
|
550 |
531 UCX_TEST_BEGIN |
551 UCX_TEST_BEGIN |
532 |
552 |
533 UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL, |
553 UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL, |
534 "extract shall fail on zero length"); |
554 "extract shall fail on zero length"); |
535 UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL, |
555 UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL, |
545 UCX_TEST(test_ucx_buffer_extract_overflow) { |
565 UCX_TEST(test_ucx_buffer_extract_overflow) { |
546 char *buffer = (char*) malloc(16); |
566 char *buffer = (char*) malloc(16); |
547 strcpy(buffer, "this is a test!"); |
567 strcpy(buffer, "this is a test!"); |
548 |
568 |
549 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
569 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
|
570 src->size = 16; |
|
571 |
550 UCX_TEST_BEGIN |
572 UCX_TEST_BEGIN |
551 |
573 |
552 UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4, |
574 UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4, |
553 UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow"); |
575 UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow"); |
554 |
576 |
555 UCX_TEST_END |
577 UCX_TEST_END |
556 |
578 |
557 ucx_buffer_free(src); |
579 ucx_buffer_free(src); |
558 } |
580 } |
559 |
581 |
560 UCX_TEST(test_ucx_stream_copy) { |
582 UCX_TEST(test_ucx_buffer_extend) { |
561 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); |
583 |
562 UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
584 UcxBuffer *b = ucx_buffer_new(NULL, 10, UCX_BUFFER_DEFAULT); |
563 |
585 |
564 UCX_TEST_BEGIN |
586 UCX_TEST_BEGIN |
565 |
587 |
566 ucx_buffer_write("01234567", 1, 8, b1); |
588 UCX_TEST_ASSERT(ucx_buffer_extend(b, 15) == 0, "shall return 0 on success"); |
567 ucx_buffer_write("abcdefgh", 1, 8, b1); |
589 UCX_TEST_ASSERT(b->capacity = 40, "wrong capacity"); |
568 UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); |
590 UCX_TEST_ASSERT((b->size == 0 && b->pos == 0), |
569 ucx_buffer_seek(b1, 0, SEEK_SET); |
591 "pos and size shall remain unchanged"); |
570 |
592 |
571 size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write); |
593 UCX_TEST_ASSERT(ucx_buffer_extend(b, SIZE_MAX - 60) != 0, |
572 UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); |
594 "shall fail and return a non-zero value on overflow"); |
573 UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); |
595 |
574 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, |
596 UCX_TEST_END |
575 "b1 and b2 have not the same content"); |
597 |
576 |
598 ucx_buffer_free(b); |
577 memset(b2->space, 0, b2->capacity); |
599 } |
578 b2->pos = 0; |
|
579 b2->size = 0; |
|
580 ucx_buffer_seek(b1, 0, SEEK_SET); |
|
581 |
|
582 FILE *file = tmpfile(); |
|
583 UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); |
|
584 |
|
585 ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite); |
|
586 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); |
|
587 |
|
588 fseek(file, 0, SEEK_SET); |
|
589 |
|
590 ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write); |
|
591 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); |
|
592 |
|
593 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, |
|
594 "b1 and b2 content mismatch"); |
|
595 |
|
596 fclose(file); |
|
597 |
|
598 ucx_buffer_clear(b1); |
|
599 ucx_buffer_seek(b2, 0, SEEK_SET); |
|
600 ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8); |
|
601 UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy"); |
|
602 UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0, |
|
603 "content wrong after ncopy"); |
|
604 |
|
605 UCX_TEST_END |
|
606 |
|
607 ucx_buffer_free(b1); |
|
608 ucx_buffer_free(b2); |
|
609 } |
|