197 lines
4.9 KiB
C
197 lines
4.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* test.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */
|
|
/* Updated: 2025/03/30 15:55:13 by tomoron ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <errno.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));\
|
|
|
|
size_t ft_strlen(char *str);
|
|
char *ft_strcpy(char *dest, char *src);
|
|
int ft_strcmp(char *dest, char *src);
|
|
ssize_t ft_write(int fd, const void *buffer, size_t count);
|
|
ssize_t ft_read(int fd, void *buffer, size_t count);
|
|
char *ft_strdup(char *src);
|
|
|
|
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 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());
|
|
}
|