xref: /reactos/sdk/include/reactos/wine/test.h (revision 748fdbcd)
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 <stdio.h> // In the future: replace by <wine/debug.h>
29 
30 #ifdef __WINE_CONFIG_H
31 #error config.h should not be used in Wine tests
32 #endif
33 #ifdef __WINE_WINE_LIBRARY_H
34 #error wine/library.h should not be used in Wine tests
35 #endif
36 #ifdef __WINE_WINE_UNICODE_H
37 #error wine/unicode.h should not be used in Wine tests
38 #endif
39 #ifdef __WINE_WINE_DEBUG_H
40 #error wine/debug.h should not be used in Wine tests
41 #endif
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #ifndef INVALID_FILE_ATTRIBUTES
48 #define INVALID_FILE_ATTRIBUTES  (~0u)
49 #endif
50 #ifndef INVALID_SET_FILE_POINTER
51 #define INVALID_SET_FILE_POINTER (~0u)
52 #endif
53 
54 /* debug level */
55 extern int winetest_debug;
56 
57 extern int report_success;
58 
59 /* running in interactive mode? */
60 extern int winetest_interactive;
61 
62 /* current platform */
63 extern const char *winetest_platform;
64 
65 extern void winetest_set_location( const char* file, int line );
66 extern void winetest_subtest(const char* name);
67 extern void winetest_start_todo( int is_todo );
68 extern int winetest_loop_todo(void);
69 extern void winetest_end_todo(void);
70 extern void winetest_start_nocount(unsigned int flags);
71 extern int winetest_loop_nocount(void);
72 extern void winetest_end_nocount(void);
73 extern int winetest_get_mainargs( char*** pargv );
74 extern LONG winetest_get_failures(void);
75 extern LONG winetest_get_successes(void);
76 extern void winetest_add_failures( LONG new_failures );
77 extern void winetest_wait_child_process( HANDLE process );
78 
79 extern const char *wine_dbgstr_wn( const WCHAR *str, intptr_t n );
80 extern const char *wine_dbgstr_an( const CHAR *str, intptr_t n );
81 extern const char *wine_dbgstr_guid( const GUID *guid );
82 extern const char *wine_dbgstr_point( const POINT *guid );
83 extern const char *wine_dbgstr_size( const SIZE *guid );
84 extern const char *wine_dbgstr_rect( const RECT *rect );
85 #ifdef WINETEST_USE_DBGSTR_LONGLONG
86 extern const char *wine_dbgstr_longlong( ULONGLONG ll );
87 #endif
debugstr_a(const char * s)88 static inline const char *debugstr_a( const char *s )  { return wine_dbgstr_an( s, -1 ); }
debugstr_an(const CHAR * s,intptr_t n)89 static inline const char *debugstr_an( const CHAR *s, intptr_t n ) { return wine_dbgstr_an( s, n ); }
debugstr_w(const WCHAR * s)90 static inline const char *debugstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
debugstr_wn(const WCHAR * s,int n)91 static inline const char *debugstr_wn( const WCHAR *s, int n ) { return wine_dbgstr_wn( s, n ); }
wine_dbgstr_a(const char * s)92 static inline const char *wine_dbgstr_a( const char *s )  { return wine_dbgstr_an( s, -1 ); }
wine_dbgstr_w(const WCHAR * s)93 static inline const char *wine_dbgstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
94 
95 /* strcmpW is available for tests compiled under Wine, but not in standalone
96  * builds under Windows, so we reimplement it under a different name. */
winetest_strcmpW(const WCHAR * str1,const WCHAR * str2)97 static inline int winetest_strcmpW( const WCHAR *str1, const WCHAR *str2 )
98 {
99     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
100     return *str1 - *str2;
101 }
102 
103 #ifdef STANDALONE
104 
105 #define START_TEST(name) \
106   static void func_##name(void); \
107   const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
108   static void func_##name(void)
109 
110 #else /* STANDALONE */
111 
112 #ifdef __cplusplus
113 #define START_TEST(name) extern "C" void func_##name(void)
114 #else
115 #define START_TEST(name) void func_##name(void)
116 #endif
117 
118 #endif /* STANDALONE */
119 
120 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
121 #define __winetest_cdecl __cdecl
122 #define __winetest_va_list __builtin_ms_va_list
123 #else
124 #define __winetest_cdecl
125 #define __winetest_va_list va_list
126 #endif
127 
128 extern int broken( int condition );
129 extern int winetest_vok( int condition, const char *msg, __winetest_va_list ap );
130 extern void winetest_vskip( const char *msg, __winetest_va_list ap );
131 
132 #ifdef __GNUC__
133 # define WINETEST_PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
134 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
135 extern void __winetest_cdecl winetest_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
136 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... ) __attribute__((format (printf,1,2)));
137 extern void __winetest_cdecl winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
138 extern void __winetest_cdecl winetest_print(const char* msg, ...) __attribute__((format(printf, 1, 2)));
139 extern void __winetest_cdecl winetest_push_context( const char *fmt, ... ) __attribute__((format(printf, 1, 2)));
140 extern void winetest_pop_context(void);
141 
142 #else /* __GNUC__ */
143 # define WINETEST_PRINTF_ATTR(fmt,args)
144 extern void __winetest_cdecl winetest_ok( int condition, const char *msg, ... );
145 extern void __winetest_cdecl winetest_skip( const char *msg, ... );
146 extern void __winetest_cdecl winetest_win_skip( const char *msg, ... );
147 extern void __winetest_cdecl winetest_trace( const char *msg, ... );
148 extern void __winetest_cdecl winetest_print(const char* msg, ...);
149 extern void __winetest_cdecl winetest_push_context( const char *fmt, ... );
150 extern void winetest_pop_context(void);
151 
152 #endif /* __GNUC__ */
153 
154 #define subtest_(file, line)  (winetest_set_location(file, line), 0) ? (void)0 : winetest_subtest
155 #define ok_(file, line)       (winetest_set_location(file, line), 0) ? (void)0 : winetest_ok
156 #define skip_(file, line)     (winetest_set_location(file, line), 0) ? (void)0 : winetest_skip
157 #define win_skip_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_win_skip
158 #define trace_(file, line)    (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
159 #define wait_child_process_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_wait_child_process
160 
161 #define subtest  subtest_(__FILE__, __LINE__)
162 #define ok       ok_(__FILE__, __LINE__)
163 #define skip     skip_(__FILE__, __LINE__)
164 #define win_skip win_skip_(__FILE__, __LINE__)
165 #define trace    trace_(__FILE__, __LINE__)
166 #define wait_child_process wait_child_process_(__FILE__, __LINE__)
167 
168 #define todo_if(is_todo) for (winetest_start_todo(is_todo); \
169                               winetest_loop_todo(); \
170                               winetest_end_todo())
171 
172 #define todo_ros                todo_if(!strcmp(winetest_platform, "reactos"))
173 #define todo_ros_if(is_todo)    todo_if((is_todo) && !strcmp(winetest_platform, "reactos"))
174 #ifdef USE_WINE_TODOS
175 #define todo_wine               todo_ros
176 #define todo_wine_if            todo_ros_if
177 #else
178 #define todo_wine               todo_if(!strcmp(winetest_platform, "wine"))
179 #define todo_wine_if(is_todo)   todo_if((is_todo) && !strcmp(winetest_platform, "wine"))
180 #endif
181 
182 #define ros_skip_flaky          for (winetest_start_nocount(3); \
183                                      winetest_loop_nocount(); \
184                                      winetest_end_nocount())
185 
186 #define disable_success_count   for (winetest_start_nocount(1); \
187                                      winetest_loop_nocount(); \
188                                      winetest_end_nocount())
189 
190 #define skip_2k3_crash if (_winver < 0x600) skip("Test skipped, because it crashes on win 2003\n"); else
191 #define skip_2k3_fail if (_winver < 0x600) skip("Test skipped, because it fails on win 2003\n"); else
192 
193 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
194 
195 #ifdef NONAMELESSUNION
196 # define U(x)  (x).u
197 # define U1(x) (x).u1
198 # define U2(x) (x).u2
199 # define U3(x) (x).u3
200 # define U4(x) (x).u4
201 # define U5(x) (x).u5
202 # define U6(x) (x).u6
203 # define U7(x) (x).u7
204 # define U8(x) (x).u8
205 #else
206 # define U(x)  (x)
207 # define U1(x) (x)
208 # define U2(x) (x)
209 # define U3(x) (x)
210 # define U4(x) (x)
211 # define U5(x) (x)
212 # define U6(x) (x)
213 # define U7(x) (x)
214 # define U8(x) (x)
215 #endif
216 
217 #ifdef NONAMELESSSTRUCT
218 # define S(x)  (x).s
219 # define S1(x) (x).s1
220 # define S2(x) (x).s2
221 # define S3(x) (x).s3
222 # define S4(x) (x).s4
223 # define S5(x) (x).s5
224 #else
225 # define S(x)  (x)
226 # define S1(x) (x)
227 # define S2(x) (x)
228 # define S3(x) (x)
229 # define S4(x) (x)
230 # define S5(x) (x)
231 #endif
232 
233 
234 /************************************************************************/
235 /* Below is the implementation of the various functions, to be included
236  * directly into the generated testlist.c file.
237  * It is done that way so that the dlls can build the test routines with
238  * different includes or flags if needed.
239  */
240 
241 #ifdef STANDALONE
242 
243 #include <stdio.h>
244 
245 #if defined(__x86_64__) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
246 # define __winetest_va_start(list,arg) __builtin_ms_va_start(list,arg)
247 # define __winetest_va_end(list) __builtin_ms_va_end(list)
248 #else
249 # define __winetest_va_start(list,arg) va_start(list,arg)
250 # define __winetest_va_end(list) va_end(list)
251 #endif
252 
253 /* Define WINETEST_MSVC_IDE_FORMATTING to alter the output format winetest will use for file/line numbers.
254    This alternate format makes the file/line numbers clickable in visual studio, to directly jump to them. */
255 #if defined(WINETEST_MSVC_IDE_FORMATTING)
256 # define __winetest_file_line_prefix "%s(%d)"
257 #else
258 # define __winetest_file_line_prefix "%s:%d"
259 #endif
260 
261 struct test
262 {
263     const char *name;
264     void (*func)(void);
265 };
266 
267 extern const struct test winetest_testlist[];
268 
269 /* debug level */
270 int winetest_debug = 1;
271 
272 /* interactive mode? */
273 int winetest_interactive = 0;
274 
275 /* current platform */
276 const char *winetest_platform = "windows";
277 
278 /* report successful tests (BOOL) */
279 int report_success = 0;
280 
281 /* passing arguments around */
282 static int winetest_argc;
283 static char** winetest_argv;
284 
285 static const struct test *current_test; /* test currently being run */
286 
287 static LONG successes;       /* number of successful tests */
288 static LONG failures;        /* number of failures */
289 static LONG skipped;         /* number of skipped test chunks */
290 static LONG todo_successes;  /* number of successful tests inside todo block */
291 static LONG todo_failures;   /* number of failures inside todo block */
292 
293 /* The following data must be kept track of on a per-thread basis */
294 typedef struct
295 {
296     const char* current_file;        /* file of current check */
297     int current_line;                /* line of current check */
298     unsigned int todo_level;         /* current todo nesting level */
299     unsigned int nocount_level;
300     int todo_do_loop;
301     char *str_pos;                   /* position in debug buffer */
302     char strings[2000];              /* buffer for debug strings */
303     char context[8][128];            /* data to print before messages */
304     unsigned int context_count;      /* number of context prefixes */
305 } tls_data;
306 static DWORD tls_index;
307 
get_tls_data(void)308 static tls_data* get_tls_data(void)
309 {
310     tls_data* data;
311     DWORD last_error;
312 
313     last_error=GetLastError();
314     data=(tls_data*)TlsGetValue(tls_index);
315     if (!data)
316     {
317         data=(tls_data*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(tls_data));
318         data->str_pos = data->strings;
319         TlsSetValue(tls_index,data);
320     }
321     SetLastError(last_error);
322     return data;
323 }
324 
325 /* allocate some tmp space for a string */
get_temp_buffer(size_t n)326 static char *get_temp_buffer( size_t n )
327 {
328     tls_data *data = get_tls_data();
329     char *res = data->str_pos;
330 
331     if (res + n >= &data->strings[sizeof(data->strings)]) res = data->strings;
332     data->str_pos = res + n;
333     return res;
334 }
335 
336 /* release extra space that we requested in gimme1() */
release_temp_buffer(char * ptr,size_t size)337 static void release_temp_buffer( char *ptr, size_t size )
338 {
339     tls_data *data = get_tls_data();
340     data->str_pos = ptr + size;
341 }
342 
exit_process(int code)343 static void exit_process( int code )
344 {
345     fflush( stdout );
346     ExitProcess( code );
347 }
348 
349 
winetest_set_location(const char * file,int line)350 void winetest_set_location( const char* file, int line )
351 {
352     tls_data* data=get_tls_data();
353 #if defined(WINETEST_MSVC_IDE_FORMATTING)
354     data->current_file = file;
355 #else
356     data->current_file=strrchr(file,'/');
357     if (data->current_file==NULL)
358         data->current_file=strrchr(file,'\\');
359     if (data->current_file==NULL)
360         data->current_file=file;
361     else
362         data->current_file++;
363 #endif
364     data->current_line=line;
365 }
366 
367 #ifdef __GNUC__
368 static void __winetest_cdecl winetest_printf( const char *msg, ... ) __attribute__((format(printf,1,2)));
369 #else
370 static void __winetest_cdecl winetest_printf(const char* msg, ...);
371 #endif
winetest_printf(const char * msg,...)372 static void __winetest_cdecl winetest_printf( const char *msg, ... )
373 {
374     tls_data *data = get_tls_data();
375     __winetest_va_list valist;
376 
377     fprintf( stdout, __winetest_file_line_prefix ": ", data->current_file, data->current_line );
378     __winetest_va_start( valist, msg );
379     vfprintf( stdout, msg, valist );
380     __winetest_va_end( valist );
381 }
382 
winetest_print_context(const char * msgtype)383 static void __winetest_cdecl winetest_print_context( const char *msgtype )
384 {
385     tls_data *data = get_tls_data();
386     unsigned int i;
387 
388     winetest_printf( "%s", msgtype );
389     for (i = 0; i < data->context_count; ++i)
390         fprintf( stdout, "%s: ", data->context[i] );
391 }
392 
winetest_subtest(const char * name)393 void winetest_subtest(const char* name)
394 {
395     tls_data* data = get_tls_data();
396     fprintf(stdout, __winetest_file_line_prefix ": Subtest %s\n",
397         data->current_file, data->current_line, name);
398 }
399 
broken(int condition)400 int broken( int condition )
401 {
402     return ((strcmp(winetest_platform, "windows") == 0)
403 #ifndef USE_WINE_TODOS
404     || (strcmp(winetest_platform, "reactos") == 0)
405 #endif
406     ) && condition;
407 }
408 
409 /*
410  * Checks condition.
411  * Parameters:
412  *   - condition - condition to check;
413  *   - msg test description;
414  *   - file - test application source code file name of the check
415  *   - line - test application source code file line number of the check
416  * Return:
417  *   0 if condition does not have the expected value, 1 otherwise
418  */
winetest_vok(int condition,const char * msg,__winetest_va_list args)419 int winetest_vok( int condition, const char *msg, __winetest_va_list args )
420 {
421     tls_data* data=get_tls_data();
422 
423     if (data->todo_level)
424     {
425         if (condition)
426         {
427             winetest_print_context( "Test succeeded inside todo block: " );
428             vfprintf(stdout, msg, args);
429             if ((data->nocount_level & 2) == 0)
430             InterlockedIncrement(&todo_failures);
431             return 0;
432         }
433         else
434         {
435             /* show todos even if traces are disabled*/
436             /*if (winetest_debug > 0)*/
437             {
438                 winetest_print_context( "Test marked todo: " );
439                 vfprintf(stdout, msg, args);
440             }
441             if ((data->nocount_level & 1) == 0)
442             InterlockedIncrement(&todo_successes);
443             return 1;
444         }
445     }
446     else
447     {
448         if (!condition)
449         {
450             winetest_print_context( "Test failed: " );
451             vfprintf(stdout, msg, args);
452             if ((data->nocount_level & 2) == 0)
453             InterlockedIncrement(&failures);
454             return 0;
455         }
456         else
457         {
458             if (report_success && (data->nocount_level & 1) == 0)
459             {
460                 winetest_printf("Test succeeded\n");
461             }
462             if ((data->nocount_level & 1) == 0)
463             InterlockedIncrement(&successes);
464             return 1;
465         }
466     }
467 }
468 
winetest_ok(int condition,const char * msg,...)469 void __winetest_cdecl winetest_ok( int condition, const char *msg, ... )
470 {
471     __winetest_va_list valist;
472 
473     __winetest_va_start(valist, msg);
474     winetest_vok(condition, msg, valist);
475     __winetest_va_end(valist);
476 }
477 
winetest_trace(const char * msg,...)478 void __winetest_cdecl winetest_trace( const char *msg, ... )
479 {
480     __winetest_va_list valist;
481 
482     if (winetest_debug > 0)
483     {
484         winetest_print_context( "" );
485         __winetest_va_start(valist, msg);
486         vfprintf(stdout, msg, valist);
487         __winetest_va_end(valist);
488     }
489 }
490 
winetest_print(const char * msg,...)491 void __winetest_cdecl winetest_print(const char* msg, ...)
492 {
493     __winetest_va_list valist;
494     tls_data* data = get_tls_data();
495 
496     fprintf(stdout, __winetest_file_line_prefix ": ", data->current_file, data->current_line);
497     __winetest_va_start(valist, msg);
498     vfprintf(stdout, msg, valist);
499     __winetest_va_end(valist);
500 }
501 
winetest_vskip(const char * msg,__winetest_va_list args)502 void winetest_vskip( const char *msg, __winetest_va_list args )
503 {
504     winetest_print_context( "Tests skipped: " );
505     vfprintf(stdout, msg, args);
506     skipped++;
507 }
508 
winetest_skip(const char * msg,...)509 void __winetest_cdecl winetest_skip( const char *msg, ... )
510 {
511     __winetest_va_list valist;
512     __winetest_va_start(valist, msg);
513     winetest_vskip(msg, valist);
514     __winetest_va_end(valist);
515 }
516 
winetest_win_skip(const char * msg,...)517 void __winetest_cdecl winetest_win_skip( const char *msg, ... )
518 {
519     __winetest_va_list valist;
520     __winetest_va_start(valist, msg);
521     if ((strcmp(winetest_platform, "windows") == 0)
522 #ifndef USE_WINE_TODOS
523     || (strcmp(winetest_platform, "reactos") == 0)
524 #endif
525     )
526         winetest_vskip(msg, valist);
527     else
528         winetest_vok(0, msg, valist);
529     __winetest_va_end(valist);
530 }
531 
winetest_start_todo(int is_todo)532 void winetest_start_todo( int is_todo )
533 {
534     tls_data* data=get_tls_data();
535     data->todo_level = (data->todo_level << 1) | (is_todo != 0);
536     data->todo_do_loop=1;
537 }
538 
winetest_loop_todo(void)539 int winetest_loop_todo(void)
540 {
541     tls_data* data=get_tls_data();
542     int do_loop=data->todo_do_loop;
543     data->todo_do_loop=0;
544     return do_loop;
545 }
546 
winetest_end_todo(void)547 void winetest_end_todo(void)
548 {
549     tls_data* data=get_tls_data();
550     data->todo_level >>= 1;
551 }
552 
winetest_start_nocount(unsigned int flags)553 void winetest_start_nocount(unsigned int flags)
554 {
555     tls_data* data = get_tls_data();
556 
557     /* The lowest 2 bits of nocount_level specify whether counting of successes
558        and/or failures is disabled. For each nested level the bits are shifted
559        left, the new lowest 2 bits are copied from the previous state and ored
560        with the new mask. This allows nested handling of both states up tp a
561        level of 16. */
562     flags |= data->nocount_level & 3;
563     data->nocount_level = (data->nocount_level << 2) | flags;
564     data->todo_do_loop = 1;
565 }
566 
winetest_loop_nocount(void)567 int winetest_loop_nocount(void)
568 {
569     tls_data* data = get_tls_data();
570     int do_loop = data->todo_do_loop;
571     data->todo_do_loop = 0;
572     return do_loop;
573 }
574 
winetest_end_nocount(void)575 void winetest_end_nocount(void)
576 {
577     tls_data* data = get_tls_data();
578     data->nocount_level >>= 2;
579 }
580 
winetest_push_context(const char * fmt,...)581 void __winetest_cdecl winetest_push_context(const char* fmt, ...)
582 {
583     tls_data* data = get_tls_data();
584     __winetest_va_list valist;
585 
586     if (data->context_count < ARRAY_SIZE(data->context))
587     {
588         __winetest_va_start(valist, fmt);
589         vsnprintf(data->context[data->context_count], sizeof(data->context[data->context_count]), fmt, valist);
590         __winetest_va_end(valist);
591         data->context[data->context_count][sizeof(data->context[data->context_count]) - 1] = 0;
592     }
593     ++data->context_count;
594 }
595 
winetest_pop_context(void)596 void winetest_pop_context(void)
597 {
598     tls_data* data = get_tls_data();
599 
600     if (data->context_count)
601         --data->context_count;
602 }
603 
winetest_get_mainargs(char *** pargv)604 int winetest_get_mainargs( char*** pargv )
605 {
606     *pargv = winetest_argv;
607     return winetest_argc;
608 }
609 
winetest_get_failures(void)610 LONG winetest_get_failures(void)
611 {
612     return failures;
613 }
614 
winetest_get_successes(void)615 LONG winetest_get_successes(void)
616 {
617     return successes;
618 }
619 
winetest_add_failures(LONG new_failures)620 void winetest_add_failures( LONG new_failures )
621 {
622     while (new_failures-- > 0)
623         InterlockedIncrement( &failures );
624 }
625 
winetest_wait_child_process(HANDLE process)626 void winetest_wait_child_process( HANDLE process )
627 {
628     DWORD exit_code = 1;
629 
630     if (WaitForSingleObject( process, 30000 ))
631         fprintf( stdout, "%s: child process wait failed\n", current_test->name );
632     else
633         GetExitCodeProcess( process, &exit_code );
634 
635     if (exit_code)
636     {
637         if (exit_code > 255)
638         {
639             fprintf( stdout, "%s: exception 0x%08x in child process\n", current_test->name, (unsigned)exit_code );
640             InterlockedIncrement( &failures );
641         }
642         else
643         {
644             fprintf( stdout, "%s: %u failures in child process\n",
645                      current_test->name, (unsigned)exit_code );
646             while (exit_code-- > 0)
647                 InterlockedIncrement(&failures);
648         }
649     }
650 }
651 
wine_dbgstr_an(const CHAR * str,intptr_t n)652 const char *wine_dbgstr_an( const CHAR *str, intptr_t n )
653 {
654     char *dst, *res;
655     size_t size;
656 
657     if (!((ULONG_PTR)str >> 16))
658     {
659         if (!str) return "(null)";
660         res = get_temp_buffer( 6 );
661         sprintf( res, "#%04x", LOWORD(str) );
662         return res;
663     }
664     if (n == -1)
665     {
666         const CHAR *end = str;
667         while (*end) end++;
668         n = end - str;
669     }
670     if (n < 0) n = 0;
671     size = 12 + min( 300, n * 5 );
672     dst = res = get_temp_buffer( size );
673     *dst++ = '"';
674     while (n-- > 0 && dst <= res + size - 10)
675     {
676         CHAR c = *str++;
677         switch (c)
678         {
679         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
680         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
681         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
682         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
683         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
684         default:
685             if (c >= ' ' && c <= 126)
686                 *dst++ = (char)c;
687             else
688             {
689                 *dst++ = '\\';
690                 sprintf(dst,"%04x",c);
691                 dst+=4;
692             }
693         }
694     }
695     *dst++ = '"';
696     if (n > 0)
697     {
698         *dst++ = '.';
699         *dst++ = '.';
700         *dst++ = '.';
701     }
702     *dst++ = 0;
703     release_temp_buffer( res, dst - res );
704     return res;
705 }
706 
wine_dbgstr_wn(const WCHAR * str,intptr_t n)707 const char *wine_dbgstr_wn( const WCHAR *str, intptr_t n )
708 {
709     char *res;
710     static const char hex[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
711     char buffer[300], *dst = buffer;
712 
713     if (!str) return "(null)";
714     if (!((ULONG_PTR)str >> 16))
715     {
716         res = get_temp_buffer( 6 );
717         sprintf( res, "#%04x", LOWORD(str) );
718         return res;
719     }
720     if (IsBadStringPtrW(str,n)) return "(invalid)";
721     if (n == -1) for (n = 0; str[n]; n++) ;
722     *dst++ = 'L';
723     *dst++ = '"';
724     while (n-- > 0 && dst <= buffer + sizeof(buffer) - 10)
725     {
726         WCHAR c = *str++;
727         switch (c)
728         {
729         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
730         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
731         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
732         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
733         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
734         default:
735             if (c < ' ' || c >= 127)
736             {
737                 *dst++ = '\\';
738                 *dst++ = hex[(c >> 12) & 0x0f];
739                 *dst++ = hex[(c >> 8) & 0x0f];
740                 *dst++ = hex[(c >> 4) & 0x0f];
741                 *dst++ = hex[c & 0x0f];
742             }
743             else *dst++ = (char)c;
744         }
745     }
746     *dst++ = '"';
747     if (n > 0)
748     {
749         *dst++ = '.';
750         *dst++ = '.';
751         *dst++ = '.';
752     }
753     *dst = 0;
754 
755     res = get_temp_buffer(strlen(buffer) + 1);
756     strcpy(res, buffer);
757     return res;
758 }
759 
wine_dbgstr_guid(const GUID * guid)760 const char *wine_dbgstr_guid( const GUID *guid )
761 {
762     char *res;
763 
764     if (!guid) return "(null)";
765     res = get_temp_buffer( 39 ); /* CHARS_IN_GUID */
766     sprintf( res, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
767              guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
768              guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
769              guid->Data4[5], guid->Data4[6], guid->Data4[7] );
770     return res;
771 }
772 
wine_dbgstr_point(const POINT * point)773 const char *wine_dbgstr_point( const POINT *point )
774 {
775     char *res;
776 
777     if (!point) return "(null)";
778     res = get_temp_buffer( 60 );
779 #ifdef __ROS_LONG64__
780     sprintf( res, "(%d,%d)", point->x, point->y );
781 #else
782     sprintf( res, "(%ld,%ld)", point->x, point->y );
783 #endif
784     release_temp_buffer( res, strlen(res) + 1 );
785     return res;
786 }
787 
wine_dbgstr_size(const SIZE * size)788 const char *wine_dbgstr_size( const SIZE *size )
789 {
790     char *res;
791 
792     if (!size) return "(null)";
793     res = get_temp_buffer( 60 );
794 #ifdef __ROS_LONG64__
795     sprintf( res, "(%d,%d)", size->cx, size->cy );
796 #else
797     sprintf( res, "(%ld,%ld)", size->cx, size->cy );
798 #endif
799     release_temp_buffer( res, strlen(res) + 1 );
800     return res;
801 }
802 
wine_dbgstr_rect(const RECT * rect)803 const char *wine_dbgstr_rect( const RECT *rect )
804 {
805     char *res;
806 
807     if (!rect) return "(null)";
808     res = get_temp_buffer( 60 );
809 #ifdef __ROS_LONG64__
810     sprintf( res, "(%d,%d)-(%d,%d)", rect->left, rect->top, rect->right, rect->bottom );
811 #else
812     sprintf( res, "(%ld,%ld)-(%ld,%ld)", rect->left, rect->top, rect->right, rect->bottom );
813 #endif
814     release_temp_buffer( res, strlen(res) + 1 );
815     return res;
816 }
817 
818 #ifdef WINETEST_USE_DBGSTR_LONGLONG
wine_dbgstr_longlong(ULONGLONG ll)819 const char *wine_dbgstr_longlong( ULONGLONG ll )
820 {
821     char *res;
822 
823     res = get_temp_buffer( 20 );
824     if (/*sizeof(ll) > sizeof(unsigned long) &&*/ ll >> 32) /* ULONGLONG is always > long in ReactOS */
825         sprintf( res, "%lx%08lx", (unsigned long)(ll >> 32), (unsigned long)ll );
826     else
827         sprintf( res, "%lx", (unsigned long)ll );
828     release_temp_buffer( res, strlen(res) + 1 );
829     return res;
830 }
831 #endif
832 
833 /* Find a test by name */
find_test(const char * name)834 static const struct test *find_test( const char *name )
835 {
836     const struct test *test;
837     const char *p;
838     size_t len;
839 
840     if ((p = strrchr( name, '/' ))) name = p + 1;
841     if ((p = strrchr( name, '\\' ))) name = p + 1;
842     len = strlen(name);
843     if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
844 
845     for (test = winetest_testlist; test->name; test++)
846     {
847         if (!strncmp( test->name, name, len ) && !test->name[len]) break;
848     }
849     return test->name ? test : NULL;
850 }
851 
852 
853 /* Display list of valid tests */
list_tests(void)854 static void list_tests(void)
855 {
856     const struct test *test;
857 
858     fprintf( stdout, "Valid test names:\n" );
859     for (test = winetest_testlist; test->name; test++) fprintf( stdout, "    %s\n", test->name );
860 }
861 
862 /* Disable false-positive claiming "test" would be NULL-dereferenced */
863 #if defined(_MSC_VER)
864 #pragma warning(push)
865 #pragma warning(disable:28182)
866 #endif
867 
868 /* Run a named test, and return exit status */
run_test(const char * name)869 static int run_test( const char *name )
870 {
871     const struct test *test;
872     int status;
873 
874     if (!(test = find_test( name )))
875     {
876         fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
877         exit_process(1);
878     }
879     successes = failures = todo_successes = todo_failures = 0;
880     tls_index=TlsAlloc();
881     current_test = test;
882     test->func();
883 
884     /* show test results even if traces are disabled */
885     /*if (winetest_debug)*/
886     {
887         fprintf( stdout, "\n%s: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
888                  test->name, (int)(successes + failures + todo_successes + todo_failures),
889                  (int)todo_successes, (int)(failures + todo_failures),
890                  (failures + todo_failures != 1) ? "failures" : "failure",
891                  (int)skipped );
892     }
893     status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
894     return status;
895 }
896 
897 #if defined(_MSC_VER)
898 #pragma warning(pop)
899 #endif
900 
901 /* Display usage and exit */
usage(const char * argv0)902 static void usage( const char *argv0 )
903 {
904     fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
905     list_tests();
906     exit_process(1);
907 }
908 
909 
910 /* main function */
main(int argc,char ** argv)911 int main( int argc, char **argv )
912 {
913     char p[128];
914 
915     setvbuf (stdout, NULL, _IONBF, 0);
916 
917     winetest_argc = argc;
918     winetest_argv = argv;
919 
920     if (GetEnvironmentVariableA( "WINETEST_PLATFORM", p, sizeof(p) )) winetest_platform = _strdup(p);
921     if (GetEnvironmentVariableA( "WINETEST_DEBUG", p, sizeof(p) )) winetest_debug = atoi(p);
922     if (GetEnvironmentVariableA( "WINETEST_INTERACTIVE", p, sizeof(p) )) winetest_interactive = atoi(p);
923     if (GetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", p, sizeof(p) )) report_success = atoi(p);
924 
925     if (!winetest_interactive) SetErrorMode( SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX );
926 
927     if (!argv[1])
928     {
929         if (winetest_testlist[0].name && !winetest_testlist[1].name)  /* only one test */
930             return run_test( winetest_testlist[0].name );
931         usage( argv[0] );
932     }
933     if (!strcmp( argv[1], "--list" ))
934     {
935         list_tests();
936         return 0;
937     }
938     return run_test(argv[1]);
939 }
940 
941 #endif  /* STANDALONE */
942 
943 // hack for ntdll winetest (this is defined in excpt.h)
944 #undef exception_info
945 
946 // Some helpful definitions
947 
948 #define ok_hex_(file, line, expression, result) \
949     do { \
950         int _value = (expression); \
951         int _result = (result); \
952         ok_(file, line)(_value == _result, "Wrong value for '%s', expected: " #result " (0x%x), got: 0x%x\n", \
953            #expression, _result, _value); \
954     } while (0)
955 #define ok_hex(expression, result)      ok_hex_(__FILE__, __LINE__, expression, result)
956 
957 #define ok_dec_(file, line, expression, result) \
958     do { \
959         int _value = (expression); \
960         int _result = (result); \
961         ok_(file, line)(_value == _result, "Wrong value for '%s', expected: " #result " (%d), got: %d\n", \
962            #expression, _result, _value); \
963     } while (0)
964 #define ok_dec(expression, result)      ok_dec_(__FILE__, __LINE__, expression, result)
965 
966 #define ok_ptr_(file, line, expression, result) \
967     do { \
968         const void *_value = (expression); \
969         const void *_result = (result); \
970         ok_(file, line)(_value == _result, "Wrong value for '%s', expected: " #result " (%p), got: %p\n", \
971            #expression, _result, _value); \
972     } while (0)
973 #define ok_ptr(expression, result)      ok_ptr_(__FILE__, __LINE__, expression, result)
974 
975 #define ok_size_t_(file, line, expression, result) \
976     do { \
977         size_t _value = (expression); \
978         size_t _result = (result); \
979         ok_(file, line)(_value == _result, "Wrong value for '%s', expected: " #result " (%Ix), got: %Ix\n", \
980            #expression, _result, _value); \
981     } while (0)
982 #define ok_size_t(expression, result)   ok_size_t_(__FILE__, __LINE__, expression, result)
983 
984 #define ok_char(expression, result) ok_hex(expression, result)
985 
986 #define ok_err_(file, line, error) \
987     ok_(file, line)(GetLastError() == (error), "Wrong last error. Expected " #error ", got 0x%lx\n", GetLastError())
988 #define ok_err(error)      ok_err_(__FILE__, __LINE__, error)
989 
990 #define ok_str_(file, line, x, y) \
991     ok_(file, line)(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x)
992 #define ok_str(x, y)      ok_str_(__FILE__, __LINE__, x, y)
993 
994 #define ok_wstr_(file, line, x, y) \
995     ok_(file, line)(wcscmp(x, y) == 0, "Wrong string. Expected '%S', got '%S'\n", y, x)
996 #define ok_wstr(x, y)     ok_wstr_(__FILE__, __LINE__, x, y)
997 
998 #define ok_long(expression, result) ok_hex(expression, result)
999 #define ok_int(expression, result) ok_dec(expression, result)
1000 #define ok_int_(file, line, expression, result) ok_dec_(file, line, expression, result)
1001 #define ok_ntstatus(status, expected) ok_hex(status, expected)
1002 #define ok_hdl ok_ptr
1003 
1004 #ifdef __cplusplus
1005 } /* extern "C" */
1006 #endif
1007 
1008 #endif  /* __WINE_WINE_TEST_H */
1009