1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| | #include <error.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#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;
}
|