1 /* Test of error.h functions.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
18
19 #include <config.h>
20
21 #include "error.h"
22
23 #include <errno.h>
24
25 #include "macros.h"
26
27 /* Custom function to not show the program name in error messages. */
28 static void
29 print_no_progname (void)
30 {
31 }
32
33 int
34 main (int argc, char *argv[])
35 {
36 /* Test error() function with zero STATUS and zero ERRNUM. */
37 error (0, 0, "bummer");
38 /* With format string arguments. */
39 errno = EINVAL; /* should be ignored */
40 error (0, 0, "Zonk %d%d%d is too large", 1, 2, 3);
41 /* With non-ASCII characters. */
42 error (0, 0, "Pokémon started");
43 /* Verify error_message_count. */
44 ASSERT (error_message_count == 3);
45
46 /* Test error_at_line() function with zero STATUS and zero ERRNUM. */
47 error_at_line (0, 0, "d1/foo.c", 10, "invalid blub");
48 error_at_line (0, 0, "d1/foo.c", 10, "invalid blarn");
49 /* Verify error_message_count. */
50 ASSERT (error_message_count == 5);
51
52 /* Test error_one_per_line. */
53 error_one_per_line = 1;
54 error_at_line (0, 0, "d1/foo.c", 10, "unsupported glink");
55 /* Another line number. */
56 error_at_line (0, 0, "d1/foo.c", 13, "invalid brump");
57 /* Another file name. */
58 error_at_line (0, 0, "d2/foo.c", 13, "unsupported flinge");
59 /* Same file name and same line number => message not shown. */
60 error_at_line (0, 0, "d2/foo.c", 13, "invalid bark");
61 /* Verify error_message_count. */
62 ASSERT (error_message_count == 8);
63 error_one_per_line = 0;
64
65 /* Test error_print_progname. */
66 error_print_progname = print_no_progname;
67 error (0, 0, "hammer");
68 error (0, 0, "boing %d%d%d is too large", 1, 2, 3);
69 #if 0
70 /* The documentation does not describe the output if the file name is NULL. */
71 error_at_line (0, 0, NULL, 42, "drummer too loud");
72 #endif
73 error_at_line (0, 0, "d2/bar.c", 11, "bark too loud");
74 /* Verify error_message_count. */
75 ASSERT (error_message_count == 11);
76 error_print_progname = NULL;
77
78 /* Test error() function with nonzero ERRNUM. */
79 errno = EINVAL; /* should be ignored */
80 error (0, EACCES, "can't steal");
81 /* Verify error_message_count. */
82 ASSERT (error_message_count == 12);
83
84 /* Test error() function with nonzero STATUS. */
85 error (4, 0, "fatal error");
86
87 return 0;
88 }