| 1476 CX_TEST_ASSERT(buf.size == 8); |
1476 CX_TEST_ASSERT(buf.size == 8); |
| 1477 CX_TEST_ASSERT(target.pos == 8); |
1477 CX_TEST_ASSERT(target.pos == 8); |
| 1478 CX_TEST_ASSERT(target.size == 8); |
1478 CX_TEST_ASSERT(target.size == 8); |
| 1479 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz123", 8)); |
1479 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz123", 8)); |
| 1480 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); |
1480 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); |
| 1481 // final test, cannot write anything more |
1481 // final test - cannot write anything more |
| 1482 written = cxBufferWrite("baz", 1, 3, &buf); |
1482 written = cxBufferWrite("baz", 1, 3, &buf); |
| 1483 CX_TEST_ASSERT(written == 0); |
1483 CX_TEST_ASSERT(written == 0); |
| 1484 CX_TEST_ASSERT(buf.pos == 8); |
1484 CX_TEST_ASSERT(buf.pos == 8); |
| 1485 CX_TEST_ASSERT(buf.size == 8); |
1485 CX_TEST_ASSERT(buf.size == 8); |
| 1486 CX_TEST_ASSERT(target.pos == 8); |
1486 CX_TEST_ASSERT(target.pos == 8); |
| 1487 CX_TEST_ASSERT(target.size == 8); |
1487 CX_TEST_ASSERT(target.size == 8); |
| 1488 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz123", 8)); |
1488 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz123", 8)); |
| |
1489 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); |
| |
1490 } |
| |
1491 cxBufferDestroy(&buf); |
| |
1492 cxBufferDestroy(&target); |
| |
1493 } |
| |
1494 |
| |
1495 CX_TEST(test_buffer_write_flush_multibyte_target_full) { |
| |
1496 CxBuffer buf, target; |
| |
1497 cxBufferInit(&target, NULL, 12, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
| |
1498 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); |
| |
1499 CX_TEST_DO { |
| |
1500 CxBufferFlushConfig flush; |
| |
1501 flush.threshold = 12; |
| |
1502 flush.blksize = 8; |
| |
1503 flush.blkmax = 2; |
| |
1504 flush.target = ⌖ |
| |
1505 flush.wfunc = cxBufferWriteFunc; |
| |
1506 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); |
| |
1507 cxBufferPutString(&buf, "preparation"); |
| |
1508 size_t written = cxBufferWrite("teststring", 2, 5, &buf); |
| |
1509 CX_TEST_ASSERT(written == 5); |
| |
1510 CX_TEST_ASSERT(buf.pos == 11); |
| |
1511 CX_TEST_ASSERT(buf.size == 11); |
| |
1512 CX_TEST_ASSERT(target.pos == 10); |
| |
1513 CX_TEST_ASSERT(target.size == 10); |
| |
1514 CX_TEST_ASSERT(0 == memcmp(buf.space, "nteststring", 11)); |
| |
1515 CX_TEST_ASSERT(0 == memcmp(target.space, "preparatio", 10)); |
| |
1516 // pop the misaligned byte from the buffer |
| |
1517 cxBufferPop(&buf, 1, 1); |
| |
1518 // write three more items, but only one fits into the target and one more into the buffer |
| |
1519 written = cxBufferWrite("123456", 2, 3, &buf); |
| |
1520 CX_TEST_ASSERT(written == 2); |
| |
1521 CX_TEST_ASSERT(buf.pos == 12); |
| |
1522 CX_TEST_ASSERT(buf.size == 12); |
| |
1523 CX_TEST_ASSERT(target.pos == 12); |
| |
1524 CX_TEST_ASSERT(target.size == 12); |
| |
1525 CX_TEST_ASSERT(0 == memcmp(buf.space, "eststrin1234", 12)); |
| |
1526 CX_TEST_ASSERT(0 == memcmp(target.space, "preparationt", 12)); |
| |
1527 } |
| |
1528 cxBufferDestroy(&buf); |
| |
1529 cxBufferDestroy(&target); |
| |
1530 } |
| |
1531 |
| |
1532 CX_TEST(test_buffer_write_flush_at_threshold_target_full) { |
| |
1533 CxBuffer buf, target; |
| |
1534 // target does NOT auto-extend and can get completely full |
| |
1535 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
| |
1536 // source may auto-extend but flushes at a certain threshold |
| |
1537 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); |
| |
1538 cxBufferPutString(&buf, "prep"); |
| |
1539 CX_TEST_DO { |
| |
1540 CxBufferFlushConfig flush; |
| |
1541 flush.threshold = 16; |
| |
1542 flush.blksize = 4; |
| |
1543 flush.blkmax = 2; |
| |
1544 flush.target = ⌖ |
| |
1545 flush.wfunc = cxBufferWriteFunc; |
| |
1546 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); |
| |
1547 // step one - adding 6 bytes does not exceed the threshold |
| |
1548 size_t written = cxBufferWrite("foobar", 1, 6, &buf); |
| |
1549 CX_TEST_ASSERT(written == 6); |
| |
1550 CX_TEST_ASSERT(buf.pos == 10); |
| |
1551 CX_TEST_ASSERT(buf.size == 10); |
| |
1552 CX_TEST_ASSERT(target.pos == 0); |
| |
1553 CX_TEST_ASSERT(target.size == 0); |
| |
1554 CX_TEST_ASSERT(0 == memcmp(buf.space, "prepfoobar", 10)); |
| |
1555 // step two - adding 8 bytes is two too many, two blocks are flushed |
| |
1556 written = cxBufferWrite("12345678", 1, 8, &buf); |
| |
1557 CX_TEST_ASSERT(written == 8); |
| |
1558 CX_TEST_ASSERT(buf.pos == 10); |
| |
1559 CX_TEST_ASSERT(buf.size == 10); |
| |
1560 CX_TEST_ASSERT(target.pos == 8); |
| |
1561 CX_TEST_ASSERT(target.size == 8); |
| |
1562 CX_TEST_ASSERT(0 == memcmp(buf.space, "ar12345678", 10)); |
| |
1563 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); |
| |
1564 // step three - cannot flush more, but can write 6 more bytes |
| |
1565 written = cxBufferWrite("ABCDEFGH", 1, 8, &buf); |
| |
1566 CX_TEST_ASSERT(written == 6); |
| |
1567 CX_TEST_ASSERT(buf.pos == 16); |
| |
1568 CX_TEST_ASSERT(buf.size == 16); |
| |
1569 CX_TEST_ASSERT(target.pos == 8); |
| |
1570 CX_TEST_ASSERT(target.size == 8); |
| |
1571 CX_TEST_ASSERT(0 == memcmp(buf.space, "ar12345678ABCDEF", 16)); |
| |
1572 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); |
| |
1573 // final test - cannot write anything more |
| |
1574 written = cxBufferWrite("baz", 1, 3, &buf); |
| |
1575 CX_TEST_ASSERT(written == 0); |
| |
1576 CX_TEST_ASSERT(buf.pos == 16); |
| |
1577 CX_TEST_ASSERT(buf.size == 16); |
| |
1578 CX_TEST_ASSERT(target.pos == 8); |
| |
1579 CX_TEST_ASSERT(target.size == 8); |
| |
1580 CX_TEST_ASSERT(0 == memcmp(buf.space, "ar12345678ABCDEF", 16)); |
| 1489 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); |
1581 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); |
| 1490 } |
1582 } |
| 1491 cxBufferDestroy(&buf); |
1583 cxBufferDestroy(&buf); |
| 1492 cxBufferDestroy(&target); |
1584 cxBufferDestroy(&target); |
| 1493 } |
1585 } |
| 1698 cx_test_register(suite, test_buffer_write_size_overflow); |
1790 cx_test_register(suite, test_buffer_write_size_overflow); |
| 1699 cx_test_register(suite, test_buffer_write_capacity_overflow); |
1791 cx_test_register(suite, test_buffer_write_capacity_overflow); |
| 1700 cx_test_register(suite, test_buffer_write_only_overwrite); |
1792 cx_test_register(suite, test_buffer_write_only_overwrite); |
| 1701 cx_test_register(suite, test_buffer_write_flush_at_capacity); |
1793 cx_test_register(suite, test_buffer_write_flush_at_capacity); |
| 1702 cx_test_register(suite, test_buffer_write_flush_at_threshold); |
1794 cx_test_register(suite, test_buffer_write_flush_at_threshold); |
| |
1795 cx_test_register(suite, test_buffer_write_flush_at_threshold_target_full); |
| 1703 cx_test_register(suite, test_buffer_write_flush_rate_limited_and_buffer_too_small); |
1796 cx_test_register(suite, test_buffer_write_flush_rate_limited_and_buffer_too_small); |
| 1704 cx_test_register(suite, test_buffer_write_flush_multibyte); |
1797 cx_test_register(suite, test_buffer_write_flush_multibyte); |
| 1705 cx_test_register(suite, test_buffer_write_flush_misaligned); |
1798 cx_test_register(suite, test_buffer_write_flush_misaligned); |
| 1706 cx_test_register(suite, test_buffer_write_flush_target_full); |
1799 cx_test_register(suite, test_buffer_write_flush_target_full); |
| |
1800 cx_test_register(suite, test_buffer_write_flush_multibyte_target_full); |
| 1707 cx_test_register(suite, test_buffer_flush); |
1801 cx_test_register(suite, test_buffer_flush); |
| 1708 cx_test_register(suite, test_buffer_get); |
1802 cx_test_register(suite, test_buffer_get); |
| 1709 cx_test_register(suite, test_buffer_get_eof); |
1803 cx_test_register(suite, test_buffer_get_eof); |
| 1710 cx_test_register(suite, test_buffer_read); |
1804 cx_test_register(suite, test_buffer_read); |
| 1711 cx_test_register(suite, test_buffer_read_oob); |
1805 cx_test_register(suite, test_buffer_read_oob); |