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