0000-basics.c (1665B)
1 #include <stdlib.h> 2 #include <string.h> 3 4 #include "assert.h" 5 #include "buf.h" 6 7 #ifndef MIN 8 #define MIN(a,b) ((a)<(b)?(a):(b)) 9 #endif 10 11 void test_basic() { 12 struct buf *buf = malloc(sizeof(struct buf)); 13 buf_clear(buf); 14 15 ASSERT("Allocation of buffer", buf != NULL); 16 ASSERT("Cleared buf has null alloc", buf->alloc == NULL); 17 ASSERT("Cleared buf has null data", buf->dat == NULL); 18 ASSERT("Cleared buf has 0 length", buf->len == 0); 19 ASSERT("Cleared buf has 0 capacity", buf->cap == 0); 20 21 buf_append_byte(buf, 'H'); 22 23 ASSERT("Appended buffer has alloc", buf->alloc != NULL); 24 ASSERT("Appended buffer has data", buf->dat != NULL); 25 ASSERT("Appended buffer has matching alloc and data", buf->dat == buf->alloc); 26 ASSERT("Appended buffer has correct length", buf->len == 1); 27 ASSERT("Appended buffer has correct capacity", buf->cap == 32); 28 29 buf_append(buf, "ello World", 10); 30 31 ASSERT("Appended buffer has matching alloc and data", buf->dat == buf->alloc); 32 ASSERT("Appended buffer has correct length", buf->len == 11); 33 ASSERT("Appended buffer has correct capacity", buf->cap == 32); 34 ASSERT("Appended buffer has correct data", strncmp(buf->dat, "Hello World", MIN(buf->len, 11)) == 0); 35 36 buf_consume(buf, 6); 37 38 ASSERT("Consumed buffer has matching alloc and data", buf->dat - buf->alloc == 6); 39 ASSERT("Consumed buffer has correct length", buf->len == 5); 40 ASSERT("Consumed buffer has correct capacity", buf->cap == 26); 41 ASSERT("Consumed buffer has correct data", strncmp(buf->dat, "World", MIN(buf->len, 5)) == 0); 42 } 43 44 int main(int argc, const char *argv[]) { 45 buf_set_allocator(realloc, free); 46 47 RUN(test_basic); 48 return TEST_REPORT(); 49 }