include: Fix the 64-bit value of magic handle constants.
[wine] / include / wine / test.h
1 /*
2  * Definitions for Wine C unit tests.
3  *
4  * Copyright (C) 2002 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #ifndef __WINE_WINE_TEST_H
22 #define __WINE_WINE_TEST_H
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <windef.h>
27 #include <winbase.h>
28
29 #ifdef __WINE_CONFIG_H
30 #error config.h should not be used in Wine tests
31 #endif
32 #ifdef __WINE_WINE_LIBRARY_H
33 #error wine/library.h should not be used in Wine tests
34 #endif
35 #ifdef __WINE_WINE_UNICODE_H
36 #error wine/unicode.h should not be used in Wine tests
37 #endif
38 #ifdef __WINE_WINE_DEBUG_H
39 #error wine/debug.h should not be used in Wine tests
40 #endif
41
42 #ifndef INVALID_FILE_ATTRIBUTES
43 #define INVALID_FILE_ATTRIBUTES  (~0u)
44 #endif
45 #ifndef INVALID_SET_FILE_POINTER
46 #define INVALID_SET_FILE_POINTER (~0u)
47 #endif
48
49 /* debug level */
50 extern int winetest_debug;
51
52 /* running in interactive mode? */
53 extern int winetest_interactive;
54
55 /* current platform */
56 extern const char *winetest_platform;
57
58 extern void winetest_set_location( const char* file, int line );
59 extern void winetest_start_todo( const char* platform );
60 extern int winetest_loop_todo(void);
61 extern void winetest_end_todo( const char* platform );
62 extern int winetest_get_mainargs( char*** pargv );
63 extern void winetest_wait_child_process( HANDLE process );
64
65 #ifdef STANDALONE
66 #define START_TEST(name) \
67   static void func_##name(void); \
68   const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
69   static void func_##name(void)
70 #else
71 #define START_TEST(name) void func_##name(void)
72 #endif
73
74 extern int broken( int condition );
75 extern int winetest_vok( int condition, const char *msg, va_list ap );
76 extern void winetest_vskip( const char *msg, va_list ap );
77
78 #ifdef __GNUC__
79
80 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
81 extern void winetest_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
82 extern void winetest_win_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
83 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
84
85 #else /* __GNUC__ */
86
87 extern int winetest_ok( int condition, const char *msg, ... );
88 extern void winetest_skip( const char *msg, ... );
89 extern void winetest_win_skip( const char *msg, ... );
90 extern void winetest_trace( const char *msg, ... );
91
92 #endif /* __GNUC__ */
93
94 #define ok_(file, line)       (winetest_set_location(file, line), 0) ? 0 : winetest_ok
95 #define skip_(file, line)     (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
96 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
97 #define trace_(file, line)    (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
98
99 #define ok       ok_(__FILE__, __LINE__)
100 #define skip     skip_(__FILE__, __LINE__)
101 #define win_skip win_skip_(__FILE__, __LINE__)
102 #define trace    trace_(__FILE__, __LINE__)
103
104 #define todo(platform) for (winetest_start_todo(platform); \
105                             winetest_loop_todo(); \
106                             winetest_end_todo(platform))
107 #define todo_wine      todo("wine")
108
109
110 #ifdef NONAMELESSUNION
111 # define U(x)  (x).u
112 # define U1(x) (x).u1
113 # define U2(x) (x).u2
114 # define U3(x) (x).u3
115 # define U4(x) (x).u4
116 # define U5(x) (x).u5
117 # define U6(x) (x).u6
118 # define U7(x) (x).u7
119 # define U8(x) (x).u8
120 #else
121 # define U(x)  (x)
122 # define U1(x) (x)
123 # define U2(x) (x)
124 # define U3(x) (x)
125 # define U4(x) (x)
126 # define U5(x) (x)
127 # define U6(x) (x)
128 # define U7(x) (x)
129 # define U8(x) (x)
130 #endif
131
132 #ifdef NONAMELESSSTRUCT
133 # define S(x)  (x).s
134 # define S1(x) (x).s1
135 # define S2(x) (x).s2
136 # define S3(x) (x).s3
137 # define S4(x) (x).s4
138 # define S5(x) (x).s5
139 #else
140 # define S(x)  (x)
141 # define S1(x) (x)
142 # define S2(x) (x)
143 # define S3(x) (x)
144 # define S4(x) (x)
145 # define S5(x) (x)
146 #endif
147
148
149 /************************************************************************/
150 /* Below is the implementation of the various functions, to be included
151  * directly into the generated testlist.c file.
152  * It is done that way so that the dlls can build the test routines with
153  * different includes or flags if needed.
154  */
155
156 #ifdef STANDALONE
157
158 #include <stdio.h>
159
160 struct test
161 {
162     const char *name;
163     void (*func)(void);
164 };
165
166 extern const struct test winetest_testlist[];
167
168 /* debug level */
169 int winetest_debug = 1;
170
171 /* interactive mode? */
172 int winetest_interactive = 0;
173
174 /* current platform */
175 const char *winetest_platform = "windows";
176
177 /* report successful tests (BOOL) */
178 static int report_success = 0;
179
180 /* passing arguments around */
181 static int winetest_argc;
182 static char** winetest_argv;
183
184 static const struct test *current_test; /* test currently being run */
185
186 static LONG successes;       /* number of successful tests */
187 static LONG failures;        /* number of failures */
188 static LONG skipped;         /* number of skipped test chunks */
189 static LONG todo_successes;  /* number of successful tests inside todo block */
190 static LONG todo_failures;   /* number of failures inside todo block */
191
192 /* The following data must be kept track of on a per-thread basis */
193 typedef struct
194 {
195     const char* current_file;        /* file of current check */
196     int current_line;                /* line of current check */
197     int todo_level;                  /* current todo nesting level */
198     int todo_do_loop;
199 } tls_data;
200 static DWORD tls_index;
201
202 static tls_data* get_tls_data(void)
203 {
204     tls_data* data;
205     DWORD last_error;
206
207     last_error=GetLastError();
208     data=TlsGetValue(tls_index);
209     if (!data)
210     {
211         data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
212         TlsSetValue(tls_index,data);
213     }
214     SetLastError(last_error);
215     return data;
216 }
217
218 static void exit_process( int code )
219 {
220     fflush( stdout );
221     ExitProcess( code );
222 }
223
224
225 void winetest_set_location( const char* file, int line )
226 {
227     tls_data* data=get_tls_data();
228     data->current_file=strrchr(file,'/');
229     if (data->current_file==NULL)
230         data->current_file=strrchr(file,'\\');
231     if (data->current_file==NULL)
232         data->current_file=file;
233     else
234         data->current_file++;
235     data->current_line=line;
236 }
237
238 int broken( int condition )
239 {
240     return (strcmp(winetest_platform, "windows") == 0) && condition;
241 }
242
243 /*
244  * Checks condition.
245  * Parameters:
246  *   - condition - condition to check;
247  *   - msg test description;
248  *   - file - test application source code file name of the check
249  *   - line - test application source code file line number of the check
250  * Return:
251  *   0 if condition does not have the expected value, 1 otherwise
252  */
253 int winetest_vok( int condition, const char *msg, va_list args )
254 {
255     tls_data* data=get_tls_data();
256
257     if (data->todo_level)
258     {
259         if (condition)
260         {
261             fprintf( stdout, "%s:%d: Test succeeded inside todo block",
262                      data->current_file, data->current_line );
263             if (msg[0])
264             {
265                 fprintf(stdout,": ");
266                 vfprintf(stdout, msg, args);
267             }
268             InterlockedIncrement(&todo_failures);
269             return 0;
270         }
271         else InterlockedIncrement(&todo_successes);
272     }
273     else
274     {
275         if (!condition)
276         {
277             fprintf( stdout, "%s:%d: Test failed",
278                      data->current_file, data->current_line );
279             if (msg[0])
280             {
281                 fprintf( stdout,": ");
282                 vfprintf(stdout, msg, args);
283             }
284             InterlockedIncrement(&failures);
285             return 0;
286         }
287         else
288         {
289             if (report_success)
290                 fprintf( stdout, "%s:%d: Test succeeded\n",
291                          data->current_file, data->current_line);
292             InterlockedIncrement(&successes);
293         }
294     }
295     return 1;
296 }
297
298 int winetest_ok( int condition, const char *msg, ... )
299 {
300     va_list valist;
301     int rc;
302
303     va_start(valist, msg);
304     rc=winetest_vok(condition, msg, valist);
305     va_end(valist);
306     return rc;
307 }
308
309 void winetest_trace( const char *msg, ... )
310 {
311     va_list valist;
312     tls_data* data=get_tls_data();
313
314     if (winetest_debug > 0)
315     {
316         fprintf( stdout, "%s:%d: ", data->current_file, data->current_line );
317         va_start(valist, msg);
318         vfprintf(stdout, msg, valist);
319         va_end(valist);
320     }
321 }
322
323 void winetest_vskip( const char *msg, va_list args )
324 {
325     tls_data* data=get_tls_data();
326
327     fprintf( stdout, "%s:%d: Tests skipped: ", data->current_file, data->current_line );
328     vfprintf(stdout, msg, args);
329     skipped++;
330 }
331
332 void winetest_skip( const char *msg, ... )
333 {
334     va_list valist;
335     va_start(valist, msg);
336     winetest_vskip(msg, valist);
337     va_end(valist);
338 }
339
340 void winetest_win_skip( const char *msg, ... )
341 {
342     va_list valist;
343     va_start(valist, msg);
344     if (strcmp(winetest_platform, "windows") == 0)
345         winetest_vskip(msg, valist);
346     else
347         winetest_vok(0, msg, valist);
348     va_end(valist);
349 }
350
351 void winetest_start_todo( const char* platform )
352 {
353     tls_data* data=get_tls_data();
354     if (strcmp(winetest_platform,platform)==0)
355         data->todo_level++;
356     data->todo_do_loop=1;
357 }
358
359 int winetest_loop_todo(void)
360 {
361     tls_data* data=get_tls_data();
362     int do_loop=data->todo_do_loop;
363     data->todo_do_loop=0;
364     return do_loop;
365 }
366
367 void winetest_end_todo( const char* platform )
368 {
369     if (strcmp(winetest_platform,platform)==0)
370     {
371         tls_data* data=get_tls_data();
372         data->todo_level--;
373     }
374 }
375
376 int winetest_get_mainargs( char*** pargv )
377 {
378     *pargv = winetest_argv;
379     return winetest_argc;
380 }
381
382 void winetest_wait_child_process( HANDLE process )
383 {
384     DWORD exit_code = 1;
385
386     if (WaitForSingleObject( process, 30000 ))
387         fprintf( stdout, "%s: child process wait failed\n", current_test->name );
388     else
389         GetExitCodeProcess( process, &exit_code );
390
391     if (exit_code)
392     {
393         if (exit_code > 255)
394         {
395             fprintf( stdout, "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
396             InterlockedIncrement( &failures );
397         }
398         else
399         {
400             fprintf( stdout, "%s: %u failures in child process\n",
401                      current_test->name, exit_code );
402             while (exit_code-- > 0)
403                 InterlockedIncrement(&failures);
404         }
405     }
406 }
407
408 /* Find a test by name */
409 static const struct test *find_test( const char *name )
410 {
411     const struct test *test;
412     const char *p;
413     size_t len;
414
415     if ((p = strrchr( name, '/' ))) name = p + 1;
416     if ((p = strrchr( name, '\\' ))) name = p + 1;
417     len = strlen(name);
418     if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
419
420     for (test = winetest_testlist; test->name; test++)
421     {
422         if (!strncmp( test->name, name, len ) && !test->name[len]) break;
423     }
424     return test->name ? test : NULL;
425 }
426
427
428 /* Display list of valid tests */
429 static void list_tests(void)
430 {
431     const struct test *test;
432
433     fprintf( stdout, "Valid test names:\n" );
434     for (test = winetest_testlist; test->name; test++) fprintf( stdout, "    %s\n", test->name );
435 }
436
437
438 /* Run a named test, and return exit status */
439 static int run_test( const char *name )
440 {
441     const struct test *test;
442     int status;
443
444     if (!(test = find_test( name )))
445     {
446         fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
447         exit_process(1);
448     }
449     successes = failures = todo_successes = todo_failures = 0;
450     tls_index=TlsAlloc();
451     current_test = test;
452     test->func();
453
454     if (winetest_debug)
455     {
456         fprintf( stdout, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
457                  test->name, successes + failures + todo_successes + todo_failures,
458                  todo_successes, failures + todo_failures,
459                  (failures + todo_failures != 1) ? "failures" : "failure",
460                  skipped );
461     }
462     status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
463     return status;
464 }
465
466
467 /* Display usage and exit */
468 static void usage( const char *argv0 )
469 {
470     fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
471     list_tests();
472     exit_process(1);
473 }
474
475
476 /* main function */
477 int main( int argc, char **argv )
478 {
479     char p[128];
480
481     setvbuf (stdout, NULL, _IONBF, 0);
482
483     winetest_argc = argc;
484     winetest_argv = argv;
485
486     if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) )) winetest_platform = strdup(p);
487     if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
488     if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
489     if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
490
491     if (!argv[1])
492     {
493         if (winetest_testlist[0].name && !winetest_testlist[1].name)  /* only one test */
494             return run_test( winetest_testlist[0].name );
495         usage( argv[0] );
496     }
497     if (!strcmp( argv[1], "--list" ))
498     {
499         list_tests();
500         return 0;
501     }
502     return run_test(argv[1]);
503 }
504
505 #endif  /* STANDALONE */
506
507 #endif  /* __WINE_WINE_TEST_H */