Merge pull request 'dev' (#1) from dev into master

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-04-02 17:37:28 +00:00
4 changed files with 200 additions and 15 deletions
+28
View File
@@ -0,0 +1,28 @@
name: Tests Basics avec Criterion
on: [push]
jobs:
test:
runs-on: ubuntu-latest
container:
image: git.blackbucket.fr/lucas/epita_tp_c:latest
steps:
- name: installation de l'env
run: pacman -Syu --noconfirm nodejs git gcc criterion
- name: checkout
uses: actions/checkout@v4
- name: Compilation
run: >
gcc -Wall -Wextra
-I./Chains_across_the_Island/Fundamentals/basics
-o test
Chains_across_the_Island/Fundamentals/basics/basics.c
tests/test_basics.c
-lcriterion -fsanitize=address -g
- name: Tests
run: ./test
@@ -5,6 +5,15 @@
struct list *list_append(struct list *l, int e)
{
struct list *new_node = malloc(sizeof(struct list));
if(!new_node) return NULL;
new_node->data = e;
new_node->next = l;
return new_node;
/*
struct list *tmp = l;
struct list *new_node = calloc(1, sizeof(struct list));
@@ -13,25 +22,25 @@ struct list *list_append(struct list *l, int e)
new_node->data = e;
if(!l) return new_node;
while (tmp->next) tmp = tmp->next;
while (tmp->next != NULL) tmp = tmp->next;
tmp->next = new_node;
return l;
*/
}
size_t list_count(struct list *l){
int nbr_nodes = 0;
if (!l) return nbr_nodes;
//if (!l) return nbr_nodes;
struct list *tmp = l;
//nbr_nodes ++;
while(tmp->next != NULL) {
while(tmp != NULL) {
nbr_nodes ++;
tmp = tmp->next;
}
return nbr_nodes + 1;
return nbr_nodes;
}
@@ -162,26 +171,23 @@ void list_print(struct list *l){
}
printf("\n");
}
/*
int main(void)
{
struct list *l = NULL;
l = list_append(l, 42); // l = [42] -> NULL
list_print(l);
l = list_append(l, 7); // l = [7] -> [42] -> NULL
list_print(l);
l = list_append(l, 3);
l = list_append(l, 2);
l = list_append(l, 1);
// l = [1] -> [2] -> [3]
list_insert(&l, 1, 99); // l = [1] -> [99] -> [2] -> [3], and list_insert returns 0
list_insert(&l, 9, 99); // list unchanged, returns 1
list_print(l);
list_count(NULL); // 0
printf("count : %li\n",list_count(l)); // 3
list_print(l);
list_destroy(l);
return 0;
}
*/
Binary file not shown.
+151
View File
@@ -0,0 +1,151 @@
#include <criterion/criterion.h>
#include <stdlib.h>
#include "../Chains_across_the_Island/Fundamentals/basics/basics.h"
// Test pour list_append
Test(basics_suite, test_append) {
struct list *l = NULL;
l = list_append(l, 42);
cr_assert_not_null(l, "la liste ne peux pas etre null apres un ajout");
cr_assert_eq(l->data, 42, "la data du 1er noeud doit être 42");
l = list_append(l, 7);
cr_assert_not_null(l->next, "le deuxième noeud doit exister");
cr_assert_eq(l->next->data, 7, "la data du 2 eme noeud doit etre 7");
list_destroy(l);
}
// Test pour list_count
Test(basics_suite, test_count) {
struct list *l = NULL;
cr_assert_eq(list_count(l), 0, "Une liste vide doit avoir une taille de 0.");
l = list_append(l, 10);
l = list_append(l, 20);
l = list_append(l, 30);
cr_assert_eq(list_count(l), 3, "La liste doit contenir 3 éléments.");
list_destroy(l);
}
// Test pour list_insert
Test(basics_suite, test_insert) {
struct list *l = NULL;
int res1 = list_insert(&l, 0, 100);
cr_assert_eq(res1, 0, "L'insertion à l'index 0 doit réussir.");
cr_assert_not_null(l, "La liste ne doit pas être NULL.");
cr_assert_eq(l->data, 100, "La valeur insérée doit être 100.");
int res2 = list_insert(&l, 5, 200);
cr_assert_eq(res2, 1, "L'insertion hors limite doit échouer et renvoyer 1.");
list_destroy(l);
}
//Test pour list_get
Test(basics_suite, test_get){
struct list *l = NULL;
l = list_append(l, 30);
l = list_append(l, 20);
l = list_append(l, 10); // L'ordre final est [30] -> [20] -> [10]
struct list *rslt = list_get(l, 0);
cr_assert_not_null(rslt, "Le pointeur ne doit pas être NULL pour l'index 0");
cr_assert_eq(rslt->data, 30, "pointer to node with data=30");
rslt = list_get(l, 2);
cr_assert_not_null(rslt, "Le pointeur ne doit pas être NULL pour l'index 2");
cr_assert_eq(rslt->data, 10, "pointer to node with data=10");
rslt = list_get(l, 5);
cr_assert_null(rslt, "Le pointeur doit etre null");
list_destroy(l);
// On ne détruit SURTOUT PAS rslt ici, car il appartient déjà à l !
}
//Test list_find
Test(basics_suite, test_find){
struct list *l = NULL;
l = list_append(l, 3);
l = list_append(l, 7);
l = list_append(l, 1);
struct list *rslt = list_find(l, 7);
cr_assert_not_null(rslt, "Le noeud doit être trouvé");
cr_assert_eq(rslt->data, 7, "pointer to node with data=7");
rslt = list_find(l, 42);
cr_assert_null(rslt, "pointeur doit etre null");
list_destroy(l);
}
//Test list_delete_at
Test(basics_suite, test_delete_at){
struct list *l = NULL;
l = list_append(l, 30);
l = list_append(l, 20);
l = list_append(l, 10); // [30] -> [20] -> [10]
struct list *rslt = NULL;
rslt = list_delete_at(&l, 1); // Retire le 20. l devient [30] -> [10]
cr_assert_not_null(rslt, "Le noeud retiré ne doit pas être NULL");
cr_assert_eq(rslt->data, 20, "La data du noeud supprimé doit etre de 20");
size_t count = list_count(l);
cr_assert_eq(count, 2, "La taille de la liste doit etre de 2");
struct list *tmp = list_get(l, 0);
cr_assert_not_null(tmp, "Le noeud 0 ne doit pas être NULL");
cr_assert_eq(tmp->data, 30, "La data du noeud 1 doit etre de 30");
tmp = list_get(l, 1);
cr_assert_not_null(tmp, "Le noeud 1 ne doit pas être NULL");
cr_assert_eq(tmp->data, 10, "La data du noeud 2 doit etre de 10");
tmp = list_get(l, 3);
cr_assert_null(tmp, "Le pointeur doit etre null");
list_destroy(l);
free(rslt); // rslt est totalement détaché de l, on le libère avec un simple free()
}
//Test list_remove
Test(basics_suite, test_list_remove){
struct list *l = NULL;
l = list_append(l, 7);
l = list_append(l, 3);
l = list_append(l, 7);
l = list_append(l, 1); // [7] -> [3] -> [7] -> [1]
int rm = list_remove(&l, 7); // Retire le premier 7. l devient [3] -> [7] -> [1]
cr_assert_eq(rm, 1, "Le noeud 7 doit etre supprimé");
struct list *tmp = list_get(l, 0);
cr_assert_not_null(tmp, "Le noeud 0 existe");
cr_assert_eq(tmp->data, 3, "Le noeud 1 a changé et devient 3 !");
tmp = list_get(l, 1);
cr_assert_not_null(tmp, "Le noeud 1 existe");
cr_assert_eq(tmp->data, 7, "Le noeud 2 doit etre egal a 7");
tmp = list_get(l, 2);
cr_assert_not_null(tmp, "Le noeud 2 existe");
cr_assert_eq(tmp->data, 1, "Le 3 eme noeud doit etre de 1");
rm = list_remove(&l, 42);
cr_assert_eq(rm, 0, "Doit renvoyer 0 car 42 n'existe pas !");
list_destroy(l);
}