Merge aicom-network-branch (without NDIS changes for now)
[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
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 extern const char *wine_dbgstr_wn( const WCHAR *str, int n );
66 static inline const char *wine_dbgstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
67
68 #ifdef STANDALONE
69 #define START_TEST(name) \
70 static void func_##name(void); \
71 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
72 static void func_##name(void)
73 #else
74 #define START_TEST(name) void func_##name(void)
75 #endif
76
77 extern int broken( int condition );
78 extern int winetest_vok( int condition, const char *msg, va_list ap );
79 extern void winetest_vskip( const char *msg, va_list ap );
80
81 #ifdef __GNUC__
82
83 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
84 extern void winetest_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
85 extern void winetest_win_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
86 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
87
88 #else /* __GNUC__ */
89
90 extern int winetest_ok( int condition, const char *msg, ... );
91 extern void winetest_skip( const char *msg, ... );
92 extern void winetest_win_skip( const char *msg, ... );
93 extern void winetest_trace( const char *msg, ... );
94
95 #endif /* __GNUC__ */
96
97 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
98 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
99 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
100 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
101
102 #define ok ok_(__FILE__, __LINE__)
103 #define skip skip_(__FILE__, __LINE__)
104 #define win_skip win_skip_(__FILE__, __LINE__)
105 #define trace trace_(__FILE__, __LINE__)
106
107 #define todo(platform) for (winetest_start_todo(platform); \
108 winetest_loop_todo(); \
109 winetest_end_todo(platform))
110 #define todo_wine todo("wine")
111
112
113 #ifdef NONAMELESSUNION
114 # define U(x) (x).u
115 # define U1(x) (x).u1
116 # define U2(x) (x).u2
117 # define U3(x) (x).u3
118 # define U4(x) (x).u4
119 # define U5(x) (x).u5
120 # define U6(x) (x).u6
121 # define U7(x) (x).u7
122 # define U8(x) (x).u8
123 #else
124 # define U(x) (x)
125 # define U1(x) (x)
126 # define U2(x) (x)
127 # define U3(x) (x)
128 # define U4(x) (x)
129 # define U5(x) (x)
130 # define U6(x) (x)
131 # define U7(x) (x)
132 # define U8(x) (x)
133 #endif
134
135 #ifdef NONAMELESSSTRUCT
136 # define S(x) (x).s
137 # define S1(x) (x).s1
138 # define S2(x) (x).s2
139 # define S3(x) (x).s3
140 # define S4(x) (x).s4
141 # define S5(x) (x).s5
142 #else
143 # define S(x) (x)
144 # define S1(x) (x)
145 # define S2(x) (x)
146 # define S3(x) (x)
147 # define S4(x) (x)
148 # define S5(x) (x)
149 #endif
150
151
152 /************************************************************************/
153 /* Below is the implementation of the various functions, to be included
154 * directly into the generated testlist.c file.
155 * It is done that way so that the dlls can build the test routines with
156 * different includes or flags if needed.
157 */
158
159 #ifdef STANDALONE
160
161 #include <stdio.h>
162
163 struct test
164 {
165 const char *name;
166 void (*func)(void);
167 };
168
169 extern const struct test winetest_testlist[];
170
171 /* debug level */
172 int winetest_debug = 1;
173
174 /* interactive mode? */
175 int winetest_interactive = 0;
176
177 /* current platform */
178 const char *winetest_platform = "windows";
179
180 /* report successful tests (BOOL) */
181 static int report_success = 0;
182
183 /* passing arguments around */
184 static int winetest_argc;
185 static char** winetest_argv;
186
187 static const struct test *current_test; /* test currently being run */
188
189 static LONG successes; /* number of successful tests */
190 static LONG failures; /* number of failures */
191 static LONG skipped; /* number of skipped test chunks */
192 static LONG todo_successes; /* number of successful tests inside todo block */
193 static LONG todo_failures; /* number of failures inside todo block */
194
195 /* The following data must be kept track of on a per-thread basis */
196 typedef struct
197 {
198 const char* current_file; /* file of current check */
199 int current_line; /* line of current check */
200 int todo_level; /* current todo nesting level */
201 int todo_do_loop;
202 char *str_pos; /* position in debug buffer */
203 char strings[2000]; /* buffer for debug strings */
204 } tls_data;
205 static DWORD tls_index;
206
207 static tls_data* get_tls_data(void)
208 {
209 tls_data* data;
210 DWORD last_error;
211
212 last_error=GetLastError();
213 data=TlsGetValue(tls_index);
214 if (!data)
215 {
216 data=HeapAlloc(GetProcessHeap(), 0, sizeof(tls_data));
217 data->todo_level = 0;
218 data->str_pos = data->strings;
219 TlsSetValue(tls_index,data);
220 }
221 SetLastError(last_error);
222 return data;
223 }
224
225 /* allocate some tmp space for a string */
226 static char *get_temp_buffer( size_t n )
227 {
228 tls_data *data = get_tls_data();
229 char *res = data->str_pos;
230
231 if (res + n >= &data->strings[sizeof(data->strings)]) res = data->strings;
232 data->str_pos = res + n;
233 return res;
234 }
235
236 /* release extra space that we requested in gimme1() */
237 static void release_temp_buffer( char *ptr, size_t size )
238 {
239 tls_data *data = get_tls_data();
240 data->str_pos = ptr + size;
241 }
242
243 static void exit_process( int code )
244 {
245 fflush( stdout );
246 ExitProcess( code );
247 }
248
249
250 void winetest_set_location( const char* file, int line )
251 {
252 tls_data* data=get_tls_data();
253 data->current_file=strrchr(file,'/');
254 if (data->current_file==NULL)
255 data->current_file=strrchr(file,'\\');
256 if (data->current_file==NULL)
257 data->current_file=file;
258 else
259 data->current_file++;
260 data->current_line=line;
261 }
262
263 int broken( int condition )
264 {
265 return (strcmp(winetest_platform, "windows") == 0) && condition;
266 }
267
268 /*
269 * Checks condition.
270 * Parameters:
271 * - condition - condition to check;
272 * - msg test description;
273 * - file - test application source code file name of the check
274 * - line - test application source code file line number of the check
275 * Return:
276 * 0 if condition does not have the expected value, 1 otherwise
277 */
278 int winetest_vok( int condition, const char *msg, va_list args )
279 {
280 tls_data* data=get_tls_data();
281
282 if (data->todo_level)
283 {
284 if (condition)
285 {
286 fprintf( stdout, "%s:%d: Test succeeded inside todo block: ",
287 data->current_file, data->current_line );
288 vfprintf(stdout, msg, args);
289 InterlockedIncrement(&todo_failures);
290 return 0;
291 }
292 else
293 {
294 if (winetest_debug > 0)
295 {
296 fprintf( stdout, "%s:%d: Test marked todo: ",
297 data->current_file, data->current_line );
298 vfprintf(stdout, msg, args);
299 }
300 InterlockedIncrement(&todo_successes);
301 return 1;
302 }
303 }
304 else
305 {
306 if (!condition)
307 {
308 fprintf( stdout, "%s:%d: Test failed: ",
309 data->current_file, data->current_line );
310 vfprintf(stdout, msg, args);
311 InterlockedIncrement(&failures);
312 return 0;
313 }
314 else
315 {
316 if (report_success)
317 fprintf( stdout, "%s:%d: Test succeeded\n",
318 data->current_file, data->current_line);
319 InterlockedIncrement(&successes);
320 return 1;
321 }
322 }
323 }
324
325 int winetest_ok( int condition, const char *msg, ... )
326 {
327 va_list valist;
328 int rc;
329
330 va_start(valist, msg);
331 rc=winetest_vok(condition, msg, valist);
332 va_end(valist);
333 return rc;
334 }
335
336 void winetest_trace( const char *msg, ... )
337 {
338 va_list valist;
339 tls_data* data=get_tls_data();
340
341 if (winetest_debug > 0)
342 {
343 fprintf( stdout, "%s:%d: ", data->current_file, data->current_line );
344 va_start(valist, msg);
345 vfprintf(stdout, msg, valist);
346 va_end(valist);
347 }
348 }
349
350 void winetest_vskip( const char *msg, va_list args )
351 {
352 tls_data* data=get_tls_data();
353
354 fprintf( stdout, "%s:%d: Tests skipped: ", data->current_file, data->current_line );
355 vfprintf(stdout, msg, args);
356 skipped++;
357 }
358
359 void winetest_skip( const char *msg, ... )
360 {
361 va_list valist;
362 va_start(valist, msg);
363 winetest_vskip(msg, valist);
364 va_end(valist);
365 }
366
367 void winetest_win_skip( const char *msg, ... )
368 {
369 va_list valist;
370 va_start(valist, msg);
371 if (strcmp(winetest_platform, "windows") == 0)
372 winetest_vskip(msg, valist);
373 else
374 winetest_vok(0, msg, valist);
375 va_end(valist);
376 }
377
378 void winetest_start_todo( const char* platform )
379 {
380 tls_data* data=get_tls_data();
381 if (strcmp(winetest_platform,platform)==0)
382 data->todo_level++;
383 data->todo_do_loop=1;
384 }
385
386 int winetest_loop_todo(void)
387 {
388 tls_data* data=get_tls_data();
389 int do_loop=data->todo_do_loop;
390 data->todo_do_loop=0;
391 return do_loop;
392 }
393
394 void winetest_end_todo( const char* platform )
395 {
396 if (strcmp(winetest_platform,platform)==0)
397 {
398 tls_data* data=get_tls_data();
399 data->todo_level--;
400 }
401 }
402
403 int winetest_get_mainargs( char*** pargv )
404 {
405 *pargv = winetest_argv;
406 return winetest_argc;
407 }
408
409 void winetest_wait_child_process( HANDLE process )
410 {
411 DWORD exit_code = 1;
412
413 if (WaitForSingleObject( process, 30000 ))
414 fprintf( stdout, "%s: child process wait failed\n", current_test->name );
415 else
416 GetExitCodeProcess( process, &exit_code );
417
418 if (exit_code)
419 {
420 if (exit_code > 255)
421 {
422 fprintf( stdout, "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
423 InterlockedIncrement( &failures );
424 }
425 else
426 {
427 fprintf( stdout, "%s: %u failures in child process\n",
428 current_test->name, exit_code );
429 while (exit_code-- > 0)
430 InterlockedIncrement(&failures);
431 }
432 }
433 }
434
435 const char *wine_dbgstr_wn( const WCHAR *str, int n )
436 {
437 char *dst, *res;
438 size_t size;
439
440 if (!((ULONG_PTR)str >> 16))
441 {
442 if (!str) return "(null)";
443 res = get_temp_buffer( 6 );
444 sprintf( res, "#%04x", LOWORD(str) );
445 return res;
446 }
447 if (n == -1)
448 {
449 const WCHAR *end = str;
450 while (*end) end++;
451 n = end - str;
452 }
453 if (n < 0) n = 0;
454 size = 12 + min( 300, n * 5 );
455 dst = res = get_temp_buffer( size );
456 *dst++ = 'L';
457 *dst++ = '"';
458 while (n-- > 0 && dst <= res + size - 10)
459 {
460 WCHAR c = *str++;
461 switch (c)
462 {
463 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
464 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
465 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
466 case '"': *dst++ = '\\'; *dst++ = '"'; break;
467 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
468 default:
469 if (c >= ' ' && c <= 126)
470 *dst++ = c;
471 else
472 {
473 *dst++ = '\\';
474 sprintf(dst,"%04x",c);
475 dst+=4;
476 }
477 }
478 }
479 *dst++ = '"';
480 if (n > 0)
481 {
482 *dst++ = '.';
483 *dst++ = '.';
484 *dst++ = '.';
485 }
486 *dst++ = 0;
487 release_temp_buffer( res, dst - res );
488 return res;
489 }
490
491 /* Find a test by name */
492 static const struct test *find_test( const char *name )
493 {
494 const struct test *test;
495 const char *p;
496 size_t len;
497
498 if ((p = strrchr( name, '/' ))) name = p + 1;
499 if ((p = strrchr( name, '\\' ))) name = p + 1;
500 len = strlen(name);
501 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
502
503 for (test = winetest_testlist; test->name; test++)
504 {
505 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
506 }
507 return test->name ? test : NULL;
508 }
509
510
511 /* Display list of valid tests */
512 static void list_tests(void)
513 {
514 const struct test *test;
515
516 fprintf( stdout, "Valid test names:\n" );
517 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
518 }
519
520
521 /* Run a named test, and return exit status */
522 static int run_test( const char *name )
523 {
524 const struct test *test;
525 int status;
526
527 if (!(test = find_test( name )))
528 {
529 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
530 exit_process(1);
531 }
532 successes = failures = todo_successes = todo_failures = 0;
533 tls_index=TlsAlloc();
534 current_test = test;
535 test->func();
536
537 if (winetest_debug)
538 {
539 fprintf( stdout, "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
540 test->name, successes + failures + todo_successes + todo_failures,
541 todo_successes, failures + todo_failures,
542 (failures + todo_failures != 1) ? "failures" : "failure",
543 skipped );
544 }
545 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
546 return status;
547 }
548
549
550 /* Display usage and exit */
551 static void usage( const char *argv0 )
552 {
553 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
554 list_tests();
555 exit_process(1);
556 }
557
558
559 /* main function */
560 int main( int argc, char **argv )
561 {
562 char p[128];
563
564 setvbuf (stdout, NULL, _IONBF, 0);
565
566 winetest_argc = argc;
567 winetest_argv = argv;
568
569 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) )) winetest_platform = _strdup(p);
570 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
571 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
572 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
573
574 if (!argv[1])
575 {
576 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
577 return run_test( winetest_testlist[0].name );
578 usage( argv[0] );
579 }
580 if (!strcmp( argv[1], "--list" ))
581 {
582 list_tests();
583 return 0;
584 }
585 return run_test(argv[1]);
586 }
587
588 #endif /* STANDALONE */
589
590 #endif /* __WINE_WINE_TEST_H */