@@ -23,7 +23,7 @@ jobs:
|
|||||||
-o test
|
-o test
|
||||||
Chains_across_the_Island/Fundamentals/basics/basics.c
|
Chains_across_the_Island/Fundamentals/basics/basics.c
|
||||||
tests/test_basics.c
|
tests/test_basics.c
|
||||||
-lcriterion
|
-lcriterion -fsanitize=address -g
|
||||||
|
|
||||||
- name: Tests
|
- name: Tests
|
||||||
run: ./test
|
run: ./test
|
||||||
|
|||||||
+39
-51
@@ -1,8 +1,5 @@
|
|||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include <criterion/internal/assert.h>
|
#include <stdlib.h>
|
||||||
#include <criterion/internal/test.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "../Chains_across_the_Island/Fundamentals/basics/basics.h"
|
#include "../Chains_across_the_Island/Fundamentals/basics/basics.h"
|
||||||
|
|
||||||
// Test pour list_append
|
// Test pour list_append
|
||||||
@@ -29,9 +26,8 @@ Test(basics_suite, test_count) {
|
|||||||
l = list_append(l, 10);
|
l = list_append(l, 10);
|
||||||
l = list_append(l, 20);
|
l = list_append(l, 20);
|
||||||
l = list_append(l, 30);
|
l = list_append(l, 30);
|
||||||
//printf("%i\n", list_count(l));
|
|
||||||
cr_assert_eq(list_count(l), 3, "La liste doit contenir 3 éléments.");
|
cr_assert_eq(list_count(l), 3, "La liste doit contenir 3 éléments.");
|
||||||
// printf("%i\n", list_count(l));
|
|
||||||
list_destroy(l);
|
list_destroy(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,12 +35,11 @@ Test(basics_suite, test_count) {
|
|||||||
Test(basics_suite, test_insert) {
|
Test(basics_suite, test_insert) {
|
||||||
struct list *l = NULL;
|
struct list *l = NULL;
|
||||||
|
|
||||||
// Insertion dans une liste vide
|
|
||||||
int res1 = list_insert(&l, 0, 100);
|
int res1 = list_insert(&l, 0, 100);
|
||||||
cr_assert_eq(res1, 0, "L'insertion à l'index 0 doit réussir.");
|
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.");
|
cr_assert_eq(l->data, 100, "La valeur insérée doit être 100.");
|
||||||
|
|
||||||
// Insertion hors limite
|
|
||||||
int res2 = list_insert(&l, 5, 200);
|
int res2 = list_insert(&l, 5, 200);
|
||||||
cr_assert_eq(res2, 1, "L'insertion hors limite doit échouer et renvoyer 1.");
|
cr_assert_eq(res2, 1, "L'insertion hors limite doit échouer et renvoyer 1.");
|
||||||
|
|
||||||
@@ -55,26 +50,27 @@ Test(basics_suite, test_insert) {
|
|||||||
Test(basics_suite, test_get){
|
Test(basics_suite, test_get){
|
||||||
struct list *l = NULL;
|
struct list *l = NULL;
|
||||||
|
|
||||||
l = list_append(l, 30); // l = [30] -> NULL
|
l = list_append(l, 30);
|
||||||
l = list_append(l, 20); // l = [20] -> [30] -> NULL
|
l = list_append(l, 20);
|
||||||
l = list_append(l, 10); // l = [10] -> [20] -> [30] -> NULL
|
l = list_append(l, 10); // L'ordre final est [30] -> [20] -> [10]
|
||||||
|
|
||||||
struct list *rslt = list_get(l,0);
|
struct list *rslt = list_get(l, 0);
|
||||||
cr_assert_eq(rslt->data, 10, "pointer to node with data=10");
|
cr_assert_not_null(rslt, "Le pointeur ne doit pas être NULL pour l'index 0");
|
||||||
|
|
||||||
rslt = list_get(l, 2);
|
|
||||||
cr_assert_eq(rslt->data, 30, "pointer to node with data=30");
|
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);
|
rslt = list_get(l, 5);
|
||||||
cr_assert_null(rslt, "Poit doit etre null");
|
cr_assert_null(rslt, "Le pointeur doit etre null");
|
||||||
|
|
||||||
list_destroy(l);
|
list_destroy(l);
|
||||||
list_destroy(rslt);
|
// On ne détruit SURTOUT PAS rslt ici, car il appartient déjà à l !
|
||||||
}
|
}
|
||||||
|
|
||||||
//Test list_find
|
//Test list_find
|
||||||
Test(basics_suite, test_find){
|
Test(basics_suite, test_find){
|
||||||
|
|
||||||
struct list *l = NULL;
|
struct list *l = NULL;
|
||||||
|
|
||||||
l = list_append(l, 3);
|
l = list_append(l, 3);
|
||||||
@@ -82,81 +78,73 @@ Test(basics_suite, test_find){
|
|||||||
l = list_append(l, 1);
|
l = list_append(l, 1);
|
||||||
|
|
||||||
struct list *rslt = list_find(l, 7);
|
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");
|
cr_assert_eq(rslt->data, 7, "pointer to node with data=7");
|
||||||
|
|
||||||
rslt = list_find(l, 42);
|
rslt = list_find(l, 42);
|
||||||
cr_assert_null(rslt, "pointeur doit etre null");
|
cr_assert_null(rslt, "pointeur doit etre null");
|
||||||
|
|
||||||
list_destroy(l);
|
list_destroy(l);
|
||||||
list_destroy(rslt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Test list_delete_at
|
//Test list_delete_at
|
||||||
Test(basics_suite, test_delete_at){
|
Test(basics_suite, test_delete_at){
|
||||||
|
|
||||||
|
|
||||||
struct list *l = NULL;
|
struct list *l = NULL;
|
||||||
|
|
||||||
l = list_append(l, 30);
|
l = list_append(l, 30);
|
||||||
l = list_append(l, 20);
|
l = list_append(l, 20);
|
||||||
l = list_append(l, 10);
|
l = list_append(l, 10); // [30] -> [20] -> [10]
|
||||||
|
|
||||||
struct list *rslt = NULL;
|
struct list *rslt = NULL;
|
||||||
|
|
||||||
rslt = list_delete_at(&l, 1); // returns pointer to node with data=20, l = [10] -> [30]
|
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");
|
cr_assert_eq(rslt->data, 20, "La data du noeud supprimé doit etre de 20");
|
||||||
|
|
||||||
size_t count = 0;
|
size_t count = list_count(l);
|
||||||
count = list_count(l);
|
|
||||||
cr_assert_eq(count, 2, "La taille de la liste doit etre de 2");
|
cr_assert_eq(count, 2, "La taille de la liste doit etre de 2");
|
||||||
|
|
||||||
rslt = list_get(l, 0);
|
struct list *tmp = list_get(l, 0);
|
||||||
cr_assert_eq(rslt->data, 10, "La data du noeud 1 doit etre de 10");
|
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");
|
||||||
|
|
||||||
rslt = list_get(l, 1);
|
tmp = list_get(l, 1);
|
||||||
cr_assert_eq(rslt->data, 30, "La data du noeud 2 doit etre de 30");
|
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");
|
||||||
|
|
||||||
rslt = list_get(l, 3);
|
tmp = list_get(l, 3);
|
||||||
cr_assert_null(rslt, "Le pointeur doit etre null");
|
cr_assert_null(tmp, "Le pointeur doit etre null");
|
||||||
|
|
||||||
list_destroy(l);
|
list_destroy(l);
|
||||||
list_destroy(rslt);
|
free(rslt); // rslt est totalement détaché de l, on le libère avec un simple free()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Test list_remove
|
//Test list_remove
|
||||||
Test(basics_suite, test_list_remove){
|
Test(basics_suite, test_list_remove){
|
||||||
|
|
||||||
struct list *l = NULL;
|
struct list *l = NULL;
|
||||||
|
|
||||||
l = list_append(l, 7);
|
l = list_append(l, 7);
|
||||||
l = list_append(l, 3);
|
l = list_append(l, 3);
|
||||||
l = list_append(l, 7);
|
l = list_append(l, 7);
|
||||||
l = list_append(l, 1);
|
l = list_append(l, 1); // [7] -> [3] -> [7] -> [1]
|
||||||
|
|
||||||
|
int rm = list_remove(&l, 7); // Retire le premier 7. l devient [3] -> [7] -> [1]
|
||||||
int rm = 0;
|
|
||||||
struct list *tmp = l;
|
|
||||||
|
|
||||||
rm = list_remove(&l, 7); // returns 1, l = [1] -> [3] -> [7]
|
|
||||||
cr_assert_eq(rm, 1, "Le noeud 7 doit etre supprimé");
|
cr_assert_eq(rm, 1, "Le noeud 7 doit etre supprimé");
|
||||||
|
|
||||||
tmp = list_get(l, 0);
|
struct list *tmp = list_get(l, 0);
|
||||||
cr_assert_eq(tmp->data, 1, "Le noeud 1 a changé !");
|
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);
|
tmp = list_get(l, 1);
|
||||||
cr_assert_eq(tmp->data, 3, "Le noeud 2 doit etre egal a 3");
|
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);
|
tmp = list_get(l, 2);
|
||||||
cr_assert_eq(tmp->data, 7, "Le 3 eme noeud doit etre de 7");
|
cr_assert_not_null(tmp, "Le noeud 2 existe");
|
||||||
|
cr_assert_eq(tmp->data, 1, "Le 3 eme noeud doit etre de 1");
|
||||||
|
|
||||||
l = tmp;
|
rm = list_remove(&l, 42);
|
||||||
rm = list_remove(&l, 42); // returns 0, list unchanged
|
cr_assert_eq(rm, 0, "Doit renvoyer 0 car 42 n'existe pas !");
|
||||||
cr_assert_eq(rm, 0, "Doit renvoyer 0 !");
|
|
||||||
cr_assert_eq(l, tmp, "La liste ne doit pas changer");
|
|
||||||
|
|
||||||
list_destroy(l);
|
list_destroy(l);
|
||||||
list_destroy(tmp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user