Big merge: thanks alex and greatlord. Not a complete merge but most
[reactos.git] / reactos / include / reactos / 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_TEST_H
22 #define __WINE_TEST_H
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <windows.h>
27
28 /* prototype for dbgprint */
29 ULONG CDECL DbgPrint(IN PCCH Format,IN ...);
30
31 /* debug level */
32 extern int winetest_debug;
33
34 /* running in interactive mode? */
35 extern int winetest_interactive;
36
37 /* current platform */
38 extern const char *winetest_platform;
39
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 );
45
46 #ifdef STANDALONE
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)
51 #else
52 #define START_TEST(name) void func_##name(void)
53 #endif
54
55 #ifdef __GNUC__
56
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)));
59
60 #else /* __GNUC__ */
61
62 extern int winetest_ok( int condition, const char *msg, ... );
63 extern void winetest_trace( const char *msg, ... );
64
65 #endif /* __GNUC__ */
66
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
69
70 #define ok ok_(__FILE__, __LINE__)
71 #define trace trace_(__FILE__, __LINE__)
72
73 #define todo(platform) for (winetest_start_todo(platform); \
74 winetest_loop_todo(); \
75 winetest_end_todo(platform))
76 #define todo_wine todo("wine")
77
78
79 #ifdef NONAMELESSUNION
80 # define U(x) (x).u
81 # define U1(x) (x).u1
82 # define U2(x) (x).u2
83 # define U3(x) (x).u3
84 # define U4(x) (x).u4
85 # define U5(x) (x).u5
86 # define U6(x) (x).u6
87 # define U7(x) (x).u7
88 # define U8(x) (x).u8
89 #else
90 # define U(x) (x)
91 # define U1(x) (x)
92 # define U2(x) (x)
93 # define U3(x) (x)
94 # define U4(x) (x)
95 # define U5(x) (x)
96 # define U6(x) (x)
97 # define U7(x) (x)
98 # define U8(x) (x)
99 #endif
100
101 #ifdef NONAMELESSSTRUCT
102 # define S(x) (x).s
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
108 #else
109 # define S(x) (x)
110 # define S1(x) (x)
111 # define S2(x) (x)
112 # define S3(x) (x)
113 # define S4(x) (x)
114 # define S5(x) (x)
115 #endif
116
117
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.
123 */
124
125 #ifdef STANDALONE
126
127 #include <stdio.h>
128
129 struct test
130 {
131 const char *name;
132 void (*func)(void);
133 };
134
135 extern const struct test winetest_testlist[];
136
137 /* debug level */
138 int winetest_debug = 1;
139
140 /* interactive mode? */
141 int winetest_interactive = 0;
142
143 /* current platform */
144 const char *winetest_platform = "windows";
145
146 /* report successful tests (BOOL) */
147 static int report_success = 0;
148
149 /* passing arguments around */
150 static int winetest_argc;
151 static char** winetest_argv;
152
153 static const struct test *current_test; /* test currently being run */
154
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 */
159
160 /* The following data must be kept track of on a per-thread basis */
161 typedef struct
162 {
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 */
166 int todo_do_loop;
167 } tls_data;
168 static DWORD tls_index;
169
170 static tls_data* get_tls_data(void)
171 {
172 tls_data* data;
173 DWORD last_error;
174
175 last_error=GetLastError();
176 data=TlsGetValue(tls_index);
177 if (!data)
178 {
179 data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
180 TlsSetValue(tls_index,data);
181 }
182 SetLastError(last_error);
183 return data;
184 }
185
186 static void exit_process( int code )
187 {
188 fflush( stdout );
189 ExitProcess( code );
190 }
191
192
193 void winetest_set_location( const char* file, int line )
194 {
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;
201 else
202 data->current_file++;
203 data->current_line=line;
204 }
205
206 /*
207 * Checks condition.
208 * Parameters:
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
213 * Return:
214 * 0 if condition does not have the expected value, 1 otherwise
215 */
216 int winetest_ok( int condition, const char *msg, ... )
217 {
218 va_list valist;
219 tls_data* data=get_tls_data();
220
221 if (data->todo_level)
222 {
223 if (condition)
224 {
225 fprintf( stdout, "%s:%d: Test succeeded inside todo block",
226 data->current_file, data->current_line );
227 if (msg[0])
228 {
229 va_start(valist, msg);
230 fprintf(stdout,": ");
231 vfprintf(stdout, msg, valist);
232 va_end(valist);
233 }
234 InterlockedIncrement(&todo_failures);
235 return 0;
236 }
237 else InterlockedIncrement(&todo_successes);
238 }
239 else
240 {
241 if (!condition)
242 {
243 fprintf( stdout, "%s:%d: Test failed",
244 data->current_file, data->current_line );
245 if (msg[0])
246 {
247 char string[1024];
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 );
254 va_end(valist);
255 }
256 else
257 {
258 DbgPrint( "%s:%d: Test failed\n",
259 data->current_file, data->current_line );
260 }
261 fputc( '\n', stdout );
262 InterlockedIncrement(&failures);
263 return 0;
264 }
265 else
266 {
267 if (report_success)
268 fprintf( stdout, "%s:%d: Test succeeded\n",
269 data->current_file, data->current_line);
270 InterlockedIncrement(&successes);
271 }
272 }
273 return 1;
274 }
275
276 void winetest_trace( const char *msg, ... )
277 {
278 va_list valist;
279 tls_data* data=get_tls_data();
280
281 if (winetest_debug > 0)
282 {
283 char string[1024];
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);
288 va_end(valist);
289 }
290 }
291
292 void winetest_start_todo( const char* platform )
293 {
294 tls_data* data=get_tls_data();
295 if (strcmp(winetest_platform,platform)==0)
296 data->todo_level++;
297 data->todo_do_loop=1;
298 }
299
300 int winetest_loop_todo(void)
301 {
302 tls_data* data=get_tls_data();
303 int do_loop=data->todo_do_loop;
304 data->todo_do_loop=0;
305 return do_loop;
306 }
307
308 void winetest_end_todo( const char* platform )
309 {
310 if (strcmp(winetest_platform,platform)==0)
311 {
312 tls_data* data=get_tls_data();
313 data->todo_level--;
314 }
315 }
316
317 int winetest_get_mainargs( char*** pargv )
318 {
319 *pargv = winetest_argv;
320 return winetest_argc;
321 }
322
323 /* Find a test by name */
324 static const struct test *find_test( const char *name )
325 {
326 const struct test *test;
327 const char *p;
328 int len;
329
330 if ((p = strrchr( name, '/' ))) name = p + 1;
331 if ((p = strrchr( name, '\\' ))) name = p + 1;
332 len = strlen(name);
333 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
334
335 for (test = winetest_testlist; test->name; test++)
336 {
337 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
338 }
339 return test->name ? test : NULL;
340 }
341
342
343 /* Display list of valid tests */
344 static void list_tests(void)
345 {
346 const struct test *test;
347
348 fprintf( stdout, "Valid test names:\n" );
349 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
350 }
351
352
353 /* Run a named test, and return exit status */
354 static int run_test( const char *name )
355 {
356 const struct test *test;
357 int status;
358
359 if (!(test = find_test( name )))
360 {
361 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
362 exit_process(1);
363 }
364 successes = failures = todo_successes = todo_failures = 0;
365 tls_index=TlsAlloc();
366 current_test = test;
367 test->func();
368
369 if (winetest_debug)
370 {
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" );
375 }
376 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
377 return status;
378 }
379
380
381 /* Display usage and exit */
382 static void usage( const char *argv0 )
383 {
384 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
385 list_tests();
386 exit_process(1);
387 }
388
389
390 /* main function */
391 int main( int argc, char **argv )
392 {
393 char *p;
394
395 setvbuf (stdout, NULL, _IONBF, 0);
396
397 winetest_argc = argc;
398 winetest_argv = argv;
399
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);
404 if (!argv[1])
405 {
406 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
407 return run_test( winetest_testlist[0].name );
408 usage( argv[0] );
409 }
410 if (!strcmp( argv[1], "--list" ))
411 {
412 list_tests();
413 return 0;
414 }
415 return run_test(argv[1]);
416 }
417
418 #endif /* STANDALONE */
419
420 #endif /* __WINE_TEST_H */