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
28 /* prototype for dbgprint */
29 ULONG CDECL
DbgPrint(IN PCCH Format
,IN
...);
32 extern int winetest_debug
;
34 /* running in interactive mode? */
35 extern int winetest_interactive
;
37 /* current platform */
38 extern const char *winetest_platform
;
40 extern void winetest_set_location( const char* file
, int line
);
41 extern void winetest_start_todo( const char* platform
);
42 extern int winetest_loop_todo(void);
43 extern void winetest_end_todo( const char* platform
);
44 extern int winetest_get_mainargs( char*** pargv
);
47 #define START_TEST(name) \
48 static void func_##name(void); \
49 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
50 static void func_##name(void)
52 #define START_TEST(name) void func_##name(void)
57 extern int winetest_ok( int condition
, const char *msg
, ... ) __attribute__((format (printf
,2,3) ));
58 extern void winetest_trace( const char *msg
, ... ) __attribute__((format (printf
,1,2)));
62 extern int winetest_ok( int condition
, const char *msg
, ... );
63 extern void winetest_trace( const char *msg
, ... );
67 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
68 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
70 #define ok ok_(__FILE__, __LINE__)
71 #define trace trace_(__FILE__, __LINE__)
73 #define todo(platform) for (winetest_start_todo(platform); \
74 winetest_loop_todo(); \
75 winetest_end_todo(platform))
76 #define todo_wine todo("wine")
79 #ifdef NONAMELESSUNION
101 #ifdef NONAMELESSSTRUCT
103 # define S1(x) (x).s1
104 # define S2(x) (x).s2
105 # define S3(x) (x).s3
106 # define S4(x) (x).s4
107 # define S5(x) (x).s5
118 /************************************************************************/
119 /* Below is the implementation of the various functions, to be included
120 * directly into the generated testlist.c file.
121 * It is done that way so that the dlls can build the test routines with
122 * different includes or flags if needed.
135 extern const struct test winetest_testlist
[];
138 int winetest_debug
= 1;
140 /* interactive mode? */
141 int winetest_interactive
= 0;
143 /* current platform */
144 const char *winetest_platform
= "windows";
146 /* report successful tests (BOOL) */
147 static int report_success
= 0;
149 /* passing arguments around */
150 static int winetest_argc
;
151 static char** winetest_argv
;
153 static const struct test
*current_test
; /* test currently being run */
155 static LONG successes
; /* number of successful tests */
156 static LONG failures
; /* number of failures */
157 static LONG todo_successes
; /* number of successful tests inside todo block */
158 static LONG todo_failures
; /* number of failures inside todo block */
160 /* The following data must be kept track of on a per-thread basis */
163 const char* current_file
; /* file of current check */
164 int current_line
; /* line of current check */
165 int todo_level
; /* current todo nesting level */
168 static DWORD tls_index
;
170 static tls_data
* get_tls_data(void)
175 last_error
=GetLastError();
176 data
=TlsGetValue(tls_index
);
179 data
=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(tls_data
));
180 TlsSetValue(tls_index
,data
);
182 SetLastError(last_error
);
186 static void exit_process( int code
)
193 void winetest_set_location( const char* file
, int line
)
195 tls_data
* data
=get_tls_data();
196 data
->current_file
=strrchr(file
,'/');
197 if (data
->current_file
==NULL
)
198 data
->current_file
=strrchr(file
,'\\');
199 if (data
->current_file
==NULL
)
200 data
->current_file
=file
;
202 data
->current_file
++;
203 data
->current_line
=line
;
209 * - condition - condition to check;
210 * - msg test description;
211 * - file - test application source code file name of the check
212 * - line - test application source code file line number of the check
214 * 0 if condition does not have the expected value, 1 otherwise
216 int winetest_ok( int condition
, const char *msg
, ... )
219 tls_data
* data
=get_tls_data();
221 if (data
->todo_level
)
225 fprintf( stdout
, "%s:%d: Test succeeded inside todo block",
226 data
->current_file
, data
->current_line
);
229 va_start(valist
, msg
);
230 fprintf(stdout
,": ");
231 vfprintf(stdout
, msg
, valist
);
234 InterlockedIncrement(&todo_failures
);
237 else InterlockedIncrement(&todo_successes
);
243 fprintf( stdout
, "%s:%d: Test failed",
244 data
->current_file
, data
->current_line
);
248 va_start(valist
, msg
);
249 fprintf( stdout
,": ");
250 vfprintf(stdout
, msg
, valist
);
251 vsprintf(string
, msg
, valist
);
252 DbgPrint( "%s:%d: Test failed: %s\n",
253 data
->current_file
, data
->current_line
, string
);
258 DbgPrint( "%s:%d: Test failed\n",
259 data
->current_file
, data
->current_line
);
261 fputc( '\n', stdout
);
262 InterlockedIncrement(&failures
);
268 fprintf( stdout
, "%s:%d: Test succeeded\n",
269 data
->current_file
, data
->current_line
);
270 InterlockedIncrement(&successes
);
276 void winetest_trace( const char *msg
, ... )
279 tls_data
* data
=get_tls_data();
281 if (winetest_debug
> 0)
284 fprintf( stdout
, "%s:%d:", data
->current_file
, data
->current_line
);
285 va_start(valist
, msg
);
286 vfprintf(stdout
, msg
, valist
);
287 DbgPrint( "%s:%d: %s", data
->current_file
, data
->current_line
, string
);
292 void winetest_start_todo( const char* platform
)
294 tls_data
* data
=get_tls_data();
295 if (strcmp(winetest_platform
,platform
)==0)
297 data
->todo_do_loop
=1;
300 int winetest_loop_todo(void)
302 tls_data
* data
=get_tls_data();
303 int do_loop
=data
->todo_do_loop
;
304 data
->todo_do_loop
=0;
308 void winetest_end_todo( const char* platform
)
310 if (strcmp(winetest_platform
,platform
)==0)
312 tls_data
* data
=get_tls_data();
317 int winetest_get_mainargs( char*** pargv
)
319 *pargv
= winetest_argv
;
320 return winetest_argc
;
323 /* Find a test by name */
324 static const struct test
*find_test( const char *name
)
326 const struct test
*test
;
330 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
331 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
333 if (len
> 2 && !strcmp( name
+ len
- 2, ".c" )) len
-= 2;
335 for (test
= winetest_testlist
; test
->name
; test
++)
337 if (!strncmp( test
->name
, name
, len
) && !test
->name
[len
]) break;
339 return test
->name
? test
: NULL
;
343 /* Display list of valid tests */
344 static void list_tests(void)
346 const struct test
*test
;
348 fprintf( stdout
, "Valid test names:\n" );
349 for (test
= winetest_testlist
; test
->name
; test
++) fprintf( stdout
, " %s\n", test
->name
);
353 /* Run a named test, and return exit status */
354 static int run_test( const char *name
)
356 const struct test
*test
;
359 if (!(test
= find_test( name
)))
361 fprintf( stdout
, "Fatal: test '%s' does not exist.\n", name
);
364 successes
= failures
= todo_successes
= todo_failures
= 0;
365 tls_index
=TlsAlloc();
371 fprintf( stdout
, "%s: %ld tests executed, %ld marked as todo, %ld %s.\n",
372 name
, successes
+ failures
+ todo_successes
+ todo_failures
,
373 todo_successes
, failures
+ todo_failures
,
374 (failures
+ todo_failures
!= 1) ? "failures" : "failure" );
376 status
= (failures
+ todo_failures
< 255) ? failures
+ todo_failures
: 255;
381 /* Display usage and exit */
382 static void usage( const char *argv0
)
384 fprintf( stdout
, "Usage: %s test_name\n\n", argv0
);
391 int main( int argc
, char **argv
)
395 setvbuf (stdout
, NULL
, _IONBF
, 0);
397 winetest_argc
= argc
;
398 winetest_argv
= argv
;
400 if ((p
= getenv( "WINETEST_PLATFORM" ))) winetest_platform
= strdup(p
);
401 if ((p
= getenv( "WINETEST_DEBUG" ))) winetest_debug
= atoi(p
);
402 if ((p
= getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive
= atoi(p
);
403 if ((p
= getenv( "WINETEST_REPORT_SUCCESS"))) report_success
= atoi(p
);
406 if (winetest_testlist
[0].name
&& !winetest_testlist
[1].name
) /* only one test */
407 return run_test( winetest_testlist
[0].name
);
410 if (!strcmp( argv
[1], "--list" ))
415 return run_test(argv
[1]);
418 #endif /* STANDALONE */
420 #endif /* __WINE_TEST_H */