diff --git a/README.md b/README.md index 003407f..cafb7a9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 🔗 Data Structures +# 🔗 Data Structures and Algorithms [![Build Status](https://travis-ci.org/recp/ds.svg?branch=master)](https://travis-ci.org/recp/libds) [![Build status](https://ci.appveyor.com/api/projects/status/yqpyll64woh39a23/branch/master?svg=true)](https://ci.appveyor.com/project/recp/libds/branch/master) @@ -58,14 +58,12 @@ main(int argc, const char * argv[]) { ### Unix (Autotools) -```text -$ sh ./build-deps.sh # run only once (dependencies) -$ +```bash $ sh autogen.sh $ ./configure $ make -$ make install -$ [sudo] make install +$ make check # [Optional] +$ [sudo] make install # [Optional] ``` ### Windows (MSBuild) diff --git a/build-deps.sh b/build-deps.sh deleted file mode 100644 index 23bb9e9..0000000 --- a/build-deps.sh +++ /dev/null @@ -1,30 +0,0 @@ -#! /bin/sh -# -# Copyright (c), Recep Aslantas. -# -# MIT License (MIT), http://opensource.org/licenses/MIT -# Full license can be found in the LICENSE file -# - -# check if deps are pulled -git submodule update --init --recursive - -# fix glibtoolize - -cd $(dirname "$0") - -if [ "$(uname)" = "Darwin" ]; then - libtoolBin=$(which glibtoolize) - libtoolBinDir=$(dirname "${libtoolBin}") - ln -s $libtoolBin "${libtoolBinDir}/libtoolize" -fi - -# general deps: gcc make autoconf automake libtool cmake - -# test - cmocka -cd ./test/lib/cmocka -mkdir build -cd build -cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug .. -make -j8 -cd ../../../../ diff --git a/configure.ac b/configure.ac index b500b94..3c91558 100644 --- a/configure.ac +++ b/configure.ac @@ -8,7 +8,7 @@ AC_PREREQ([2.69]) AC_INIT([ds], [0.2.1], [info@recp.me]) -AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects]) +AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects serial-tests]) AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_SRCDIR([src/]) diff --git a/makefile.am b/makefile.am index 0f205eb..e123609 100644 --- a/makefile.am +++ b/makefile.am @@ -19,12 +19,9 @@ lib_LTLIBRARIES = libds.la libds_la_LDFLAGS = -no-undefined -version-info 0:1:0 checkLDFLAGS = -L./.libs \ - -L./test/lib/cmocka/build/src \ - -lcmocka \ -lm \ -lds -checkCFLAGS = -I./test/lib/cmocka/include \ - -I./include +checkCFLAGS = -I./include check_PROGRAMS = test/tests TESTS = $(check_PROGRAMS) @@ -56,11 +53,9 @@ libds_la_SOURCES=\ src/sort/sort.c test_tests_SOURCES=\ + test/runner.c \ test/src/test_common.c \ - test/src/test_main.c \ test/src/test_rb.c \ test/src/test_flist.c \ test/src/test_flist_sep.c \ test/src/test_htable.c -all-local: - sh ./post-build.sh diff --git a/post-build.sh b/post-build.sh deleted file mode 100644 index 9861ac4..0000000 --- a/post-build.sh +++ /dev/null @@ -1,19 +0,0 @@ -#! /bin/sh -# -# Copyright (c), Recep Aslantas. -# -# MIT License (MIT), http://opensource.org/licenses/MIT -# Full license can be found in the LICENSE file -# - -cd $(dirname "$0") - -mkdir -p .libs - -if [ "$(uname)" = "Darwin" ]; then - ln -sf $(pwd)/test/lib/cmocka/build/src/libcmocka.0.dylib \ - .libs/libcmocka.0.dylib; -else - ln -sf $(pwd)/test/lib/cmocka/build/src/libcmocka.so.0 \ - .libs/libcmocka.so.0; -fi diff --git a/test/include/common.h b/test/include/common.h new file mode 100644 index 0000000..2e00377 --- /dev/null +++ b/test/include/common.h @@ -0,0 +1,115 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef tests_common_h +#define tests_common_h + +#include +#include +#include + +typedef struct test_status_t { + const char *msg; + int status; +} test_status_t; + +typedef test_status_t (*fntest)(void); + +typedef struct test_entry_t { + char *name; + fntest entry; + int ret; + int show_output; +} test_entry_t; + +#define RESET "\033[0m" +#define BLACK "\033[30m" /* Black */ +#define RED "\033[31m" /* Red */ +#define GREEN "\033[32m" /* Green */ +#define YELLOW "\033[33m" /* Yellow */ +#define BLUE "\033[34m" /* Blue */ +#define MAGENTA "\033[35m" /* Magenta */ +#define CYAN "\033[36m" /* Cyan */ +#define WHITE "\033[37m" /* White */ +#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ +#define BOLDRED "\033[1m\033[31m" /* Bold Red */ +#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ +#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ +#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ +#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ +#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ +#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ + +#define TEST_DECLARE(FUN) test_status_t test_ ## FUN(void); +#define TEST_ENTRY(FUN) { #FUN, test_ ## FUN, 0, 0 }, +#define TEST_LIST static test_entry_t tests[] = + +/* __VA_ARGS__ workaround for MSVC: https://stackoverflow.com/a/5134656 */ +#define EXPAND(x) x + +#define TEST_OK 1 +#define TEST_SUCCESS return (test_status_t){NULL, TEST_OK}; + +#define TEST_IMPL_ARG1(FUN) \ + test_status_t test_ ## FUN (void); \ + test_status_t test_ ## FUN() + +#define TEST_IMPL_ARG2(PREFIX, FUN) TEST_IMPL_ARG1(PREFIX ## FUN) +#define TEST_IMPL_ARG3(arg1, arg2, arg3, ...) arg3 + +#define TEST_IMPL_CHOOSER(...) \ + EXPAND(TEST_IMPL_ARG3(__VA_ARGS__, TEST_IMPL_ARG2, TEST_IMPL_ARG1)) + +#define TEST_IMPL(...) EXPAND(TEST_IMPL_CHOOSER(__VA_ARGS__)(__VA_ARGS__)) + +#define ASSERT_EXT(expr, msg) \ + if (!(expr)) { \ + fprintf(stderr, \ + RED " assert fail" RESET \ + " in " BOLDCYAN "%s " RESET \ + "on " BOLDMAGENTA "line %d" RESET \ + " : " BOLDWHITE " ASSERT(%s)\n" RESET, \ + __FILE__, \ + __LINE__, \ + #expr); \ + return (test_status_t){msg, 0}; \ + } + +#define ASSERT_ARG1(expr) ASSERT_EXT(expr, NULL) +#define ASSERT_ARG2(expr, msg) ASSERT_EXT(expr, msg) +#define ASSERT_ARG3(arg1, arg2, arg3, ...) arg3 + +#define ASSERT_CHOOSER(...) ASSERT_ARG3(__VA_ARGS__, ASSERT_ARG2, ASSERT_ARG1) +#define ASSERT(...) do { ASSERT_CHOOSER(__VA_ARGS__)(__VA_ARGS__) } while(0); +#define ASSERTIFY(expr) do { \ + test_status_t ts; \ + ts = expr; \ + if (ts.status != TEST_OK) { \ + fprintf(stderr, \ + RED " assert fail" RESET \ + " in " BOLDCYAN "%s " RESET \ + "on " BOLDMAGENTA "line %d" RESET \ + " : " BOLDWHITE " ASSERTIFY(%s)\n" RESET, \ + __FILE__, \ + __LINE__, \ + #expr); \ + return (test_status_t){ts.msg, 0}; \ + } \ + } while(0); + +#if defined(_WIN32) +# define drand48() ((float)(rand() / (RAND_MAX + 1.0))) +# define OK_TEXT "ok:" +# define FAIL_TEXT "fail:" +# define FINAL_TEXT "^_^" +#else +# define OK_TEXT "✔︎" +# define FAIL_TEXT "𐄂" +# define FINAL_TEXT "🎉" +#endif + +#endif /* common_h */ diff --git a/test/runner.c b/test/runner.c new file mode 100644 index 0000000..5640351 --- /dev/null +++ b/test/runner.c @@ -0,0 +1,100 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "include/common.h" +#include "tests.h" + +#include +#include +#include + +#define TARGET_NAME "libds" + +int +main(int argc, const char * argv[]) { + test_entry_t *entry; + test_status_t st; + int32_t i, count, passed, failed, maxlen; + double start, end, elapsed, total; + + passed = failed = maxlen = 0; + total = 0.0; + count = sizeof(tests) / sizeof(tests[0]); + + fprintf(stderr, CYAN "\nWelcome to %s tests\n\n" RESET, TARGET_NAME); + + for (i = 0; i < count; i++) { + int32_t len; + + entry = tests + i; + len = (int32_t)strlen(entry->name); + + if (len > maxlen) + maxlen = len; + } + + maxlen += 5; + + fprintf(stderr, + BOLDWHITE " %-*s %-*s\n", + maxlen, "Test Name", maxlen, "Elapsed Time"); + + for (i = 0; i < count; i++) { + entry = tests + i; + start = clock(); + st = entry->entry(); + end = clock(); + elapsed = (end - start) / CLOCKS_PER_SEC; + total += elapsed; + + if (!st.status) { + fprintf(stderr, + BOLDRED " " FAIL_TEXT BOLDWHITE " %s " RESET, entry->name); + if (st.msg) { + fprintf(stderr, + YELLOW "- %s" RESET, + st.msg); + } + + fprintf(stderr, "\n"); + + failed++; + } else { + fprintf(stderr, GREEN " " OK_TEXT RESET " %-*s ", maxlen, entry->name); + + if (elapsed > 0.01) + fprintf(stderr, YELLOW "%.2fs", elapsed); + else + fprintf(stderr, "0"); + + fprintf(stderr, "\n" RESET); + passed++; + } + } + + if (failed == 0) { + fprintf(stderr, + BOLDGREEN "\n All tests are passed " FINAL_TEXT "\n" RESET); + } + + fprintf(stderr, + CYAN "\n%s test results (%0.2fs):\n" RESET + "--------------------------\n" + + MAGENTA "%d" RESET " tests are runned, " + GREEN "%d" RESET " %s passed, " + RED "%d" RESET " %s failed\n\n" RESET, + TARGET_NAME, + total, + count, + passed, + passed > 1 ? "are" : "is", + failed, + failed > 1 ? "are" : "is"); + + return failed; +} diff --git a/test/src/test_common.c b/test/src/test_common.c index 6c73276..81334b1 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -4,8 +4,7 @@ */ #include "test_common.h" -#include -#include +#include void rand_str(char *dest, size_t length) { diff --git a/test/src/test_common.h b/test/src/test_common.h index 67c5e3d..5ec04c9 100644 --- a/test/src/test_common.h +++ b/test/src/test_common.h @@ -6,11 +6,12 @@ #ifndef test_common_h #define test_common_h +#include "../include/common.h" + #include #include #include #include -#include #include #include diff --git a/test/src/test_flist.c b/test/src/test_flist.c index d183a40..d60566c 100644 --- a/test/src/test_flist.c +++ b/test/src/test_flist.c @@ -16,8 +16,7 @@ test_flist_onfree(FList *flist, FListItem *item) { free(item->data); } -void -test_flist(void **state) { +TEST_IMPL(flist) { FList *flist; float *value; int count, i; @@ -35,24 +34,24 @@ test_flist(void **state) { /* test insert */ if (i % 2 == 0) { flist_insert(flist, value); - assert_non_null(flist->first); - assert_non_null(flist->last); + ASSERT(flist->first); + ASSERT(flist->last); } /* test append */ else { flist_append(flist, value); - assert_non_null(flist->first); - assert_non_null(flist->last); + ASSERT(flist->first); + ASSERT(flist->last); } /* test find node */ - assert_true(flist_contains(flist, value)); + ASSERT(flist_contains(flist, value)); /* test remove */ if (i == 0) { flist_remove_by(flist, value); - assert_false(flist_contains(flist, value)); + ASSERT(!flist_contains(flist, value)); } /* test remove by item */ @@ -60,12 +59,12 @@ test_flist(void **state) { FListItem *item; float *val; - assert(flist_last(flist) == flist_at(flist, 9)); + ASSERT(flist_last(flist) == flist_at(flist, 9)); item = flist->first; val = item->data; flist_remove(flist, item); - assert_false(flist_contains(flist, val)); + ASSERT(!flist_contains(flist, val)); } if (i == 20 && flist->last) { @@ -74,17 +73,21 @@ test_flist(void **state) { item = flist->last; val = item->data; + flist_remove(flist, item); + if (flist->first) - assert_non_null(flist->last); - assert_false(flist_contains(flist, val)); + ASSERT(flist->last); + + ASSERT(!flist_contains(flist, val)); } if (i == 30) { void *val; val = flist->last->data; + flist_remove_by(flist, val); - assert_false(flist_contains(flist, val)); + ASSERT(!flist_contains(flist, val)); } /* pick random item */ @@ -105,16 +108,18 @@ test_flist(void **state) { flist_remove(flist, item1); flist_remove(flist, item2); - assert_false(flist_contains(flist, val1)); - assert_false(flist_contains(flist, val2)); + ASSERT(!flist_contains(flist, val1)); + ASSERT(!flist_contains(flist, val2)); } /* test empty */ if (i == 100) { flist_empty(flist); - assert_true(flist_isempty(flist)); + ASSERT(flist_isempty(flist)); } } flist_destroy(flist); + + TEST_SUCCESS } diff --git a/test/src/test_flist_sep.c b/test/src/test_flist_sep.c index 7ac5c75..e51d51c 100644 --- a/test/src/test_flist_sep.c +++ b/test/src/test_flist_sep.c @@ -5,9 +5,9 @@ #include "test_common.h" #include +#include -void -test_flist_sep(void **state) { +TEST_IMPL(flist_sep) { FListItem *first; float *value; int count, i; @@ -24,22 +24,22 @@ test_flist_sep(void **state) { /* test insert */ if (i % 2 == 0) { flist_sp_insert(&first, value); - assert_non_null(first); + ASSERT(first); } /* test append */ else { flist_sp_append(&first, value); - assert_non_null(first); + ASSERT(first); } /* test find node */ - assert_true(flist_sp_contains(&first, value)); + ASSERT(flist_sp_contains(&first, value)); /* test remove */ if (i == 0) { flist_sp_remove_by(&first, value); - assert_false(flist_sp_contains(&first, value)); + ASSERT(!flist_sp_contains(&first, value)); } /* test remove by item */ @@ -47,19 +47,19 @@ test_flist_sep(void **state) { FListItem *item; float *val; - assert(flist_sp_last(&first) == flist_sp_at(&first, 9)); + ASSERT(flist_sp_last(&first) == flist_sp_at(&first, 9)); item = first; val = item->data; flist_sp_remove(&first, item); - assert_false(flist_sp_contains(&first, val)); + ASSERT(!flist_sp_contains(&first, val)); } if (i == 30) { void *val; val = first->data; flist_sp_remove_by(&first, val); - assert_false(flist_sp_contains(&first, val)); + ASSERT(!flist_sp_contains(&first, val)); } /* pick random item */ @@ -80,17 +80,19 @@ test_flist_sep(void **state) { flist_sp_remove(&first, item1); flist_sp_remove(&first, item2); - assert_false(flist_sp_contains(&first, val1)); - assert_false(flist_sp_contains(&first, val2)); + ASSERT(!flist_sp_contains(&first, val1)); + ASSERT(!flist_sp_contains(&first, val2)); } /* test empty */ if (i == 100) { flist_sp_destroy(&first); - assert_null(first); + ASSERT(!first); } } flist_sp_destroy(&first); + + TEST_SUCCESS } diff --git a/test/src/test_htable.c b/test/src/test_htable.c index 0953512..001e866 100644 --- a/test/src/test_htable.c +++ b/test/src/test_htable.c @@ -10,8 +10,7 @@ /* keep top 2000 to test remove */ static char *inserted_items[2000]; -void -test_htable(void **state) { +TEST_IMPL(htable) { char keybuf[256]; HTable *htable; void *found; @@ -23,7 +22,7 @@ test_htable(void **state) { htable = hash_new_str(capacity); /* 25 is not a prime number */ - assert_true(htable->capacity > 3); + ASSERT(htable->capacity > 3); count = 1000; @@ -40,16 +39,16 @@ test_htable(void **state) { /* test find value */ found = hash_get(htable, key); - assert_non_null(found); + ASSERT(found); /* found values must be same */ - assert_ptr_equal(key, found); + ASSERT(key == found); inserted_items[i] = key; } /* increase size */ hash_resize(htable, 9); - assert_true(htable->count == 2); + ASSERT(htable->count == 2); for (i = 0; i < count; i++) { /* random key length */ @@ -62,10 +61,10 @@ test_htable(void **state) { /* test find value */ found = hash_get(htable, key); - assert_non_null(found); + ASSERT(found); /* found values must be same */ - assert_ptr_equal(key, found); + ASSERT(key == found); inserted_items[i] = key; if (i == 0 || i == 10 || i == 50) { @@ -75,7 +74,7 @@ test_htable(void **state) { hash_unset(htable, key); found = hash_get(htable, key); - assert_null(found); + ASSERT(!found); /* allow only once */ hash_set(htable, key, key); @@ -86,7 +85,7 @@ test_htable(void **state) { hash_set(htable, key, NULL); found = hash_get(htable, key); - assert_null(found); + ASSERT(!found); } } @@ -96,11 +95,11 @@ test_htable(void **state) { /* increase size */ hash_resize(htable, 100); - assert_true(htable->count == count); + ASSERT(htable->count == count); /* decrease size */ hash_resize(htable, 15); - assert_true(htable->count == count); + ASSERT(htable->count == count); for (i = 0; i < 10; i++) { /* random key length */ @@ -113,16 +112,16 @@ test_htable(void **state) { /* test find value */ found = hash_get(htable, key); - assert_non_null(found); + ASSERT(found); /* found values must be same */ - assert_ptr_equal(key, found); + ASSERT(key == found); inserted_items[i] = key; if (i == 10 || i == 50) { hash_unset(htable, key); found = hash_get(htable, key); - assert_null(found); + ASSERT(!found); } } @@ -132,5 +131,7 @@ test_htable(void **state) { htable->hashfn = ds_hashfn_sdbm; hash_resize(htable, 50); - assert_true(htable->count == count); + ASSERT(htable->count == count); + + TEST_SUCCESS } diff --git a/test/src/test_main.c b/test/src/test_main.c deleted file mode 100644 index 806a72c..0000000 --- a/test/src/test_main.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c), Recep Aslantas. - * MIT License (MIT), http://opensource.org/licenses/MIT - */ - -#include "test_common.h" -#include "test_tests.h" - -int -main(int argc, const char * argv[]) { - const struct CMUnitTest tests[] = { - /* red black tree */ - cmocka_unit_test(test_rb_topdown_str), - cmocka_unit_test(test_rb_topdown_ptr), - cmocka_unit_test(test_rb_topdown_custom_cmp), - cmocka_unit_test(test_rb_topdown_freeenode), - cmocka_unit_test(test_rb_topdown_custom_cmp_i32), - cmocka_unit_test(test_rb_topdown_custom_cmp_i64), - - /* forward list */ - cmocka_unit_test(test_flist), - cmocka_unit_test(test_flist_sep), - - /* hash table */ - cmocka_unit_test(test_htable) - }; - - return cmocka_run_group_tests(tests, - NULL, - NULL); -} diff --git a/test/src/test_rb.c b/test/src/test_rb.c index 325a953..7615bae 100644 --- a/test/src/test_rb.c +++ b/test/src/test_rb.c @@ -33,35 +33,36 @@ test_rb_print_float(void *key) { static void test_rb_freenode(RBTree *tree, RBNode *node) { - assert_non_null(node); - assert_non_null(node->val); + assert(node); + assert(node->val); /* free value */ free(node->val); } +static void test_rb_foundkey(RBTree *tree, void* key, bool *replace) { printf("duplicated key: '%s'\n", key); *replace = true; } +static void test_rb_walk(RBTree *tree, RBNode *node) { RBNode *parent; int side; - assert_non_null(node->chld[0]); - assert_non_null(node->chld[1]); + assert(node->chld[0]); + assert(node->chld[1]); side = rb_parent(tree, node->key, &parent); - assert_non_null(parent); /* because we are walking on it */ + assert(parent); /* because we are walking on it */ - assert_true(parent->chld[side] == node); + assert(parent->chld[side] == node); } -void -test_rb_topdown_str(void **state) { +TEST_IMPL(rb_topdown_str) { RBTree *tree; RBNode *node; void *found; @@ -86,18 +87,18 @@ test_rb_topdown_str(void **state) { rb_insert(tree, key, key); /* test balance */ - assert(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); /* test find node */ node = rb_find_node(tree, key); - assert_non_null(node); + ASSERT(node); /* test find value */ found = rb_find(tree, key); - assert_non_null(node); + ASSERT(node); /* found values must be same */ - assert_ptr_equal(node->val, found); + ASSERT(node->val == found); inserted_items[i] = node->key; } @@ -113,18 +114,18 @@ test_rb_topdown_str(void **state) { rb_insert(tree, key, key); /* test balance */ - assert_true(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); /* test find node */ node = rb_find_node(tree, key); - assert_non_null(node); + ASSERT(node); /* test find value */ found = rb_find(tree, key); - assert_non_null(node); + ASSERT(node); /* found values must be same */ - assert_ptr_equal(node->val, found); + ASSERT(node->val == found); inserted_items[i + 1000] = node->key; } @@ -134,11 +135,11 @@ test_rb_topdown_str(void **state) { rb_remove(tree, inserted_items[i]); /* test balance */ - assert_true(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); } /* we remove all nodes */ - assert_true(rb_isempty(tree)); + ASSERT(rb_isempty(tree)); /* random size */ for (i = 0; i < 100; i++) { @@ -151,18 +152,18 @@ test_rb_topdown_str(void **state) { rb_insert(tree, key, key); /* test balance */ - assert_true(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); /* test find node */ node = rb_find_node(tree, key); - assert_non_null(node); + ASSERT(node); /* test find value */ found = rb_find(tree, key); - assert_non_null(node); + ASSERT(node); /* found values must be same */ - assert_ptr_equal(node->val, found); + ASSERT(node->val == found); inserted_items[i] = node->key; } @@ -172,17 +173,18 @@ test_rb_topdown_str(void **state) { /* we removed all nodes */ rb_empty(tree); - assert_true(rb_isempty(tree)); + ASSERT(rb_isempty(tree)); key = strdup(keybuf); rb_insert(tree, key, key); rb_print(tree); rb_destroy(tree); + + TEST_SUCCESS } -void -test_rb_topdown_ptr(void **state) { +TEST_IMPL(rb_topdown_ptr) { RBTree *tree; RBNode *node; void *key; @@ -204,23 +206,23 @@ test_rb_topdown_ptr(void **state) { rb_insert(tree, key, key); /* test balance */ - assert(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); /* test find node */ node = rb_find_node(tree, key); - assert_non_null(node); + ASSERT(node); /* test find value */ found = rb_find(tree, key); - assert_non_null(node); + ASSERT(node); /* found values must be same */ - assert_ptr_equal(node->val, found); + ASSERT(node->val == found); } /* we removed all nodes */ rb_empty(tree); - assert_true(rb_isempty(tree)); + ASSERT(rb_isempty(tree)); k = rand() % 10000; key = (void *)k; @@ -228,10 +230,11 @@ test_rb_topdown_ptr(void **state) { rb_print(tree); rb_destroy(tree); + + TEST_SUCCESS } -void -test_rb_topdown_custom_cmp(void **state) { +TEST_IMPL(rb_topdown_custom_cmp) { RBTree *tree; RBNode *node; float *key; @@ -253,23 +256,23 @@ test_rb_topdown_custom_cmp(void **state) { rb_insert(tree, key, key); /* test balance */ - assert(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); /* test find node */ node = rb_find_node(tree, key); - assert_non_null(node); + ASSERT(node); /* test find value */ found = rb_find(tree, key); - assert_non_null(node); + ASSERT(node); /* found values must be same */ - assert_ptr_equal(node->val, found); + ASSERT(node->val == found); } /* we removed all nodes */ rb_empty(tree); - assert_true(rb_isempty(tree)); + ASSERT(rb_isempty(tree)); key = malloc(sizeof(*key)); *key = (float)drand48(); @@ -277,10 +280,11 @@ test_rb_topdown_custom_cmp(void **state) { rb_print(tree); rb_destroy(tree); + + TEST_SUCCESS } -void -test_rb_topdown_custom_cmp_i32(void **state) { +TEST_IMPL(rb_topdown_custom_cmp_i32) { RBTree *tree; RBNode *node; int32_t *key; @@ -302,23 +306,23 @@ test_rb_topdown_custom_cmp_i32(void **state) { rb_insert(tree, key, key); /* test balance */ - assert(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); /* test find node */ node = rb_find_node(tree, key); - assert_non_null(node); + ASSERT(node); /* test find value */ found = rb_find(tree, key); - assert_non_null(node); + ASSERT(node); /* found values must be same */ - assert_ptr_equal(node->val, found); + ASSERT(node->val == found); } /* we removed all nodes */ rb_empty(tree); - assert_true(rb_isempty(tree)); + ASSERT(rb_isempty(tree)); key = malloc(sizeof(*key)); *key = rand() % 10000; @@ -326,10 +330,11 @@ test_rb_topdown_custom_cmp_i32(void **state) { rb_print(tree); rb_destroy(tree); + + TEST_SUCCESS } -void -test_rb_topdown_custom_cmp_i64(void **state) { +TEST_IMPL(rb_topdown_custom_cmp_i64) { RBTree *tree; RBNode *node; int64_t *key; @@ -351,23 +356,23 @@ test_rb_topdown_custom_cmp_i64(void **state) { rb_insert(tree, key, key); /* test balance */ - assert(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); /* test find node */ node = rb_find_node(tree, key); - assert_non_null(node); + ASSERT(node); /* test find value */ found = rb_find(tree, key); - assert_non_null(node); + ASSERT(node); /* found values must be same */ - assert_ptr_equal(node->val, found); + ASSERT(node->val == found); } /* we removed all nodes */ rb_empty(tree); - assert_true(rb_isempty(tree)); + ASSERT(rb_isempty(tree)); key = malloc(sizeof(*key)); *key = rand() % 10000; @@ -375,10 +380,11 @@ test_rb_topdown_custom_cmp_i64(void **state) { rb_print(tree); rb_destroy(tree); + + TEST_SUCCESS } -void -test_rb_topdown_freeenode(void **state) { +TEST_IMPL(rb_topdown_freeenode) { RBTree *tree; RBNode *node; float *key; @@ -401,23 +407,23 @@ test_rb_topdown_freeenode(void **state) { rb_insert(tree, key, key); /* test balance */ - assert(rb_assert(tree, tree->root->chld[1])); + ASSERT(rb_assert(tree, tree->root->chld[1])); /* test find node */ node = rb_find_node(tree, key); - assert_non_null(node); + ASSERT(node); /* test find value */ found = rb_find(tree, key); - assert_non_null(node); + ASSERT(node); /* found values must be same */ - assert_ptr_equal(node->val, found); + ASSERT(node->val == found); } /* we removed all nodes */ rb_empty(tree); - assert_true(rb_isempty(tree)); + ASSERT(rb_isempty(tree)); key = malloc(sizeof(*key)); *key = (float)drand48(); @@ -425,4 +431,6 @@ test_rb_topdown_freeenode(void **state) { rb_print(tree); rb_destroy(tree); + + TEST_SUCCESS } diff --git a/test/src/test_tests.h b/test/src/test_tests.h deleted file mode 100644 index 78706b6..0000000 --- a/test/src/test_tests.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c), Recep Aslantas. - * MIT License (MIT), http://opensource.org/licenses/MIT - */ - -#ifndef test_tests_h -#define test_tests_h - -/* RB Tree */ -void test_rb_topdown_str(void **state); -void test_rb_topdown_ptr(void **stater); -void test_rb_topdown_custom_cmp(void **state); -void test_rb_topdown_freeenode(void **state); -void test_rb_topdown_custom_cmp_i32(void **state); -void test_rb_topdown_custom_cmp_i64(void **state); - -/* Forward List*/ -void test_flist(void **state); -void test_flist_sep(void **state); - -/* hash table */ -void test_htable(void **state); - -#endif /* test_tests_h */ diff --git a/test/tests.h b/test/tests.h new file mode 100644 index 0000000..9f64462 --- /dev/null +++ b/test/tests.h @@ -0,0 +1,53 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef tests_h +#define tests_h + +#include "include/common.h" + +/* + * To register a test: + * 1. use TEST_DECLARE() to forward declare test + * 2. use TEST_ENTRY() to add test to list + */ + +/* red black tree */ +TEST_DECLARE(rb_topdown_str) +TEST_DECLARE(rb_topdown_ptr) +TEST_DECLARE(rb_topdown_custom_cmp) +TEST_DECLARE(rb_topdown_freeenode) +TEST_DECLARE(rb_topdown_custom_cmp_i32) +TEST_DECLARE(rb_topdown_custom_cmp_i64) + +/* forward list */ +TEST_DECLARE(flist) +TEST_DECLARE(flist_sep) + +/* hash table */ +TEST_DECLARE(htable) + +/*****************************************************************************/ + +TEST_LIST { + /* red black tree */ + TEST_ENTRY(rb_topdown_str) + TEST_ENTRY(rb_topdown_ptr) + TEST_ENTRY(rb_topdown_custom_cmp) + TEST_ENTRY(rb_topdown_freeenode) + TEST_ENTRY(rb_topdown_custom_cmp_i32) + TEST_ENTRY(rb_topdown_custom_cmp_i64) + + /* forward list */ + TEST_ENTRY(flist) + TEST_ENTRY(flist_sep) + + /* hash table */ + TEST_ENTRY(htable) +}; + +#endif /* tests_h */