push a18c97e3310ae68c9cf1d09430f25ca292f320eb
[wine/hacks.git] / include / wine / test.h
blob32850ceec84b91404b61910ed429b9c569b30f70
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_WINE_LIBRARY_H
30 #error wine/library.h should not be used in Wine tests
31 #endif
32 #ifdef __WINE_WINE_UNICODE_H
33 #error wine/unicode.h should not be used in Wine tests
34 #endif
35 #ifdef __WINE_WINE_DEBUG_H
36 #error wine/debug.h should not be used in Wine tests
37 #endif
39 #ifndef INVALID_FILE_ATTRIBUTES
40 #define INVALID_FILE_ATTRIBUTES ((DWORD)~0UL)
41 #endif
42 #ifndef INVALID_SET_FILE_POINTER
43 #define INVALID_SET_FILE_POINTER ((DWORD)~0UL)
44 #endif
46 /* debug level */
47 extern int winetest_debug;
49 /* running in interactive mode? */
50 extern int winetest_interactive;
52 /* current platform */
53 extern const char *winetest_platform;
55 extern void winetest_set_location( const char* file, int line );
56 extern void winetest_start_todo( const char* platform );
57 extern int winetest_loop_todo(void);
58 extern void winetest_end_todo( const char* platform );
59 extern int winetest_get_mainargs( char*** pargv );
60 extern void winetest_wait_child_process( HANDLE process );
62 #ifdef STANDALONE
63 #define START_TEST(name) \
64 static void func_##name(void); \
65 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
66 static void func_##name(void)
67 #else
68 #define START_TEST(name) void func_##name(void)
69 #endif
71 #ifdef __GNUC__
73 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
74 extern void winetest_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
75 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
77 #else /* __GNUC__ */
79 extern int winetest_ok( int condition, const char *msg, ... );
80 extern void winetest_skip( const char *msg, ... );
81 extern void winetest_trace( const char *msg, ... );
83 #endif /* __GNUC__ */
85 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
86 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
87 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
89 #define ok ok_(__FILE__, __LINE__)
90 #define skip skip_(__FILE__, __LINE__)
91 #define trace trace_(__FILE__, __LINE__)
93 #define todo(platform) for (winetest_start_todo(platform); \
94 winetest_loop_todo(); \
95 winetest_end_todo(platform))
96 #define todo_wine todo("wine")
99 #ifdef NONAMELESSUNION
100 # define U(x) (x).u
101 # define U1(x) (x).u1
102 # define U2(x) (x).u2
103 # define U3(x) (x).u3
104 # define U4(x) (x).u4
105 # define U5(x) (x).u5
106 # define U6(x) (x).u6
107 # define U7(x) (x).u7
108 # define U8(x) (x).u8
109 #else
110 # define U(x) (x)
111 # define U1(x) (x)
112 # define U2(x) (x)
113 # define U3(x) (x)
114 # define U4(x) (x)
115 # define U5(x) (x)
116 # define U6(x) (x)
117 # define U7(x) (x)
118 # define U8(x) (x)
119 #endif
121 #ifdef NONAMELESSSTRUCT
122 # define S(x) (x).s
123 # define S1(x) (x).s1
124 # define S2(x) (x).s2
125 # define S3(x) (x).s3
126 # define S4(x) (x).s4
127 # define S5(x) (x).s5
128 #else
129 # define S(x) (x)
130 # define S1(x) (x)
131 # define S2(x) (x)
132 # define S3(x) (x)
133 # define S4(x) (x)
134 # define S5(x) (x)
135 #endif
138 /************************************************************************/
139 /* Below is the implementation of the various functions, to be included
140 * directly into the generated testlist.c file.
141 * It is done that way so that the dlls can build the test routines with
142 * different includes or flags if needed.
145 #ifdef STANDALONE
147 #include <stdio.h>
149 struct test
151 const char *name;
152 void (*func)(void);
155 extern const struct test winetest_testlist[];
157 /* debug level */
158 int winetest_debug = 1;
160 /* interactive mode? */
161 int winetest_interactive = 0;
163 /* current platform */
164 const char *winetest_platform = "windows";
166 /* report successful tests (BOOL) */
167 static int report_success = 0;
169 /* passing arguments around */
170 static int winetest_argc;
171 static char** winetest_argv;
173 static const struct test *current_test; /* test currently being run */
175 static LONG successes; /* number of successful tests */
176 static LONG failures; /* number of failures */
177 static LONG skipped; /* number of skipped test chunks */
178 static LONG todo_successes; /* number of successful tests inside todo block */
179 static LONG todo_failures; /* number of failures inside todo block */
181 /* The following data must be kept track of on a per-thread basis */
182 typedef struct
184 const char* current_file; /* file of current check */
185 int current_line; /* line of current check */
186 int todo_level; /* current todo nesting level */
187 int todo_do_loop;
188 } tls_data;
189 static DWORD tls_index;
191 static tls_data* get_tls_data(void)
193 tls_data* data;
194 DWORD last_error;
196 last_error=GetLastError();
197 data=TlsGetValue(tls_index);
198 if (!data)
200 data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
201 TlsSetValue(tls_index,data);
203 SetLastError(last_error);
204 return data;
207 static void exit_process( int code )
209 fflush( stdout );
210 ExitProcess( code );
214 void winetest_set_location( const char* file, int line )
216 tls_data* data=get_tls_data();
217 data->current_file=strrchr(file,'/');
218 if (data->current_file==NULL)
219 data->current_file=strrchr(file,'\\');
220 if (data->current_file==NULL)
221 data->current_file=file;
222 else
223 data->current_file++;
224 data->current_line=line;
228 * Checks condition.
229 * Parameters:
230 * - condition - condition to check;
231 * - msg test description;
232 * - file - test application source code file name of the check
233 * - line - test application source code file line number of the check
234 * Return:
235 * 0 if condition does not have the expected value, 1 otherwise
237 int winetest_ok( int condition, const char *msg, ... )
239 va_list valist;
240 tls_data* data=get_tls_data();
242 if (data->todo_level)
244 if (condition)
246 fprintf( stdout, "%s:%d: Test succeeded inside todo block",
247 data->current_file, data->current_line );
248 if (msg[0])
250 va_start(valist, msg);
251 fprintf(stdout,": ");
252 vfprintf(stdout, msg, valist);
253 va_end(valist);
255 InterlockedIncrement(&todo_failures);
256 return 0;
258 else InterlockedIncrement(&todo_successes);
260 else
262 if (!condition)
264 fprintf( stdout, "%s:%d: Test failed",
265 data->current_file, data->current_line );
266 if (msg[0])
268 va_start(valist, msg);
269 fprintf( stdout,": ");
270 vfprintf(stdout, msg, valist);
271 va_end(valist);
273 InterlockedIncrement(&failures);
274 return 0;
276 else
278 if (report_success)
279 fprintf( stdout, "%s:%d: Test succeeded\n",
280 data->current_file, data->current_line);
281 InterlockedIncrement(&successes);
284 return 1;
287 void winetest_trace( const char *msg, ... )
289 va_list valist;
290 tls_data* data=get_tls_data();
292 if (winetest_debug > 0)
294 fprintf( stdout, "%s:%d:", data->current_file, data->current_line );
295 va_start(valist, msg);
296 vfprintf(stdout, msg, valist);
297 va_end(valist);
301 void winetest_skip( const char *msg, ... )
303 va_list valist;
304 tls_data* data=get_tls_data();
306 fprintf( stdout, "%s:%d: Tests skipped: ", data->current_file, data->current_line );
307 va_start(valist, msg);
308 vfprintf(stdout, msg, valist);
309 va_end(valist);
310 skipped++;
313 void winetest_start_todo( const char* platform )
315 tls_data* data=get_tls_data();
316 if (strcmp(winetest_platform,platform)==0)
317 data->todo_level++;
318 data->todo_do_loop=1;
321 int winetest_loop_todo(void)
323 tls_data* data=get_tls_data();
324 int do_loop=data->todo_do_loop;
325 data->todo_do_loop=0;
326 return do_loop;
329 void winetest_end_todo( const char* platform )
331 if (strcmp(winetest_platform,platform)==0)
333 tls_data* data=get_tls_data();
334 data->todo_level--;
338 int winetest_get_mainargs( char*** pargv )
340 *pargv = winetest_argv;
341 return winetest_argc;
344 void winetest_wait_child_process( HANDLE process )
346 DWORD exit_code = 1;
348 if (WaitForSingleObject( process, 30000 ))
349 fprintf( stdout, "%s: child process wait failed\n", current_test->name );
350 else
351 GetExitCodeProcess( process, &exit_code );
353 if (exit_code)
355 fprintf( stdout, "%s: %u failures in child process\n",
356 current_test->name, exit_code );
357 InterlockedExchangeAdd( &failures, exit_code );
361 /* Find a test by name */
362 static const struct test *find_test( const char *name )
364 const struct test *test;
365 const char *p;
366 size_t len;
368 if ((p = strrchr( name, '/' ))) name = p + 1;
369 if ((p = strrchr( name, '\\' ))) name = p + 1;
370 len = strlen(name);
371 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
373 for (test = winetest_testlist; test->name; test++)
375 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
377 return test->name ? test : NULL;
381 /* Display list of valid tests */
382 static void list_tests(void)
384 const struct test *test;
386 fprintf( stdout, "Valid test names:\n" );
387 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
391 /* Run a named test, and return exit status */
392 static int run_test( const char *name )
394 const struct test *test;
395 int status;
397 if (!(test = find_test( name )))
399 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
400 exit_process(1);
402 successes = failures = todo_successes = todo_failures = 0;
403 tls_index=TlsAlloc();
404 current_test = test;
405 test->func();
407 if (winetest_debug)
409 fprintf( stdout, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
410 test->name, successes + failures + todo_successes + todo_failures,
411 todo_successes, failures + todo_failures,
412 (failures + todo_failures != 1) ? "failures" : "failure",
413 skipped );
415 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
416 return status;
420 /* Display usage and exit */
421 static void usage( const char *argv0 )
423 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
424 list_tests();
425 exit_process(1);
429 /* main function */
430 int main( int argc, char **argv )
432 char *p;
434 setvbuf (stdout, NULL, _IONBF, 0);
436 winetest_argc = argc;
437 winetest_argv = argv;
439 if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = strdup(p);
440 if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
441 if ((p = getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive = atoi(p);
442 if ((p = getenv( "WINETEST_REPORT_SUCCESS"))) report_success = atoi(p);
443 if (!argv[1])
445 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
446 return run_test( winetest_testlist[0].name );
447 usage( argv[0] );
449 if (!strcmp( argv[1], "--list" ))
451 list_tests();
452 return 0;
454 return run_test(argv[1]);
457 #endif /* STANDALONE */
459 #endif /* __WINE_WINE_TEST_H */