Warning killing commit
[reactos.git] / reactos / include / reactos / wine / test.h
1 /*
2 * Definitions for Wine C unit tests.
3 *
4 * Copyright (C) 2002 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #ifndef __WINE_WINE_TEST_H
22 #define __WINE_WINE_TEST_H
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <windef.h>
27 #include <winbase.h>
28 #include <wingdi.h>
29 #include <winreg.h>
30
31 #ifdef __WINE_WINE_LIBRARY_H
32 #error wine/library.h should not be used in Wine tests
33 #endif
34 #ifdef __WINE_WINE_UNICODE_H
35 #error wine/unicode.h should not be used in Wine tests
36 #endif
37 #ifdef __WINE_WINE_DEBUG_H
38 #error wine/debug.h should not be used in Wine tests
39 #endif
40
41 #ifndef INVALID_FILE_ATTRIBUTES
42 #define INVALID_FILE_ATTRIBUTES ((DWORD)~0UL)
43 #endif
44 #ifndef INVALID_SET_FILE_POINTER
45 #define INVALID_SET_FILE_POINTER ((DWORD)~0UL)
46 #endif
47
48 /* debug level */
49 extern int winetest_debug;
50
51 /* running in interactive mode? */
52 extern int winetest_interactive;
53
54 /* current platform */
55 extern const char *winetest_platform;
56
57 extern void winetest_set_location( const char* file, int line );
58 extern void winetest_start_todo( const char* platform );
59 extern int winetest_loop_todo(void);
60 extern void winetest_end_todo( const char* platform );
61 extern int winetest_get_mainargs( char*** pargv );
62 extern void winetest_wait_child_process( HANDLE process );
63
64 #ifdef STANDALONE
65 #define START_TEST(name) \
66 static void func_##name(void); \
67 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
68 static void func_##name(void)
69 #else
70 #define START_TEST(name) void func_##name(void)
71 #endif
72
73 extern int broken( int condition );
74 extern int winetest_vok( int condition, const char *msg, va_list ap );
75 extern void winetest_vskip( const char *msg, va_list ap );
76
77 #ifdef __GNUC__
78
79 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
80 extern void winetest_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
81 extern void winetest_win_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
82 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
83
84 #else /* __GNUC__ */
85
86 extern int winetest_ok( int condition, const char *msg, ... );
87 extern void winetest_skip( const char *msg, ... );
88 extern void winetest_win_skip( const char *msg, ... );
89 extern void winetest_trace( const char *msg, ... );
90
91 #endif /* __GNUC__ */
92
93 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
94 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
95 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
96 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
97
98 #define ok ok_(__FILE__, __LINE__)
99 #define skip skip_(__FILE__, __LINE__)
100 #define win_skip win_skip_(__FILE__, __LINE__)
101 #define trace trace_(__FILE__, __LINE__)
102
103 #define todo(platform) for (winetest_start_todo(platform); \
104 winetest_loop_todo(); \
105 winetest_end_todo(platform))
106 #define todo_wine todo("wine")
107
108
109 #ifdef NONAMELESSUNION
110 # define U(x) (x).u
111 # define U1(x) (x).u1
112 # define U2(x) (x).u2
113 # define U3(x) (x).u3
114 # define U4(x) (x).u4
115 # define U5(x) (x).u5
116 # define U6(x) (x).u6
117 # define U7(x) (x).u7
118 # define U8(x) (x).u8
119 #else
120 # define U(x) (x)
121 # define U1(x) (x)
122 # define U2(x) (x)
123 # define U3(x) (x)
124 # define U4(x) (x)
125 # define U5(x) (x)
126 # define U6(x) (x)
127 # define U7(x) (x)
128 # define U8(x) (x)
129 #endif
130
131 #ifdef NONAMELESSSTRUCT
132 # define S(x) (x).s
133 # define S1(x) (x).s1
134 # define S2(x) (x).s2
135 # define S3(x) (x).s3
136 # define S4(x) (x).s4
137 # define S5(x) (x).s5
138 #else
139 # define S(x) (x)
140 # define S1(x) (x)
141 # define S2(x) (x)
142 # define S3(x) (x)
143 # define S4(x) (x)
144 # define S5(x) (x)
145 #endif
146
147
148 /************************************************************************/
149 /* Below is the implementation of the various functions, to be included
150 * directly into the generated testlist.c file.
151 * It is done that way so that the dlls can build the test routines with
152 * different includes or flags if needed.
153 */
154
155 #ifdef STANDALONE
156
157 #include <stdio.h>
158
159 struct test
160 {
161 const char *name;
162 void (*func)(void);
163 };
164
165 extern const struct test winetest_testlist[];
166
167 /* debug level */
168 int winetest_debug = 1;
169
170 /* interactive mode? */
171 int winetest_interactive = 0;
172
173 /* current platform */
174 const char *winetest_platform = "windows";
175
176 /* report successful tests (BOOL) */
177 static int report_success = 0;
178
179 /* passing arguments around */
180 static int winetest_argc;
181 static char** winetest_argv;
182
183 static const struct test *current_test; /* test currently being run */
184
185 static LONG successes; /* number of successful tests */
186 static LONG failures; /* number of failures */
187 static LONG skipped; /* number of skipped test chunks */
188 static LONG todo_successes; /* number of successful tests inside todo block */
189 static LONG todo_failures; /* number of failures inside todo block */
190
191 /* The following data must be kept track of on a per-thread basis */
192 typedef struct
193 {
194 const char* current_file; /* file of current check */
195 int current_line; /* line of current check */
196 int todo_level; /* current todo nesting level */
197 int todo_do_loop;
198 } tls_data;
199 static DWORD tls_index;
200
201 static tls_data* get_tls_data(void)
202 {
203 tls_data* data;
204 DWORD last_error;
205
206 last_error=GetLastError();
207 data=TlsGetValue(tls_index);
208 if (!data)
209 {
210 data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
211 TlsSetValue(tls_index,data);
212 }
213 SetLastError(last_error);
214 return data;
215 }
216
217 static void exit_process( int code )
218 {
219 fflush( stdout );
220 ExitProcess( code );
221 }
222
223
224 void winetest_set_location( const char* file, int line )
225 {
226 tls_data* data=get_tls_data();
227 data->current_file=strrchr(file,'/');
228 if (data->current_file==NULL)
229 data->current_file=strrchr(file,'\\');
230 if (data->current_file==NULL)
231 data->current_file=file;
232 else
233 data->current_file++;
234 data->current_line=line;
235 }
236
237 int broken( int condition )
238 {
239 return (strcmp(winetest_platform, "windows") == 0) && condition;
240 }
241
242 /*
243 * Checks condition.
244 * Parameters:
245 * - condition - condition to check;
246 * - msg test description;
247 * - file - test application source code file name of the check
248 * - line - test application source code file line number of the check
249 * Return:
250 * 0 if condition does not have the expected value, 1 otherwise
251 */
252 int winetest_vok( int condition, const char *msg, va_list args )
253 {
254 tls_data* data=get_tls_data();
255
256 if (data->todo_level)
257 {
258 if (condition)
259 {
260 fprintf( stdout, "%s:%d: Test succeeded inside todo block",
261 data->current_file, data->current_line );
262 if (msg[0])
263 {
264 fprintf(stdout,": ");
265 vfprintf(stdout, msg, args);
266 }
267 InterlockedIncrement(&todo_failures);
268 return 0;
269 }
270 else InterlockedIncrement(&todo_successes);
271 }
272 else
273 {
274 if (!condition)
275 {
276 fprintf( stdout, "%s:%d: Test failed",
277 data->current_file, data->current_line );
278 if (msg[0])
279 {
280 fprintf( stdout,": ");
281 vfprintf(stdout, msg, args);
282 }
283 InterlockedIncrement(&failures);
284 return 0;
285 }
286 else
287 {
288 if (report_success)
289 fprintf( stdout, "%s:%d: Test succeeded\n",
290 data->current_file, data->current_line);
291 InterlockedIncrement(&successes);
292 }
293 }
294 return 1;
295 }
296
297 int winetest_ok( int condition, const char *msg, ... )
298 {
299 va_list valist;
300 int rc;
301
302 va_start(valist, msg);
303 rc=winetest_vok(condition, msg, valist);
304 va_end(valist);
305 return rc;
306 }
307
308 void winetest_trace( const char *msg, ... )
309 {
310 va_list valist;
311 tls_data* data=get_tls_data();
312
313 if (winetest_debug > 0)
314 {
315 fprintf( stdout, "%s:%d: ", data->current_file, data->current_line );
316 va_start(valist, msg);
317 vfprintf(stdout, msg, valist);
318 va_end(valist);
319 }
320 }
321
322 void winetest_vskip( const char *msg, va_list args )
323 {
324 tls_data* data=get_tls_data();
325
326 fprintf( stdout, "%s:%d: Tests skipped: ", data->current_file, data->current_line );
327 vfprintf(stdout, msg, args);
328 skipped++;
329 }
330
331 void winetest_skip( const char *msg, ... )
332 {
333 va_list valist;
334 va_start(valist, msg);
335 winetest_vskip(msg, valist);
336 va_end(valist);
337 }
338
339 void winetest_win_skip( const char *msg, ... )
340 {
341 va_list valist;
342 va_start(valist, msg);
343 if (strcmp(winetest_platform, "windows") == 0)
344 winetest_vskip(msg, valist);
345 else
346 winetest_vok(0, msg, valist);
347 va_end(valist);
348 }
349
350 void winetest_start_todo( const char* platform )
351 {
352 tls_data* data=get_tls_data();
353 if (strcmp(winetest_platform,platform)==0)
354 data->todo_level++;
355 data->todo_do_loop=1;
356 }
357
358 int winetest_loop_todo(void)
359 {
360 tls_data* data=get_tls_data();
361 int do_loop=data->todo_do_loop;
362 data->todo_do_loop=0;
363 return do_loop;
364 }
365
366 void winetest_end_todo( const char* platform )
367 {
368 if (strcmp(winetest_platform,platform)==0)
369 {
370 tls_data* data=get_tls_data();
371 data->todo_level--;
372 }
373 }
374
375 int winetest_get_mainargs( char*** pargv )
376 {
377 *pargv = winetest_argv;
378 return winetest_argc;
379 }
380
381 void winetest_wait_child_process( HANDLE process )
382 {
383 DWORD exit_code = 1;
384
385 if (WaitForSingleObject( process, 30000 ))
386 fprintf( stdout, "%s: child process wait failed\n", current_test->name );
387 else
388 GetExitCodeProcess( process, &exit_code );
389
390 if (exit_code)
391 {
392 if (exit_code > 255)
393 {
394 fprintf( stdout, "%s: exception 0x%08x in child process\n", current_test->name, (unsigned int)exit_code );
395 InterlockedIncrement( &failures );
396 }
397 else
398 {
399 fprintf( stdout, "%s: %u failures in child process\n",
400 current_test->name, (unsigned int)exit_code );
401 while (exit_code-- > 0)
402 InterlockedIncrement(&failures);
403 }
404 }
405 }
406
407 /* Find a test by name */
408 static const struct test *find_test( const char *name )
409 {
410 const struct test *test;
411 const char *p;
412 size_t len;
413
414 if ((p = strrchr( name, '/' ))) name = p + 1;
415 if ((p = strrchr( name, '\\' ))) name = p + 1;
416 len = strlen(name);
417 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
418
419 for (test = winetest_testlist; test->name; test++)
420 {
421 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
422 }
423 return test->name ? test : NULL;
424 }
425
426
427 /* Display list of valid tests */
428 static void list_tests(void)
429 {
430 const struct test *test;
431
432 fprintf( stdout, "Valid test names:\n" );
433 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
434 }
435
436
437 /* Run a named test, and return exit status */
438 static int run_test( const char *name )
439 {
440 const struct test *test;
441 int status;
442
443 if (!(test = find_test( name )))
444 {
445 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
446 exit_process(1);
447 }
448 successes = failures = todo_successes = todo_failures = 0;
449 tls_index=TlsAlloc();
450 current_test = test;
451 test->func();
452
453 if (winetest_debug)
454 {
455 fprintf( stdout, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
456 test->name, (int)(successes + failures + todo_successes + todo_failures),
457 (int)todo_successes, (int)(failures + todo_failures),
458 (failures + todo_failures != 1) ? "failures" : "failure",
459 (int)skipped );
460 }
461 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
462 return status;
463 }
464
465
466 /* Display usage and exit */
467 static void usage( const char *argv0 )
468 {
469 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
470 list_tests();
471 exit_process(1);
472 }
473
474
475 /* main function */
476 int main( int argc, char **argv )
477 {
478 char *p;
479
480 setvbuf (stdout, NULL, _IONBF, 0);
481
482 winetest_argc = argc;
483 winetest_argv = argv;
484
485 if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = _strdup(p);
486 if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
487 if ((p = getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive = atoi(p);
488 if ((p = getenv( "WINETEST_REPORT_SUCCESS"))) report_success = atoi(p);
489 if (!argv[1])
490 {
491 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
492 return run_test( winetest_testlist[0].name );
493 usage( argv[0] );
494 }
495 if (!strcmp( argv[1], "--list" ))
496 {
497 list_tests();
498 return 0;
499 }
500 return run_test(argv[1]);
501 }
502
503 #endif /* STANDALONE */
504
505 #endif /* __WINE_WINE_TEST_H */