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
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
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
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
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
66 If CLEANUP_HANDLER is defined, it must expand to the name of a
69 void CLEANUP_HANDLER (void);
71 This function will be called from the timeout (SIGALRM) signal
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
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
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
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>
107 main (int argc
, char **argv
)
109 struct test_config test_config
;
110 memset (&test_config
, 0, sizeof (test_config
));
113 test_config
.prepare_function
= (PREPARE
);
116 #if defined (TEST_FUNCTION) && defined (TEST_FUNCTON_ARGV)
117 # error TEST_FUNCTION and TEST_FUNCTION_ARGV cannot be defined at the same time
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
;
124 test_config
.test_function
= do_test
;
127 #ifdef CLEANUP_HANDLER
128 test_config
.cleanup_function
= CLEANUP_HANDLER
;
131 #ifdef EXPECTED_SIGNAL
132 test_config
.expected_signal
= (EXPECTED_SIGNAL
);
135 #ifdef EXPECTED_STATUS
136 test_config
.expected_status
= (EXPECTED_STATUS
);
139 #ifdef TEST_NO_MALLOPT
140 test_config
.no_mallopt
= 1;
143 #ifdef TEST_NO_SETVBUF
144 test_config
.no_setvbuf
= 1;
148 test_config
.timeout
= TIMEOUT
;
151 #ifdef CMDLINE_OPTIONS
152 struct option options
[] =
157 test_config
.options
= &options
;
159 #ifdef CMDLINE_PROCESS
160 test_config
.cmdline_function
= CMDLINE_PROCESS
;
162 #ifdef CMDLINE_OPTSTRING
163 test_config
.optstring
= "+" CMDLINE_OPTSTRING
;
165 test_config
.optstring
= "+";
168 return support_test_main (argc
, argv
, &test_config
);