push 43f03fe87c2254c6df67b2de3c08b5b20fd64327
[wine/hacks.git] / include / wine / test.h
bloba8f6f03d282432c8ed98d5e4f280606a1ed3f6df
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_WINE_TEST_H
22 #define __WINE_WINE_TEST_H
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <windef.h>
27 #include <winbase.h>
29 #ifdef __WINE_CONFIG_H
30 #error config.h should not be used in Wine tests
31 #endif
32 #ifdef __WINE_WINE_LIBRARY_H
33 #error wine/library.h should not be used in Wine tests
34 #endif
35 #ifdef __WINE_WINE_UNICODE_H
36 #error wine/unicode.h should not be used in Wine tests
37 #endif
38 #ifdef __WINE_WINE_DEBUG_H
39 #error wine/debug.h should not be used in Wine tests
40 #endif
42 #ifndef INVALID_FILE_ATTRIBUTES
43 #define INVALID_FILE_ATTRIBUTES (~0u)
44 #endif
45 #ifndef INVALID_SET_FILE_POINTER
46 #define INVALID_SET_FILE_POINTER (~0u)
47 #endif
49 /* debug level */
50 extern int winetest_debug;
52 /* running in interactive mode? */
53 extern int winetest_interactive;
55 /* current platform */
56 extern const char *winetest_platform;
58 extern void winetest_set_location( const char* file, int line );
59 extern void winetest_start_todo( const char* platform );
60 extern int winetest_loop_todo(void);
61 extern void winetest_end_todo( const char* platform );
62 extern int winetest_get_mainargs( char*** pargv );
63 extern void winetest_wait_child_process( HANDLE process );
65 #ifdef STANDALONE
66 #define START_TEST(name) \
67 static void func_##name(void); \
68 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
69 static void func_##name(void)
70 #else
71 #define START_TEST(name) void func_##name(void)
72 #endif
74 extern int broken( int condition );
75 extern int winetest_vok( int condition, const char *msg, va_list ap );
76 extern void winetest_vskip( const char *msg, va_list ap );
78 #ifdef __GNUC__
80 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
81 extern void winetest_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
82 extern void winetest_win_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
83 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
85 #else /* __GNUC__ */
87 extern int winetest_ok( int condition, const char *msg, ... );
88 extern void winetest_skip( const char *msg, ... );
89 extern void winetest_win_skip( const char *msg, ... );
90 extern void winetest_trace( const char *msg, ... );
92 #endif /* __GNUC__ */
94 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
95 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
96 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
97 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
99 #define ok ok_(__FILE__, __LINE__)
100 #define skip skip_(__FILE__, __LINE__)
101 #define win_skip win_skip_(__FILE__, __LINE__)
102 #define trace trace_(__FILE__, __LINE__)
104 #define todo(platform) for (winetest_start_todo(platform); \
105 winetest_loop_todo(); \
106 winetest_end_todo(platform))
107 #define todo_wine todo("wine")
110 #ifdef NONAMELESSUNION
111 # define U(x) (x).u
112 # define U1(x) (x).u1
113 # define U2(x) (x).u2
114 # define U3(x) (x).u3
115 # define U4(x) (x).u4
116 # define U5(x) (x).u5
117 # define U6(x) (x).u6
118 # define U7(x) (x).u7
119 # define U8(x) (x).u8
120 #else
121 # define U(x) (x)
122 # define U1(x) (x)
123 # define U2(x) (x)
124 # define U3(x) (x)
125 # define U4(x) (x)
126 # define U5(x) (x)
127 # define U6(x) (x)
128 # define U7(x) (x)
129 # define U8(x) (x)
130 #endif
132 #ifdef NONAMELESSSTRUCT
133 # define S(x) (x).s
134 # define S1(x) (x).s1
135 # define S2(x) (x).s2
136 # define S3(x) (x).s3
137 # define S4(x) (x).s4
138 # define S5(x) (x).s5
139 #else
140 # define S(x) (x)
141 # define S1(x) (x)
142 # define S2(x) (x)
143 # define S3(x) (x)
144 # define S4(x) (x)
145 # define S5(x) (x)
146 #endif
149 /************************************************************************/
150 /* Below is the implementation of the various functions, to be included
151 * directly into the generated testlist.c file.
152 * It is done that way so that the dlls can build the test routines with
153 * different includes or flags if needed.
156 #ifdef STANDALONE
158 #include <stdio.h>
160 struct test
162 const char *name;
163 void (*func)(void);
166 extern const struct test winetest_testlist[];
168 /* debug level */
169 int winetest_debug = 1;
171 /* interactive mode? */
172 int winetest_interactive = 0;
174 /* current platform */
175 const char *winetest_platform = "windows";
177 /* report successful tests (BOOL) */
178 static int report_success = 0;
180 /* passing arguments around */
181 static int winetest_argc;
182 static char** winetest_argv;
184 static const struct test *current_test; /* test currently being run */
186 static LONG successes; /* number of successful tests */
187 static LONG failures; /* number of failures */
188 static LONG skipped; /* number of skipped test chunks */
189 static LONG todo_successes; /* number of successful tests inside todo block */
190 static LONG todo_failures; /* number of failures inside todo block */
192 /* The following data must be kept track of on a per-thread basis */
193 typedef struct
195 const char* current_file; /* file of current check */
196 int current_line; /* line of current check */
197 int todo_level; /* current todo nesting level */
198 int todo_do_loop;
199 } tls_data;
200 static DWORD tls_index;
202 static tls_data* get_tls_data(void)
204 tls_data* data;
205 DWORD last_error;
207 last_error=GetLastError();
208 data=TlsGetValue(tls_index);
209 if (!data)
211 data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
212 TlsSetValue(tls_index,data);
214 SetLastError(last_error);
215 return data;
218 static void exit_process( int code )
220 fflush( stdout );
221 ExitProcess( code );
225 void winetest_set_location( const char* file, int line )
227 tls_data* data=get_tls_data();
228 data->current_file=strrchr(file,'/');
229 if (data->current_file==NULL)
230 data->current_file=strrchr(file,'\\');
231 if (data->current_file==NULL)
232 data->current_file=file;
233 else
234 data->current_file++;
235 data->current_line=line;
238 int broken( int condition )
240 return (strcmp(winetest_platform, "windows") == 0) && condition;
244 * Checks condition.
245 * Parameters:
246 * - condition - condition to check;
247 * - msg test description;
248 * - file - test application source code file name of the check
249 * - line - test application source code file line number of the check
250 * Return:
251 * 0 if condition does not have the expected value, 1 otherwise
253 int winetest_vok( int condition, const char *msg, va_list args )
255 tls_data* data=get_tls_data();
257 if (data->todo_level)
259 if (condition)
261 fprintf( stdout, "%s:%d: Test succeeded inside todo block: ",
262 data->current_file, data->current_line );
263 vfprintf(stdout, msg, args);
264 InterlockedIncrement(&todo_failures);
265 return 0;
267 else
269 if (winetest_debug > 0)
271 fprintf( stdout, "%s:%d: Test marked todo: ",
272 data->current_file, data->current_line );
273 vfprintf(stdout, msg, args);
275 InterlockedIncrement(&todo_successes);
276 return 1;
279 else
281 if (!condition)
283 fprintf( stdout, "%s:%d: Test failed: ",
284 data->current_file, data->current_line );
285 vfprintf(stdout, msg, args);
286 InterlockedIncrement(&failures);
287 return 0;
289 else
291 if (report_success)
292 fprintf( stdout, "%s:%d: Test succeeded\n",
293 data->current_file, data->current_line);
294 InterlockedIncrement(&successes);
295 return 1;
300 int winetest_ok( int condition, const char *msg, ... )
302 va_list valist;
303 int rc;
305 va_start(valist, msg);
306 rc=winetest_vok(condition, msg, valist);
307 va_end(valist);
308 return rc;
311 void winetest_trace( const char *msg, ... )
313 va_list valist;
314 tls_data* data=get_tls_data();
316 if (winetest_debug > 0)
318 fprintf( stdout, "%s:%d: ", data->current_file, data->current_line );
319 va_start(valist, msg);
320 vfprintf(stdout, msg, valist);
321 va_end(valist);
325 void winetest_vskip( const char *msg, va_list args )
327 tls_data* data=get_tls_data();
329 fprintf( stdout, "%s:%d: Tests skipped: ", data->current_file, data->current_line );
330 vfprintf(stdout, msg, args);
331 skipped++;
334 void winetest_skip( const char *msg, ... )
336 va_list valist;
337 va_start(valist, msg);
338 winetest_vskip(msg, valist);
339 va_end(valist);
342 void winetest_win_skip( const char *msg, ... )
344 va_list valist;
345 va_start(valist, msg);
346 if (strcmp(winetest_platform, "windows") == 0)
347 winetest_vskip(msg, valist);
348 else
349 winetest_vok(0, msg, valist);
350 va_end(valist);
353 void winetest_start_todo( const char* platform )
355 tls_data* data=get_tls_data();
356 if (strcmp(winetest_platform,platform)==0)
357 data->todo_level++;
358 data->todo_do_loop=1;
361 int winetest_loop_todo(void)
363 tls_data* data=get_tls_data();
364 int do_loop=data->todo_do_loop;
365 data->todo_do_loop=0;
366 return do_loop;
369 void winetest_end_todo( const char* platform )
371 if (strcmp(winetest_platform,platform)==0)
373 tls_data* data=get_tls_data();
374 data->todo_level--;
378 int winetest_get_mainargs( char*** pargv )
380 *pargv = winetest_argv;
381 return winetest_argc;
384 void winetest_wait_child_process( HANDLE process )
386 DWORD exit_code = 1;
388 if (WaitForSingleObject( process, 30000 ))
389 fprintf( stdout, "%s: child process wait failed\n", current_test->name );
390 else
391 GetExitCodeProcess( process, &exit_code );
393 if (exit_code)
395 if (exit_code > 255)
397 fprintf( stdout, "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
398 InterlockedIncrement( &failures );
400 else
402 fprintf( stdout, "%s: %u failures in child process\n",
403 current_test->name, exit_code );
404 while (exit_code-- > 0)
405 InterlockedIncrement(&failures);
410 /* Find a test by name */
411 static const struct test *find_test( const char *name )
413 const struct test *test;
414 const char *p;
415 size_t len;
417 if ((p = strrchr( name, '/' ))) name = p + 1;
418 if ((p = strrchr( name, '\\' ))) name = p + 1;
419 len = strlen(name);
420 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
422 for (test = winetest_testlist; test->name; test++)
424 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
426 return test->name ? test : NULL;
430 /* Display list of valid tests */
431 static void list_tests(void)
433 const struct test *test;
435 fprintf( stdout, "Valid test names:\n" );
436 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
440 /* Run a named test, and return exit status */
441 static int run_test( const char *name )
443 const struct test *test;
444 int status;
446 if (!(test = find_test( name )))
448 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
449 exit_process(1);
451 successes = failures = todo_successes = todo_failures = 0;
452 tls_index=TlsAlloc();
453 current_test = test;
454 test->func();
456 if (winetest_debug)
458 fprintf( stdout, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
459 test->name, successes + failures + todo_successes + todo_failures,
460 todo_successes, failures + todo_failures,
461 (failures + todo_failures != 1) ? "failures" : "failure",
462 skipped );
464 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
465 return status;
469 /* Display usage and exit */
470 static void usage( const char *argv0 )
472 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
473 list_tests();
474 exit_process(1);
478 /* main function */
479 int main( int argc, char **argv )
481 char p[128];
483 setvbuf (stdout, NULL, _IONBF, 0);
485 winetest_argc = argc;
486 winetest_argv = argv;
488 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) )) winetest_platform = strdup(p);
489 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
490 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
491 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
493 if (!argv[1])
495 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
496 return run_test( winetest_testlist[0].name );
497 usage( argv[0] );
499 if (!strcmp( argv[1], "--list" ))
501 list_tests();
502 return 0;
504 return run_test(argv[1]);
507 #endif /* STANDALONE */
509 #endif /* __WINE_WINE_TEST_H */