349 buf.flush_func = reinterpret_cast<cx_write_func>(cxBufferWrite); |
349 buf.flush_func = reinterpret_cast<cx_write_func>(cxBufferWrite); |
350 buf.flush_blkmax = 1; |
350 buf.flush_blkmax = 1; |
351 } |
351 } |
352 }; |
352 }; |
353 |
353 |
|
354 static size_t mock_write_limited_rate( |
|
355 void const *ptr, |
|
356 size_t size, |
|
357 __attribute__((unused)) size_t nitems, |
|
358 CxBuffer *buffer |
|
359 ) { |
|
360 // simulate limited target drain capacity |
|
361 static bool full = false; |
|
362 if (full) { |
|
363 full = false; |
|
364 return 0; |
|
365 } else { |
|
366 full = true; |
|
367 return cxBufferWrite(ptr, size, nitems > 2 ? 2 : nitems, buffer); |
|
368 } |
|
369 } |
|
370 |
354 TEST_F(BufferWrite, SizeOneFit) { |
371 TEST_F(BufferWrite, SizeOneFit) { |
355 const char *data = "test"; |
372 const char *data = "test"; |
356 ASSERT_EQ(buf.capacity, 8); |
373 ASSERT_EQ(buf.capacity, 8); |
357 ASSERT_EQ(buf.pos, 4); |
374 ASSERT_EQ(buf.pos, 4); |
358 ASSERT_EQ(buf.size, 4); |
375 ASSERT_EQ(buf.size, 4); |
593 EXPECT_EQ(target.pos, 15); |
610 EXPECT_EQ(target.pos, 15); |
594 ASSERT_EQ(target.size, 15); |
611 ASSERT_EQ(target.size, 15); |
595 EXPECT_EQ(memcmp(target.space, "prepfoobarhello", 15), 0); |
612 EXPECT_EQ(memcmp(target.space, "prepfoobarhello", 15), 0); |
596 } |
613 } |
597 |
614 |
|
615 TEST_F(BufferWrite, FlushRateLimited) { |
|
616 enableFlushing(); |
|
617 // limit the rate of the flush function and the capacity of the target |
|
618 target.capacity = 16; |
|
619 target.flags &= ~CX_BUFFER_AUTO_EXTEND; |
|
620 buf.flush_func = (cx_write_func) mock_write_limited_rate; |
|
621 ASSERT_EQ(buf.capacity, 8); |
|
622 ASSERT_EQ(buf.pos, 4); |
|
623 size_t written = cxBufferWrite("foo", 1, 3, &buf); |
|
624 EXPECT_EQ(written, 3); |
|
625 ASSERT_EQ(buf.pos, 7); |
|
626 ASSERT_EQ(buf.size, 7); |
|
627 ASSERT_EQ(target.pos, 0); |
|
628 ASSERT_EQ(target.size, 0); |
|
629 written = cxBufferWrite("hello, world!", 1, 13, &buf); |
|
630 // " world!" fits into this buffer, the remaining stuff is flushed out |
|
631 EXPECT_EQ(written, 13); |
|
632 EXPECT_EQ(buf.pos, 7); |
|
633 EXPECT_EQ(buf.size, 7); |
|
634 EXPECT_EQ(buf.capacity, 8); |
|
635 EXPECT_EQ(memcmp(buf.space, " world!", 7), 0); |
|
636 EXPECT_EQ(target.pos, 13); |
|
637 ASSERT_EQ(target.size, 13); |
|
638 EXPECT_EQ(target.capacity, 16); |
|
639 EXPECT_EQ(memcmp(target.space, "prepfoohello,", 13), 0); |
|
640 } |
|
641 |
598 class BufferSeek : public BufferFixture { |
642 class BufferSeek : public BufferFixture { |
599 }; |
643 }; |
600 |
644 |
601 TEST_F(BufferSeek, SetZero) { |
645 TEST_F(BufferSeek, SetZero) { |
602 int result = cxBufferSeek(&buf, 0, SEEK_SET); |
646 int result = cxBufferSeek(&buf, 0, SEEK_SET); |