Sun, 01 May 2022 16:12:13 +0200
#184 #170 first basic flush test
src/cx/buffer.h | file | annotate | diff | comparison | revisions | |
src/cx/common.h | file | annotate | diff | comparison | revisions | |
test/test_buffer.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cx/buffer.h Sun May 01 13:14:31 2022 +0200 1.2 +++ b/src/cx/buffer.h Sun May 01 16:12:13 2022 +0200 1.3 @@ -117,12 +117,7 @@ 1.4 * The write function used for flushing. 1.5 * If NULL, the flushed content gets discarded. 1.6 */ 1.7 - size_t (*flush_func)( 1.8 - void const *, 1.9 - size_t, 1.10 - size_t, 1.11 - void * 1.12 - ); 1.13 + cx_write_func flush_func; 1.14 1.15 /** 1.16 * The target for \c flush_func.
2.1 --- a/src/cx/common.h Sun May 01 13:14:31 2022 +0200 2.2 +++ b/src/cx/common.h Sun May 01 16:12:13 2022 +0200 2.3 @@ -93,6 +93,16 @@ 2.4 #include <stddef.h> 2.5 #include <stdbool.h> 2.6 2.7 +/** 2.8 + * Function pointer compatible with fwrite-like functions. 2.9 + */ 2.10 +typedef size_t (*cx_write_func)( 2.11 + void const *, 2.12 + size_t, 2.13 + size_t, 2.14 + void * 2.15 +); 2.16 + 2.17 #ifdef _WIN32 2.18 #if !(defined __ssize_t_defined || defined _SSIZE_T_) 2.19 #include <BaseTsd.h>
3.1 --- a/test/test_buffer.cpp Sun May 01 13:14:31 2022 +0200 3.2 +++ b/test/test_buffer.cpp Sun May 01 16:12:13 2022 +0200 3.3 @@ -293,9 +293,10 @@ 3.4 3.5 class BufferWrite : public ::testing::Test { 3.6 protected: 3.7 - CxBuffer buf{}; 3.8 + CxBuffer buf{}, target{}; 3.9 3.10 void SetUp() override { 3.11 + cxBufferInit(&target, nullptr, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); 3.12 cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 3.13 buf.capacity = 8; // artificially reduce capacity to check OOB writes 3.14 memset(buf.space, 0, 16); 3.15 @@ -305,6 +306,13 @@ 3.16 3.17 void TearDown() override { 3.18 cxBufferDestroy(&buf); 3.19 + cxBufferDestroy(&target); 3.20 + } 3.21 + 3.22 + void enableFlushing() { 3.23 + buf.flush_target = ⌖ 3.24 + buf.flush_func = reinterpret_cast<cx_write_func>(cxBufferWrite); 3.25 + buf.flush_blkmax = 1; 3.26 } 3.27 }; 3.28 3.29 @@ -507,3 +515,23 @@ 3.30 EXPECT_EQ(buf.pos, 7); 3.31 EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0); 3.32 } 3.33 + 3.34 +TEST_F(BufferWrite, FlushAtCapacity) { 3.35 + enableFlushing(); 3.36 + ASSERT_EQ(buf.capacity, 8); 3.37 + ASSERT_EQ(buf.pos, 4); 3.38 + size_t written = cxBufferWrite("foo", 1, 3, &buf); 3.39 + EXPECT_EQ(written, 3); 3.40 + ASSERT_EQ(buf.pos, 7); 3.41 + ASSERT_EQ(buf.size, 7); 3.42 + ASSERT_EQ(target.pos, 0); 3.43 + ASSERT_EQ(target.size, 0); 3.44 + written = cxBufferWrite("hello", 1, 5, &buf); 3.45 + EXPECT_EQ(written, 5); 3.46 + EXPECT_EQ(buf.pos, 0); 3.47 + EXPECT_EQ(buf.size, 0); 3.48 + EXPECT_EQ(buf.capacity, 8); 3.49 + EXPECT_EQ(target.pos, 12); 3.50 + ASSERT_EQ(target.size, 12); 3.51 + EXPECT_EQ(memcmp(target.space, "prepfoohello", 12), 0); 3.52 +}