1 /* Test of error.h functions.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
28 /* Custom function to not show the program name in error messages. */
30 print_no_progname (void)
37 /* Test error() function with zero STATUS and zero ERRNUM. */
38 error (0, 0, "bummer");
39 /* With format string arguments. */
40 errno
= EINVAL
; /* should be ignored */
41 error (0, 0, "Zonk %d%d%d is too large", 1, 2, 3);
42 /* With non-ASCII characters. */
43 error (0, 0, "Pokémon started");
44 /* Verify error_message_count. */
45 ASSERT (error_message_count
== 3);
47 /* Test error_at_line() function with zero STATUS and zero ERRNUM. */
48 error_at_line (0, 0, "d1/foo.c", 10, "invalid blub");
49 error_at_line (0, 0, "d1/foo.c", 10, "invalid blarn");
50 /* Verify error_message_count. */
51 ASSERT (error_message_count
== 5);
53 /* Test error_one_per_line. */
54 error_one_per_line
= 1;
55 error_at_line (0, 0, "d1/foo.c", 10, "unsupported glink");
56 /* Another line number. */
57 error_at_line (0, 0, "d1/foo.c", 13, "invalid brump");
58 /* Another file name. */
59 error_at_line (0, 0, "d2/foo.c", 13, "unsupported flinge");
60 /* Same file name and same line number => message not shown. */
61 error_at_line (0, 0, "d2/foo.c", 13, "invalid bark");
62 /* Verify error_message_count. */
63 ASSERT (error_message_count
== 8);
64 error_one_per_line
= 0;
66 /* Test error_print_progname. */
67 error_print_progname
= print_no_progname
;
68 error (0, 0, "hammer");
69 error (0, 0, "boing %d%d%d is too large", 1, 2, 3);
71 /* The documentation does not describe the output if the file name is NULL. */
72 error_at_line (0, 0, NULL
, 42, "drummer too loud");
74 error_at_line (0, 0, "d2/bar.c", 11, "bark too loud");
75 /* Verify error_message_count. */
76 ASSERT (error_message_count
== 11);
77 error_print_progname
= NULL
;
79 /* Test error() function with nonzero ERRNUM. */
80 errno
= EINVAL
; /* should be ignored */
81 error (0, EACCES
, "can't steal");
82 /* Verify error_message_count. */
83 ASSERT (error_message_count
== 12);
85 /* Test error() function with nonzero STATUS. */
86 error (4, 0, "fatal error");
88 return test_exit_status
;