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
46 #ifndef INVALID_FILE_ATTRIBUTES
47 #define INVALID_FILE_ATTRIBUTES (~0u)
49 #ifndef INVALID_SET_FILE_POINTER
50 #define INVALID_SET_FILE_POINTER (~0u)
54 extern int winetest_debug
;
56 /* running in interactive mode? */
57 extern int winetest_interactive
;
59 /* current platform */
60 extern const char *winetest_platform
;
62 extern void winetest_set_location( const char* file
, int line
);
63 extern void winetest_start_todo( int is_todo
);
64 extern int winetest_loop_todo(void);
65 extern void winetest_end_todo(void);
66 extern int winetest_get_mainargs( char*** pargv
);
67 extern LONG
winetest_get_failures(void);
68 extern void winetest_add_failures( LONG new_failures
);
69 extern void winetest_wait_child_process( HANDLE process
);
71 extern const char *wine_dbgstr_wn( const WCHAR
*str
, intptr_t n
);
72 extern const char *wine_dbgstr_guid( const GUID
*guid
);
73 extern const char *wine_dbgstr_point( const POINT
*guid
);
74 extern const char *wine_dbgstr_size( const SIZE
*guid
);
75 extern const char *wine_dbgstr_rect( const RECT
*rect
);
76 static inline const char *wine_dbgstr_w( const WCHAR
*s
) { return wine_dbgstr_wn( s
, -1 ); }
78 /* strcmpW is available for tests compiled under Wine, but not in standalone
79 * builds under Windows, so we reimplement it under a different name. */
80 static inline int winetest_strcmpW( const WCHAR
*str1
, const WCHAR
*str2
)
82 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
88 #define START_TEST(name) \
89 static void func_##name(void); \
90 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
91 static void func_##name(void)
93 #else /* STANDALONE */
96 #define START_TEST(name) extern "C" void func_##name(void)
98 #define START_TEST(name) void func_##name(void)
101 #endif /* STANDALONE */
103 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
104 #define __winetest_cdecl __cdecl
105 #define __winetest_va_list __builtin_ms_va_list
107 #define __winetest_cdecl
108 #define __winetest_va_list va_list
111 extern int broken( int condition
);
112 extern int winetest_vok( int condition
, const char *msg
, __winetest_va_list ap
);
113 extern void winetest_vskip( const char *msg
, __winetest_va_list ap
);
116 # define WINETEST_PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
117 extern void __winetest_cdecl
winetest_ok( int condition
, const char *msg
, ... ) __attribute__((format (printf
,2,3) ));
118 extern void __winetest_cdecl
winetest_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
119 extern void __winetest_cdecl
winetest_win_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
120 extern void __winetest_cdecl
winetest_trace( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
123 # define WINETEST_PRINTF_ATTR(fmt,args)
124 extern void __winetest_cdecl
winetest_ok( int condition
, const char *msg
, ... );
125 extern void __winetest_cdecl
winetest_skip( const char *msg
, ... );
126 extern void __winetest_cdecl
winetest_win_skip( const char *msg
, ... );
127 extern void __winetest_cdecl
winetest_trace( const char *msg
, ... );
129 #endif /* __GNUC__ */
131 #define ok_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_ok
132 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
133 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
134 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
136 #define ok ok_(__FILE__, __LINE__)
137 #define skip skip_(__FILE__, __LINE__)
138 #define win_skip win_skip_(__FILE__, __LINE__)
139 #define trace trace_(__FILE__, __LINE__)
141 #define todo_if(is_todo) for (winetest_start_todo(is_todo); \
142 winetest_loop_todo(); \
145 #define todo_ros todo_if(!strcmp(winetest_platform, "reactos"))
146 #define todo_ros_if(is_todo) todo_if((is_todo) && !strcmp(winetest_platform, "reactos"))
147 #ifdef USE_WINE_TODOS
148 #define todo_wine todo_ros
149 #define todo_wine_if todo_ros_if
151 #define todo_wine todo_if(!strcmp(winetest_platform, "wine"))
152 #define todo_wine_if(is_todo) todo_if((is_todo) && !strcmp(winetest_platform, "wine"))
156 #ifdef NONAMELESSUNION
158 # define U1(x) (x).u1
159 # define U2(x) (x).u2
160 # define U3(x) (x).u3
161 # define U4(x) (x).u4
162 # define U5(x) (x).u5
163 # define U6(x) (x).u6
164 # define U7(x) (x).u7
165 # define U8(x) (x).u8
178 #ifdef NONAMELESSSTRUCT
180 # define S1(x) (x).s1
181 # define S2(x) (x).s2
182 # define S3(x) (x).s3
183 # define S4(x) (x).s4
184 # define S5(x) (x).s5
195 /************************************************************************/
196 /* Below is the implementation of the various functions, to be included
197 * directly into the generated testlist.c file.
198 * It is done that way so that the dlls can build the test routines with
199 * different includes or flags if needed.
206 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
207 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
208 # define __winetest_va_end(list) __builtin_ms_va_end(list)
210 # define __winetest_va_start(list,arg) va_start(list,arg)
211 # define __winetest_va_end(list) va_end(list)
214 /* Define WINETEST_MSVC_IDE_FORMATTING to alter the output format winetest will use for file/line numbers.
215 This alternate format makes the file/line numbers clickable in visual studio, to directly jump to them. */
216 #if defined(WINETEST_MSVC_IDE_FORMATTING)
217 # define __winetest_file_line_prefix "%s(%d)"
219 # define __winetest_file_line_prefix "%s:%d"
228 extern const struct test winetest_testlist
[];
231 int winetest_debug
= 1;
233 /* interactive mode? */
234 int winetest_interactive
= 0;
236 /* current platform */
237 const char *winetest_platform
= "windows";
239 /* report successful tests (BOOL) */
240 static int report_success
= 0;
242 /* passing arguments around */
243 static int winetest_argc
;
244 static char** winetest_argv
;
246 static const struct test
*current_test
; /* test currently being run */
248 static LONG successes
; /* number of successful tests */
249 static LONG failures
; /* number of failures */
250 static LONG skipped
; /* number of skipped test chunks */
251 static LONG todo_successes
; /* number of successful tests inside todo block */
252 static LONG todo_failures
; /* number of failures inside todo block */
254 /* The following data must be kept track of on a per-thread basis */
257 const char* current_file
; /* file of current check */
258 int current_line
; /* line of current check */
259 unsigned int todo_level
; /* current todo nesting level */
261 char *str_pos
; /* position in debug buffer */
262 char strings
[2000]; /* buffer for debug strings */
264 static DWORD tls_index
;
266 static tls_data
* get_tls_data(void)
271 last_error
=GetLastError();
272 data
=(tls_data
*)TlsGetValue(tls_index
);
275 data
=(tls_data
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(tls_data
));
276 data
->str_pos
= data
->strings
;
277 TlsSetValue(tls_index
,data
);
279 SetLastError(last_error
);
283 /* allocate some tmp space for a string */
284 static char *get_temp_buffer( size_t n
)
286 tls_data
*data
= get_tls_data();
287 char *res
= data
->str_pos
;
289 if (res
+ n
>= &data
->strings
[sizeof(data
->strings
)]) res
= data
->strings
;
290 data
->str_pos
= res
+ n
;
294 /* release extra space that we requested in gimme1() */
295 static void release_temp_buffer( char *ptr
, size_t size
)
297 tls_data
*data
= get_tls_data();
298 data
->str_pos
= ptr
+ size
;
301 static void exit_process( int code
)
308 void winetest_set_location( const char* file
, int line
)
310 tls_data
* data
=get_tls_data();
311 #if defined(WINETEST_MSVC_IDE_FORMATTING)
312 data
->current_file
= file
;
314 data
->current_file
=strrchr(file
,'/');
315 if (data
->current_file
==NULL
)
316 data
->current_file
=strrchr(file
,'\\');
317 if (data
->current_file
==NULL
)
318 data
->current_file
=file
;
320 data
->current_file
++;
322 data
->current_line
=line
;
325 int broken( int condition
)
327 return ((strcmp(winetest_platform
, "windows") == 0)
328 #ifndef USE_WINE_TODOS
329 || (strcmp(winetest_platform
, "reactos") == 0)
337 * - condition - condition to check;
338 * - msg test description;
339 * - file - test application source code file name of the check
340 * - line - test application source code file line number of the check
342 * 0 if condition does not have the expected value, 1 otherwise
344 int winetest_vok( int condition
, const char *msg
, __winetest_va_list args
)
346 tls_data
* data
=get_tls_data();
348 if (data
->todo_level
)
352 fprintf( stdout
, __winetest_file_line_prefix
": Test succeeded inside todo block: ",
353 data
->current_file
, data
->current_line
);
354 vfprintf(stdout
, msg
, args
);
355 InterlockedIncrement(&todo_failures
);
360 /* show todos even if traces are disabled*/
361 /*if (winetest_debug > 0)*/
363 fprintf( stdout
, __winetest_file_line_prefix
": Test marked todo: ",
364 data
->current_file
, data
->current_line
);
365 vfprintf(stdout
, msg
, args
);
367 InterlockedIncrement(&todo_successes
);
375 fprintf( stdout
, __winetest_file_line_prefix
": Test failed: ",
376 data
->current_file
, data
->current_line
);
377 vfprintf(stdout
, msg
, args
);
378 InterlockedIncrement(&failures
);
384 fprintf( stdout
, __winetest_file_line_prefix
": Test succeeded\n",
385 data
->current_file
, data
->current_line
);
386 InterlockedIncrement(&successes
);
392 void __winetest_cdecl
winetest_ok( int condition
, const char *msg
, ... )
394 __winetest_va_list valist
;
396 __winetest_va_start(valist
, msg
);
397 winetest_vok(condition
, msg
, valist
);
398 __winetest_va_end(valist
);
401 void __winetest_cdecl
winetest_trace( const char *msg
, ... )
403 __winetest_va_list valist
;
404 tls_data
* data
=get_tls_data();
406 if (winetest_debug
> 0)
408 fprintf( stdout
, __winetest_file_line_prefix
": ", data
->current_file
, data
->current_line
);
409 __winetest_va_start(valist
, msg
);
410 vfprintf(stdout
, msg
, valist
);
411 __winetest_va_end(valist
);
415 void winetest_vskip( const char *msg
, __winetest_va_list args
)
417 tls_data
* data
=get_tls_data();
419 fprintf( stdout
, __winetest_file_line_prefix
": Tests skipped: ", data
->current_file
, data
->current_line
);
420 vfprintf(stdout
, msg
, args
);
424 void __winetest_cdecl
winetest_skip( const char *msg
, ... )
426 __winetest_va_list valist
;
427 __winetest_va_start(valist
, msg
);
428 winetest_vskip(msg
, valist
);
429 __winetest_va_end(valist
);
432 void __winetest_cdecl
winetest_win_skip( const char *msg
, ... )
434 __winetest_va_list valist
;
435 __winetest_va_start(valist
, msg
);
436 if ((strcmp(winetest_platform
, "windows") == 0)
437 #ifndef USE_WINE_TODOS
438 || (strcmp(winetest_platform
, "reactos") == 0)
441 winetest_vskip(msg
, valist
);
443 winetest_vok(0, msg
, valist
);
444 __winetest_va_end(valist
);
447 void winetest_start_todo( int is_todo
)
449 tls_data
* data
=get_tls_data();
450 data
->todo_level
= (data
->todo_level
<< 1) | (is_todo
!= 0);
451 data
->todo_do_loop
=1;
454 int winetest_loop_todo(void)
456 tls_data
* data
=get_tls_data();
457 int do_loop
=data
->todo_do_loop
;
458 data
->todo_do_loop
=0;
462 void winetest_end_todo(void)
464 tls_data
* data
=get_tls_data();
465 data
->todo_level
>>= 1;
468 int winetest_get_mainargs( char*** pargv
)
470 *pargv
= winetest_argv
;
471 return winetest_argc
;
474 LONG
winetest_get_failures(void)
479 void winetest_add_failures( LONG new_failures
)
481 while (new_failures
-- > 0)
482 InterlockedIncrement( &failures
);
485 void winetest_wait_child_process( HANDLE process
)
489 if (WaitForSingleObject( process
, 30000 ))
490 fprintf( stdout
, "%s: child process wait failed\n", current_test
->name
);
492 GetExitCodeProcess( process
, &exit_code
);
498 fprintf( stdout
, "%s: exception 0x%08x in child process\n", current_test
->name
, (unsigned)exit_code
);
499 InterlockedIncrement( &failures
);
503 fprintf( stdout
, "%s: %u failures in child process\n",
504 current_test
->name
, (unsigned)exit_code
);
505 while (exit_code
-- > 0)
506 InterlockedIncrement(&failures
);
511 const char *wine_dbgstr_wn( const WCHAR
*str
, intptr_t n
)
516 if (!((ULONG_PTR
)str
>> 16))
518 if (!str
) return "(null)";
519 res
= get_temp_buffer( 6 );
520 sprintf( res
, "#%04x", LOWORD(str
) );
525 const WCHAR
*end
= str
;
530 size
= 12 + min( 300, n
* 5 );
531 dst
= res
= get_temp_buffer( size
);
534 while (n
-- > 0 && dst
<= res
+ size
- 10)
539 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
540 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
541 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
542 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
543 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
545 if (c
>= ' ' && c
<= 126)
550 sprintf(dst
,"%04x",c
);
563 release_temp_buffer( res
, dst
- res
);
567 const char *wine_dbgstr_guid( const GUID
*guid
)
571 if (!guid
) return "(null)";
572 res
= get_temp_buffer( 39 ); /* CHARS_IN_GUID */
573 sprintf( res
, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
574 guid
->Data1
, guid
->Data2
, guid
->Data3
, guid
->Data4
[0],
575 guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3], guid
->Data4
[4],
576 guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7] );
580 const char *wine_dbgstr_point( const POINT
*point
)
584 if (!point
) return "(null)";
585 res
= get_temp_buffer( 60 );
586 #ifdef __ROS_LONG64__
587 sprintf( res
, "(%d,%d)", point
->x
, point
->y
);
589 sprintf( res
, "(%ld,%ld)", point
->x
, point
->y
);
591 release_temp_buffer( res
, strlen(res
) + 1 );
595 const char *wine_dbgstr_size( const SIZE
*size
)
599 if (!size
) return "(null)";
600 res
= get_temp_buffer( 60 );
601 #ifdef __ROS_LONG64__
602 sprintf( res
, "(%d,%d)", size
->cx
, size
->cy
);
604 sprintf( res
, "(%ld,%ld)", size
->cx
, size
->cy
);
606 release_temp_buffer( res
, strlen(res
) + 1 );
610 const char *wine_dbgstr_rect( const RECT
*rect
)
614 if (!rect
) return "(null)";
615 res
= get_temp_buffer( 60 );
616 #ifdef __ROS_LONG64__
617 sprintf( res
, "(%d,%d)-(%d,%d)", rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
619 sprintf( res
, "(%ld,%ld)-(%ld,%ld)", rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
621 release_temp_buffer( res
, strlen(res
) + 1 );
625 /* Find a test by name */
626 static const struct test
*find_test( const char *name
)
628 const struct test
*test
;
632 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
633 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
635 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
637 for (test
= winetest_testlist
; test
->name
; test
++)
639 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
641 return test
->name
? test
: NULL
;
645 /* Display list of valid tests */
646 static void list_tests(void)
648 const struct test
*test
;
650 fprintf( stdout
, "Valid test names:\n" );
651 for (test
= winetest_testlist
; test
->name
; test
++) fprintf( stdout
, " %s\n", test
->name
);
654 /* Disable false-positive claiming "test" would be NULL-dereferenced */
655 #if defined(_MSC_VER)
656 #pragma warning(push)
657 #pragma warning(disable:28182)
660 /* Run a named test, and return exit status */
661 static int run_test( const char *name
)
663 const struct test
*test
;
666 if (!(test
= find_test( name
)))
668 fprintf( stdout
, "Fatal: test '%s' does not exist.\n", name
);
671 successes
= failures
= todo_successes
= todo_failures
= 0;
672 tls_index
=TlsAlloc();
676 /* show test results even if traces are disabled */
677 /*if (winetest_debug)*/
679 fprintf( stdout
, "\n%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
680 test
->name
, (int)(successes
+ failures
+ todo_successes
+ todo_failures
),
681 (int)todo_successes
, (int)(failures
+ todo_failures
),
682 (failures
+ todo_failures
!= 1) ? "failures" : "failure",
685 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
689 #if defined(_MSC_VER)
693 /* Display usage and exit */
694 static void usage( const char *argv0
)
696 fprintf( stdout
, "Usage: %s test_name\n\n", argv0
);
703 int main( int argc
, char **argv
)
707 setvbuf (stdout
, NULL
, _IONBF
, 0);
709 winetest_argc
= argc
;
710 winetest_argv
= argv
;
712 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p
, sizeof(p
) )) winetest_platform
= _strdup(p
);
713 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p
, sizeof(p
) )) winetest_debug
= atoi(p
);
714 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p
, sizeof(p
) )) winetest_interactive
= atoi(p
);
715 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p
, sizeof(p
) )) report_success
= atoi(p
);
717 if (!winetest_interactive
) SetErrorMode( SEM_FAILCRITICALERRORS
| SEM_NOGPFAULTERRORBOX
);
721 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
722 return run_test( winetest_testlist
[0].name
);
725 if (!strcmp( argv
[1], "--list" ))
730 return run_test(argv
[1]);
733 #endif /* STANDALONE */
735 // hack for ntdll winetest (this is defined in excpt.h)
736 #undef exception_info
738 // Some helpful definitions
740 #define ok_hex(expression, result) \
742 int _value = (expression); \
743 ok(_value == (result), "Wrong value for '%s', expected: " #result " (0x%x), got: 0x%x\n", \
744 #expression, (int)(result), _value); \
747 #define ok_dec(expression, result) \
749 int _value = (expression); \
750 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%d), got: %d\n", \
751 #expression, (int)(result), _value); \
754 #define ok_ptr(expression, result) \
756 void *_value = (expression); \
757 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%p), got: %p\n", \
758 #expression, (void*)(result), _value); \
761 #define ok_size_t(expression, result) \
763 size_t _value = (expression); \
764 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%Ix), got: %Ix\n", \
765 #expression, (size_t)(result), _value); \
768 #define ok_char(expression, result) ok_hex(expression, result)
770 #define ok_err(error) \
771 ok(GetLastError() == (error), "Wrong last error. Expected " #error ", got 0x%lx\n", GetLastError())
773 #define ok_str(x, y) \
774 ok(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x)
776 #define ok_wstr(x, y) \
777 ok(wcscmp(x, y) == 0, "Wrong string. Expected '%S', got '%S'\n", y, x)
779 #define ok_long(expression, result) ok_hex(expression, result)
780 #define ok_int(expression, result) ok_dec(expression, result)
781 #define ok_ntstatus(status, expected) ok_hex(status, expected)
782 #define ok_hdl ok_ptr
788 #endif /* __WINE_WINE_TEST_H */