libdev.a

C Helper library
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/libdev.a

commit ad606b2466c810c50f817a3fac0fd5dc020e1727
parent 0e521d10213a2d7469fb243e4028c117ecb69198
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 10 Oct 2016 11:23:01 +0200

Add(Unit Tests):

Add Unit Tests to the library
Add a function to search in a linked list

Diffstat:
Mlibdev.h | 32++++++++++++++++++++++++++++++--
Mlists.c | 13+++++++++++++
Atests.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 116 insertions(+), 2 deletions(-)

diff --git a/libdev.h b/libdev.h @@ -26,7 +26,7 @@ # define BACKTRACE_SIZE 10 -#define L_ASSERT(condition)\ +# define L_ASSERT(condition)\ {\ if (!condition) {\ warning("Assertion (%s) failed at %s:%d\n", #condition, __FILE__, __LINE__);\ @@ -36,6 +36,18 @@ }\ } +# define TEST(name) char *test_##name(void) +# define reg_test(group, name) register_test(group, &test_##name, #name); +# define T_ASSERT(condition, error_name) {\ + if (!condition) {\ + char *ret = malloc(250);\ + sprintf(ret, "\t%s: Test: '%s', File %s:%d", error_name, #condition, __FILE__, __LINE__);\ + return (ret);\ + }\ +} +# define TESTS_LIST 0x1 +# define TEST_SUCCESS 0x0 + /* TYPEDEFS */ // Signed typedef signed char s8_t; @@ -49,7 +61,6 @@ typedef unsigned short u16_t; typedef unsigned int u32_t; typedef unsigned long long u64_t; - /* STRUCTS */ typedef struct s_list { void *member; // Actual member @@ -64,6 +75,18 @@ typedef struct s_singleton { t_list *ptr; // Actual pointer to the list } t_singleton; +typedef struct s_test { + char *(*fn_test)(void); + char *group; + char *name; +} t_test; + +typedef struct s_test_results { + size_t success; + size_t failed; + size_t total; +} t_test_results; + /* FUNCTIONS DEFINITIONS */ // lists.c @@ -72,6 +95,7 @@ t_list *list_get_last(t_list *list); t_list *list_insert_after(t_list *org, t_list *ptr, void *member, size_t size); t_list *list_insert_before(t_list *org, t_list *ptr, void *member, size_t size); size_t list_size(t_list *list); +void *list_get(t_list *list, void *ptr, size_t size); // singleton.c t_list *singleton_lists(u_char list_type, t_list *ptr); @@ -82,4 +106,8 @@ void info(char *str, ...); void warning(char *str, ...); void print_trace(void); +// tests.c +void register_test(char *group, char *(*test)(void), char *name); +t_test_results test_group(char *group); + #endif /* __LIBDEV__ */ diff --git a/lists.c b/lists.c @@ -92,3 +92,16 @@ size_t list_size(t_list *list) { for (tmp = list, i = 0; tmp; tmp = tmp->next, i++); return i; } + +void *list_get(t_list *list, void *member, size_t size) { + t_list *tmp; + void *ptr; + + if (!list) + return 0x0; + list_for_each(list, tmp, ptr) { + if (!memcmp(ptr, member, size)) + return ptr; + } + return 0x0; +} diff --git a/tests.c b/tests.c @@ -0,0 +1,73 @@ +#include <libdev.h> + +void register_test(char *group, char *(*fn_test)(void), char *name) { + t_list *tests = singleton_lists(TESTS_LIST, 0x0); + t_test *ptr; + + ptr = malloc(sizeof(t_test)); + L_ASSERT(ptr); + ptr->group = malloc(strlen(group)); + ptr->name = malloc(strlen(name)); + L_ASSERT(ptr->group && ptr->name); + strcpy(ptr->group, group); + strcpy(ptr->name, name); + ptr->fn_test = fn_test; + list_add(tests, ptr, sizeof(t_test)); + singleton_lists(TESTS_LIST, tests); +} + +t_test_results test_group(char *group) { + t_list *tests = singleton_lists(TESTS_LIST, 0x0), *tmp; + t_test *ptr; + t_test_results res; + size_t tab; + char *s_tmp; + + res.total = res.success = res.failed = 0; + info("======================= GROUP %s TESTS =======================\n", group); + list_for_each(tests, tmp, ptr) { + if (!strcmp(ptr->group, group)) { + res.total++; + info("Testing %s ...", ptr->name); + for (tab = strlen(ptr->name); tab < 40; tab++, printf(" ")); + if ((s_tmp = ptr->fn_test())) { + printf("[ \033[1;31mFAILED\033[0m ]\n"); + warning("\033[0;37m%s\033[0m\n", s_tmp); + free(s_tmp); + res.failed++; + } else { + printf("[ \033[1;32mOK\033[0m ]\n"); + res.success++; + } + } + } + if (!res.total) { + warning("Could not find any registered tests in %s group.\n", group); + } else { + info("Results: Total: %d, Success: %d, Failed: %d. COVERAGE: %d%%\n", + res.total, res.success, res.failed, (res.success * 100) / res.total); + } + return res; +} + +void test_all(void) { + t_list *tests = singleton_lists(TESTS_LIST, 0x0), *tmp, *groups = 0x0; + t_test *ptr; + t_test_results res; + size_t total = 0, success = 0, failed = 0; + + list_for_each(tests, tmp, ptr) { + if (!list_get(groups, ptr->group, strlen(ptr->group))) { + res = test_group(ptr->group); + total += res.total; + success += res.success; + failed += res.failed; + list_add(groups, ptr->group, strlen(ptr->group)); + } + } + printf("\n"); + info("============================= RESULTS =============================\n"); + info("\033[1;32mTESTS SUCCESS\033[0m:\t%d\n", success); + info("\033[1;31mTESTS FAILED\033[0m:\t\t%d\n", failed); + info("\033[1;34mTOTAL COVERAGE\033[0m:\t%d%%\n", (success * 100) / total); +}