aboutsummaryrefslogtreecommitdiff
path: root/helper.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-11-02 01:27:45 +0100
committerCharles <sircharlesaze@gmail.com>2019-11-02 01:27:45 +0100
commitfb41e23423854a865201c0803803191d1f65c8fa (patch)
tree515ba48b4bc97f1e7b550e46a4dbff6e5de12a92 /helper.c
parent762ff2fcaf45d6b94fb580e168baf3f01ebd61e1 (diff)
downloadft_printf_test-fb41e23423854a865201c0803803191d1f65c8fa.tar.gz
ft_printf_test-fb41e23423854a865201c0803803191d1f65c8fa.tar.bz2
ft_printf_test-fb41e23423854a865201c0803803191d1f65c8fa.zip
Fixed false positive, basic prettier, more test from pft
Diffstat (limited to 'helper.c')
-rw-r--r--helper.c63
1 files changed, 57 insertions, 6 deletions
diff --git a/helper.c b/helper.c
index 1c16409..4b2a7ec 100644
--- a/helper.c
+++ b/helper.c
@@ -1,27 +1,78 @@
#include <stdio.h>
+#include <unistd.h>
#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 <fcntl.h>
+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);}