#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; }