add ft_list_sort

This commit is contained in:
2025-04-06 01:39:39 +02:00
parent 34b139c60b
commit 919948abe9
11 changed files with 163 additions and 86 deletions

111
test.c
View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */
/* Updated: 2025/04/03 04:52:46 by tomoron ### ########.fr */
/* Updated: 2025/04/06 01:39:26 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,7 +22,7 @@
#define FNC_TEST(title, condition)\
printf(title":\n");\
if(condition)\
if(!condition)\
printf("RESULT: "OK);\
else\
printf(" RESULT: "KO);
@ -31,57 +31,44 @@
write(1, title, strlen(title));\
write(1, ": ", 2);\
if(condition)\
{\
write(1, OK, strlen(OK));\
passed++;\
}\
else \
{\
write(1, KO, strlen(KO));\
fail++;\
}
int test_strlen(void)
{
int nb_tests;
int passed;
nb_tests = 3;
passed = 0;
int fail = 0;
TEST("normal string", ft_strlen("hello") == 5);
TEST("empty string", ft_strlen("") == 0);
TEST("null pointer", ft_strlen(NULL) == 0);
return(nb_tests == passed);
return(fail);
}
int test_strcpy(void)
{
int nb_tests;
int passed;
int fail = 0;
char buf[256];
memset(buf,'a', 255);
buf[255] = 0;
nb_tests = 5;
passed = 0;
TEST("copy normal string", strcmp(ft_strcpy(buf, "hello"),"hello") == 0);
TEST("copy empty string", strcmp(ft_strcpy(buf, ""),"") == 0);
TEST("copy to null pointer", ft_strcpy(NULL, "") == 0);
TEST("copy from null pointer", ft_strcpy("", NULL) == 0);
TEST("copy from and to null pointer", ft_strcpy(NULL, NULL) == 0);
return(nb_tests == passed);
return(fail);
}
int test_strcmp(void)
{
int nb_tests;
int passed;
nb_tests = 5;
passed = 0;
int fail = 0;
TEST("equal string", ft_strcmp("coucou", "coucou") == 0);
TEST("second smaller string", ft_strcmp("coucou", "c") == 1);
@ -89,17 +76,13 @@ int test_strcmp(void)
TEST("first null", ft_strcmp(NULL, "coucou") == 0);
TEST("second null", ft_strcmp("hello", NULL) == 0);
return(nb_tests == passed);
return(fail);
}
int test_write(void)
{
int nb_tests;
int passed;
int fail = 0;
nb_tests = 5;
passed = 0;
TEST("writing hello and test return value", ft_write(1, "hello ", 6) == 6);
errno = 0;
TEST("failed write return value", ft_write(-1, "", 0) == (size_t)-1);
@ -108,19 +91,15 @@ int test_write(void)
TEST("call write with null pointer, return value", ft_write(1, NULL, 1) == (size_t)-1);
TEST("call write with null pointer, errno", errno == 14);
return(nb_tests == passed);
return(fail);
}
int test_read(void)
{
int nb_tests;
int passed;
int fail = 0;
int fd;
char buf[512];
nb_tests = 8;
passed = 0;
printf("creating test file to test the read function\n");
fd = open("test_file.tmp", O_WRONLY | O_CREAT, 0600);
write(fd, "patate douce", 12);
@ -141,19 +120,15 @@ int test_read(void)
close(fd);
unlink("test_file.tmp");
return(nb_tests == passed);
return(fail);
}
int test_strdup(void)
{
int nb_tests;
int passed;
int fail = 0;
char *src = "patate douce";
char *dest;
nb_tests = 4;
passed = 0;
dest = ft_strdup(src);
if(!dest)
@ -171,17 +146,12 @@ int test_strdup(void)
free(dest);
TEST("strdup null pointer", ft_strdup(NULL) == NULL);
return(nb_tests == passed);
return(fail);
}
int test_atoi_base(void)
{
int nb_tests;
int passed;
nb_tests = 14;
passed = 0;
int fail = 0;
TEST("normal number base 10", ft_atoi_base("42", "0123456789") == 42);
TEST("normal number base 16", ft_atoi_base("1A4", "0123456789ABCDEF") == 420);
@ -202,18 +172,14 @@ int test_atoi_base(void)
TEST("non-standard base ordering", ft_atoi_base("123", "9876543210") == 876);
TEST("base 3", ft_atoi_base("321", "123") == 21);
return(nb_tests == passed);
return(fail);
}
int test_list_push_front()
{
int nb_tests;
int passed;
int fail = 0;
t_list *tmp;
nb_tests = 4;
passed = 0;
tmp = (void *)0x321;
ft_list_push_front(&tmp, (void *)0x123);
TEST("does it have a different address", tmp != (void *)0x321);
@ -224,17 +190,14 @@ int test_list_push_front()
ft_list_push_front(0, 0);
TEST("call with null pointer (should not segfault)", 1);
return(nb_tests == passed);
return(fail);
}
int test_list_size()
{
int nb_tests;
int passed;
int fail = 0;
t_list *tmp;
nb_tests = 2;
passed = 0;
tmp = 0;
ft_list_push_front(&tmp, (void *)0x123);
@ -248,9 +211,33 @@ int test_list_size()
free(tmp->next);
free(tmp);
return(nb_tests == passed);
return(fail);
}
int test_list_sort()
{
int fail = 0;
t_list *tmp;
tmp = 0;
ft_list_push_front(&tmp, "d");
ft_list_push_front(&tmp, "a");
ft_list_push_front(&tmp, "b");
ft_list_push_front(&tmp, "c");
ft_list_sort(&tmp, strcmp);
TEST("is list ordered 1", (*(char *)tmp->data) == 'a');
TEST("is list ordered 2", (*(char *)tmp->next->data) == 'b');
TEST("is list ordered 3", (*(char *)tmp->next->next->data) == 'c');
TEST("is list ordered 4", (*(char *)tmp->next->next->next->data) == 'd');
ft_list_sort(0, 0);
TEST("null pointer (should not segfault)", 1);
return(fail);
}
int main(void)
{
FNC_TEST("ft_strlen", test_strlen());
@ -270,4 +257,8 @@ int main(void)
FNC_TEST("list_push_front", test_list_push_front());
printf("\n\n");
FNC_TEST("list_size", test_list_size());
printf("\n\n");
FNC_TEST("ft_list_sort", test_list_sort());
printf("\n\n");
FNC_TEST("check valgrind", 1);
}