libasm/test/main.c

282 lines
7.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/12 14:02:17 by rparodi #+# #+# */
/* Updated: 2026/01/13 12:12:01 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libasm.h"
#include "_internal_test.h"
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdbool.h>
char *strs[] = {
"",
"a",
"aa",
"aaa",
" ",
" ",
"\t",
"\n",
"\t\n ",
"Hello\0 World!",
"Hello World!",
"Hello World!",
"assembly is fun!",
"Assembly is fun!",
"Assembly is fun!!",
"42",
"0042",
"42Paris",
"azerty",
"azertY",
"azertyuiopqsdfghjklm",
"0123456789",
"!@#$%^&*()_+-=[]{}|;:',.<>/?",
"é",
"école",
"École",
LOREM,
LOREM_LONG,
NULL
};
static void _test_strlen()
{
printf("\n%sTesting '%sstrlen%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
ft_strlen(NULL);
printf("%s✔%s ", CLR_GREEN, RESET);
for (int i = 0; strs[i]; i++)
{
size_t original = strlen(strs[i]);
size_t homemade = ft_strlen(strs[i]);
if (original == homemade)
printf("%s✔%s", CLR_GREEN, RESET);
else
dprintf(2, "\t%s✘%s", CLR_RED, RESET);
putchar(' ');
}
putchar('\n');
}
static void _test_strcmp()
{
printf("\n%sTesting '%sstrcmp%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
ft_strcmp(NULL, "a");
printf("%s✔%s ", CLR_GREEN, RESET);
ft_strcmp("a", NULL);
printf("%s✔%s ", CLR_GREEN, RESET);
ft_strcmp(NULL, NULL);
printf("%s✔%s ", CLR_GREEN, RESET);
for (int i = 0; strs[i]; i++)
{
for (int j = i; strs[i] && strs[j]; j++)
{
size_t original = strcmp(strs[i], strs[j]);
size_t homemade = ft_strcmp(strs[i], strs[j]);
if (original == homemade)
printf("%s✔%s", CLR_GREEN, RESET);
else
dprintf(2, "\t%s✘%s", CLR_RED, RESET);
putchar(' ');
}
}
putchar('\n');
}
static void _test_strcpy(void)
{
printf("\n%sTesting '%sstrcpy%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
ft_strcpy(NULL, "a");
printf("%s✔%s ", CLR_GREEN, RESET);
ft_strcpy("a", NULL);
printf("%s✔%s ", CLR_GREEN, RESET);
ft_strcpy(NULL, NULL);
printf("%s✔%s ", CLR_GREEN, RESET);
for (int i = 0; strs[i]; i++)
{
char buf_orig[8192];
char buf_home[8192];
memset(buf_orig, 0, sizeof(buf_orig));
memset(buf_home, 0, sizeof(buf_home));
char *ret_orig = strcpy(buf_orig, strs[i]);
char *ret_home = ft_strcpy(buf_home, strs[i]);
if (strcmp(buf_orig, buf_home) == 0 && ret_orig == buf_orig && ret_home == buf_home)
printf("%s✔%s", CLR_GREEN, RESET);
else
dprintf(2, "\t%s✘%s", CLR_RED, RESET);
putchar(' ');
}
putchar('\n');
}
static void _test_strdup(void)
{
printf("\n%sTesting '%sstrdup%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
ft_strdup(NULL);
printf("%s✔%s ", CLR_GREEN, RESET);
for (int i = 0; strs[i]; i++)
{
char buf_orig[8192];
char buf_home[8192];
memset(buf_orig, 0, sizeof(buf_orig));
memset(buf_home, 0, sizeof(buf_home));
char *ret_orig = strdup(strs[i]);
char *ret_home = ft_strdup(strs[i]);
if (strcmp(ret_orig, ret_home) == 0 && strcmp(buf_orig, buf_home) == 0)
printf("%s✔%s", CLR_GREEN, RESET);
else
dprintf(2, "\t%s✘%s", CLR_RED, RESET);
putchar(' ');
free(ret_orig);
free(ret_home);
}
putchar('\n');
}
static void _test_write(void)
{
int original, homemade;
int err_original, err_homemade, test_file;
printf("\n%sTesting '%sft_write%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
errno = 0;
original = write(1, NULL, 5);
err_original = errno;
errno = 0;
homemade = ft_write(1, NULL, 5);
err_homemade = errno;
if (original == homemade && err_original == err_homemade)
printf("%s✔%s ", CLR_GREEN, RESET);
else
printf("%s✘%s ", CLR_RED, RESET);
for (int i = 0; strs[i]; i++)
{
errno = 0;
test_file = open("/tmp/rparodi_libasm_write.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
original = write(test_file, strs[i], strlen(strs[i]));
err_original = errno;
errno = 0;
homemade = ft_write(test_file, strs[i], strlen(strs[i]));
err_homemade = errno;
if (original == homemade && err_original == err_homemade)
printf("%s✔%s", CLR_GREEN, RESET);
else
dprintf(2, "\t%s✘%s", CLR_RED, RESET);
close(test_file);
putchar(' ');
}
errno = 0;
original = write(-1, "A", 1);
err_original = errno;
errno = 0;
homemade = ft_write(-1, "A", 1);
err_homemade = errno;
if (original == homemade && err_original == err_homemade)
printf("%s✔%s", CLR_GREEN, RESET);
else
printf("%s✘%s", CLR_RED, RESET);
putchar('\n');
}
static void _test_read(void)
{
int original, homemade;
int err_original, err_homemade;
int test_file;
char buf_orig[8192];
char buf_home[8192];
printf("\n%sTesting '%sft_read%s'%s\n",
CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
errno = 0;
homemade = ft_read(1, NULL, 10);
err_homemade = errno;
printf("%s✔%s ", CLR_GREEN, RESET);
for (int i = 0; strs[i]; i++)
{
test_file = open("/tmp/rparodi_libasm_read.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
write(test_file, strs[i], strlen(strs[i]));
test_file = open("/tmp/rparodi_libasm_read.txt", O_RDONLY);
errno = 0;
original = read(test_file, buf_orig, sizeof(buf_orig));
err_original = errno;
buf_orig[original > 0 ? original : 0] = '\0';
lseek(test_file, 0, SEEK_SET);
errno = 0;
homemade = ft_read(test_file, buf_home, sizeof(buf_home));
err_homemade = errno;
buf_home[homemade > 0 ? homemade : 0] = '\0';
if (original == homemade && err_original == err_homemade && ((original <= 0) || strcmp(buf_orig, buf_home) == 0))
printf("%s✔%s", CLR_GREEN, RESET);
else
dprintf(2, "\t%s✘%s", CLR_RED, RESET);
close(test_file);
putchar(' ');
}
errno = 0;
original = read(-1, buf_orig, 10);
err_original = errno;
errno = 0;
homemade = ft_read(-1, buf_home, 10);
err_homemade = errno;
if (original == homemade && err_original == err_homemade)
printf("%s✔%s", CLR_GREEN, RESET);
else
printf("%s✘%s", CLR_RED, RESET);
putchar('\n');
}
int main(int argc, char *argv[])
{
t_test_func tests[] = {
_test_strlen,
_test_strcmp,
_test_strcpy,
_test_write,
_test_read,
_test_strdup,
NULL
};
char *func_name[] = {
"strlen",
"strcmp",
"strcpy",
"write",
"read",
"strdup",
NULL
};
if (argc < 2)
for (size_t i = 0; tests[i]; i++)
tests[i]();
else
{
for (size_t i = 0; func_name[i]; i++)
{
if (strcmp(func_name[i], argv[1]) == 0)
{
tests[i]();
return (0);
}
}
dprintf(2, "%sError:\n\t%s is not available\nHere's the list of available test:%s\n\n", CLR_RED, argv[0], RESET);
for (size_t i = 0; func_name[i]; i++)
dprintf(2, "\t%s%s%s\n", CLR_RED, func_name[i], RESET);
return (1);
}
return (0);
}