usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-verror.c
blob1e9dcd9cc5f00aa39c7e769594e34fc3dba36371
1 /* Test of verror.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. */
19 #include <config.h>
21 #include "verror.h"
23 #include <errno.h>
24 #include <stdarg.h>
26 #include <error.h>
27 #include "macros.h"
29 /* Custom function to not show the program name in error messages. */
30 static void
31 print_no_progname (void)
35 static void
36 test_zero (const char *format, ...)
38 va_list args;
40 va_start (args, format);
41 verror (0, 0, format, args);
42 va_end (args);
45 static void
46 test_zero_at_line (const char *filename, unsigned int lineno,
47 const char *format, ...)
49 va_list args;
51 va_start (args, format);
52 verror_at_line (0, 0, filename, lineno, format, args);
53 va_end (args);
56 static void
57 test_errnum (const char *format, ...)
59 va_list args;
61 va_start (args, format);
62 verror (0, EACCES, format, args);
63 va_end (args);
66 static void
67 test_fatal (const char *format, ...)
69 va_list args;
71 va_start (args, format);
72 verror (4, 0, format, args);
73 va_end (args);
76 int
77 main (int argc, char *argv[])
79 /* Test verror() function with zero STATUS and zero ERRNUM. */
80 test_zero ("bummer");
81 /* With format string arguments. */
82 errno = EINVAL; /* should be ignored */
83 test_zero ("Zonk %d%d%d is too large", 1, 2, 3);
84 /* With non-ASCII characters. */
85 test_zero ("Pokémon started");
86 /* Verify error_message_count. */
87 ASSERT (error_message_count == 3);
89 /* Test verror_at_line() function with zero STATUS and zero ERRNUM. */
90 test_zero_at_line ("d1/foo.c", 10, "invalid blub");
91 test_zero_at_line ("d1/foo.c", 10, "invalid blarn");
92 /* Verify error_message_count. */
93 ASSERT (error_message_count == 5);
95 /* Test error_one_per_line. */
96 error_one_per_line = 1;
97 test_zero_at_line ("d1/foo.c", 10, "unsupported glink");
98 /* Another line number. */
99 test_zero_at_line ("d1/foo.c", 13, "invalid brump");
100 /* Another file name. */
101 test_zero_at_line ("d2/foo.c", 13, "unsupported flinge");
102 /* Same file name and same line number => message not shown. */
103 test_zero_at_line ("d2/foo.c", 13, "invalid bark");
104 /* Verify error_message_count. */
105 ASSERT (error_message_count == 8);
106 error_one_per_line = 0;
108 /* Test error_print_progname. */
109 error_print_progname = print_no_progname;
110 test_zero ("hammer");
111 test_zero ("boing %d%d%d is too large", 1, 2, 3);
112 #if 0
113 /* The documentation does not describe the output if the file name is NULL. */
114 test_zero_at_line (NULL, 42, "drummer too loud");
115 #endif
116 test_zero_at_line ("d2/bar.c", 11, "bark too loud");
117 /* Verify error_message_count. */
118 ASSERT (error_message_count == 11);
119 error_print_progname = NULL;
121 /* Test verror() function with nonzero ERRNUM. */
122 errno = EINVAL; /* should be ignored */
123 test_errnum ("can't steal");
124 /* Verify error_message_count. */
125 ASSERT (error_message_count == 12);
127 /* Test verror() function with nonzero STATUS. */
128 test_fatal ("fatal error");
130 return test_exit_status;