buf.c

Simple byte buffers
git clone git://git.finwo.net/lib/buf.c
Log | Files | Refs | README | LICENSE

commit 36a5f602091dfff1d59fa275ce66500c55af9766
parent f88627142306a660af3420ddc42ceda8c2b148bf
Author: finwo <finwo@pm.me>
Date:   Wed, 11 Nov 2026 14:37:06 +0100

Test append paths after buf_consume moved dat past alloc

Diffstat:
Mtest/0001-large-append.c | 10++++------
Atest/0002-consume-append.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/test/0001-large-append.c b/test/0001-large-append.c @@ -8,10 +8,8 @@ static char chunk[CHUNK_SIZE]; -// Appending a block larger than double the current capacity must grow the -// buffer to fit it, not just double the old capacity. The growth path in -// buf_append used to compute MAX(32, cap * 2) without regard for the pending -// append length, overflowing the allocation on every such append. +// Regression: grow path used MAX(32, cap * 2), ignoring append length; +// appends larger than 2x cap overran the allocation void test_large_append_grows_to_fit() { struct buf subject; memset(&subject, 0, sizeof subject); @@ -30,8 +28,8 @@ void test_large_append_grows_to_fit() { buf_clear(&subject); } -// Same bug via the consume path: after buf_consume, the eject step reclaims -// head space but may still leave too little capacity, hitting the grow step. +// After buf_consume, eject reclaims head space but can leave len + length +// > cap, so the grow step must still size to fit the append void test_large_append_after_consume() { struct buf subject; memset(&subject, 0, sizeof subject); diff --git a/test/0002-consume-append.c b/test/0002-consume-append.c @@ -0,0 +1,52 @@ +#include <stdlib.h> +#include <string.h> + +#include "assert.h" +#include "buf.h" + +// Append paths after buf_consume moved dat past alloc + +void test_append_reuses_tail_capacity() { + struct buf subject; + memset(&subject, 0, sizeof subject); + + ASSERT("Fill buffer", buf_append(&subject, "0123456789", 10) == 0); + ASSERT("Consume head of buffer", buf_consume(&subject, 4) == 4); + + // 6 + 20 <= 28 available, so neither eject nor grow may run + ASSERT("Append into remaining tail capacity", buf_append(&subject, "ABCDEFGHIJKLMNOPQRST", 20) == 0); + + ASSERT("Data pointer unchanged by in-place append", subject.dat - subject.alloc == 4); + ASSERT("Capacity unchanged by in-place append", subject.cap == 28); + ASSERT("Length includes append", subject.len == 26); + ASSERT("Appended data follows unconsumed data", memcmp(subject.dat, "456789ABCDEFGHIJKLMNOPQRST", 26) == 0); + + buf_clear(&subject); +} + +void test_append_ejects_without_growing() { + struct buf subject; + memset(&subject, 0, sizeof subject); + + ASSERT("Fill buffer", buf_append(&subject, "0123456789", 10) == 0); + ASSERT("Consume head of buffer", buf_consume(&subject, 8) == 8); + + // 2 + 25 > 24 available, but the 8 consumed bytes reclaim enough: + // eject must run and grow must not + ASSERT("Append forcing eject", buf_append(&subject, "abcdefghijklmnopqrstuvwxy", 25) == 0); + + ASSERT("Data pointer reset to allocation start", subject.dat == subject.alloc); + ASSERT("Eject reclaimed consumed head space", subject.cap == 32); + ASSERT("Length includes append", subject.len == 27); + ASSERT("Appended data follows unconsumed data", memcmp(subject.dat, "89abcdefghijklmnopqrstuvwxy", 27) == 0); + + buf_clear(&subject); +} + +int main(int argc, const char *argv[]) { + buf_set_allocator(realloc, free); + + RUN(test_append_reuses_tail_capacity); + RUN(test_append_ejects_without_growing); + return TEST_REPORT(); +}