-
Notifications
You must be signed in to change notification settings - Fork 2
/
header.h
138 lines (121 loc) · 5.44 KB
/
header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* header.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/06 18:13:32 by cacharle #+# #+# */
/* Updated: 2021/01/31 03:03:38 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_TEST_HEADER_H
# define FT_PRINTF_TEST_HEADER_H
# include <sys/select.h>
# include <sys/wait.h>
# include <stdlib.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
# include <stdbool.h>
# include <signal.h>
extern int pid;
extern int pid2;
extern bool signaled;
extern bool origin_signaled ;
extern bool user_signaled ;
extern int origin_ret;
extern int user_ret;
extern char *origin_buf;
extern char *user_buf;
extern struct timeval timeout;
extern fd_set input_set;
extern int timeout_pipe[2];
extern int fd_read_num;
#define PIPE_WRITE 1
#define PIPE_READ 0
// #define PRINTF_NULL_STR "(nil)"
// int printf_wrapper(const char *format, ...);
void generated_test(void);
void saved_test(void);
int ft_printf(const char *format, ...);
void print_buf_ko(char *msg);
void print_ret_ko(char *msg);
void print_signaled_ko(char *msg);
void print_timeouted(char *msg);
void print_ok(void);
void print_ko(void);
void ft_putstr_non_printable(char *str);
void test_setup(void);
void test_tear_down(void);
char *read_stdout_buf(void);
extern char fd_buf[10];
# define TEST_TIMEOUT(x) do { \
if ((pid2 = fork()) == -1) \
exit(EXIT_FAILURE); \
if (pid2 == 0) { \
do { x; } while (0); \
write(timeout_pipe[PIPE_WRITE], "a", 1); \
exit(EXIT_SUCCESS); \
} \
FD_ZERO(&input_set); \
FD_SET(timeout_pipe[PIPE_READ], &input_set); \
fd_read_num = select(FD_SETSIZE, &input_set, NULL, NULL, &timeout); \
if (fd_read_num == -1) \
exit(EXIT_FAILURE); \
if (fd_read_num == 0) \
kill(pid2, 9); \
else { \
wait(&pid2); \
read(timeout_pipe[PIPE_READ], fd_buf, 1); \
} \
} while (0);
# define TEST_SEGFAULT(x) do { \
if ((pid = fork()) < 0) \
exit(EXIT_FAILURE); \
if (pid == 0) { \
do { x } while(0); \
exit(EXIT_SUCCESS); \
} \
wait(&pid); \
signaled = WIFSIGNALED(pid); \
} while(0);
# define TEST_PRINTF(...) do { \
test_setup(); \
TEST_SEGFAULT( \
origin_ret = printf(__VA_ARGS__); \
); \
origin_buf = strdup(read_stdout_buf()); \
origin_signaled = signaled; \
fd_read_num = 1; \
TEST_SEGFAULT( \
TEST_TIMEOUT(user_ret = ft_printf(__VA_ARGS__)); \
); \
user_buf = strdup(read_stdout_buf()); \
user_signaled = signaled; \
test_tear_down(); \
} while (0)
# define ASSERT_PRINTF(...) do { \
TEST_PRINTF(__VA_ARGS__); \
if (fd_read_num == 0) \
print_timeouted(#__VA_ARGS__); \
else \
{ \
if (!origin_signaled && user_signaled) \
print_signaled_ko(#__VA_ARGS__); \
else if (origin_signaled && user_signaled) \
print_ok(); \
else if (!origin_signaled && !user_signaled) { \
if (memcmp(origin_buf, user_buf, \
strlen(origin_buf) + 1) != 0) \
print_buf_ko(#__VA_ARGS__); \
else if (origin_ret != user_ret) \
print_ret_ko(#__VA_ARGS__); \
else \
print_ok(); \
} \
} \
free(origin_buf); \
free(user_buf); \
} while(0);
#endif