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 /* 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 void winetest_wait_child_process( HANDLE process
);
66 #define START_TEST(name) \
67 static void func_##name(void); \
68 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
69 static void func_##name(void)
71 #define START_TEST(name) void func_##name(void)
74 extern int broken( int condition
);
75 extern int winetest_vok( int condition
, const char *msg
, va_list ap
);
76 extern void winetest_vskip( const char *msg
, va_list ap
);
80 extern int winetest_ok( int condition
, const char *msg
, ... ) __attribute__((format (printf
,2,3) ));
81 extern void winetest_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
82 extern void winetest_win_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
83 extern void winetest_trace( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
87 extern int winetest_ok( int condition
, const char *msg
, ... );
88 extern void winetest_skip( const char *msg
, ... );
89 extern void winetest_win_skip( const char *msg
, ... );
90 extern void winetest_trace( const char *msg
, ... );
94 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
95 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
96 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
97 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
99 #define ok ok_(__FILE__, __LINE__)
100 #define skip skip_(__FILE__, __LINE__)
101 #define win_skip win_skip_(__FILE__, __LINE__)
102 #define trace trace_(__FILE__, __LINE__)
104 #define todo(platform) for (winetest_start_todo(platform); \
105 winetest_loop_todo(); \
106 winetest_end_todo(platform))
107 #define todo_wine todo("wine")
110 #ifdef NONAMELESSUNION
112 # define U1(x) (x).u1
113 # define U2(x) (x).u2
114 # define U3(x) (x).u3
115 # define U4(x) (x).u4
116 # define U5(x) (x).u5
117 # define U6(x) (x).u6
118 # define U7(x) (x).u7
119 # define U8(x) (x).u8
132 #ifdef NONAMELESSSTRUCT
134 # define S1(x) (x).s1
135 # define S2(x) (x).s2
136 # define S3(x) (x).s3
137 # define S4(x) (x).s4
138 # define S5(x) (x).s5
149 /************************************************************************/
150 /* Below is the implementation of the various functions, to be included
151 * directly into the generated testlist.c file.
152 * It is done that way so that the dlls can build the test routines with
153 * different includes or flags if needed.
166 extern const struct test winetest_testlist
[];
169 int winetest_debug
= 1;
171 /* interactive mode? */
172 int winetest_interactive
= 0;
174 /* current platform */
175 const char *winetest_platform
= "windows";
177 /* report successful tests (BOOL) */
178 static int report_success
= 0;
180 /* passing arguments around */
181 static int winetest_argc
;
182 static char** winetest_argv
;
184 static const struct test
*current_test
; /* test currently being run */
186 static LONG successes
; /* number of successful tests */
187 static LONG failures
; /* number of failures */
188 static LONG skipped
; /* number of skipped test chunks */
189 static LONG todo_successes
; /* number of successful tests inside todo block */
190 static LONG todo_failures
; /* number of failures inside todo block */
192 /* The following data must be kept track of on a per-thread basis */
195 const char* current_file
; /* file of current check */
196 int current_line
; /* line of current check */
197 int todo_level
; /* current todo nesting level */
200 static DWORD tls_index
;
202 static tls_data
* get_tls_data(void)
207 last_error
=GetLastError();
208 data
=TlsGetValue(tls_index
);
211 data
=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(tls_data
));
212 TlsSetValue(tls_index
,data
);
214 SetLastError(last_error
);
218 static void exit_process( int code
)
225 void winetest_set_location( const char* file
, int line
)
227 tls_data
* data
=get_tls_data();
228 data
->current_file
=strrchr(file
,'/');
229 if (data
->current_file
==NULL
)
230 data
->current_file
=strrchr(file
,'\\');
231 if (data
->current_file
==NULL
)
232 data
->current_file
=file
;
234 data
->current_file
++;
235 data
->current_line
=line
;
238 int broken( int condition
)
240 return (strcmp(winetest_platform
, "windows") == 0) && condition
;
246 * - condition - condition to check;
247 * - msg test description;
248 * - file - test application source code file name of the check
249 * - line - test application source code file line number of the check
251 * 0 if condition does not have the expected value, 1 otherwise
253 int winetest_vok( int condition
, const char *msg
, va_list args
)
255 tls_data
* data
=get_tls_data();
257 if (data
->todo_level
)
261 fprintf( stdout
, "%s:%d: Test succeeded inside todo block: ",
262 data
->current_file
, data
->current_line
);
263 vfprintf(stdout
, msg
, args
);
264 InterlockedIncrement(&todo_failures
);
269 if (winetest_debug
> 0)
271 fprintf( stdout
, "%s:%d: Test marked todo: ",
272 data
->current_file
, data
->current_line
);
273 vfprintf(stdout
, msg
, args
);
275 InterlockedIncrement(&todo_successes
);
283 fprintf( stdout
, "%s:%d: Test failed: ",
284 data
->current_file
, data
->current_line
);
285 vfprintf(stdout
, msg
, args
);
286 InterlockedIncrement(&failures
);
292 fprintf( stdout
, "%s:%d: Test succeeded\n",
293 data
->current_file
, data
->current_line
);
294 InterlockedIncrement(&successes
);
300 int winetest_ok( int condition
, const char *msg
, ... )
305 va_start(valist
, msg
);
306 rc
=winetest_vok(condition
, msg
, valist
);
311 void winetest_trace( const char *msg
, ... )
314 tls_data
* data
=get_tls_data();
316 if (winetest_debug
> 0)
318 fprintf( stdout
, "%s:%d: ", data
->current_file
, data
->current_line
);
319 va_start(valist
, msg
);
320 vfprintf(stdout
, msg
, valist
);
325 void winetest_vskip( const char *msg
, va_list args
)
327 tls_data
* data
=get_tls_data();
329 fprintf( stdout
, "%s:%d: Tests skipped: ", data
->current_file
, data
->current_line
);
330 vfprintf(stdout
, msg
, args
);
334 void winetest_skip( const char *msg
, ... )
337 va_start(valist
, msg
);
338 winetest_vskip(msg
, valist
);
342 void winetest_win_skip( const char *msg
, ... )
345 va_start(valist
, msg
);
346 if (strcmp(winetest_platform
, "windows") == 0)
347 winetest_vskip(msg
, valist
);
349 winetest_vok(0, msg
, valist
);
353 void winetest_start_todo( const char* platform
)
355 tls_data
* data
=get_tls_data();
356 if (strcmp(winetest_platform
,platform
)==0)
358 data
->todo_do_loop
=1;
361 int winetest_loop_todo(void)
363 tls_data
* data
=get_tls_data();
364 int do_loop
=data
->todo_do_loop
;
365 data
->todo_do_loop
=0;
369 void winetest_end_todo( const char* platform
)
371 if (strcmp(winetest_platform
,platform
)==0)
373 tls_data
* data
=get_tls_data();
378 int winetest_get_mainargs( char*** pargv
)
380 *pargv
= winetest_argv
;
381 return winetest_argc
;
384 void winetest_wait_child_process( HANDLE process
)
388 if (WaitForSingleObject( process
, 30000 ))
389 fprintf( stdout
, "%s: child process wait failed\n", current_test
->name
);
391 GetExitCodeProcess( process
, &exit_code
);
397 fprintf( stdout
, "%s: exception 0x%08x in child process\n", current_test
->name
, exit_code
);
398 InterlockedIncrement( &failures
);
402 fprintf( stdout
, "%s: %u failures in child process\n",
403 current_test
->name
, exit_code
);
404 while (exit_code
-- > 0)
405 InterlockedIncrement(&failures
);
410 /* Find a test by name */
411 static const struct test
*find_test( const char *name
)
413 const struct test
*test
;
417 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
418 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
420 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
422 for (test
= winetest_testlist
; test
->name
; test
++)
424 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
426 return test
->name
? test
: NULL
;
430 /* Display list of valid tests */
431 static void list_tests(void)
433 const struct test
*test
;
435 fprintf( stdout
, "Valid test names:\n" );
436 for (test
= winetest_testlist
; test
->name
; test
++) fprintf( stdout
, " %s\n", test
->name
);
440 /* Run a named test, and return exit status */
441 static int run_test( const char *name
)
443 const struct test
*test
;
446 if (!(test
= find_test( name
)))
448 fprintf( stdout
, "Fatal: test '%s' does not exist.\n", name
);
451 successes
= failures
= todo_successes
= todo_failures
= 0;
452 tls_index
=TlsAlloc();
458 fprintf( stdout
, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
459 test
->name
, successes
+ failures
+ todo_successes
+ todo_failures
,
460 todo_successes
, failures
+ todo_failures
,
461 (failures
+ todo_failures
!= 1) ? "failures" : "failure",
464 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
469 /* Display usage and exit */
470 static void usage( const char *argv0
)
472 fprintf( stdout
, "Usage: %s test_name\n\n", argv0
);
479 int main( int argc
, char **argv
)
483 setvbuf (stdout
, NULL
, _IONBF
, 0);
485 winetest_argc
= argc
;
486 winetest_argv
= argv
;
488 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p
, sizeof(p
) )) winetest_platform
= strdup(p
);
489 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p
, sizeof(p
) )) winetest_debug
= atoi(p
);
490 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p
, sizeof(p
) )) winetest_interactive
= atoi(p
);
491 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p
, sizeof(p
) )) report_success
= atoi(p
);
495 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
496 return run_test( winetest_testlist
[0].name
);
499 if (!strcmp( argv
[1], "--list" ))
504 return run_test(argv
[1]);
507 #endif /* STANDALONE */
509 #endif /* __WINE_WINE_TEST_H */