test(read): adding the read function to test

This commit is contained in:
Raphael 2025-12-11 23:28:19 +01:00
parent d93443fb91
commit a03b1bec56
No known key found for this signature in database

View file

@ -108,18 +108,67 @@ static void _test_write(void)
putchar('\n');
}
static void _test_read(void)
{
int original, homemade;
int err_original, err_homemade;
int test_file;
char buf_orig[100];
char buf_home[100];
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);
}
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_write,
_test_read,
NULL
};
char *func_name[] = {
"strlen",
"strcmp",
"write",
"read",
NULL
};