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_WINE_LIBRARY_H
30 #error wine/library.h should not be used in Wine tests
32 #ifdef __WINE_WINE_UNICODE_H
33 #error wine/unicode.h should not be used in Wine tests
35 #ifdef __WINE_WINE_DEBUG_H
36 #error wine/debug.h should not be used in Wine tests
39 #ifndef INVALID_FILE_ATTRIBUTES
40 #define INVALID_FILE_ATTRIBUTES ((DWORD)~0UL)
42 #ifndef INVALID_SET_FILE_POINTER
43 #define INVALID_SET_FILE_POINTER ((DWORD)~0UL)
47 extern int winetest_debug
;
49 /* running in interactive mode? */
50 extern int winetest_interactive
;
52 /* current platform */
53 extern const char *winetest_platform
;
55 extern void winetest_set_location( const char* file
, int line
);
56 extern void winetest_start_todo( const char* platform
);
57 extern int winetest_loop_todo(void);
58 extern void winetest_end_todo( const char* platform
);
59 extern int winetest_get_mainargs( char*** pargv
);
60 extern void winetest_wait_child_process( HANDLE process
);
63 #define START_TEST(name) \
64 static void func_##name(void); \
65 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
66 static void func_##name(void)
68 #define START_TEST(name) void func_##name(void)
71 extern int broken( int condition
);
75 extern int winetest_ok( int condition
, const char *msg
, ... ) __attribute__((format (printf
,2,3) ));
76 extern void winetest_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
77 extern void winetest_trace( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
81 extern int winetest_ok( int condition
, const char *msg
, ... );
82 extern void winetest_skip( const char *msg
, ... );
83 extern void winetest_trace( const char *msg
, ... );
87 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
88 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
89 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
91 #define ok ok_(__FILE__, __LINE__)
92 #define skip skip_(__FILE__, __LINE__)
93 #define trace trace_(__FILE__, __LINE__)
95 #define todo(platform) for (winetest_start_todo(platform); \
96 winetest_loop_todo(); \
97 winetest_end_todo(platform))
98 #define todo_wine todo("wine")
101 #ifdef NONAMELESSUNION
103 # define U1(x) (x).u1
104 # define U2(x) (x).u2
105 # define U3(x) (x).u3
106 # define U4(x) (x).u4
107 # define U5(x) (x).u5
108 # define U6(x) (x).u6
109 # define U7(x) (x).u7
110 # define U8(x) (x).u8
123 #ifdef NONAMELESSSTRUCT
125 # define S1(x) (x).s1
126 # define S2(x) (x).s2
127 # define S3(x) (x).s3
128 # define S4(x) (x).s4
129 # define S5(x) (x).s5
140 /************************************************************************/
141 /* Below is the implementation of the various functions, to be included
142 * directly into the generated testlist.c file.
143 * It is done that way so that the dlls can build the test routines with
144 * different includes or flags if needed.
157 extern const struct test winetest_testlist
[];
160 int winetest_debug
= 1;
162 /* interactive mode? */
163 int winetest_interactive
= 0;
165 /* current platform */
166 const char *winetest_platform
= "windows";
168 /* report successful tests (BOOL) */
169 static int report_success
= 0;
171 /* passing arguments around */
172 static int winetest_argc
;
173 static char** winetest_argv
;
175 static const struct test
*current_test
; /* test currently being run */
177 static LONG successes
; /* number of successful tests */
178 static LONG failures
; /* number of failures */
179 static LONG skipped
; /* number of skipped test chunks */
180 static LONG todo_successes
; /* number of successful tests inside todo block */
181 static LONG todo_failures
; /* number of failures inside todo block */
183 /* The following data must be kept track of on a per-thread basis */
186 const char* current_file
; /* file of current check */
187 int current_line
; /* line of current check */
188 int todo_level
; /* current todo nesting level */
191 static DWORD tls_index
;
193 static tls_data
* get_tls_data(void)
198 last_error
=GetLastError();
199 data
=TlsGetValue(tls_index
);
202 data
=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(tls_data
));
203 TlsSetValue(tls_index
,data
);
205 SetLastError(last_error
);
209 static void exit_process( int code
)
216 void winetest_set_location( const char* file
, int line
)
218 tls_data
* data
=get_tls_data();
219 data
->current_file
=strrchr(file
,'/');
220 if (data
->current_file
==NULL
)
221 data
->current_file
=strrchr(file
,'\\');
222 if (data
->current_file
==NULL
)
223 data
->current_file
=file
;
225 data
->current_file
++;
226 data
->current_line
=line
;
229 int broken( int condition
)
231 return (strcmp(winetest_platform
, "windows") == 0) && condition
;
237 * - condition - condition to check;
238 * - msg test description;
239 * - file - test application source code file name of the check
240 * - line - test application source code file line number of the check
242 * 0 if condition does not have the expected value, 1 otherwise
244 int winetest_ok( int condition
, const char *msg
, ... )
247 tls_data
* data
=get_tls_data();
249 if (data
->todo_level
)
253 fprintf( stdout
, "%s:%d: Test succeeded inside todo block",
254 data
->current_file
, data
->current_line
);
257 va_start(valist
, msg
);
258 fprintf(stdout
,": ");
259 vfprintf(stdout
, msg
, valist
);
262 InterlockedIncrement(&todo_failures
);
265 else InterlockedIncrement(&todo_successes
);
271 fprintf( stdout
, "%s:%d: Test failed",
272 data
->current_file
, data
->current_line
);
275 va_start(valist
, msg
);
276 fprintf( stdout
,": ");
277 vfprintf(stdout
, msg
, valist
);
280 InterlockedIncrement(&failures
);
286 fprintf( stdout
, "%s:%d: Test succeeded\n",
287 data
->current_file
, data
->current_line
);
288 InterlockedIncrement(&successes
);
294 void winetest_trace( const char *msg
, ... )
297 tls_data
* data
=get_tls_data();
299 if (winetest_debug
> 0)
301 fprintf( stdout
, "%s:%d:", data
->current_file
, data
->current_line
);
302 va_start(valist
, msg
);
303 vfprintf(stdout
, msg
, valist
);
308 void winetest_skip( const char *msg
, ... )
311 tls_data
* data
=get_tls_data();
313 fprintf( stdout
, "%s:%d: Tests skipped: ", data
->current_file
, data
->current_line
);
314 va_start(valist
, msg
);
315 vfprintf(stdout
, msg
, valist
);
320 void winetest_start_todo( const char* platform
)
322 tls_data
* data
=get_tls_data();
323 if (strcmp(winetest_platform
,platform
)==0)
325 data
->todo_do_loop
=1;
328 int winetest_loop_todo(void)
330 tls_data
* data
=get_tls_data();
331 int do_loop
=data
->todo_do_loop
;
332 data
->todo_do_loop
=0;
336 void winetest_end_todo( const char* platform
)
338 if (strcmp(winetest_platform
,platform
)==0)
340 tls_data
* data
=get_tls_data();
345 int winetest_get_mainargs( char*** pargv
)
347 *pargv
= winetest_argv
;
348 return winetest_argc
;
351 void winetest_wait_child_process( HANDLE process
)
355 if (WaitForSingleObject( process
, 30000 ))
356 fprintf( stdout
, "%s: child process wait failed\n", current_test
->name
);
358 GetExitCodeProcess( process
, &exit_code
);
364 fprintf( stdout
, "%s: exception 0x%08x in child process\n", current_test
->name
, exit_code
);
365 InterlockedIncrement( &failures
);
369 fprintf( stdout
, "%s: %u failures in child process\n",
370 current_test
->name
, exit_code
);
371 while (exit_code
-- > 0)
372 InterlockedIncrement(&failures
);
377 /* Find a test by name */
378 static const struct test
*find_test( const char *name
)
380 const struct test
*test
;
384 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
385 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
387 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
389 for (test
= winetest_testlist
; test
->name
; test
++)
391 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
393 return test
->name
? test
: NULL
;
397 /* Display list of valid tests */
398 static void list_tests(void)
400 const struct test
*test
;
402 fprintf( stdout
, "Valid test names:\n" );
403 for (test
= winetest_testlist
; test
->name
; test
++) fprintf( stdout
, " %s\n", test
->name
);
407 /* Run a named test, and return exit status */
408 static int run_test( const char *name
)
410 const struct test
*test
;
413 if (!(test
= find_test( name
)))
415 fprintf( stdout
, "Fatal: test '%s' does not exist.\n", name
);
418 successes
= failures
= todo_successes
= todo_failures
= 0;
419 tls_index
=TlsAlloc();
425 fprintf( stdout
, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
426 test
->name
, successes
+ failures
+ todo_successes
+ todo_failures
,
427 todo_successes
, failures
+ todo_failures
,
428 (failures
+ todo_failures
!= 1) ? "failures" : "failure",
431 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
436 /* Display usage and exit */
437 static void usage( const char *argv0
)
439 fprintf( stdout
, "Usage: %s test_name\n\n", argv0
);
446 int main( int argc
, char **argv
)
450 setvbuf (stdout
, NULL
, _IONBF
, 0);
452 winetest_argc
= argc
;
453 winetest_argv
= argv
;
455 if ((p
= getenv( "WINETEST_PLATFORM" ))) winetest_platform
= strdup(p
);
456 if ((p
= getenv( "WINETEST_DEBUG" ))) winetest_debug
= atoi(p
);
457 if ((p
= getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive
= atoi(p
);
458 if ((p
= getenv( "WINETEST_REPORT_SUCCESS"))) report_success
= atoi(p
);
461 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
462 return run_test( winetest_testlist
[0].name
);
465 if (!strcmp( argv
[1], "--list" ))
470 return run_test(argv
[1]);
473 #endif /* STANDALONE */
475 #endif /* __WINE_WINE_TEST_H */