move api tests into separate file

This commit is contained in:
Gregory Burd 2024-03-15 14:40:01 -04:00
parent fbd4f1d1d9
commit b01cd70160
3 changed files with 184 additions and 184 deletions

View file

@ -1,6 +1,6 @@
CFLAGS = -Wall -Wextra -Wpedantic -Og -g -std=c99 -Iinclude/ -fPIC
TEST_FLAGS = -std=c99 -Itests/ -fsanitize=address,undefined
TEST_FLAGS = -Itests/ -fsanitize=address,undefined
OBJS = skiplist.o
STATIC_LIB = libskiplist.a
@ -29,7 +29,7 @@ $(SHARED_LIB): $(OBJS)
tests/%.o: tests/%.c
$(CC) $(CFLAGS) -c -o $@ $^
tests: $(TESTS)
test: $(TESTS)
./tests/test
tests/test: tests/test.o tests/munit.o $(STATIC_LIB)
@ -41,7 +41,7 @@ examples/example: examples/example.c $(STATIC_LIB)
$(CC) $^ -o $@ $(CFLAGS) $(TEST_FLAGS) -pthread
clean:
rm -f $(OBJS)
rm -f $(OBJS) munit.o test.o
rm -f $(STATIC_LIB)
rm -f $(TESTS)
rm -f $(EXAMPLES)

180
tests/api.c Normal file
View file

@ -0,0 +1,180 @@
static void *
test_api_setup(const MunitParameter params[], void *user_data)
{
struct test_info *info = (struct test_info *)user_data;
(void)info;
(void)params;
ex_sl_t *slist = calloc(sizeof(ex_sl_t), 1);
if (slist == NULL)
return NULL;
sl_init(slist, uint32_key_cmp);
return (void *)(uintptr_t)slist;
}
static void
test_api_tear_down(void *fixture)
{
ex_sl_t *slist = (ex_sl_t *)fixture;
assert_ptr_not_null(slist);
sl_node *cursor = sl_begin(slist);
while (cursor) {
assert_ptr_not_null(cursor);
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
assert_ptr_not_null(entry);
assert_uint32(entry->key, ==, entry->value);
cursor = sl_next(slist, cursor);
sl_erase_node(slist, &entry->snode);
sl_release_node(&entry->snode);
sl_wait_for_free(&entry->snode);
sl_free_node(&entry->snode);
free(entry);
}
sl_free(slist);
free(fixture);
}
static void *
test_api_insert_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_insert_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_insert(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
assert_ptr_not_null(data);
int n = munit_rand_int_range(128, 4096);
int key = munit_rand_int_range(0, (((uint32_t)0) - 1) / 10);
while (n--) {
ex_node_t *node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
sl_init_node(&node->snode);
node->key = key;
node->value = key;
sl_insert(slist, &node->snode);
}
return MUNIT_OK;
}
static void *
test_api_remove_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_remove_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_remove(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_find_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_find_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_find(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_update_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_update_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_update(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_delete_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_delete_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_delete(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_duplicates_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_duplicates_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_duplicates(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_size_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_size_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_size(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_iterators_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_iterators_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_iterators(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}

View file

@ -119,187 +119,7 @@ uint32_key_cmp(sl_node *a, sl_node *b, void *aux)
return 0;
}
static void *
test_api_setup(const MunitParameter params[], void *user_data)
{
struct test_info *info = (struct test_info *)user_data;
(void)info;
(void)params;
ex_sl_t *slist = calloc(sizeof(ex_sl_t), 1);
if (slist == NULL)
return NULL;
sl_init(slist, uint32_key_cmp);
return (void *)(uintptr_t)slist;
}
static void
test_api_tear_down(void *fixture)
{
ex_sl_t *slist = (ex_sl_t *)fixture;
assert_ptr_not_null(slist);
sl_node *cursor = sl_begin(slist);
while (cursor) {
assert_ptr_not_null(cursor);
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
assert_ptr_not_null(entry);
assert_uint32(entry->key, ==, entry->value);
cursor = sl_next(slist, cursor);
sl_erase_node(slist, &entry->snode);
sl_release_node(&entry->snode);
sl_wait_for_free(&entry->snode);
sl_free_node(&entry->snode);
free(entry);
}
sl_free(slist);
free(fixture);
}
static void *
test_api_insert_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_insert_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_insert(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
assert_ptr_not_null(data);
int n = munit_rand_int_range(128, 4096);
int key = munit_rand_int_range(0, (((uint32_t)0) - 1) / 10);
while (n--) {
ex_node_t *node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
sl_init_node(&node->snode);
node->key = key;
node->value = key;
sl_insert(slist, &node->snode);
}
return MUNIT_OK;
}
static void *
test_api_remove_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_remove_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_remove(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_find_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_find_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_find(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_update_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_update_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_update(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_delete_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_delete_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_delete(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_duplicates_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_duplicates_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_duplicates(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_size_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_size_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_size(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_iterators_setup(const MunitParameter params[], void *user_data) {
return test_api_setup(params, user_data);
}
static void
test_api_iterators_tear_down(void *fixture) {
test_api_tear_down(fixture);
}
static MunitResult
test_api_iterators(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
#include "api.c"
static MunitTest api_test_suite[] = {
{ (char *)"/api/insert", test_api_insert, test_api_insert_setup,
test_api_insert_tear_down, MUNIT_TEST_OPTION_NONE, NULL },