From 751247d2cdcf3f64aa27da0dc9801169b78abec5 Mon Sep 17 00:00:00 2001 From: James Y Knight Date: Mon, 22 Jul 2019 22:57:39 -0400 Subject: [PATCH] Add tests for functions. --- src/functional/error.c | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/functional/error.c diff --git a/src/functional/error.c b/src/functional/error.c new file mode 100644 index 0000000..e0b063c --- /dev/null +++ b/src/functional/error.c @@ -0,0 +1,68 @@ +#include + +#include +#include +#include +#include +#include +#include +#include "test.h" + +#define ASSERT(c) do { \ + errno = 0; \ + if (!(c)) { \ + t_error("%s failed (errno: %s)\n", #c, strerror(errno)); \ + exit(1); \ + } \ +} while(0) + +void my_print_progname(void) { + fputs("Progname:", stderr); +} + +const char *expected_output = + "src/functional/error.exe: Test1\n" + "src/functional/error.exe:File:44: Test2 hello\n" + "Progname:File:44: Test3 4\n" + "Progname:Test4 hello: No such file or directory\n" + "Progname:OtherFile:44: Test6\n" + "Progname:OtherFile:47: Test7\n" + "error_message_count:6\n" + "Progname:Last error\n"; + +int main() { + int fd, pid, status; + int pipefds[2]; + ASSERT(pipe(pipefds) == 0); + + ASSERT((pid = fork()) >= 0); + if (pid == 0) { + ASSERT(dup2(pipefds[1], 1) == 1); + ASSERT(dup2(pipefds[1], 2) == 2); + + error(0, 0, "Test1"); + error_at_line(0, 0, "File", 44, "Test2 %s", "hello"); + + error_print_progname = my_print_progname; + + error_one_per_line = 1; + error_at_line(0, 0, "File", 44, "Test3 %d", 4); + error(0, ENOENT, "Test4 %s", "hello"); + error_at_line(0, 0, "File", 44, "Test5 (skipped)"); + error_at_line(0, 0, "OtherFile", 44, "Test6"); + error_at_line(0, 0, "OtherFile", 47, "Test7"); + printf("error_message_count:%d\n", error_message_count); + error(77, 0, "Last error"); + } + ASSERT(waitpid(pid, &status, 0) == pid); + ASSERT(WIFEXITED(status) && WEXITSTATUS(status) == 77); + + char buf[1000]; + ssize_t count; + ASSERT((count = read(pipefds[0], buf, sizeof(buf)-1)) >= 0); + buf[count] = 0; + if (strcmp(buf, expected_output)) { + t_error("Unexpected output. Got:\n%s\n", buf); + } + return t_status; +} -- 2.22.0.657.g960e92d24f-goog