winex11.drv: Improve setting IME result text with no composition.
[wine.git] / include / wine / test.h
blob0d81a2444875529b3f0aa654eec00491295976c5
1 /*
2 * Definitions for Wine C unit tests.
4 * Copyright (C) 2002 Alexandre Julliard
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.
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.
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
21 #ifndef __WINE_WINE_TEST_H
22 #define __WINE_WINE_TEST_H
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <windef.h>
27 #include <winbase.h>
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
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
49 /* debug level */
50 extern int winetest_debug;
52 /* running in interactive mode? */
53 extern int winetest_interactive;
55 /* current platform */
56 extern const char *winetest_platform;
58 extern void winetest_set_location( const char* file, int line );
59 extern void winetest_start_todo( int is_todo );
60 extern int winetest_loop_todo(void);
61 extern void winetest_end_todo(void);
62 extern int winetest_get_mainargs( char*** pargv );
63 extern LONG winetest_get_failures(void);
64 extern void winetest_add_failures( LONG new_failures );
65 extern void winetest_wait_child_process( HANDLE process );
67 extern const char *wine_dbgstr_wn( const WCHAR *str, int n );
68 extern const char *wine_dbgstr_guid( const GUID *guid );
69 static inline const char *wine_dbgstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
71 /* strcmpW is available for tests compiled under Wine, but not in standalone
72 * builds under Windows, so we reimplement it under a different name. */
73 static inline int winetest_strcmpW( const WCHAR *str1, const WCHAR *str2 )
75 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
76 return *str1 - *str2;
79 #ifdef STANDALONE
80 #define START_TEST(name) \
81 static void func_##name(void); \
82 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
83 static void func_##name(void)
84 #else
85 #define START_TEST(name) void func_##name(void)
86 #endif
88 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
89 #define __winetest_cdecl __cdecl
90 #define __winetest_va_list __builtin_ms_va_list
91 #else
92 #define __winetest_cdecl
93 #define __winetest_va_list va_list
94 #endif
96 extern int broken( int condition );
97 extern int winetest_vok( int condition, const char *msg, __winetest_va_list ap );
98 extern void winetest_vskip( const char *msg, __winetest_va_list ap );
100 #ifdef __GNUC__
101 # define WINETEST_PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
102 #else
103 # define WINETEST_PRINTF_ATTR(fmt,args)
104 #endif
106 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... ) WINETEST_PRINTF_ATTR(2,3);
107 extern void __winetest_cdecl winetest_skip( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
108 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
109 extern void __winetest_cdecl winetest_trace( const char *msg, ... ) WINETEST_PRINTF_ATTR(1,2);
111 #define ok_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_ok
112 #define skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
113 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
114 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
116 #define ok ok_(__FILE__, __LINE__)
117 #define skip skip_(__FILE__, __LINE__)
118 #define win_skip win_skip_(__FILE__, __LINE__)
119 #define trace trace_(__FILE__, __LINE__)
121 #define todo_if(is_todo) for (winetest_start_todo(is_todo); \
122 winetest_loop_todo(); \
123 winetest_end_todo())
124 #define todo_wine todo_if(!strcmp(winetest_platform, "wine"))
125 #define todo_wine_if(is_todo) todo_if((is_todo) && !strcmp(winetest_platform, "wine"))
128 #ifdef NONAMELESSUNION
129 # define U(x) (x).u
130 # define U1(x) (x).u1
131 # define U2(x) (x).u2
132 # define U3(x) (x).u3
133 # define U4(x) (x).u4
134 # define U5(x) (x).u5
135 # define U6(x) (x).u6
136 # define U7(x) (x).u7
137 # define U8(x) (x).u8
138 #else
139 # define U(x) (x)
140 # define U1(x) (x)
141 # define U2(x) (x)
142 # define U3(x) (x)
143 # define U4(x) (x)
144 # define U5(x) (x)
145 # define U6(x) (x)
146 # define U7(x) (x)
147 # define U8(x) (x)
148 #endif
150 #ifdef NONAMELESSSTRUCT
151 # define S(x) (x).s
152 # define S1(x) (x).s1
153 # define S2(x) (x).s2
154 # define S3(x) (x).s3
155 # define S4(x) (x).s4
156 # define S5(x) (x).s5
157 #else
158 # define S(x) (x)
159 # define S1(x) (x)
160 # define S2(x) (x)
161 # define S3(x) (x)
162 # define S4(x) (x)
163 # define S5(x) (x)
164 #endif
167 /************************************************************************/
168 /* Below is the implementation of the various functions, to be included
169 * directly into the generated testlist.c file.
170 * It is done that way so that the dlls can build the test routines with
171 * different includes or flags if needed.
174 #ifdef STANDALONE
176 #include <stdio.h>
177 #include <excpt.h>
179 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
180 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
181 # define __winetest_va_end(list) __builtin_ms_va_end(list)
182 #else
183 # define __winetest_va_start(list,arg) va_start(list,arg)
184 # define __winetest_va_end(list) va_end(list)
185 #endif
187 struct test
189 const char *name;
190 void (*func)(void);
193 extern const struct test winetest_testlist[];
195 /* debug level */
196 int winetest_debug = 1;
198 /* interactive mode? */
199 int winetest_interactive = 0;
201 /* current platform */
202 const char *winetest_platform = "windows";
204 /* report successful tests (BOOL) */
205 static int report_success = 0;
207 /* passing arguments around */
208 static int winetest_argc;
209 static char** winetest_argv;
211 static const struct test *current_test; /* test currently being run */
213 static LONG successes; /* number of successful tests */
214 static LONG failures; /* number of failures */
215 static LONG skipped; /* number of skipped test chunks */
216 static LONG todo_successes; /* number of successful tests inside todo block */
217 static LONG todo_failures; /* number of failures inside todo block */
219 /* The following data must be kept track of on a per-thread basis */
220 typedef struct
222 const char* current_file; /* file of current check */
223 int current_line; /* line of current check */
224 unsigned int todo_level; /* current todo nesting level */
225 int todo_do_loop;
226 char *str_pos; /* position in debug buffer */
227 char strings[2000]; /* buffer for debug strings */
228 } tls_data;
229 static DWORD tls_index;
231 static tls_data* get_tls_data(void)
233 tls_data* data;
234 DWORD last_error;
236 last_error=GetLastError();
237 data=TlsGetValue(tls_index);
238 if (!data)
240 data=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(tls_data));
241 data->str_pos = data->strings;
242 TlsSetValue(tls_index,data);
244 SetLastError(last_error);
245 return data;
248 /* allocate some tmp space for a string */
249 static char *get_temp_buffer( size_t n )
251 tls_data *data = get_tls_data();
252 char *res = data->str_pos;
254 if (res + n >= &data->strings[sizeof(data->strings)]) res = data->strings;
255 data->str_pos = res + n;
256 return res;
259 /* release extra space that we requested in get_temp_buffer() */
260 static void release_temp_buffer( char *ptr, size_t size )
262 tls_data *data = get_tls_data();
263 data->str_pos = ptr + size;
266 static void exit_process( int code )
268 fflush( stdout );
269 ExitProcess( code );
273 void winetest_set_location( const char* file, int line )
275 tls_data* data=get_tls_data();
276 data->current_file=strrchr(file,'/');
277 if (data->current_file==NULL)
278 data->current_file=strrchr(file,'\\');
279 if (data->current_file==NULL)
280 data->current_file=file;
281 else
282 data->current_file++;
283 data->current_line=line;
286 int broken( int condition )
288 return (strcmp(winetest_platform, "windows") == 0) && condition;
292 * Checks condition.
293 * Parameters:
294 * - condition - condition to check;
295 * - msg test description;
296 * - file - test application source code file name of the check
297 * - line - test application source code file line number of the check
298 * Return:
299 * 0 if condition does not have the expected value, 1 otherwise
301 int winetest_vok( int condition, const char *msg, __winetest_va_list args )
303 tls_data* data=get_tls_data();
305 if (data->todo_level)
307 if (condition)
309 printf( "%s:%d: Test succeeded inside todo block: ",
310 data->current_file, data->current_line );
311 vprintf(msg, args);
312 InterlockedIncrement(&todo_failures);
313 return 0;
315 else
317 if (winetest_debug > 0)
319 printf( "%s:%d: Test marked todo: ",
320 data->current_file, data->current_line );
321 vprintf(msg, args);
323 InterlockedIncrement(&todo_successes);
324 return 1;
327 else
329 if (!condition)
331 printf( "%s:%d: Test failed: ",
332 data->current_file, data->current_line );
333 vprintf(msg, args);
334 InterlockedIncrement(&failures);
335 return 0;
337 else
339 if (report_success)
340 printf( "%s:%d: Test succeeded\n",
341 data->current_file, data->current_line);
342 InterlockedIncrement(&successes);
343 return 1;
348 void __winetest_cdecl winetest_ok( int condition, const char *msg, ... )
350 __winetest_va_list valist;
352 __winetest_va_start(valist, msg);
353 winetest_vok(condition, msg, valist);
354 __winetest_va_end(valist);
357 void __winetest_cdecl winetest_trace( const char *msg, ... )
359 __winetest_va_list valist;
360 tls_data* data=get_tls_data();
362 if (winetest_debug > 0)
364 printf( "%s:%d: ", data->current_file, data->current_line );
365 __winetest_va_start(valist, msg);
366 vprintf(msg, valist);
367 __winetest_va_end(valist);
371 void winetest_vskip( const char *msg, __winetest_va_list args )
373 tls_data* data=get_tls_data();
375 printf( "%s:%d: Tests skipped: ", data->current_file, data->current_line );
376 vprintf(msg, args);
377 skipped++;
380 void __winetest_cdecl winetest_skip( const char *msg, ... )
382 __winetest_va_list valist;
383 __winetest_va_start(valist, msg);
384 winetest_vskip(msg, valist);
385 __winetest_va_end(valist);
388 void __winetest_cdecl winetest_win_skip( const char *msg, ... )
390 __winetest_va_list valist;
391 __winetest_va_start(valist, msg);
392 if (strcmp(winetest_platform, "windows") == 0)
393 winetest_vskip(msg, valist);
394 else
395 winetest_vok(0, msg, valist);
396 __winetest_va_end(valist);
399 void winetest_start_todo( int is_todo )
401 tls_data* data=get_tls_data();
402 data->todo_level = (data->todo_level << 1) | (is_todo != 0);
403 data->todo_do_loop=1;
406 int winetest_loop_todo(void)
408 tls_data* data=get_tls_data();
409 int do_loop=data->todo_do_loop;
410 data->todo_do_loop=0;
411 return do_loop;
414 void winetest_end_todo(void)
416 tls_data* data=get_tls_data();
417 data->todo_level >>= 1;
420 int winetest_get_mainargs( char*** pargv )
422 *pargv = winetest_argv;
423 return winetest_argc;
426 LONG winetest_get_failures(void)
428 return failures;
431 void winetest_add_failures( LONG new_failures )
433 while (new_failures-- > 0)
434 InterlockedIncrement( &failures );
437 void winetest_wait_child_process( HANDLE process )
439 DWORD exit_code = 1;
441 if (WaitForSingleObject( process, 30000 ))
442 printf( "%s: child process wait failed\n", current_test->name );
443 else
444 GetExitCodeProcess( process, &exit_code );
446 if (exit_code)
448 if (exit_code > 255)
450 printf( "%s: exception 0x%08x in child process\n", current_test->name, exit_code );
451 InterlockedIncrement( &failures );
453 else
455 printf( "%s: %u failures in child process\n",
456 current_test->name, exit_code );
457 while (exit_code-- > 0)
458 InterlockedIncrement(&failures);
463 const char *wine_dbgstr_wn( const WCHAR *str, int n )
465 char *dst, *res;
466 size_t size;
468 if (!((ULONG_PTR)str >> 16))
470 if (!str) return "(null)";
471 res = get_temp_buffer( 6 );
472 sprintf( res, "#%04x", LOWORD(str) );
473 return res;
475 if (n == -1)
477 const WCHAR *end = str;
478 while (*end) end++;
479 n = end - str;
481 if (n < 0) n = 0;
482 size = 12 + min( 300, n * 5 );
483 dst = res = get_temp_buffer( size );
484 *dst++ = 'L';
485 *dst++ = '"';
486 while (n-- > 0 && dst <= res + size - 10)
488 WCHAR c = *str++;
489 switch (c)
491 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
492 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
493 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
494 case '"': *dst++ = '\\'; *dst++ = '"'; break;
495 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
496 default:
497 if (c >= ' ' && c <= 126)
498 *dst++ = c;
499 else
501 *dst++ = '\\';
502 sprintf(dst,"%04x",c);
503 dst+=4;
507 *dst++ = '"';
508 if (n > 0)
510 *dst++ = '.';
511 *dst++ = '.';
512 *dst++ = '.';
514 *dst++ = 0;
515 release_temp_buffer( res, dst - res );
516 return res;
519 const char *wine_dbgstr_guid( const GUID *guid )
521 char *res;
523 if (!guid) return "(null)";
524 res = get_temp_buffer( 39 ); /* CHARS_IN_GUID */
525 sprintf( res, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
526 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
527 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
528 guid->Data4[5], guid->Data4[6], guid->Data4[7] );
529 return res;
532 /* Find a test by name */
533 static const struct test *find_test( const char *name )
535 const struct test *test;
536 const char *p;
537 size_t len;
539 if ((p = strrchr( name, '/' ))) name = p + 1;
540 if ((p = strrchr( name, '\\' ))) name = p + 1;
541 len = strlen(name);
542 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
544 for (test = winetest_testlist; test->name; test++)
546 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
548 return test->name ? test : NULL;
552 /* Display list of valid tests */
553 static void list_tests(void)
555 const struct test *test;
557 printf( "Valid test names:\n" );
558 for (test = winetest_testlist; test->name; test++)
559 printf( " %s\n", test->name );
563 /* Run a named test, and return exit status */
564 static int run_test( const char *name )
566 const struct test *test;
567 int status;
569 if (!(test = find_test( name )))
571 printf( "Fatal: test '%s' does not exist.\n", name );
572 exit_process(1);
574 successes = failures = todo_successes = todo_failures = 0;
575 tls_index=TlsAlloc();
576 current_test = test;
577 test->func();
579 if (winetest_debug)
581 printf( "%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
582 test->name, successes + failures + todo_successes + todo_failures,
583 todo_successes, failures + todo_failures,
584 (failures + todo_failures != 1) ? "failures" : "failure",
585 skipped );
587 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
588 return status;
592 /* Display usage and exit */
593 static void usage( const char *argv0 )
595 printf( "Usage: %s test_name\n\n", argv0 );
596 list_tests();
597 exit_process(1);
600 /* trap unhandled exceptions */
601 static LONG CALLBACK exc_filter( EXCEPTION_POINTERS *ptrs )
603 tls_data *data = get_tls_data();
605 if (data->current_file)
606 printf( "%s:%d: this is the last test seen before the exception\n",
607 data->current_file, data->current_line );
608 printf( "%s: unhandled exception %08x at %p\n", current_test->name,
609 ptrs->ExceptionRecord->ExceptionCode, ptrs->ExceptionRecord->ExceptionAddress );
610 fflush( stdout );
611 return EXCEPTION_EXECUTE_HANDLER;
614 /* check if we're running under wine */
615 static BOOL running_under_wine(void)
617 HMODULE module = GetModuleHandleA( "ntdll.dll" );
618 if (!module) return FALSE;
619 return (GetProcAddress( module, "wine_server_call" ) != NULL);
622 #ifdef __GNUC__
623 void _fpreset(void) {} /* override the mingw fpu init code */
624 #endif
626 /* main function */
627 int main( int argc, char **argv )
629 char p[128];
631 setvbuf (stdout, NULL, _IONBF, 0);
633 winetest_argc = argc;
634 winetest_argv = argv;
636 if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) ))
637 winetest_platform = strdup(p);
638 else if (running_under_wine())
639 winetest_platform = "wine";
641 if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
642 if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
643 if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
645 if (!strcmp( winetest_platform, "windows" )) SetUnhandledExceptionFilter( exc_filter );
646 if (!winetest_interactive) SetErrorMode( SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX );
648 if (!argv[1])
650 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
651 return run_test( winetest_testlist[0].name );
652 usage( argv[0] );
654 if (!strcmp( argv[1], "--list" ))
656 list_tests();
657 return 0;
659 return run_test(argv[1]);
662 #endif /* STANDALONE */
664 #endif /* __WINE_WINE_TEST_H */