tests/test_buffer.c

changeset 1024
8f99f6c28bd3
parent 1007
81b2986d2b04
child 1026
ca20f9ffcb62
equal deleted inserted replaced
1017:b0098854071f 1024:8f99f6c28bd3
213 CX_TEST_ASSERT(buf.pos == 0); 213 CX_TEST_ASSERT(buf.pos == 0);
214 } 214 }
215 cxBufferDestroy(&buf); 215 cxBufferDestroy(&buf);
216 } 216 }
217 217
218 CX_TEST(test_buffer_clear_copy_on_write) {
219 char space[16];
220 strcpy(space, "clear test");
221 CxBuffer buf;
222 cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_COPY_ON_WRITE);
223 CX_TEST_DO {
224 buf.size = 5;
225 buf.pos = 3;
226 cxBufferClear(&buf);
227 CX_TEST_ASSERT(0 == memcmp(space, "clear test", 10));
228 CX_TEST_ASSERT(buf.size == 0);
229 CX_TEST_ASSERT(buf.pos == 0);
230 }
231 cxBufferDestroy(&buf);
232 }
233
218 CX_TEST(test_buffer_reset) { 234 CX_TEST(test_buffer_reset) {
219 char space[16]; 235 char space[16];
220 strcpy(space, "reset test"); 236 strcpy(space, "reset test");
221 CxBuffer buf; 237 CxBuffer buf;
222 cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 238 cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
504 CX_TEST_ASSERT(memcmp(buf.space, "stst____XXXXXXXX", 16) == 0); 520 CX_TEST_ASSERT(memcmp(buf.space, "stst____XXXXXXXX", 16) == 0);
505 TEST_BUFFER_SHIFT_TEARDOWN(buf); 521 TEST_BUFFER_SHIFT_TEARDOWN(buf);
506 } 522 }
507 } 523 }
508 524
525 CX_TEST(test_buffer_shift_left_copy_on_write) {
526 TEST_BUFFER_SHIFT_SETUP(buf);
527 buf.flags |= CX_BUFFER_COPY_ON_WRITE;
528 char *original = buf.space;
529 CX_TEST_DO {
530 int ret = cxBufferShiftLeft(&buf, 2);
531 CX_TEST_ASSERT(0 == (buf.flags & CX_BUFFER_COPY_ON_WRITE));
532 CX_TEST_ASSERT(0 != (buf.flags & CX_BUFFER_FREE_CONTENTS));
533 CX_TEST_ASSERT(ret == 0);
534 CX_TEST_ASSERT(buf.pos == 2);
535 CX_TEST_ASSERT(buf.size == 2);
536 CX_TEST_ASSERT(memcmp(original, "test____XXXXXXXX", 16) == 0);
537 CX_TEST_ASSERT(memcmp(buf.space, "st", 2) == 0);
538 cxFree(buf.allocator, original);
539 TEST_BUFFER_SHIFT_TEARDOWN(buf);
540 }
541 }
542
509 CX_TEST(test_buffer_shift_right_zero) { 543 CX_TEST(test_buffer_shift_right_zero) {
510 TEST_BUFFER_SHIFT_SETUP(buf); 544 TEST_BUFFER_SHIFT_SETUP(buf);
511 CX_TEST_DO { 545 CX_TEST_DO {
512 int ret = cxBufferShiftRight(&buf, 0); 546 int ret = cxBufferShiftRight(&buf, 0);
513 CX_TEST_ASSERT(ret == 0); 547 CX_TEST_ASSERT(ret == 0);
578 int ret = cxBufferShift(&buf, 2); 612 int ret = cxBufferShift(&buf, 2);
579 CX_TEST_ASSERT(ret == 0); 613 CX_TEST_ASSERT(ret == 0);
580 CX_TEST_ASSERT(buf.pos == 5); 614 CX_TEST_ASSERT(buf.pos == 5);
581 CX_TEST_ASSERT(buf.size == 6); 615 CX_TEST_ASSERT(buf.size == 6);
582 CX_TEST_ASSERT(memcmp(buf.space, "tetest__XXXXXXXX", 16) == 0); 616 CX_TEST_ASSERT(memcmp(buf.space, "tetest__XXXXXXXX", 16) == 0);
617 TEST_BUFFER_SHIFT_TEARDOWN(buf);
618 }
619 }
620
621 CX_TEST(test_buffer_shift_right_copy_on_write) {
622 TEST_BUFFER_SHIFT_SETUP(buf);
623 buf.flags |= CX_BUFFER_COPY_ON_WRITE;
624 char *original = buf.space;
625 CX_TEST_DO {
626 int ret = cxBufferShiftRight(&buf, 3);
627 CX_TEST_ASSERT(0 == (buf.flags & CX_BUFFER_COPY_ON_WRITE));
628 CX_TEST_ASSERT(0 != (buf.flags & CX_BUFFER_FREE_CONTENTS));
629 CX_TEST_ASSERT(ret == 0);
630 CX_TEST_ASSERT(buf.pos == 7);
631 CX_TEST_ASSERT(buf.size == 7);
632 CX_TEST_ASSERT(memcmp(original, "test____XXXXXXXX", 16) == 0);
633 CX_TEST_ASSERT(memcmp(buf.space, "testest", 7) == 0);
634 cxFree(buf.allocator, original);
583 TEST_BUFFER_SHIFT_TEARDOWN(buf); 635 TEST_BUFFER_SHIFT_TEARDOWN(buf);
584 } 636 }
585 } 637 }
586 638
587 static size_t mock_write_limited_rate( 639 static size_t mock_write_limited_rate(
655 CX_TEST_ASSERT(0 == memcmp(buf.space, "preptesting", 11)); 707 CX_TEST_ASSERT(0 == memcmp(buf.space, "preptesting", 11));
656 } 708 }
657 cxBufferDestroy(&buf); 709 cxBufferDestroy(&buf);
658 } 710 }
659 711
712 CX_TEST(test_buffer_write_copy_on_write) {
713 CxBuffer buf;
714 char original[16] = "preparedXXXXXXX\0";
715 cxBufferInit(&buf, original, 16, cxDefaultAllocator, CX_BUFFER_COPY_ON_WRITE);
716 buf.capacity = 8;
717 buf.size = 8;
718 buf.pos = 0;
719 const char *data = "testing";
720 CX_TEST_DO {
721 size_t written = cxBufferWrite(data, 1, 7, &buf);
722 CX_TEST_ASSERT(written == 7);
723 CX_TEST_ASSERT(buf.size == 8);
724 CX_TEST_ASSERT(buf.pos == 7);
725 CX_TEST_ASSERT(buf.capacity == 8);
726 CX_TEST_ASSERT(0 == memcmp(buf.space, "testingd", 8));
727 CX_TEST_ASSERT(0 == memcmp(original, "preparedXXXXXXX\0", 16));
728 CX_TEST_ASSERT(0 == (buf.flags & CX_BUFFER_COPY_ON_WRITE));
729 CX_TEST_ASSERT(0 != (buf.flags & CX_BUFFER_FREE_CONTENTS));
730 }
731 cxBufferDestroy(&buf);
732 }
733
660 CX_TEST(test_buffer_write_multibyte_fit) { 734 CX_TEST(test_buffer_write_multibyte_fit) {
661 CxBuffer buf; 735 CxBuffer buf;
662 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 736 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
663 memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16); 737 memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
664 buf.capacity = 8; 738 buf.capacity = 8;
768 CX_TEST_ASSERT(0 == memcmp(buf.space, "prep\0\0\0\0a", 9)); 842 CX_TEST_ASSERT(0 == memcmp(buf.space, "prep\0\0\0\0a", 9));
769 } 843 }
770 cxBufferDestroy(&buf); 844 cxBufferDestroy(&buf);
771 } 845 }
772 846
847 CX_TEST(test_buffer_put_copy_on_write) {
848 CxBuffer buf;
849 char original[16] = "preparedXXXXXXX\0";
850 cxBufferInit(&buf, original, 16, cxDefaultAllocator, CX_BUFFER_COPY_ON_WRITE);
851 buf.capacity = 8;
852 buf.size = 8;
853 buf.pos = 8;
854 CX_TEST_DO {
855 int c = cxBufferPut(&buf, 0x200 | 'a');
856 CX_TEST_ASSERT(c == EOF);
857 CX_TEST_ASSERT(buf.size == 8);
858 CX_TEST_ASSERT(buf.pos == 8);
859 CX_TEST_ASSERT(buf.capacity == 8);
860 CX_TEST_ASSERT(0 == memcmp(buf.space, "prepared", 8));
861 // discarded, no write happend!
862 CX_TEST_ASSERT(original == buf.space);
863 CX_TEST_ASSERT(0 != (buf.flags & CX_BUFFER_COPY_ON_WRITE));
864 CX_TEST_ASSERT(0 == (buf.flags & CX_BUFFER_FREE_CONTENTS));
865 // now actually write somewhere
866 buf.pos = 2;
867 c = cxBufferPut(&buf, 0x200 | 'a');
868 CX_TEST_ASSERT(c == 'a');
869 CX_TEST_ASSERT(buf.size == 8);
870 CX_TEST_ASSERT(buf.pos == 3);
871 CX_TEST_ASSERT(buf.capacity == 8);
872 CX_TEST_ASSERT(0 == memcmp(buf.space, "prapared", 8));
873 CX_TEST_ASSERT(original != buf.space);
874 CX_TEST_ASSERT(0 == memcmp(original, "preparedXXXXXXX\0", 16));
875 CX_TEST_ASSERT(0 == (buf.flags & CX_BUFFER_COPY_ON_WRITE));
876 CX_TEST_ASSERT(0 != (buf.flags & CX_BUFFER_FREE_CONTENTS));
877 }
878 cxBufferDestroy(&buf);
879 }
880
773 CX_TEST(test_buffer_put_string_fit) { 881 CX_TEST(test_buffer_put_string_fit) {
774 CxBuffer buf; 882 CxBuffer buf;
775 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 883 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
776 memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16); 884 memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
777 buf.capacity = 8; 885 buf.capacity = 8;
819 CX_TEST_ASSERT(written == 7); 927 CX_TEST_ASSERT(written == 7);
820 CX_TEST_ASSERT(buf.size == 11); 928 CX_TEST_ASSERT(buf.size == 11);
821 CX_TEST_ASSERT(buf.pos == 11); 929 CX_TEST_ASSERT(buf.pos == 11);
822 CX_TEST_ASSERT(buf.capacity >= 11); 930 CX_TEST_ASSERT(buf.capacity >= 11);
823 CX_TEST_ASSERT(0 == memcmp(buf.space, "preptesting", 11)); 931 CX_TEST_ASSERT(0 == memcmp(buf.space, "preptesting", 11));
932 }
933 cxBufferDestroy(&buf);
934 }
935
936 CX_TEST(test_buffer_put_string_extend_copy_on_write) {
937 CxBuffer buf;
938 char original[16] = "preparedXXXXXXX\0";
939 cxBufferInit(&buf, original, 16, cxDefaultAllocator, CX_BUFFER_COPY_ON_WRITE);
940 buf.capacity = 8;
941 buf.size = 8;
942 buf.pos = 4;
943 buf.flags |= CX_BUFFER_AUTO_EXTEND;
944 const char *data = "testing";
945 CX_TEST_DO {
946 size_t written = cxBufferPutString(&buf, data);
947 CX_TEST_ASSERT(written == 7);
948 CX_TEST_ASSERT(buf.size == 11);
949 CX_TEST_ASSERT(buf.pos == 11);
950 CX_TEST_ASSERT(buf.capacity >= 11);
951 CX_TEST_ASSERT(0 == memcmp(buf.space, "preptesting", 11));
952 CX_TEST_ASSERT(original != buf.space);
953 CX_TEST_ASSERT(0 == memcmp(original, "preparedXXXXXXX\0", 16));
954 CX_TEST_ASSERT(0 == (buf.flags & CX_BUFFER_COPY_ON_WRITE));
955 CX_TEST_ASSERT(0 != (buf.flags & CX_BUFFER_FREE_CONTENTS));
824 } 956 }
825 cxBufferDestroy(&buf); 957 cxBufferDestroy(&buf);
826 } 958 }
827 959
828 CX_TEST(test_buffer_terminate) { 960 CX_TEST(test_buffer_terminate) {
1111 cx_test_register(suite, test_buffer_init_fresh_space); 1243 cx_test_register(suite, test_buffer_init_fresh_space);
1112 cx_test_register(suite, test_buffer_init_on_heap); 1244 cx_test_register(suite, test_buffer_init_on_heap);
1113 cx_test_register(suite, test_buffer_minimum_capacity_sufficient); 1245 cx_test_register(suite, test_buffer_minimum_capacity_sufficient);
1114 cx_test_register(suite, test_buffer_minimum_capacity_extend); 1246 cx_test_register(suite, test_buffer_minimum_capacity_extend);
1115 cx_test_register(suite, test_buffer_clear); 1247 cx_test_register(suite, test_buffer_clear);
1248 cx_test_register(suite, test_buffer_clear_copy_on_write);
1116 cx_test_register(suite, test_buffer_reset); 1249 cx_test_register(suite, test_buffer_reset);
1117 cx_test_register(suite, test_buffer_seek_set_zero); 1250 cx_test_register(suite, test_buffer_seek_set_zero);
1118 cx_test_register(suite, test_buffer_seek_set_valid); 1251 cx_test_register(suite, test_buffer_seek_set_valid);
1119 cx_test_register(suite, test_buffer_seek_set_invalid); 1252 cx_test_register(suite, test_buffer_seek_set_invalid);
1120 cx_test_register(suite, test_buffer_seek_cur_zero); 1253 cx_test_register(suite, test_buffer_seek_cur_zero);
1132 cx_test_register(suite, test_buffer_shift_left_zero_offset_interface); 1265 cx_test_register(suite, test_buffer_shift_left_zero_offset_interface);
1133 cx_test_register(suite, test_buffer_shift_left_standard); 1266 cx_test_register(suite, test_buffer_shift_left_standard);
1134 cx_test_register(suite, test_buffer_shift_left_overshift); 1267 cx_test_register(suite, test_buffer_shift_left_overshift);
1135 cx_test_register(suite, test_buffer_shift_left_overshift_pos_only); 1268 cx_test_register(suite, test_buffer_shift_left_overshift_pos_only);
1136 cx_test_register(suite, test_buffer_shift_left_offset_interface); 1269 cx_test_register(suite, test_buffer_shift_left_offset_interface);
1270 cx_test_register(suite, test_buffer_shift_left_copy_on_write);
1137 cx_test_register(suite, test_buffer_shift_right_zero); 1271 cx_test_register(suite, test_buffer_shift_right_zero);
1138 cx_test_register(suite, test_buffer_shift_right_zero_offset_interface); 1272 cx_test_register(suite, test_buffer_shift_right_zero_offset_interface);
1139 cx_test_register(suite, test_buffer_shift_right_standard); 1273 cx_test_register(suite, test_buffer_shift_right_standard);
1140 cx_test_register(suite, test_buffer_shift_right_overshift_discard); 1274 cx_test_register(suite, test_buffer_shift_right_overshift_discard);
1141 cx_test_register(suite, test_buffer_shift_right_overshift_extend); 1275 cx_test_register(suite, test_buffer_shift_right_overshift_extend);
1142 cx_test_register(suite, test_buffer_shift_right_offset_interface); 1276 cx_test_register(suite, test_buffer_shift_right_offset_interface);
1277 cx_test_register(suite, test_buffer_shift_right_copy_on_write);
1143 cx_test_register(suite, test_buffer_write_size_one_fit); 1278 cx_test_register(suite, test_buffer_write_size_one_fit);
1144 cx_test_register(suite, test_buffer_write_size_one_discard); 1279 cx_test_register(suite, test_buffer_write_size_one_discard);
1145 cx_test_register(suite, test_buffer_write_size_one_extend); 1280 cx_test_register(suite, test_buffer_write_size_one_extend);
1146 cx_test_register(suite, test_buffer_write_multibyte_fit); 1281 cx_test_register(suite, test_buffer_write_multibyte_fit);
1147 cx_test_register(suite, test_buffer_write_multibyte_discard); 1282 cx_test_register(suite, test_buffer_write_multibyte_discard);
1148 cx_test_register(suite, test_buffer_write_multibyte_extend); 1283 cx_test_register(suite, test_buffer_write_multibyte_extend);
1284 cx_test_register(suite, test_buffer_write_copy_on_write);
1149 cx_test_register(suite, test_buffer_put_fit); 1285 cx_test_register(suite, test_buffer_put_fit);
1150 cx_test_register(suite, test_buffer_put_discard); 1286 cx_test_register(suite, test_buffer_put_discard);
1151 cx_test_register(suite, test_buffer_put_extend); 1287 cx_test_register(suite, test_buffer_put_extend);
1288 cx_test_register(suite, test_buffer_put_copy_on_write);
1152 cx_test_register(suite, test_buffer_put_string_fit); 1289 cx_test_register(suite, test_buffer_put_string_fit);
1153 cx_test_register(suite, test_buffer_put_string_discard); 1290 cx_test_register(suite, test_buffer_put_string_discard);
1154 cx_test_register(suite, test_buffer_put_string_extend); 1291 cx_test_register(suite, test_buffer_put_string_extend);
1292 cx_test_register(suite, test_buffer_put_string_extend_copy_on_write);
1155 cx_test_register(suite, test_buffer_terminate); 1293 cx_test_register(suite, test_buffer_terminate);
1156 cx_test_register(suite, test_buffer_write_size_overflow); 1294 cx_test_register(suite, test_buffer_write_size_overflow);
1157 cx_test_register(suite, test_buffer_write_capacity_overflow); 1295 cx_test_register(suite, test_buffer_write_capacity_overflow);
1158 cx_test_register(suite, test_buffer_write_only_overwrite); 1296 cx_test_register(suite, test_buffer_write_only_overwrite);
1159 cx_test_register(suite, test_buffer_write_flush_at_capacity); 1297 cx_test_register(suite, test_buffer_write_flush_at_capacity);

mercurial