Sat, 22 Nov 2025 18:49:43 +0100
increase test coverage for buffer.c
| tests/test_buffer.c | file | annotate | diff | comparison | revisions |
--- a/tests/test_buffer.c Sat Nov 22 18:49:39 2025 +0100 +++ b/tests/test_buffer.c Sat Nov 22 18:49:43 2025 +0100 @@ -28,9 +28,39 @@ #include "cx/test.h" #include "util_allocator.h" +#include <errno.h> #include "cx/buffer.h" +#ifdef _WIN32 +#include <Windows.h> +#include <sysinfoapi.h> +static unsigned long system_page_size() { + static unsigned long ps = 0; + if (ps == 0) { + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + ps = sysinfo.dwPageSize; + } + return ps; +} +#else +#include <unistd.h> +static unsigned long system_page_size() { + static unsigned long ps = 0; + if (ps == 0) { + long sc = sysconf(_SC_PAGESIZE); + if (sc < 0) { + // fallback for systems which do not report a value here + ps = 4096; // LCOV_EXCL_LINE + } else { + ps = (unsigned long) sc; + } + } + return ps; +} +#endif + CX_TEST(test_buffer_init_wrap_space) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); @@ -147,6 +177,23 @@ cx_testing_allocator_destroy(&talloc); } +CX_TEST(test_buffer_create_defaulted_allocator) { + CX_TEST_DO { + CxBuffer *buf; + buf = cxBufferCreate(NULL, 16, NULL, 0); + CX_TEST_ASSERT(buf != NULL); + CX_TEST_ASSERT(buf->flush == NULL); + CX_TEST_ASSERT(buf->space != NULL); + CX_TEST_ASSERT((buf->flags & CX_BUFFER_AUTO_EXTEND) == 0); + CX_TEST_ASSERT((buf->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); + CX_TEST_ASSERT(buf->pos == 0); + CX_TEST_ASSERT(buf->size == 0); + CX_TEST_ASSERT(buf->capacity == 16); + CX_TEST_ASSERT(buf->allocator == cxDefaultAllocator); + cxBufferFree(buf); + } +} + CX_TEST(test_buffer_minimum_capacity_sufficient) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); @@ -181,6 +228,15 @@ CX_TEST_ASSERT(buf.capacity == 16); CX_TEST_ASSERT(buf.size == 8); CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0); + + // below page size, new capacity should be power of two + cxBufferMinimumCapacity(&buf, 200); + CX_TEST_ASSERT(buf.capacity == 256); + + // greater than page size, new capacity should be multiple of pagesize + cxBufferMinimumCapacity(&buf, system_page_size() + 200); + CX_TEST_ASSERT(buf.capacity == system_page_size()*2); + cxBufferDestroy(&buf); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } @@ -691,6 +747,17 @@ } } +CX_TEST(test_buffer_shift_right_overflow) { + TEST_BUFFER_SHIFT_SETUP(buf); + CX_TEST_DO { + errno = 0; + int ret = cxBufferShiftRight(&buf, SIZE_MAX - 2); + CX_TEST_ASSERT(ret != 0); + CX_TEST_ASSERT(errno == EOVERFLOW); + TEST_BUFFER_SHIFT_TEARDOWN(buf); + } +} + static size_t mock_write_limited_rate( const void *ptr, size_t size, @@ -1503,6 +1570,12 @@ CX_TEST_ASSERT(read == 4); CX_TEST_ASSERT(0 == memcmp(&target, "me d", 4)); CX_TEST_ASSERT(buf.pos == 6); + + // overflow + errno = 0; + read = cxBufferRead(&target, 4, SIZE_MAX/2, &buf); + CX_TEST_ASSERT(read == 0); + CX_TEST_ASSERT(errno == EOVERFLOW); } cxBufferDestroy(&buf); } @@ -1565,6 +1638,7 @@ cx_test_register(suite, test_buffer_init_wrap_space_auto_free); cx_test_register(suite, test_buffer_init_fresh_space); cx_test_register(suite, test_buffer_init_on_heap); + cx_test_register(suite, test_buffer_create_defaulted_allocator); cx_test_register(suite, test_buffer_minimum_capacity_sufficient); cx_test_register(suite, test_buffer_minimum_capacity_extend); cx_test_register(suite, test_buffer_shrink); @@ -1601,6 +1675,7 @@ cx_test_register(suite, test_buffer_shift_right_overshift_extend); cx_test_register(suite, test_buffer_shift_right_offset_interface); cx_test_register(suite, test_buffer_shift_right_copy_on_write); + cx_test_register(suite, test_buffer_shift_right_overflow); cx_test_register(suite, test_buffer_write_size_one_fit); cx_test_register(suite, test_buffer_write_size_one_discard); cx_test_register(suite, test_buffer_write_size_one_extend);