Sat, 30 Apr 2022 09:03:17 +0200
#170 first basic write tests
src/buffer.c | file | annotate | diff | comparison | revisions | |
test/test_buffer.cpp | file | annotate | diff | comparison | revisions |
--- a/src/buffer.c Sat Apr 30 08:40:40 2022 +0200 +++ b/src/buffer.c Sat Apr 30 09:03:17 2022 +0200 @@ -169,6 +169,26 @@ return len / size; } +int cxBufferPut( + CxBuffer *buffer, + int c +) { + c &= 0xFF; + unsigned char const ch = c; + if (cxBufferWrite(&ch, 1, 1, buffer) == 1) { + return c; + } else { + return EOF; + } +} + +size_t cxBufferPutString( + CxBuffer *buffer, + const char *str +) { + return cxBufferWrite(str, 1, strlen(str), buffer); +} + size_t cxBufferRead( void *ptr, size_t size, @@ -194,29 +214,6 @@ return len / size; } -int cxBufferPut( - CxBuffer *buffer, - int c -) { - if (buffer->pos >= buffer->capacity) { - if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) { - if (cxBufferMinimumCapacity(buffer, buffer->capacity + 1)) { - return EOF; - } - } else { - return EOF; - } - } - - c &= 0xFF; - buffer->bytes[buffer->pos] = (unsigned char) c; - buffer->pos++; - if (buffer->pos > buffer->size) { - buffer->size = buffer->pos; - } - return c; -} - int cxBufferGet(CxBuffer *buffer) { if (cxBufferEof(buffer)) { return EOF; @@ -227,13 +224,6 @@ } } -size_t cxBufferPutString( - CxBuffer *buffer, - const char *str -) { - return cxBufferWrite(str, 1, strlen(str), buffer); -} - int cxBufferShiftLeft( CxBuffer *buffer, size_t shift
--- a/test/test_buffer.cpp Sat Apr 30 08:40:40 2022 +0200 +++ b/test/test_buffer.cpp Sat Apr 30 09:03:17 2022 +0200 @@ -258,4 +258,127 @@ EXPECT_EQ(memcmp(space, "\0\0\0\0\0 test", 10), 0); EXPECT_EQ(buf.size, 0); EXPECT_EQ(buf.pos, 0); + cxBufferDestroy(&buf); } + +class BufferWrite : public ::testing::Test { +protected: + CxBuffer buf{}; + + void SetUp() override { + cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); + buf.capacity = 8; // artificially reduce capacity to check OOB writes + memset(buf.space, 0, 16); + memcpy(buf.space, "prep", 4); + buf.size = buf.pos = 4; + } + + void TearDown() override { + cxBufferDestroy(&buf); + } +}; + +TEST_F(BufferWrite, SizeOneFit) { + const char *data = "test"; + ASSERT_EQ(buf.capacity, 8); + ASSERT_EQ(buf.pos, 4); + size_t written = cxBufferWrite(data, 1, 4, &buf); + EXPECT_EQ(written, 4); + EXPECT_EQ(buf.size, 8); + EXPECT_EQ(buf.pos, 8); + EXPECT_EQ(buf.capacity, 8); + EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0); +} + +TEST_F(BufferWrite, SizeOneOverwrite) { + +} + +TEST_F(BufferWrite, SizeOnePartialOverwrite) { + +} + +TEST_F(BufferWrite, SizeOneDiscard) { + const char *data = "testing"; + ASSERT_EQ(buf.capacity, 8); + ASSERT_EQ(buf.pos, 4); + size_t written = cxBufferWrite(data, 1, 7, &buf); + EXPECT_EQ(written, 4); + EXPECT_EQ(buf.size, 8); + EXPECT_EQ(buf.pos, 8); + EXPECT_EQ(buf.capacity, 8); + EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0); +} + +TEST_F(BufferWrite, SizeOneExtend) { + buf.flags |= CX_BUFFER_AUTO_EXTEND; + const char *data = "testing"; + ASSERT_EQ(buf.capacity, 8); + ASSERT_EQ(buf.pos, 4); + size_t written = cxBufferWrite(data, 1, 7, &buf); + EXPECT_EQ(written, 7); + EXPECT_EQ(buf.size, 11); + EXPECT_EQ(buf.pos, 11); + EXPECT_GE(buf.capacity, 11); + EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0); +} + +TEST_F(BufferWrite, MultibyteOverwrite) { + +} + +TEST_F(BufferWrite, MultibytePartialOverwrite) { + +} + +TEST_F(BufferWrite, MultibyteFit) { + +} + +TEST_F(BufferWrite, MultibyteDiscard) { + // remember: whole elements are discarded if they do not fit +} + +TEST_F(BufferWrite, MultibyteExtend) { + +} + +TEST_F(BufferWrite, PutcWrapperFit) { + +} + +TEST_F(BufferWrite, PutcWrapperOverwrite) { + +} + +TEST_F(BufferWrite, PutcWrapperPartialOverwrite) { + +} + +TEST_F(BufferWrite, PutcWrapperDiscard) { + +} + +TEST_F(BufferWrite, PutcWrapperExtend) { + +} + +TEST_F(BufferWrite, PutStringWrapperFit) { + +} + +TEST_F(BufferWrite, PutStringWrapperOverwrite) { + +} + +TEST_F(BufferWrite, PutStringWrapperPartialOverwrite) { + +} + +TEST_F(BufferWrite, PutStringWrapperDiscard) { + +} + +TEST_F(BufferWrite, PutStringWrapperExtend) { + +}