diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml new file mode 100644 index 0000000..aa1aff5 --- /dev/null +++ b/.gitea/workflows/test.yaml @@ -0,0 +1,22 @@ +name: Tests C avec Criterion + +on: [push] + +jobs: + test: + runs-on: ubuntu-latest + container: + image: alpine:latest + + steps: + - name: Récupérer le code + uses: actions/checkout@v4 + + - name: Installer GCC et Criterion + run: apk add --no-cache gcc musl-dev criterion-dev + + - name: Compiler + run: gcc -Wall -Wextra -I./Chains_across_the_Island/Fundamentals/basics/ -o tests Chains_across_the_Island/Fundamentals/basics/basics.c tests/tests.c -lcriterion + + - name: Executer + run: ./tests diff --git a/Chains_across_the_Island/Fundamentals/basics/basics.c b/Chains_across_the_Island/Fundamentals/basics/basics.c index 7910492..0165a96 100644 --- a/Chains_across_the_Island/Fundamentals/basics/basics.c +++ b/Chains_across_the_Island/Fundamentals/basics/basics.c @@ -1,16 +1,16 @@ #include "basics.h" #include +#include #include struct list *list_append(struct list *l, int e) { - if (!l) return NULL; - struct list *tmp = l; + struct list *tmp = l; struct list *new_node = calloc(1, sizeof(struct list)); if (!new_node) return NULL; new_node->data = e; - if(!tmp) return new_node; + if(!l) return new_node; while (tmp->next) tmp = tmp->next; tmp->next = new_node; @@ -84,17 +84,99 @@ void list_destroy(struct list *l){ free(l); } +struct list *list_find(struct list *l, int e){ + + if(!l) return NULL; + + struct list *tmp = l; + + while (tmp->data != e) tmp = tmp->next; + + return tmp; + +} + +struct list *list_delete_at(struct list **l, size_t index){ + + struct list *old = *l; + + if ( index == 0){ + *l = old->next; + return old; + } + + struct list *tmp = *l; + + size_t ind = 0; + while(tmp->next != NULL && ind < index - 1){ + + tmp = tmp->next; + ind ++; + } + + if (ind < index) { + old = tmp->next; + tmp->next = old->next; + return old; + } + return NULL; + +} +int list_remove(struct list **l, int e){ + + struct list *old = *l; + struct list *tmp = *l; + + while (tmp->next != NULL && tmp->data != e){ + + tmp = tmp->next; + } + + if(tmp->data == e) { + + old = tmp->next; + tmp->next = old->next; + return 1; + } + return 0; +} + +void list_print(struct list *l){ + + if(!l) { + printf("\n"); + return ; + } + + struct list *tmp = l; + + while (tmp != NULL){ + + printf("%i",tmp->data); + + if (tmp->next != NULL) printf(" -> "); + + tmp = tmp->next; + + } + 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_count(NULL); // 0 - list_count(l); // 3 + list_print(l); - // Here, you should call "list_destroy(l);" when it will be implemented to - // prevent any memory leak + // 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_destroy(l); return 0; } diff --git a/Chains_across_the_Island/Fundamentals/basics/test b/Chains_across_the_Island/Fundamentals/basics/test index 9c3ddc3..350dd97 100755 Binary files a/Chains_across_the_Island/Fundamentals/basics/test and b/Chains_across_the_Island/Fundamentals/basics/test differ diff --git a/tests/tests.c b/tests/tests.c new file mode 100644 index 0000000..b3ed00e --- /dev/null +++ b/tests/tests.c @@ -0,0 +1,48 @@ +#include +#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 doit pas être NULL après le premier ajout."); + cr_assert_eq(l->data, 42, "La donnée du premier nœud doit être 42."); + + l = list_append(l, 7); + cr_assert_not_null(l->next, "Le deuxième nœud doit exister."); + cr_assert_eq(l->next->data, 7, "La donnée du deuxième nœud doit être 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; + + // Insertion dans une liste vide + int res1 = list_insert(&l, 0, 100); + cr_assert_eq(res1, 0, "L'insertion à l'index 0 doit réussir."); + cr_assert_eq(l->data, 100, "La valeur insérée doit être 100."); + + // Insertion hors limite + int res2 = list_insert(&l, 5, 200); + cr_assert_eq(res2, 1, "L'insertion hors limite doit échouer et renvoyer 1."); + + list_destroy(l); +}