test/test_buffer.cpp

changeset 545
3700ac4bd9a3
parent 542
45bcfd152f85
child 546
900795d59d03
equal deleted inserted replaced
544:2e73456e5f84 545:3700ac4bd9a3
291 cxBufferDestroy(&buf); 291 cxBufferDestroy(&buf);
292 } 292 }
293 293
294 class BufferWrite : public ::testing::Test { 294 class BufferWrite : public ::testing::Test {
295 protected: 295 protected:
296 CxBuffer buf{}; 296 CxBuffer buf{}, target{};
297 297
298 void SetUp() override { 298 void SetUp() override {
299 cxBufferInit(&target, nullptr, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
299 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 300 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
300 buf.capacity = 8; // artificially reduce capacity to check OOB writes 301 buf.capacity = 8; // artificially reduce capacity to check OOB writes
301 memset(buf.space, 0, 16); 302 memset(buf.space, 0, 16);
302 memcpy(buf.space, "prep", 4); 303 memcpy(buf.space, "prep", 4);
303 buf.size = buf.pos = 4; 304 buf.size = buf.pos = 4;
304 } 305 }
305 306
306 void TearDown() override { 307 void TearDown() override {
307 cxBufferDestroy(&buf); 308 cxBufferDestroy(&buf);
309 cxBufferDestroy(&target);
310 }
311
312 void enableFlushing() {
313 buf.flush_target = ⌖
314 buf.flush_func = reinterpret_cast<cx_write_func>(cxBufferWrite);
315 buf.flush_blkmax = 1;
308 } 316 }
309 }; 317 };
310 318
311 TEST_F(BufferWrite, SizeOneFit) { 319 TEST_F(BufferWrite, SizeOneFit) {
312 const char *data = "test"; 320 const char *data = "test";
505 EXPECT_EQ(buf.capacity, 8); 513 EXPECT_EQ(buf.capacity, 8);
506 EXPECT_EQ(buf.size, 8); 514 EXPECT_EQ(buf.size, 8);
507 EXPECT_EQ(buf.pos, 7); 515 EXPECT_EQ(buf.pos, 7);
508 EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0); 516 EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0);
509 } 517 }
518
519 TEST_F(BufferWrite, FlushAtCapacity) {
520 enableFlushing();
521 ASSERT_EQ(buf.capacity, 8);
522 ASSERT_EQ(buf.pos, 4);
523 size_t written = cxBufferWrite("foo", 1, 3, &buf);
524 EXPECT_EQ(written, 3);
525 ASSERT_EQ(buf.pos, 7);
526 ASSERT_EQ(buf.size, 7);
527 ASSERT_EQ(target.pos, 0);
528 ASSERT_EQ(target.size, 0);
529 written = cxBufferWrite("hello", 1, 5, &buf);
530 EXPECT_EQ(written, 5);
531 EXPECT_EQ(buf.pos, 0);
532 EXPECT_EQ(buf.size, 0);
533 EXPECT_EQ(buf.capacity, 8);
534 EXPECT_EQ(target.pos, 12);
535 ASSERT_EQ(target.size, 12);
536 EXPECT_EQ(memcmp(target.space, "prepfoohello", 12), 0);
537 }

mercurial