From bcdfc07ee75fbecc7bd2b4c253a24f78ba3a4804 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 3 Apr 2026 16:31:26 +0200 Subject: [PATCH] tt --- .../Fundamentals/double/double.c | 111 +++++++++++++- tests/{test_basics.c => tests.c} | 141 ++++++++++++++++++ 2 files changed, 247 insertions(+), 5 deletions(-) rename tests/{test_basics.c => tests.c} (55%) diff --git a/Chains_across_the_Island/Fundamentals/double/double.c b/Chains_across_the_Island/Fundamentals/double/double.c index fb73334..e28e8a2 100644 --- a/Chains_across_the_Island/Fundamentals/double/double.c +++ b/Chains_across_the_Island/Fundamentals/double/double.c @@ -1,26 +1,127 @@ #include "double.h" +#include +#include +#include struct dlist *dlist_append(struct dlist *l, int e) { - //FIXME + struct dlist *new_node = calloc(1, sizeof(struct dlist)); + if(!new_node) return NULL; + + new_node->prev = NULL; + new_node->next = l; + new_node->data = e; + l->prev = new_node; + + return new_node; + } int dlist_insert(struct dlist **l, size_t index, int e) { - //FIXME + struct dlist *tmp = *l; + + if(index == 0){ + dlist_append(*l, e); + return 0; + } + + size_t ind = 0; + while(tmp != NULL && ind < index - 1){ + + tmp = tmp->next; + ind ++; + } + + if(!tmp) return 1; + + struct dlist *new_node = malloc(sizeof(struct dlist)); + if(!new_node) return 1; + + new_node->data = e; + new_node->next = tmp; + tmp->prev->next = new_node; + new_node->prev = tmp->prev; + tmp->prev = new_node; + + return 0; + + } struct dlist *dlist_delete_at(struct dlist **l, size_t index) { - //FIXME + if(!l || *l) return NULL; + + + struct dlist *old = *l; + if(index == 0){ + + old->next->prev = NULL; + *l = old->next; + return old; + } + + struct dlist *tmp = *l; + size_t ind = 0; + while(tmp != NULL && ind < index -1){ + + tmp = tmp->next; + ind ++; + } + + if(ind == index -1){ + + old = tmp->next; + tmp->prev = old->prev; + tmp->next = old->next; + return old; + } + return NULL; } int dlist_remove(struct dlist **l, int e) { - //FIXME + struct dlist *old = NULL; + struct dlist *tmp = *l; + + while(tmp != NULL && tmp->data != e){ + + old = tmp; + tmp = tmp->next; + } + + if(!tmp) return 0; + + if(!old){ + + *l = tmp->next; + } + else { + + old->next = tmp->next; + old->prev = tmp->prev; + } + + free(tmp); + + return 1; } void dlist_print(struct dlist *l) { - //FIXME + + struct dlist *tmp = l; + + while (tmp != NULL){ + + printf("%i", tmp->data); + + if (tmp->next != NULL) printf(" <-> "); + + tmp = tmp->next; + } + + printf("\n"); + } diff --git a/tests/test_basics.c b/tests/tests.c similarity index 55% rename from tests/test_basics.c rename to tests/tests.c index db2d3d6..8717f10 100644 --- a/tests/test_basics.c +++ b/tests/tests.c @@ -1,7 +1,9 @@ #include #include +#include #include #include "../Chains_across_the_Island/Fundamentals/basics/basics.h" +#include "../Chains_across_the_Island/Fundamentals/double/double.h" #include // Test pour list_append Test(basics_suite, test_append) { @@ -152,3 +154,142 @@ Test(basics_suite, test_list_remove){ list_destroy(l); } +// TEST POUR double + +void dlist_destroy(struct dlist *l){ + + struct dlist *tmp = l; + struct dlist *nex = NULL; + + while(tmp != NULL) { + nex = tmp->next; + free(tmp); + tmp = nex; + } +} + +int dlist_count(struct dlist *l){ + + struct dlist *tmp = l; + int ind = 0; + + while(tmp != NULL) { + + ind ++; + tmp = tmp->next; + } + return ind; +} + +struct dlist *dlist_get(struct dlist *l, size_t index){ + + struct dlist *tmp = l; + size_t ind = 0; + + while(tmp != NULL && ind < index){ + tmp = tmp->next; + ind ++; + } + + return tmp; + +} + +//Test dlist_append +Test(double_suite, test_append){ + + struct dlist *l = NULL; + + l = dlist_append(l, 3); // [3] + cr_assert_not_null(l, "la liste ne peux pas etre null apres un ajout"); + cr_assert_eq(l->data, 3, "la data du 1er noeud doit être 3"); + + l = dlist_append(l, 2); // [2] <-> [3] + cr_assert_not_null(l->next, "le deuxieme noeud doit exister"); + cr_assert_eq(l->data, 2, "Le premier noeud doit etre 2"); + + l = dlist_append(l, 1); // [1] <-> [2] <-> [3] + cr_assert_not_null(l->next, "le troisieme noeud doit exister"); + cr_assert_eq(l->data, 1, "Le premier noeud doit etre 1"); + cr_assert_eq(l->next->data, 2, "Le second noeud doit etre 2"); + + dlist_destroy(l); +} + +Test(double_suite, test_insert){ + + struct dlist *l = NULL; + + int res1 = dlist_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 = dlist_insert(&l, 5, 200); + cr_assert_eq(res2, 1, "Out of Range !"); + + dlist_destroy(l); +} + +Test(double_suite, test_delete_at){ + + struct dlist *l = NULL; + + l = dlist_append(l, 30); + l = dlist_append(l, 20); + l = dlist_append(l, 10); // [10] <-> [20] <-> [30] + + struct dlist *rslt = NULL; + + rslt = dlist_delete_at(&l, 1); // Retire le 20. l devient [10] -> [30] + 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 = dlist_count(l); + cr_assert_eq(count, 2, "La taille de la liste doit etre de 2"); + + struct dlist *tmp = dlist_get(l, 0); + cr_assert_not_null(tmp, "Le noeud 0 ne doit pas être NULL"); + cr_assert_eq(tmp->data, 10, "La data du noeud 1 doit etre de 10"); + + tmp = dlist_get(l, 1); + cr_assert_not_null(tmp, "Le noeud 1 ne doit pas être NULL"); + cr_assert_eq(tmp->data, 30, "La data du noeud 2 doit etre de 30"); + + tmp = dlist_get(l, 3); + cr_assert_null(tmp, "Le pointeur doit etre null"); + + dlist_destroy(l); +} + +Test(double_suite, test_remove){ + + struct dlist *l = NULL; + + l = dlist_append(l, 7); + l = dlist_append(l, 3); + l = dlist_append(l, 7); + l = dlist_append(l, 1); // [1] <-> [7] <-> [3] <-> [7] + + int rm = dlist_remove(&l, 7); // Retire le premier 7. l devient [1] <-> [3] <-> [7] + cr_assert_eq(rm, 1, "Le noeud 7 doit etre supprimé"); + + struct dlist *tmp = dlist_get(l, 0); + cr_assert_not_null(tmp, "Le noeud 0 existe"); + cr_assert_eq(tmp->data, 1, "Le noeud 1 a changé et devient 1 !"); + + tmp = dlist_get(l, 1); + cr_assert_not_null(tmp, "Le noeud 1 existe"); + cr_assert_eq(tmp->data, 3, "Le noeud 2 doit etre egal a 3"); + + tmp = dlist_get(l, 2); + cr_assert_not_null(tmp, "Le noeud 2 existe"); + cr_assert_eq(tmp->data,7, "Le 3 eme noeud doit etre de 7"); + + rm = dlist_remove(&l, 42); + cr_assert_eq(rm, 0, "Doit renvoyer 0 car 42 n'existe pas !"); + + dlist_destroy(l); + + +}