]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - tests/example_error.c
update copyright year
[coloredstderr/coloredstderr.git] / tests / example_error.c
1 /*
2  * Test error() and error_at_line(). Non-standard, GNU only.
3  *
4  * Copyright (C) 2013-2015  Simon Ruderich
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21
22 #define _GNU_SOURCE /* for program_invocation_name */
23 #include <errno.h>
24 #include <error.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30
31 #include "../src/compiler.h"
32 #include "example.h"
33
34
35 void (*error_print_progname)(void);
36
37
38 static void print_progname(void) {
39     fprintf(stderr, "PROG");
40 }
41
42
43 int main(int argc unused, char **argv unused) {
44     pid_t pid;
45
46     char name[] = "./example_error";
47     program_invocation_name = name;
48
49     error(0, 0, "<message>");
50     error_at_line(0, 0, "file", 42, "<message>");
51     FORKED_TEST(pid) { error(1, 0, "<message>"); }
52     FORKED_TEST(pid) { error_at_line(1, 0, "file", 42, "<message>"); }
53
54     error(0, ENOMEM, "<message>");
55     error_at_line(0, ENOMEM, "file", 42, "<message>");
56     error_at_line(0, ENOMEM, "file", 42, "<message>");
57     FORKED_TEST(pid) { error(1, ENOMEM, "<message>"); }
58     FORKED_TEST(pid) { error_at_line(1, ENOMEM, "file", 42, "<message>"); }
59
60     fflush(stdout);
61     printf("\n\n");
62     fflush(stdout);
63
64     error_print_progname = print_progname;
65     error_one_per_line = 1;
66
67     error(0, 0, "<message>");
68     error_at_line(0, 0, "file", 42, "<message>");
69     FORKED_TEST(pid) { error(1, 0, "<message>"); }
70     FORKED_TEST(pid) { error_at_line(1, 0, "file", 42, "<message>"); }
71
72     error(0, ENOMEM, "<message>");
73     error_at_line(0, ENOMEM, "file", 42, "<message>");
74     error_at_line(0, ENOMEM, "file", 42, "<message>");
75     FORKED_TEST(pid) { error(1, ENOMEM, "<message>"); }
76
77     error_one_per_line = 0;
78     FORKED_TEST(pid) { error_at_line(1, ENOMEM, "file", 42, "<message>"); }
79
80     return EXIT_SUCCESS;
81 }