Files
libasm/test.c

274 lines
6.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#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));\
passed++;\
}\
else \
write(1, KO, strlen(KO));\
int test_strlen(void)
{
int nb_tests;
int passed;
nb_tests = 3;
passed = 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);
}
int test_strcpy(void)
{
int nb_tests;
int passed;
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);
}
int test_strcmp(void)
{
int nb_tests;
int passed;
nb_tests = 5;
passed = 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(nb_tests == passed);
}
int test_write(void)
{
int nb_tests;
int passed;
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);
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(nb_tests == passed);
}
int test_read(void)
{
int nb_tests;
int passed;
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);
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(nb_tests == passed);
}
int test_strdup(void)
{
int nb_tests;
int passed;
char *src = "patate douce";
char *dest;
nb_tests = 4;
passed = 0;
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(nb_tests == passed);
}
int test_atoi_base(void)
{
int nb_tests;
int passed;
nb_tests = 14;
passed = 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(nb_tests == passed);
}
int test_list_push_front()
{
int nb_tests;
int passed;
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);
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(nb_tests == passed);
}
int test_list_size()
{
int nb_tests;
int passed;
t_list *tmp;
nb_tests = 2;
passed = 0;
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(nb_tests == passed);
}
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());
}