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( const char* platform
);
64 extern int winetest_loop_todo(void);
65 extern void winetest_end_todo( const char* platform
);
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 static inline const char *wine_dbgstr_w( const WCHAR
*s
) { return wine_dbgstr_wn( s
, -1 ); }
75 /* strcmpW is available for tests compiled under Wine, but not in standalone
76 * builds under Windows, so we reimplement it under a different name. */
77 static inline int winetest_strcmpW( const WCHAR
*str1
, const WCHAR
*str2
)
79 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 #else /* STANDALONE */
93 #define START_TEST(name) extern "C" void func_##name(void)
95 #define START_TEST(name) void func_##name(void)
98 #endif /* STANDALONE */
100 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
101 #define __winetest_cdecl __cdecl
102 #define __winetest_va_list __builtin_ms_va_list
104 #define __winetest_cdecl
105 #define __winetest_va_list va_list
108 extern int broken( int condition
);
109 extern int winetest_vok( int condition
, const char *msg
, __winetest_va_list ap
);
110 extern void winetest_vskip( const char *msg
, __winetest_va_list ap
);
114 extern void __winetest_cdecl
winetest_ok( int condition
, const char *msg
, ... ) __attribute__((format (printf
,2,3) ));
115 extern void __winetest_cdecl
winetest_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
116 extern void __winetest_cdecl
winetest_win_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
117 extern void __winetest_cdecl
winetest_trace( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
121 extern void __winetest_cdecl
winetest_ok( int condition
, const char *msg
, ... );
122 extern void __winetest_cdecl
winetest_skip( const char *msg
, ... );
123 extern void __winetest_cdecl
winetest_win_skip( const char *msg
, ... );
124 extern void __winetest_cdecl
winetest_trace( const char *msg
, ... );
126 #endif /* __GNUC__ */
128 #define ok_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_ok
129 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
130 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
131 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
133 #define ok ok_(__FILE__, __LINE__)
134 #define skip skip_(__FILE__, __LINE__)
135 #define win_skip win_skip_(__FILE__, __LINE__)
136 #define trace trace_(__FILE__, __LINE__)
138 #define todo(platform) for (winetest_start_todo(platform); \
139 winetest_loop_todo(); \
140 winetest_end_todo(platform))
142 #define todo_ros todo("reactos")
143 #ifdef USE_WINE_TODOS
144 #define todo_wine todo_ros
146 #define todo_wine todo("wine")
150 #ifdef NONAMELESSUNION
152 # define U1(x) (x).u1
153 # define U2(x) (x).u2
154 # define U3(x) (x).u3
155 # define U4(x) (x).u4
156 # define U5(x) (x).u5
157 # define U6(x) (x).u6
158 # define U7(x) (x).u7
159 # define U8(x) (x).u8
172 #ifdef NONAMELESSSTRUCT
174 # define S1(x) (x).s1
175 # define S2(x) (x).s2
176 # define S3(x) (x).s3
177 # define S4(x) (x).s4
178 # define S5(x) (x).s5
189 /************************************************************************/
190 /* Below is the implementation of the various functions, to be included
191 * directly into the generated testlist.c file.
192 * It is done that way so that the dlls can build the test routines with
193 * different includes or flags if needed.
200 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
201 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
202 # define __winetest_va_end(list) __builtin_ms_va_end(list)
204 # define __winetest_va_start(list,arg) va_start(list,arg)
205 # define __winetest_va_end(list) va_end(list)
214 extern const struct test winetest_testlist
[];
217 int winetest_debug
= 1;
219 /* interactive mode? */
220 int winetest_interactive
= 0;
222 /* current platform */
223 const char *winetest_platform
= "windows";
225 /* report successful tests (BOOL) */
226 static int report_success
= 0;
228 /* passing arguments around */
229 static int winetest_argc
;
230 static char** winetest_argv
;
232 static const struct test
*current_test
; /* test currently being run */
234 static LONG successes
; /* number of successful tests */
235 static LONG failures
; /* number of failures */
236 static LONG skipped
; /* number of skipped test chunks */
237 static LONG todo_successes
; /* number of successful tests inside todo block */
238 static LONG todo_failures
; /* number of failures inside todo block */
240 /* The following data must be kept track of on a per-thread basis */
243 const char* current_file
; /* file of current check */
244 int current_line
; /* line of current check */
245 int todo_level
; /* current todo nesting level */
247 char *str_pos
; /* position in debug buffer */
248 char strings
[2000]; /* buffer for debug strings */
250 static DWORD tls_index
;
252 static tls_data
* get_tls_data(void)
257 last_error
=GetLastError();
258 data
=(tls_data
*)TlsGetValue(tls_index
);
261 data
=(tls_data
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(tls_data
));
262 data
->str_pos
= data
->strings
;
263 TlsSetValue(tls_index
,data
);
265 SetLastError(last_error
);
269 /* allocate some tmp space for a string */
270 static char *get_temp_buffer( size_t n
)
272 tls_data
*data
= get_tls_data();
273 char *res
= data
->str_pos
;
275 if (res
+ n
>= &data
->strings
[sizeof(data
->strings
)]) res
= data
->strings
;
276 data
->str_pos
= res
+ n
;
280 /* release extra space that we requested in gimme1() */
281 static void release_temp_buffer( char *ptr
, size_t size
)
283 tls_data
*data
= get_tls_data();
284 data
->str_pos
= ptr
+ size
;
287 static void exit_process( int code
)
294 void winetest_set_location( const char* file
, int line
)
296 tls_data
* data
=get_tls_data();
297 data
->current_file
=strrchr(file
,'/');
298 if (data
->current_file
==NULL
)
299 data
->current_file
=strrchr(file
,'\\');
300 if (data
->current_file
==NULL
)
301 data
->current_file
=file
;
303 data
->current_file
++;
304 data
->current_line
=line
;
307 int broken( int condition
)
309 return ((strcmp(winetest_platform
, "windows") == 0)
310 #ifndef USE_WINE_TODOS
311 || (strcmp(winetest_platform
, "reactos") == 0)
319 * - condition - condition to check;
320 * - msg test description;
321 * - file - test application source code file name of the check
322 * - line - test application source code file line number of the check
324 * 0 if condition does not have the expected value, 1 otherwise
326 int winetest_vok( int condition
, const char *msg
, __winetest_va_list args
)
328 tls_data
* data
=get_tls_data();
330 if (data
->todo_level
)
334 fprintf( stdout
, "%s:%d: Test succeeded inside todo block: ",
335 data
->current_file
, data
->current_line
);
336 vfprintf(stdout
, msg
, args
);
337 InterlockedIncrement(&todo_failures
);
342 /* show todos even if traces are disabled*/
343 /*if (winetest_debug > 0)*/
345 fprintf( stdout
, "%s:%d: Test marked todo: ",
346 data
->current_file
, data
->current_line
);
347 vfprintf(stdout
, msg
, args
);
349 InterlockedIncrement(&todo_successes
);
357 fprintf( stdout
, "%s:%d: Test failed: ",
358 data
->current_file
, data
->current_line
);
359 vfprintf(stdout
, msg
, args
);
360 InterlockedIncrement(&failures
);
366 fprintf( stdout
, "%s:%d: Test succeeded\n",
367 data
->current_file
, data
->current_line
);
368 InterlockedIncrement(&successes
);
374 void __winetest_cdecl
winetest_ok( int condition
, const char *msg
, ... )
376 __winetest_va_list valist
;
378 __winetest_va_start(valist
, msg
);
379 winetest_vok(condition
, msg
, valist
);
380 __winetest_va_end(valist
);
383 void __winetest_cdecl
winetest_trace( const char *msg
, ... )
385 __winetest_va_list valist
;
386 tls_data
* data
=get_tls_data();
388 if (winetest_debug
> 0)
390 fprintf( stdout
, "%s:%d: ", data
->current_file
, data
->current_line
);
391 __winetest_va_start(valist
, msg
);
392 vfprintf(stdout
, msg
, valist
);
393 __winetest_va_end(valist
);
397 void winetest_vskip( const char *msg
, __winetest_va_list args
)
399 tls_data
* data
=get_tls_data();
401 fprintf( stdout
, "%s:%d: Tests skipped: ", data
->current_file
, data
->current_line
);
402 vfprintf(stdout
, msg
, args
);
406 void __winetest_cdecl
winetest_skip( const char *msg
, ... )
408 __winetest_va_list valist
;
409 __winetest_va_start(valist
, msg
);
410 winetest_vskip(msg
, valist
);
411 __winetest_va_end(valist
);
414 void __winetest_cdecl
winetest_win_skip( const char *msg
, ... )
416 __winetest_va_list valist
;
417 __winetest_va_start(valist
, msg
);
418 if ((strcmp(winetest_platform
, "windows") == 0)
419 #ifndef USE_WINE_TODOS
420 || (strcmp(winetest_platform
, "reactos") == 0)
423 winetest_vskip(msg
, valist
);
425 winetest_vok(0, msg
, valist
);
426 __winetest_va_end(valist
);
429 void winetest_start_todo( const char* platform
)
431 tls_data
* data
=get_tls_data();
432 if (strcmp(winetest_platform
,platform
)==0)
434 data
->todo_do_loop
=1;
437 int winetest_loop_todo(void)
439 tls_data
* data
=get_tls_data();
440 int do_loop
=data
->todo_do_loop
;
441 data
->todo_do_loop
=0;
445 void winetest_end_todo( const char* platform
)
447 if (strcmp(winetest_platform
,platform
)==0)
449 tls_data
* data
=get_tls_data();
454 int winetest_get_mainargs( char*** pargv
)
456 *pargv
= winetest_argv
;
457 return winetest_argc
;
460 LONG
winetest_get_failures(void)
465 void winetest_add_failures( LONG new_failures
)
467 while (new_failures
-- > 0)
468 InterlockedIncrement( &failures
);
471 void winetest_wait_child_process( HANDLE process
)
475 if (WaitForSingleObject( process
, 30000 ))
476 fprintf( stdout
, "%s: child process wait failed\n", current_test
->name
);
478 GetExitCodeProcess( process
, &exit_code
);
484 fprintf( stdout
, "%s: exception 0x%08x in child process\n", current_test
->name
, exit_code
);
485 InterlockedIncrement( &failures
);
489 fprintf( stdout
, "%s: %u failures in child process\n",
490 current_test
->name
, exit_code
);
491 while (exit_code
-- > 0)
492 InterlockedIncrement(&failures
);
497 const char *wine_dbgstr_wn( const WCHAR
*str
, intptr_t n
)
502 if (!((ULONG_PTR
)str
>> 16))
504 if (!str
) return "(null)";
505 res
= get_temp_buffer( 6 );
506 sprintf( res
, "#%04x", LOWORD(str
) );
511 const WCHAR
*end
= str
;
516 size
= 12 + min( 300, n
* 5 );
517 dst
= res
= get_temp_buffer( size
);
520 while (n
-- > 0 && dst
<= res
+ size
- 10)
525 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
526 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
527 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
528 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
529 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
531 if (c
>= ' ' && c
<= 126)
536 sprintf(dst
,"%04x",c
);
549 release_temp_buffer( res
, dst
- res
);
553 const char *wine_dbgstr_guid( const GUID
*guid
)
557 if (!guid
) return "(null)";
558 res
= get_temp_buffer( 39 ); /* CHARS_IN_GUID */
559 sprintf( res
, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
560 guid
->Data1
, guid
->Data2
, guid
->Data3
, guid
->Data4
[0],
561 guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3], guid
->Data4
[4],
562 guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7] );
566 /* Find a test by name */
567 static const struct test
*find_test( const char *name
)
569 const struct test
*test
;
573 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
574 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
576 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
578 for (test
= winetest_testlist
; test
->name
; test
++)
580 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
582 return test
->name
? test
: NULL
;
586 /* Display list of valid tests */
587 static void list_tests(void)
589 const struct test
*test
;
591 fprintf( stdout
, "Valid test names:\n" );
592 for (test
= winetest_testlist
; test
->name
; test
++) fprintf( stdout
, " %s\n", test
->name
);
595 /* Disable false-positive claiming "test" would be NULL-dereferenced */
596 #if defined(_MSC_VER)
597 #pragma warning(push)
598 #pragma warning(disable:28182)
601 /* Run a named test, and return exit status */
602 static int run_test( const char *name
)
604 const struct test
*test
;
607 if (!(test
= find_test( name
)))
609 fprintf( stdout
, "Fatal: test '%s' does not exist.\n", name
);
612 successes
= failures
= todo_successes
= todo_failures
= 0;
613 tls_index
=TlsAlloc();
617 /* show test results even if traces are disabled */
618 /*if (winetest_debug)*/
620 fprintf( stdout
, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
621 test
->name
, successes
+ failures
+ todo_successes
+ todo_failures
,
622 todo_successes
, failures
+ todo_failures
,
623 (failures
+ todo_failures
!= 1) ? "failures" : "failure",
626 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
630 #if defined(_MSC_VER)
634 /* Display usage and exit */
635 static void usage( const char *argv0
)
637 fprintf( stdout
, "Usage: %s test_name\n\n", argv0
);
644 int main( int argc
, char **argv
)
648 setvbuf (stdout
, NULL
, _IONBF
, 0);
650 winetest_argc
= argc
;
651 winetest_argv
= argv
;
653 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p
, sizeof(p
) )) winetest_platform
= _strdup(p
);
654 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p
, sizeof(p
) )) winetest_debug
= atoi(p
);
655 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p
, sizeof(p
) )) winetest_interactive
= atoi(p
);
656 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p
, sizeof(p
) )) report_success
= atoi(p
);
658 if (!winetest_interactive
) SetErrorMode( SEM_FAILCRITICALERRORS
| SEM_NOGPFAULTERRORBOX
);
662 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
663 return run_test( winetest_testlist
[0].name
);
666 if (!strcmp( argv
[1], "--list" ))
671 return run_test(argv
[1]);
674 #endif /* STANDALONE */
676 // hack for ntdll winetest (this is defined in excpt.h)
677 #undef exception_info
679 // Some helpful definitions
681 #define ok_hex(expression, result) \
683 int _value = (expression); \
684 ok(_value == (result), "Wrong value for '%s', expected: " #result " (0x%x), got: 0x%x\n", \
685 #expression, (int)(result), _value); \
688 #define ok_dec(expression, result) \
690 int _value = (expression); \
691 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%d), got: %d\n", \
692 #expression, (int)(result), _value); \
695 #define ok_ptr(expression, result) \
697 void *_value = (expression); \
698 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%p), got: %p\n", \
699 #expression, (void*)(result), _value); \
702 #define ok_size_t(expression, result) \
704 size_t _value = (expression); \
705 ok(_value == (result), "Wrong value for '%s', expected: " #result " (%Ix), got: %Ix\n", \
706 #expression, (size_t)(result), _value); \
709 #define ok_char(expression, result) ok_hex(expression, result)
711 #define ok_err(error) \
712 ok(GetLastError() == (error), "Wrong last error. Expected " #error ", got 0x%lx\n", GetLastError())
714 #define ok_str(x, y) \
715 ok(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x)
717 #define ok_wstr(x, y) \
718 ok(wcscmp(x, y) == 0, "Wrong string. Expected '%S', got '%S'\n", y, x)
720 #define ok_long(expression, result) ok_hex(expression, result)
721 #define ok_int(expression, result) ok_dec(expression, result)
722 #define ok_ntstatus(status, expected) ok_hex(status, expected)
723 #define ok_hdl ok_ptr
729 #endif /* __WINE_WINE_TEST_H */