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