]>
Commit | Line | Data |
---|---|---|
c23de0aa | 1 | /* Main function for test programs. |
2b778ceb | 2 | Copyright (C) 2016-2021 Free Software Foundation, Inc. |
c23de0aa FW |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
c23de0aa FW |
18 | |
19 | /* This file should be included from test cases. It will define a | |
20 | main function which provides the test wrapper. | |
21 | ||
22 | It assumes that the test case defines a function | |
23 | ||
24 | int do_test (void); | |
25 | ||
26 | and arranges for that function being called under the test wrapper. | |
27 | The do_test function should return 0 to indicate a passing test, 1 | |
28 | to indicate a failing test, or 77 to indicate an unsupported test. | |
29 | Other result values could be used to indicate a failing test, but | |
30 | the result of the expression is passed to exit and exit only | |
31 | returns the lower 8 bits of its input. A non-zero return with some | |
32 | values could cause a test to incorrectly be considered passing when | |
33 | it really failed. For this reason, the function should always | |
34 | return 0 (EXIT_SUCCESS), 1 (EXIT_FAILURE), or 77 | |
35 | (EXIT_UNSUPPORTED). | |
36 | ||
37 | The test function may print out diagnostic or warning messages as well | |
38 | as messages about failures. These messages should be printed to stdout | |
39 | and not stderr so that the output is properly ordered with respect to | |
40 | the rest of the glibc testsuite run output. | |
41 | ||
42 | Several preprocessors macros can be defined before including this | |
43 | file. | |
44 | ||
45 | The name of the do_test function can be changed with the | |
46 | TEST_FUNCTION macro. It must expand to the desired function name. | |
47 | ||
48 | If the test case needs access to command line parameters, it must | |
49 | define the TEST_FUNCTION_ARGV macro with the name of the test | |
50 | function. It must have the following type: | |
51 | ||
52 | int TEST_FUNCTION_ARGV (int argc, char **argv); | |
53 | ||
54 | This overrides the do_test default function and is incompatible | |
55 | with the TEST_FUNCTION macro. | |
56 | ||
57 | If PREPARE is defined, it must expand to the name of a function of | |
58 | the type | |
59 | ||
60 | void PREPARE (int argc, char **); | |
61 | ||
62 | This function will be called early, after parsing the command line, | |
63 | but before running the test, in the parent process which acts as | |
64 | the test supervisor. | |
65 | ||
66 | If CLEANUP_HANDLER is defined, it must expand to the name of a | |
67 | function of the type | |
68 | ||
69 | void CLEANUP_HANDLER (void); | |
70 | ||
71 | This function will be called from the timeout (SIGALRM) signal | |
72 | handler. | |
73 | ||
74 | If EXPECTED_SIGNAL is defined, it must expanded to a constant which | |
75 | denotes the expected signal number. | |
76 | ||
77 | If EXPECTED_STATUS is defined, it must expand to the expected exit | |
78 | status. | |
79 | ||
80 | If TIMEOUT is defined, it must be positive constant. It overrides | |
81 | the default test timeout and is measured in seconds. | |
82 | ||
83 | If TEST_NO_MALLOPT is defined, the test wrapper will not call | |
84 | mallopt. | |
85 | ||
86 | Custom command line handling can be implemented by defining the | |
87 | CMDLINE_OPTION macro (after including the <getopt.h> header; this | |
88 | requires _GNU_SOURCE to be defined). This macro must expand to a | |
89 | to a comma-separated list of braced initializers for struct option | |
90 | from <getopt.h>, with a trailing comma. CMDLINE_PROCESS can be | |
91 | defined as the name of a function which is called to process these | |
92 | options. The function is passed the option character/number and | |
93 | has this type: | |
94 | ||
95 | void CMDLINE_PROCESS (int); | |
244361eb AZ |
96 | |
97 | If the program also to process custom default short command line | |
98 | argument (similar to getopt) it must define CMDLINE_OPTSTRING | |
99 | with the expected options (for instance "vb"). | |
c23de0aa FW |
100 | */ |
101 | ||
102 | #include <support/test-driver.h> | |
103 | ||
104 | #include <string.h> | |
105 | ||
106 | int | |
107 | main (int argc, char **argv) | |
108 | { | |
109 | struct test_config test_config; | |
110 | memset (&test_config, 0, sizeof (test_config)); | |
111 | ||
112 | #ifdef PREPARE | |
113 | test_config.prepare_function = (PREPARE); | |
114 | #endif | |
115 | ||
116 | #if defined (TEST_FUNCTION) && defined (TEST_FUNCTON_ARGV) | |
117 | # error TEST_FUNCTION and TEST_FUNCTION_ARGV cannot be defined at the same time | |
118 | #endif | |
119 | #if defined (TEST_FUNCTION) | |
120 | test_config.test_function = TEST_FUNCTION; | |
121 | #elif defined (TEST_FUNCTION_ARGV) | |
122 | test_config.test_function_argv = TEST_FUNCTION_ARGV; | |
123 | #else | |
124 | test_config.test_function = do_test; | |
125 | #endif | |
126 | ||
127 | #ifdef CLEANUP_HANDLER | |
128 | test_config.cleanup_function = CLEANUP_HANDLER; | |
129 | #endif | |
130 | ||
131 | #ifdef EXPECTED_SIGNAL | |
132 | test_config.expected_signal = (EXPECTED_SIGNAL); | |
133 | #endif | |
134 | ||
135 | #ifdef EXPECTED_STATUS | |
136 | test_config.expected_status = (EXPECTED_STATUS); | |
137 | #endif | |
138 | ||
139 | #ifdef TEST_NO_MALLOPT | |
140 | test_config.no_mallopt = 1; | |
141 | #endif | |
142 | ||
5c0202af FW |
143 | #ifdef TEST_NO_SETVBUF |
144 | test_config.no_setvbuf = 1; | |
145 | #endif | |
146 | ||
c23de0aa FW |
147 | #ifdef TIMEOUT |
148 | test_config.timeout = TIMEOUT; | |
149 | #endif | |
150 | ||
151 | #ifdef CMDLINE_OPTIONS | |
152 | struct option options[] = | |
153 | { | |
154 | CMDLINE_OPTIONS | |
f47ae518 | 155 | TEST_DEFAULT_OPTIONS |
c23de0aa FW |
156 | }; |
157 | test_config.options = &options; | |
158 | #endif | |
159 | #ifdef CMDLINE_PROCESS | |
160 | test_config.cmdline_function = CMDLINE_PROCESS; | |
161 | #endif | |
244361eb AZ |
162 | #ifdef CMDLINE_OPTSTRING |
163 | test_config.optstring = "+" CMDLINE_OPTSTRING; | |
164 | #else | |
165 | test_config.optstring = "+"; | |
166 | #endif | |
c23de0aa FW |
167 | |
168 | return support_test_main (argc, argv, &test_config); | |
169 | } |