This commit was manufactured by cvs2svn to create branch 'captive'.
[reactos.git] / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #ifndef __WINE_TEST_H
22 #define __WINE_TEST_H
23
24 #include <stdarg.h>
25 #include "windows.h"
26
27 /* debug level */
28 extern int winetest_debug;
29
30 /* running in interactive mode? */
31 extern int winetest_interactive;
32
33 /* current platform */
34 extern const char *winetest_platform;
35
36 extern void winetest_set_location( const char* file, int line );
37 extern void winetest_start_todo( const char* platform );
38 extern int winetest_loop_todo(void);
39 extern void winetest_end_todo( const char* platform );
40 extern int winetest_get_mainargs( char*** pargv );
41
42 #define START_TEST(name) void func_##name(void)
43
44 #ifdef __GNUC__
45
46 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
47 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
48
49 #else /* __GNUC__ */
50
51 extern int winetest_ok( int condition, const char *msg, ... );
52 extern void winetest_trace( const char *msg, ... );
53
54 #endif /* __GNUC__ */
55
56 #define ok_(file, line)     (winetest_set_location(file, line), 0) ? 0 : winetest_ok
57 #define trace_(file, line)  (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
58
59 #define ok     ok_(__FILE__, __LINE__)
60 #define trace  trace_(__FILE__, __LINE__)
61
62 #define todo(platform) for (winetest_start_todo(platform); \
63                             winetest_loop_todo(); \
64                             winetest_end_todo(platform))
65 #define todo_wine      todo("wine")
66
67
68 /************************************************************************/
69 /* Below is the implementation of the various functions, to be included
70  * directly into the generated testlist.c file.
71  * It is done that way so that the dlls can build the test routines with
72  * different includes or flags if needed.
73  */
74
75 #ifdef WINETEST_WANT_MAIN
76
77 /* debug level */
78 int winetest_debug = 1;
79
80 /* interactive mode? */
81 int winetest_interactive = 0;
82
83 /* current platform */
84 const char *winetest_platform = "windows";
85
86 /* report successful tests (BOOL) */
87 static int report_success = 0;
88
89 /* passing arguments around */
90 static int winetest_argc;
91 static char** winetest_argv;
92
93 static const struct test *current_test; /* test currently being run */
94
95 static LONG successes;       /* number of successful tests */
96 static LONG failures;        /* number of failures */
97 static LONG todo_successes;  /* number of successful tests inside todo block */
98 static LONG todo_failures;   /* number of failures inside todo block */
99
100 /* The following data must be kept track of on a per-thread basis */
101 typedef struct
102 {
103     const char* current_file;        /* file of current check */
104     int current_line;                /* line of current check */
105     int todo_level;                  /* current todo nesting level */
106     int todo_do_loop;
107 } tls_data;
108 static DWORD tls_index;
109
110 static tls_data* get_tls_data(void)
111 {
112     tls_data* data;
113     DWORD last_error;
114
115     last_error=GetLastError();
116     data=TlsGetValue(tls_index);
117     if (!data)
118     {
119         data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
120         TlsSetValue(tls_index,data);
121     }
122     SetLastError(last_error);
123     return data;
124 }
125
126 static void exit_process( int code )
127 {
128     fflush( stdout );
129     ExitProcess( code );
130 }
131
132
133 void winetest_set_location( const char* file, int line )
134 {
135     tls_data* data=get_tls_data();
136     data->current_file=strrchr(file,'/');
137     if (data->current_file==NULL)
138         data->current_file=strrchr(file,'\\');
139     if (data->current_file==NULL)
140         data->current_file=file;
141     else
142         data->current_file++;
143     data->current_line=line;
144 }
145
146 /*
147  * Checks condition.
148  * Parameters:
149  *   - condition - condition to check;
150  *   - msg test description;
151  *   - file - test application source code file name of the check
152  *   - line - test application source code file line number of the check
153  * Return:
154  *   0 if condition does not have the expected value, 1 otherwise
155  */
156 int winetest_ok( int condition, const char *msg, ... )
157 {
158     va_list valist;
159     tls_data* data=get_tls_data();
160
161     if (data->todo_level)
162     {
163         if (condition)
164         {
165             fprintf( stdout, "%s:%d: Test succeeded inside todo block",
166                      data->current_file, data->current_line );
167             if (msg && msg[0])
168             {
169                 va_start(valist, msg);
170                 fprintf(stdout,": ");
171                 vfprintf(stdout, msg, valist);
172                 va_end(valist);
173             }
174             fputc( '\n', stdout );
175             InterlockedIncrement(&todo_failures);
176             return 0;
177         }
178         else InterlockedIncrement(&todo_successes);
179     }
180     else
181     {
182         if (!condition)
183         {
184             fprintf( stdout, "%s:%d: Test failed",
185                      data->current_file, data->current_line );
186             if (msg && msg[0])
187             {
188                 va_start(valist, msg);
189                 fprintf( stdout,": ");
190                 vfprintf(stdout, msg, valist);
191                 va_end(valist);
192             }
193             fputc( '\n', stdout );
194             InterlockedIncrement(&failures);
195             return 0;
196         }
197         else
198         {
199             if (report_success)
200                 fprintf( stdout, "%s:%d: Test succeeded\n",
201                          data->current_file, data->current_line);
202             InterlockedIncrement(&successes);
203         }
204     }
205     return 1;
206 }
207
208 void winetest_trace( const char *msg, ... )
209 {
210     va_list valist;
211     tls_data* data=get_tls_data();
212
213     if (winetest_debug > 0)
214     {
215         fprintf( stdout, "%s:%d:", data->current_file, data->current_line );
216         va_start(valist, msg);
217         vfprintf(stdout, msg, valist);
218         va_end(valist);
219     }
220 }
221
222 void winetest_start_todo( const char* platform )
223 {
224     tls_data* data=get_tls_data();
225     if (strcmp(winetest_platform,platform)==0)
226         data->todo_level++;
227     data->todo_do_loop=1;
228 }
229
230 int winetest_loop_todo(void)
231 {
232     tls_data* data=get_tls_data();
233     int do_loop=data->todo_do_loop;
234     data->todo_do_loop=0;
235     return do_loop;
236 }
237
238 void winetest_end_todo( const char* platform )
239 {
240     if (strcmp(winetest_platform,platform)==0)
241     {
242         tls_data* data=get_tls_data();
243         data->todo_level--;
244     }
245 }
246
247 int winetest_get_mainargs( char*** pargv )
248 {
249     *pargv = winetest_argv;
250     return winetest_argc;
251 }
252
253 /* Find a test by name */
254 static const struct test *find_test( const char *name )
255 {
256     const struct test *test;
257     const char *p;
258     int len;
259
260     if ((p = strrchr( name, '/' ))) name = p + 1;
261     if ((p = strrchr( name, '\\' ))) name = p + 1;
262     len = strlen(name);
263     if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
264
265     for (test = winetest_testlist; test->name; test++)
266     {
267         if (!strncmp( test->name, name, len ) && !test->name[len]) break;
268     }
269     return test->name ? test : NULL;
270 }
271
272
273 /* Run a named test, and return exit status */
274 static int run_test( const char *name )
275 {
276     const struct test *test;
277     int status;
278
279     if (!(test = find_test( name )))
280     {
281         fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
282         exit_process(1);
283     }
284     successes = failures = todo_successes = todo_failures = 0;
285     tls_index=TlsAlloc();
286     current_test = test;
287     test->func();
288
289     if (winetest_debug)
290     {
291         fprintf( stdout, "%s: %ld tests executed, %ld marked as todo, %ld %s.\n",
292                  name, successes + failures + todo_successes + todo_failures,
293                  todo_successes, failures + todo_failures,
294                  (failures + todo_failures != 1) ? "failures" : "failure" );
295     }
296     status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
297     return status;
298 }
299
300
301 /* Display usage and exit */
302 static void usage( const char *argv0 )
303 {
304     const struct test *test;
305
306     fprintf( stdout, "Usage: %s test_name\n", argv0 );
307     fprintf( stdout, "\nValid test names:\n" );
308     for (test = winetest_testlist; test->name; test++) fprintf( stdout, "    %s\n", test->name );
309     exit_process(1);
310 }
311
312
313 /* main function */
314 int main( int argc, char **argv )
315 {
316     char *p;
317
318     winetest_argc = argc;
319     winetest_argv = argv;
320
321     if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = p;
322     if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
323     if ((p = getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive = atoi(p);
324     if ((p = getenv( "WINETEST_REPORT_SUCCESS"))) report_success = atoi(p);
325     if (!argv[1]) usage( argv[0] );
326
327     return run_test(argv[1]);
328 }
329
330 #endif  /* WINETEST_WANT_MAIN */
331
332 #endif  /* __WINE_TEST_H */