2 * Definitions for Wine C unit tests.
4 * Copyright (C) 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 extern int winetest_debug;
32 /* running in interactive mode? */
33 extern int winetest_interactive;
35 /* current platform */
36 extern const char *winetest_platform;
38 extern void winetest_set_location( const char* file, int line );
39 extern void winetest_start_todo( const char* platform );
40 extern int winetest_loop_todo(void);
41 extern void winetest_end_todo( const char* platform );
42 extern int winetest_get_mainargs( char*** pargv );
45 #define START_TEST(name) \
46 static void func_##name(void); \
47 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
48 static void func_##name(void)
50 #define START_TEST(name) void func_##name(void)
55 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
56 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
60 extern int winetest_ok( int condition, const char *msg, ... );
61 extern void winetest_trace( const char *msg, ... );
65 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
66 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
68 #define ok ok_(__FILE__, __LINE__)
69 #define trace trace_(__FILE__, __LINE__)
71 #define todo(platform) for (winetest_start_todo(platform); \
72 winetest_loop_todo(); \
73 winetest_end_todo(platform))
74 #define todo_wine todo("wine")
77 #ifdef NONAMELESSUNION
99 #ifdef NONAMELESSSTRUCT
101 # define S1(x) (x).s1
102 # define S2(x) (x).s2
103 # define S3(x) (x).s3
104 # define S4(x) (x).s4
105 # define S5(x) (x).s5
116 /************************************************************************/
117 /* Below is the implementation of the various functions, to be included
118 * directly into the generated testlist.c file.
119 * It is done that way so that the dlls can build the test routines with
120 * different includes or flags if needed.
133 extern const struct test winetest_testlist[];
136 int winetest_debug = 1;
138 /* interactive mode? */
139 int winetest_interactive = 0;
141 /* current platform */
142 const char *winetest_platform = "windows";
144 /* report successful tests (BOOL) */
145 static int report_success = 0;
147 /* passing arguments around */
148 static int winetest_argc;
149 static char** winetest_argv;
151 static const struct test *current_test; /* test currently being run */
153 static LONG successes; /* number of successful tests */
154 static LONG failures; /* number of failures */
155 static LONG todo_successes; /* number of successful tests inside todo block */
156 static LONG todo_failures; /* number of failures inside todo block */
158 /* The following data must be kept track of on a per-thread basis */
161 const char* current_file; /* file of current check */
162 int current_line; /* line of current check */
163 int todo_level; /* current todo nesting level */
166 static DWORD tls_index;
168 static tls_data* get_tls_data(void)
173 last_error=GetLastError();
174 data=TlsGetValue(tls_index);
177 data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
178 TlsSetValue(tls_index,data);
180 SetLastError(last_error);
184 static void exit_process( int code )
191 void winetest_set_location( const char* file, int line )
193 tls_data* data=get_tls_data();
194 data->current_file=strrchr(file,'/');
195 if (data->current_file==NULL)
196 data->current_file=strrchr(file,'\\');
197 if (data->current_file==NULL)
198 data->current_file=file;
200 data->current_file++;
201 data->current_line=line;
207 * - condition - condition to check;
208 * - msg test description;
209 * - file - test application source code file name of the check
210 * - line - test application source code file line number of the check
212 * 0 if condition does not have the expected value, 1 otherwise
214 int winetest_ok( int condition, const char *msg, ... )
217 tls_data* data=get_tls_data();
219 if (data->todo_level)
223 fprintf( stdout, "%s:%d: Test succeeded inside todo block",
224 data->current_file, data->current_line );
227 va_start(valist, msg);
228 fprintf(stdout,": ");
229 vfprintf(stdout, msg, valist);
232 InterlockedIncrement(&todo_failures);
235 else InterlockedIncrement(&todo_successes);
241 fprintf( stdout, "%s:%d: Test failed",
242 data->current_file, data->current_line );
245 va_start(valist, msg);
246 fprintf( stdout,": ");
247 vfprintf(stdout, msg, valist);
250 InterlockedIncrement(&failures);
256 fprintf( stdout, "%s:%d: Test succeeded\n",
257 data->current_file, data->current_line);
258 InterlockedIncrement(&successes);
264 void winetest_trace( const char *msg, ... )
267 tls_data* data=get_tls_data();
269 if (winetest_debug > 0)
271 fprintf( stdout, "%s:%d:", data->current_file, data->current_line );
272 va_start(valist, msg);
273 vfprintf(stdout, msg, valist);
278 void winetest_start_todo( const char* platform )
280 tls_data* data=get_tls_data();
281 if (strcmp(winetest_platform,platform)==0)
283 data->todo_do_loop=1;
286 int winetest_loop_todo(void)
288 tls_data* data=get_tls_data();
289 int do_loop=data->todo_do_loop;
290 data->todo_do_loop=0;
294 void winetest_end_todo( const char* platform )
296 if (strcmp(winetest_platform,platform)==0)
298 tls_data* data=get_tls_data();
303 int winetest_get_mainargs( char*** pargv )
305 *pargv = winetest_argv;
306 return winetest_argc;
309 /* Find a test by name */
310 static const struct test *find_test( const char *name )
312 const struct test *test;
316 if ((p = strrchr( name, '/' ))) name = p + 1;
317 if ((p = strrchr( name, '\\' ))) name = p + 1;
319 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
321 for (test = winetest_testlist; test->name; test++)
323 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
325 return test->name ? test : NULL;
329 /* Display list of valid tests */
330 static void list_tests(void)
332 const struct test *test;
334 fprintf( stdout, "Valid test names:\n" );
335 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
339 /* Run a named test, and return exit status */
340 static int run_test( const char *name )
342 const struct test *test;
345 if (!(test = find_test( name )))
347 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
350 successes = failures = todo_successes = todo_failures = 0;
351 tls_index=TlsAlloc();
357 fprintf( stdout, "%s: %ld tests executed, %ld marked as todo, %ld %s.\n",
358 name, successes + failures + todo_successes + todo_failures,
359 todo_successes, failures + todo_failures,
360 (failures + todo_failures != 1) ? "failures" : "failure" );
362 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
367 /* Display usage and exit */
368 static void usage( const char *argv0 )
370 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
377 int main( int argc, char **argv )
381 setvbuf (stdout, NULL, _IONBF, 0);
383 winetest_argc = argc;
384 winetest_argv = argv;
386 if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = p;
387 if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
388 if ((p = getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive = atoi(p);
389 if ((p = getenv( "WINETEST_REPORT_SUCCESS"))) report_success = atoi(p);
392 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
393 return run_test( winetest_testlist[0].name );
396 if (!strcmp( argv[1], "--list" ))
401 return run_test(argv[1]);
404 #endif /* STANDALONE */
406 #endif /* __WINE_TEST_H */