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)
73 extern int winetest_ok( int condition
, const char *msg
, ... ) __attribute__((format (printf
,2,3) ));
74 extern void winetest_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
75 extern void winetest_trace( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
79 extern int winetest_ok( int condition
, const char *msg
, ... );
80 extern void winetest_skip( const char *msg
, ... );
81 extern void winetest_trace( const char *msg
, ... );
85 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
86 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
87 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
89 #define ok ok_(__FILE__, __LINE__)
90 #define skip skip_(__FILE__, __LINE__)
91 #define trace trace_(__FILE__, __LINE__)
93 #define todo(platform) for (winetest_start_todo(platform); \
94 winetest_loop_todo(); \
95 winetest_end_todo(platform))
96 #define todo_wine todo("wine")
99 #ifdef NONAMELESSUNION
101 # define U1(x) (x).u1
102 # define U2(x) (x).u2
103 # define U3(x) (x).u3
104 # define U4(x) (x).u4
105 # define U5(x) (x).u5
106 # define U6(x) (x).u6
107 # define U7(x) (x).u7
108 # define U8(x) (x).u8
121 #ifdef NONAMELESSSTRUCT
123 # define S1(x) (x).s1
124 # define S2(x) (x).s2
125 # define S3(x) (x).s3
126 # define S4(x) (x).s4
127 # define S5(x) (x).s5
138 /************************************************************************/
139 /* Below is the implementation of the various functions, to be included
140 * directly into the generated testlist.c file.
141 * It is done that way so that the dlls can build the test routines with
142 * different includes or flags if needed.
155 extern const struct test winetest_testlist
[];
158 int winetest_debug
= 1;
160 /* interactive mode? */
161 int winetest_interactive
= 0;
163 /* current platform */
164 const char *winetest_platform
= "windows";
166 /* report successful tests (BOOL) */
167 static int report_success
= 0;
169 /* passing arguments around */
170 static int winetest_argc
;
171 static char** winetest_argv
;
173 static const struct test
*current_test
; /* test currently being run */
175 static LONG successes
; /* number of successful tests */
176 static LONG failures
; /* number of failures */
177 static LONG skipped
; /* number of skipped test chunks */
178 static LONG todo_successes
; /* number of successful tests inside todo block */
179 static LONG todo_failures
; /* number of failures inside todo block */
181 /* The following data must be kept track of on a per-thread basis */
184 const char* current_file
; /* file of current check */
185 int current_line
; /* line of current check */
186 int todo_level
; /* current todo nesting level */
189 static DWORD tls_index
;
191 static tls_data
* get_tls_data(void)
196 last_error
=GetLastError();
197 data
=TlsGetValue(tls_index
);
200 data
=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(tls_data
));
201 TlsSetValue(tls_index
,data
);
203 SetLastError(last_error
);
207 static void exit_process( int code
)
214 void winetest_set_location( const char* file
, int line
)
216 tls_data
* data
=get_tls_data();
217 data
->current_file
=strrchr(file
,'/');
218 if (data
->current_file
==NULL
)
219 data
->current_file
=strrchr(file
,'\\');
220 if (data
->current_file
==NULL
)
221 data
->current_file
=file
;
223 data
->current_file
++;
224 data
->current_line
=line
;
230 * - condition - condition to check;
231 * - msg test description;
232 * - file - test application source code file name of the check
233 * - line - test application source code file line number of the check
235 * 0 if condition does not have the expected value, 1 otherwise
237 int winetest_ok( int condition
, const char *msg
, ... )
240 tls_data
* data
=get_tls_data();
242 if (data
->todo_level
)
246 fprintf( stdout
, "%s:%d: Test succeeded inside todo block",
247 data
->current_file
, data
->current_line
);
250 va_start(valist
, msg
);
251 fprintf(stdout
,": ");
252 vfprintf(stdout
, msg
, valist
);
255 InterlockedIncrement(&todo_failures
);
258 else InterlockedIncrement(&todo_successes
);
264 fprintf( stdout
, "%s:%d: Test failed",
265 data
->current_file
, data
->current_line
);
268 va_start(valist
, msg
);
269 fprintf( stdout
,": ");
270 vfprintf(stdout
, msg
, valist
);
273 InterlockedIncrement(&failures
);
279 fprintf( stdout
, "%s:%d: Test succeeded\n",
280 data
->current_file
, data
->current_line
);
281 InterlockedIncrement(&successes
);
287 void winetest_trace( const char *msg
, ... )
290 tls_data
* data
=get_tls_data();
292 if (winetest_debug
> 0)
294 fprintf( stdout
, "%s:%d:", data
->current_file
, data
->current_line
);
295 va_start(valist
, msg
);
296 vfprintf(stdout
, msg
, valist
);
301 void winetest_skip( const char *msg
, ... )
304 tls_data
* data
=get_tls_data();
306 fprintf( stdout
, "%s:%d: Tests skipped: ", data
->current_file
, data
->current_line
);
307 va_start(valist
, msg
);
308 vfprintf(stdout
, msg
, valist
);
313 void winetest_start_todo( const char* platform
)
315 tls_data
* data
=get_tls_data();
316 if (strcmp(winetest_platform
,platform
)==0)
318 data
->todo_do_loop
=1;
321 int winetest_loop_todo(void)
323 tls_data
* data
=get_tls_data();
324 int do_loop
=data
->todo_do_loop
;
325 data
->todo_do_loop
=0;
329 void winetest_end_todo( const char* platform
)
331 if (strcmp(winetest_platform
,platform
)==0)
333 tls_data
* data
=get_tls_data();
338 int winetest_get_mainargs( char*** pargv
)
340 *pargv
= winetest_argv
;
341 return winetest_argc
;
344 void winetest_wait_child_process( HANDLE process
)
348 if (WaitForSingleObject( process
, 30000 ))
349 fprintf( stdout
, "%s: child process wait failed\n", current_test
->name
);
351 GetExitCodeProcess( process
, &exit_code
);
355 fprintf( stdout
, "%s: %u failures in child process\n",
356 current_test
->name
, exit_code
);
357 while (exit_code
-- > 0)
358 InterlockedIncrement(&failures
);
362 /* Find a test by name */
363 static const struct test
*find_test( const char *name
)
365 const struct test
*test
;
369 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
370 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
372 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
374 for (test
= winetest_testlist
; test
->name
; test
++)
376 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
378 return test
->name
? test
: NULL
;
382 /* Display list of valid tests */
383 static void list_tests(void)
385 const struct test
*test
;
387 fprintf( stdout
, "Valid test names:\n" );
388 for (test
= winetest_testlist
; test
->name
; test
++) fprintf( stdout
, " %s\n", test
->name
);
392 /* Run a named test, and return exit status */
393 static int run_test( const char *name
)
395 const struct test
*test
;
398 if (!(test
= find_test( name
)))
400 fprintf( stdout
, "Fatal: test '%s' does not exist.\n", name
);
403 successes
= failures
= todo_successes
= todo_failures
= 0;
404 tls_index
=TlsAlloc();
410 fprintf( stdout
, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
411 test
->name
, successes
+ failures
+ todo_successes
+ todo_failures
,
412 todo_successes
, failures
+ todo_failures
,
413 (failures
+ todo_failures
!= 1) ? "failures" : "failure",
416 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
421 /* Display usage and exit */
422 static void usage( const char *argv0
)
424 fprintf( stdout
, "Usage: %s test_name\n\n", argv0
);
431 int main( int argc
, char **argv
)
435 setvbuf (stdout
, NULL
, _IONBF
, 0);
437 winetest_argc
= argc
;
438 winetest_argv
= argv
;
440 if ((p
= getenv( "WINETEST_PLATFORM" ))) winetest_platform
= strdup(p
);
441 if ((p
= getenv( "WINETEST_DEBUG" ))) winetest_debug
= atoi(p
);
442 if ((p
= getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive
= atoi(p
);
443 if ((p
= getenv( "WINETEST_REPORT_SUCCESS"))) report_success
= atoi(p
);
446 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
447 return run_test( winetest_testlist
[0].name
);
450 if (!strcmp( argv
[1], "--list" ))
455 return run_test(argv
[1]);
458 #endif /* STANDALONE */
460 #endif /* __WINE_WINE_TEST_H */