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