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