webservices: Implement WsReceiveMessage.
[wine.git] / include / wine / test.h
blobf33ab9ab560693c9f497de3388f264c30731d14f
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( int is_todo );
60 extern int winetest_loop_todo(void);
61 extern void winetest_end_todo(void);
62 extern int winetest_get_mainargs( char*** pargv );
63 extern LONG winetest_get_failures(void);
64 extern void winetest_add_failures( LONG new_failures );
65 extern void winetest_wait_child_process( HANDLE process );
67 extern const char *wine_dbgstr_wn( const WCHAR *str, int n );
68 extern const char *wine_dbgstr_guid( const GUID *guid );
69 extern const char *wine_dbgstr_rect( const RECT *rect );
70 static inline const char *wine_dbgstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
72 /* strcmpW is available 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 )
76 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
77 return *str1 - *str2;
80 #ifdef STANDALONE
81 #define START_TEST(name) \
82 static void func_##name(void); \
83 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
84 static void func_##name(void)
85 #else
86 #define START_TEST(name) void func_##name(void)
87 #endif
89 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
90 #define __winetest_cdecl __cdecl
91 #define __winetest_va_list __builtin_ms_va_list
92 #else
93 #define __winetest_cdecl
94 #define __winetest_va_list va_list
95 #endif
97 extern int broken( int condition );
98 extern int winetest_vok( int condition, const char *msg, __winetest_va_list ap );
99 extern void winetest_vskip( const char *msg, __winetest_va_list ap );
101 #ifdef __GNUC__
102 # define WINETEST_PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
103 #else
104 # define WINETEST_PRINTF_ATTR(fmt,args)
105 #endif
107 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... ) WINETEST_PRINTF_ATTR(2,3);
108 extern void __winetest_cdecl winetest_skip( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
109 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
110 extern void __winetest_cdecl winetest_trace( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
112 #define ok_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_ok
113 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
114 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
115 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
117 #define ok ok_(__FILE__, __LINE__)
118 #define skip skip_(__FILE__, __LINE__)
119 #define win_skip win_skip_(__FILE__, __LINE__)
120 #define trace trace_(__FILE__, __LINE__)
122 #define todo_if(is_todo) for (winetest_start_todo(is_todo); \
123 winetest_loop_todo(); \
124 winetest_end_todo())
125 #define todo_wine todo_if(!strcmp(winetest_platform, "wine"))
126 #define todo_wine_if(is_todo) todo_if((is_todo) && !strcmp(winetest_platform, "wine"))
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
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
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.
175 #ifdef STANDALONE
177 #include <stdio.h>
178 #include <excpt.h>
180 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
181 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
182 # define __winetest_va_end(list) __builtin_ms_va_end(list)
183 #else
184 # define __winetest_va_start(list,arg) va_start(list,arg)
185 # define __winetest_va_end(list) va_end(list)
186 #endif
188 struct test
190 const char *name;
191 void (*func)(void);
194 extern const struct test winetest_testlist[];
196 /* debug level */
197 int winetest_debug = 1;
199 /* interactive mode? */
200 int winetest_interactive = 0;
202 /* current platform */
203 const char *winetest_platform = "windows";
205 /* report successful tests (BOOL) */
206 static int report_success = 0;
208 /* passing arguments around */
209 static int winetest_argc;
210 static char** winetest_argv;
212 static const struct test *current_test; /* test currently being run */
214 static LONG successes; /* number of successful tests */
215 static LONG failures; /* number of failures */
216 static LONG skipped; /* number of skipped test chunks */
217 static LONG todo_successes; /* number of successful tests inside todo block */
218 static LONG todo_failures; /* number of failures inside todo block */
220 /* The following data must be kept track of on a per-thread basis */
221 typedef struct
223 const char* current_file; /* file of current check */
224 int current_line; /* line of current check */
225 unsigned int todo_level; /* current todo nesting level */
226 int todo_do_loop;
227 char *str_pos; /* position in debug buffer */
228 char strings[2000]; /* buffer for debug strings */
229 } tls_data;
230 static DWORD tls_index;
232 static tls_data* get_tls_data(void)
234 tls_data* data;
235 DWORD last_error;
237 last_error=GetLastError();
238 data=TlsGetValue(tls_index);
239 if (!data)
241 data=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(tls_data));
242 data->str_pos = data->strings;
243 TlsSetValue(tls_index,data);
245 SetLastError(last_error);
246 return data;
249 /* allocate some tmp space for a string */
250 static char *get_temp_buffer( size_t n )
252 tls_data *data = get_tls_data();
253 char *res = data->str_pos;
255 if (res + n >= &data->strings[sizeof(data->strings)]) res = data->strings;
256 data->str_pos = res + n;
257 return res;
260 /* release extra space that we requested in get_temp_buffer() */
261 static void release_temp_buffer( char *ptr, size_t size )
263 tls_data *data = get_tls_data();
264 data->str_pos = ptr + size;
267 static void exit_process( int code )
269 fflush( stdout );
270 ExitProcess( code );
274 void winetest_set_location( const char* file, int line )
276 tls_data* data=get_tls_data();
277 data->current_file=strrchr(file,'/');
278 if (data->current_file==NULL)
279 data->current_file=strrchr(file,'\\');
280 if (data->current_file==NULL)
281 data->current_file=file;
282 else
283 data->current_file++;
284 data->current_line=line;
287 int broken( int condition )
289 return (strcmp(winetest_platform, "windows") == 0) && condition;
293 * Checks condition.
294 * Parameters:
295 * - condition - condition to check;
296 * - msg test description;
297 * - file - test application source code file name of the check
298 * - line - test application source code file line number of the check
299 * Return:
300 * 0 if condition does not have the expected value, 1 otherwise
302 int winetest_vok( int condition, const char *msg, __winetest_va_list args )
304 tls_data* data=get_tls_data();
306 if (data->todo_level)
308 if (condition)
310 printf( "%s:%d: Test succeeded inside todo block: ",
311 data->current_file, data->current_line );
312 vprintf(msg, args);
313 InterlockedIncrement(&todo_failures);
314 return 0;
316 else
318 if (winetest_debug > 0)
320 printf( "%s:%d: Test marked todo: ",
321 data->current_file, data->current_line );
322 vprintf(msg, args);
324 InterlockedIncrement(&todo_successes);
325 return 1;
328 else
330 if (!condition)
332 printf( "%s:%d: Test failed: ",
333 data->current_file, data->current_line );
334 vprintf(msg, args);
335 InterlockedIncrement(&failures);
336 return 0;
338 else
340 if (report_success)
341 printf( "%s:%d: Test succeeded\n",
342 data->current_file, data->current_line);
343 InterlockedIncrement(&successes);
344 return 1;
349 void __winetest_cdecl winetest_ok( int condition, const char *msg, ... )
351 __winetest_va_list valist;
353 __winetest_va_start(valist, msg);
354 winetest_vok(condition, msg, valist);
355 __winetest_va_end(valist);
358 void __winetest_cdecl winetest_trace( const char *msg, ... )
360 __winetest_va_list valist;
361 tls_data* data=get_tls_data();
363 if (winetest_debug > 0)
365 printf( "%s:%d: ", data->current_file, data->current_line );
366 __winetest_va_start(valist, msg);
367 vprintf(msg, valist);
368 __winetest_va_end(valist);
372 void winetest_vskip( const char *msg, __winetest_va_list args )
374 tls_data* data=get_tls_data();
376 printf( "%s:%d: Tests skipped: ", data->current_file, data->current_line );
377 vprintf(msg, args);
378 skipped++;
381 void __winetest_cdecl winetest_skip( const char *msg, ... )
383 __winetest_va_list valist;
384 __winetest_va_start(valist, msg);
385 winetest_vskip(msg, valist);
386 __winetest_va_end(valist);
389 void __winetest_cdecl winetest_win_skip( const char *msg, ... )
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);
400 void winetest_start_todo( int is_todo )
402 tls_data* data=get_tls_data();
403 data->todo_level = (data->todo_level << 1) | (is_todo != 0);
404 data->todo_do_loop=1;
407 int winetest_loop_todo(void)
409 tls_data* data=get_tls_data();
410 int do_loop=data->todo_do_loop;
411 data->todo_do_loop=0;
412 return do_loop;
415 void winetest_end_todo(void)
417 tls_data* data=get_tls_data();
418 data->todo_level >>= 1;
421 int winetest_get_mainargs( char*** pargv )
423 *pargv = winetest_argv;
424 return winetest_argc;
427 LONG winetest_get_failures(void)
429 return failures;
432 void winetest_add_failures( LONG new_failures )
434 while (new_failures-- > 0)
435 InterlockedIncrement( &failures );
438 void winetest_wait_child_process( HANDLE process )
440 DWORD exit_code = 1;
442 if (WaitForSingleObject( process, 30000 ))
443 printf( "%s: child process wait failed\n", current_test->name );
444 else
445 GetExitCodeProcess( process, &exit_code );
447 if (exit_code)
449 if (exit_code > 255)
451 printf( "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
452 InterlockedIncrement( &failures );
454 else
456 printf( "%s: %u failures in child process\n",
457 current_test->name, exit_code );
458 while (exit_code-- > 0)
459 InterlockedIncrement(&failures);
464 const char *wine_dbgstr_wn( const WCHAR *str, int n )
466 char *dst, *res;
467 size_t size;
469 if (!((ULONG_PTR)str >> 16))
471 if (!str) return "(null)";
472 res = get_temp_buffer( 6 );
473 sprintf( res, "#%04x", LOWORD(str) );
474 return res;
476 if (n == -1)
478 const WCHAR *end = str;
479 while (*end) end++;
480 n = end - str;
482 if (n < 0) n = 0;
483 size = 12 + min( 300, n * 5 );
484 dst = res = get_temp_buffer( size );
485 *dst++ = 'L';
486 *dst++ = '"';
487 while (n-- > 0 && dst <= res + size - 10)
489 WCHAR c = *str++;
490 switch (c)
492 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
493 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
494 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
495 case '"': *dst++ = '\\'; *dst++ = '"'; break;
496 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
497 default:
498 if (c >= ' ' && c <= 126)
499 *dst++ = c;
500 else
502 *dst++ = '\\';
503 sprintf(dst,"%04x",c);
504 dst+=4;
508 *dst++ = '"';
509 if (n > 0)
511 *dst++ = '.';
512 *dst++ = '.';
513 *dst++ = '.';
515 *dst++ = 0;
516 release_temp_buffer( res, dst - res );
517 return res;
520 const char *wine_dbgstr_guid( const GUID *guid )
522 char *res;
524 if (!guid) return "(null)";
525 res = get_temp_buffer( 39 ); /* CHARS_IN_GUID */
526 sprintf( res, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
527 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
528 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
529 guid->Data4[5], guid->Data4[6], guid->Data4[7] );
530 return res;
533 const char *wine_dbgstr_rect( const RECT *rect )
535 char *res;
537 if (!rect) return "(null)";
538 res = get_temp_buffer( 60 );
539 sprintf( res, "(%d,%d)-(%d,%d)", rect->left, rect->top, rect->right, rect->bottom );
540 release_temp_buffer( res, strlen(res) + 1 );
541 return res;
544 /* Find a test by name */
545 static const struct test *find_test( const char *name )
547 const struct test *test;
548 const char *p;
549 size_t len;
551 if ((p = strrchr( name, '/' ))) name = p + 1;
552 if ((p = strrchr( name, '\\' ))) name = p + 1;
553 len = strlen(name);
554 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
556 for (test = winetest_testlist; test->name; test++)
558 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
560 return test->name ? test : NULL;
564 /* Display list of valid tests */
565 static void list_tests(void)
567 const struct test *test;
569 printf( "Valid test names:\n" );
570 for (test = winetest_testlist; test->name; test++)
571 printf( " %s\n", test->name );
575 /* Run a named test, and return exit status */
576 static int run_test( const char *name )
578 const struct test *test;
579 int status;
581 if (!(test = find_test( name )))
583 printf( "Fatal: test '%s' does not exist.\n", name );
584 exit_process(1);
586 successes = failures = todo_successes = todo_failures = 0;
587 tls_index=TlsAlloc();
588 current_test = test;
589 test->func();
591 if (winetest_debug)
593 printf( "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
594 test->name, successes + failures + todo_successes + todo_failures,
595 todo_successes, failures + todo_failures,
596 (failures + todo_failures != 1) ? "failures" : "failure",
597 skipped );
599 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
600 return status;
604 /* Display usage and exit */
605 static void usage( const char *argv0 )
607 printf( "Usage: %s test_name\n\n", argv0 );
608 list_tests();
609 exit_process(1);
612 /* trap unhandled exceptions */
613 static LONG CALLBACK exc_filter( EXCEPTION_POINTERS *ptrs )
615 tls_data *data = get_tls_data();
617 if (data->current_file)
618 printf( "%s:%d: this is the last test seen before the exception\n",
619 data->current_file, data->current_line );
620 printf( "%s: unhandled exception %08x at %p\n", current_test->name,
621 ptrs->ExceptionRecord->ExceptionCode, ptrs->ExceptionRecord->ExceptionAddress );
622 fflush( stdout );
623 return EXCEPTION_EXECUTE_HANDLER;
626 /* check if we're running under wine */
627 static BOOL running_under_wine(void)
629 HMODULE module = GetModuleHandleA( "ntdll.dll" );
630 if (!module) return FALSE;
631 return (GetProcAddress( module, "wine_server_call" ) != NULL);
634 #ifdef __GNUC__
635 void _fpreset(void) {} /* override the mingw fpu init code */
636 #endif
638 /* main function */
639 int main( int argc, char **argv )
641 char p[128];
643 setvbuf (stdout, NULL, _IONBF, 0);
645 winetest_argc = argc;
646 winetest_argv = argv;
648 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) ))
649 winetest_platform = strdup(p);
650 else if (running_under_wine())
651 winetest_platform = "wine";
653 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
654 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
655 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
657 if (!strcmp( winetest_platform, "windows" )) SetUnhandledExceptionFilter( exc_filter );
658 if (!winetest_interactive) SetErrorMode( SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX );
660 if (!argv[1])
662 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
663 return run_test( winetest_testlist[0].name );
664 usage( argv[0] );
666 if (!strcmp( argv[1], "--list" ))
668 list_tests();
669 return 0;
671 return run_test(argv[1]);
674 #endif /* STANDALONE */
676 #endif /* __WINE_WINE_TEST_H */