ole32: Fix some memory leaks in the marshal tests.
[wine.git] / include / wine / test.h
blob8ef8a31a3967bd793d2901ef4d664b6a3354866a
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_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
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
46 /* debug level */
47 extern int winetest_debug;
49 /* running in interactive mode? */
50 extern int winetest_interactive;
52 /* current platform */
53 extern const char *winetest_platform;
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 );
61 #ifdef STANDALONE
62 #define START_TEST(name) \
63 static void func_##name(void); \
64 const struct test winetest_testlist[] = { { #name, func_##name }, { 0, 0 } }; \
65 static void func_##name(void)
66 #else
67 #define START_TEST(name) void func_##name(void)
68 #endif
70 #ifdef __GNUC__
72 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
73 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
75 #else /* __GNUC__ */
77 extern int winetest_ok( int condition, const char *msg, ... );
78 extern void winetest_trace( const char *msg, ... );
80 #endif /* __GNUC__ */
82 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
83 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
85 #define ok ok_(__FILE__, __LINE__)
86 #define trace trace_(__FILE__, __LINE__)
88 #define todo(platform) for (winetest_start_todo(platform); \
89 winetest_loop_todo(); \
90 winetest_end_todo(platform))
91 #define todo_wine todo("wine")
94 #ifdef NONAMELESSUNION
95 # define U(x) (x).u
96 # define U1(x) (x).u1
97 # define U2(x) (x).u2
98 # define U3(x) (x).u3
99 # define U4(x) (x).u4
100 # define U5(x) (x).u5
101 # define U6(x) (x).u6
102 # define U7(x) (x).u7
103 # define U8(x) (x).u8
104 #else
105 # define U(x) (x)
106 # define U1(x) (x)
107 # define U2(x) (x)
108 # define U3(x) (x)
109 # define U4(x) (x)
110 # define U5(x) (x)
111 # define U6(x) (x)
112 # define U7(x) (x)
113 # define U8(x) (x)
114 #endif
116 #ifdef NONAMELESSSTRUCT
117 # define S(x) (x).s
118 # define S1(x) (x).s1
119 # define S2(x) (x).s2
120 # define S3(x) (x).s3
121 # define S4(x) (x).s4
122 # define S5(x) (x).s5
123 #else
124 # define S(x) (x)
125 # define S1(x) (x)
126 # define S2(x) (x)
127 # define S3(x) (x)
128 # define S4(x) (x)
129 # define S5(x) (x)
130 #endif
133 /************************************************************************/
134 /* Below is the implementation of the various functions, to be included
135 * directly into the generated testlist.c file.
136 * It is done that way so that the dlls can build the test routines with
137 * different includes or flags if needed.
140 #ifdef STANDALONE
142 #include <stdio.h>
144 struct test
146 const char *name;
147 void (*func)(void);
150 extern const struct test winetest_testlist[];
152 /* debug level */
153 int winetest_debug = 1;
155 /* interactive mode? */
156 int winetest_interactive = 0;
158 /* current platform */
159 const char *winetest_platform = "windows";
161 /* report successful tests (BOOL) */
162 static int report_success = 0;
164 /* passing arguments around */
165 static int winetest_argc;
166 static char** winetest_argv;
168 static const struct test *current_test; /* test currently being run */
170 static LONG successes; /* number of successful tests */
171 static LONG failures; /* number of failures */
172 static LONG todo_successes; /* number of successful tests inside todo block */
173 static LONG todo_failures; /* number of failures inside todo block */
175 /* The following data must be kept track of on a per-thread basis */
176 typedef struct
178 const char* current_file; /* file of current check */
179 int current_line; /* line of current check */
180 int todo_level; /* current todo nesting level */
181 int todo_do_loop;
182 } tls_data;
183 static DWORD tls_index;
185 static tls_data* get_tls_data(void)
187 tls_data* data;
188 DWORD last_error;
190 last_error=GetLastError();
191 data=TlsGetValue(tls_index);
192 if (!data)
194 data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
195 TlsSetValue(tls_index,data);
197 SetLastError(last_error);
198 return data;
201 static void exit_process( int code )
203 fflush( stdout );
204 ExitProcess( code );
208 void winetest_set_location( const char* file, int line )
210 tls_data* data=get_tls_data();
211 data->current_file=strrchr(file,'/');
212 if (data->current_file==NULL)
213 data->current_file=strrchr(file,'\\');
214 if (data->current_file==NULL)
215 data->current_file=file;
216 else
217 data->current_file++;
218 data->current_line=line;
222 * Checks condition.
223 * Parameters:
224 * - condition - condition to check;
225 * - msg test description;
226 * - file - test application source code file name of the check
227 * - line - test application source code file line number of the check
228 * Return:
229 * 0 if condition does not have the expected value, 1 otherwise
231 int winetest_ok( int condition, const char *msg, ... )
233 va_list valist;
234 tls_data* data=get_tls_data();
236 if (data->todo_level)
238 if (condition)
240 fprintf( stdout, "%s:%d: Test succeeded inside todo block",
241 data->current_file, data->current_line );
242 if (msg[0])
244 va_start(valist, msg);
245 fprintf(stdout,": ");
246 vfprintf(stdout, msg, valist);
247 va_end(valist);
249 InterlockedIncrement(&todo_failures);
250 return 0;
252 else InterlockedIncrement(&todo_successes);
254 else
256 if (!condition)
258 fprintf( stdout, "%s:%d: Test failed",
259 data->current_file, data->current_line );
260 if (msg[0])
262 va_start(valist, msg);
263 fprintf( stdout,": ");
264 vfprintf(stdout, msg, valist);
265 va_end(valist);
267 InterlockedIncrement(&failures);
268 return 0;
270 else
272 if (report_success)
273 fprintf( stdout, "%s:%d: Test succeeded\n",
274 data->current_file, data->current_line);
275 InterlockedIncrement(&successes);
278 return 1;
281 void winetest_trace( const char *msg, ... )
283 va_list valist;
284 tls_data* data=get_tls_data();
286 if (winetest_debug > 0)
288 fprintf( stdout, "%s:%d:", data->current_file, data->current_line );
289 va_start(valist, msg);
290 vfprintf(stdout, msg, valist);
291 va_end(valist);
295 void winetest_start_todo( const char* platform )
297 tls_data* data=get_tls_data();
298 if (strcmp(winetest_platform,platform)==0)
299 data->todo_level++;
300 data->todo_do_loop=1;
303 int winetest_loop_todo(void)
305 tls_data* data=get_tls_data();
306 int do_loop=data->todo_do_loop;
307 data->todo_do_loop=0;
308 return do_loop;
311 void winetest_end_todo( const char* platform )
313 if (strcmp(winetest_platform,platform)==0)
315 tls_data* data=get_tls_data();
316 data->todo_level--;
320 int winetest_get_mainargs( char*** pargv )
322 *pargv = winetest_argv;
323 return winetest_argc;
326 /* Find a test by name */
327 static const struct test *find_test( const char *name )
329 const struct test *test;
330 const char *p;
331 int len;
333 if ((p = strrchr( name, '/' ))) name = p + 1;
334 if ((p = strrchr( name, '\\' ))) name = p + 1;
335 len = strlen(name);
336 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
338 for (test = winetest_testlist; test->name; test++)
340 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
342 return test->name ? test : NULL;
346 /* Display list of valid tests */
347 static void list_tests(void)
349 const struct test *test;
351 fprintf( stdout, "Valid test names:\n" );
352 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
356 /* Run a named test, and return exit status */
357 static int run_test( const char *name )
359 const struct test *test;
360 int status;
362 if (!(test = find_test( name )))
364 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
365 exit_process(1);
367 successes = failures = todo_successes = todo_failures = 0;
368 tls_index=TlsAlloc();
369 current_test = test;
370 test->func();
372 if (winetest_debug)
374 #if defined(WINE_NO_LONG_AS_INT) && !defined(_WIN64)
375 fprintf( stdout, "%s: %ld tests executed, %ld marked as todo, %ld %s.\n",
376 #else
377 fprintf( stdout, "%s: %d tests executed, %d marked as todo, %d %s.\n",
378 #endif
379 name, successes + failures + todo_successes + todo_failures,
380 todo_successes, failures + todo_failures,
381 (failures + todo_failures != 1) ? "failures" : "failure" );
383 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
384 return status;
388 /* Display usage and exit */
389 static void usage( const char *argv0 )
391 fprintf( stdout, "Usage: %s test_name\n\n", argv0 );
392 list_tests();
393 exit_process(1);
397 /* main function */
398 int main( int argc, char **argv )
400 char *p;
402 setvbuf (stdout, NULL, _IONBF, 0);
404 winetest_argc = argc;
405 winetest_argv = argv;
407 if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = strdup(p);
408 if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
409 if ((p = getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive = atoi(p);
410 if ((p = getenv( "WINETEST_REPORT_SUCCESS"))) report_success = atoi(p);
411 if (!argv[1])
413 if (winetest_testlist[0].name && !winetest_testlist[1].name) /* only one test */
414 return run_test( winetest_testlist[0].name );
415 usage( argv[0] );
417 if (!strcmp( argv[1], "--list" ))
419 list_tests();
420 return 0;
422 return run_test(argv[1]);
425 #endif /* STANDALONE */
427 #endif /* __WINE_WINE_TEST_H */