From 99a4f8397a9652b15d171ecb86e7281b80ed57d7 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 1 May 2020 10:11:13 +0200 Subject: Adding errno checking according to new subject (#2) prettier compatible with python < 3.6 removed unaccurate test for ft_read --- libasm_test.h | 13 +++++++-- prettier.py | 15 +++++----- test/ft_read_test.c | 78 +++++++++++++++++++++++++++++++-------------------- test/ft_strdup_test.c | 11 ++++---- test/ft_write_test.c | 34 ++++++++++++++-------- 5 files changed, 93 insertions(+), 58 deletions(-) diff --git a/libasm_test.h b/libasm_test.h index 362d91a..1e26c34 100644 --- a/libasm_test.h +++ b/libasm_test.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 03:07:19 by cacharle #+# #+# */ -/* Updated: 2020/04/13 14:41:40 by charles ### ########.fr */ +/* Updated: 2020/05/04 16:05:51 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ # include # include # include +# include # define TO_STRING(x) #x @@ -113,7 +114,7 @@ bool signaled; wait(&pid); \ signaled = !WIFEXITED(pid); \ } \ -} while(0); +} while(0) char *test_name; @@ -123,6 +124,12 @@ char *test_name; print_signaled_ko(); \ else \ print_ok(); \ -} while(0); +} while(0) + +# define ERRNO_WRAP(x, errno_save) do { \ + errno = 0; \ + do { x; } while (0); \ + errno_save = errno; \ +} while(0) #endif diff --git a/prettier.py b/prettier.py index f73e0e4..1833a71 100644 --- a/prettier.py +++ b/prettier.py @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/05 23:04:13 by cacharle #+# #+# # -# Updated: 2020/02/05 23:07:13 by cacharle ### ########.fr # +# Updated: 2020/05/04 14:44:44 by charles ### ########.fr # # # # **************************************************************************** # @@ -17,11 +17,11 @@ import argparse def green(*strings): - return "".join([f"\033[32m{s}\033[0m" for s in strings]) + return "".join(["\033[32m{}\033[0m".format(s) for s in strings]) def red(*strings): - return "".join([f"\033[31m{s}\033[0m" for s in strings]) + return "".join(["\033[31m{}\033[0m".format(s) for s in strings]) def create_logs_entry(logs, key): logs[key] = {} @@ -89,9 +89,10 @@ if __name__ == "__main__": for k, v in logs.items(): for e in v["ko_info"]: if e['type'] == "SEGFAULT": - print(f"{k} : {red('SEGFAULT')}") + print("{} : {}".format(k, red('SEGFAULT'))) elif e['type'] == "COMPARE": - p = f"{k}" + p = str(k) if e["with"] is not None: - p += f"with {e['with']}" - print(p, f":\n {green('expected: ') + e['expected']}\n {red('actual: ') + e['actual']}") + p += "with {}".format(e['with']) + print(p, ":\n {}\n {}".format(green('expected: ') + e['expected'], + red('actual: ') + e['actual'])) 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/test/ft_strdup_test.c b/test/ft_strdup_test.c index a53e58f..19ffe2c 100644 --- a/test/ft_strdup_test.c +++ b/test/ft_strdup_test.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 03:08:06 by cacharle #+# #+# */ -/* Updated: 2020/04/13 14:45:00 by charles ### ########.fr */ +/* Updated: 2020/05/04 15:21:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,6 @@ static void ft_strdup_test_segfault(void) { char *tmp2 = NULL; - char a; TEST_ASM_FUNCTION(ft_strdup("")); TEST_ASM_FUNCTION(ft_strdup("abc")); @@ -42,13 +41,13 @@ ac tortor et lectus fermentum lobortis eu at mauris. Vestibulum sit amet posuere tortor, sit amet consequat amet.")); tmp2 = ft_strdup(""); - TEST_ASM_FUNCTION(a = *tmp2); + TEST_ASM_FUNCTION(char a = *tmp2; a++;); tmp2 = ft_strdup("abc"); - TEST_ASM_FUNCTION(a = *tmp2); + TEST_ASM_FUNCTION(char a = *tmp2; a++;); tmp2 = ft_strdup("asl;fjl;asd"); - TEST_ASM_FUNCTION(a = *tmp2); + TEST_ASM_FUNCTION(char a = *tmp2; a++;); tmp2 = ft_strdup("yope\0la"); - TEST_ASM_FUNCTION(a = *tmp2); + TEST_ASM_FUNCTION(char a = *tmp2; a++;); } static diff --git a/test/ft_write_test.c b/test/ft_write_test.c index 57b1b0b..f961791 100644 --- a/test/ft_write_test.c +++ b/test/ft_write_test.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 03:07:48 by cacharle #+# #+# */ -/* Updated: 2020/04/13 14:51:39 by charles ### ########.fr */ +/* Updated: 2020/05/04 16:06:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,17 +16,25 @@ static int ft_write_pipe[2]; static char buf[FT_WRITE_BUF_SIZE] = {0}; -static unsigned long write_ret; -static int ret; +static ssize_t write_ret; +static ssize_t write_origin_ret; +static int write_errno; +static int write_origin_errno; +static ssize_t ret; #define FT_WRITE_EXPECT(str) do { \ if (pipe(ft_write_pipe) < 0) \ exit(EXIT_FAILURE); \ fcntl(ft_write_pipe[0], F_SETFL, O_NONBLOCK); \ - write_ret = ft_write(ft_write_pipe[1], str, strlen(str)); \ + ERRNO_WRAP(write_origin_ret = write(ft_write_pipe[1], str, strlen(str)), write_origin_errno); \ + read(ft_write_pipe[0], buf, FT_WRITE_BUF_SIZE); \ + ERRNO_WRAP(write_ret = ft_write(ft_write_pipe[1], str, strlen(str)), write_errno); \ ret = read(ft_write_pipe[0], buf, FT_WRITE_BUF_SIZE); \ buf[ret] = '\0'; \ - if (strcmp(buf, str) != 0 || write_ret != strlen(str)) \ + if (write_errno != write_origin_errno) \ + printf("KO: [COMPARE]: %s: expected: errno %d got: errno %d with: "#str"\n", \ + test_name, write_origin_errno, write_errno); \ + else if (strcmp(buf, str) != 0 || write_ret != write_origin_ret) \ printf("KO: [COMPARE]: %s: expected: %lu \"%s\" got: %lu \"%s\" with: %d, \"%s\", %zu \n", \ test_name, strlen(str), str, write_ret, buf, ft_write_pipe[0], buf, strlen(str)); \ else \ @@ -35,13 +43,17 @@ static int ret; close(ft_write_pipe[0]); \ } while (0); -#define FT_WRITE_EXPECT_ERROR(fd, str, size) do { \ - write_ret = ft_write(fd, str, size); \ - if ((long)write_ret != -1) \ +#define FT_WRITE_EXPECT_ERROR(fd, str, size) do { \ + ERRNO_WRAP(write_ret = ft_write(fd, str, size), write_errno); \ + ERRNO_WRAP(write_origin_ret = write(fd, str, size), write_origin_errno); \ + if ((long)write_ret != -1) \ printf("KO: [COMPARE]: %s: expected: %ld got: %ld with: %d "#str", %d\n", \ test_name, -1l, (long)write_ret, fd, size); \ - else \ - print_ok(); \ + else if (write_errno != write_origin_errno) \ + printf("KO: [COMPARE]: %s: expected: errno %d got: errno %d with: %d "#str", %d\n", \ + test_name, write_origin_errno, write_errno, fd, size); \ + else \ + print_ok(); \ } while (0); static @@ -71,7 +83,7 @@ void ft_write_test_compare(void) FT_WRITE_EXPECT(""); FT_WRITE_EXPECT("bon"); FT_WRITE_EXPECT("bonjour"); - FT_WRITE_EXPECT("%c%s%p%x%X%e%f%g"); + FT_WRITE_EXPECT("#c#s#p#x#X#e#f#g"); FT_WRITE_EXPECT("the\0hidden"); FT_WRITE_EXPECT("Lorem ipsum dolor sit amet, consectetur adipiscing\ elit. Sed in malesuada purus. Etiam a scelerisque massa. Ut non euismod elit. Aliquam\ -- cgit