stash
[wine/wine64.git] / include / wine / test.h
blobcaa3f35a699e62ae4db91357570c50b5cff6ba83
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 if (msg[0])
265 fprintf(stdout,": ");
266 vfprintf(stdout, msg, args);
268 InterlockedIncrement(&todo_failures);
269 return 0;
271 else InterlockedIncrement(&todo_successes);
273 else
275 if (!condition)
277 fprintf( stdout, "%s:%d: Test failed",
278 data->current_file, data->current_line );
279 if (msg[0])
281 fprintf( stdout,": ");
282 vfprintf(stdout, msg, args);
284 InterlockedIncrement(&failures);
285 return 0;
287 else
289 if (report_success)
290 fprintf( stdout, "%s:%d: Test succeeded\n",
291 data->current_file, data->current_line);
292 InterlockedIncrement(&successes);
295 return 1;
298 int winetest_ok( int condition, const char *msg, ... )
300 va_list valist;
301 int rc;
303 va_start(valist, msg);
304 rc=winetest_vok(condition, msg, valist);
305 va_end(valist);
306 return rc;
309 void winetest_trace( const char *msg, ... )
311 va_list valist;
312 tls_data* data=get_tls_data();
314 if (winetest_debug > 0)
316 fprintf( stdout, "%s:%d: ", data->current_file, data->current_line );
317 va_start(valist, msg);
318 vfprintf(stdout, msg, valist);
319 va_end(valist);
323 void winetest_vskip( const char *msg, va_list args )
325 tls_data* data=get_tls_data();
327 fprintf( stdout, "%s:%d: Tests skipped: ", data->current_file, data->current_line );
328 vfprintf(stdout, msg, args);
329 skipped++;
332 void winetest_skip( const char *msg, ... )
334 va_list valist;
335 va_start(valist, msg);
336 winetest_vskip(msg, valist);
337 va_end(valist);
340 void winetest_win_skip( const char *msg, ... )
342 va_list valist;
343 va_start(valist, msg);
344 if (strcmp(winetest_platform, "windows") == 0)
345 winetest_vskip(msg, valist);
346 else
347 winetest_vok(0, msg, valist);
348 va_end(valist);
351 void winetest_start_todo( const char* platform )
353 tls_data* data=get_tls_data();
354 if (strcmp(winetest_platform,platform)==0)
355 data->todo_level++;
356 data->todo_do_loop=1;
359 int winetest_loop_todo(void)
361 tls_data* data=get_tls_data();
362 int do_loop=data->todo_do_loop;
363 data->todo_do_loop=0;
364 return do_loop;
367 void winetest_end_todo( const char* platform )
369 if (strcmp(winetest_platform,platform)==0)
371 tls_data* data=get_tls_data();
372 data->todo_level--;
376 int winetest_get_mainargs( char*** pargv )
378 *pargv = winetest_argv;
379 return winetest_argc;
382 void winetest_wait_child_process( HANDLE process )
384 DWORD exit_code = 1;
386 if (WaitForSingleObject( process, 30000 ))
387 fprintf( stdout, "%s: child process wait failed\n", current_test->name );
388 else
389 GetExitCodeProcess( process, &exit_code );
391 if (exit_code)
393 if (exit_code > 255)
395 fprintf( stdout, "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
396 InterlockedIncrement( &failures );
398 else
400 fprintf( stdout, "%s: %u failures in child process\n",
401 current_test->name, exit_code );
402 while (exit_code-- > 0)
403 InterlockedIncrement(&failures);
408 /* Find a test by name */
409 static const struct test *find_test( const char *name )
411 const struct test *test;
412 const char *p;
413 size_t len;
415 if ((p = strrchr( name, '/' ))) name = p + 1;
416 if ((p = strrchr( name, '\\' ))) name = p + 1;
417 len = strlen(name);
418 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
420 for (test = winetest_testlist; test->name; test++)
422 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
424 return test->name ? test : NULL;
428 /* Display list of valid tests */
429 static void list_tests(void)
431 const struct test *test;
433 fprintf( stdout, "Valid test names:\n" );
434 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
438 /* Run a named test, and return exit status */
439 static int run_test( const char *name )
441 const struct test *test;
442 int status;
444 if (!(test = find_test( name )))
446 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
447 exit_process(1);
449 successes = failures = todo_successes = todo_failures = 0;
450 tls_index=TlsAlloc();
451 current_test = test;
452 test->func();
454 if (winetest_debug)
456 fprintf( stdout, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
457 test->name, successes + failures + todo_successes + todo_failures,
458 todo_successes, failures + todo_failures,
459 (failures + todo_failures != 1) ? "failures" : "failure",
460 skipped );
462 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
463 return status;
467 /* Display usage and exit */
468 static void usage( const char *argv0 )
470 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
471 list_tests();
472 exit_process(1);
476 /* main function */
477 int main( int argc, char **argv )
479 char p[128];
481 setvbuf (stdout, NULL, _IONBF, 0);
483 winetest_argc = argc;
484 winetest_argv = argv;
486 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) )) winetest_platform = strdup(p);
487 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
488 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
489 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
491 if (!argv[1])
493 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
494 return run_test( winetest_testlist[0].name );
495 usage( argv[0] );
497 if (!strcmp( argv[1], "--list" ))
499 list_tests();
500 return 0;
502 return run_test(argv[1]);
505 #endif /* STANDALONE */
507 #endif /* __WINE_WINE_TEST_H */