From ec0df49e5245cf0bc775ffc65e75c87853e93316 Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Sat, 24 Jan 2026 15:26:42 +0100 Subject: [PATCH] fix some stuff --- Makefile | 2 +- launch_test.c | 34 +++++++++++++++------------------- util_unit.c | 29 +---------------------------- util_unit2.c | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 48 deletions(-) create mode 100644 util_unit2.c diff --git a/Makefile b/Makefile index cce932f..8d01a12 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CXXFLAGS = -Wall -Wextra -Werror -MMD -MP CXXFLAGS += -g3 CXXFLAGS += -I $(LIBFT_DIR) LDFLAGS = -Llibft -lft -SRC = util_unit.c +SRC = util_unit.c util_unit2.c OBJ = $(SRC:.cpp=.o) LIBFT_DIR = libft LIBFT_A = $(LIBFT_DIR)/libft.a diff --git a/launch_test.c b/launch_test.c index e36fb89..aa40b80 100644 --- a/launch_test.c +++ b/launch_test.c @@ -10,23 +10,16 @@ /* */ /* ************************************************************************** */ -#include "libunit.h" -#include "libft.h" +#include "util_unit.h" +#include "util_unit.h" #include #include #include -static int test_func(t_unit_test *test_node) +static size_t interpret_status(int status, const char *fn_name + , char *test_name) { - int res; - - res = test_node->func; - -} - -static size_t interpret_status(int status, const char *fn_name, char *test_name) -{ - ft_putstr_fd(fn_name, 1); + ft_putstr_fd((char *) fn_name, 1); ft_putstr_fd(" : ", 1); ft_putstr_fd(test_name, 1); ft_putstr_fd(" : ", 1); @@ -54,30 +47,33 @@ static void print_passed_test(size_t ok_tests, t_unit_test *test_list) { ft_putnbr_fd(ok_tests, 1); ft_putchar_fd('/', 1); - ft_putnbr_fd(total_node(test_list)); + ft_putnbr_fd((int)count_tests(test_list), 1); ft_putstr_fd(" tests checked", 1); } int launch_tests(t_unit_test *test_list, const char *fn_name) { - size_t ok_tests; - pid_t wpid; - int status; + size_t ok_tests; + pid_t wpid; + int status; + int i; ok_tests = 0; - while (1) + i = -1; + while (i++ < (int)count_tests(test_list)) { wpid = fork(); if (wpid < 0) return (1); else if (wpid == 0) - test_func(test_func); + exit(!get_test_at(test_list, i)->func()); else { wpid = wait(&status); if (wpid < 0) return (1); - ok_tests += interpret_status(status, fn_name, test_node->title); + ok_tests += interpret_status(status, fn_name, + get_test_at(test_list, i)->title); } } print_passed_test(ok_tests, test_list); diff --git a/util_unit.c b/util_unit.c index a117ea0..f34354e 100644 --- a/util_unit.c +++ b/util_unit.c @@ -13,34 +13,6 @@ #include "util_unit.h" #include -int dummy(void) -{ - return (1); -} - -int main(void) -{ - t_unit_test *testlist; - - testlist = NULL; - load_test(&testlist, "hi", &dummy); -} - -/* we loop over instead of shifting the pointer to make sure we don't skip -unexisting tests and end up in unallocated/invalid memory */ -t_unit_test *get_test_at(t_unit_test *head, size_t target_idx) -{ - size_t j; - - j = 0; - while ((j < target_idx) && head) - { - head = head->next; - j++; - } - return (head); -} - size_t count_tests(t_unit_test *head) { size_t j; @@ -107,6 +79,7 @@ void clear_tests(t_unit_test **head_ptr) t_unit_test *next; next = *head_ptr; + head = NULL; while (head) { head = *head_ptr; diff --git a/util_unit2.c b/util_unit2.c new file mode 100644 index 0000000..50963e0 --- /dev/null +++ b/util_unit2.c @@ -0,0 +1,16 @@ +#include "util_unit.h" + +/* we loop over instead of shifting the pointer to make sure we don't skip +unexisting tests and end up in unallocated/invalid memory */ +t_unit_test *get_test_at(t_unit_test *head, size_t target_idx) +{ + size_t j; + + j = 0; + while ((j < target_idx) && head) + { + head = head->next; + j++; + } + return (head); +}