Merge branch 'feat/r1'

This commit is contained in:
2026-01-25 12:00:09 +01:00
24 changed files with 236 additions and 143 deletions

2
.gitignore vendored
View File

@@ -55,4 +55,6 @@ dkms.conf
*.dwo
/libunit
/real_tests/real_tests
/tests/tests

View File

@@ -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)

5
README Normal file
View File

@@ -0,0 +1,5 @@
libunit
=======
This is an implementation of the `libunit` 42 Rush project.

View File

@@ -6,56 +6,64 @@
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
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);

View File

@@ -6,14 +6,14 @@
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <sys/types.h>
# include <unistd.h>
@@ -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

View File

@@ -1,28 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 00_launcher.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

38
real_tests/Makefile Normal file
View File

@@ -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

View File

@@ -6,22 +6,22 @@
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@@ -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);
}

View File

@@ -1,18 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 01_basic_test.c :+: :+: :+: */
/* 03_large.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@@ -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);

View File

@@ -2,5 +2,5 @@
int strncmp_ok_test(void)
{
return (ft_strncmp("test", "test", 4));
return (!ft_strncmp("test", "test", 4));
}

View File

@@ -2,5 +2,5 @@
int strncmp_ko_test(void)
{
return (ft_strncmp("blabla", "test", 4));
return (!ft_strncmp("blabla", "test", 4));
}

View File

@@ -5,16 +5,16 @@
/* +:+ +:+ +:+ */
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}

View File

@@ -6,7 +6,7 @@
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

36
tests/Makefile Normal file
View File

@@ -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

View File

@@ -6,24 +6,23 @@
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@@ -10,6 +10,7 @@
/* */
/* ************************************************************************** */
int test_ko(void) {
return (0);
int test_ko(void)
{
return (0);
}

View File

@@ -12,7 +12,8 @@
#include <signal.h>
int test_sigsegv(void) {
raise(SIGSEGV);
return (1);
int test_sigsegv(void)
{
raise(SIGSEGV);
return (1);
}

View File

@@ -12,7 +12,8 @@
#include <signal.h>
int test_sigbus(void) {
raise(SIGBUS);
return (1);
int test_sigbus(void)
{
raise(SIGBUS);
return (1);
}

View File

@@ -1,17 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 02_null_test.c :+: :+: :+: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}

View File

@@ -6,7 +6,7 @@
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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