buf.c

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

0001-large-append.c (2374B)


      1 #include <stdlib.h>
      2 #include <string.h>
      3 
      4 #include "assert.h"
      5 #include "buf.h"
      6 
      7 #define CHUNK_SIZE 4096
      8 
      9 static char chunk[CHUNK_SIZE];
     10 
     11 // Regression: grow path used MAX(32, cap * 2), ignoring append length;
     12 // appends larger than 2x cap overran the allocation
     13 void test_large_append_grows_to_fit() {
     14   struct buf subject;
     15   memset(&subject, 0, sizeof subject);
     16 
     17   ASSERT("Small append allocates initial capacity", buf_append(&subject, "hello", 5) == 0);
     18   ASSERT("Initial capacity is 32", subject.cap == 32);
     19 
     20   memset(chunk, 'A', sizeof chunk);
     21   ASSERT("Large append succeeds", buf_append(&subject, chunk, sizeof chunk) == 0);
     22 
     23   ASSERT("Capacity covers length after large append", subject.cap >= subject.len);
     24   ASSERT("Length includes large append", subject.len == 5 + CHUNK_SIZE);
     25   ASSERT("Original data survived large append", memcmp(subject.dat, "hello", 5) == 0);
     26   ASSERT("Large appended data is intact", memcmp(subject.dat + 5, chunk, CHUNK_SIZE) == 0);
     27 
     28   buf_clear(&subject);
     29 }
     30 
     31 // After buf_consume, eject reclaims head space but can leave len + length
     32 // > cap, so the grow step must still size to fit the append
     33 void test_large_append_after_consume() {
     34   struct buf subject;
     35   memset(&subject, 0, sizeof subject);
     36 
     37   memset(chunk, 'B', sizeof chunk);
     38   ASSERT("Fill to initial capacity", buf_append(&subject, chunk, 5) == 0);
     39   ASSERT("Fill past initial capacity", buf_append(&subject, chunk, 40) == 0);
     40   ASSERT("Capacity doubled on overflow", subject.cap == 64);
     41 
     42   ASSERT("Consume head of buffer", buf_consume(&subject, 30) == 30);
     43   ASSERT("Data pointer moved past consumed bytes", subject.dat - subject.alloc == 30);
     44 
     45   ASSERT("Large append into consumed buffer succeeds", buf_append(&subject, chunk, sizeof chunk) == 0);
     46 
     47   ASSERT("Capacity covers length after large append", subject.cap >= subject.len);
     48   ASSERT("Length includes large append", subject.len == 15 + CHUNK_SIZE);
     49   ASSERT("Data pointer reset to allocation start", subject.dat == subject.alloc);
     50   ASSERT("Remaining data survived large append", memcmp(subject.dat, chunk, 15) == 0);
     51   ASSERT("Large appended data is intact", memcmp(subject.dat + 15, chunk, CHUNK_SIZE) == 0);
     52 
     53   buf_clear(&subject);
     54 }
     55 
     56 int main(int argc, const char *argv[]) {
     57   buf_set_allocator(realloc, free);
     58 
     59   RUN(test_large_append_grows_to_fit);
     60   RUN(test_large_append_after_consume);
     61   return TEST_REPORT();
     62 }