301 lines
7.9 KiB
C
301 lines
7.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* test.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */
|
|
/* Updated: 2025/04/12 18:33:43 by tomoron ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
#include "libasm.h"
|
|
|
|
#define OK "\033[32mOK\n\033[0m"
|
|
#define KO "\033[31mKO\n\033[0m"
|
|
|
|
#define FNC_TEST(title, condition)\
|
|
printf(title":\n");\
|
|
if(!condition)\
|
|
printf("RESULT: "OK);\
|
|
else\
|
|
printf(" RESULT: "KO);
|
|
|
|
#define TEST(title, condition) \
|
|
write(1, title, strlen(title));\
|
|
write(1, ": ", 2);\
|
|
if(condition)\
|
|
write(1, OK, strlen(OK));\
|
|
else \
|
|
{\
|
|
write(1, KO, strlen(KO));\
|
|
fail++;\
|
|
}
|
|
|
|
int test_strlen(void)
|
|
{
|
|
int fail = 0;
|
|
|
|
TEST("normal string", ft_strlen("hello") == 5);
|
|
TEST("empty string", ft_strlen("") == 0);
|
|
TEST("null pointer", ft_strlen(NULL) == 0);
|
|
|
|
return(fail);
|
|
}
|
|
|
|
int test_strcpy(void)
|
|
{
|
|
int fail = 0;
|
|
char buf[256];
|
|
|
|
memset(buf,'a', 255);
|
|
buf[255] = 0;
|
|
|
|
TEST("copy normal string", strcmp(ft_strcpy(buf, "patate douce"),"patate douce") == 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(fail);
|
|
}
|
|
|
|
int test_strcmp(void)
|
|
{
|
|
int fail = 0;
|
|
|
|
TEST("equal string", ft_strcmp("coucou", "coucou") == 0);
|
|
TEST("second smaller string", ft_strcmp("coucou", "c") == 1);
|
|
TEST("first smaller string", ft_strcmp("c", "coucou") == -1);
|
|
TEST("first null", ft_strcmp(NULL, "coucou") == 0);
|
|
TEST("second null", ft_strcmp("hello", NULL) == 0);
|
|
|
|
return(fail);
|
|
}
|
|
|
|
int test_write(void)
|
|
{
|
|
int fail = 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);
|
|
TEST("failed write errno test", errno == 9);
|
|
errno = 0;
|
|
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(fail);
|
|
}
|
|
|
|
int test_read(void)
|
|
{
|
|
int fail = 0;
|
|
int fd;
|
|
char buf[512];
|
|
|
|
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);
|
|
close(fd);
|
|
|
|
fd = open("test_file.tmp", O_RDONLY);
|
|
TEST("read 1 char", ft_read(fd, buf, 1) == 1);
|
|
TEST("check read char", memcmp(buf, "p", 1) == 0);
|
|
TEST("read multiple char", ft_read(fd, buf, 5) == 5);
|
|
TEST("compare with expected", memcmp(buf, "atate", 5) == 0);
|
|
|
|
errno = 0;
|
|
TEST("invalid fd read, return", ft_read(-1, buf, 20) == (size_t)-1);
|
|
TEST("invalid fd read, errno", errno == 9);
|
|
errno = 0;
|
|
TEST("read to null pointer, return", ft_read(fd, NULL, 4) == (size_t)-1)
|
|
TEST("read to null pointer, errno", errno == 14)
|
|
close(fd);
|
|
unlink("test_file.tmp");
|
|
|
|
return(fail);
|
|
}
|
|
|
|
int test_strdup(void)
|
|
{
|
|
int fail = 0;
|
|
char *src = "patate douce";
|
|
char *dest;
|
|
|
|
dest = ft_strdup(src);
|
|
|
|
if(!dest)
|
|
{
|
|
printf("strdup failed, probably testing errno, returing with result\n");
|
|
return(errno == ENOMEM);
|
|
}
|
|
|
|
TEST("strdup normal stiring, diff pointer", dest != src);
|
|
TEST("strdup normal string , content", !strcmp(dest, src));
|
|
free(dest);
|
|
|
|
dest = ft_strdup("");
|
|
TEST("strdup empty string", *dest == 0);
|
|
free(dest);
|
|
TEST("strdup null pointer", ft_strdup(NULL) == NULL);
|
|
|
|
return(fail);
|
|
}
|
|
|
|
int test_atoi_base(void)
|
|
{
|
|
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);
|
|
TEST("normal number base 2", ft_atoi_base("101", "01") == 5);
|
|
|
|
TEST("0", ft_atoi_base("0", "0123456789") == 0);
|
|
TEST("leading zeros", ft_atoi_base("0001", "0123456789") == 1);
|
|
TEST("max 16-bit hex", ft_atoi_base("FFFF", "0123456789ABCDEF") == 65535);
|
|
|
|
TEST("empty number", ft_atoi_base("", "0123456789") == 0);
|
|
TEST("empty base", ft_atoi_base("123", "") == 0);
|
|
TEST("invalid digit in base", ft_atoi_base("42abc", "0123456789") == 42);
|
|
TEST("duplicate characters in base", ft_atoi_base("123", "01234566789") == 0);
|
|
|
|
TEST("large number within int range", ft_atoi_base("2147483647", "0123456789") == 2147483647);
|
|
TEST("negative number", ft_atoi_base("-42", "0123456789") == -42);
|
|
|
|
TEST("non-standard base ordering", ft_atoi_base("123", "9876543210") == 876);
|
|
TEST("base 3", ft_atoi_base("321", "123") == 21);
|
|
|
|
return(fail);
|
|
}
|
|
|
|
int test_list_push_front()
|
|
{
|
|
int fail = 0;
|
|
t_list *tmp;
|
|
|
|
tmp = (void *)0x321;
|
|
ft_list_push_front(&tmp, (void *)0x123);
|
|
TEST("does it have a different address", tmp != (void *)0x321);
|
|
TEST("value is in the right field", tmp->data == (void *)0x123);
|
|
TEST("is the next at prev", tmp->next == (void *)0x321);
|
|
|
|
free(tmp);
|
|
ft_list_push_front(0, 0);
|
|
TEST("call with null pointer (should not segfault)", 1);
|
|
|
|
return(fail);
|
|
}
|
|
|
|
int test_list_size()
|
|
{
|
|
int fail = 0;
|
|
t_list *tmp;
|
|
|
|
tmp = 0;
|
|
|
|
ft_list_push_front(&tmp, (void *)0x123);
|
|
ft_list_push_front(&tmp, (void *)0x123);
|
|
ft_list_push_front(&tmp, (void *)0x123);
|
|
|
|
TEST("null pointer (empty list)", ft_list_size(0) == 0);
|
|
TEST("list of 3", ft_list_size(tmp) == 3);
|
|
|
|
free(tmp->next->next);
|
|
free(tmp->next);
|
|
free(tmp);
|
|
|
|
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 remove_if_compare(void *data, int *nb)
|
|
{
|
|
return((long)data == *nb);
|
|
}
|
|
|
|
void remove_if_free(void *data)
|
|
{
|
|
printf("%ld\n", (long int) data);
|
|
}
|
|
|
|
int test_list_remove_if()
|
|
{
|
|
int fail = 0;
|
|
int nb = 2;
|
|
t_list *tmp;
|
|
|
|
tmp = 0;
|
|
ft_list_push_front(&tmp, (void *)4);
|
|
ft_list_push_front(&tmp, (void *)3);
|
|
ft_list_push_front(&tmp, (void *)2);
|
|
ft_list_push_front(&tmp, (void *)1);
|
|
|
|
ft_list_remove_if(&tmp, &nb, remove_if_compare, remove_if_free);
|
|
TEST("is the number 2 gone", (long int)tmp->next->data == 3);
|
|
nb = 1;
|
|
ft_list_remove_if(&tmp, &nb, remove_if_compare, remove_if_free);
|
|
TEST("is the number 1 gone", (long int)tmp->data == 3);
|
|
ft_list_remove_if(&tmp, &nb, 0, remove_if_free);
|
|
ft_list_remove_if(&tmp, &nb, remove_if_compare, 0);
|
|
TEST("null pointers in functions", 1);
|
|
|
|
return(fail);
|
|
}
|
|
|
|
|
|
int main(void)
|
|
{
|
|
FNC_TEST("ft_strlen", test_strlen());
|
|
printf("\n\n");
|
|
FNC_TEST("ft_strcpy", test_strcpy());
|
|
printf("\n\n");
|
|
FNC_TEST("ft_strcmp", test_strcmp());
|
|
printf("\n\n");
|
|
FNC_TEST("ft_write", test_write());
|
|
printf("\n\n");
|
|
FNC_TEST("ft_read", test_read());
|
|
printf("\n\n");
|
|
FNC_TEST("ft_strdup", test_strdup());
|
|
printf("\n\n");
|
|
FNC_TEST("atoi_base", test_atoi_base());
|
|
printf("\n\n");
|
|
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("ft_list_sort", test_list_remove_if());
|
|
printf("\n\n");
|
|
FNC_TEST("check valgrind", 1);
|
|
}
|