diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-05-01 10:11:13 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-05-04 21:05:22 +0200 |
| commit | 99a4f8397a9652b15d171ecb86e7281b80ed57d7 (patch) | |
| tree | dd303233795aa1cdddae44555bad941fada70762 /test/ft_read_test.c | |
| parent | 4388cc2d74f1ee8930ca1baaeeea67da925f8a2c (diff) | |
| download | libasm_test-errno.tar.gz libasm_test-errno.tar.bz2 libasm_test-errno.zip | |
Adding errno checking according to new subject (#2)errno
prettier compatible with python < 3.6
removed unaccurate test for ft_read
Diffstat (limited to 'test/ft_read_test.c')
| -rw-r--r-- | test/ft_read_test.c | 78 |
1 files changed, 47 insertions, 31 deletions
diff --git a/test/ft_read_test.c b/test/ft_read_test.c index c175489..f061c5f 100644 --- a/test/ft_read_test.c +++ b/test/ft_read_test.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 03:07:44 by cacharle #+# #+# */ -/* Updated: 2020/04/13 14:52:41 by charles ### ########.fr */ +/* Updated: 2020/05/04 16:09:34 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,52 +16,63 @@ static int ft_read_pipe[2]; static char buf[FT_READ_BUF_SIZE] = {0}; -static int ret; +static ssize_t read_ret; +static ssize_t read_origin_ret; +static int read_errno; +static int read_origin_errno; #define FT_READ_EXPECT(str) do { \ if (pipe(ft_read_pipe) < 0) \ exit(EXIT_FAILURE); \ fcntl(ft_read_pipe[0], F_SETFL, O_NONBLOCK); \ write(ft_read_pipe[1], str, strlen(str)); \ - ret = ft_read(ft_read_pipe[0], buf, strlen(str)); \ - buf[ret] = '\0'; \ - if (strcmp(buf, str) != 0 || ret != strlen(str)) \ - printf("KO: [COMPARE]: %s: expected: %lu \"%s\" got: %d \"%s\" with: %d, \"%s\", %zu \n", \ - test_name, strlen(str), str, ret, buf, ft_read_pipe[0], buf, strlen(str)); \ + ERRNO_WRAP(read_origin_ret = read(ft_read_pipe[0], buf, strlen(str)), read_origin_errno); \ + write(ft_read_pipe[1], str, strlen(str)); \ + ERRNO_WRAP(read_ret = ft_read(ft_read_pipe[0], buf, strlen(str)), read_errno); \ + buf[read_ret] = '\0'; \ + if (read_errno != read_origin_errno) \ + printf("KO: [COMPARE]: %s: expected: errno %d got: errno %d with: "#str"\n", \ + test_name, read_origin_errno, read_errno); \ + else if (strcmp(buf, str) != 0 || read_ret != read_origin_ret) \ + printf("KO: [COMPARE]: %s: expected: %lu \"%s\" got: %lu \"%s\" with: %d, \"%s\", %zu \n", \ + test_name, strlen(str), str, read_ret, buf, ft_read_pipe[0], buf, read_origin_ret); \ else \ print_ok(); \ close(ft_read_pipe[1]); \ close(ft_read_pipe[0]); \ } while (0); -#define FT_READ_EXPECT_ERROR(fd, str, size) do { \ - ret = ft_read(fd, str, size); \ - if ((long)ret != -1) \ - printf("KO: [COMPARE]: %s: expected: %ld got: %ld with: %d "#str" %d\n", \ - test_name, -1l, (long)ret, fd, size); \ - else \ - print_ok(); \ +#define FT_READ_EXPECT_ERROR(fd, buf, size) do { \ + ERRNO_WRAP(read_ret = ft_read(fd, buf, size), read_errno); \ + ERRNO_WRAP(read_origin_ret = read(fd, buf, size), read_origin_errno); \ + if ((long)read_ret != -1) \ + printf("KO: [COMPARE]: %s: expected: %ld got: %ld with: %d "#buf" %d\n", \ + test_name, -1l, (long)read_ret, fd, size); \ + else if (read_errno != read_origin_errno) \ + printf("KO: [COMPARE]: %s: expected: errno %d got: errno %d with: %d "#buf", %d\n", \ + test_name, read_origin_errno, read_errno, fd, size); \ + else \ + print_ok(); \ } while (0); static void ft_read_test_segfault(void) { int tmp[2]; + char buf_read[2048]; + if (pipe(tmp) < 0) exit(EXIT_FAILURE); - TEST_ASM_FUNCTION(ft_read(-1, "test", 5)); - TEST_ASM_FUNCTION(ft_read(tmp[1], NULL, 5)); - TEST_ASM_FUNCTION(ft_read(tmp[1], "test", 0)); - TEST_ASM_FUNCTION(ft_read(tmp[1], "test", 5)); - TEST_ASM_FUNCTION(ft_read(tmp[1], "t", 1)); - TEST_ASM_FUNCTION(ft_read(tmp[1], "", 0)); - TEST_ASM_FUNCTION(ft_read(tmp[1], "test", 4)); - TEST_ASM_FUNCTION(ft_read(tmp[1], "test", 2)); - TEST_ASM_FUNCTION(ft_read(tmp[1], NULL, 2)); + write(tmp[1], "asdfkasdfkl;jasd;ljkfa;lssdlfasdfasdfasdfasdfasdfasdfasdfasdfasdf", 40); + TEST_ASM_FUNCTION(ft_read(-1, buf_read, 2)); + TEST_ASM_FUNCTION(ft_read(tmp[0], NULL, 2)); + TEST_ASM_FUNCTION(ft_read(tmp[0], buf_read, 0)); + TEST_ASM_FUNCTION(ft_read(tmp[0], buf_read, 5)); + TEST_ASM_FUNCTION(ft_read(tmp[0], buf_read, 32)); close(tmp[0]); close(tmp[1]); - TEST_ASM_FUNCTION(ft_read(-1, "tt", 2)); - TEST_ASM_FUNCTION(ft_read(OPEN_MAX + 1, "tt", 2)); + TEST_ASM_FUNCTION(ft_read(-1, buf_read, 2)); + TEST_ASM_FUNCTION(ft_read(OPEN_MAX + 1, buf_read, 2)); } static @@ -70,7 +81,7 @@ void ft_read_test_compare(void) FT_READ_EXPECT(""); FT_READ_EXPECT("bon"); FT_READ_EXPECT("bonjour"); - FT_READ_EXPECT("%c%s%p%x%X%e%f%g"); + FT_READ_EXPECT("#c#s#p#x#X#e#f#g"); FT_READ_EXPECT("the\0hidden"); FT_READ_EXPECT("Lorem ipsum dolor sit amet, consectetur adipiscing\ elit. Sed in malesuada purus. Etiam a scelerisque massa. Ut non euismod elit. Aliquam\ @@ -80,11 +91,16 @@ felis sed purus. Mauris magna ex, mollis non suscipit eu, lacinia ac turpis. Pha ac tortor et lectus fermentum lobortis eu at mauris. Vestibulum sit amet posuere\ tortor, sit amet consequat amet."); - FT_READ_EXPECT_ERROR(STDOUT_FILENO, NULL, 3); - FT_READ_EXPECT_ERROR(-1, "bonjour", 7); - FT_READ_EXPECT_ERROR(42, "bonjour", 7); - FT_READ_EXPECT_ERROR(9809, "bonjour", 7); - FT_READ_EXPECT_ERROR(98123, "", 1); + int tmp[2]; + pipe(tmp); + char buf[32]; + + write(tmp[1], "bonjour", 7); + FT_READ_EXPECT_ERROR(tmp[0], NULL, 3); + FT_READ_EXPECT_ERROR(-1, buf, 7); + FT_READ_EXPECT_ERROR(42, buf, 7); + FT_READ_EXPECT_ERROR(9809, buf, 7); + FT_READ_EXPECT_ERROR(98123, buf, 1); FT_READ_EXPECT_ERROR(42, NULL, 7); } |
