test/buffer_tests.c

changeset 140
15f871f50bfd
parent 134
4d320dc3a7af
child 147
1aa598f36872
equal deleted inserted replaced
139:dddb9348ea42 140:15f871f50bfd
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "buffer_tests.h" 29 #include "buffer_tests.h"
30 #include "ucx/utils.h"
30 31
31 UCX_TEST(test_ucx_buffer_seektell) { 32 UCX_TEST(test_ucx_buffer_seektell) {
32 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); 33 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
33 b->size = 16; // less than capacity 34 b->size = 16; // less than capacity
34 int r; 35 int r;
290 291
291 ucx_buffer_free(dst); 292 ucx_buffer_free(dst);
292 ucx_buffer_free(src); 293 ucx_buffer_free(src);
293 } 294 }
294 295
295 UCX_TEST(test_ucx_buffer_generic_copy) { 296 UCX_TEST(test_ucx_stream_copy) {
296 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); 297 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
297 UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); 298 UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
298 299
299 UCX_TEST_BEGIN 300 UCX_TEST_BEGIN
300 301
301 ucx_buffer_write("01234567", 1, 8, b1); 302 ucx_buffer_write("01234567", 1, 8, b1);
302 ucx_buffer_write("abcdefgh", 1, 8, b1); 303 ucx_buffer_write("abcdefgh", 1, 8, b1);
303 UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); 304 UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
304 ucx_buffer_seek(b1, 0, SEEK_SET); 305 ucx_buffer_seek(b1, 0, SEEK_SET);
305 306
306 size_t ncp = ucx_buffer_copy(b1, b2, ucx_buffer_read, ucx_buffer_write); 307 size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
307 UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); 308 UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
308 UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); 309 UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
309 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, 310 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
310 "b1 and b2 have not the same content"); 311 "b1 and b2 have not the same content");
311 312
315 ucx_buffer_seek(b1, 0, SEEK_SET); 316 ucx_buffer_seek(b1, 0, SEEK_SET);
316 317
317 FILE *file = tmpfile(); 318 FILE *file = tmpfile();
318 UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); 319 UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
319 320
320 ncp = ucx_buffer_copy(b1, file, ucx_buffer_read, fwrite); 321 ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
321 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); 322 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
322 323
323 fseek(file, 0, SEEK_SET); 324 fseek(file, 0, SEEK_SET);
324 325
325 ncp = ucx_buffer_copy(file, b2, fread, ucx_buffer_write); 326 ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
326 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); 327 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
327 328
328 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, 329 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
329 "b1 and b2 content mismatch"); 330 "b1 and b2 content mismatch");
330 331
331 fclose(file); 332 fclose(file);
332 333
334 ucx_buffer_clear(b1);
335 ucx_buffer_seek(b2, 0, SEEK_SET);
336 ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
337 UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
338 UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
339 "content wrong after ncopy");
333 340
334 UCX_TEST_END 341 UCX_TEST_END
335 342
336 ucx_buffer_free(b1); 343 ucx_buffer_free(b1);
337 ucx_buffer_free(b2); 344 ucx_buffer_free(b2);

mercurial