mirror of
https://github.com/janet-lang/janet
synced 2025-01-24 14:16:52 +00:00
Adding some more unit tests for various components.
This commit is contained in:
parent
f6dcb07c8d
commit
9a858d5a97
5
Makefile
5
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 #####
|
||||
|
@ -24,12 +24,16 @@
|
||||
|
||||
/* Initialize a buffer */
|
||||
DstBuffer *dst_buffer_init(DstBuffer *buffer, uint32_t capacity) {
|
||||
uint8_t *data = malloc(sizeof(uint8_t) * capacity);
|
||||
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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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 */
|
||||
|
@ -22,9 +22,19 @@
|
||||
|
||||
|
||||
#include <dst/dst.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
int main() {
|
||||
int64_t i;
|
||||
dst_init();
|
||||
DstArray *array = dst_array(10);
|
||||
assert(array->capacity == 10);
|
||||
assert(array->count == 0);
|
||||
|
19
unittests/buffer_test.c
Normal file
19
unittests/buffer_test.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include "unit.h"
|
||||
#include <dst/dst.h>
|
||||
|
||||
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;
|
||||
}
|
15
unittests/table_test.c
Normal file
15
unittests/table_test.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include "unit.h"
|
||||
#include <dst/dst.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user