d2d1: Implement d2d_state_block_GetFactory().
[wine.git] / include / wine / test.h
blob567229a7c6799b823c19595471f8c3c9f5eb4927
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( 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 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 static inline const char *wine_dbgstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
71 /* strcmpW is available for tests compiled under Wine, but not in standalone
72 * builds under Windows, so we reimplement it under a different name. */
73 static inline int winetest_strcmpW( const WCHAR *str1, const WCHAR *str2 )
75 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
76 return *str1 - *str2;
79 #ifdef STANDALONE
80 #define START_TEST(name) \
81 static void func_##name(void); \
82 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
83 static void func_##name(void)
84 #else
85 #define START_TEST(name) void func_##name(void)
86 #endif
88 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
89 #define __winetest_cdecl __cdecl
90 #define __winetest_va_list __builtin_ms_va_list
91 #else
92 #define __winetest_cdecl
93 #define __winetest_va_list va_list
94 #endif
96 extern int broken( int condition );
97 extern int winetest_vok( int condition, const char *msg, __winetest_va_list ap );
98 extern void winetest_vskip( const char *msg, __winetest_va_list ap );
100 #ifdef __GNUC__
102 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
103 extern void __winetest_cdecl winetest_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
104 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
105 extern void __winetest_cdecl winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
107 #else /* __GNUC__ */
109 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... );
110 extern void __winetest_cdecl winetest_skip( const char *msg, ... );
111 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... );
112 extern void __winetest_cdecl winetest_trace( const char *msg, ... );
114 #endif /* __GNUC__ */
116 #define ok_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_ok
117 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
118 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
119 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
121 #define ok ok_(__FILE__, __LINE__)
122 #define skip skip_(__FILE__, __LINE__)
123 #define win_skip win_skip_(__FILE__, __LINE__)
124 #define trace trace_(__FILE__, __LINE__)
126 #define todo(platform) for (winetest_start_todo(platform); \
127 winetest_loop_todo(); \
128 winetest_end_todo(platform))
129 #define todo_wine todo("wine")
132 #ifdef NONAMELESSUNION
133 # define U(x) (x).u
134 # define U1(x) (x).u1
135 # define U2(x) (x).u2
136 # define U3(x) (x).u3
137 # define U4(x) (x).u4
138 # define U5(x) (x).u5
139 # define U6(x) (x).u6
140 # define U7(x) (x).u7
141 # define U8(x) (x).u8
142 #else
143 # define U(x) (x)
144 # define U1(x) (x)
145 # define U2(x) (x)
146 # define U3(x) (x)
147 # define U4(x) (x)
148 # define U5(x) (x)
149 # define U6(x) (x)
150 # define U7(x) (x)
151 # define U8(x) (x)
152 #endif
154 #ifdef NONAMELESSSTRUCT
155 # define S(x) (x).s
156 # define S1(x) (x).s1
157 # define S2(x) (x).s2
158 # define S3(x) (x).s3
159 # define S4(x) (x).s4
160 # define S5(x) (x).s5
161 #else
162 # define S(x) (x)
163 # define S1(x) (x)
164 # define S2(x) (x)
165 # define S3(x) (x)
166 # define S4(x) (x)
167 # define S5(x) (x)
168 #endif
171 /************************************************************************/
172 /* Below is the implementation of the various functions, to be included
173 * directly into the generated testlist.c file.
174 * It is done that way so that the dlls can build the test routines with
175 * different includes or flags if needed.
178 #ifdef STANDALONE
180 #include <stdio.h>
181 #include <excpt.h>
183 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
184 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
185 # define __winetest_va_end(list) __builtin_ms_va_end(list)
186 #else
187 # define __winetest_va_start(list,arg) va_start(list,arg)
188 # define __winetest_va_end(list) va_end(list)
189 #endif
191 struct test
193 const char *name;
194 void (*func)(void);
197 extern const struct test winetest_testlist[];
199 /* debug level */
200 int winetest_debug = 1;
202 /* interactive mode? */
203 int winetest_interactive = 0;
205 /* current platform */
206 const char *winetest_platform = "windows";
208 /* report successful tests (BOOL) */
209 static int report_success = 0;
211 /* passing arguments around */
212 static int winetest_argc;
213 static char** winetest_argv;
215 static const struct test *current_test; /* test currently being run */
217 static LONG successes; /* number of successful tests */
218 static LONG failures; /* number of failures */
219 static LONG skipped; /* number of skipped test chunks */
220 static LONG todo_successes; /* number of successful tests inside todo block */
221 static LONG todo_failures; /* number of failures inside todo block */
223 /* The following data must be kept track of on a per-thread basis */
224 typedef struct
226 const char* current_file; /* file of current check */
227 int current_line; /* line of current check */
228 int todo_level; /* current todo nesting level */
229 int todo_do_loop;
230 char *str_pos; /* position in debug buffer */
231 char strings[2000]; /* buffer for debug strings */
232 } tls_data;
233 static DWORD tls_index;
235 static tls_data* get_tls_data(void)
237 tls_data* data;
238 DWORD last_error;
240 last_error=GetLastError();
241 data=TlsGetValue(tls_index);
242 if (!data)
244 data=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(tls_data));
245 data->str_pos = data->strings;
246 TlsSetValue(tls_index,data);
248 SetLastError(last_error);
249 return data;
252 /* allocate some tmp space for a string */
253 static char *get_temp_buffer( size_t n )
255 tls_data *data = get_tls_data();
256 char *res = data->str_pos;
258 if (res + n >= &data->strings[sizeof(data->strings)]) res = data->strings;
259 data->str_pos = res + n;
260 return res;
263 /* release extra space that we requested in get_temp_buffer() */
264 static void release_temp_buffer( char *ptr, size_t size )
266 tls_data *data = get_tls_data();
267 data->str_pos = ptr + size;
270 static void exit_process( int code )
272 fflush( stdout );
273 ExitProcess( code );
277 void winetest_set_location( const char* file, int line )
279 tls_data* data=get_tls_data();
280 data->current_file=strrchr(file,'/');
281 if (data->current_file==NULL)
282 data->current_file=strrchr(file,'\\');
283 if (data->current_file==NULL)
284 data->current_file=file;
285 else
286 data->current_file++;
287 data->current_line=line;
290 int broken( int condition )
292 return (strcmp(winetest_platform, "windows") == 0) && condition;
296 * Checks condition.
297 * Parameters:
298 * - condition - condition to check;
299 * - msg test description;
300 * - file - test application source code file name of the check
301 * - line - test application source code file line number of the check
302 * Return:
303 * 0 if condition does not have the expected value, 1 otherwise
305 int winetest_vok( int condition, const char *msg, __winetest_va_list args )
307 tls_data* data=get_tls_data();
309 if (data->todo_level)
311 if (condition)
313 fprintf( stdout, "%s:%d: Test succeeded inside todo block: ",
314 data->current_file, data->current_line );
315 vfprintf(stdout, msg, args);
316 InterlockedIncrement(&todo_failures);
317 return 0;
319 else
321 if (winetest_debug > 0)
323 fprintf( stdout, "%s:%d: Test marked todo: ",
324 data->current_file, data->current_line );
325 vfprintf(stdout, msg, args);
327 InterlockedIncrement(&todo_successes);
328 return 1;
331 else
333 if (!condition)
335 fprintf( stdout, "%s:%d: Test failed: ",
336 data->current_file, data->current_line );
337 vfprintf(stdout, msg, args);
338 InterlockedIncrement(&failures);
339 return 0;
341 else
343 if (report_success)
344 fprintf( stdout, "%s:%d: Test succeeded\n",
345 data->current_file, data->current_line);
346 InterlockedIncrement(&successes);
347 return 1;
352 void __winetest_cdecl winetest_ok( int condition, const char *msg, ... )
354 __winetest_va_list valist;
356 __winetest_va_start(valist, msg);
357 winetest_vok(condition, msg, valist);
358 __winetest_va_end(valist);
361 void __winetest_cdecl winetest_trace( const char *msg, ... )
363 __winetest_va_list valist;
364 tls_data* data=get_tls_data();
366 if (winetest_debug > 0)
368 fprintf( stdout, "%s:%d: ", data->current_file, data->current_line );
369 __winetest_va_start(valist, msg);
370 vfprintf(stdout, msg, valist);
371 __winetest_va_end(valist);
375 void winetest_vskip( const char *msg, __winetest_va_list args )
377 tls_data* data=get_tls_data();
379 fprintf( stdout, "%s:%d: Tests skipped: ", data->current_file, data->current_line );
380 vfprintf(stdout, msg, args);
381 skipped++;
384 void __winetest_cdecl winetest_skip( const char *msg, ... )
386 __winetest_va_list valist;
387 __winetest_va_start(valist, msg);
388 winetest_vskip(msg, valist);
389 __winetest_va_end(valist);
392 void __winetest_cdecl winetest_win_skip( const char *msg, ... )
394 __winetest_va_list valist;
395 __winetest_va_start(valist, msg);
396 if (strcmp(winetest_platform, "windows") == 0)
397 winetest_vskip(msg, valist);
398 else
399 winetest_vok(0, msg, valist);
400 __winetest_va_end(valist);
403 void winetest_start_todo( const char* platform )
405 tls_data* data=get_tls_data();
406 if (strcmp(winetest_platform,platform)==0)
407 data->todo_level++;
408 data->todo_do_loop=1;
411 int winetest_loop_todo(void)
413 tls_data* data=get_tls_data();
414 int do_loop=data->todo_do_loop;
415 data->todo_do_loop=0;
416 return do_loop;
419 void winetest_end_todo( const char* platform )
421 if (strcmp(winetest_platform,platform)==0)
423 tls_data* data=get_tls_data();
424 data->todo_level--;
428 int winetest_get_mainargs( char*** pargv )
430 *pargv = winetest_argv;
431 return winetest_argc;
434 LONG winetest_get_failures(void)
436 return failures;
439 void winetest_add_failures( LONG new_failures )
441 while (new_failures-- > 0)
442 InterlockedIncrement( &failures );
445 void winetest_wait_child_process( HANDLE process )
447 DWORD exit_code = 1;
449 if (WaitForSingleObject( process, 30000 ))
450 fprintf( stdout, "%s: child process wait failed\n", current_test->name );
451 else
452 GetExitCodeProcess( process, &exit_code );
454 if (exit_code)
456 if (exit_code > 255)
458 fprintf( stdout, "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
459 InterlockedIncrement( &failures );
461 else
463 fprintf( stdout, "%s: %u failures in child process\n",
464 current_test->name, exit_code );
465 while (exit_code-- > 0)
466 InterlockedIncrement(&failures);
471 const char *wine_dbgstr_wn( const WCHAR *str, int n )
473 char *dst, *res;
474 size_t size;
476 if (!((ULONG_PTR)str >> 16))
478 if (!str) return "(null)";
479 res = get_temp_buffer( 6 );
480 sprintf( res, "#%04x", LOWORD(str) );
481 return res;
483 if (n == -1)
485 const WCHAR *end = str;
486 while (*end) end++;
487 n = end - str;
489 if (n < 0) n = 0;
490 size = 12 + min( 300, n * 5 );
491 dst = res = get_temp_buffer( size );
492 *dst++ = 'L';
493 *dst++ = '"';
494 while (n-- > 0 && dst <= res + size - 10)
496 WCHAR c = *str++;
497 switch (c)
499 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
500 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
501 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
502 case '"': *dst++ = '\\'; *dst++ = '"'; break;
503 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
504 default:
505 if (c >= ' ' && c <= 126)
506 *dst++ = c;
507 else
509 *dst++ = '\\';
510 sprintf(dst,"%04x",c);
511 dst+=4;
515 *dst++ = '"';
516 if (n > 0)
518 *dst++ = '.';
519 *dst++ = '.';
520 *dst++ = '.';
522 *dst++ = 0;
523 release_temp_buffer( res, dst - res );
524 return res;
527 const char *wine_dbgstr_guid( const GUID *guid )
529 char *res;
531 if (!guid) return "(null)";
532 res = get_temp_buffer( 39 ); /* CHARS_IN_GUID */
533 sprintf( res, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
534 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
535 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
536 guid->Data4[5], guid->Data4[6], guid->Data4[7] );
537 return res;
540 /* Find a test by name */
541 static const struct test *find_test( const char *name )
543 const struct test *test;
544 const char *p;
545 size_t len;
547 if ((p = strrchr( name, '/' ))) name = p + 1;
548 if ((p = strrchr( name, '\\' ))) name = p + 1;
549 len = strlen(name);
550 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
552 for (test = winetest_testlist; test->name; test++)
554 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
556 return test->name ? test : NULL;
560 /* Display list of valid tests */
561 static void list_tests(void)
563 const struct test *test;
565 fprintf( stdout, "Valid test names:\n" );
566 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
570 /* Run a named test, and return exit status */
571 static int run_test( const char *name )
573 const struct test *test;
574 int status;
576 if (!(test = find_test( name )))
578 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
579 exit_process(1);
581 successes = failures = todo_successes = todo_failures = 0;
582 tls_index=TlsAlloc();
583 current_test = test;
584 test->func();
586 if (winetest_debug)
588 fprintf( stdout, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
589 test->name, successes + failures + todo_successes + todo_failures,
590 todo_successes, failures + todo_failures,
591 (failures + todo_failures != 1) ? "failures" : "failure",
592 skipped );
594 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
595 return status;
599 /* Display usage and exit */
600 static void usage( const char *argv0 )
602 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
603 list_tests();
604 exit_process(1);
607 /* trap unhandled exceptions */
608 static LONG CALLBACK exc_filter( EXCEPTION_POINTERS *ptrs )
610 tls_data *data = get_tls_data();
612 if (data->current_file)
613 fprintf( stdout, "%s:%d: this is the last test seen before the exception\n",
614 data->current_file, data->current_line );
615 fprintf( stdout, "%s: unhandled exception %08x at %p\n", current_test->name,
616 ptrs->ExceptionRecord->ExceptionCode, ptrs->ExceptionRecord->ExceptionAddress );
617 fflush( stdout );
618 return EXCEPTION_EXECUTE_HANDLER;
621 /* check if we're running under wine */
622 static BOOL running_under_wine(void)
624 HMODULE module = GetModuleHandleA( "ntdll.dll" );
625 if (!module) return FALSE;
626 return (GetProcAddress( module, "wine_server_call" ) != NULL);
629 #ifdef __GNUC__
630 void _fpreset(void) {} /* override the mingw fpu init code */
631 #endif
633 /* main function */
634 int main( int argc, char **argv )
636 char p[128];
638 setvbuf (stdout, NULL, _IONBF, 0);
640 winetest_argc = argc;
641 winetest_argv = argv;
643 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) ))
644 winetest_platform = strdup(p);
645 else if (running_under_wine())
646 winetest_platform = "wine";
648 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
649 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
650 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
652 if (!strcmp( winetest_platform, "windows" )) SetUnhandledExceptionFilter( exc_filter );
653 if (!winetest_interactive) SetErrorMode( SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX );
655 if (!argv[1])
657 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
658 return run_test( winetest_testlist[0].name );
659 usage( argv[0] );
661 if (!strcmp( argv[1], "--list" ))
663 list_tests();
664 return 0;
666 return run_test(argv[1]);
669 #endif /* STANDALONE */
671 #endif /* __WINE_WINE_TEST_H */