diff --git a/.gitignore b/.gitignore index 32dd980..79e6c40 100644 --- a/.gitignore +++ b/.gitignore @@ -57,4 +57,5 @@ dkms.conf /libunit /real_tests/real_tests /tests/tests +*.log diff --git a/Makefile b/Makefile index 26f548c..9d16112 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,13 @@ NAME = libunit.a AR = ar rcs CC = cc -CCFLAGS = -Wall -Wextra -Werror -MMD -MP +CCFLAGS = -Wall -Wextra -Werror -MMD -MP -g3 CCFLAGS += -I$(LIBFT_DIR) -I. -Iframework LDFLAGS = -Llibft -lunit -lft -L. LIBFT_DIR = libft LIBFT_A = $(LIBFT_DIR)/libft.a -SRC = framework/libunit_util.c framework/libunit_util2.c framework/libunit.c +SRC = framework/libunit_util.c framework/libunit_util2.c framework/libunit.c framework/libunit_logger.c OBJ = $(SRC:.c=.o) TESTD = tests @@ -38,8 +38,8 @@ fclean: clean @$(MAKE) -C $(TESTD) fclean @$(MAKE) -C $(RTESTD) fclean -re: fclean - @$(MAKE) all +re: fclean all + @$(MAKE) $(TESTB): FORCE @$(MAKE) -C $(TESTD) diff --git a/framework/libunit.c b/framework/libunit.c index cda3a8e..3e97fc0 100644 --- a/framework/libunit.c +++ b/framework/libunit.c @@ -6,94 +6,82 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 12:43:35 by dgaillet #+# #+# */ -/* Updated: 2026/01/24 16:50:51 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 15:15:05 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft/libft.h" #include "libunit.h" +#include #include #include #include -static void print_wtermsig(int signal) +int parent_helper(int ffd, struct s_helper *h, const char *fn_name, + t_unit_test *test_list) { - if (signal == 11) - ft_putstr_fd("[\x1B[33mSIGSEGV\x1B[0m]\n", 1); - else if (signal == 7) - ft_putstr_fd("[\x1B[33mSIGBUS\x1B[0m]\n", 1); - else if (signal == 6) - ft_putstr_fd("[\x1B[33mSIGABRT\x1B[0m]\n", 1); - else if (signal == 8) - ft_putstr_fd("[\x1B[33mSIGFPE\x1B[0m]\n", 1); - else if (signal == 13) - ft_putstr_fd("[\x1B[33mSIGPIPE\x1B[0m]\n", 1); - else if (signal == 4) - ft_putstr_fd("[\x1B[33mSIGILL\x1B[0m]\n", 1); -} + int status; -static size_t interpret_status(int status, const char *fn_name, - char *test_name) -{ - ft_putstr_fd((char *)fn_name, 1); - ft_putstr_fd(" : ", 1); - ft_putstr_fd(test_name, 1); - ft_putstr_fd(" : ", 1); - if (WIFEXITED(status)) - { - if (WEXITSTATUS(status)) - ft_putstr_fd("[\x1B[31m\x1B[1mKO\x1B[0m]\n", 1); - else - { - ft_putstr_fd("[\x1B[32mOK\x1B[0m]\n", 1); - return (1); - } - } - else if (WIFSIGNALED(status)) - print_wtermsig(WTERMSIG(status)); + h->wpid = waitpid(h->wpid, &status, WCONTINUED); + if (h->wpid < 0) + return (-1); + h->ok_tests += interpret_status(ffd, status, fn_name, get_test_at(test_list, + h->i)->title); return (0); } -static void print_passed_test(size_t ok_tests, t_unit_test *test_list) +int child_helper(t_unit_test *test_list, t_h h) { - size_t total; + int fd; + int res; + t_unit_test *test; - total = count_tests(test_list); - if (ok_tests == total) - ft_putstr_fd("\x1B[32m", 1); - else - ft_putstr_fd("\x1B[31m", 1); - ft_putnbr_fd(ok_tests, 1); - ft_putchar_fd('/', 1); - ft_putnbr_fd((int)total, 1); - ft_putstr_fd("\x1B[0m tests succeeded\n\n", 1); + fd = open("/dev/null", O_WRONLY); + if (!fd) + return (-1); + if (dup2(fd, STDOUT_FILENO) < 0) + return (-1); + test = get_test_at(test_list, h.i); + if (test->timeout) + alarm(test->timeout); + res = test->func(); + close(fd); + exit(res); +} + +int handle_out(const char *fn_name) +{ + char *tmp; + int fd; + + tmp = ft_strjoin(fn_name, ".log"); + if (!tmp) + return (-1); + fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0644); + free(tmp); + return (fd); } int launch_tests(t_unit_test *test_list, const char *fn_name) { - size_t ok_tests; - pid_t wpid; - int status; - int i; + int ffd; + t_h h; - ok_tests = 0; - i = -1; - while (++i < (int)count_tests(test_list)) + h.ok_tests = 0; + h.i = -1; + ffd = handle_out(fn_name); + if (ffd < 0) + return (1); + while (++h.i < (int)count_tests(test_list)) { - wpid = fork(); - if (wpid < 0) + h.wpid = fork(); + if (h.wpid < 0) + return (1); + else if (h.wpid == 0 && child_helper(test_list, h) < 0) + return (1); + else if (parent_helper(ffd, &h, fn_name, test_list)) return (1); - else if (wpid == 0) - exit(!get_test_at(test_list, i)->func()); - else - { - wpid = wait(&status); - if (wpid < 0) - return (1); - ok_tests += interpret_status(status, fn_name, get_test_at(test_list, - i)->title); - } } - print_passed_test(ok_tests, test_list); + print_passed_test(ffd, h.ok_tests, test_list); + close(ffd); return (0); } diff --git a/framework/libunit.h b/framework/libunit.h index f68a14c..dd20b2f 100644 --- a/framework/libunit.h +++ b/framework/libunit.h @@ -6,14 +6,14 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 14:38:40 by elagouch #+# #+# */ -/* Updated: 2026/01/24 18:56:03 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 15:09:41 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef LIBUNIT_H # define LIBUNIT_H -# include "libft.h" +# include "../libft/libft.h" # include # include @@ -25,6 +25,7 @@ typedef struct s_unit_test char *title; int (*func)(void); struct s_unit_test *next; + unsigned int timeout; } t_unit_test; /** @@ -53,7 +54,7 @@ size_t count_tests(t_unit_test *head); * @return -1 on error, 0 on success */ size_t load_test(t_unit_test **head_ptr, const char *title, - int (*test_func)(void)); + int (*test_func)(void), unsigned int timeout); /** * @brief clears the tests memory @@ -66,4 +67,17 @@ void clear_tests(t_unit_test **head_ptr); int launch_tests(t_unit_test *test_list, const char *fn_name); +typedef struct s_helper +{ + pid_t wpid; + size_t ok_tests; + int i; +} t_h; + +size_t interpret_status(int ffd, int status, + const char *fn_name, char *test_name); + +void print_passed_test(int ffd, size_t ok_tests, + t_unit_test *test_list); + #endif // !LIBUNIT_H diff --git a/framework/libunit_logger.c b/framework/libunit_logger.c new file mode 100644 index 0000000..dacfe42 --- /dev/null +++ b/framework/libunit_logger.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libunit_logger.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/25 14:26:54 by elagouch #+# #+# */ +/* Updated: 2026/01/25 14:56:06 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libunit.h" + +static void print_wtermsig(int ffd, int signal) +{ + static char *sigs[15]; + char *msg; + + sigs[4] = "SIGKILL"; + sigs[6] = "SIGABRT"; + sigs[7] = "SIGBUS"; + sigs[8] = "SIGFPE"; + sigs[11] = "SIGSEGV"; + sigs[13] = "SIGPIPE"; + sigs[14] = "TIMEOUT"; + ft_putstr_fd("[\x1B[33m", 1); + ft_putstr_fd("[", ffd); + if ((signal >= 6 && signal <=8) || signal == 4 || signal == 11 || (signal >= 13 && signal <= 14)) + msg = sigs[signal]; + else + msg = "UNKNOWN"; + ft_putstr_fd(msg, 1); + ft_putstr_fd(msg, ffd); + ft_putstr_fd("\x1B[0m]\n", 1); + ft_putstr_fd("]\n", ffd); +} + +size_t interpret_status(int ffd, int status, const char *fn_name, + char *test_name) +{ + ft_putstr_fd((char *)fn_name, 1); + ft_putstr_fd((char *)fn_name, ffd); + ft_putstr_fd(" : ", 1); + ft_putstr_fd(" : ", ffd); + ft_putstr_fd(test_name, 1); + ft_putstr_fd(test_name, ffd); + ft_putstr_fd(" : ", 1); + ft_putstr_fd(" : ", ffd); + if (WIFEXITED(status)) + { + if (WEXITSTATUS(status)) + { + ft_putstr_fd("[\x1B[31m\x1B[1mKO\x1B[0m]\n", 1); + ft_putstr_fd("[KO]\n", ffd); + } + else + { + ft_putstr_fd("[\x1B[32mOK\x1B[0m]\n", 1); + ft_putstr_fd("[OK]\n", ffd); + return (1); + } + } + else if (WIFSIGNALED(status)) + print_wtermsig(ffd, WTERMSIG(status)); + return (0); +} + +void print_passed_test(int ffd, size_t ok_tests, t_unit_test *test_list) +{ + size_t total; + + total = count_tests(test_list); + if (ok_tests == total) + ft_putstr_fd("\x1B[32m", 1); + else + ft_putstr_fd("\x1B[31m", 1); + ft_putnbr_fd(ok_tests, 1); + ft_putnbr_fd(ok_tests, ffd); + ft_putchar_fd('/', 1); + ft_putchar_fd('/', ffd); + ft_putnbr_fd((int)total, 1); + ft_putnbr_fd((int)total, ffd); + ft_putstr_fd("\x1B[0m tests succeeded\n\n", 1); + ft_putstr_fd(" tests succeeded\n\n", ffd); +} diff --git a/framework/libunit_util.c b/framework/libunit_util.c index 1bdb2b3..7cbc2d2 100644 --- a/framework/libunit_util.c +++ b/framework/libunit_util.c @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 14:35:57 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:15:35 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 15:11:24 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ t_unit_test *get_last(t_unit_test *head) /** * @brief util function to allocate a new test */ -t_unit_test *alloc_test(const char *title, int (*test_func)(void)) +static t_unit_test *alloc_test(const char *title, int (*test_func)(void), unsigned int timeout) { t_unit_test *p; @@ -47,11 +47,12 @@ t_unit_test *alloc_test(const char *title, int (*test_func)(void)) p->title = ft_strdup(title); p->func = test_func; p->next = NULL; + p->timeout = timeout; return (p); } size_t load_test(t_unit_test **head_ptr, const char *title, - int (*test_func)(void)) + int (*test_func)(void), unsigned int timeout) { t_unit_test *p; t_unit_test *last; @@ -60,14 +61,14 @@ size_t load_test(t_unit_test **head_ptr, const char *title, return (-1); if (!*head_ptr) { - p = alloc_test(title, test_func); + p = alloc_test(title, test_func, timeout); if (!p) return (-1); *head_ptr = p; return (0); } last = get_last(*head_ptr); - last->next = alloc_test(title, test_func); + last->next = alloc_test(title, test_func, timeout); if (!last->next) return (-1); return (0); diff --git a/real_tests/ft_atoi/00_launcher.c b/real_tests/ft_atoi/00_launcher.c index 40649c3..95d5929 100644 --- a/real_tests/ft_atoi/00_launcher.c +++ b/real_tests/ft_atoi/00_launcher.c @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 16:22:35 by dgaillet #+# #+# */ -/* Updated: 2026/01/24 16:22:36 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/25 16:32:35 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,10 @@ int atoi_launcher(void) size_t res; testlist = NULL; - load_test(&testlist, "Basic test", &atoi_basic_test); - load_test(&testlist, "INT MAX test", &atoi_int_max_test); - load_test(&testlist, "INT MIN test", &atoi_int_min_test); - load_test(&testlist, "NULL test", &atoi_null_test); + load_test(&testlist, "Basic test", &atoi_basic_test, 0); + load_test(&testlist, "INT MAX test", &atoi_int_max_test, 0); + load_test(&testlist, "INT MIN test", &atoi_int_min_test, 0); + load_test(&testlist, "NULL test", &atoi_null_test, 0); res = launch_tests(testlist, "atoi"); clear_tests(&testlist); return (res); diff --git a/real_tests/ft_atoi/01_basic.c b/real_tests/ft_atoi/01_basic.c index 04912c4..6718671 100644 --- a/real_tests/ft_atoi/01_basic.c +++ b/real_tests/ft_atoi/01_basic.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 01_ok.c :+: :+: :+: */ +/* 01_basic.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 17:22:48 by dgaillet #+# #+# */ -/* Updated: 2026/01/24 17:22:51 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/25 16:33:19 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,5 +15,5 @@ int atoi_basic_test(void) { - return (ft_atoi("42") == 42); + return (!(ft_atoi("42") == 42)); } diff --git a/real_tests/ft_atoi/02_int_max.c b/real_tests/ft_atoi/02_int_max.c index 87e4cc3..af11e5d 100644 --- a/real_tests/ft_atoi/02_int_max.c +++ b/real_tests/ft_atoi/02_int_max.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 02_ko.c :+: :+: :+: */ +/* 02_int_max.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 17:23:00 by dgaillet #+# #+# */ -/* Updated: 2026/01/24 17:23:01 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/25 16:33:35 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,5 @@ int atoi_int_max_test(void) { - return (ft_atoi("2147483647") == 2147483647); + return (!(ft_atoi("2147483647") == 2147483647)); } diff --git a/real_tests/ft_atoi/03_int_min.c b/real_tests/ft_atoi/03_int_min.c index 2ece39c..e19931b 100644 --- a/real_tests/ft_atoi/03_int_min.c +++ b/real_tests/ft_atoi/03_int_min.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 03_sigsegv.c :+: :+: :+: */ +/* 03_int_min.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 17:23:10 by dgaillet #+# #+# */ -/* Updated: 2026/01/24 17:23:11 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/25 16:33:46 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,5 @@ int atoi_int_min_test(void) { - return (ft_atoi("-2147483648") == -2147483648); + return (!(ft_atoi("-2147483648") == -2147483648)); } diff --git a/real_tests/ft_strlen/00_launcher.c b/real_tests/ft_strlen/00_launcher.c index 10d6ad5..127461a 100644 --- a/real_tests/ft_strlen/00_launcher.c +++ b/real_tests/ft_strlen/00_launcher.c @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:32:32 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:34:12 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 15:13:57 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,9 @@ int strlen_launcher(void) size_t res; testlist = NULL; - load_test(&testlist, "null", &test_null); - load_test(&testlist, "basic", &test_basic); - load_test(&testlist, "large", &test_large); + load_test(&testlist, "null", &test_null, 0); + load_test(&testlist, "basic", &test_basic, 0); + load_test(&testlist, "large", &test_large, 0); res = launch_tests(testlist, "strlen"); clear_tests(&testlist); return (res); diff --git a/real_tests/ft_strlen/01_basic.c b/real_tests/ft_strlen/01_basic.c index 3c5047e..282ef0e 100644 --- a/real_tests/ft_strlen/01_basic.c +++ b/real_tests/ft_strlen/01_basic.c @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:17:14 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 15:16:59 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,5 @@ int test_basic(void) { - return (ft_strlen("hello, world") == 12); + return (!(ft_strlen("hello, world") == 12)); } diff --git a/real_tests/ft_strlen/02_null.c b/real_tests/ft_strlen/02_null.c index fbf978f..baaa7ae 100644 --- a/real_tests/ft_strlen/02_null.c +++ b/real_tests/ft_strlen/02_null.c @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:17:04 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 15:16:28 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,5 @@ int test_null(void) { - return (ft_strlen(NULL) == 0); + return (!(ft_strlen(NULL) == 0)); } diff --git a/real_tests/ft_strlen/03_large.c b/real_tests/ft_strlen/03_large.c index f471b2e..fefd46d 100644 --- a/real_tests/ft_strlen/03_large.c +++ b/real_tests/ft_strlen/03_large.c @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:36:42 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 15:16:42 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,5 +23,5 @@ int test_large(void) s[i] = ' '; i++; } - return (ft_strlen(s) == sizeof(s) + 1); + return (!(ft_strlen(s) == sizeof(s) + 1)); } diff --git a/real_tests/ft_strncmp/00_launcher.c b/real_tests/ft_strncmp/00_launcher.c index 6fa1cb7..0af3cd9 100644 --- a/real_tests/ft_strncmp/00_launcher.c +++ b/real_tests/ft_strncmp/00_launcher.c @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/25 12:05:05 by dgaillet #+# #+# */ -/* Updated: 2026/01/25 12:05:17 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/25 16:35:04 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,9 @@ int strncmp_launcher(void) size_t res; testlist = NULL; - load_test(&testlist, "Basic test", &strncmp_basic_test); - load_test(&testlist, "Len test", &strncmp_len_test); - load_test(&testlist, "High len test", &strncmp_high_len_test); + load_test(&testlist, "Basic test", &strncmp_basic_test, 0); + load_test(&testlist, "Len test", &strncmp_len_test, 0); + load_test(&testlist, "High len test", &strncmp_high_len_test, 0); res = launch_tests(testlist, "ft_strncmp"); clear_tests(&testlist); return (res); diff --git a/real_tests/ft_strncmp/01_basic.c b/real_tests/ft_strncmp/01_basic.c index cd49484..63e8c12 100644 --- a/real_tests/ft_strncmp/01_basic.c +++ b/real_tests/ft_strncmp/01_basic.c @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/25 12:05:24 by dgaillet #+# #+# */ -/* Updated: 2026/01/25 12:05:25 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/25 15:15:39 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,5 @@ int strncmp_basic_test(void) { - return (!ft_strncmp("test", "test", 4)); + return (ft_strncmp("test", "test", 4)); } diff --git a/real_tests/ft_strncmp/02_len.c b/real_tests/ft_strncmp/02_len.c index b9ad60b..d8d1db7 100644 --- a/real_tests/ft_strncmp/02_len.c +++ b/real_tests/ft_strncmp/02_len.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 02_ko.c :+: :+: :+: */ +/* 02_len.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/25 12:05:30 by dgaillet #+# #+# */ -/* Updated: 2026/01/25 12:05:32 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/25 16:34:24 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,5 @@ int strncmp_len_test(void) { - return (!ft_strncmp("test123", "test", 4)); + return (ft_strncmp("test123", "test", 4)); } diff --git a/real_tests/ft_strncmp/03_high_len.c b/real_tests/ft_strncmp/03_high_len.c index c98eb45..d94c301 100644 --- a/real_tests/ft_strncmp/03_high_len.c +++ b/real_tests/ft_strncmp/03_high_len.c @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/25 16:01:01 by dgaillet #+# #+# */ -/* Updated: 2026/01/25 16:01:03 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/25 16:35:34 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,5 @@ int strncmp_high_len_test(void) { - return (!ft_strncmp("test", "test", 42)); + return (ft_strncmp("test", "test", 42)); } diff --git a/tests/Makefile b/tests/Makefile index f78f9b0..e53363c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,9 +1,9 @@ NAME = tests CC = cc -CCFLAGS = -Wall -Wextra -Werror -MMD -MP +CCFLAGS = -Wall -Wextra -Werror -MMD -MP -g3 SRC = main.c -SRC += libunit/00_launcher.c libunit/01_ok.c libunit/02_ko.c libunit/03_sigsegv.c libunit/04_sigbus.c +SRC += libunit/00_launcher.c libunit/01_ok.c libunit/02_ko.c libunit/03_sigsegv.c libunit/04_sigbus.c libunit/05_sigkill.c libunit/06_sigabrt.c libunit/07_sigfpe.c libunit/08_sigpipe.c libunit/09_timeout.c OBJ = $(SRC:.c=.o) LIBS = -L.. -lunit -L../libft -lft diff --git a/tests/libunit/00_launcher.c b/tests/libunit/00_launcher.c index 4f9aa3e..8ba0bad 100644 --- a/tests/libunit/00_launcher.c +++ b/tests/libunit/00_launcher.c @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:32:32 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:47:43 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 16:41:49 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,15 @@ int libunit_launcher(void) size_t res; testlist = NULL; - load_test(&testlist, "successful", &test_ok); - load_test(&testlist, "unsuccessful", &test_ko); - load_test(&testlist, "sigsegv", &test_sigsegv); - load_test(&testlist, "sigbus", &test_sigbus); + load_test(&testlist, "successful", &test_ok, 0); + load_test(&testlist, "unsuccessful", &test_ko, 0); + load_test(&testlist, "sigsegv", &test_sigsegv, 0); + load_test(&testlist, "sigbus", &test_sigbus, 0); + load_test(&testlist, "sigkill", &test_sigkill, 0); + load_test(&testlist, "sigabrt", &test_sigabrt, 0); + load_test(&testlist, "sigfpe", &test_sigfpe, 0); + load_test(&testlist, "sigpipe", &test_sigpipe, 0); + load_test(&testlist, "timeout", &test_timeout, 1); res = launch_tests(testlist, "libunit"); clear_tests(&testlist); return (res); diff --git a/tests/libunit/01_ok.c b/tests/libunit/01_ok.c index 818f7d3..1c64142 100644 --- a/tests/libunit/01_ok.c +++ b/tests/libunit/01_ok.c @@ -6,11 +6,11 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:16:32 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 16:36:02 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ int test_ok(void) { - return (1); + return (0); } diff --git a/tests/libunit/02_ko.c b/tests/libunit/02_ko.c index f8cf1a2..d740ab8 100644 --- a/tests/libunit/02_ko.c +++ b/tests/libunit/02_ko.c @@ -6,11 +6,11 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:16:37 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 16:36:07 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ int test_ko(void) { - return (0); + return (1); } diff --git a/tests/libunit/03_sigsegv.c b/tests/libunit/03_sigsegv.c index b92429e..57f0d15 100644 --- a/tests/libunit/03_sigsegv.c +++ b/tests/libunit/03_sigsegv.c @@ -6,14 +6,14 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:19:43 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 16:36:33 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#include "../tests.h" int test_sigsegv(void) { - raise(SIGSEGV); + return (ft_atoi(NULL) == 0); return (1); } diff --git a/tests/libunit/04_sigbus.c b/tests/libunit/04_sigbus.c index 06aca74..94a4659 100644 --- a/tests/libunit/04_sigbus.c +++ b/tests/libunit/04_sigbus.c @@ -6,14 +6,20 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:19:50 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 15:22:52 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#include int test_sigbus(void) { - raise(SIGBUS); - return (1); + char *cptr; + int *iptr; + + __asm__("pushf\norl $0x40000,(%rsp)\npopf"); + cptr = malloc(sizeof(int) + 1); + iptr = (int *)++cptr; + *iptr = 42; + return (1); } diff --git a/tests/libunit/05_sigkill.c b/tests/libunit/05_sigkill.c new file mode 100644 index 0000000..443d266 --- /dev/null +++ b/tests/libunit/05_sigkill.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 05_sigkill.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ +/* Updated: 2026/01/25 16:38:20 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int test_sigkill(void) +{ + raise(SIGKILL); + return (1); +} diff --git a/tests/libunit/06_sigabrt.c b/tests/libunit/06_sigabrt.c new file mode 100644 index 0000000..494d836 --- /dev/null +++ b/tests/libunit/06_sigabrt.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 06_sigabrt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ +/* Updated: 2026/01/25 16:38:37 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int test_sigabrt(void) +{ + raise(SIGABRT); + return (1); +} diff --git a/tests/libunit/07_sigfpe.c b/tests/libunit/07_sigfpe.c new file mode 100644 index 0000000..4cdaf87 --- /dev/null +++ b/tests/libunit/07_sigfpe.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 07_sigfpe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ +/* Updated: 2026/01/25 16:39:11 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int test_sigfpe(void) +{ + raise(SIGFPE); + return (1); +} diff --git a/tests/libunit/08_sigpipe.c b/tests/libunit/08_sigpipe.c new file mode 100644 index 0000000..66a212a --- /dev/null +++ b/tests/libunit/08_sigpipe.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 08_sigpipe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ +/* Updated: 2026/01/25 16:39:33 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int test_sigpipe(void) +{ + raise(SIGPIPE); + return (1); +} diff --git a/tests/libunit/09_timeout.c b/tests/libunit/09_timeout.c new file mode 100644 index 0000000..e66d0c1 --- /dev/null +++ b/tests/libunit/09_timeout.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 09_timeout.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ +/* Updated: 2026/01/25 16:40:07 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int test_timeout(void) +{ + while(1) + {} + return (1); +} diff --git a/tests/tests.h b/tests/tests.h index 0fc03ff..0b8d409 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:32:30 by elagouch #+# #+# */ -/* Updated: 2026/01/24 18:41:02 by elagouch ### ########.fr */ +/* Updated: 2026/01/25 16:40:37 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,5 +23,10 @@ int test_ok(void); int test_ko(void); int test_sigsegv(void); int test_sigbus(void); +int test_sigkill(void); +int test_sigabrt(void); +int test_sigfpe(void); +int test_sigpipe(void); +int test_timeout(void); #endif // !TESTS_H