style: norm
This commit is contained in:
117
util_unit.c
117
util_unit.c
@@ -1,71 +1,116 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* util_unit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/24 14:35:57 by elagouch #+# #+# */
|
||||||
|
/* Updated: 2026/01/24 14:38:20 by elagouch ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "util_unit.h"
|
#include "util_unit.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int dummy(void) { return 1; }
|
int dummy(void)
|
||||||
|
{
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void)
|
||||||
t_unit_test *testlist = NULL;
|
{
|
||||||
|
t_unit_test *testlist;
|
||||||
|
|
||||||
|
testlist = NULL;
|
||||||
load_test(&testlist, "hi", &dummy);
|
load_test(&testlist, "hi", &dummy);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_unit_test *getTestAt(t_unit_test *head, size_t target_idx) {
|
|
||||||
/* we loop over instead of shifting the pointer to make sure we don't skip
|
/* we loop over instead of shifting the pointer to make sure we don't skip
|
||||||
* unexisting tests and end up in unallocated/invalid memory */
|
unexisting tests and end up in unallocated/invalid memory */
|
||||||
for (size_t j = 0; (j < target_idx) && head; j++, head = head->next)
|
t_unit_test *get_test_at(t_unit_test *head, size_t target_idx)
|
||||||
;
|
{
|
||||||
return head;
|
size_t j;
|
||||||
|
|
||||||
|
j = 0;
|
||||||
|
while ((j < target_idx) && head)
|
||||||
|
{
|
||||||
|
head = head->next;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return (head);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t countTests(t_unit_test *head) {
|
size_t count_tests(t_unit_test *head)
|
||||||
size_t j = 0;
|
{
|
||||||
for (; head; j++, head = head->next)
|
size_t j;
|
||||||
;
|
|
||||||
return j;
|
j = 0;
|
||||||
|
while (head)
|
||||||
|
{
|
||||||
|
head = head->next;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return (j);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief util function to get last test of the chain
|
* @brief util function to get last test of the chain
|
||||||
*/
|
*/
|
||||||
t_unit_test *getLast(t_unit_test *head) {
|
t_unit_test *get_last(t_unit_test *head)
|
||||||
for (; head; head = head->next)
|
{
|
||||||
;
|
while (head->next)
|
||||||
return head;
|
head = head->next;
|
||||||
|
return (head);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief util function to allocate a new test
|
* @brief util function to allocate a new test
|
||||||
*/
|
*/
|
||||||
t_unit_test *alloc_test(const char *title, int (*test_func)(void)) {
|
t_unit_test *alloc_test(const char *title, int (*test_func)(void))
|
||||||
t_unit_test *p = malloc(sizeof(t_unit_test));
|
{
|
||||||
|
t_unit_test *p;
|
||||||
|
|
||||||
|
p = malloc(sizeof(t_unit_test));
|
||||||
p->title = ft_strdup(title);
|
p->title = ft_strdup(title);
|
||||||
p->func = test_func;
|
p->func = test_func;
|
||||||
p->next = NULL;
|
p->next = NULL;
|
||||||
return p;
|
return (p);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t load_test(t_unit_test **head_ptr, const char *title,
|
size_t load_test(t_unit_test **head_ptr, const char *title,
|
||||||
int (*test_func)(void)) {
|
int (*test_func)(void))
|
||||||
if (!head_ptr)
|
{
|
||||||
return -1;
|
t_unit_test *p;
|
||||||
if (!*head_ptr) { // initial
|
t_unit_test *last;
|
||||||
t_unit_test *p = alloc_test(title, test_func);
|
|
||||||
if (!p)
|
|
||||||
return -1;
|
|
||||||
*head_ptr = p;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
t_unit_test *last = getLast(*head_ptr);
|
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);
|
last->next = alloc_test(title, test_func);
|
||||||
if (!last->next)
|
if (!last->next)
|
||||||
return -1;
|
return (-1);
|
||||||
return 0;
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear_tests(t_unit_test **head_ptr) {
|
void clear_tests(t_unit_test **head_ptr)
|
||||||
for (t_unit_test *head = *head_ptr; head;) {
|
{
|
||||||
t_unit_test *next = head = head->next;
|
t_unit_test *head;
|
||||||
|
t_unit_test *next;
|
||||||
|
|
||||||
|
next = *head_ptr;
|
||||||
|
while (head)
|
||||||
|
{
|
||||||
|
head = *head_ptr;
|
||||||
|
next = head->next;
|
||||||
head->next = NULL;
|
head->next = NULL;
|
||||||
free(head->title);
|
free(head->title);
|
||||||
head->title = NULL;
|
head->title = NULL;
|
||||||
|
|||||||
15
util_unit.h
15
util_unit.h
@@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* util_unit.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/24 14:38:40 by elagouch #+# #+# */
|
||||||
|
/* Updated: 2026/01/24 14:38:41 by elagouch ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef UTIL_UNIT_H
|
#ifndef UTIL_UNIT_H
|
||||||
# define UTIL_UNIT_H
|
# define UTIL_UNIT_H
|
||||||
|
|
||||||
@@ -8,7 +20,8 @@
|
|||||||
/**
|
/**
|
||||||
* @brief singular unit test def
|
* @brief singular unit test def
|
||||||
*/
|
*/
|
||||||
typedef struct s_unit_test {
|
typedef struct s_unit_test
|
||||||
|
{
|
||||||
char *title;
|
char *title;
|
||||||
int (*func)(void);
|
int (*func)(void);
|
||||||
struct s_unit_test *next;
|
struct s_unit_test *next;
|
||||||
|
|||||||
Reference in New Issue
Block a user