lib

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

commit 110220ffcba120cceed09bed8d80a966fb3228ca
parent 40e599318f5aba88eb272d84aa3494b0cd4781b9
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 20 Jan 2017 16:42:28 +0100

Add(Tests): Beginning of tests on margs

Diffstat:
Minc/m_args.h | 2++
Msrc/m_args.c | 5+++++
Mtests/main.c | 1+
Mtests/test.h | 13+++++++++++++
Atests/test_args.c | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 127 insertions(+), 0 deletions(-)

diff --git a/inc/m_args.h b/inc/m_args.h @@ -52,6 +52,8 @@ typedef struct s_args { void (*callback)(const char *); } margs_t; +#define ARGS_EOL {0, NULL, NULL, false, NULL} + u32_t read_opt(const int ac, char **av, const margs_t *args); void opt_help(const margs_t *args); diff --git a/src/m_args.c b/src/m_args.c @@ -36,6 +36,9 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { u32_t ret = 0, it, k; u8_t n_dash; + if (ac == 0 || av == NULL || args == NULL) + return ret; + for (u32_t i = 1, j = 0; i < (u32_t)ac; i++) { if (av[i] == NULL || strlen(av[i]) == 0) @@ -86,6 +89,7 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { } } else { args[it].callback(NULL); + ret++; } } } @@ -135,6 +139,7 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { args[it].callback(&(av[i][k + 3])); else args[it].callback(NULL); + ret++; } } } diff --git a/tests/main.c b/tests/main.c @@ -4,6 +4,7 @@ int main(void) { u32_t ret; register_infos_tests(); + register_args_tests(); m_info("Beginning tests...\n"); ret = test_all(); diff --git a/tests/test.h b/tests/test.h @@ -2,7 +2,20 @@ # define TEST_H # include <morphux.h> +# include <unistd.h> +# include <sys/types.h> +# include <sys/wait.h> void register_infos_tests(void); +void register_args_tests(void); + +struct margs_tests { + bool opt_q; + bool opt_w; + bool opt_e; + bool opt_r; + bool opt_t; + bool opt_y; +}; #endif /* TEST_H */ diff --git a/tests/test_args.c b/tests/test_args.c @@ -0,0 +1,106 @@ +#include "test.h" + +static struct margs_tests args; + +TEST(args_NULL) { + TEST_ASSERT(read_opt(0, NULL, NULL) == 0, "Not handling null arguments"); + return TEST_SUCCESS; +} + +TEST(args_NULL_1) { + TEST_ASSERT(read_opt(10, NULL, NULL) == 0, "Not handling null arguments"); + return TEST_SUCCESS; +} + +TEST(args_NULL_2) { + char *tab[] = {"test", "test2"}; + TEST_ASSERT(read_opt(0, tab, NULL) == 0, "Not handling null arguments"); + return TEST_SUCCESS; +} + +TEST(args_NULL_3) { + char *tab[] = {"test", "test2"}; + TEST_ASSERT(read_opt(10, tab, NULL) == 0, "Not handling null arguments"); + return TEST_SUCCESS; +} + +TEST(args_NULL_4) { + char *tab[] = {"test", "test2"}; + margs_t t; + + TEST_ASSERT(read_opt(0, tab, &t) == 0, "Not handling null arguments"); + return TEST_SUCCESS; +} + +TEST(args_NULL_5) { + margs_t t; + + TEST_ASSERT(read_opt(10, NULL, &t) == 0, "Not handling null arguments"); + return TEST_SUCCESS; +} + +TEST(args_empty_1) { + margs_t t; + char *tab[] = {"", "", ""}; + + TEST_ASSERT(read_opt(3, tab, &t) == 0, "Not handling null arguments"); + return TEST_SUCCESS; +} + +TEST(args_empty_2) { + margs_t t; + char *tab[] = {NULL, NULL, NULL}; + + TEST_ASSERT(read_opt(3, tab, &t) == 0, "Not handling null arguments"); + return TEST_SUCCESS; +} + + +TEST(args_unhandled_1) { + fflush(stdout); + margs_t opt[] = { + {'z', "--zoiberg", "No idea.", false, NULL}, + ARGS_EOL + }; + char *av[] = {"./tests", "oui", "--allow"}; + pid_t pid; + int st, fd[2]; + + pipe(fd); + + if ((pid = fork()) == 0) { + + dup2(fd[1], 1); + dup2(fd[1], 2); + close(fd[1]); + + TEST_ASSERT(read_opt(sizeof(av), av, opt) == 0, "Not handling properly unknown arguments"); + } else { + pid = waitpid(pid, &st, 0); + close(fd[1]); + TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return"); + } + return TEST_SUCCESS; +} + + +void reset_args(void) { + args.opt_q = false; + args.opt_w = false; + args.opt_e = false; + args.opt_r = false; + args.opt_t = false; + args.opt_y = false; +} + +void register_args_tests(void) { + reg_test("m_args", args_NULL); + reg_test("m_args", args_NULL_1); + reg_test("m_args", args_NULL_2); + reg_test("m_args", args_NULL_3); + reg_test("m_args", args_NULL_4); + reg_test("m_args", args_NULL_5); + reg_test("m_args", args_empty_1); + reg_test("m_args", args_empty_2); + reg_test("m_args", args_unhandled_1); +}