tests/test_buffer.c

changeset 792
3ca984931e1d
parent 789
9b2f5661bebd
child 793
db1c8dfe403a
equal deleted inserted replaced
791:945ee9bf2dd1 792:3ca984931e1d
387 CX_TEST_ASSERT(buf.pos == 3); 387 CX_TEST_ASSERT(buf.pos == 3);
388 } 388 }
389 cxBufferDestroy(&buf); 389 cxBufferDestroy(&buf);
390 } 390 }
391 391
392 CX_TEST(test_buffer_eof_reached) {
393 CxBuffer buf;
394 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
395 buf.size = buf.pos = 3;
396 CX_TEST_DO {
397 CX_TEST_ASSERT(cxBufferEof(&buf));
398 buf.pos = buf.size - 1;
399 CX_TEST_ASSERT(!cxBufferEof(&buf));
400 cxBufferPut(&buf, 'a');
401 CX_TEST_ASSERT(cxBufferEof(&buf));
402 }
403 cxBufferDestroy(&buf);
404 }
405
406 CX_TEST(test_buffer_eof_not_reached) {
407 CxBuffer buf;
408 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
409 buf.size = 6;
410 CX_TEST_DO {
411 buf.pos = buf.size - 1;
412 CX_TEST_ASSERT(!cxBufferEof(&buf));
413 buf.pos = 0;
414 cxBufferWrite("test", 1, 5, &buf);
415 CX_TEST_ASSERT(!cxBufferEof(&buf));
416 }
417 cxBufferDestroy(&buf);
418 }
419
420 #define TEST_BUFFER_SHIFT_SETUP(buf) \
421 CxTestingAllocator talloc; \
422 cx_testing_allocator_init(&talloc); \
423 CxAllocator *alloc = &talloc.base; \
424 CxBuffer buf; \
425 cxBufferInit(&buf, NULL, 16, alloc, CX_BUFFER_DEFAULT); \
426 memcpy(buf.space, "test____XXXXXXXX", 16); \
427 buf.capacity = 8; \
428 buf.pos = 4; \
429 buf.size = 4
430 #define TEST_BUFFER_SHIFT_TEARDOWN(buf) \
431 cxBufferDestroy(&buf); \
432 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); \
433 cx_testing_allocator_destroy(&talloc);
434
435
436 CX_TEST(test_buffer_shift_left_zero) {
437 TEST_BUFFER_SHIFT_SETUP(buf);
438 CX_TEST_DO {
439 int ret = cxBufferShiftLeft(&buf, 0);
440 CX_TEST_ASSERT(ret == 0);
441 CX_TEST_ASSERT(buf.pos == 4);
442 CX_TEST_ASSERT(buf.size == 4);
443 CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
444 TEST_BUFFER_SHIFT_TEARDOWN(buf);
445 }
446 }
447
448 CX_TEST(test_buffer_shift_left_zero_offset_interface) {
449 TEST_BUFFER_SHIFT_SETUP(buf);
450 CX_TEST_DO {
451 int ret = cxBufferShift(&buf, -0);
452 CX_TEST_ASSERT(ret == 0);
453 CX_TEST_ASSERT(buf.pos == 4);
454 CX_TEST_ASSERT(buf.size == 4);
455 CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
456 TEST_BUFFER_SHIFT_TEARDOWN(buf);
457 }
458 }
459
460 CX_TEST(test_buffer_shift_left_standard) {
461 TEST_BUFFER_SHIFT_SETUP(buf);
462 CX_TEST_DO {
463 int ret = cxBufferShiftLeft(&buf, 2);
464 CX_TEST_ASSERT(ret == 0);
465 CX_TEST_ASSERT(buf.pos == 2);
466 CX_TEST_ASSERT(buf.size == 2);
467 CX_TEST_ASSERT(memcmp(buf.space, "stst____XXXXXXXX", 16) == 0);
468 TEST_BUFFER_SHIFT_TEARDOWN(buf);
469 }
470 }
471
472 CX_TEST(test_buffer_shift_left_overshift) {
473 TEST_BUFFER_SHIFT_SETUP(buf);
474 CX_TEST_DO {
475 int ret = cxBufferShiftLeft(&buf, 6);
476 CX_TEST_ASSERT(ret == 0);
477 CX_TEST_ASSERT(buf.pos == 0);
478 CX_TEST_ASSERT(buf.size == 0);
479 CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
480 TEST_BUFFER_SHIFT_TEARDOWN(buf);
481 }
482 }
483
484 CX_TEST(test_buffer_shift_left_overshift_pos_only) {
485 TEST_BUFFER_SHIFT_SETUP(buf);
486 buf.pos = 2;
487 CX_TEST_DO {
488 int ret = cxBufferShiftLeft(&buf, 3);
489 CX_TEST_ASSERT(ret == 0);
490 CX_TEST_ASSERT(buf.pos == 0);
491 CX_TEST_ASSERT(buf.size == 1);
492 CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
493 TEST_BUFFER_SHIFT_TEARDOWN(buf);
494 }
495 }
496
497 CX_TEST(test_buffer_shift_left_offset_interface) {
498 TEST_BUFFER_SHIFT_SETUP(buf);
499 buf.pos = 3;
500 CX_TEST_DO {
501 int ret = cxBufferShift(&buf, -2);
502 CX_TEST_ASSERT(ret == 0);
503 CX_TEST_ASSERT(buf.pos == 1);
504 CX_TEST_ASSERT(buf.size == 2);
505 CX_TEST_ASSERT(memcmp(buf.space, "stst____XXXXXXXX", 16) == 0);
506 TEST_BUFFER_SHIFT_TEARDOWN(buf);
507 }
508 }
509
510 CX_TEST(test_buffer_shift_right_zero) {
511 TEST_BUFFER_SHIFT_SETUP(buf);
512 CX_TEST_DO {
513 int ret = cxBufferShiftRight(&buf, 0);
514 CX_TEST_ASSERT(ret == 0);
515 CX_TEST_ASSERT(buf.pos == 4);
516 CX_TEST_ASSERT(buf.size == 4);
517 CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
518 TEST_BUFFER_SHIFT_TEARDOWN(buf);
519 }
520 }
521
522 CX_TEST(test_buffer_shift_right_zero_offset_interface) {
523 TEST_BUFFER_SHIFT_SETUP(buf);
524 CX_TEST_DO {
525 int ret = cxBufferShift(&buf, +0);
526 CX_TEST_ASSERT(ret == 0);
527 CX_TEST_ASSERT(buf.pos == 4);
528 CX_TEST_ASSERT(buf.size == 4);
529 CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
530 TEST_BUFFER_SHIFT_TEARDOWN(buf);
531 }
532 }
533
534 CX_TEST(test_buffer_shift_right_standard) {
535 TEST_BUFFER_SHIFT_SETUP(buf);
536 CX_TEST_DO {
537 int ret = cxBufferShiftRight(&buf, 3);
538 CX_TEST_ASSERT(ret == 0);
539 CX_TEST_ASSERT(buf.pos == 7);
540 CX_TEST_ASSERT(buf.size == 7);
541 CX_TEST_ASSERT(memcmp(buf.space, "testest_XXXXXXXX", 16) == 0);
542 TEST_BUFFER_SHIFT_TEARDOWN(buf);
543 }
544 }
545
546 CX_TEST(test_buffer_shift_right_overshift_discard) {
547 TEST_BUFFER_SHIFT_SETUP(buf);
548 CX_TEST_DO {
549 int ret = cxBufferShiftRight(&buf, 6);
550 CX_TEST_ASSERT(ret == 0);
551 CX_TEST_ASSERT(buf.pos == 8);
552 CX_TEST_ASSERT(buf.size == 8);
553 CX_TEST_ASSERT(buf.capacity == 8);
554 CX_TEST_ASSERT(memcmp(buf.space, "test__teXXXXXXXX", 16) == 0);
555 TEST_BUFFER_SHIFT_TEARDOWN(buf);
556 }
557 }
558
559 CX_TEST(test_buffer_shift_right_overshift_extend) {
560 TEST_BUFFER_SHIFT_SETUP(buf);
561 buf.flags |= CX_BUFFER_AUTO_EXTEND;
562 CX_TEST_DO {
563 int ret = cxBufferShiftRight(&buf, 6);
564 CX_TEST_ASSERT(ret == 0);
565 CX_TEST_ASSERT(buf.pos == 10);
566 CX_TEST_ASSERT(buf.size == 10);
567 CX_TEST_ASSERT(buf.capacity >= 10);
568 // cannot assert more than 10 bytes because
569 // the buffer was required to reallocate the space
570 CX_TEST_ASSERT(memcmp(buf.space, "test__test", 10) == 0);
571 TEST_BUFFER_SHIFT_TEARDOWN(buf);
572 }
573 }
574
575 CX_TEST(test_buffer_shift_right_offset_interface) {
576 TEST_BUFFER_SHIFT_SETUP(buf);
577 buf.pos = 3;
578 CX_TEST_DO {
579 int ret = cxBufferShift(&buf, 2);
580 CX_TEST_ASSERT(ret == 0);
581 CX_TEST_ASSERT(buf.pos == 5);
582 CX_TEST_ASSERT(buf.size == 6);
583 CX_TEST_ASSERT(memcmp(buf.space, "tetest__XXXXXXXX", 16) == 0);
584 TEST_BUFFER_SHIFT_TEARDOWN(buf);
585 }
586 }
587
392 CxTestSuite *cx_test_suite_buffer(void) { 588 CxTestSuite *cx_test_suite_buffer(void) {
393 CxTestSuite *suite = cx_test_suite_new("buffer"); 589 CxTestSuite *suite = cx_test_suite_new("buffer");
394 590
395 cx_test_register(suite, test_buffer_init_wrap_space); 591 cx_test_register(suite, test_buffer_init_wrap_space);
396 cx_test_register(suite, test_buffer_init_wrap_space_auto_extend); 592 cx_test_register(suite, test_buffer_init_wrap_space_auto_extend);
411 cx_test_register(suite, test_buffer_seek_cur_invalid_negative); 607 cx_test_register(suite, test_buffer_seek_cur_invalid_negative);
412 cx_test_register(suite, test_buffer_seek_end_zero); 608 cx_test_register(suite, test_buffer_seek_end_zero);
413 cx_test_register(suite, test_buffer_seek_end_valid); 609 cx_test_register(suite, test_buffer_seek_end_valid);
414 cx_test_register(suite, test_buffer_seek_end_invalid); 610 cx_test_register(suite, test_buffer_seek_end_invalid);
415 cx_test_register(suite, test_buffer_seek_whence_invalid); 611 cx_test_register(suite, test_buffer_seek_whence_invalid);
612 cx_test_register(suite, test_buffer_eof_reached);
613 cx_test_register(suite, test_buffer_eof_not_reached);
614 cx_test_register(suite, test_buffer_shift_left_zero);
615 cx_test_register(suite, test_buffer_shift_left_zero_offset_interface);
616 cx_test_register(suite, test_buffer_shift_left_standard);
617 cx_test_register(suite, test_buffer_shift_left_overshift);
618 cx_test_register(suite, test_buffer_shift_left_overshift_pos_only);
619 cx_test_register(suite, test_buffer_shift_left_offset_interface);
620 cx_test_register(suite, test_buffer_shift_right_zero);
621 cx_test_register(suite, test_buffer_shift_right_zero_offset_interface);
622 cx_test_register(suite, test_buffer_shift_right_standard);
623 cx_test_register(suite, test_buffer_shift_right_overshift_discard);
624 cx_test_register(suite, test_buffer_shift_right_overshift_extend);
625 cx_test_register(suite, test_buffer_shift_right_offset_interface);
416 626
417 return suite; 627 return suite;
418 } 628 }

mercurial