test(write): adding write test
This commit is contained in:
parent
923dc8a6ca
commit
90b1ca1289
1 changed files with 75 additions and 12 deletions
87
test/main.c
87
test/main.c
|
|
@ -1,7 +1,10 @@
|
||||||
#include "libasm.h"
|
#include "libasm.h"
|
||||||
|
#include <errno.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef void (*t_test_func)(void);
|
typedef void (*t_test_func)(void);
|
||||||
|
|
@ -20,31 +23,89 @@ char *strs[] = {
|
||||||
|
|
||||||
static void _test_strlen()
|
static void _test_strlen()
|
||||||
{
|
{
|
||||||
printf("\n\n\n%sTesting '%sstrlen%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
printf("\n%sTesting '%sstrlen%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
||||||
printf("%sTesting on: '%sNULL%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
ft_strlen(NULL);
|
||||||
printf("%sHomeMade: %s%zu%s\n\n\n", CLR_YELLOW, CLR_BLUE, ft_strlen(NULL), RESET);
|
printf("%s✔%s ", CLR_GREEN, RESET);
|
||||||
for (int i = 0; strs[i]; i++)
|
for (int i = 0; strs[i]; i++)
|
||||||
{
|
{
|
||||||
printf("%sTesting on: '%s%s%s'%s\n", CLR_YELLOW, CLR_BLUE, strs[i], CLR_YELLOW, RESET);
|
size_t original = strlen(strs[i]);
|
||||||
printf("\n%sOriginal: %s%zu%s\n", CLR_YELLOW, CLR_BLUE, strlen(strs[i]), RESET);
|
size_t homemade = ft_strlen(strs[i]);
|
||||||
printf("%sHomeMade: %s%zu%s\n\n\n", CLR_YELLOW, CLR_BLUE, ft_strlen(strs[i]), RESET);
|
if (original == homemade)
|
||||||
|
printf("%s✔%s ", CLR_GREEN, RESET);
|
||||||
|
else
|
||||||
|
dprintf(2, "\t%s✘%s ", CLR_RED, RESET);
|
||||||
}
|
}
|
||||||
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _test_strcmp()
|
static void _test_strcmp()
|
||||||
{
|
{
|
||||||
printf("\n\n\n%sTesting '%sstrcmp%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
printf("\n%sTesting '%sstrcmp%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
||||||
printf("%sTesting on: '%sNULL%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
ft_strcmp(NULL, "a");
|
||||||
printf("%sHomeMade: %s%i%s\n\n\n", CLR_YELLOW, CLR_BLUE, ft_strcmp(NULL, ""), RESET);
|
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 i = 0; strs[i]; i++)
|
||||||
{
|
{
|
||||||
for (int j = i; strs[i] && strs[j]; j++)
|
for (int j = i; strs[i] && strs[j]; j++)
|
||||||
{
|
{
|
||||||
printf("%sTesting on: '%s%s%s'/'%s%s%s'%s\n", CLR_YELLOW, CLR_BLUE, strs[i], CLR_YELLOW, CLR_BLUE, strs[j], CLR_YELLOW, RESET);
|
size_t original = strcmp(strs[i], strs[j]);
|
||||||
printf("\n%sOriginal: %s%i%s\n", CLR_YELLOW, CLR_BLUE, strcmp(strs[i], strs[j]), RESET);
|
size_t homemade = ft_strcmp(strs[i], strs[j]);
|
||||||
printf("%sHomeMade: %s%i%s\n\n\n", CLR_YELLOW, CLR_BLUE, ft_strcmp(strs[i], strs[j]), RESET);
|
if (original == homemade)
|
||||||
|
printf("%s✔%s ", CLR_GREEN, RESET);
|
||||||
|
else
|
||||||
|
dprintf(2, "\t%s✘%s ", CLR_RED, RESET);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
|
@ -52,11 +113,13 @@ int main(int argc, char *argv[])
|
||||||
t_test_func tests[] = {
|
t_test_func tests[] = {
|
||||||
_test_strlen,
|
_test_strlen,
|
||||||
_test_strcmp,
|
_test_strcmp,
|
||||||
|
_test_write,
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
char *func_name[] = {
|
char *func_name[] = {
|
||||||
"strlen",
|
"strlen",
|
||||||
"strcmp",
|
"strcmp",
|
||||||
|
"write",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue