packaging: add --disable-experimental-malloc
[platform/upstream/glibc.git] / support / test-driver.c
1 /* Main function for test programs.
2    Copyright (C) 2016-2023 Free Software Foundation, Inc.
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
17    <https://www.gnu.org/licenses/>.  */
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);
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").
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 #ifdef RUN_COMMAND_MODE
120   test_config.run_command_mode = 1;
121 #elif defined (TEST_FUNCTION)
122   test_config.test_function = TEST_FUNCTION;
123 #elif defined (TEST_FUNCTION_ARGV)
124   test_config.test_function_argv = TEST_FUNCTION_ARGV;
125 #else
126   test_config.test_function = do_test;
127 #endif
128
129 #ifdef CLEANUP_HANDLER
130   test_config.cleanup_function = CLEANUP_HANDLER;
131 #endif
132
133 #ifdef EXPECTED_SIGNAL
134   test_config.expected_signal = (EXPECTED_SIGNAL);
135 #endif
136
137 #ifdef EXPECTED_STATUS
138   test_config.expected_status = (EXPECTED_STATUS);
139 #endif
140
141 #ifdef TEST_NO_MALLOPT
142   test_config.no_mallopt = 1;
143 #endif
144
145 #ifdef TEST_NO_SETVBUF
146   test_config.no_setvbuf = 1;
147 #endif
148
149 #ifdef TIMEOUT
150   test_config.timeout = TIMEOUT;
151 #endif
152
153 #ifdef CMDLINE_OPTIONS
154   struct option options[] =
155     {
156       CMDLINE_OPTIONS
157       TEST_DEFAULT_OPTIONS
158     };
159   test_config.options = &options;
160 #endif
161 #ifdef CMDLINE_PROCESS
162   test_config.cmdline_function = CMDLINE_PROCESS;
163 #endif
164 #ifdef CMDLINE_OPTSTRING
165   test_config.optstring = "+" CMDLINE_OPTSTRING;
166 #else
167   test_config.optstring = "+";
168 #endif
169
170   return support_test_main (argc, argv, &test_config);
171 }