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