nptl: Remove __ASSUME_PRIVATE_FUTEX
[glibc.git] / support / test-driver.c
blob09c8783e4f54d8094d6827aba704a55a23c8fb7e
1 /* Main function for test programs.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* This file should be included from test cases. It will define a
20 main function which provides the test wrapper.
22 It assumes that the test case defines a function
24 int do_test (void);
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).
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.
42 Several preprocessors macros can be defined before including this
43 file.
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.
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:
52 int TEST_FUNCTION_ARGV (int argc, char **argv);
54 This overrides the do_test default function and is incompatible
55 with the TEST_FUNCTION macro.
57 If PREPARE is defined, it must expand to the name of a function of
58 the type
60 void PREPARE (int argc, char **);
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.
66 If CLEANUP_HANDLER is defined, it must expand to the name of a
67 function of the type
69 void CLEANUP_HANDLER (void);
71 This function will be called from the timeout (SIGALRM) signal
72 handler.
74 If EXPECTED_SIGNAL is defined, it must expanded to a constant which
75 denotes the expected signal number.
77 If EXPECTED_STATUS is defined, it must expand to the expected exit
78 status.
80 If TIMEOUT is defined, it must be positive constant. It overrides
81 the default test timeout and is measured in seconds.
83 If TEST_NO_MALLOPT is defined, the test wrapper will not call
84 mallopt.
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:
95 void CMDLINE_PROCESS (int);
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").
102 #include <support/test-driver.h>
104 #include <string.h>
107 main (int argc, char **argv)
109 struct test_config test_config;
110 memset (&test_config, 0, sizeof (test_config));
112 #ifdef PREPARE
113 test_config.prepare_function = (PREPARE);
114 #endif
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
127 #ifdef CLEANUP_HANDLER
128 test_config.cleanup_function = CLEANUP_HANDLER;
129 #endif
131 #ifdef EXPECTED_SIGNAL
132 test_config.expected_signal = (EXPECTED_SIGNAL);
133 #endif
135 #ifdef EXPECTED_STATUS
136 test_config.expected_status = (EXPECTED_STATUS);
137 #endif
139 #ifdef TEST_NO_MALLOPT
140 test_config.no_mallopt = 1;
141 #endif
143 #ifdef TIMEOUT
144 test_config.timeout = TIMEOUT;
145 #endif
147 #ifdef CMDLINE_OPTIONS
148 struct option options[] =
150 CMDLINE_OPTIONS
151 TEST_DEFAULT_OPTIONS
153 test_config.options = &options;
154 #endif
155 #ifdef CMDLINE_PROCESS
156 test_config.cmdline_function = CMDLINE_PROCESS;
157 #endif
158 #ifdef CMDLINE_OPTSTRING
159 test_config.optstring = "+" CMDLINE_OPTSTRING;
160 #else
161 test_config.optstring = "+";
162 #endif
164 return support_test_main (argc, argv, &test_config);