Synchronize with trunk revision 59636 (just before Alex's CreateProcess revamp).
[reactos.git] / 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
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
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 #ifndef INVALID_FILE_ATTRIBUTES
47 #define INVALID_FILE_ATTRIBUTES (~0u)
48 #endif
49 #ifndef INVALID_SET_FILE_POINTER
50 #define INVALID_SET_FILE_POINTER (~0u)
51 #endif
52
53 /* debug level */
54 extern int winetest_debug;
55
56 /* running in interactive mode? */
57 extern int winetest_interactive;
58
59 /* current platform */
60 extern const char *winetest_platform;
61
62 extern void winetest_set_location( const char* file, int line );
63 extern void winetest_start_todo( const char* platform );
64 extern int winetest_loop_todo(void);
65 extern void winetest_end_todo( const char* platform );
66 extern int winetest_get_mainargs( char*** pargv );
67 extern void winetest_wait_child_process( HANDLE process );
68
69 extern const char *wine_dbgstr_wn( const WCHAR *str, int n );
70 static inline const char *wine_dbgstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
71
72 /* strcmpW is avaiable for tests compiled under Wine, but not in standalone
73 * builds under Windows, so we reimplement it under a different name. */
74 static inline int winetest_strcmpW( const WCHAR *str1, const WCHAR *str2 )
75 {
76 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
77 return *str1 - *str2;
78 }
79
80 #ifdef STANDALONE
81
82 #define START_TEST(name) \
83 static void func_##name(void); \
84 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
85 static void func_##name(void)
86
87 #else /* STANDALONE */
88
89 #ifdef __cplusplus
90 #define START_TEST(name) extern "C" void func_##name(void)
91 #else
92 #define START_TEST(name) void func_##name(void)
93 #endif
94
95 #endif /* STANDALONE */
96
97 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
98 #define __winetest_cdecl __cdecl
99 #define __winetest_va_list __builtin_ms_va_list
100 #else
101 #define __winetest_cdecl
102 #define __winetest_va_list va_list
103 #endif
104
105 extern int broken( int condition );
106 extern int winetest_vok( int condition, const char *msg, __winetest_va_list ap );
107 extern void winetest_vskip( const char *msg, __winetest_va_list ap );
108
109 #ifdef __GNUC__
110
111 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
112 extern void __winetest_cdecl winetest_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
113 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
114 extern void __winetest_cdecl winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
115
116 #else /* __GNUC__ */
117
118 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... );
119 extern void __winetest_cdecl winetest_skip( const char *msg, ... );
120 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... );
121 extern void __winetest_cdecl winetest_trace( const char *msg, ... );
122
123 #endif /* __GNUC__ */
124
125 #define ok_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_ok
126 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
127 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
128 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
129
130 #define ok ok_(__FILE__, __LINE__)
131 #define skip skip_(__FILE__, __LINE__)
132 #define win_skip win_skip_(__FILE__, __LINE__)
133 #define trace trace_(__FILE__, __LINE__)
134
135 #define todo(platform) for (winetest_start_todo(platform); \
136 winetest_loop_todo(); \
137 winetest_end_todo(platform))
138 #define todo_wine todo("wine")
139
140
141 #ifdef NONAMELESSUNION
142 # define U(x) (x).u
143 # define U1(x) (x).u1
144 # define U2(x) (x).u2
145 # define U3(x) (x).u3
146 # define U4(x) (x).u4
147 # define U5(x) (x).u5
148 # define U6(x) (x).u6
149 # define U7(x) (x).u7
150 # define U8(x) (x).u8
151 #else
152 # define U(x) (x)
153 # define U1(x) (x)
154 # define U2(x) (x)
155 # define U3(x) (x)
156 # define U4(x) (x)
157 # define U5(x) (x)
158 # define U6(x) (x)
159 # define U7(x) (x)
160 # define U8(x) (x)
161 #endif
162
163 #ifdef NONAMELESSSTRUCT
164 # define S(x) (x).s
165 # define S1(x) (x).s1
166 # define S2(x) (x).s2
167 # define S3(x) (x).s3
168 # define S4(x) (x).s4
169 # define S5(x) (x).s5
170 #else
171 # define S(x) (x)
172 # define S1(x) (x)
173 # define S2(x) (x)
174 # define S3(x) (x)
175 # define S4(x) (x)
176 # define S5(x) (x)
177 #endif
178
179
180 /************************************************************************/
181 /* Below is the implementation of the various functions, to be included
182 * directly into the generated testlist.c file.
183 * It is done that way so that the dlls can build the test routines with
184 * different includes or flags if needed.
185 */
186
187 #ifdef STANDALONE
188
189 #include <stdio.h>
190
191 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
192 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
193 # define __winetest_va_end(list) __builtin_ms_va_end(list)
194 #else
195 # define __winetest_va_start(list,arg) va_start(list,arg)
196 # define __winetest_va_end(list) va_end(list)
197 #endif
198
199 struct test
200 {
201 const char *name;
202 void (*func)(void);
203 };
204
205 extern const struct test winetest_testlist[];
206
207 /* debug level */
208 int winetest_debug = 1;
209
210 /* interactive mode? */
211 int winetest_interactive = 0;
212
213 /* current platform */
214 const char *winetest_platform = "windows";
215
216 /* report successful tests (BOOL) */
217 static int report_success = 0;
218
219 /* passing arguments around */
220 static int winetest_argc;
221 static char** winetest_argv;
222
223 static const struct test *current_test; /* test currently being run */
224
225 static LONG successes; /* number of successful tests */
226 static LONG failures; /* number of failures */
227 static LONG skipped; /* number of skipped test chunks */
228 static LONG todo_successes; /* number of successful tests inside todo block */
229 static LONG todo_failures; /* number of failures inside todo block */
230
231 /* The following data must be kept track of on a per-thread basis */
232 typedef struct
233 {
234 const char* current_file; /* file of current check */
235 int current_line; /* line of current check */
236 int todo_level; /* current todo nesting level */
237 int todo_do_loop;
238 char *str_pos; /* position in debug buffer */
239 char strings[2000]; /* buffer for debug strings */
240 } tls_data;
241 static DWORD tls_index;
242
243 static tls_data* get_tls_data(void)
244 {
245 tls_data* data;
246 DWORD last_error;
247
248 last_error=GetLastError();
249 data=TlsGetValue(tls_index);
250 if (!data)
251 {
252 data=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(tls_data));
253 data->str_pos = data->strings;
254 TlsSetValue(tls_index,data);
255 }
256 SetLastError(last_error);
257 return data;
258 }
259
260 /* allocate some tmp space for a string */
261 static char *get_temp_buffer( size_t n )
262 {
263 tls_data *data = get_tls_data();
264 char *res = data->str_pos;
265
266 if (res + n >= &data->strings[sizeof(data->strings)]) res = data->strings;
267 data->str_pos = res + n;
268 return res;
269 }
270
271 /* release extra space that we requested in gimme1() */
272 static void release_temp_buffer( char *ptr, size_t size )
273 {
274 tls_data *data = get_tls_data();
275 data->str_pos = ptr + size;
276 }
277
278 static void exit_process( int code )
279 {
280 fflush( stdout );
281 ExitProcess( code );
282 }
283
284
285 void winetest_set_location( const char* file, int line )
286 {
287 tls_data* data=get_tls_data();
288 data->current_file=strrchr(file,'/');
289 if (data->current_file==NULL)
290 data->current_file=strrchr(file,'\\');
291 if (data->current_file==NULL)
292 data->current_file=file;
293 else
294 data->current_file++;
295 data->current_line=line;
296 }
297
298 int broken( int condition )
299 {
300 return (strcmp(winetest_platform, "windows") == 0) && condition;
301 }
302
303 /*
304 * Checks condition.
305 * Parameters:
306 * - condition - condition to check;
307 * - msg test description;
308 * - file - test application source code file name of the check
309 * - line - test application source code file line number of the check
310 * Return:
311 * 0 if condition does not have the expected value, 1 otherwise
312 */
313 int winetest_vok( int condition, const char *msg, __winetest_va_list args )
314 {
315 tls_data* data=get_tls_data();
316
317 if (data->todo_level)
318 {
319 if (condition)
320 {
321 fprintf( stdout, "%s:%d: Test succeeded inside todo block: ",
322 data->current_file, data->current_line );
323 vfprintf(stdout, msg, args);
324 InterlockedIncrement(&todo_failures);
325 return 0;
326 }
327 else
328 {
329 /* show todos even if traces are disabled*/
330 /*if (winetest_debug > 0)*/
331 {
332 fprintf( stdout, "%s:%d: Test marked todo: ",
333 data->current_file, data->current_line );
334 vfprintf(stdout, msg, args);
335 }
336 InterlockedIncrement(&todo_successes);
337 return 1;
338 }
339 }
340 else
341 {
342 if (!condition)
343 {
344 fprintf( stdout, "%s:%d: Test failed: ",
345 data->current_file, data->current_line );
346 vfprintf(stdout, msg, args);
347 InterlockedIncrement(&failures);
348 return 0;
349 }
350 else
351 {
352 if (report_success)
353 fprintf( stdout, "%s:%d: Test succeeded\n",
354 data->current_file, data->current_line);
355 InterlockedIncrement(&successes);
356 return 1;
357 }
358 }
359 }
360
361 void __winetest_cdecl winetest_ok( int condition, const char *msg, ... )
362 {
363 __winetest_va_list valist;
364
365 __winetest_va_start(valist, msg);
366 winetest_vok(condition, msg, valist);
367 __winetest_va_end(valist);
368 }
369
370 void __winetest_cdecl winetest_trace( const char *msg, ... )
371 {
372 __winetest_va_list valist;
373 tls_data* data=get_tls_data();
374
375 if (winetest_debug > 0)
376 {
377 fprintf( stdout, "%s:%d: ", data->current_file, data->current_line );
378 __winetest_va_start(valist, msg);
379 vfprintf(stdout, msg, valist);
380 __winetest_va_end(valist);
381 }
382 }
383
384 void winetest_vskip( const char *msg, __winetest_va_list args )
385 {
386 tls_data* data=get_tls_data();
387
388 fprintf( stdout, "%s:%d: Tests skipped: ", data->current_file, data->current_line );
389 vfprintf(stdout, msg, args);
390 skipped++;
391 }
392
393 void __winetest_cdecl winetest_skip( const char *msg, ... )
394 {
395 __winetest_va_list valist;
396 __winetest_va_start(valist, msg);
397 winetest_vskip(msg, valist);
398 __winetest_va_end(valist);
399 }
400
401 void __winetest_cdecl winetest_win_skip( const char *msg, ... )
402 {
403 __winetest_va_list valist;
404 __winetest_va_start(valist, msg);
405 if (strcmp(winetest_platform, "windows") == 0)
406 winetest_vskip(msg, valist);
407 else
408 winetest_vok(0, msg, valist);
409 __winetest_va_end(valist);
410 }
411
412 void winetest_start_todo( const char* platform )
413 {
414 tls_data* data=get_tls_data();
415 if (strcmp(winetest_platform,platform)==0)
416 data->todo_level++;
417 data->todo_do_loop=1;
418 }
419
420 int winetest_loop_todo(void)
421 {
422 tls_data* data=get_tls_data();
423 int do_loop=data->todo_do_loop;
424 data->todo_do_loop=0;
425 return do_loop;
426 }
427
428 void winetest_end_todo( const char* platform )
429 {
430 if (strcmp(winetest_platform,platform)==0)
431 {
432 tls_data* data=get_tls_data();
433 data->todo_level--;
434 }
435 }
436
437 int winetest_get_mainargs( char*** pargv )
438 {
439 *pargv = winetest_argv;
440 return winetest_argc;
441 }
442
443 void winetest_wait_child_process( HANDLE process )
444 {
445 DWORD exit_code = 1;
446
447 if (WaitForSingleObject( process, 30000 ))
448 fprintf( stdout, "%s: child process wait failed\n", current_test->name );
449 else
450 GetExitCodeProcess( process, &exit_code );
451
452 if (exit_code)
453 {
454 if (exit_code > 255)
455 {
456 fprintf( stdout, "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
457 InterlockedIncrement( &failures );
458 }
459 else
460 {
461 fprintf( stdout, "%s: %u failures in child process\n",
462 current_test->name, exit_code );
463 while (exit_code-- > 0)
464 InterlockedIncrement(&failures);
465 }
466 }
467 }
468
469 const char *wine_dbgstr_wn( const WCHAR *str, int n )
470 {
471 char *dst, *res;
472 size_t size;
473
474 if (!((ULONG_PTR)str >> 16))
475 {
476 if (!str) return "(null)";
477 res = get_temp_buffer( 6 );
478 sprintf( res, "#%04x", LOWORD(str) );
479 return res;
480 }
481 if (n == -1)
482 {
483 const WCHAR *end = str;
484 while (*end) end++;
485 n = end - str;
486 }
487 if (n < 0) n = 0;
488 size = 12 + min( 300, n * 5 );
489 dst = res = get_temp_buffer( size );
490 *dst++ = 'L';
491 *dst++ = '"';
492 while (n-- > 0 && dst <= res + size - 10)
493 {
494 WCHAR c = *str++;
495 switch (c)
496 {
497 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
498 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
499 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
500 case '"': *dst++ = '\\'; *dst++ = '"'; break;
501 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
502 default:
503 if (c >= ' ' && c <= 126)
504 *dst++ = (char)c;
505 else
506 {
507 *dst++ = '\\';
508 sprintf(dst,"%04x",c);
509 dst+=4;
510 }
511 }
512 }
513 *dst++ = '"';
514 if (n > 0)
515 {
516 *dst++ = '.';
517 *dst++ = '.';
518 *dst++ = '.';
519 }
520 *dst++ = 0;
521 release_temp_buffer( res, dst - res );
522 return res;
523 }
524
525 /* Find a test by name */
526 static const struct test *find_test( const char *name )
527 {
528 const struct test *test;
529 const char *p;
530 size_t len;
531
532 if ((p = strrchr( name, '/' ))) name = p + 1;
533 if ((p = strrchr( name, '\\' ))) name = p + 1;
534 len = strlen(name);
535 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
536
537 for (test = winetest_testlist; test->name; test++)
538 {
539 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
540 }
541 return test->name ? test : NULL;
542 }
543
544
545 /* Display list of valid tests */
546 static void list_tests(void)
547 {
548 const struct test *test;
549
550 fprintf( stdout, "Valid test names:\n" );
551 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
552 }
553
554 /* Disable false-positive claiming "test" would be NULL-dereferenced */
555 #if defined(_MSC_VER)
556 #pragma warning(push)
557 #pragma warning(disable:28182)
558 #endif
559
560 /* Run a named test, and return exit status */
561 static int run_test( const char *name )
562 {
563 const struct test *test;
564 int status;
565
566 if (!(test = find_test( name )))
567 {
568 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
569 exit_process(1);
570 }
571 successes = failures = todo_successes = todo_failures = 0;
572 tls_index=TlsAlloc();
573 current_test = test;
574 test->func();
575
576 /* show test results even if traces are disabled */
577 /*if (winetest_debug)*/
578 {
579 fprintf( stdout, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
580 test->name, successes + failures + todo_successes + todo_failures,
581 todo_successes, failures + todo_failures,
582 (failures + todo_failures != 1) ? "failures" : "failure",
583 skipped );
584 }
585 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
586 return status;
587 }
588
589 #if defined(_MSC_VER)
590 #pragma warning(pop)
591 #endif
592
593 /* Display usage and exit */
594 static void usage( const char *argv0 )
595 {
596 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
597 list_tests();
598 exit_process(1);
599 }
600
601
602 /* main function */
603 int main( int argc, char **argv )
604 {
605 char p[128];
606
607 setvbuf (stdout, NULL, _IONBF, 0);
608
609 winetest_argc = argc;
610 winetest_argv = argv;
611
612 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) )) winetest_platform = _strdup(p);
613 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
614 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
615 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
616
617 if (!winetest_interactive) SetErrorMode( SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX );
618
619 if (!argv[1])
620 {
621 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
622 return run_test( winetest_testlist[0].name );
623 usage( argv[0] );
624 }
625 if (!strcmp( argv[1], "--list" ))
626 {
627 list_tests();
628 return 0;
629 }
630 return run_test(argv[1]);
631 }
632
633 #endif /* STANDALONE */
634
635 // hack for ntdll winetest (this is defined in excpt.h)
636 #undef exception_info
637
638 // Some helpful definitions
639
640 #define ok_hex(expression, result) \
641 do { \
642 int _value = (expression); \
643 ok(_value == (result), "Wrong value for '%s', expected: " #result " (0x%x), got: 0x%x\n", \
644 #expression, (int)(result), _value); \
645 } while (0)
646
647 #define ok_dec(expression, result) \
648 do { \
649 int _value = (expression); \
650 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%d), got: %d\n", \
651 #expression, (int)(result), _value); \
652 } while (0)
653
654 #define ok_ptr(expression, result) \
655 do { \
656 void *_value = (expression); \
657 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%p), got: %p\n", \
658 #expression, (void*)(result), _value); \
659 } while (0)
660
661 #define ok_size_t(expression, result) \
662 do { \
663 size_t _value = (expression); \
664 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%Ix), got: %Ix\n", \
665 #expression, (size_t)(result), _value); \
666 } while (0)
667
668 #define ok_char(expression, result) ok_hex(expression, result)
669
670 #define ok_err(error) \
671 ok(GetLastError() == (error), "Wrong last error. Expected " #error ", got %d\n", (int)GetLastError())
672
673 #define ok_str(x, y) \
674 ok(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x)
675
676 #define ok_wstr(x, y) \
677 ok(wcscmp(x, y) == 0, "Wrong string. Expected '%S', got '%S'\n", y, x)
678
679 #define ok_long(expression, result) ok_hex(expression, result)
680 #define ok_int(expression, result) ok_dec(expression, result)
681 #define ok_ntstatus(status, expected) ok_hex(status, expected)
682 #define ok_hdl ok_ptr
683
684 #ifdef __cplusplus
685 } /* extern "C" */
686 #endif
687
688 #endif /* __WINE_WINE_TEST_H */