1.1 --- a/test/utils_tests.c Tue May 06 10:56:54 2014 +0200 1.2 +++ b/test/utils_tests.c Tue May 06 12:03:16 2014 +0200 1.3 @@ -90,3 +90,54 @@ 1.4 free(teststr1); 1.5 free(teststr2); 1.6 } 1.7 + 1.8 +UCX_TEST(test_ucx_stream_copy) { 1.9 + UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); 1.10 + UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); 1.11 + 1.12 + UCX_TEST_BEGIN 1.13 + 1.14 + ucx_buffer_write("01234567", 1, 8, b1); 1.15 + ucx_buffer_write("abcdefgh", 1, 8, b1); 1.16 + UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); 1.17 + ucx_buffer_seek(b1, 0, SEEK_SET); 1.18 + 1.19 + size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write); 1.20 + UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); 1.21 + UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); 1.22 + UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, 1.23 + "b1 and b2 have not the same content"); 1.24 + 1.25 + memset(b2->space, 0, b2->capacity); 1.26 + b2->pos = 0; 1.27 + b2->size = 0; 1.28 + ucx_buffer_seek(b1, 0, SEEK_SET); 1.29 + 1.30 + FILE *file = tmpfile(); 1.31 + UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); 1.32 + 1.33 + ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite); 1.34 + UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); 1.35 + 1.36 + fseek(file, 0, SEEK_SET); 1.37 + 1.38 + ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write); 1.39 + UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); 1.40 + 1.41 + UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, 1.42 + "b1 and b2 content mismatch"); 1.43 + 1.44 + fclose(file); 1.45 + 1.46 + ucx_buffer_clear(b1); 1.47 + ucx_buffer_seek(b2, 0, SEEK_SET); 1.48 + ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8); 1.49 + UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy"); 1.50 + UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0, 1.51 + "content wrong after ncopy"); 1.52 + 1.53 + UCX_TEST_END 1.54 + 1.55 + ucx_buffer_free(b1); 1.56 + ucx_buffer_free(b2); 1.57 +}