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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#include "libft.h"
#include "test_utils.h"
#include <limits.h>
/* ── helper: capture fd output into a buffer ──────────────────────── */
static int
_s_read_pipe (void (*fn) (int), char *buf, size_t bufsz)
{
int fds[2];
ssize_t n;
if (pipe (fds) < 0)
return (-1);
fn (fds[1]);
close (fds[1]);
n = read (fds[0], buf, bufsz - 1);
close (fds[0]);
if (n < 0)
return (-1);
buf[n] = '\0';
return (n);
}
/* ======================================
* ft_putchar_fd
* ====================================== */
static void
_s_putchar_a (int fd)
{
ft_putchar_fd ('a', fd);
}
static void
_s_putchar_nl (int fd)
{
ft_putchar_fd ('\n', fd);
}
static void
_s_putchar_zero (int fd)
{
ft_putchar_fd ('\0', fd);
}
static void
_s_test_putchar_fd (void)
{
char buf[64];
_s_section ("ft_putchar_fd");
_s_read_pipe (_s_putchar_a, buf, sizeof (buf));
_s_check ("char 'a'", strcmp (buf, "a") == 0);
_s_read_pipe (_s_putchar_nl, buf, sizeof (buf));
_s_check ("newline", strcmp (buf, "\n") == 0);
{
int n = _s_read_pipe (_s_putchar_zero, buf, sizeof (buf));
_s_check ("null byte", n == 1 && buf[0] == '\0');
}
}
/* ======================================
* ft_putstr_fd
* ====================================== */
static void
_s_putstr_hello (int fd)
{
ft_putstr_fd ("hello", fd);
}
static void
_s_putstr_empty (int fd)
{
ft_putstr_fd ("", fd);
}
_S_CRASH_V (ft_putstr_null, ft_putstr_fd (NULL, 1))
static void
_s_test_putstr_fd (void)
{
char buf[256];
_s_section ("ft_putstr_fd");
_s_check ("NULL crashes", _s_crashes (_s_crash_ft_putstr_null));
_s_read_pipe (_s_putstr_hello, buf, sizeof (buf));
_s_check ("hello", strcmp (buf, "hello") == 0);
{
int n = _s_read_pipe (_s_putstr_empty, buf, sizeof (buf));
_s_check ("empty", n == 0);
}
}
/* ======================================
* ft_putendl_fd
* ====================================== */
static void
_s_putendl_hello (int fd)
{
ft_putendl_fd ("hello", fd);
}
static void
_s_putendl_empty (int fd)
{
ft_putendl_fd ("", fd);
}
_S_CRASH_V (ft_putendl_null, ft_putendl_fd (NULL, 1))
static void
_s_test_putendl_fd (void)
{
char buf[256];
_s_section ("ft_putendl_fd");
_s_check ("NULL crashes", _s_crashes (_s_crash_ft_putendl_null));
_s_read_pipe (_s_putendl_hello, buf, sizeof (buf));
_s_check ("hello\\n", strcmp (buf, "hello\n") == 0);
_s_read_pipe (_s_putendl_empty, buf, sizeof (buf));
_s_check ("empty\\n", strcmp (buf, "\n") == 0);
}
/* ======================================
* ft_putnbr_fd
* ====================================== */
static void
_s_putnbr_0 (int fd)
{
ft_putnbr_fd (0, fd);
}
static void
_s_putnbr_42 (int fd)
{
ft_putnbr_fd (42, fd);
}
static void
_s_putnbr_neg (int fd)
{
ft_putnbr_fd (-42, fd);
}
static void
_s_putnbr_max (int fd)
{
ft_putnbr_fd (INT_MAX, fd);
}
static void
_s_putnbr_min (int fd)
{
ft_putnbr_fd (INT_MIN, fd);
}
static void
_s_test_putnbr_fd (void)
{
char buf[64];
char expected[64];
_s_section ("ft_putnbr_fd");
_s_read_pipe (_s_putnbr_0, buf, sizeof (buf));
_s_check ("zero", strcmp (buf, "0") == 0);
_s_read_pipe (_s_putnbr_42, buf, sizeof (buf));
_s_check ("positive", strcmp (buf, "42") == 0);
_s_read_pipe (_s_putnbr_neg, buf, sizeof (buf));
_s_check ("negative", strcmp (buf, "-42") == 0);
_s_read_pipe (_s_putnbr_max, buf, sizeof (buf));
snprintf (expected, sizeof (expected), "%d", INT_MAX);
_s_check ("INT_MAX", strcmp (buf, expected) == 0);
_s_read_pipe (_s_putnbr_min, buf, sizeof (buf));
snprintf (expected, sizeof (expected), "%d", INT_MIN);
_s_check ("INT_MIN", strcmp (buf, expected) == 0);
}
int
main (void)
{
_s_test_putchar_fd ();
_s_test_putstr_fd ();
_s_test_putendl_fd ();
_s_test_putnbr_fd ();
_s_print_results ();
return (_s_fail != 0);
}
|