| 1 |
/* Test of error.h functions. |
| 2 |
Copyright (C) 2023-2025 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 <[email protected]>, 2023. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
/* Specification. */ |
| 22 |
#include <error.h> |
| 23 |
|
| 24 |
#include <errno.h> |
| 25 |
|
| 26 |
#include "macros.h" |
| 27 |
|
| 28 |
/* Custom function to not show the program name in error messages. */ |
| 29 |
static void |
| 30 |
print_no_progname (void) |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
int |
| 35 |
main () |
| 36 |
{ |
| 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); |
| 46 |
|
| 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); |
| 52 |
|
| 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; |
| 65 |
|
| 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); |
| 70 |
#if 0 |
| 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"); |
| 73 |
#endif |
| 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; |
| 78 |
|
| 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); |
| 84 |
|
| 85 |
/* Test error() function with nonzero STATUS. */ |
| 86 |
error (4, 0, "fatal error"); |
| 87 |
|
| 88 |
return test_exit_status; |
| 89 |
} |