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
);
64 #define START_TEST(name) \
65 static void func_##name(void); \
66 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
67 static void func_##name(void)
69 #define START_TEST(name) void func_##name(void)
74 extern int winetest_ok( int condition
, const char *msg
, ... ) __attribute__((format (printf
,2,3) ));
75 extern void winetest_skip( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
76 extern void winetest_trace( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
80 extern int winetest_ok( int condition
, const char *msg
, ... );
81 extern void winetest_skip( const char *msg
, ... );
82 extern void winetest_trace( const char *msg
, ... );
86 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
87 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
88 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
90 #define ok ok_(__FILE__, __LINE__)
91 #define skip skip_(__FILE__, __LINE__)
92 #define trace trace_(__FILE__, __LINE__)
94 #define todo(platform) for (winetest_start_todo(platform); \
95 winetest_loop_todo(); \
96 winetest_end_todo(platform))
97 #define todo_wine todo("wine")
100 #ifdef NONAMELESSUNION
102 # define U1(x) (x).u1
103 # define U2(x) (x).u2
104 # define U3(x) (x).u3
105 # define U4(x) (x).u4
106 # define U5(x) (x).u5
107 # define U6(x) (x).u6
108 # define U7(x) (x).u7
109 # define U8(x) (x).u8
122 #ifdef NONAMELESSSTRUCT
124 # define S1(x) (x).s1
125 # define S2(x) (x).s2
126 # define S3(x) (x).s3
127 # define S4(x) (x).s4
128 # define S5(x) (x).s5
139 /************************************************************************/
140 /* Below is the implementation of the various functions, to be included
141 * directly into the generated testlist.c file.
142 * It is done that way so that the dlls can build the test routines with
143 * different includes or flags if needed.
156 extern const struct test winetest_testlist
[];
159 int winetest_debug
= 1;
161 /* interactive mode? */
162 int winetest_interactive
= 0;
164 /* current platform */
165 const char *winetest_platform
= "windows";
167 /* report successful tests (BOOL) */
168 static int report_success
= 0;
170 /* passing arguments around */
171 static int winetest_argc
;
172 static char** winetest_argv
;
174 static const struct test
*current_test
; /* test currently being run */
176 static LONG successes
; /* number of successful tests */
177 static LONG failures
; /* number of failures */
178 static LONG skipped
; /* number of skipped test chunks */
179 static LONG todo_successes
; /* number of successful tests inside todo block */
180 static LONG todo_failures
; /* number of failures inside todo block */
182 /* The following data must be kept track of on a per-thread basis */
185 const char* current_file
; /* file of current check */
186 int current_line
; /* line of current check */
187 int todo_level
; /* current todo nesting level */
190 static DWORD tls_index
;
192 static tls_data
* get_tls_data(void)
197 last_error
=GetLastError();
198 data
=TlsGetValue(tls_index
);
201 data
=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(tls_data
));
202 TlsSetValue(tls_index
,data
);
204 SetLastError(last_error
);
208 static void exit_process( int code
)
215 void winetest_set_location( const char* file
, int line
)
217 tls_data
* data
=get_tls_data();
218 data
->current_file
=strrchr(file
,'/');
219 if (data
->current_file
==NULL
)
220 data
->current_file
=strrchr(file
,'\\');
221 if (data
->current_file
==NULL
)
222 data
->current_file
=file
;
224 data
->current_file
++;
225 data
->current_line
=line
;
231 * - condition - condition to check;
232 * - msg test description;
233 * - file - test application source code file name of the check
234 * - line - test application source code file line number of the check
236 * 0 if condition does not have the expected value, 1 otherwise
238 int winetest_ok( int condition
, const char *msg
, ... )
241 tls_data
* data
=get_tls_data();
243 if (data
->todo_level
)
247 fprintf( stdout
, "%s:%d: Test succeeded inside todo block",
248 data
->current_file
, data
->current_line
);
251 va_start(valist
, msg
);
252 fprintf(stdout
,": ");
253 vfprintf(stdout
, msg
, valist
);
256 InterlockedIncrement(&todo_failures
);
259 else InterlockedIncrement(&todo_successes
);
265 fprintf( stdout
, "%s:%d: Test failed",
266 data
->current_file
, data
->current_line
);
269 va_start(valist
, msg
);
270 fprintf( stdout
,": ");
271 vfprintf(stdout
, msg
, valist
);
274 InterlockedIncrement(&failures
);
280 fprintf( stdout
, "%s:%d: Test succeeded\n",
281 data
->current_file
, data
->current_line
);
282 InterlockedIncrement(&successes
);
288 void winetest_trace( const char *msg
, ... )
291 tls_data
* data
=get_tls_data();
293 if (winetest_debug
> 0)
295 fprintf( stdout
, "%s:%d:", data
->current_file
, data
->current_line
);
296 va_start(valist
, msg
);
297 vfprintf(stdout
, msg
, valist
);
302 void winetest_skip( const char *msg
, ... )
305 tls_data
* data
=get_tls_data();
307 fprintf( stdout
, "%s:%d: Tests skipped: ", data
->current_file
, data
->current_line
);
308 va_start(valist
, msg
);
309 vfprintf(stdout
, msg
, valist
);
314 void winetest_start_todo( const char* platform
)
316 tls_data
* data
=get_tls_data();
317 if (strcmp(winetest_platform
,platform
)==0)
319 data
->todo_do_loop
=1;
322 int winetest_loop_todo(void)
324 tls_data
* data
=get_tls_data();
325 int do_loop
=data
->todo_do_loop
;
326 data
->todo_do_loop
=0;
330 void winetest_end_todo( const char* platform
)
332 if (strcmp(winetest_platform
,platform
)==0)
334 tls_data
* data
=get_tls_data();
339 int winetest_get_mainargs( char*** pargv
)
341 *pargv
= winetest_argv
;
342 return winetest_argc
;
345 /* Find a test by name */
346 static const struct test
*find_test( const char *name
)
348 const struct test
*test
;
352 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
353 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
355 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
357 for (test
= winetest_testlist
; test
->name
; test
++)
359 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
361 return test
->name
? test
: NULL
;
365 /* Display list of valid tests */
366 static void list_tests(void)
368 const struct test
*test
;
370 fprintf( stdout
, "Valid test names:\n" );
371 for (test
= winetest_testlist
; test
->name
; test
++) fprintf( stdout
, " %s\n", test
->name
);
375 /* Run a named test, and return exit status */
376 static int run_test( const char *name
)
378 const struct test
*test
;
381 if (!(test
= find_test( name
)))
383 fprintf( stdout
, "Fatal: test '%s' does not exist.\n", name
);
386 successes
= failures
= todo_successes
= todo_failures
= 0;
387 tls_index
=TlsAlloc();
393 fprintf( stdout
, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
394 test
->name
, successes
+ failures
+ todo_successes
+ todo_failures
,
395 todo_successes
, failures
+ todo_failures
,
396 (failures
+ todo_failures
!= 1) ? "failures" : "failure",
399 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
404 /* Display usage and exit */
405 static void usage( const char *argv0
)
407 fprintf( stdout
, "Usage: %s test_name\n\n", argv0
);
414 int main( int argc
, char **argv
)
418 setvbuf (stdout
, NULL
, _IONBF
, 0);
420 winetest_argc
= argc
;
421 winetest_argv
= argv
;
423 if ((p
= getenv( "WINETEST_PLATFORM" ))) winetest_platform
= strdup(p
);
424 if ((p
= getenv( "WINETEST_DEBUG" ))) winetest_debug
= atoi(p
);
425 if ((p
= getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive
= atoi(p
);
426 if ((p
= getenv( "WINETEST_REPORT_SUCCESS"))) report_success
= atoi(p
);
429 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
430 return run_test( winetest_testlist
[0].name
);
433 if (!strcmp( argv
[1], "--list" ))
438 return run_test(argv
[1]);
441 #endif /* STANDALONE */
443 #endif /* __WINE_WINE_TEST_H */