From 760c91c949f462be5e33f4de9cb904393ef89564 Mon Sep 17 00:00:00 2001 From: airone01 <21955960+airone01@users.noreply.github.com> Date: Sat, 24 Jan 2026 15:45:40 +0100 Subject: [PATCH] feat: finally tests --- Makefile | 7 +++++-- launch_test.c | 4 ++-- tests/ft_strlen/00_launcher.c | 28 ++++++++++++++++++++++++++++ tests/ft_strlen/01_basic_test.c | 17 +++++++++++++++++ tests/ft_strlen/02_null_test.c | 17 +++++++++++++++++ tests/main.c | 18 ++++++++++++++++++ tests/tests.h | 23 +++++++++++++++++++++++ util_unit.c | 3 ++- util_unit.h | 8 +++++++- 9 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 tests/ft_strlen/00_launcher.c create mode 100644 tests/ft_strlen/01_basic_test.c create mode 100644 tests/ft_strlen/02_null_test.c create mode 100644 tests/main.c create mode 100644 tests/tests.h diff --git a/Makefile b/Makefile index 8d01a12..0fab83b 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,14 @@ CXXFLAGS = -Wall -Wextra -Werror -MMD -MP CXXFLAGS += -g3 CXXFLAGS += -I $(LIBFT_DIR) LDFLAGS = -Llibft -lft -SRC = util_unit.c util_unit2.c -OBJ = $(SRC:.cpp=.o) LIBFT_DIR = libft LIBFT_A = $(LIBFT_DIR)/libft.a +SRC = util_unit.c util_unit2.c launch_test.c +SRC += tests/main.c +SRC += tests/ft_strlen/00_launcher.c tests/ft_strlen/01_basic_test.c tests/ft_strlen/02_null_test.c +OBJ = $(SRC:.cpp=.o) + all: $(NAME) $(NAME): $(OBJ) | $(LIBFT_A) diff --git a/launch_test.c b/launch_test.c index aa40b80..60d798b 100644 --- a/launch_test.c +++ b/launch_test.c @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 12:43:35 by dgaillet #+# #+# */ -/* Updated: 2026/01/24 12:43:37 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/24 15:44:35 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,7 +60,7 @@ int launch_tests(t_unit_test *test_list, const char *fn_name) ok_tests = 0; i = -1; - while (i++ < (int)count_tests(test_list)) + while (++i < (int)count_tests(test_list)) { wpid = fork(); if (wpid < 0) diff --git a/tests/ft_strlen/00_launcher.c b/tests/ft_strlen/00_launcher.c new file mode 100644 index 0000000..0155bdd --- /dev/null +++ b/tests/ft_strlen/00_launcher.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 00_launcher.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:32:32 by elagouch #+# #+# */ +/* Updated: 2026/01/24 15:44:00 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../util_unit.h" +#include "../tests.h" + +int strlen_launcher(void) { + t_unit_test *testlist; + size_t res; + + testlist = NULL; + load_test(&testlist, "null test", &test_null); + load_test(&testlist, "basic test", &test_basic); + + res = launch_tests(testlist, "strlen"); + clear_tests(&testlist); + + return (res); +} diff --git a/tests/ft_strlen/01_basic_test.c b/tests/ft_strlen/01_basic_test.c new file mode 100644 index 0000000..f9ffda6 --- /dev/null +++ b/tests/ft_strlen/01_basic_test.c @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 01_basic_test.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ +/* Updated: 2026/01/24 15:39:06 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int test_basic(void) { + return (ft_strlen("hello, world") == 12); +} diff --git a/tests/ft_strlen/02_null_test.c b/tests/ft_strlen/02_null_test.c new file mode 100644 index 0000000..753154e --- /dev/null +++ b/tests/ft_strlen/02_null_test.c @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 02_null_test.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ +/* Updated: 2026/01/24 15:40:42 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int test_null(void) { + return (ft_strlen(NULL) == 0); +} diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..dc43521 --- /dev/null +++ b/tests/main.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:40:58 by elagouch #+# #+# */ +/* Updated: 2026/01/24 15:41:56 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +int main(void) +{ + strlen_launcher(); +} diff --git a/tests/tests.h b/tests/tests.h new file mode 100644 index 0000000..4c20501 --- /dev/null +++ b/tests/tests.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: elagouch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/24 15:32:30 by elagouch #+# #+# */ +/* Updated: 2026/01/24 15:41:32 by elagouch ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TESTS_H +# define TESTS_H + +// LAUNCHERS +int strlen_launcher(void); + +// strlen +int test_basic(void); +int test_null(void); + +#endif // !TESTS_H diff --git a/util_unit.c b/util_unit.c index f34354e..cf81cf8 100644 --- a/util_unit.c +++ b/util_unit.c @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 14:35:57 by elagouch #+# #+# */ -/* Updated: 2026/01/24 14:38:20 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 15:37:46 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,4 +90,5 @@ void clear_tests(t_unit_test **head_ptr) head->func = NULL; head = next; } + *head_ptr = NULL; } diff --git a/util_unit.h b/util_unit.h index 469a825..6b874d8 100644 --- a/util_unit.h +++ b/util_unit.h @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 14:38:40 by elagouch #+# #+# */ -/* Updated: 2026/01/24 14:38:41 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 15:35:30 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,4 +60,10 @@ size_t load_test(t_unit_test **head_ptr, const char *title, */ void clear_tests(t_unit_test **head_ptr); +/** + * @brief launches the tests and returns the reported status + */ +int launch_tests(t_unit_test *test_list, + const char *fn_name); + #endif // !UTIL_UNIT_H