From 9a858d5a97bbedf0f6643dafc445f72781ee20e5 Mon Sep 17 00:00:00 2001 From: bakpakin Date: Mon, 6 Nov 2017 09:44:10 -0500 Subject: [PATCH] Adding some more unit tests for various components. --- Makefile | 5 +-- core/buffer.c | 72 +++++++++++++++++++---------------------- core/cache.c | 2 +- core/gc.c | 5 +++ core/syscalls.c | 12 ++++++- unittests/array_test.c | 1 + unittests/buffer_test.c | 19 +++++++++++ unittests/table_test.c | 15 +++++++++ 8 files changed, 88 insertions(+), 43 deletions(-) create mode 100644 unittests/buffer_test.c create mode 100644 unittests/table_test.c diff --git a/Makefile b/Makefile index 0550f778..8db496dc 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,6 @@ CFLAGS=-std=c99 -Wall -Wextra -I./include -I./libs -g -DDST_VERSION=$(VERSION) PREFIX=/usr/local DST_TARGET=dst DST_XXD=xxd -# Use gdb. On osx use lldb DEBUGGER=lldb DST_INTERNAL_HEADERS=$(addprefix core/,cache.h opcodes.h) DST_HEADERS=$(addprefix include/dst/,dst.h) @@ -79,13 +78,15 @@ $(DST_TARGET): $(DST_CORE_OBJECTS) CCU_FLAGS = $(CFLAGS) -DDST_UNIT_TEST DST_UNIT_BINARIES=$(addprefix unittests/,\ - array_test.out) + array_test.out buffer_test.out table_test.out) %.out: %.c $(DST_CORE_OBJECTS) $(DST_ALL_HEADERS) unittests/unit.h $(CC) $(CCU_FLAGS) $(DST_CORE_OBJECTS) $< -o $@ unit: $(DST_UNIT_BINARIES) unittests/array_test.out + unittests/buffer_test.out + unittests/table_test.out ################### ##### Testing ##### diff --git a/core/buffer.c b/core/buffer.c index 208c4efe..2106cc6f 100644 --- a/core/buffer.c +++ b/core/buffer.c @@ -24,12 +24,16 @@ /* Initialize a buffer */ DstBuffer *dst_buffer_init(DstBuffer *buffer, uint32_t capacity) { - uint8_t *data = malloc(sizeof(uint8_t) * capacity); - if (NULL == data) { - DST_OUT_OF_MEMORY; + uint8_t *data = NULL; + if (capacity > 0) { + data = malloc(sizeof(uint8_t) * capacity); + if (NULL == data) { + DST_OUT_OF_MEMORY; + } } buffer->count = 0; buffer->capacity = capacity; + buffer->data = data; return buffer; } @@ -46,40 +50,38 @@ DstBuffer *dst_buffer(uint32_t capacity) { /* Ensure that the buffer has enough internal capacity */ void dst_buffer_ensure(DstBuffer *buffer, uint32_t capacity) { - uint8_t *newData; + uint8_t *new_data; uint8_t *old = buffer->data; if (capacity <= buffer->capacity) return; - newData = realloc(old, capacity * sizeof(uint8_t)); - if (NULL == newData) { + new_data = realloc(old, capacity * sizeof(uint8_t)); + if (NULL == new_data) { DST_OUT_OF_MEMORY; } - buffer->data = newData; + buffer->data = new_data; buffer->capacity = capacity; } /* Adds capacity for enough extra bytes to the buffer. Ensures that the * next n bytes pushed to the buffer will not cause a reallocation */ void dst_buffer_extra(DstBuffer *buffer, uint32_t n) { - uint32_t newCount = buffer->count + n; - if (newCount > buffer->capacity) { - uint32_t newCapacity = newCount * 2; - uint8_t *newData = realloc(buffer->data, newCapacity * sizeof(uint8_t)); - if (NULL == newData) { + uint32_t new_size = buffer->count + n; + if (new_size > buffer->capacity) { + uint32_t new_capacity = new_size * 2; + uint8_t *new_data = realloc(buffer->data, new_capacity * sizeof(uint8_t)); + if (NULL == new_data) { DST_OUT_OF_MEMORY; } - buffer->data = newData; - buffer->capacity = newCapacity; + buffer->data = new_data; + buffer->capacity = new_capacity; } } /* Push multiple bytes into the buffer */ void dst_buffer_push_bytes(DstBuffer *buffer, const uint8_t *string, uint32_t length) { - uint32_t newSize = buffer->count + length; - if (newSize > buffer->capacity) { - dst_buffer_ensure(buffer, 2 * newSize); - } + uint32_t new_size = buffer->count + length; + dst_buffer_ensure(buffer, new_size); memcpy(buffer->data + buffer->count, string, length); - buffer->count = newSize; + buffer->count = new_size; } /* Push a cstring to buffer */ @@ -91,44 +93,36 @@ void dst_buffer_push_cstring(DstBuffer *buffer, const char *cstring) { /* Push a single byte to the buffer */ void dst_buffer_push_u8(DstBuffer *buffer, uint8_t byte) { - uint32_t newSize = buffer->count + 1; - if (newSize > buffer->capacity) { - dst_buffer_ensure(buffer, 2 * newSize); - } + uint32_t new_size = buffer->count + 1; + dst_buffer_ensure(buffer, new_size); buffer->data[buffer->count] = byte; - buffer->count = newSize; + buffer->count = new_size; } /* Push a 16 bit unsigned integer to the buffer */ void dst_buffer_push_u16(DstBuffer *buffer, uint16_t x) { - uint32_t newSize = buffer->count + 2; - if (newSize > buffer->capacity) { - dst_buffer_ensure(buffer, 2 * newSize); - } + uint32_t new_size = buffer->count + 1; + dst_buffer_ensure(buffer, new_size); buffer->data[buffer->count] = x & 0xFF; buffer->data[buffer->count + 1] = (x >> 8) & 0xFF; - buffer->count = newSize; + buffer->count = new_size; } /* Push a 32 bit unsigned integer to the buffer */ void dst_buffer_push_u32(DstBuffer *buffer, uint32_t x) { - uint32_t newSize = buffer->count + 4; - if (newSize > buffer->capacity) { - dst_buffer_ensure(buffer, 2 * newSize); - } + uint32_t new_size = buffer->count + 4; + dst_buffer_ensure(buffer, new_size); buffer->data[buffer->count] = x & 0xFF; buffer->data[buffer->count + 1] = (x >> 8) & 0xFF; buffer->data[buffer->count + 2] = (x >> 16) & 0xFF; buffer->data[buffer->count + 3] = (x >> 24) & 0xFF; - buffer->count = newSize; + buffer->count = new_size; } /* Push a 64 bit unsigned integer to the buffer */ void dst_buffer_push_u64(DstBuffer *buffer, uint64_t x) { - uint32_t newSize = buffer->count + 8; - if (newSize > buffer->capacity) { - dst_buffer_ensure(buffer, 2 * newSize); - } + uint32_t new_size = buffer->count + 8; + dst_buffer_ensure(buffer, new_size); buffer->data[buffer->count] = x & 0xFF; buffer->data[buffer->count + 1] = (x >> 8) & 0xFF; buffer->data[buffer->count + 2] = (x >> 16) & 0xFF; @@ -137,5 +131,5 @@ void dst_buffer_push_u64(DstBuffer *buffer, uint64_t x) { buffer->data[buffer->count + 5] = (x >> 40) & 0xFF; buffer->data[buffer->count + 6] = (x >> 48) & 0xFF; buffer->data[buffer->count + 7] = (x >> 56) & 0xFF; - buffer->count = newSize; + buffer->count = new_size; } diff --git a/core/cache.c b/core/cache.c index d2d21099..bce065d4 100644 --- a/core/cache.c +++ b/core/cache.c @@ -33,7 +33,7 @@ */ /* Cache state */ -DstValue *dst_vm_cache; +DstValue *dst_vm_cache = NULL; uint32_t dst_vm_cache_capacity; uint32_t dst_vm_cache_count; uint32_t dst_vm_cache_deleted; diff --git a/core/gc.c b/core/gc.c index 168d3cd7..7e226ec1 100644 --- a/core/gc.c +++ b/core/gc.c @@ -302,6 +302,11 @@ void dst_sweep() { void *dst_alloc(DstMemoryType type, size_t size) { DstGCMemoryHeader *mdata; size_t total = size + sizeof(DstGCMemoryHeader); + + /* Make sure everything is inited */ + if (NULL == dst_vm_cache) { + DST_PLEASE_INIT; + } void *mem = malloc(total); /* Check for bad malloc */ diff --git a/core/syscalls.c b/core/syscalls.c index e4df0494..0247839a 100644 --- a/core/syscalls.c +++ b/core/syscalls.c @@ -22,9 +22,19 @@ #include +#include int dst_print(DstFiber *fiber, DstValue *argv, uint32_t argn) { - printf("Hello!\n"); + uint32_t i; + for (i = 0; i < argn; ++i) { + uint32_t j, len; + const uint8_t *vstr = dst_to_string(argv[i]); + len = dst_string_length(vstr); + for (j = 0; j < len; ++j) { + putc(vstr[j], stdout); + } + } + putc('\n', stdout); return 0; } diff --git a/unittests/array_test.c b/unittests/array_test.c index 7cfb9bec..b98c55f0 100644 --- a/unittests/array_test.c +++ b/unittests/array_test.c @@ -3,6 +3,7 @@ int main() { int64_t i; + dst_init(); DstArray *array = dst_array(10); assert(array->capacity == 10); assert(array->count == 0); diff --git a/unittests/buffer_test.c b/unittests/buffer_test.c new file mode 100644 index 00000000..14f1df9e --- /dev/null +++ b/unittests/buffer_test.c @@ -0,0 +1,19 @@ +#include "unit.h" +#include + +int main() { + dst_init(); + DstBuffer *buffer = dst_buffer(100); + assert(buffer->count == 0); + assert(buffer->capacity == 100); + dst_buffer_push_u8(buffer, 'h'); + dst_buffer_push_u8(buffer, 'e'); + dst_buffer_push_u8(buffer, 'l'); + dst_buffer_push_u8(buffer, 'l'); + dst_buffer_push_u8(buffer, 'o'); + assert(dst_equals( + dst_wrap_string(dst_cstring("hello")), + dst_wrap_string(dst_string(buffer->data, buffer->count)) + )); + return 0; +} diff --git a/unittests/table_test.c b/unittests/table_test.c new file mode 100644 index 00000000..bd9fee54 --- /dev/null +++ b/unittests/table_test.c @@ -0,0 +1,15 @@ +#include "unit.h" +#include + +int main() { + dst_init(); + DstTable *table = dst_table(10); + assert(table->count == 0); + dst_table_put(table, dst_cstringv("a"), dst_cstringv("b")); + dst_table_put(table, dst_cstringv("b"), dst_cstringv("a")); + dst_table_put(table, dst_cstringv("a"), dst_cstringv("c")); + assert(table->count == 2); + dst_table_remove(table, dst_cstringv("a")); + assert(table->count == 1); + return 0; +}