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
31 #ifdef __WINE_WINE_LIBRARY_H
32 #error wine/library.h should not be used in Wine tests
34 #ifdef __WINE_WINE_UNICODE_H
35 #error wine/unicode.h should not be used in Wine tests
37 #ifdef __WINE_WINE_DEBUG_H
38 #error wine/debug.h should not be used in Wine tests
41 #ifndef INVALID_FILE_ATTRIBUTES
42 #define INVALID_FILE_ATTRIBUTES ((DWORD)~0UL)
44 #ifndef INVALID_SET_FILE_POINTER
45 #define INVALID_SET_FILE_POINTER ((DWORD)~0UL)
49 extern int winetest_debug
;
51 /* running in interactive mode? */
52 extern int winetest_interactive
;
54 /* current platform */
55 extern const char *winetest_platform
;
57 extern void winetest_set_location( const char* file
, int line
);
58 extern void winetest_start_todo( const char* platform
);
59 extern int winetest_loop_todo(void);
60 extern void winetest_end_todo( const char* platform
);
61 extern int winetest_get_mainargs( char*** pargv
);
62 extern void winetest_wait_child_process( HANDLE process
);
65 #define START_TEST(name) \
66 static void func_##name(void); \
67 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
68 static void func_##name(void)
70 #define START_TEST(name) void func_##name(void)
73 extern int broken( int condition
);
74 extern int winetest_vok( int condition
, const char *msg
, va_list ap
);
75 extern void winetest_vskip( const char *msg
, va_list ap
);
79 extern int winetest_ok( int condition
, const char *msg
, ... ) __attribute__((format (printf
,2,3) ));
80 extern void winetest_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
81 extern void winetest_win_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
82 extern void winetest_trace( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
86 extern int winetest_ok( int condition
, const char *msg
, ... );
87 extern void winetest_skip( const char *msg
, ... );
88 extern void winetest_win_skip( const char *msg
, ... );
89 extern void winetest_trace( const char *msg
, ... );
93 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
94 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
95 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
96 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
98 #define ok ok_(__FILE__, __LINE__)
99 #define skip skip_(__FILE__, __LINE__)
100 #define win_skip win_skip_(__FILE__, __LINE__)
101 #define trace trace_(__FILE__, __LINE__)
103 #define todo(platform) for (winetest_start_todo(platform); \
104 winetest_loop_todo(); \
105 winetest_end_todo(platform))
106 #define todo_wine todo("wine")
109 #ifdef NONAMELESSUNION
111 # define U1(x) (x).u1
112 # define U2(x) (x).u2
113 # define U3(x) (x).u3
114 # define U4(x) (x).u4
115 # define U5(x) (x).u5
116 # define U6(x) (x).u6
117 # define U7(x) (x).u7
118 # define U8(x) (x).u8
131 #ifdef NONAMELESSSTRUCT
133 # define S1(x) (x).s1
134 # define S2(x) (x).s2
135 # define S3(x) (x).s3
136 # define S4(x) (x).s4
137 # define S5(x) (x).s5
148 /************************************************************************/
149 /* Below is the implementation of the various functions, to be included
150 * directly into the generated testlist.c file.
151 * It is done that way so that the dlls can build the test routines with
152 * different includes or flags if needed.
165 extern const struct test winetest_testlist
[];
168 int winetest_debug
= 1;
170 /* interactive mode? */
171 int winetest_interactive
= 0;
173 /* current platform */
174 const char *winetest_platform
= "windows";
176 /* report successful tests (BOOL) */
177 static int report_success
= 0;
179 /* passing arguments around */
180 static int winetest_argc
;
181 static char** winetest_argv
;
183 static const struct test
*current_test
; /* test currently being run */
185 static LONG successes
; /* number of successful tests */
186 static LONG failures
; /* number of failures */
187 static LONG skipped
; /* number of skipped test chunks */
188 static LONG todo_successes
; /* number of successful tests inside todo block */
189 static LONG todo_failures
; /* number of failures inside todo block */
191 /* The following data must be kept track of on a per-thread basis */
194 const char* current_file
; /* file of current check */
195 int current_line
; /* line of current check */
196 int todo_level
; /* current todo nesting level */
199 static DWORD tls_index
;
201 static tls_data
* get_tls_data(void)
206 last_error
=GetLastError();
207 data
=TlsGetValue(tls_index
);
210 data
=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(tls_data
));
211 TlsSetValue(tls_index
,data
);
213 SetLastError(last_error
);
217 static void exit_process( int code
)
224 void winetest_set_location( const char* file
, int line
)
226 tls_data
* data
=get_tls_data();
227 data
->current_file
=strrchr(file
,'/');
228 if (data
->current_file
==NULL
)
229 data
->current_file
=strrchr(file
,'\\');
230 if (data
->current_file
==NULL
)
231 data
->current_file
=file
;
233 data
->current_file
++;
234 data
->current_line
=line
;
237 int broken( int condition
)
239 return (strcmp(winetest_platform
, "windows") == 0) && condition
;
245 * - condition - condition to check;
246 * - msg test description;
247 * - file - test application source code file name of the check
248 * - line - test application source code file line number of the check
250 * 0 if condition does not have the expected value, 1 otherwise
252 int winetest_vok( int condition
, const char *msg
, va_list args
)
254 tls_data
* data
=get_tls_data();
256 if (data
->todo_level
)
260 fprintf( stdout
, "%s:%d: Test succeeded inside todo block",
261 data
->current_file
, data
->current_line
);
264 fprintf(stdout
,": ");
265 vfprintf(stdout
, msg
, args
);
267 InterlockedIncrement(&todo_failures
);
270 else InterlockedIncrement(&todo_successes
);
276 fprintf( stdout
, "%s:%d: Test failed",
277 data
->current_file
, data
->current_line
);
280 fprintf( stdout
,": ");
281 vfprintf(stdout
, msg
, args
);
283 InterlockedIncrement(&failures
);
289 fprintf( stdout
, "%s:%d: Test succeeded\n",
290 data
->current_file
, data
->current_line
);
291 InterlockedIncrement(&successes
);
297 int winetest_ok( int condition
, const char *msg
, ... )
302 va_start(valist
, msg
);
303 rc
=winetest_vok(condition
, msg
, valist
);
308 void winetest_trace( const char *msg
, ... )
311 tls_data
* data
=get_tls_data();
313 if (winetest_debug
> 0)
315 fprintf( stdout
, "%s:%d: ", data
->current_file
, data
->current_line
);
316 va_start(valist
, msg
);
317 vfprintf(stdout
, msg
, valist
);
322 void winetest_vskip( const char *msg
, va_list args
)
324 tls_data
* data
=get_tls_data();
326 fprintf( stdout
, "%s:%d: Tests skipped: ", data
->current_file
, data
->current_line
);
327 vfprintf(stdout
, msg
, args
);
331 void winetest_skip( const char *msg
, ... )
334 va_start(valist
, msg
);
335 winetest_vskip(msg
, valist
);
339 void winetest_win_skip( const char *msg
, ... )
342 va_start(valist
, msg
);
343 if (strcmp(winetest_platform
, "windows") == 0)
344 winetest_vskip(msg
, valist
);
346 winetest_vok(0, msg
, valist
);
350 void winetest_start_todo( const char* platform
)
352 tls_data
* data
=get_tls_data();
353 if (strcmp(winetest_platform
,platform
)==0)
355 data
->todo_do_loop
=1;
358 int winetest_loop_todo(void)
360 tls_data
* data
=get_tls_data();
361 int do_loop
=data
->todo_do_loop
;
362 data
->todo_do_loop
=0;
366 void winetest_end_todo( const char* platform
)
368 if (strcmp(winetest_platform
,platform
)==0)
370 tls_data
* data
=get_tls_data();
375 int winetest_get_mainargs( char*** pargv
)
377 *pargv
= winetest_argv
;
378 return winetest_argc
;
381 void winetest_wait_child_process( HANDLE process
)
385 if (WaitForSingleObject( process
, 30000 ))
386 fprintf( stdout
, "%s: child process wait failed\n", current_test
->name
);
388 GetExitCodeProcess( process
, &exit_code
);
394 fprintf( stdout
, "%s: exception 0x%08x in child process\n", current_test
->name
, exit_code
);
395 InterlockedIncrement( &failures
);
399 fprintf( stdout
, "%s: %u failures in child process\n",
400 current_test
->name
, exit_code
);
401 while (exit_code
-- > 0)
402 InterlockedIncrement(&failures
);
407 /* Find a test by name */
408 static const struct test
*find_test( const char *name
)
410 const struct test
*test
;
414 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
415 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
417 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
419 for (test
= winetest_testlist
; test
->name
; test
++)
421 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
423 return test
->name
? test
: NULL
;
427 /* Display list of valid tests */
428 static void list_tests(void)
430 const struct test
*test
;
432 fprintf( stdout
, "Valid test names:\n" );
433 for (test
= winetest_testlist
; test
->name
; test
++) fprintf( stdout
, " %s\n", test
->name
);
437 /* Run a named test, and return exit status */
438 static int run_test( const char *name
)
440 const struct test
*test
;
443 if (!(test
= find_test( name
)))
445 fprintf( stdout
, "Fatal: test '%s' does not exist.\n", name
);
448 successes
= failures
= todo_successes
= todo_failures
= 0;
449 tls_index
=TlsAlloc();
455 fprintf( stdout
, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
456 test
->name
, successes
+ failures
+ todo_successes
+ todo_failures
,
457 todo_successes
, failures
+ todo_failures
,
458 (failures
+ todo_failures
!= 1) ? "failures" : "failure",
461 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
466 /* Display usage and exit */
467 static void usage( const char *argv0
)
469 fprintf( stdout
, "Usage: %s test_name\n\n", argv0
);
476 int main( int argc
, char **argv
)
480 setvbuf (stdout
, NULL
, _IONBF
, 0);
482 winetest_argc
= argc
;
483 winetest_argv
= argv
;
485 if ((p
= getenv( "WINETEST_PLATFORM" ))) winetest_platform
= strdup(p
);
486 if ((p
= getenv( "WINETEST_DEBUG" ))) winetest_debug
= atoi(p
);
487 if ((p
= getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive
= atoi(p
);
488 if ((p
= getenv( "WINETEST_REPORT_SUCCESS"))) report_success
= atoi(p
);
491 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
492 return run_test( winetest_testlist
[0].name
);
495 if (!strcmp( argv
[1], "--list" ))
500 return run_test(argv
[1]);
503 #endif /* STANDALONE */
505 #endif /* __WINE_WINE_TEST_H */