From a03b1bec56cb1aefda26ed032f84c9c007b3cf33 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 11 Dec 2025 23:28:19 +0100 Subject: [PATCH] test(read): adding the read function to test --- test/main.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/main.c b/test/main.c index 8adccce..3eb0adf 100644 --- a/test/main.c +++ b/test/main.c @@ -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 };