From fb41e23423854a865201c0803803191d1f65c8fa Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 2 Nov 2019 01:27:45 +0100 Subject: Fixed false positive, basic prettier, more test from pft --- helper.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 6 deletions(-) (limited to 'helper.c') diff --git a/helper.c b/helper.c index 1c16409..4b2a7ec 100644 --- a/helper.c +++ b/helper.c @@ -1,27 +1,78 @@ #include +#include #include "header.h" +/* #define BUF_SIZE (1 << 10) */ + +int pipefd[2]; +int saved_stdout = -1; +char buf[BUF_SIZE + 1] = {0}; + +void test_setup(void) +{ + if (saved_stdout != -1) + { + dup2(saved_stdout, STDOUT_FILENO); + close(saved_stdout); + saved_stdout = -1; + close(pipefd[0]); + close(pipefd[1]); + } + if (pipe(pipefd) != 0) + exit(EXIT_FAILURE); + saved_stdout = dup(STDOUT_FILENO); + dup2(pipefd[1], STDOUT_FILENO); +} + +void test_tear_down(void) +{ + if (saved_stdout != -1) + { + dup2(saved_stdout, STDOUT_FILENO); + close(saved_stdout); + saved_stdout = -1; + close(pipefd[0]); + close(pipefd[1]); + } +} + +#include +char *read_stdout_buf(void) +{ + fflush(stdout); + long flags = fcntl(pipefd[0], F_GETFL); + flags |= O_NONBLOCK; + fcntl(pipefd[0], F_SETFL, flags); + int ret = read(pipefd[0], buf, BUF_SIZE); + buf[ret] = '\0'; + fflush(stdout); + return (buf); +} + void print_buf_ko(char *msg) { print_ko(); printf("ft_printf(%s) has wrong output\n", msg); - printf("actual: \"%s\"", origin_buf); - printf("expected: \"%s\"", user_buf); + printf("actual: \"%s\"\n", user_buf); + printf("expected: \"%s\"\n", origin_buf); + fflush(stdout); } void print_ret_ko(char *msg) { print_ko(); printf("ft_printf(%s) has wrong return value\n", msg); - printf("actual: %d", origin_ret); - printf("expected: %d", user_ret); + printf("actual: %d\n", user_ret); + printf("expected: %d\n", origin_ret); + fflush(stdout); } void print_signaled_ko(char *msg) { print_ko(); printf("ft_printf(\"%s\") has been signaled (segfault and friends)\n", msg); + fflush(stdout); } -inline void print_ok(void) { printf("\033[32m[OK]\033[0m\n"); } -inline void print_ko(void) { printf("\033[31m[KO]\033[0m "); } +inline void print_ok(void) { printf("[OK]\n"); fflush(stdout); } +inline void print_ko(void) { printf("[KO] "); fflush(stdout);} -- cgit