ole2disp.dll16: Implement SafeArrayGet[UBound|LBound].
[wine.git] / include / wine / test.h
blob4ec66d5391d186edf916b04fa465c9dd867ced0e
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 ); }
71 extern const char *wine_dbgstr_longlong( ULONGLONG ll );
73 /* strcmpW is available for tests compiled under Wine, but not in standalone
74 * builds under Windows, so we reimplement it under a different name. */
75 static inline int winetest_strcmpW( const WCHAR *str1, const WCHAR *str2 )
77 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
78 return *str1 - *str2;
81 #ifdef STANDALONE
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 #else
87 #define START_TEST(name) void func_##name(void)
88 #endif
90 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
91 #define __winetest_cdecl __cdecl
92 #define __winetest_va_list __builtin_ms_va_list
93 #else
94 #define __winetest_cdecl
95 #define __winetest_va_list va_list
96 #endif
98 extern int broken( int condition );
99 extern int winetest_vok( int condition, const char *msg, __winetest_va_list ap );
100 extern void winetest_vskip( const char *msg, __winetest_va_list ap );
102 #ifdef __GNUC__
103 # define WINETEST_PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
104 #else
105 # define WINETEST_PRINTF_ATTR(fmt,args)
106 #endif
108 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... ) WINETEST_PRINTF_ATTR(2,3);
109 extern void __winetest_cdecl winetest_skip( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
110 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
111 extern void __winetest_cdecl winetest_trace( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
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
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__)
123 #define todo_if(is_todo) for (winetest_start_todo(is_todo); \
124 winetest_loop_todo(); \
125 winetest_end_todo())
126 #define todo_wine todo_if(!strcmp(winetest_platform, "wine"))
127 #define todo_wine_if(is_todo) todo_if((is_todo) && !strcmp(winetest_platform, "wine"))
130 #ifdef NONAMELESSUNION
131 # define U(x) (x).u
132 # define U1(x) (x).u1
133 # define U2(x) (x).u2
134 # define U3(x) (x).u3
135 # define U4(x) (x).u4
136 # define U5(x) (x).u5
137 # define U6(x) (x).u6
138 # define U7(x) (x).u7
139 # define U8(x) (x).u8
140 #else
141 # define U(x) (x)
142 # define U1(x) (x)
143 # define U2(x) (x)
144 # define U3(x) (x)
145 # define U4(x) (x)
146 # define U5(x) (x)
147 # define U6(x) (x)
148 # define U7(x) (x)
149 # define U8(x) (x)
150 #endif
152 #ifdef NONAMELESSSTRUCT
153 # define S(x) (x).s
154 # define S1(x) (x).s1
155 # define S2(x) (x).s2
156 # define S3(x) (x).s3
157 # define S4(x) (x).s4
158 # define S5(x) (x).s5
159 #else
160 # define S(x) (x)
161 # define S1(x) (x)
162 # define S2(x) (x)
163 # define S3(x) (x)
164 # define S4(x) (x)
165 # define S5(x) (x)
166 #endif
169 /************************************************************************/
170 /* Below is the implementation of the various functions, to be included
171 * directly into the generated testlist.c file.
172 * It is done that way so that the dlls can build the test routines with
173 * different includes or flags if needed.
176 #ifdef STANDALONE
178 #include <stdio.h>
179 #include <excpt.h>
181 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
182 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
183 # define __winetest_va_end(list) __builtin_ms_va_end(list)
184 #else
185 # define __winetest_va_start(list,arg) va_start(list,arg)
186 # define __winetest_va_end(list) va_end(list)
187 #endif
189 struct test
191 const char *name;
192 void (*func)(void);
195 extern const struct test winetest_testlist[];
197 /* debug level */
198 int winetest_debug = 1;
200 /* interactive mode? */
201 int winetest_interactive = 0;
203 /* current platform */
204 const char *winetest_platform = "windows";
206 /* report successful tests (BOOL) */
207 static int report_success = 0;
209 /* passing arguments around */
210 static int winetest_argc;
211 static char** winetest_argv;
213 static const struct test *current_test; /* test currently being run */
215 static LONG successes; /* number of successful tests */
216 static LONG failures; /* number of failures */
217 static LONG skipped; /* number of skipped test chunks */
218 static LONG todo_successes; /* number of successful tests inside todo block */
219 static LONG todo_failures; /* number of failures inside todo block */
221 /* The following data must be kept track of on a per-thread basis */
222 struct tls_data
224 const char* current_file; /* file of current check */
225 int current_line; /* line of current check */
226 unsigned int todo_level; /* current todo nesting level */
227 int todo_do_loop;
228 char *str_pos; /* position in debug buffer */
229 char strings[2000]; /* buffer for debug strings */
231 static DWORD tls_index;
233 static struct tls_data *get_tls_data(void)
235 struct tls_data *data;
236 DWORD last_error;
238 last_error=GetLastError();
239 data=TlsGetValue(tls_index);
240 if (!data)
242 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
243 data->str_pos = data->strings;
244 TlsSetValue(tls_index,data);
246 SetLastError(last_error);
247 return data;
250 /* allocate some tmp space for a string */
251 static char *get_temp_buffer( size_t n )
253 struct tls_data *data = get_tls_data();
254 char *res = data->str_pos;
256 if (res + n >= &data->strings[sizeof(data->strings)]) res = data->strings;
257 data->str_pos = res + n;
258 return res;
261 /* release extra space that we requested in get_temp_buffer() */
262 static void release_temp_buffer( char *ptr, size_t size )
264 struct tls_data *data = get_tls_data();
265 data->str_pos = ptr + size;
268 static void exit_process( int code )
270 fflush( stdout );
271 ExitProcess( code );
275 void winetest_set_location( const char* file, int line )
277 struct tls_data *data = get_tls_data();
278 data->current_file=strrchr(file,'/');
279 if (data->current_file==NULL)
280 data->current_file=strrchr(file,'\\');
281 if (data->current_file==NULL)
282 data->current_file=file;
283 else
284 data->current_file++;
285 data->current_line=line;
288 int broken( int condition )
290 return (strcmp(winetest_platform, "windows") == 0) && condition;
294 * Checks condition.
295 * Parameters:
296 * - condition - condition to check;
297 * - msg test description;
298 * - file - test application source code file name of the check
299 * - line - test application source code file line number of the check
300 * Return:
301 * 0 if condition does not have the expected value, 1 otherwise
303 int winetest_vok( int condition, const char *msg, __winetest_va_list args )
305 struct tls_data *data = get_tls_data();
307 if (data->todo_level)
309 if (condition)
311 printf( "%s:%d: Test succeeded inside todo block: ",
312 data->current_file, data->current_line );
313 vprintf(msg, args);
314 InterlockedIncrement(&todo_failures);
315 return 0;
317 else
319 if (winetest_debug > 0)
321 printf( "%s:%d: Test marked todo: ",
322 data->current_file, data->current_line );
323 vprintf(msg, args);
325 InterlockedIncrement(&todo_successes);
326 return 1;
329 else
331 if (!condition)
333 printf( "%s:%d: Test failed: ",
334 data->current_file, data->current_line );
335 vprintf(msg, args);
336 InterlockedIncrement(&failures);
337 return 0;
339 else
341 if (report_success)
342 printf( "%s:%d: Test succeeded\n",
343 data->current_file, data->current_line);
344 InterlockedIncrement(&successes);
345 return 1;
350 void __winetest_cdecl winetest_ok( int condition, const char *msg, ... )
352 __winetest_va_list valist;
354 __winetest_va_start(valist, msg);
355 winetest_vok(condition, msg, valist);
356 __winetest_va_end(valist);
359 void __winetest_cdecl winetest_trace( const char *msg, ... )
361 __winetest_va_list valist;
362 struct tls_data *data = get_tls_data();
364 if (winetest_debug > 0)
366 printf( "%s:%d: ", data->current_file, data->current_line );
367 __winetest_va_start(valist, msg);
368 vprintf(msg, valist);
369 __winetest_va_end(valist);
373 void winetest_vskip( const char *msg, __winetest_va_list args )
375 struct tls_data *data = get_tls_data();
377 printf( "%s:%d: Tests skipped: ", data->current_file, data->current_line );
378 vprintf(msg, args);
379 skipped++;
382 void __winetest_cdecl winetest_skip( const char *msg, ... )
384 __winetest_va_list valist;
385 __winetest_va_start(valist, msg);
386 winetest_vskip(msg, valist);
387 __winetest_va_end(valist);
390 void __winetest_cdecl winetest_win_skip( const char *msg, ... )
392 __winetest_va_list valist;
393 __winetest_va_start(valist, msg);
394 if (strcmp(winetest_platform, "windows") == 0)
395 winetest_vskip(msg, valist);
396 else
397 winetest_vok(0, msg, valist);
398 __winetest_va_end(valist);
401 void winetest_start_todo( int is_todo )
403 struct tls_data *data = get_tls_data();
404 data->todo_level = (data->todo_level << 1) | (is_todo != 0);
405 data->todo_do_loop=1;
408 int winetest_loop_todo(void)
410 struct tls_data *data = get_tls_data();
411 int do_loop=data->todo_do_loop;
412 data->todo_do_loop=0;
413 return do_loop;
416 void winetest_end_todo(void)
418 struct tls_data *data = get_tls_data();
419 data->todo_level >>= 1;
422 int winetest_get_mainargs( char*** pargv )
424 *pargv = winetest_argv;
425 return winetest_argc;
428 LONG winetest_get_failures(void)
430 return failures;
433 void winetest_add_failures( LONG new_failures )
435 while (new_failures-- > 0)
436 InterlockedIncrement( &failures );
439 void winetest_wait_child_process( HANDLE process )
441 DWORD exit_code = 1;
443 if (WaitForSingleObject( process, 30000 ))
444 printf( "%s: child process wait failed\n", current_test->name );
445 else
446 GetExitCodeProcess( process, &exit_code );
448 if (exit_code)
450 if (exit_code > 255)
452 printf( "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
453 InterlockedIncrement( &failures );
455 else
457 printf( "%s: %u failures in child process\n",
458 current_test->name, exit_code );
459 while (exit_code-- > 0)
460 InterlockedIncrement(&failures);
465 const char *wine_dbgstr_wn( const WCHAR *str, int n )
467 char *dst, *res;
468 size_t size;
470 if (!((ULONG_PTR)str >> 16))
472 if (!str) return "(null)";
473 res = get_temp_buffer( 6 );
474 sprintf( res, "#%04x", LOWORD(str) );
475 return res;
477 if (n == -1)
479 const WCHAR *end = str;
480 while (*end) end++;
481 n = end - str;
483 if (n < 0) n = 0;
484 size = 12 + min( 300, n * 5 );
485 dst = res = get_temp_buffer( size );
486 *dst++ = 'L';
487 *dst++ = '"';
488 while (n-- > 0 && dst <= res + size - 10)
490 WCHAR c = *str++;
491 switch (c)
493 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
494 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
495 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
496 case '"': *dst++ = '\\'; *dst++ = '"'; break;
497 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
498 default:
499 if (c >= ' ' && c <= 126)
500 *dst++ = c;
501 else
503 *dst++ = '\\';
504 sprintf(dst,"%04x",c);
505 dst+=4;
509 *dst++ = '"';
510 if (n > 0)
512 *dst++ = '.';
513 *dst++ = '.';
514 *dst++ = '.';
516 *dst++ = 0;
517 release_temp_buffer( res, dst - res );
518 return res;
521 const char *wine_dbgstr_guid( const GUID *guid )
523 char *res;
525 if (!guid) return "(null)";
526 res = get_temp_buffer( 39 ); /* CHARS_IN_GUID */
527 sprintf( res, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
528 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
529 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
530 guid->Data4[5], guid->Data4[6], guid->Data4[7] );
531 return res;
534 const char *wine_dbgstr_rect( const RECT *rect )
536 char *res;
538 if (!rect) return "(null)";
539 res = get_temp_buffer( 60 );
540 sprintf( res, "(%d,%d)-(%d,%d)", rect->left, rect->top, rect->right, rect->bottom );
541 release_temp_buffer( res, strlen(res) + 1 );
542 return res;
545 const char *wine_dbgstr_longlong( ULONGLONG ll )
547 char *res;
549 res = get_temp_buffer( 17 );
550 if (sizeof(ll) > sizeof(unsigned long) && ll >> 32)
551 sprintf( res, "%lx%08lx", (unsigned long)(ll >> 32), (unsigned long)ll );
552 else
553 sprintf( res, "%lx", (unsigned long)ll );
554 release_temp_buffer( res, strlen(res) + 1 );
555 return res;
558 /* Find a test by name */
559 static const struct test *find_test( const char *name )
561 const struct test *test;
562 const char *p;
563 size_t len;
565 if ((p = strrchr( name, '/' ))) name = p + 1;
566 if ((p = strrchr( name, '\\' ))) name = p + 1;
567 len = strlen(name);
568 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
570 for (test = winetest_testlist; test->name; test++)
572 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
574 return test->name ? test : NULL;
578 /* Display list of valid tests */
579 static void list_tests(void)
581 const struct test *test;
583 printf( "Valid test names:\n" );
584 for (test = winetest_testlist; test->name; test++)
585 printf( " %s\n", test->name );
589 /* Run a named test, and return exit status */
590 static int run_test( const char *name )
592 const struct test *test;
593 int status;
595 if (!(test = find_test( name )))
597 printf( "Fatal: test '%s' does not exist.\n", name );
598 exit_process(1);
600 successes = failures = todo_successes = todo_failures = 0;
601 tls_index=TlsAlloc();
602 current_test = test;
603 test->func();
605 if (winetest_debug)
607 printf( "%04x:%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
608 GetCurrentProcessId(), test->name,
609 successes + failures + todo_successes + todo_failures,
610 todo_successes, failures + todo_failures,
611 (failures + todo_failures != 1) ? "failures" : "failure",
612 skipped );
614 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
615 return status;
619 /* Display usage and exit */
620 static void usage( const char *argv0 )
622 printf( "Usage: %s test_name\n\n", argv0 );
623 list_tests();
624 exit_process(1);
627 /* trap unhandled exceptions */
628 static LONG CALLBACK exc_filter( EXCEPTION_POINTERS *ptrs )
630 struct tls_data *data = get_tls_data();
632 if (data->current_file)
633 printf( "%s:%d: this is the last test seen before the exception\n",
634 data->current_file, data->current_line );
635 printf( "%04x:%s: unhandled exception %08x at %p\n",
636 GetCurrentProcessId(), current_test->name,
637 ptrs->ExceptionRecord->ExceptionCode, ptrs->ExceptionRecord->ExceptionAddress );
638 fflush( stdout );
639 return EXCEPTION_EXECUTE_HANDLER;
642 /* check if we're running under wine */
643 static BOOL running_under_wine(void)
645 HMODULE module = GetModuleHandleA( "ntdll.dll" );
646 if (!module) return FALSE;
647 return (GetProcAddress( module, "wine_server_call" ) != NULL);
650 #ifdef __GNUC__
651 void _fpreset(void) {} /* override the mingw fpu init code */
652 #endif
654 /* main function */
655 int main( int argc, char **argv )
657 char p[128];
659 setvbuf (stdout, NULL, _IONBF, 0);
661 winetest_argc = argc;
662 winetest_argv = argv;
664 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) ))
665 winetest_platform = strdup(p);
666 else if (running_under_wine())
667 winetest_platform = "wine";
669 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
670 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
671 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
673 if (!strcmp( winetest_platform, "windows" )) SetUnhandledExceptionFilter( exc_filter );
674 if (!winetest_interactive) SetErrorMode( SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX );
676 if (!argv[1])
678 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
679 return run_test( winetest_testlist[0].name );
680 usage( argv[0] );
682 if (!strcmp( argv[1], "--list" ))
684 list_tests();
685 return 0;
687 return run_test(argv[1]);
690 #endif /* STANDALONE */
692 #endif /* __WINE_WINE_TEST_H */