tests/test_buffer.c

changeset 1129
cc60b7d07912
parent 1112
22dc2163fffd
equal deleted inserted replaced
1128:3672b89e606c 1129:cc60b7d07912
1173 CX_TEST_ASSERT(0 == memcmp(buf.space, " world!", 7)); 1173 CX_TEST_ASSERT(0 == memcmp(buf.space, " world!", 7));
1174 CX_TEST_ASSERT(target.pos == 13); 1174 CX_TEST_ASSERT(target.pos == 13);
1175 CX_TEST_ASSERT(target.size == 13); 1175 CX_TEST_ASSERT(target.size == 13);
1176 CX_TEST_ASSERT(target.capacity >= 13); 1176 CX_TEST_ASSERT(target.capacity >= 13);
1177 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoohello,", 13)); 1177 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoohello,", 13));
1178 }
1179 cxBufferDestroy(&buf);
1180 cxBufferDestroy(&target);
1181 }
1182
1183 CX_TEST(test_buffer_write_flush_multibyte) {
1184 // this test case tests that flushing works correctly even when the
1185 // contents in the buffer are currently not aligned with the item size
1186 CxBuffer buf, target;
1187 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
1188 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT);
1189 memset(buf.space, 0, 8);
1190 cxBufferPutString(&buf, "pre");
1191 CX_TEST_DO {
1192 CxBufferFlushConfig flush;
1193 flush.threshold = 0;
1194 flush.blksize = 4; // test blksize that is not aligned
1195 flush.blkmax = 2; // test with two blocks
1196 flush.target = ⌖
1197 flush.wfunc = cxBufferWriteFunc;
1198 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush));
1199 // first case: string fits after flush
1200 size_t written = cxBufferWrite("foobar", 3, 2, &buf);
1201 CX_TEST_ASSERT(written == 2);
1202 CX_TEST_ASSERT(buf.pos == 6);
1203 CX_TEST_ASSERT(buf.size == 6);
1204 CX_TEST_ASSERT(target.pos == 3);
1205 CX_TEST_ASSERT(target.size == 3);
1206 CX_TEST_ASSERT(0 == memcmp(buf.space, "foobar", 6));
1207 CX_TEST_ASSERT(0 == memcmp(target.space, "pre", 3));
1208 // second case: string does not fit, data is relayed, but only two blocks!
1209 written = cxBufferWrite("bazfooBAR", 3, 3, &buf);
1210 CX_TEST_ASSERT(written == 3);
1211 CX_TEST_ASSERT(buf.pos == 3);
1212 CX_TEST_ASSERT(buf.size == 3);
1213 CX_TEST_ASSERT(target.pos == 15);
1214 CX_TEST_ASSERT(target.size == 15);
1215 CX_TEST_ASSERT(0 == memcmp(buf.space, "BAR", 3));
1216 CX_TEST_ASSERT(0 == memcmp(target.space, "prefoobarbazfoo", 15));
1217 // third case: everything can be relayed, block size is large enough
1218 buf.flush->blkmax = 3;
1219 written = cxBufferWrite("abcdef012", 3, 3, &buf);
1220 CX_TEST_ASSERT(written == 3);
1221 CX_TEST_ASSERT(buf.pos == 0);
1222 CX_TEST_ASSERT(buf.size == 0);
1223 CX_TEST_ASSERT(target.pos == 27);
1224 CX_TEST_ASSERT(target.size == 27);
1225 CX_TEST_ASSERT(0 == memcmp(target.space, "prefoobarbazfooBARabcdef012", 27));
1226 }
1227 cxBufferDestroy(&buf);
1228 cxBufferDestroy(&target);
1229 }
1230
1231 CX_TEST(test_buffer_write_flush_misaligned) {
1232 // this test case tests that flushing works correctly even when the
1233 // contents in the buffer are currently not aligned with the item size
1234 CxBuffer buf, target;
1235 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
1236 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT);
1237 memset(buf.space, 0, 8);
1238 cxBufferPutString(&buf, "prep");
1239 CX_TEST_DO {
1240 CxBufferFlushConfig flush;
1241 flush.threshold = 0;
1242 flush.blksize = 32;
1243 flush.blkmax = 1;
1244 flush.target = ⌖
1245 flush.wfunc = cxBufferWriteFunc;
1246 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush));
1247 // first case: string fits after flush
1248 size_t written = cxBufferWrite("foobar", 3, 2, &buf);
1249 CX_TEST_ASSERT(written == 2);
1250 CX_TEST_ASSERT(buf.pos == 7);
1251 CX_TEST_ASSERT(buf.size == 7);
1252 CX_TEST_ASSERT(target.pos == 3);
1253 CX_TEST_ASSERT(target.size == 3);
1254 CX_TEST_ASSERT(0 == memcmp(buf.space, "pfoobar", 7));
1255 CX_TEST_ASSERT(0 == memcmp(target.space, "pre", 3));
1256 // second case: string does not fit, relaying not possible due to misalignment
1257 written = cxBufferWrite("bazfoobar", 3, 3, &buf);
1258 CX_TEST_ASSERT(written == 0);
1259 CX_TEST_ASSERT(buf.pos == 1);
1260 CX_TEST_ASSERT(buf.size == 1);
1261 CX_TEST_ASSERT(target.pos == 9);
1262 CX_TEST_ASSERT(target.size == 9);
1263 CX_TEST_ASSERT(0 == memcmp(buf.space, "r", 1));
1264 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfooba", 9));
1178 } 1265 }
1179 cxBufferDestroy(&buf); 1266 cxBufferDestroy(&buf);
1180 cxBufferDestroy(&target); 1267 cxBufferDestroy(&target);
1181 } 1268 }
1182 1269
1375 cx_test_register(suite, test_buffer_write_capacity_overflow); 1462 cx_test_register(suite, test_buffer_write_capacity_overflow);
1376 cx_test_register(suite, test_buffer_write_only_overwrite); 1463 cx_test_register(suite, test_buffer_write_only_overwrite);
1377 cx_test_register(suite, test_buffer_write_flush_at_capacity); 1464 cx_test_register(suite, test_buffer_write_flush_at_capacity);
1378 cx_test_register(suite, test_buffer_write_flush_at_threshold); 1465 cx_test_register(suite, test_buffer_write_flush_at_threshold);
1379 cx_test_register(suite, test_buffer_write_flush_rate_limited_and_buffer_too_small); 1466 cx_test_register(suite, test_buffer_write_flush_rate_limited_and_buffer_too_small);
1467 cx_test_register(suite, test_buffer_write_flush_multibyte);
1468 cx_test_register(suite, test_buffer_write_flush_misaligned);
1380 cx_test_register(suite, test_buffer_flush); 1469 cx_test_register(suite, test_buffer_flush);
1381 cx_test_register(suite, test_buffer_get); 1470 cx_test_register(suite, test_buffer_get);
1382 cx_test_register(suite, test_buffer_get_eof); 1471 cx_test_register(suite, test_buffer_get_eof);
1383 cx_test_register(suite, test_buffer_read); 1472 cx_test_register(suite, test_buffer_read);
1384 cx_test_register(suite, test_buffer_read_oob); 1473 cx_test_register(suite, test_buffer_read_oob);

mercurial