lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit feff184ecb323a4373ddfbfb35704678dbc6fe8f
parent 8b26ce1d4940e09a52925eab6ac36534fd3b5504
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu, 19 Jan 2017 16:57:00 +0100

Add(Tests)

Diffstat:
Minc/m_args.h | 4++--
Minc/m_test.h | 6+++---
Minc/morphux.h | 2++
Msrc/m_args.c | 4++--
Msrc/m_infos.c | 18+++++++++++++++---
Msrc/m_test.c | 9++++++---
Atests/.gdb_history | 3+++
Atests/Makefile | 24++++++++++++++++++++++++
Atests/main.c | 13+++++++++++++
Atests/test.h | 8++++++++
Atests/test_infos.c | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 176 insertions(+), 13 deletions(-)

diff --git a/inc/m_args.h b/inc/m_args.h @@ -54,7 +54,7 @@ typedef struct s_args { u32_t read_opt(const int ac, char **av, const margs_t *args); -static void opt_help(const margs_t *args); -static void p_version(void); +void opt_help(const margs_t *args); +void p_version(void); #endif /* M_ARGS_H */ diff --git a/inc/m_test.h b/inc/m_test.h @@ -50,11 +50,11 @@ typedef struct s_test_results { } /* Functions */ -static void title(char *s); +void title(char *s); void register_test(char *group, char *(*fn_test)(void), char *name); mtest_results_t test_group(char *group); -void test_all(void); +u32_t test_all(void); void test_free(void); -static int single_test_free(void *ptr); +int single_test_free(void *ptr); #endif /* M_TEST_H */ diff --git a/inc/morphux.h b/inc/morphux.h @@ -17,6 +17,8 @@ #ifndef MORPHUX_H # define MORPHUX_H +# define _GNU_SOURCE + # include <m_types.h> # include <m_print.h> # include <m_args.h> diff --git a/src/m_args.c b/src/m_args.c @@ -145,7 +145,7 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { * \brief Print helps with a list of argument * \param args List of arguments to print */ -static void opt_help(const margs_t *args) { +void opt_help(const margs_t *args) { m_info("Help:\n"); for (u32_t i = 0; args[i].opt != 0; i++) { m_info("\t-%c | --%s : %s\n", args[i].opt, args[i].s_opt, args[i].desc); @@ -160,7 +160,7 @@ static void opt_help(const margs_t *args) { /*! * \brief Print the program name, the version and the maintainer, then exit */ -static void p_version(void) { +void p_version(void) { m_info("Program: %s\n", get_program_name()); m_info("Version: %s\n", get_version()); m_info("%s\n", get_maintainer()); diff --git a/src/m_infos.c b/src/m_infos.c @@ -26,21 +26,33 @@ static char maintainer[INFOS_G_LEN_MAX] = ""; * Set the program name to a string */ void set_program_name(const char *str) { - memcpy(program, str, strlen(str)); + if (str == NULL) { + strcpy(program, ""); + } else { + strcpy(program, str); + } } /*! * Set the version name to a string */ void set_version(const char *str) { - memcpy(version, str, strlen(str)); + if (str == NULL) { + strcpy(version, ""); + } else { + strcpy(version, str); + } } /*! * Set the maintainer name to a string */ void set_maintainer(const char *str) { - memcpy(maintainer, str, strlen(str)); + if (str == NULL) { + strcpy(maintainer, ""); + } else { + strcpy(maintainer, str); + } } /*! diff --git a/src/m_test.c b/src/m_test.c @@ -22,7 +22,7 @@ static mlist_t *tests = NULL; * \brief Print a title * \param s Title name */ -static void title(char *s) { +void title(char *s) { u8_t len = TITLE_LEN; int i; @@ -114,8 +114,9 @@ mtest_results_t test_group(char *group) { /*! * \brief Test all registered tests + * \return Numbers of tests failed */ -void test_all(void) { +u32_t test_all(void) { mlist_t *tmp, *groups = NULL; mtest_t *ptr; mtest_results_t res; @@ -139,6 +140,8 @@ void test_all(void) { printf("\n"); m_info("Results: %d%% (%d/%d)\n", (success * 100) / total, success, total); } + + return failed; } /*! @@ -153,7 +156,7 @@ void test_free(void) { * \brief Free a single test * \note Used in test_free, as a list_free callback */ -static int single_test_free(void *ptr) { +int single_test_free(void *ptr) { mtest_t *tmp = ptr; if (ptr) { diff --git a/tests/.gdb_history b/tests/.gdb_history @@ -0,0 +1,3 @@ +r +bt +q diff --git a/tests/Makefile b/tests/Makefile @@ -0,0 +1,24 @@ +NAME = test +CC = gcc +CFLAGS = -Wall -Wextra -g -O3 -I../inc -lmorphux +SRCS = $(wildcard *.c) +OBJS = $(SRCS:%.c=%.o) + +all: $(NAME) + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) $(OBJS) -o $(NAME) -L../ -lmorphux + +check: $(NAME) + ./$(NAME) + +valgrind: $(NAME) + valgrind --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all --error-exitcode=1 ./$(NAME) + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all diff --git a/tests/main.c b/tests/main.c @@ -0,0 +1,13 @@ +#include "test.h" + +int main(void) { + u32_t ret; + + register_infos_tests(); + + m_info("Beginning tests...\n"); + ret = test_all(); + test_free(); + + return ret; +} diff --git a/tests/test.h b/tests/test.h @@ -0,0 +1,8 @@ +#ifndef TEST_H +# define TEST_H + +# include <morphux.h> + +void register_infos_tests(void); + +#endif /* TEST_H */ diff --git a/tests/test_infos.c b/tests/test_infos.c @@ -0,0 +1,98 @@ +#include "test.h" + +TEST(program_name_1) { + TEST_ASSERT((get_program_name() == NULL), "Wrong program name init"); + return TEST_SUCCESS; +} + +TEST(program_name_2) { + set_program_name("Hey !"); + TEST_ASSERT(strcmp(get_program_name(), "Hey !") == 0, "Wrong program_name return"); + return TEST_SUCCESS; +} + +TEST(program_name_3) { + TEST_ASSERT(strcmp(get_program_name(), "Hey !") == 0, "Wrong program_name return"); + set_program_name("Yo !"); + TEST_ASSERT(strcmp(get_program_name(), "Yo !") == 0, "Wrong program_name return"); + return TEST_SUCCESS; +} + +TEST(program_name_4) { + TEST_ASSERT(strcmp(get_program_name(), "Yo !") == 0, "Wrong program_name return"); + set_program_name(""); + TEST_ASSERT((get_program_name() == NULL), "Wrong program_name return"); + set_program_name(NULL); + TEST_ASSERT((get_program_name() == NULL), "Wrong program_name return"); + return TEST_SUCCESS; +} + +TEST(version_1) { + TEST_ASSERT((get_version() == NULL), "Wrong program name init"); + return TEST_SUCCESS; +} + +TEST(version_2) { + set_version("Hey !"); + TEST_ASSERT(strcmp(get_version(), "Hey !") == 0, "Wrong program_name return"); + return TEST_SUCCESS; +} + +TEST(version_3) { + TEST_ASSERT(strcmp(get_version(), "Hey !") == 0, "Wrong program_name return"); + set_version("Yo !"); + TEST_ASSERT(strcmp(get_version(), "Yo !") == 0, "Wrong program_name return"); + return TEST_SUCCESS; +} + +TEST(version_4) { + TEST_ASSERT(strcmp(get_version(), "Yo !") == 0, "Wrong program_name return"); + set_version(""); + TEST_ASSERT((get_version() == NULL), "Wrong program_name return"); + set_version(NULL); + TEST_ASSERT((get_version() == NULL), "Wrong program_name return"); + return TEST_SUCCESS; +} + +TEST(maintainer_1) { + TEST_ASSERT((get_version() == NULL), "Wrong program name init"); + return TEST_SUCCESS; +} + +TEST(maintainer_2) { + set_version("Hey !"); + TEST_ASSERT(strcmp(get_version(), "Hey !") == 0, "Wrong program_name return"); + return TEST_SUCCESS; +} + +TEST(maintainer_3) { + TEST_ASSERT(strcmp(get_version(), "Hey !") == 0, "Wrong program_name return"); + set_version("Yo !"); + TEST_ASSERT(strcmp(get_version(), "Yo !") == 0, "Wrong program_name return"); + return TEST_SUCCESS; +} + +TEST(maintainer_4) { + TEST_ASSERT(strcmp(get_version(), "Yo !") == 0, "Wrong program_name return"); + set_version(""); + TEST_ASSERT((get_version() == NULL), "Wrong program_name return"); + set_version(NULL); + TEST_ASSERT((get_version() == NULL), "Wrong program_name return"); + return TEST_SUCCESS; +} + + +void register_infos_tests(void) { + reg_test("m_infos", program_name_1); + reg_test("m_infos", program_name_2); + reg_test("m_infos", program_name_3); + reg_test("m_infos", program_name_4); + reg_test("m_infos", version_1); + reg_test("m_infos", version_2); + reg_test("m_infos", version_3); + reg_test("m_infos", version_4); + reg_test("m_infos", maintainer_1); + reg_test("m_infos", maintainer_2); + reg_test("m_infos", maintainer_3); + reg_test("m_infos", maintainer_4); +}