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
29 #ifdef __WINE_CONFIG_H
30 #error config.h should not be used in Wine tests
32 #ifdef __WINE_WINE_LIBRARY_H
33 #error wine/library.h should not be used in Wine tests
35 #ifdef __WINE_WINE_UNICODE_H
36 #error wine/unicode.h should not be used in Wine tests
38 #ifdef __WINE_WINE_DEBUG_H
39 #error wine/debug.h should not be used in Wine tests
42 #ifndef INVALID_FILE_ATTRIBUTES
43 #define INVALID_FILE_ATTRIBUTES (~0u)
45 #ifndef INVALID_SET_FILE_POINTER
46 #define INVALID_SET_FILE_POINTER (~0u)
50 extern int winetest_debug
;
52 /* running in interactive mode? */
53 extern int winetest_interactive
;
55 /* report successful tests (BOOL) */
56 extern int winetest_report_success
;
58 /* current platform */
59 extern const char *winetest_platform
;
61 extern void winetest_set_location( const char* file
, int line
);
62 extern void winetest_start_todo( int is_todo
);
63 extern int winetest_loop_todo(void);
64 extern void winetest_end_todo(void);
65 extern int winetest_get_mainargs( char*** pargv
);
66 extern LONG
winetest_get_failures(void);
67 extern void winetest_add_failures( LONG new_failures
);
68 extern void winetest_wait_child_process( HANDLE process
);
70 extern const char *wine_dbgstr_wn( const WCHAR
*str
, int n
);
71 extern const char *wine_dbgstr_guid( const GUID
*guid
);
72 extern const char *wine_dbgstr_rect( const RECT
*rect
);
73 static inline const char *wine_dbgstr_w( const WCHAR
*s
) { return wine_dbgstr_wn( s
, -1 ); }
74 extern const char *wine_dbgstr_longlong( ULONGLONG ll
);
76 /* strcmpW is available for tests compiled under Wine, but not in standalone
77 * builds under Windows, so we reimplement it under a different name. */
78 static inline int winetest_strcmpW( const WCHAR
*str1
, const WCHAR
*str2
)
80 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
85 #define START_TEST(name) \
86 static void func_##name(void); \
87 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
88 static void func_##name(void)
90 #define START_TEST(name) void func_##name(void)
93 #if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
94 #define __winetest_cdecl __cdecl
95 #define __winetest_va_list __builtin_ms_va_list
97 #define __winetest_cdecl
98 #define __winetest_va_list va_list
101 extern int broken( int condition
);
102 extern int winetest_vok( int condition
, const char *msg
, __winetest_va_list ap
);
103 extern void winetest_vskip( const char *msg
, __winetest_va_list ap
);
106 # define WINETEST_PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
108 # define WINETEST_PRINTF_ATTR(fmt,args)
111 extern void __winetest_cdecl
winetest_ok( int condition
, const char *msg
, ... ) WINETEST_PRINTF_ATTR(2,3);
112 extern void __winetest_cdecl
winetest_skip( const char *msg
, ... ) WINETEST_PRINTF_ATTR(1,2);
113 extern void __winetest_cdecl
winetest_win_skip( const char *msg
, ... ) WINETEST_PRINTF_ATTR(1,2);
114 extern void __winetest_cdecl
winetest_trace( const char *msg
, ... ) WINETEST_PRINTF_ATTR(1,2);
116 #ifdef WINETEST_NO_LINE_NUMBERS
117 # define ok_(file, line) (winetest_set_location(file, 0), 0) ? (void)0 : winetest_ok
118 # define skip_(file, line) (winetest_set_location(file, 0), 0) ? (void)0 : winetest_skip
119 # define win_skip_(file, line) (winetest_set_location(file, 0), 0) ? (void)0 : winetest_win_skip
120 # define trace_(file, line) (winetest_set_location(file, 0), 0) ? (void)0 : winetest_trace
122 # define ok_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_ok
123 # define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
124 # define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
125 # define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
128 #define ok ok_(__FILE__, __LINE__)
129 #define skip skip_(__FILE__, __LINE__)
130 #define win_skip win_skip_(__FILE__, __LINE__)
131 #define trace trace_(__FILE__, __LINE__)
133 #define todo_if(is_todo) for (winetest_start_todo(is_todo); \
134 winetest_loop_todo(); \
136 #define todo_wine todo_if(!strcmp(winetest_platform, "wine"))
137 #define todo_wine_if(is_todo) todo_if((is_todo) && !strcmp(winetest_platform, "wine"))
141 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
144 #ifdef NONAMELESSUNION
146 # define U1(x) (x).u1
147 # define U2(x) (x).u2
148 # define U3(x) (x).u3
149 # define U4(x) (x).u4
150 # define U5(x) (x).u5
151 # define U6(x) (x).u6
152 # define U7(x) (x).u7
153 # define U8(x) (x).u8
166 #ifdef NONAMELESSSTRUCT
168 # define S1(x) (x).s1
169 # define S2(x) (x).s2
170 # define S3(x) (x).s3
171 # define S4(x) (x).s4
172 # define S5(x) (x).s5
183 /************************************************************************/
184 /* Below is the implementation of the various functions, to be included
185 * directly into the generated testlist.c file.
186 * It is done that way so that the dlls can build the test routines with
187 * different includes or flags if needed.
195 #if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
196 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
197 # define __winetest_va_end(list) __builtin_ms_va_end(list)
199 # define __winetest_va_start(list,arg) va_start(list,arg)
200 # define __winetest_va_end(list) va_end(list)
209 extern const struct test winetest_testlist
[];
212 int winetest_debug
= 1;
214 /* interactive mode? */
215 int winetest_interactive
= 0;
217 /* current platform */
218 const char *winetest_platform
= "windows";
220 /* report successful tests (BOOL) */
221 int winetest_report_success
= 0;
223 /* passing arguments around */
224 static int winetest_argc
;
225 static char** winetest_argv
;
227 static const struct test
*current_test
; /* test currently being run */
229 static LONG successes
; /* number of successful tests */
230 static LONG failures
; /* number of failures */
231 static LONG skipped
; /* number of skipped test chunks */
232 static LONG todo_successes
; /* number of successful tests inside todo block */
233 static LONG todo_failures
; /* number of failures inside todo block */
235 /* The following data must be kept track of on a per-thread basis */
238 const char* current_file
; /* file of current check */
239 int current_line
; /* line of current check */
240 unsigned int todo_level
; /* current todo nesting level */
242 char *str_pos
; /* position in debug buffer */
243 char strings
[2000]; /* buffer for debug strings */
245 static DWORD tls_index
;
247 static struct tls_data
*get_tls_data(void)
249 struct tls_data
*data
;
252 last_error
=GetLastError();
253 data
=TlsGetValue(tls_index
);
256 data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
));
257 data
->str_pos
= data
->strings
;
258 TlsSetValue(tls_index
,data
);
260 SetLastError(last_error
);
264 /* allocate some tmp space for a string */
265 static char *get_temp_buffer( size_t n
)
267 struct tls_data
*data
= get_tls_data();
268 char *res
= data
->str_pos
;
270 if (res
+ n
>= &data
->strings
[sizeof(data
->strings
)]) res
= data
->strings
;
271 data
->str_pos
= res
+ n
;
275 /* release extra space that we requested in get_temp_buffer() */
276 static void release_temp_buffer( char *ptr
, size_t size
)
278 struct tls_data
*data
= get_tls_data();
279 data
->str_pos
= ptr
+ size
;
282 static void exit_process( int code
)
289 void winetest_set_location( const char* file
, int line
)
291 struct tls_data
*data
= get_tls_data();
292 data
->current_file
=strrchr(file
,'/');
293 if (data
->current_file
==NULL
)
294 data
->current_file
=strrchr(file
,'\\');
295 if (data
->current_file
==NULL
)
296 data
->current_file
=file
;
298 data
->current_file
++;
299 data
->current_line
=line
;
302 int broken( int condition
)
304 return (strcmp(winetest_platform
, "windows") == 0) && condition
;
310 * - condition - condition to check;
311 * - msg test description;
312 * - file - test application source code file name of the check
313 * - line - test application source code file line number of the check
315 * 0 if condition does not have the expected value, 1 otherwise
317 int winetest_vok( int condition
, const char *msg
, __winetest_va_list args
)
319 struct tls_data
*data
= get_tls_data();
321 if (data
->todo_level
)
325 printf( "%s:%d: Test succeeded inside todo block: ",
326 data
->current_file
, data
->current_line
);
328 InterlockedIncrement(&todo_failures
);
333 if (winetest_debug
> 0)
335 printf( "%s:%d: Test marked todo: ",
336 data
->current_file
, data
->current_line
);
339 InterlockedIncrement(&todo_successes
);
347 printf( "%s:%d: Test failed: ",
348 data
->current_file
, data
->current_line
);
350 InterlockedIncrement(&failures
);
355 if (winetest_report_success
)
356 printf( "%s:%d: Test succeeded\n",
357 data
->current_file
, data
->current_line
);
358 InterlockedIncrement(&successes
);
364 void __winetest_cdecl
winetest_ok( int condition
, const char *msg
, ... )
366 __winetest_va_list valist
;
368 __winetest_va_start(valist
, msg
);
369 winetest_vok(condition
, msg
, valist
);
370 __winetest_va_end(valist
);
373 void __winetest_cdecl
winetest_trace( const char *msg
, ... )
375 __winetest_va_list valist
;
376 struct tls_data
*data
= get_tls_data();
378 if (winetest_debug
> 0)
380 printf( "%s:%d: ", data
->current_file
, data
->current_line
);
381 __winetest_va_start(valist
, msg
);
382 vprintf(msg
, valist
);
383 __winetest_va_end(valist
);
387 void winetest_vskip( const char *msg
, __winetest_va_list args
)
389 struct tls_data
*data
= get_tls_data();
391 printf( "%s:%d: Tests skipped: ", data
->current_file
, data
->current_line
);
396 void __winetest_cdecl
winetest_skip( const char *msg
, ... )
398 __winetest_va_list valist
;
399 __winetest_va_start(valist
, msg
);
400 winetest_vskip(msg
, valist
);
401 __winetest_va_end(valist
);
404 void __winetest_cdecl
winetest_win_skip( const char *msg
, ... )
406 __winetest_va_list valist
;
407 __winetest_va_start(valist
, msg
);
408 if (strcmp(winetest_platform
, "windows") == 0)
409 winetest_vskip(msg
, valist
);
411 winetest_vok(0, msg
, valist
);
412 __winetest_va_end(valist
);
415 void winetest_start_todo( int is_todo
)
417 struct tls_data
*data
= get_tls_data();
418 data
->todo_level
= (data
->todo_level
<< 1) | (is_todo
!= 0);
419 data
->todo_do_loop
=1;
422 int winetest_loop_todo(void)
424 struct tls_data
*data
= get_tls_data();
425 int do_loop
=data
->todo_do_loop
;
426 data
->todo_do_loop
=0;
430 void winetest_end_todo(void)
432 struct tls_data
*data
= get_tls_data();
433 data
->todo_level
>>= 1;
436 int winetest_get_mainargs( char*** pargv
)
438 *pargv
= winetest_argv
;
439 return winetest_argc
;
442 LONG
winetest_get_failures(void)
447 void winetest_add_failures( LONG new_failures
)
449 while (new_failures
-- > 0)
450 InterlockedIncrement( &failures
);
453 void winetest_wait_child_process( HANDLE process
)
457 if (WaitForSingleObject( process
, 30000 ))
458 printf( "%s: child process wait failed\n", current_test
->name
);
460 GetExitCodeProcess( process
, &exit_code
);
466 printf( "%s: exception 0x%08x in child process\n", current_test
->name
, exit_code
);
467 InterlockedIncrement( &failures
);
471 printf( "%s: %u failures in child process\n",
472 current_test
->name
, exit_code
);
473 while (exit_code
-- > 0)
474 InterlockedIncrement(&failures
);
479 const char *wine_dbgstr_wn( const WCHAR
*str
, int n
)
484 if (!((ULONG_PTR
)str
>> 16))
486 if (!str
) return "(null)";
487 res
= get_temp_buffer( 6 );
488 sprintf( res
, "#%04x", LOWORD(str
) );
493 const WCHAR
*end
= str
;
498 size
= 12 + min( 300, n
* 5 );
499 dst
= res
= get_temp_buffer( size
);
502 while (n
-- > 0 && dst
<= res
+ size
- 10)
507 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
508 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
509 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
510 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
511 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
513 if (c
>= ' ' && c
<= 126)
518 sprintf(dst
,"%04x",c
);
531 release_temp_buffer( res
, dst
- res
);
535 const char *wine_dbgstr_guid( const GUID
*guid
)
539 if (!guid
) return "(null)";
540 res
= get_temp_buffer( 39 ); /* CHARS_IN_GUID */
541 sprintf( res
, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
542 guid
->Data1
, guid
->Data2
, guid
->Data3
, guid
->Data4
[0],
543 guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3], guid
->Data4
[4],
544 guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7] );
548 const char *wine_dbgstr_rect( const RECT
*rect
)
552 if (!rect
) return "(null)";
553 res
= get_temp_buffer( 60 );
554 sprintf( res
, "(%d,%d)-(%d,%d)", rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
555 release_temp_buffer( res
, strlen(res
) + 1 );
559 const char *wine_dbgstr_longlong( ULONGLONG ll
)
563 res
= get_temp_buffer( 17 );
564 if (sizeof(ll
) > sizeof(unsigned long) && ll
>> 32)
565 sprintf( res
, "%lx%08lx", (unsigned long)(ll
>> 32), (unsigned long)ll
);
567 sprintf( res
, "%lx", (unsigned long)ll
);
568 release_temp_buffer( res
, strlen(res
) + 1 );
572 /* Find a test by name */
573 static const struct test
*find_test( const char *name
)
575 const struct test
*test
;
579 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
580 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
582 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
584 for (test
= winetest_testlist
; test
->name
; test
++)
586 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
588 return test
->name
? test
: NULL
;
592 /* Display list of valid tests */
593 static void list_tests(void)
595 const struct test
*test
;
597 printf( "Valid test names:\n" );
598 for (test
= winetest_testlist
; test
->name
; test
++)
599 printf( " %s\n", test
->name
);
603 /* Run a named test, and return exit status */
604 static int run_test( const char *name
)
606 const struct test
*test
;
609 if (!(test
= find_test( name
)))
611 printf( "Fatal: test '%s' does not exist.\n", name
);
614 successes
= failures
= todo_successes
= todo_failures
= 0;
615 tls_index
=TlsAlloc();
621 printf( "%04x:%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
622 GetCurrentProcessId(), test
->name
,
623 successes
+ failures
+ todo_successes
+ todo_failures
,
624 todo_successes
, failures
+ todo_failures
,
625 (failures
+ todo_failures
!= 1) ? "failures" : "failure",
628 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
633 /* Display usage and exit */
634 static void usage( const char *argv0
)
636 printf( "Usage: %s test_name\n\n", argv0
);
641 /* trap unhandled exceptions */
642 static LONG CALLBACK
exc_filter( EXCEPTION_POINTERS
*ptrs
)
644 struct tls_data
*data
= get_tls_data();
646 if (data
->current_file
)
647 printf( "%s:%d: this is the last test seen before the exception\n",
648 data
->current_file
, data
->current_line
);
649 printf( "%04x:%s: unhandled exception %08x at %p\n",
650 GetCurrentProcessId(), current_test
->name
,
651 ptrs
->ExceptionRecord
->ExceptionCode
, ptrs
->ExceptionRecord
->ExceptionAddress
);
653 return EXCEPTION_EXECUTE_HANDLER
;
656 /* check if we're running under wine */
657 static BOOL
running_under_wine(void)
659 HMODULE module
= GetModuleHandleA( "ntdll.dll" );
660 if (!module
) return FALSE
;
661 return (GetProcAddress( module
, "wine_server_call" ) != NULL
);
665 void _fpreset(void) {} /* override the mingw fpu init code */
669 int main( int argc
, char **argv
)
673 setvbuf (stdout
, NULL
, _IONBF
, 0);
675 winetest_argc
= argc
;
676 winetest_argv
= argv
;
678 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p
, sizeof(p
) ))
679 winetest_platform
= strdup(p
);
680 else if (running_under_wine())
681 winetest_platform
= "wine";
683 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p
, sizeof(p
) )) winetest_debug
= atoi(p
);
684 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p
, sizeof(p
) )) winetest_interactive
= atoi(p
);
685 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p
, sizeof(p
) )) winetest_report_success
= atoi(p
);
687 if (!strcmp( winetest_platform
, "windows" )) SetUnhandledExceptionFilter( exc_filter
);
688 if (!winetest_interactive
) SetErrorMode( SEM_FAILCRITICALERRORS
| SEM_NOGPFAULTERRORBOX
);
692 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
693 return run_test( winetest_testlist
[0].name
);
696 if (!strcmp( argv
[1], "--list" ))
701 return run_test(argv
[1]);
704 #endif /* STANDALONE */
706 #endif /* __WINE_WINE_TEST_H */