tests/test_buffer.c

changeset 1135
f79415d974d3
parent 1131
644f77f903b1
equal deleted inserted replaced
1134:60edcd57d54c 1135:f79415d974d3
1273 // this test case tests that flushing works correctly even when the 1273 // this test case tests that flushing works correctly even when the
1274 // contents in the buffer are currently not aligned with the item size 1274 // contents in the buffer are currently not aligned with the item size
1275 CxBuffer buf, target; 1275 CxBuffer buf, target;
1276 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); 1276 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
1277 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1277 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT);
1278 memset(buf.space, 0, 8);
1279 cxBufferPutString(&buf, "prep"); 1278 cxBufferPutString(&buf, "prep");
1280 CX_TEST_DO { 1279 CX_TEST_DO {
1281 CxBufferFlushConfig flush; 1280 CxBufferFlushConfig flush;
1282 flush.threshold = 0; 1281 flush.threshold = 0;
1283 flush.blksize = 32; 1282 flush.blksize = 32;
1293 CX_TEST_ASSERT(target.pos == 3); 1292 CX_TEST_ASSERT(target.pos == 3);
1294 CX_TEST_ASSERT(target.size == 3); 1293 CX_TEST_ASSERT(target.size == 3);
1295 CX_TEST_ASSERT(0 == memcmp(buf.space, "pfoobar", 7)); 1294 CX_TEST_ASSERT(0 == memcmp(buf.space, "pfoobar", 7));
1296 CX_TEST_ASSERT(0 == memcmp(target.space, "pre", 3)); 1295 CX_TEST_ASSERT(0 == memcmp(target.space, "pre", 3));
1297 // second case: string does not fit, relaying not possible due to misalignment 1296 // second case: string does not fit, relaying not possible due to misalignment
1297 // string will be truncated (two items fit into buffer, the third does not)
1298 written = cxBufferWrite("bazfoobar", 3, 3, &buf); 1298 written = cxBufferWrite("bazfoobar", 3, 3, &buf);
1299 CX_TEST_ASSERT(written == 0); 1299 CX_TEST_ASSERT(written == 2);
1300 CX_TEST_ASSERT(buf.pos == 1); 1300 CX_TEST_ASSERT(buf.pos == 7);
1301 CX_TEST_ASSERT(buf.size == 1); 1301 CX_TEST_ASSERT(buf.size == 7);
1302 CX_TEST_ASSERT(target.pos == 9); 1302 CX_TEST_ASSERT(target.pos == 9);
1303 CX_TEST_ASSERT(target.size == 9); 1303 CX_TEST_ASSERT(target.size == 9);
1304 CX_TEST_ASSERT(0 == memcmp(buf.space, "r", 1)); 1304 CX_TEST_ASSERT(0 == memcmp(buf.space, "rbazfoo", 7));
1305 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfooba", 9)); 1305 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfooba", 9));
1306 }
1307 cxBufferDestroy(&buf);
1308 cxBufferDestroy(&target);
1309 }
1310
1311 CX_TEST(test_buffer_write_flush_target_full) {
1312 CxBuffer buf, target;
1313 // target does NOT auto-extend and can get completely full
1314 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT);
1315 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT);
1316 cxBufferPutString(&buf, "prep");
1317 CX_TEST_DO {
1318 CxBufferFlushConfig flush;
1319 flush.threshold = 0;
1320 flush.blksize = 32;
1321 flush.blkmax = 1;
1322 flush.target = ⌖
1323 flush.wfunc = cxBufferWriteFunc;
1324 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush));
1325 // step one - flush 4 existing bytes, write 6 new bytes
1326 size_t written = cxBufferWrite("foobar", 1, 6, &buf);
1327 CX_TEST_ASSERT(written == 6);
1328 CX_TEST_ASSERT(buf.pos == 6);
1329 CX_TEST_ASSERT(buf.size == 6);
1330 CX_TEST_ASSERT(target.pos == 4);
1331 CX_TEST_ASSERT(target.size == 4);
1332 CX_TEST_ASSERT(0 == memcmp(buf.space, "foobar", 6));
1333 CX_TEST_ASSERT(0 == memcmp(target.space, "prep", 4));
1334 // step two - can only flush 4 more bytes, but rest fits into buffer
1335 written = cxBufferWrite("xyz", 1, 3, &buf);
1336 CX_TEST_ASSERT(written == 3);
1337 CX_TEST_ASSERT(buf.pos == 5);
1338 CX_TEST_ASSERT(buf.size == 5);
1339 CX_TEST_ASSERT(target.pos == 8);
1340 CX_TEST_ASSERT(target.size == 8);
1341 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz", 5));
1342 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8));
1343 // step three - cannot flush more, but can write 3 more bytes
1344 written = cxBufferWrite("123456", 1, 6, &buf);
1345 CX_TEST_ASSERT(written == 3);
1346 CX_TEST_ASSERT(buf.pos == 8);
1347 CX_TEST_ASSERT(buf.size == 8);
1348 CX_TEST_ASSERT(target.pos == 8);
1349 CX_TEST_ASSERT(target.size == 8);
1350 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz123", 8));
1351 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8));
1352 // final test, cannot write anything more
1353 written = cxBufferWrite("baz", 1, 3, &buf);
1354 CX_TEST_ASSERT(written == 0);
1355 CX_TEST_ASSERT(buf.pos == 8);
1356 CX_TEST_ASSERT(buf.size == 8);
1357 CX_TEST_ASSERT(target.pos == 8);
1358 CX_TEST_ASSERT(target.size == 8);
1359 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz123", 8));
1360 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8));
1306 } 1361 }
1307 cxBufferDestroy(&buf); 1362 cxBufferDestroy(&buf);
1308 cxBufferDestroy(&target); 1363 cxBufferDestroy(&target);
1309 } 1364 }
1310 1365
1506 cx_test_register(suite, test_buffer_write_flush_at_capacity); 1561 cx_test_register(suite, test_buffer_write_flush_at_capacity);
1507 cx_test_register(suite, test_buffer_write_flush_at_threshold); 1562 cx_test_register(suite, test_buffer_write_flush_at_threshold);
1508 cx_test_register(suite, test_buffer_write_flush_rate_limited_and_buffer_too_small); 1563 cx_test_register(suite, test_buffer_write_flush_rate_limited_and_buffer_too_small);
1509 cx_test_register(suite, test_buffer_write_flush_multibyte); 1564 cx_test_register(suite, test_buffer_write_flush_multibyte);
1510 cx_test_register(suite, test_buffer_write_flush_misaligned); 1565 cx_test_register(suite, test_buffer_write_flush_misaligned);
1566 cx_test_register(suite, test_buffer_write_flush_target_full);
1511 cx_test_register(suite, test_buffer_flush); 1567 cx_test_register(suite, test_buffer_flush);
1512 cx_test_register(suite, test_buffer_get); 1568 cx_test_register(suite, test_buffer_get);
1513 cx_test_register(suite, test_buffer_get_eof); 1569 cx_test_register(suite, test_buffer_get_eof);
1514 cx_test_register(suite, test_buffer_read); 1570 cx_test_register(suite, test_buffer_read);
1515 cx_test_register(suite, test_buffer_read_oob); 1571 cx_test_register(suite, test_buffer_read_oob);

mercurial