From 1e2d7601a6e607052457bf4e9d9e61d05cb8173c Mon Sep 17 00:00:00 2001 From: Khairul Haaziq Date: Sat, 18 Feb 2023 21:27:57 +0800 Subject: [PATCH] add --- mini-moul/tests/C00/ex00/ft_putchar.c | 10 +-- mini-moul/tests/C00/ex04/ft_is_negative.c | 8 +- mini-moul/tests/C00/ex07/ft_putnbr.c | 8 +- mini-moul/tests/C01/ex02/ft_swap.c | 42 +++++++++- mini-moul/tests/C01/ex03/ft_div_mod.c | 52 ++++++++++++- .../tests/C01/ex04/ft_ultimate_div_mod.c | 46 ++++++++++- mini-moul/tests/C01/ex05/ft_putstr.c | 78 +++++++++++++++++-- mini-moul/tests/C04/ex01/ft_putstr.c | 16 +--- mini-moul/tests/C04/ex04/ft_putnbr_base.c | 78 +++++++------------ mini-moul/tests/C04/ex05/ft_atoi_base.c | 19 ++--- 10 files changed, 252 insertions(+), 105 deletions(-) diff --git a/mini-moul/tests/C00/ex00/ft_putchar.c b/mini-moul/tests/C00/ex00/ft_putchar.c index 89461b4..7328514 100644 --- a/mini-moul/tests/C00/ex00/ft_putchar.c +++ b/mini-moul/tests/C00/ex00/ft_putchar.c @@ -47,7 +47,7 @@ int test1(void) return (-1); } else - printf(" " GREEN CHECKMARK GREY " ft_putchar(\'c\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " GREEN CHECKMARK GREY " [1] ft_putchar(\'c\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } @@ -78,12 +78,12 @@ int test2(void) const char *expected_output = "x"; if (strncmp(buffer, expected_output, 1) != 0) { - printf(" " RED "[1] ft_putchar(\'x\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " RED "[2] ft_putchar(\'x\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } else - printf(" " GREEN CHECKMARK GREY " ft_putchar(\'x\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " GREEN CHECKMARK GREY " [2] ft_putchar(\'x\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } @@ -114,12 +114,12 @@ int test3(void) const char *expected_output = "t"; if (strncmp(buffer, expected_output, 1) != 0) { - printf(" " RED "[1] ft_putchar(\'t\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " RED "[3] ft_putchar(\'t\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } else - printf(" " GREEN CHECKMARK GREY " ft_putchar(\'t\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " GREEN CHECKMARK GREY " [3] ft_putchar(\'t\') Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } \ No newline at end of file diff --git a/mini-moul/tests/C00/ex04/ft_is_negative.c b/mini-moul/tests/C00/ex04/ft_is_negative.c index 53eb8e3..934d48e 100644 --- a/mini-moul/tests/C00/ex04/ft_is_negative.c +++ b/mini-moul/tests/C00/ex04/ft_is_negative.c @@ -77,12 +77,12 @@ int test2(void) const char *expected_output = "N"; if (strncmp(buffer, expected_output, 1) != 0) { - printf(" " RED "[1] ft_is_negative(8) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " RED "[2] ft_is_negative(8) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } else - printf(" " GREEN CHECKMARK GREY " [1] ft_is_negative(8) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " GREEN CHECKMARK GREY " [2] ft_is_negative(8) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } @@ -112,12 +112,12 @@ int test3(void) const char *expected_output = "N"; if (strncmp(buffer, expected_output, 1) != 0) { - printf(" " RED "[1] ft_is_negative(-2147483648) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " RED "[3] ft_is_negative(-2147483648) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } else - printf(" " GREEN CHECKMARK GREY " [1] ft_is_negative(-2147483648) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " GREEN CHECKMARK GREY " [3] ft_is_negative(-2147483648) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } \ No newline at end of file diff --git a/mini-moul/tests/C00/ex07/ft_putnbr.c b/mini-moul/tests/C00/ex07/ft_putnbr.c index c789a7d..b8ff413 100644 --- a/mini-moul/tests/C00/ex07/ft_putnbr.c +++ b/mini-moul/tests/C00/ex07/ft_putnbr.c @@ -75,11 +75,11 @@ int test2(void) const char *expected_output = "2008"; if (strncmp(buffer, expected_output, 4) != 0) { - printf(" " RED "[1] ft_putnbr(2008) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " RED "[2] ft_putnbr(2008) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } - else printf(" " GREEN CHECKMARK GREY " [1] ft_putnbr(2008) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + else printf(" " GREEN CHECKMARK GREY " [2] ft_putnbr(2008) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } @@ -109,11 +109,11 @@ int test3(void) const char *expected_output = "-425"; if (strncmp(buffer, expected_output, 4) != 0) { - printf(" " RED "[1] ft_putnbr(-425) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " RED "[3] ft_putnbr(-425) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } - else printf(" " GREEN CHECKMARK GREY " [1] ft_putnbr(-425) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + else printf(" " GREEN CHECKMARK GREY " [3] ft_putnbr(-425) Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } \ No newline at end of file diff --git a/mini-moul/tests/C01/ex02/ft_swap.c b/mini-moul/tests/C01/ex02/ft_swap.c index 6e3b669..eecb9c9 100644 --- a/mini-moul/tests/C01/ex02/ft_swap.c +++ b/mini-moul/tests/C01/ex02/ft_swap.c @@ -6,10 +6,12 @@ #include "../../../utils/constants.h" int test1(void); +int test2(void); +int test3(void); int main(void) { - if (test1() != 0) + if (test1()+test2()+test3() != 0) return (-1); return (0); } @@ -31,4 +33,42 @@ int test1(void) else printf(" " GREEN CHECKMARK GREY " [1] ft_swap(x = 2, y = 3) output x = 3, y = 2\n" DEFAULT); return (0); +} + +int test2(void) +{ + int x; + int y; + + x = 9; + y = -4; + ft_swap(&x, &y); + + if (x != -4 || y != 9) + { + printf(" " RED "[2] ft_swap(x = 9, y = -4) Expected x = -4, got %d. Expected y = 9, got %d\n" DEFAULT, x, y); + return (-1); + } + else + printf(" " GREEN CHECKMARK GREY " [2] ft_swap(x = 9, y = -4) output x = -4, y = 9\n" DEFAULT); + return (0); +} + +int test3(void) +{ + int x; + int y; + + x = -32; + y = 54; + ft_swap(&x, &y); + + if (x != 54 || y != -32) + { + printf(" " RED "[3] ft_swap(x = -32, y = 54) Expected x = 54, got %d. Expected y = -32, got %d\n" DEFAULT, x, y); + return (-1); + } + else + printf(" " GREEN CHECKMARK GREY " [3] ft_swap(x = -32, y = 54) output x = 54, y = -32\n" DEFAULT); + return (0); } \ No newline at end of file diff --git a/mini-moul/tests/C01/ex03/ft_div_mod.c b/mini-moul/tests/C01/ex03/ft_div_mod.c index 77934ac..53deb81 100644 --- a/mini-moul/tests/C01/ex03/ft_div_mod.c +++ b/mini-moul/tests/C01/ex03/ft_div_mod.c @@ -6,10 +6,12 @@ #include "../../../utils/constants.h" int test1(void); +int test2(void); +int test3(void); int main(void) { - if (test1() != 0) + if (test1()+test2()+test3() != 0) return (-1); return (0); } @@ -28,10 +30,54 @@ int test1(void) if (x != 2 || y != 0) { - printf(" " RED "[1] ft_div_mod(10, 5, &x, &y) Expected x = 3, got %d. Expected y = 2, got %d\n" DEFAULT, x, y); + printf(" " RED "[1] ft_div_mod(10, 5, &x, &y) Expected x = 2, got %d. Expected y = 0, got %d\n" DEFAULT, x, y); return (-1); } else - printf(" " GREEN CHECKMARK GREY " [1] ft_div_mod(10, 5, &x, &y) Expected x = 3, got %d. Expected y = 2, got %d\n" DEFAULT, x, y); + printf(" " GREEN CHECKMARK GREY " [1] ft_div_mod(10, 5, &x, &y) Expected x = 2, got %d. Expected y = 0, got %d\n" DEFAULT, x, y); + return (0); +} + +int test2(void) +{ + int a; + int b; + int x; + int y; + + a = 25; + b = 6; + ft_div_mod(a, b, &x, &y); + + + if (x != 4 || y != 1) + { + printf(" " RED "[2] ft_div_mod(25, 6, &x, &y) Expected x = 4, got %d. Expected y = 1, got %d\n" DEFAULT, x, y); + return (-1); + } + else + printf(" " GREEN CHECKMARK GREY " [2] ft_div_mod(25, 6, &x, &y) Expected x = 4, got %d. Expected y = 1, got %d\n" DEFAULT, x, y); + return (0); +} + +int test3(void) +{ + int a; + int b; + int x; + int y; + + a = 47; + b = 8; + ft_div_mod(a, b, &x, &y); + + + if (x != 5 || y != 7) + { + printf(" " RED "[3] ft_div_mod(47, 8, &x, &y) Expected x = 5, got %d. Expected y = 7, got %d\n" DEFAULT, x, y); + return (-1); + } + else + printf(" " GREEN CHECKMARK GREY " [3] ft_div_mod(47, 8, &x, &y) Expected x = 5, got %d. Expected y = 7, got %d\n" DEFAULT, x, y); return (0); } \ No newline at end of file diff --git a/mini-moul/tests/C01/ex04/ft_ultimate_div_mod.c b/mini-moul/tests/C01/ex04/ft_ultimate_div_mod.c index 1fbc679..df538b7 100644 --- a/mini-moul/tests/C01/ex04/ft_ultimate_div_mod.c +++ b/mini-moul/tests/C01/ex04/ft_ultimate_div_mod.c @@ -6,10 +6,12 @@ #include "../../../utils/constants.h" int test1(void); +int test2(void); +int test3(void); int main(void) { - if (test1() != 0) + if (test1()+test2()+test3() != 0) return (-1); return (0); } @@ -25,10 +27,48 @@ int test1(void) if (x != 5 || y != 0) { - printf(" " RED "[1] ft_ultimate_div_mod(x = 15, y = 3) Expected x = 3, got %d. Expected y = 2, got %d\n" DEFAULT, x, y); + printf(" " RED "[1] ft_ultimate_div_mod(x = 15, y = 3) Expected x = 5, got %d. Expected y = 0, got %d\n" DEFAULT, x, y); return (-1); } else - printf(" " GREEN CHECKMARK GREY " [1] ft_ultimate_div_mod(x = 15, y = 3) Expected x = 3, got %d. Expected y = 2, got %d\n" DEFAULT, x, y); + printf(" " GREEN CHECKMARK GREY " [1] ft_ultimate_div_mod(x = 15, y = 3) Expected x = 5, got %d. Expected y = 0, got %d\n" DEFAULT, x, y); + return (0); +} + +int test2(void) +{ + int x; + int y; + + x = 23; + y = 5; + ft_ultimate_div_mod(&x, &y); + + if (x != 4 || y != 3) + { + printf(" " RED "[2] ft_ultimate_div_mod(x = 23, y = 5) Expected x = 4, got %d. Expected y = 3, got %d\n" DEFAULT, x, y); + return (-1); + } + else + printf(" " GREEN CHECKMARK GREY " [2] ft_ultimate_div_mod(x = 23, y = 5) Expected x = 4, got %d. Expected y = 3, got %d\n" DEFAULT, x, y); + return (0); +} + +int test3(void) +{ + int x; + int y; + + x = 32; + y = 5; + ft_ultimate_div_mod(&x, &y); + + if (x != 6 || y != 2) + { + printf(" " RED "[3] ft_ultimate_div_mod(x = 32, y = 5) Expected x = 6, got %d. Expected y = 2, got %d\n" DEFAULT, x, y); + return (-1); + } + else + printf(" " GREEN CHECKMARK GREY " [3] ft_ultimate_div_mod(x = 32, y = 5) Expected x = 6, got %d. Expected y = 2, got %d\n" DEFAULT, x, y); return (0); } \ No newline at end of file diff --git a/mini-moul/tests/C01/ex05/ft_putstr.c b/mini-moul/tests/C01/ex05/ft_putstr.c index 8ca3607..7698c9e 100644 --- a/mini-moul/tests/C01/ex05/ft_putstr.c +++ b/mini-moul/tests/C01/ex05/ft_putstr.c @@ -5,13 +5,13 @@ #include "../../../../ex05/ft_putstr.c" #include "../../../utils/constants.h" -void print_success(char *str, int num); -void print_error(char *str, int num); int test1(void); +int test2(void); +int test3(void); int main(void) { - if (test1() != 0) + if (test1()+test2()+test3() != 0) return (-1); return (0); } @@ -40,7 +40,7 @@ int test1(void) // Check that the output matches the expected value const char *expected_output = "hallo world"; - if (strcmp(buffer, expected_output) != 0) + if (strncmp(buffer, expected_output, 11) != 0) { printf(" " RED "[1] ft_putstr(\"hallo world\") Expected \"%s\", got \"%s\"\n", expected_output, buffer); remove("output.txt"); @@ -52,12 +52,74 @@ int test1(void) return (0); } -void print_success(char *str, int num) +int test2(void) { - printf(" " GREEN CHECKMARK GREY " [%d] %s\n" DEFAULT, num, str); + // Redirect the output to a file + int saved_stdout = dup(STDOUT_FILENO); + int output_fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + dup2(output_fd, STDOUT_FILENO); + close(output_fd); + + // Call the function to be tested + ft_putstr("yosfjasnfoafoans"); + + // Restore the original output + fflush(stdout); + dup2(saved_stdout, STDOUT_FILENO); + close(saved_stdout); + + // Open the output file and check its contents + FILE *fp = fopen("output.txt", "r"); + char buffer[1024]; + fgets(buffer, sizeof(buffer), fp); + fclose(fp); + + // Check that the output matches the expected value + const char *expected_output = "yosfjasnfoafoans"; + if (strncmp(buffer, expected_output, 16) != 0) + { + printf(" " RED "[2] ft_putstr(\"yosfjasnfoafoans\") Expected \"%s\", got \"%s\"\n", expected_output, buffer); + remove("output.txt"); + return (-1); + } + else + printf(" " GREEN CHECKMARK GREY " [2] ft_putstr(\"yosfjasnfoafoans\") Expected \"%s\", got \"%s\"\n", expected_output, buffer); + remove("output.txt"); + return (0); } -void print_error(char *str, int num) +int test3(void) { - printf(" " RED "[%d] %s\n" DEFAULT, num, str); + // Redirect the output to a file + int saved_stdout = dup(STDOUT_FILENO); + int output_fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + dup2(output_fd, STDOUT_FILENO); + close(output_fd); + + // Call the function to be tested + ft_putstr(" ---wnfiw=-0=1e093"); + + // Restore the original output + fflush(stdout); + dup2(saved_stdout, STDOUT_FILENO); + close(saved_stdout); + + // Open the output file and check its contents + FILE *fp = fopen("output.txt", "r"); + char buffer[1024]; + fgets(buffer, sizeof(buffer), fp); + fclose(fp); + + // Check that the output matches the expected value + const char *expected_output = " ---wnfiw=-0=1e093"; + if (strncmp(buffer, expected_output, 20) != 0) + { + printf(" " RED "[3] ft_putstr(\" ---wnfiw=-0=1e093\") Expected \"%s\", got \"%s\"\n", expected_output, buffer); + remove("output.txt"); + return (-1); + } + else + printf(" " GREEN CHECKMARK GREY " [3] ft_putstr(\" ---wnfiw=-0=1e093\") Expected \"%s\", got \"%s\"\n", expected_output, buffer); + remove("output.txt"); + return (0); } \ No newline at end of file diff --git a/mini-moul/tests/C04/ex01/ft_putstr.c b/mini-moul/tests/C04/ex01/ft_putstr.c index 2d9e9d6..1007169 100644 --- a/mini-moul/tests/C04/ex01/ft_putstr.c +++ b/mini-moul/tests/C04/ex01/ft_putstr.c @@ -5,8 +5,6 @@ #include "../../../../ex01/ft_putstr.c" #include "../../../utils/constants.h" -void print_success(char *str, int num); -void print_error(char *str, int num); int test1(void); int main(void) @@ -42,22 +40,12 @@ int test1(void) const char *expected_output = "hallo world"; if (strcmp(buffer, expected_output) != 0) { - printf(" " RED "[1] Expected \"%s\", got \"%s\"\n", expected_output, buffer); + printf(" " RED "[1] ft_putstr(\"hallo world\") Expected \"%s\", got \"%s\"\n", expected_output, buffer); remove("output.txt"); return (-1); } else - printf(" " GREEN CHECKMARK GREY " [1] ft_putstr() output \"%s\"\n" DEFAULT, buffer); + printf(" " GREEN CHECKMARK GREY " [1] ft_putstr(\"hallo world\") Expected \"%s\", got \"%s\"\n" DEFAULT, expected_output, buffer); remove("output.txt"); return (0); -} - -void print_success(char *str, int num) -{ - printf(" " GREEN CHECKMARK GREY " [%d] %s\n" DEFAULT, num, str); -} - -void print_error(char *str, int num) -{ - printf(" " RED "[%d] %s\n" DEFAULT, num, str); } \ No newline at end of file diff --git a/mini-moul/tests/C04/ex04/ft_putnbr_base.c b/mini-moul/tests/C04/ex04/ft_putnbr_base.c index e2eb7e0..70f16c4 100644 --- a/mini-moul/tests/C04/ex04/ft_putnbr_base.c +++ b/mini-moul/tests/C04/ex04/ft_putnbr_base.c @@ -5,13 +5,9 @@ #include "../../../../ex04/ft_putnbr_base.c" #include "../../../utils/constants.h" -void redirect_output(void); int test1(void); -void redirect_output2(void); int test2(void); -void redirect_output3(void); int test3(void); -void redirect_output4(void); int test4(void); int main(void) @@ -23,7 +19,19 @@ int main(void) int test1(void) { - redirect_output(); + // Redirect the output to a file + int saved_stdout = dup(STDOUT_FILENO); + int output_fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + dup2(output_fd, STDOUT_FILENO); + close(output_fd); + + // Call the function to be tested + ft_putnbr_base(-12, "01"); + + // Restore the original output + fflush(stdout); + dup2(saved_stdout, STDOUT_FILENO); + close(saved_stdout); // Open the output file and check its contents FILE *fp = fopen("output.txt", "r"); char buffer[80024]; @@ -34,16 +42,16 @@ int test1(void) const char *expected_output = "-1100"; if (strcmp(buffer, expected_output) != 0) { - printf(" " RED "[1] Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + printf(" " RED "[1] ft_putnbr_base(-12, \"01\") Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } - else printf(" " GREEN CHECKMARK GREY " [1] Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + else printf(" " GREEN CHECKMARK GREY " [1] ft_putnbr_base(-12, \"01\") Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } -void redirect_output(void) +int test2(void) { // Redirect the output to a file int saved_stdout = dup(STDOUT_FILENO); @@ -52,17 +60,12 @@ void redirect_output(void) close(output_fd); // Call the function to be tested - ft_putnbr_base(-12, "01"); + ft_putnbr_base(40, "poneyvif"); // Restore the original output fflush(stdout); dup2(saved_stdout, STDOUT_FILENO); close(saved_stdout); -} - -int test2(void) -{ - redirect_output2(); // Open the output file and check its contents FILE *fp = fopen("output.txt", "r"); char buffer[80024]; @@ -73,16 +76,16 @@ int test2(void) const char *expected_output = "vp\0"; if (strncmp(buffer, expected_output, 2) != 0) { - printf(" " RED "[2] Expected \"%s\", got \"%s\"\n", expected_output, buffer); + printf(" " RED "[2] ft_putnbr_base(40, \"poneyvif\") Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } - else printf(" " GREEN CHECKMARK GREY " [2] Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + else printf(" " GREEN CHECKMARK GREY " [2] ft_putnbr_base(40, \"poneyvif\") Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } -void redirect_output2(void) +int test3(void) { // Redirect the output to a file int saved_stdout = dup(STDOUT_FILENO); @@ -91,17 +94,12 @@ void redirect_output2(void) close(output_fd); // Call the function to be tested - ft_putnbr_base(40, "poneyvif"); + ft_putnbr_base(894867, "0123456789"); // Restore the original output fflush(stdout); dup2(saved_stdout, STDOUT_FILENO); close(saved_stdout); -} - -int test3(void) -{ - redirect_output3(); // Open the output file and check its contents FILE *fp = fopen("output.txt", "r"); char buffer[80024]; @@ -112,16 +110,16 @@ int test3(void) const char *expected_output = "894867"; if (strncmp(buffer, expected_output, 6) != 0) { - printf(" " RED "[3] Expected \"%s\", got \"%s\"\n", expected_output, buffer); + printf(" " RED "[3] ft_putnbr_base(894867, \"0123456789\") Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } - else printf(" " GREEN CHECKMARK GREY " [3] Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); + else printf(" " GREEN CHECKMARK GREY " [3] ft_putnbr_base(894867, \"0123456789\") Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); } -void redirect_output3(void) +int test4(void) { // Redirect the output to a file int saved_stdout = dup(STDOUT_FILENO); @@ -130,17 +128,12 @@ void redirect_output3(void) close(output_fd); // Call the function to be tested - ft_putnbr_base(894867, "0123456789"); + ft_putnbr_base(53, "0123456789abcdef"); // Restore the original output fflush(stdout); dup2(saved_stdout, STDOUT_FILENO); close(saved_stdout); -} - -int test4(void) -{ - redirect_output4(); // Open the output file and check its contents FILE *fp = fopen("output.txt", "r"); char buffer[80024]; @@ -151,28 +144,11 @@ int test4(void) const char *expected_output = "35"; if (strncmp(buffer, expected_output, 2) != 0) { - printf(" " RED "[1] Expected \"%s\", got \"%s\"\n", expected_output, buffer); + printf(" " RED "[4] ft_putnbr_base(53, \"0123456789abcdef\") Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (-1); } - else printf(" " GREEN CHECKMARK GREY " [1] ft_putnbr(-2147483648) output \"%s\"\n" DEFAULT, buffer); + else printf(" " GREEN CHECKMARK GREY " [4] ft_putnbr_base(53, \"0123456789abcdef\") Expected \"%s\", got \"%s\"\n"DEFAULT, expected_output, buffer); remove("output.txt"); return (0); -} - -void redirect_output4(void) -{ - // Redirect the output to a file - int saved_stdout = dup(STDOUT_FILENO); - int output_fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - dup2(output_fd, STDOUT_FILENO); - close(output_fd); - - // Call the function to be tested - ft_putnbr_base(53, "0123456789abcdef"); - - // Restore the original output - fflush(stdout); - dup2(saved_stdout, STDOUT_FILENO); - close(saved_stdout); } \ No newline at end of file diff --git a/mini-moul/tests/C04/ex05/ft_atoi_base.c b/mini-moul/tests/C04/ex05/ft_atoi_base.c index a63e61e..076f893 100644 --- a/mini-moul/tests/C04/ex05/ft_atoi_base.c +++ b/mini-moul/tests/C04/ex05/ft_atoi_base.c @@ -2,12 +2,7 @@ #include #include #include "../../../../ex05/ft_atoi_base.c" - -#define GREEN "\033[38;5;84m" -#define RED "\033[31m" -#define GREY "\033[38;5;8m" -#define DEFAULT "\033[0m" -#define CHECKMARK "\xE2\x9C\x93" +#include "../../../utils/constants.h" int test1(void); int test2(void); @@ -29,11 +24,11 @@ int test1(void) expected_output = -14353; if (result != expected_output) { - printf(" " RED "[1] Expected %d, got %d\n"DEFAULT, expected_output, result); + printf(" " RED "[1] ft_atoi_base(\" +-14353\", \"0123456789\") Expected %d, got %d\n"DEFAULT, expected_output, result); return (-1); } else - printf(" " GREEN CHECKMARK GREY " [1] Expected %d, got %d\n"DEFAULT, expected_output, result); + printf(" " GREEN CHECKMARK GREY " [1] ft_atoi_base(\" +-14353\", \"0123456789\") Expected %d, got %d\n"DEFAULT, expected_output, result); return (0); } @@ -46,11 +41,11 @@ int test2(void) expected_output = -169; if (result != expected_output) { - printf(" " RED "[2] Expected %d, got %d\n"DEFAULT, expected_output, result); + printf(" " RED "[2] ft_atoi_base(\" ---10101001\", \"01\") Expected %d, got %d\n"DEFAULT, expected_output, result); return (-1); } else - printf(" " GREEN CHECKMARK GREY " [2] Expected %d, got %d\n"DEFAULT, expected_output, result); + printf(" " GREEN CHECKMARK GREY " [2] ft_atoi_base(\" ---10101001\", \"01\") Expected %d, got %d\n"DEFAULT, expected_output, result); return (0); } @@ -63,10 +58,10 @@ int test3(void) expected_output = -89; if (result != expected_output) { - printf(" " RED "[3] Expected %d, got %d\n"DEFAULT, expected_output, result); + printf(" " RED "[3] ft_atoi_base(\" +---59\", \"0123456789abcdef\") Expected %d, got %d\n"DEFAULT, expected_output, result); return (-1); } else - printf(" " GREEN CHECKMARK GREY " [3] Expected %d, got %d\n"DEFAULT, expected_output, result); + printf(" " GREEN CHECKMARK GREY " [3] ft_atoi_base(\" +---59\", \"0123456789abcdef\") Expected %d, got %d\n"DEFAULT, expected_output, result); return (0); }