2017-11-30 21:00:47 +01:00
|
|
|
#include <check.h>
|
|
|
|
#include <stdlib.h>
|
2017-12-13 20:31:04 +01:00
|
|
|
#include <stdio.h>
|
2017-12-19 09:06:21 +01:00
|
|
|
#include "../src/linkedlist.h"
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
START_TEST(test_linkedlist_create)
|
|
|
|
{
|
|
|
|
LinkedList *list = linkedlist_create();
|
|
|
|
ck_assert( list == NULL );
|
|
|
|
linkedlist_destroy(&list);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_linkedlist_append)
|
|
|
|
{
|
2017-12-13 20:31:04 +01:00
|
|
|
int *v1, *v2;
|
|
|
|
|
|
|
|
v1 = malloc(sizeof(int));
|
|
|
|
v2 = malloc(sizeof(int));
|
|
|
|
|
|
|
|
*v1 = 34;
|
|
|
|
*v2 = 44;
|
|
|
|
|
2017-11-30 21:00:47 +01:00
|
|
|
LinkedList *list = linkedlist_create();
|
|
|
|
ck_assert(linkedlist_size(list) == 0);
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_append(&list, v1);
|
2017-11-30 21:00:47 +01:00
|
|
|
ck_assert(linkedlist_size(list) == 1);
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_append(&list, v2);
|
2017-11-30 21:00:47 +01:00
|
|
|
ck_assert(linkedlist_size(list) == 2);
|
|
|
|
linkedlist_destroy(&list);
|
|
|
|
ck_assert( list == NULL );
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_linkedlist_poplast)
|
|
|
|
{
|
2017-12-13 20:31:04 +01:00
|
|
|
int *v1, *v2, *v3;
|
|
|
|
|
|
|
|
v1 = malloc(sizeof(int));
|
|
|
|
v2 = malloc(sizeof(int));
|
|
|
|
v3 = malloc(sizeof(int));
|
|
|
|
|
|
|
|
*v1 = 1;
|
|
|
|
*v2 = 2;
|
|
|
|
*v3 = 3;
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
int *pop1;
|
|
|
|
int *pop2;
|
|
|
|
int *pop3;
|
|
|
|
|
|
|
|
LinkedList *list = linkedlist_create();
|
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 0);
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_append(&list, v3);
|
|
|
|
linkedlist_append(&list, v2);
|
|
|
|
linkedlist_append(&list, v1);
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 3);
|
|
|
|
|
|
|
|
pop1 = linkedlist_poplast(&list);
|
|
|
|
pop2 = linkedlist_poplast(&list);
|
|
|
|
pop3 = linkedlist_poplast(&list);
|
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 0);
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
ck_assert(*pop1 == *v1);
|
|
|
|
ck_assert(*pop2 == *v2);
|
|
|
|
ck_assert(*pop3 == *v3);
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
free(pop1);
|
|
|
|
free(pop2);
|
|
|
|
free(pop3);
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_destroy(&list);
|
|
|
|
|
2017-11-30 21:00:47 +01:00
|
|
|
ck_assert(list == NULL);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_linkedlist_push)
|
|
|
|
{
|
2017-12-13 20:31:04 +01:00
|
|
|
int *v1, *v2;
|
|
|
|
|
|
|
|
v1 = malloc(sizeof(int));
|
|
|
|
v2 = malloc(sizeof(int));
|
|
|
|
|
|
|
|
*v1 = 1;
|
|
|
|
*v2 = 1;
|
|
|
|
|
2017-11-30 21:00:47 +01:00
|
|
|
LinkedList *list = linkedlist_create();
|
|
|
|
ck_assert(linkedlist_size(list) == 0);
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_push(&list, v1);
|
2017-11-30 21:00:47 +01:00
|
|
|
ck_assert(linkedlist_size(list) == 1);
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_push(&list, v2);
|
2017-11-30 21:00:47 +01:00
|
|
|
ck_assert(linkedlist_size(list) == 2);
|
|
|
|
linkedlist_destroy(&list);
|
|
|
|
ck_assert( list == NULL );
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_linkedlist_pop)
|
|
|
|
{
|
2017-12-13 20:31:04 +01:00
|
|
|
int *value1, *value2, *value3;
|
|
|
|
int *pop1, *pop2, *pop3;
|
2017-11-30 21:00:47 +01:00
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
value1 = malloc(sizeof(int));
|
|
|
|
value2 = malloc(sizeof(int));
|
|
|
|
value3 = malloc(sizeof(int));
|
|
|
|
|
|
|
|
*value1 = 1;
|
|
|
|
*value2 = 2;
|
|
|
|
*value3 = 3;
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
LinkedList *list = linkedlist_create();
|
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 0);
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_push(&list, value3);
|
|
|
|
linkedlist_push(&list, value2);
|
|
|
|
linkedlist_push(&list, value1);
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 3);
|
|
|
|
|
|
|
|
pop1 = linkedlist_pop(&list);
|
|
|
|
pop2 = linkedlist_pop(&list);
|
|
|
|
pop3 = linkedlist_pop(&list);
|
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 0);
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
ck_assert(*pop1 == *value1);
|
|
|
|
ck_assert(*pop2 == *value2);
|
|
|
|
ck_assert(*pop3 == *value3);
|
|
|
|
|
|
|
|
linkedlist_destroy(&list);
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
free(pop1);
|
|
|
|
free(pop2);
|
|
|
|
free(pop3);
|
|
|
|
|
|
|
|
ck_assert(list == NULL);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2017-12-02 16:24:31 +01:00
|
|
|
START_TEST(test_linkedlist_get_index)
|
|
|
|
{
|
2017-12-13 20:31:04 +01:00
|
|
|
int *value1, *value2, *get;
|
|
|
|
|
|
|
|
value1 = malloc(sizeof(int));
|
|
|
|
value2 = malloc(sizeof(int));
|
|
|
|
|
|
|
|
*value1 = 1;
|
|
|
|
*value2 = 2;
|
2017-12-02 16:24:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
LinkedList *list = linkedlist_create();
|
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 0);
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_push(&list, value2);
|
|
|
|
linkedlist_push(&list, value1);
|
2017-12-02 16:24:31 +01:00
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 2);
|
|
|
|
|
|
|
|
get = linkedlist_get(&list, 1);
|
|
|
|
|
|
|
|
ck_assert(linkedlist_size(list) == 2);
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
ck_assert(*get == *value2);
|
|
|
|
ck_assert(get == value2);
|
2017-12-02 16:24:31 +01:00
|
|
|
|
|
|
|
linkedlist_destroy(&list);
|
|
|
|
|
|
|
|
ck_assert(list == NULL);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
static void
|
|
|
|
increase(int *number)
|
|
|
|
{
|
|
|
|
(*number)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST (test_linkedlist_each)
|
|
|
|
{
|
|
|
|
LinkedList *list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
list = linkedlist_create();
|
|
|
|
|
|
|
|
for (i = 0; i < 10; ++i) {
|
2017-12-13 23:35:06 +01:00
|
|
|
int *append = malloc(sizeof(int));
|
2017-12-13 20:31:04 +01:00
|
|
|
*append = i;
|
|
|
|
linkedlist_append(&list, append);
|
|
|
|
}
|
|
|
|
|
|
|
|
linkedlist_each(&list, (void (*)(void*)) increase);
|
|
|
|
|
|
|
|
for (i = 0; i < 10; ++i) {
|
2017-12-13 23:27:20 +01:00
|
|
|
int *pop = linkedlist_pop(&list);
|
2017-12-13 20:31:04 +01:00
|
|
|
ck_assert( i+1 == *pop );
|
|
|
|
free(pop);
|
|
|
|
pop = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
linkedlist_destroy(&list);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2017-11-30 21:00:47 +01:00
|
|
|
Suite* t_suite_create()
|
|
|
|
{
|
|
|
|
Suite *s;
|
|
|
|
TCase *tc_core;
|
|
|
|
|
|
|
|
s = suite_create("LinkedList");
|
|
|
|
tc_core = tcase_create("Core");
|
|
|
|
|
|
|
|
tcase_add_test(tc_core, test_linkedlist_create);
|
|
|
|
tcase_add_test(tc_core, test_linkedlist_push);
|
|
|
|
tcase_add_test(tc_core, test_linkedlist_pop);
|
|
|
|
tcase_add_test(tc_core, test_linkedlist_append);
|
|
|
|
tcase_add_test(tc_core, test_linkedlist_poplast);
|
2017-12-02 16:24:31 +01:00
|
|
|
tcase_add_test(tc_core, test_linkedlist_get_index);
|
2017-12-13 20:31:04 +01:00
|
|
|
tcase_add_test(tc_core, test_linkedlist_each);
|
2017-11-30 21:00:47 +01:00
|
|
|
suite_add_tcase(s, tc_core);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
int number_failed;
|
|
|
|
Suite *s;
|
|
|
|
SRunner *sr;
|
|
|
|
|
|
|
|
s = t_suite_create();
|
|
|
|
sr = srunner_create(s);
|
|
|
|
|
|
|
|
srunner_run_all(sr, CK_NORMAL);
|
|
|
|
number_failed = srunner_ntests_failed(sr);
|
|
|
|
|
|
|
|
srunner_free(sr);
|
|
|
|
|
|
|
|
return number_failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|