88 UCX_TEST_END |
88 UCX_TEST_END |
89 |
89 |
90 free(teststr1); |
90 free(teststr1); |
91 free(teststr2); |
91 free(teststr2); |
92 } |
92 } |
|
93 |
|
94 UCX_TEST(test_ucx_stream_copy) { |
|
95 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); |
|
96 UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
|
97 |
|
98 UCX_TEST_BEGIN |
|
99 |
|
100 ucx_buffer_write("01234567", 1, 8, b1); |
|
101 ucx_buffer_write("abcdefgh", 1, 8, b1); |
|
102 UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); |
|
103 ucx_buffer_seek(b1, 0, SEEK_SET); |
|
104 |
|
105 size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write); |
|
106 UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); |
|
107 UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); |
|
108 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, |
|
109 "b1 and b2 have not the same content"); |
|
110 |
|
111 memset(b2->space, 0, b2->capacity); |
|
112 b2->pos = 0; |
|
113 b2->size = 0; |
|
114 ucx_buffer_seek(b1, 0, SEEK_SET); |
|
115 |
|
116 FILE *file = tmpfile(); |
|
117 UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); |
|
118 |
|
119 ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite); |
|
120 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); |
|
121 |
|
122 fseek(file, 0, SEEK_SET); |
|
123 |
|
124 ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write); |
|
125 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); |
|
126 |
|
127 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, |
|
128 "b1 and b2 content mismatch"); |
|
129 |
|
130 fclose(file); |
|
131 |
|
132 ucx_buffer_clear(b1); |
|
133 ucx_buffer_seek(b2, 0, SEEK_SET); |
|
134 ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8); |
|
135 UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy"); |
|
136 UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0, |
|
137 "content wrong after ncopy"); |
|
138 |
|
139 UCX_TEST_END |
|
140 |
|
141 ucx_buffer_free(b1); |
|
142 ucx_buffer_free(b2); |
|
143 } |