refactor: move to framework dit

This commit is contained in:
airone01
2026-01-24 17:12:11 +01:00
parent 803127f57c
commit 22de4eb11a
5 changed files with 19 additions and 12 deletions

88
framework/libunit.c Normal file
View File

@@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libunit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/24 12:43:35 by dgaillet #+# #+# */
/* Updated: 2026/01/24 16:50:51 by elagouch ### ########.fr */
/* */
/* ************************************************************************** */
#include "libunit.h"
#include "libft/libft.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)
{
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))
{
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)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;
ok_tests = 0;
i = -1;
while (++i < (int)count_tests(test_list))
{
wpid = fork();
if (wpid < 0)
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);
return (0);
}

73
framework/libunit.h Normal file
View File

@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libunit.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#ifndef UTIL_UNIT_H
# define UTIL_UNIT_H
# include "libft/libft.h"
# include <sys/types.h>
# include <unistd.h>
/**
* @brief singular unit test def
*/
typedef struct s_unit_test
{
char *title;
int (*func)(void);
struct s_unit_test *next;
} t_unit_test;
/**
* @brief finds a test at index
*
* @return test or NULL
*/
t_unit_test *get_test_at(t_unit_test *head, size_t target_idx);
/**
* @brief count tests
*
* @return size_t test count
*/
size_t count_tests(t_unit_test *head);
/**
* @brief adds a new test
* @brief alternatively, if the passed head is empty, replace it with the
* initial test
*
* @param head_ptr pointer to the head of the tests list
* @param title name of the test
* @param test_func function to execute the test
*
* @return -1 on error, 0 on success
*/
size_t load_test(t_unit_test **head_ptr, const char *title,
int (*test_func)(void));
/**
* @brief clears the tests memory
*/
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);
// LAUNCHERS
int strlen_launcher(void);
int libunit_launcher(void);
#endif // !UTIL_UNIT_H

94
framework/libunit_util.c Normal file
View File

@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libunit_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/24 14:35:57 by elagouch #+# #+# */
/* Updated: 2026/01/24 16:15:35 by elagouch ### ########.fr */
/* */
/* ************************************************************************** */
#include "libunit.h"
#include <stdlib.h>
size_t count_tests(t_unit_test *head)
{
size_t j;
j = 0;
while (head)
{
head = head->next;
j++;
}
return (j);
}
/**
* @brief util function to get last test of the chain
*/
t_unit_test *get_last(t_unit_test *head)
{
while (head->next)
head = head->next;
return (head);
}
/**
* @brief util function to allocate a new test
*/
t_unit_test *alloc_test(const char *title, int (*test_func)(void))
{
t_unit_test *p;
p = malloc(sizeof(t_unit_test));
p->title = ft_strdup(title);
p->func = test_func;
p->next = NULL;
return (p);
}
size_t load_test(t_unit_test **head_ptr, const char *title,
int (*test_func)(void))
{
t_unit_test *p;
t_unit_test *last;
if (!head_ptr)
return (-1);
if (!*head_ptr)
{
p = alloc_test(title, test_func);
if (!p)
return (-1);
*head_ptr = p;
return (0);
}
last = get_last(*head_ptr);
last->next = alloc_test(title, test_func);
if (!last->next)
return (-1);
return (0);
}
void clear_tests(t_unit_test **head_ptr)
{
t_unit_test *head;
t_unit_test *next;
next = *head_ptr;
head = NULL;
while (head)
{
head = *head_ptr;
next = head->next;
head->next = NULL;
free(head->title);
head->title = NULL;
head->func = NULL;
head = next;
}
*head_ptr = NULL;
}

28
framework/libunit_util2.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libunit_util2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/24 15:46:43 by elagouch #+# #+# */
/* Updated: 2026/01/24 16:15:43 by elagouch ### ########.fr */
/* */
/* ************************************************************************** */
#include "libunit.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);
}