diff --git a/.gitignore b/.gitignore index db12e57..32dd980 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,6 @@ dkms.conf *.dwo /libunit +/real_tests/real_tests +/tests/tests diff --git a/Makefile b/Makefile index 0955987..26f548c 100644 --- a/Makefile +++ b/Makefile @@ -1,37 +1,57 @@ -NAME = libunit -CXX = cc -CXXFLAGS = -Wall -Wextra -Werror -MMD -MP -CXXFLAGS += -g3 -CXXFLAGS += -I $(LIBFT_DIR) -I. -LDFLAGS = -Llibft -lft +NAME = libunit.a +AR = ar rcs +CC = cc +CCFLAGS = -Wall -Wextra -Werror -MMD -MP +CCFLAGS += -I$(LIBFT_DIR) -I. -Iframework +LDFLAGS = -Llibft -lunit -lft -L. LIBFT_DIR = libft LIBFT_A = $(LIBFT_DIR)/libft.a -SRC = libunit_util.c libunit_util2.c libunit.c main.c -SRC += real_tests/ft_strlen/00_launcher.c real_tests/ft_strlen/01_basic.c real_tests/ft_strlen/02_null.c -SRC += real_tests/ft_atoi/00_launcher.c real_tests/ft_atoi/01_ok.c real_tests/ft_atoi/02_ko.c real_tests/ft_atoi/03_sigsegv.c real_tests/ft_atoi/04_sigbus.c -SRC += tests/libunit/00_launcher.c tests/libunit/01_ok.c tests/libunit/02_ko.c tests/libunit/03_sigsegv.c tests/libunit/04_sigbus.c +SRC = framework/libunit_util.c framework/libunit_util2.c framework/libunit.c OBJ = $(SRC:.c=.o) +TESTD = tests +TESTB = $(TESTD)/tests +TESTM = $(TESTD)/Makefile +RTESTD = real_tests +RTESTB = $(RTESTD)/real_tests +RTESTM = $(RTESTD)/Makefile + all: $(NAME) $(NAME): $(OBJ) | $(LIBFT_A) - $(CXX) $(CXXFLAGS) $(OBJ) $(LDFLAGS) -o $(NAME) + $(AR) $(NAME) $(OBJ) %.o: %.c Makefile - $(CXX) $(CXXFLAGS) -c $< -o $@ + $(CC) $(CCFLAGS) -c $< -o $@ -$(LIBFT_A): - $(MAKE) -C $(LIBFT_DIR) +$(LIBFT_A): FORCE + @$(MAKE) -C $(LIBFT_DIR) clean: $(RM) $(OBJ) $(SRC:.c=.d) + @$(MAKE) -C $(TESTD) clean + @$(MAKE) -C $(RTESTD) clean fclean: clean $(RM) $(NAME) + @$(MAKE) -C $(TESTD) fclean + @$(MAKE) -C $(RTESTD) fclean re: fclean - $(MAKE) all + @$(MAKE) all + +$(TESTB): FORCE + @$(MAKE) -C $(TESTD) + +$(RTESTB): FORCE + @$(MAKE) -C $(RTESTD) + +test: $(TESTB) $(RTESTB) + $(TESTB) + $(RTESTB) + +FORCE: ; -include $(SRC:.c=.d) diff --git a/README b/README new file mode 100644 index 0000000..cfd792a --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +libunit +======= + +This is an implementation of the `libunit` 42 Rush project. + diff --git a/libunit.c b/framework/libunit.c similarity index 65% rename from libunit.c rename to framework/libunit.c index b0b3a8c..f98e249 100644 --- a/libunit.c +++ b/framework/libunit.c @@ -6,56 +6,64 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 12:43:35 by dgaillet #+# #+# */ -/* Updated: 2026/01/24 16:15:02 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 16:50:51 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft/libft.h" #include "libunit.h" +#include #include #include -#include -static size_t interpret_status(int status, const char *fn_name - , char *test_name) +static size_t interpret_status(int status, const char *fn_name, + char *test_name) { - ft_putstr_fd((char *) fn_name, 1); + ft_putstr_fd((char *)fn_name, 1); ft_putstr_fd(" : ", 1); ft_putstr_fd(test_name, 1); ft_putstr_fd(" : ", 1); - if (WIFSIGNALED(status)) - { - if (WTERMSIG(status) == 11) - ft_putstr_fd("[SIGSEGV]\n", 1); - else if (WTERMSIG(status) == 10) - ft_putstr_fd("[SIGBUS]\n", 1); - } - else if (WIFEXITED(status)) + if (WIFEXITED(status)) { if (WEXITSTATUS(status)) - ft_putstr_fd("[KO]\n", 1); + ft_putstr_fd("[\x1B[31m\x1B[1mKO\x1B[0m]\n", 1); else { - ft_putstr_fd("[OK]\n", 1); + ft_putstr_fd("[\x1B[32mOK\x1B[0m]\n", 1); return (1); } } + else if (WIFSIGNALED(status)) + { + if (WTERMSIG(status) == 11) + ft_putstr_fd("[\x1B[33mSIGSEGV\x1B[0m]\n", 1); + else if (WTERMSIG(status) == 7) + ft_putstr_fd("[\x1B[33mSIGBUS\x1B[0m]\n", 1); + } return (0); } static void print_passed_test(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_putchar_fd('/', 1); - ft_putnbr_fd((int)count_tests(test_list), 1); - ft_putstr_fd(" tests checked\n", 1); + ft_putnbr_fd((int)total, 1); + ft_putstr_fd("\x1B[0m tests succeeded\n\n", 1); } int launch_tests(t_unit_test *test_list, const char *fn_name) { - size_t ok_tests; - pid_t wpid; - int status; - int i; + size_t ok_tests; + pid_t wpid; + int status; + int i; ok_tests = 0; i = -1; @@ -71,8 +79,8 @@ int launch_tests(t_unit_test *test_list, const char *fn_name) wpid = wait(&status); if (wpid < 0) return (1); - ok_tests += interpret_status(status, fn_name, - get_test_at(test_list, i)->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/libunit.h b/framework/libunit.h similarity index 87% rename from libunit.h rename to framework/libunit.h index 166ad06..f68a14c 100644 --- a/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 16:15:23 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 18:56:03 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef UTIL_UNIT_H -# define UTIL_UNIT_H +#ifndef LIBUNIT_H +# define LIBUNIT_H -# include "libft/libft.h" +# include "libft.h" # include # include @@ -66,9 +66,4 @@ void clear_tests(t_unit_test **head_ptr); int launch_tests(t_unit_test *test_list, const char *fn_name); -// LAUNCHERS -int strlen_launcher(void); -int libunit_launcher(void); -int atoi_launcher(void); - -#endif // !UTIL_UNIT_H +#endif // !LIBUNIT_H diff --git a/libunit_util.c b/framework/libunit_util.c similarity index 100% rename from libunit_util.c rename to framework/libunit_util.c diff --git a/libunit_util2.c b/framework/libunit_util2.c similarity index 100% rename from libunit_util2.c rename to framework/libunit_util2.c diff --git a/libft/ft_strlen/00_launcher.c b/libft/ft_strlen/00_launcher.c deleted file mode 100644 index e1ff4fa..0000000 --- a/libft/ft_strlen/00_launcher.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 00_launcher.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: elagouch +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2026/01/24 15:32:32 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:01:11 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/real_tests/Makefile b/real_tests/Makefile new file mode 100644 index 0000000..b98eea1 --- /dev/null +++ b/real_tests/Makefile @@ -0,0 +1,38 @@ +NAME = real_tests +CC = cc +CCFLAGS = -Wall -Wextra -Werror -MMD -MP + +SRC = main.c +SRC += ft_strlen/00_launcher.c ft_strlen/01_basic.c ft_strlen/02_null.c ft_strlen/03_large.c +SRC += ft_atoi/00_launcher.c ft_atoi/01_ok.c ft_atoi/02_ko.c ft_atoi/03_sigsegv.c ft_atoi/04_sigbus.c +SRC += ft_strncmp/00_launcher.c ft_strncmp/01_ok.c ft_strncmp/02_ko.c +OBJ = $(SRC:.c=.o) + +LIBS = -L.. -lunit -L../libft -lft +INC = -I. -I../framework -I../libft + +all: $(NAME) + +test: $(NAME) + ./$(NAME) + +%.o: %.c Makefile + $(CC) $(CCFLAGS) $(INC) -c $< -o $@ + +$(NAME): $(OBJ) | libunit + $(CC) $(CCFLAGS) $(INC) $(OBJ) $(LIBS) -o $(NAME) + +libunit: + @$(MAKE) -C .. + +clean: + $(RM) $(OBJ) $(SRC:.c=.d) + +fclean: clean + $(RM) $(NAME) + +re: fclean all + +-include $(SRC:.c=.d) + +.PHONY: all clean fclean re libunit diff --git a/real_tests/ft_strlen/00_launcher.c b/real_tests/ft_strlen/00_launcher.c index 48d5b7e..10d6ad5 100644 --- a/real_tests/ft_strlen/00_launcher.c +++ b/real_tests/ft_strlen/00_launcher.c @@ -6,22 +6,22 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:32:32 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:17:20 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 16:34:12 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ #include "../tests.h" -int strlen_launcher(void) { - t_unit_test *testlist; - size_t res; +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); + testlist = NULL; + load_test(&testlist, "null", &test_null); + load_test(&testlist, "basic", &test_basic); + load_test(&testlist, "large", &test_large); + res = launch_tests(testlist, "strlen"); + clear_tests(&testlist); + return (res); } diff --git a/real_tests/ft_strlen/02_null.c b/real_tests/ft_strlen/02_null.c index ae665f8..fbf978f 100644 --- a/real_tests/ft_strlen/02_null.c +++ b/real_tests/ft_strlen/02_null.c @@ -12,6 +12,7 @@ #include "../tests.h" -int test_null(void) { - return (ft_strlen(NULL) == 0); +int test_null(void) +{ + return (ft_strlen(NULL) == 0); } diff --git a/libft/ft_strlen/01_basic_test.c b/real_tests/ft_strlen/03_large.c similarity index 72% rename from libft/ft_strlen/01_basic_test.c rename to real_tests/ft_strlen/03_large.c index 9ffa4c5..f471b2e 100644 --- a/libft/ft_strlen/01_basic_test.c +++ b/real_tests/ft_strlen/03_large.c @@ -1,18 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 01_basic_test.c :+: :+: :+: */ +/* 03_large.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 15:39:06 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 16:36:42 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" +#include "../tests.h" -int test_basic(void) +int test_large(void) { - return (ft_strlen("hello, world") == 12); + size_t i; + char s[10000]; + + i = 0; + while (i < sizeof(s)) + { + s[i] = ' '; + i++; + } + 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 4e0e207..2b4798a 100644 --- a/real_tests/ft_strncmp/00_launcher.c +++ b/real_tests/ft_strncmp/00_launcher.c @@ -7,6 +7,7 @@ int strncmp_launcher(void) testlist = NULL; load_test(&testlist, "OK test", &strncmp_ok_test); + load_test(&testlist, "KO test", &strncmp_ko_test); res = launch_tests(testlist, "ft_strncmp"); clear_tests(&testlist); return (res); diff --git a/real_tests/ft_strncmp/01_ok.c b/real_tests/ft_strncmp/01_ok.c index 68ab5c1..94f5547 100644 --- a/real_tests/ft_strncmp/01_ok.c +++ b/real_tests/ft_strncmp/01_ok.c @@ -2,5 +2,5 @@ int strncmp_ok_test(void) { - return (ft_strncmp("test", "test", 4)); + return (!ft_strncmp("test", "test", 4)); } diff --git a/real_tests/ft_strncmp/02_ko_test.c b/real_tests/ft_strncmp/02_ko.c similarity index 54% rename from real_tests/ft_strncmp/02_ko_test.c rename to real_tests/ft_strncmp/02_ko.c index e3e5d3c..d348569 100644 --- a/real_tests/ft_strncmp/02_ko_test.c +++ b/real_tests/ft_strncmp/02_ko.c @@ -2,5 +2,5 @@ int strncmp_ko_test(void) { - return (ft_strncmp("blabla", "test", 4)); + return (!ft_strncmp("blabla", "test", 4)); } diff --git a/main.c b/real_tests/main.c similarity index 81% rename from main.c rename to real_tests/main.c index 9cdf324..a54be34 100644 --- a/main.c +++ b/real_tests/main.c @@ -5,16 +5,16 @@ /* +:+ +:+ +:+ */ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2026/01/24 15:40:58 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:15:52 by elagouch ### ########.fr */ +/* Created: 2026/01/24 18:53:18 by elagouch #+# #+# */ +/* Updated: 2026/01/24 18:53:18 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libunit.h" +#include "tests.h" -int main(void) +int main(void) { - libunit_launcher(); strlen_launcher(); atoi_launcher(); + strncmp_launcher(); } diff --git a/real_tests/tests.h b/real_tests/tests.h index f1d5b8c..3a5c58e 100644 --- a/real_tests/tests.h +++ b/real_tests/tests.h @@ -6,7 +6,7 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:32:30 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:16:13 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 18:40:48 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,9 +15,15 @@ # include "libunit.h" +// LAUNCHERS +int strlen_launcher(void); +int atoi_launcher(void); +int strncmp_launcher(void); + // strlen -int test_basic(void); -int test_null(void); +int test_basic(void); +int test_null(void); +int test_large(void); // atoi int atoi_ok_test(void); @@ -27,6 +33,7 @@ int atoi_sigbus_test(void); //strncmp int strncmp_ok_test(void); +int strncmp_ko_test(void); #endif // !TESTS_H diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..f78f9b0 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,36 @@ +NAME = tests +CC = cc +CCFLAGS = -Wall -Wextra -Werror -MMD -MP + +SRC = main.c +SRC += libunit/00_launcher.c libunit/01_ok.c libunit/02_ko.c libunit/03_sigsegv.c libunit/04_sigbus.c +OBJ = $(SRC:.c=.o) + +LIBS = -L.. -lunit -L../libft -lft +INC = -I. -I../framework -I../libft + +all: $(NAME) + +test: $(NAME) + ./$(NAME) + +%.o: %.c Makefile + $(CC) $(CCFLAGS) $(INC) -c $< -o $@ + +$(NAME): $(OBJ) | libunit + $(CC) $(CCFLAGS) $(INC) $(OBJ) $(LIBS) -o $(NAME) + +libunit: + @$(MAKE) -C .. + +clean: + $(RM) $(OBJ) $(SRC:.c=.d) + +fclean: clean + $(RM) $(NAME) + +re: fclean all + +-include $(SRC:.c=.d) + +.PHONY: all clean fclean re libunit diff --git a/tests/libunit/00_launcher.c b/tests/libunit/00_launcher.c index 483488e..4f9aa3e 100644 --- a/tests/libunit/00_launcher.c +++ b/tests/libunit/00_launcher.c @@ -6,24 +6,23 @@ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 15:32:32 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:16:24 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 16:47:43 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ #include "../tests.h" -int libunit_launcher(void) { - t_unit_test *testlist; - size_t res; +int libunit_launcher(void) +{ + t_unit_test *testlist; + size_t res; - testlist = NULL; - load_test(&testlist, "successful", &test_ok); - load_test(&testlist, "unsuccessful", &test_ko); - load_test(&testlist, "sigsegv", &test_ko); - load_test(&testlist, "sigbus", &test_ko); - - res = launch_tests(testlist, "libunit"); - clear_tests(&testlist); - - return (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); + res = launch_tests(testlist, "libunit"); + clear_tests(&testlist); + return (res); } diff --git a/tests/libunit/02_ko.c b/tests/libunit/02_ko.c index 22cd63c..f8cf1a2 100644 --- a/tests/libunit/02_ko.c +++ b/tests/libunit/02_ko.c @@ -10,6 +10,7 @@ /* */ /* ************************************************************************** */ -int test_ko(void) { - return (0); +int test_ko(void) +{ + return (0); } diff --git a/tests/libunit/03_sigsegv.c b/tests/libunit/03_sigsegv.c index b7cc362..b92429e 100644 --- a/tests/libunit/03_sigsegv.c +++ b/tests/libunit/03_sigsegv.c @@ -12,7 +12,8 @@ #include -int test_sigsegv(void) { - raise(SIGSEGV); - return (1); +int test_sigsegv(void) +{ + raise(SIGSEGV); + return (1); } diff --git a/tests/libunit/04_sigbus.c b/tests/libunit/04_sigbus.c index b8c568a..06aca74 100644 --- a/tests/libunit/04_sigbus.c +++ b/tests/libunit/04_sigbus.c @@ -12,7 +12,8 @@ #include -int test_sigbus(void) { - raise(SIGBUS); - return (1); +int test_sigbus(void) +{ + raise(SIGBUS); + return (1); } diff --git a/libft/ft_strlen/02_null_test.c b/tests/main.c similarity index 72% rename from libft/ft_strlen/02_null_test.c rename to tests/main.c index 565d235..11bf0f7 100644 --- a/libft/ft_strlen/02_null_test.c +++ b/tests/main.c @@ -1,17 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 02_null_test.c :+: :+: :+: */ +/* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: elagouch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2026/01/24 15:38:17 by elagouch #+# #+# */ -/* Updated: 2026/01/24 16:01:12 by elagouch ### ########.fr */ +/* Created: 2026/01/24 18:53:13 by elagouch #+# #+# */ +/* Updated: 2026/01/24 18:53:14 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" +#include "tests.h" -int test_null(void) { - return (ft_strlen(NULL) == 0); +int main(void) +{ + libunit_launcher(); } diff --git a/tests/tests.h b/tests/tests.h index 24a4f2d..0fc03ff 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 16:16:20 by elagouch ### ########.fr */ +/* Updated: 2026/01/24 18:41:02 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,16 +16,12 @@ # include "libunit.h" // LAUNCHERS -int strlen_launcher(void); +int libunit_launcher(void); // libunit -int test_ok(void); -int test_ko(void); -int test_sigsegv(void); -int test_sigbus(void); - -// strlen -int test_basic(void); -int test_null(void); +int test_ok(void); +int test_ko(void); +int test_sigsegv(void); +int test_sigbus(void); #endif // !TESTS_H