diff -r aed60ba37acf -r 24a012440dee test/utils_tests.c --- a/test/utils_tests.c Tue May 06 10:56:54 2014 +0200 +++ b/test/utils_tests.c Tue May 06 12:03:16 2014 +0200 @@ -90,3 +90,54 @@ free(teststr1); free(teststr2); } + +UCX_TEST(test_ucx_stream_copy) { + UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); + UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); + + UCX_TEST_BEGIN + + ucx_buffer_write("01234567", 1, 8, b1); + ucx_buffer_write("abcdefgh", 1, 8, b1); + UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); + ucx_buffer_seek(b1, 0, SEEK_SET); + + size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write); + UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); + UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); + UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, + "b1 and b2 have not the same content"); + + memset(b2->space, 0, b2->capacity); + b2->pos = 0; + b2->size = 0; + ucx_buffer_seek(b1, 0, SEEK_SET); + + FILE *file = tmpfile(); + UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); + + ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite); + UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); + + fseek(file, 0, SEEK_SET); + + ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write); + UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); + + UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, + "b1 and b2 content mismatch"); + + fclose(file); + + ucx_buffer_clear(b1); + ucx_buffer_seek(b2, 0, SEEK_SET); + ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8); + UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy"); + UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0, + "content wrong after ncopy"); + + UCX_TEST_END + + ucx_buffer_free(b1); + ucx_buffer_free(b2); +}