Skip to content

Commit 86ee1c1

Browse files
committedMay 1, 2020
errno checking example
1 parent 4388cc2 commit 86ee1c1

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed
 

‎libasm_test.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2020/02/08 03:07:19 by cacharle #+# #+# */
9-
/* Updated: 2020/04/13 14:41:40 by charles ### ########.fr */
9+
/* Updated: 2020/05/01 10:06:45 by charles ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -22,6 +22,7 @@
2222
# include <sys/wait.h>
2323
# include <limits.h>
2424
# include <stddef.h>
25+
# include <errno.h>
2526

2627
# define TO_STRING(x) #x
2728

‎test/ft_read_test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2020/02/08 03:07:44 by cacharle #+# #+# */
9-
/* Updated: 2020/04/13 14:52:41 by charles ### ########.fr */
9+
/* Updated: 2020/05/01 10:10:10 by charles ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -70,7 +70,7 @@ void ft_read_test_compare(void)
7070
FT_READ_EXPECT("");
7171
FT_READ_EXPECT("bon");
7272
FT_READ_EXPECT("bonjour");
73-
FT_READ_EXPECT("%c%s%p%x%X%e%f%g");
73+
FT_READ_EXPECT("#c#s#p#x#X#e#f#g");
7474
FT_READ_EXPECT("the\0hidden");
7575
FT_READ_EXPECT("Lorem ipsum dolor sit amet, consectetur adipiscing\
7676
elit. Sed in malesuada purus. Etiam a scelerisque massa. Ut non euismod elit. Aliquam\

‎test/ft_write_test.c

+23-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2020/02/08 03:07:48 by cacharle #+# #+# */
9-
/* Updated: 2020/04/13 14:51:39 by charles ### ########.fr */
9+
/* Updated: 2020/05/01 10:09:41 by charles ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -17,16 +17,28 @@
1717
static int ft_write_pipe[2];
1818
static char buf[FT_WRITE_BUF_SIZE] = {0};
1919
static unsigned long write_ret;
20+
static unsigned long write_origin_ret;
21+
static unsigned long write_errno;
22+
static unsigned long write_origin_errno;
2023
static int ret;
2124

2225
#define FT_WRITE_EXPECT(str) do { \
2326
if (pipe(ft_write_pipe) < 0) \
2427
exit(EXIT_FAILURE); \
2528
fcntl(ft_write_pipe[0], F_SETFL, O_NONBLOCK); \
29+
write_origin_errno = 0; \
30+
write_origin_ret = write(ft_write_pipe[1], str, strlen(str)); \
31+
write_origin_errno = errno; \
32+
ret = read(ft_write_pipe[0], buf, FT_WRITE_BUF_SIZE); \
33+
write_errno = 0; \
2634
write_ret = ft_write(ft_write_pipe[1], str, strlen(str)); \
35+
write_errno = errno; \
2736
ret = read(ft_write_pipe[0], buf, FT_WRITE_BUF_SIZE); \
2837
buf[ret] = '\0'; \
29-
if (strcmp(buf, str) != 0 || write_ret != strlen(str)) \
38+
if (write_errno != write_origin_errno) \
39+
printf("KO: [COMPARE]: %s: expected: errno %ld got: errno %ld with: "#str"\n", \
40+
test_name, write_origin_errno, write_errno); \
41+
else if (strcmp(buf, str) != 0 || write_ret != strlen(str)) \
3042
printf("KO: [COMPARE]: %s: expected: %lu \"%s\" got: %lu \"%s\" with: %d, \"%s\", %zu \n", \
3143
test_name, strlen(str), str, write_ret, buf, ft_write_pipe[0], buf, strlen(str)); \
3244
else \
@@ -36,10 +48,18 @@ static int ret;
3648
} while (0);
3749

3850
#define FT_WRITE_EXPECT_ERROR(fd, str, size) do { \
51+
write_errno = 0; \
3952
write_ret = ft_write(fd, str, size); \
53+
write_errno = errno; \
54+
write_origin_errno = 0; \
55+
write_origin_ret = write(fd, str, size); \
56+
write_origin_errno = errno; \
4057
if ((long)write_ret != -1) \
4158
printf("KO: [COMPARE]: %s: expected: %ld got: %ld with: %d "#str", %d\n", \
4259
test_name, -1l, (long)write_ret, fd, size); \
60+
else if (write_errno != write_origin_errno) \
61+
printf("KO: [COMPARE]: %s: expected: errno %ld got: errno %ld with: %d "#str", %d\n", \
62+
test_name, write_origin_errno, write_errno, fd, size); \
4363
else \
4464
print_ok(); \
4565
} while (0);
@@ -71,7 +91,7 @@ void ft_write_test_compare(void)
7191
FT_WRITE_EXPECT("");
7292
FT_WRITE_EXPECT("bon");
7393
FT_WRITE_EXPECT("bonjour");
74-
FT_WRITE_EXPECT("%c%s%p%x%X%e%f%g");
94+
FT_WRITE_EXPECT("#c#s#p#x#X#e#f#g");
7595
FT_WRITE_EXPECT("the\0hidden");
7696
FT_WRITE_EXPECT("Lorem ipsum dolor sit amet, consectetur adipiscing\
7797
elit. Sed in malesuada purus. Etiam a scelerisque massa. Ut non euismod elit. Aliquam\

0 commit comments

Comments
 (0)