Initial commit
This commit is contained in:
20
tests/CMakeLists.txt
Normal file
20
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
set(CMAKE_C_STANDARD 90)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_C_EXTENSIONS OFF)
|
||||
|
||||
# Enable warnings and pedantics
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic")
|
||||
|
||||
# Enable testing
|
||||
include(CTest)
|
||||
enable_testing()
|
||||
|
||||
# Search files
|
||||
file(GLOB TEST_FILES "src/*.c")
|
||||
|
||||
foreach(TEST_FILENAME ${TEST_FILES})
|
||||
get_filename_component(TEST_NAME ${TEST_FILENAME} NAME_WE)
|
||||
add_executable("${TEST_NAME}" ${TEST_FILENAME})
|
||||
target_link_libraries("${TEST_NAME}" bhlib bhunit)
|
||||
add_test(NAME "${TEST_NAME}" COMMAND "${TEST_NAME}")
|
||||
endforeach()
|
||||
278
tests/src/hashmap.c
Normal file
278
tests/src/hashmap.c
Normal file
@@ -0,0 +1,278 @@
|
||||
#include <bh/internal/hashmap.h>
|
||||
#include <bh/unit.h>
|
||||
|
||||
static size_t direct_hash(const void *ptr)
|
||||
{
|
||||
return BH_PTR_TO_INT(ptr);
|
||||
}
|
||||
|
||||
static int direct_equal(const void *lhs, const void *rhs)
|
||||
{
|
||||
return BH_PTR_TO_INT(lhs) - BH_PTR_TO_INT(rhs);
|
||||
}
|
||||
|
||||
static int init_destroy(void)
|
||||
{
|
||||
bh_hashmap_t hashmap;
|
||||
|
||||
bh_hashmap_init(&hashmap, direct_equal, direct_hash);
|
||||
|
||||
/* Check internal state is valid and empty */
|
||||
bh_unit_assert(hashmap.data == NULL);
|
||||
bh_unit_assert(hashmap.psls == NULL);
|
||||
bh_unit_assert(hashmap.size == 0);
|
||||
bh_unit_assert(hashmap.capacity == 0);
|
||||
bh_unit_assert(hashmap.threshold == 0);
|
||||
bh_unit_assert(hashmap.factor >= 0.15f && hashmap.factor <= 1.0f);
|
||||
bh_unit_assert(hashmap.equal == direct_equal);
|
||||
bh_unit_assert(hashmap.hash == direct_hash);
|
||||
|
||||
bh_hashmap_destroy(&hashmap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int new_free(void)
|
||||
{
|
||||
bh_hashmap_t *hashmap;
|
||||
|
||||
hashmap = bh_hashmap_new(direct_equal, direct_hash);
|
||||
bh_unit_assert(hashmap != NULL);
|
||||
|
||||
/* Check internal state is valid and empty */
|
||||
bh_unit_assert(hashmap->data == NULL);
|
||||
bh_unit_assert(hashmap->psls == NULL);
|
||||
bh_unit_assert(hashmap->size == 0);
|
||||
bh_unit_assert(hashmap->capacity == 0);
|
||||
bh_unit_assert(hashmap->threshold == 0);
|
||||
bh_unit_assert(hashmap->factor >= 0.15f && hashmap->factor <= 1.0f);
|
||||
bh_unit_assert(hashmap->equal == direct_equal);
|
||||
bh_unit_assert(hashmap->hash == direct_hash);
|
||||
|
||||
bh_hashmap_free(hashmap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int grow_shrink(void)
|
||||
{
|
||||
bh_hashmap_t *hashmap;
|
||||
bh_hashmap_node_t *old_data;
|
||||
size_t *old_psls;
|
||||
void *iter;
|
||||
|
||||
hashmap = bh_hashmap_new(direct_equal, direct_hash);
|
||||
bh_unit_assert(hashmap != NULL);
|
||||
|
||||
/* Allocate space for 1024 entries and insert 1 element */
|
||||
bh_unit_assert(bh_hashmap_reserve(hashmap, 1024) == 0);
|
||||
bh_unit_assert(bh_hashmap_insert(hashmap, BH_INT_TO_PTR(1337), BH_INT_TO_PTR(80085)) == 0);
|
||||
|
||||
/* Check hashmap contents */
|
||||
iter = bh_hashmap_iter_next(hashmap, NULL);
|
||||
bh_unit_assert(iter != NULL);
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_hashmap_iter_key(iter)) == 1337);
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_hashmap_iter_value(iter)) == 80085);
|
||||
bh_unit_assert(bh_hashmap_iter_next(hashmap, iter) == NULL);
|
||||
|
||||
bh_unit_assert(hashmap->data != NULL);
|
||||
bh_unit_assert(hashmap->psls != NULL);
|
||||
bh_unit_assert(hashmap->capacity >= 1024);
|
||||
bh_unit_assert(hashmap->threshold == (size_t)(hashmap->capacity * hashmap->factor));
|
||||
bh_unit_assert(hashmap->size == 1);
|
||||
|
||||
/* Save old pointers */
|
||||
old_data = hashmap->data;
|
||||
old_psls = hashmap->psls;
|
||||
|
||||
/* Change factor and grow */
|
||||
bh_hashmap_set_factor(hashmap, 0.35f);
|
||||
bh_unit_assert(bh_hashmap_reserve(hashmap, 8192) == 0);
|
||||
|
||||
bh_unit_assert(hashmap->data != NULL);
|
||||
bh_unit_assert(hashmap->psls != NULL);
|
||||
bh_unit_assert(hashmap->capacity >= 8192);
|
||||
bh_unit_assert(hashmap->data != old_data);
|
||||
bh_unit_assert(hashmap->psls != old_psls);
|
||||
bh_unit_assert(hashmap->factor == 0.35f);
|
||||
bh_unit_assert(hashmap->threshold == (size_t)(hashmap->capacity * hashmap->factor));
|
||||
bh_unit_assert(hashmap->size == 1);
|
||||
|
||||
old_data = hashmap->data;
|
||||
old_psls = hashmap->psls;
|
||||
|
||||
/* Check hashmap contents */
|
||||
iter = bh_hashmap_iter_next(hashmap, NULL);
|
||||
bh_unit_assert(iter != NULL);
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_hashmap_iter_key(iter)) == 1337);
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_hashmap_iter_value(iter)) == 80085);
|
||||
bh_unit_assert(bh_hashmap_iter_next(hashmap, iter) == NULL);
|
||||
|
||||
/* Shrink */
|
||||
bh_unit_assert(bh_hashmap_reserve(hashmap, 0) == 0);
|
||||
|
||||
bh_unit_assert(hashmap->data != NULL);
|
||||
bh_unit_assert(hashmap->psls != NULL);
|
||||
bh_unit_assert(hashmap->capacity < 8192);
|
||||
bh_unit_assert(hashmap->data != old_data);
|
||||
bh_unit_assert(hashmap->psls != old_psls);
|
||||
bh_unit_assert(hashmap->factor == 0.35f);
|
||||
bh_unit_assert(hashmap->threshold == (size_t)(hashmap->capacity * hashmap->factor));
|
||||
bh_unit_assert(hashmap->size == 1);
|
||||
|
||||
/* Check hashmap contents */
|
||||
iter = bh_hashmap_iter_next(hashmap, NULL);
|
||||
bh_unit_assert(iter != NULL);
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_hashmap_iter_key(iter)) == 1337);
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_hashmap_iter_value(iter)) == 80085);
|
||||
bh_unit_assert(bh_hashmap_iter_next(hashmap, iter) == NULL);
|
||||
|
||||
/* Shrink to 0 (deallocate) */
|
||||
bh_hashmap_clear(hashmap);
|
||||
bh_unit_assert(hashmap->size == 0);
|
||||
|
||||
bh_unit_assert(bh_hashmap_reserve(hashmap, 0) == 0);
|
||||
bh_unit_assert(hashmap->capacity == 0);
|
||||
bh_unit_assert(hashmap->size == 0);
|
||||
|
||||
/* Check hashmap contents */
|
||||
iter = bh_hashmap_iter_next(hashmap, NULL);
|
||||
bh_unit_assert(iter == NULL);
|
||||
|
||||
bh_hashmap_free(hashmap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int insert_remove(void)
|
||||
{
|
||||
bh_hashmap_t *hashmap;
|
||||
size_t i, added, removed;
|
||||
void *iter;
|
||||
|
||||
hashmap = bh_hashmap_new(direct_equal, direct_hash);
|
||||
bh_unit_assert(hashmap != NULL);
|
||||
bh_hashmap_set_factor(hashmap, 1.0f);
|
||||
|
||||
/* Insert elements into hashmap */
|
||||
added = 0;
|
||||
for (i = 1024; i > 0; i--)
|
||||
{
|
||||
added += (i - 1) / 4;
|
||||
bh_unit_assert(bh_hashmap_insert(hashmap, BH_INT_TO_PTR((i - 1) / 4), BH_INT_TO_PTR(i)) == 0);
|
||||
}
|
||||
|
||||
/* Remove elements */
|
||||
iter = bh_hashmap_iter_next(hashmap, NULL);
|
||||
removed = 0;
|
||||
while (iter)
|
||||
{
|
||||
removed += BH_PTR_TO_INT(bh_hashmap_iter_key(iter));
|
||||
bh_hashmap_iter_remove(hashmap, iter);
|
||||
|
||||
iter = bh_hashmap_iter_next(hashmap, NULL);
|
||||
}
|
||||
|
||||
/* Check inserted elements are equal to removed */
|
||||
bh_unit_assert(added == removed);
|
||||
|
||||
bh_hashmap_free(hashmap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lookup(void)
|
||||
{
|
||||
bh_hashmap_t *hashmap;
|
||||
size_t i;
|
||||
|
||||
hashmap = bh_hashmap_new(direct_equal, direct_hash);
|
||||
bh_unit_assert(hashmap != NULL);
|
||||
|
||||
/* Insert elements into hashmap */
|
||||
for (i = 0; i < 256; i++)
|
||||
bh_unit_assert(bh_hashmap_insert(hashmap, BH_INT_TO_PTR(i * 4), BH_INT_TO_PTR(i)) == 0);
|
||||
|
||||
/* Lookup inserted elements */
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
int exists;
|
||||
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_hashmap_at(hashmap, BH_INT_TO_PTR(i * 4), NULL)) == i);
|
||||
bh_hashmap_at(hashmap, BH_INT_TO_PTR(i * 4), &exists);
|
||||
bh_unit_assert(exists == 1);
|
||||
}
|
||||
|
||||
/* Lookup non-existing elements */
|
||||
for (i = 256; i < 512; i++)
|
||||
{
|
||||
int exists;
|
||||
|
||||
bh_unit_assert(bh_hashmap_at(hashmap, BH_INT_TO_PTR(i * 4), NULL) == NULL);
|
||||
bh_hashmap_at(hashmap, BH_INT_TO_PTR(i * 4), &exists);
|
||||
bh_unit_assert(exists == 0);
|
||||
}
|
||||
|
||||
bh_hashmap_free(hashmap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int clear(void)
|
||||
{
|
||||
bh_hashmap_t *hashmap;
|
||||
size_t i;
|
||||
|
||||
hashmap = bh_hashmap_new(direct_equal, direct_hash);
|
||||
bh_unit_assert(hashmap != NULL);
|
||||
|
||||
/* Insert elements into hashmap */
|
||||
for (i = 0; i < 128; i++)
|
||||
bh_unit_assert(bh_hashmap_insert(hashmap, BH_INT_TO_PTR(i), 0) == 0);
|
||||
|
||||
bh_hashmap_clear(hashmap);
|
||||
|
||||
/* Remove non-existing elements */
|
||||
for (i = 0; i < 128; i++)
|
||||
bh_hashmap_remove(hashmap, BH_INT_TO_PTR(i));
|
||||
|
||||
bh_hashmap_free(hashmap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fields(void)
|
||||
{
|
||||
bh_hashmap_t *hashmap;
|
||||
size_t i;
|
||||
|
||||
hashmap = bh_hashmap_new(direct_equal, direct_hash);
|
||||
bh_unit_assert(hashmap != NULL);
|
||||
bh_unit_assert(bh_hashmap_empty(hashmap) == 1);
|
||||
|
||||
/* Insert elements into hashmap */
|
||||
for (i = 0; i < 14; i++)
|
||||
bh_unit_assert(bh_hashmap_insert(hashmap, BH_INT_TO_PTR(i), NULL) == 0);
|
||||
|
||||
/* Check hashmap fields correspond to getter functions */
|
||||
bh_unit_assert(hashmap->size == 14);
|
||||
bh_unit_assert(hashmap->capacity >= 14);
|
||||
bh_unit_assert(bh_hashmap_size(hashmap) == hashmap->size);
|
||||
bh_unit_assert(bh_hashmap_capacity(hashmap) == hashmap->capacity);
|
||||
bh_unit_assert(bh_hashmap_factor(hashmap) == hashmap->factor);
|
||||
bh_unit_assert(bh_hashmap_empty(hashmap) == 0);
|
||||
|
||||
bh_hashmap_free(hashmap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
/* Add unit tests */
|
||||
bh_unit_add("init_destroy", init_destroy);
|
||||
bh_unit_add("new_free", new_free);
|
||||
bh_unit_add("grow_shrink", grow_shrink);
|
||||
bh_unit_add("insert_remove", insert_remove);
|
||||
bh_unit_add("lookup", lookup);
|
||||
bh_unit_add("clear", clear);
|
||||
bh_unit_add("fields", fields);
|
||||
|
||||
return bh_unit_run();
|
||||
}
|
||||
183
tests/src/queue.c
Normal file
183
tests/src/queue.c
Normal file
@@ -0,0 +1,183 @@
|
||||
#include <bh/internal/queue.h>
|
||||
#include <bh/unit.h>
|
||||
|
||||
static int init_destroy(void)
|
||||
{
|
||||
bh_queue_t queue;
|
||||
|
||||
bh_queue_init(&queue);
|
||||
|
||||
bh_unit_assert(queue.size == 0);
|
||||
bh_unit_assert(queue.capacity == 0);
|
||||
bh_unit_assert(queue.data == NULL);
|
||||
bh_unit_assert(queue.head == queue.tail);
|
||||
|
||||
bh_queue_destroy(&queue);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int new_free(void)
|
||||
{
|
||||
bh_queue_t *queue;
|
||||
|
||||
queue = bh_queue_new();
|
||||
bh_unit_assert(queue != NULL);
|
||||
bh_unit_assert(queue->size == 0);
|
||||
bh_unit_assert(queue->capacity == 0);
|
||||
bh_unit_assert(queue->data == NULL);
|
||||
bh_unit_assert(queue->head == queue->tail);
|
||||
|
||||
bh_queue_free(queue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int grow_shrink(void)
|
||||
{
|
||||
bh_queue_t *queue;
|
||||
void *old_data;
|
||||
|
||||
queue = bh_queue_new();
|
||||
bh_unit_assert(queue != NULL);
|
||||
|
||||
/* Reserve 1024 elements and insert item into queue */
|
||||
bh_unit_assert(bh_queue_reserve(queue, 1024) == 0);
|
||||
bh_unit_assert(bh_queue_insert(queue, BH_INT_TO_PTR(1337)) == 0);
|
||||
bh_unit_assert(queue->capacity >= 1024);
|
||||
bh_unit_assert(queue->size == 1);
|
||||
bh_unit_assert(bh_queue_empty(queue) == 0);
|
||||
bh_unit_assert(queue->data != NULL);
|
||||
|
||||
/* Check queue content */
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_queue_front(queue)) == 1337);
|
||||
|
||||
old_data = queue->data;
|
||||
|
||||
/* Grow queue */
|
||||
bh_unit_assert(bh_queue_reserve(queue, 8192) == 0);
|
||||
bh_unit_assert(queue->capacity >= 8192);
|
||||
bh_unit_assert(queue->size == 1);
|
||||
bh_unit_assert(bh_queue_empty(queue) == 0);
|
||||
bh_unit_assert(queue->data != NULL);
|
||||
bh_unit_assert(queue->data != old_data);
|
||||
|
||||
/* Check queue content */
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_queue_front(queue)) == 1337);
|
||||
|
||||
old_data = queue->data;
|
||||
|
||||
/* Shrink the queue */
|
||||
bh_unit_assert(bh_queue_reserve(queue, 0) == 0);
|
||||
bh_unit_assert(queue->capacity >= 1 && queue->capacity <= 8192);
|
||||
bh_unit_assert(queue->size == 1);
|
||||
bh_unit_assert(bh_queue_empty(queue) == 0);
|
||||
bh_unit_assert(queue->data != NULL);
|
||||
bh_unit_assert(queue->data != old_data);
|
||||
|
||||
/* Check queue content */
|
||||
bh_unit_assert(BH_PTR_TO_INT(bh_queue_front(queue)) == 1337);
|
||||
|
||||
/* Shrink to 0 (deallocate) */
|
||||
bh_queue_clear(queue);
|
||||
bh_unit_assert(queue->size == 0);
|
||||
|
||||
bh_unit_assert(bh_queue_reserve(queue, 0) == 0);
|
||||
bh_unit_assert(queue->capacity == 0);
|
||||
bh_unit_assert(queue->size == 0);
|
||||
bh_unit_assert(bh_queue_empty(queue) == 1);
|
||||
bh_unit_assert(queue->data == NULL);
|
||||
|
||||
bh_queue_free(queue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int insert_remove(void)
|
||||
{
|
||||
bh_queue_t *queue;
|
||||
size_t i, added, removed;
|
||||
void *iter;
|
||||
|
||||
queue = bh_queue_new();
|
||||
bh_unit_assert(queue != NULL);
|
||||
|
||||
added = 0;
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
added += i * 2;
|
||||
bh_unit_assert(bh_queue_insert(queue, BH_INT_TO_PTR(i * 2)) == 0);
|
||||
}
|
||||
|
||||
removed = 0;
|
||||
iter = bh_queue_iter_next(queue, NULL);
|
||||
while (iter)
|
||||
{
|
||||
removed += BH_PTR_TO_INT(bh_queue_front(queue));
|
||||
bh_queue_remove(queue);
|
||||
iter = bh_queue_iter_next(queue, NULL);
|
||||
}
|
||||
|
||||
bh_unit_assert(added == removed);
|
||||
bh_unit_assert(queue->size == 0);
|
||||
|
||||
bh_queue_free(queue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rollover(void)
|
||||
{
|
||||
bh_queue_t *queue;
|
||||
size_t i, j, capacity;
|
||||
|
||||
queue = bh_queue_new();
|
||||
bh_unit_assert(queue != NULL);
|
||||
|
||||
bh_unit_assert(bh_queue_reserve(queue, 128) == 0);
|
||||
capacity = queue->capacity;
|
||||
|
||||
for (i = 0; i < 128; i++)
|
||||
{
|
||||
for (j = 0; j < 3; j++)
|
||||
bh_queue_remove(queue);
|
||||
|
||||
for (j = 0; j < 4 && queue->size < 128; j++)
|
||||
bh_unit_assert(bh_queue_insert(queue, BH_INT_TO_PTR(i * 4 + j)) == 0);
|
||||
}
|
||||
|
||||
bh_unit_assert(queue->size == 128);
|
||||
bh_unit_assert(queue->capacity == capacity);
|
||||
|
||||
bh_queue_free(queue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fields(void)
|
||||
{
|
||||
bh_queue_t *queue;
|
||||
|
||||
queue = bh_queue_new();
|
||||
bh_unit_assert(queue != NULL);
|
||||
|
||||
bh_unit_assert(bh_queue_insert(queue, BH_INT_TO_PTR(1337)) == 0);
|
||||
bh_unit_assert(queue->size == 1);
|
||||
bh_unit_assert(queue->capacity >= 1);
|
||||
bh_unit_assert(bh_queue_size(queue) == queue->size);
|
||||
bh_unit_assert(bh_queue_capacity(queue) == queue->capacity);
|
||||
|
||||
bh_queue_free(queue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
bh_unit_add("init_destroy", init_destroy);
|
||||
bh_unit_add("new_free", new_free);
|
||||
bh_unit_add("grow_shrink", grow_shrink);
|
||||
bh_unit_add("insert_remove", insert_remove);
|
||||
bh_unit_add("rollover", rollover);
|
||||
bh_unit_add("fields", fields);
|
||||
|
||||
return bh_unit_run();
|
||||
}
|
||||
Reference in New Issue
Block a user