Registry entries for shell folders
[reactos.git] / reactos / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifndef __WINE_TEST_H
22 #define __WINE_TEST_H
23
24 #include <stdarg.h>
25 #include "windows.h"
26
27 /* prototype for dbgprint */
28 ULONG CDECL DbgPrint(IN PCH Format,IN ...);
29
30 /* debug level */
31 extern int winetest_debug;
32
33 /* running in interactive mode? */
34 extern int winetest_interactive;
35
36 /* current platform */
37 extern const char *winetest_platform;
38
39 extern void winetest_set_location( const char* file, int line );
40 extern void winetest_start_todo( const char* platform );
41 extern int winetest_loop_todo(void);
42 extern void winetest_end_todo( const char* platform );
43 extern int winetest_get_mainargs( char*** pargv );
44
45 #define START_TEST(name) void func_##name(void)
46
47 #ifdef __GNUC__
48
49 extern int winetest_ok( int condition, const char *msg, ... ) __attribute__((format (printf,2,3) ));
50 extern void winetest_trace( const char *msg, ... ) __attribute__((format (printf,1,2)));
51
52 #else /* __GNUC__ */
53
54 extern int winetest_ok( int condition, const char *msg, ... );
55 extern void winetest_trace( const char *msg, ... );
56
57 #endif /* __GNUC__ */
58
59 #define ok_(file, line) (winetest_set_location(file, line), 0) ? 0 : winetest_ok
60 #define trace_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : winetest_trace
61
62 #define ok ok_(__FILE__, __LINE__)
63 #define trace trace_(__FILE__, __LINE__)
64
65 #define todo(platform) for (winetest_start_todo(platform); \
66 winetest_loop_todo(); \
67 winetest_end_todo(platform))
68 #define todo_wine todo("wine")
69
70
71 /************************************************************************/
72 /* Below is the implementation of the various functions, to be included
73 * directly into the generated testlist.c file.
74 * It is done that way so that the dlls can build the test routines with
75 * different includes or flags if needed.
76 */
77
78 #ifdef WINETEST_WANT_MAIN
79
80 /* debug level */
81 int winetest_debug = 1;
82
83 /* interactive mode? */
84 int winetest_interactive = 0;
85
86 /* current platform */
87 const char *winetest_platform = "windows";
88
89 /* report successful tests (BOOL) */
90 static int report_success = 0;
91
92 /* passing arguments around */
93 static int winetest_argc;
94 static char** winetest_argv;
95
96 static const struct test *current_test; /* test currently being run */
97
98 static LONG successes; /* number of successful tests */
99 static LONG failures; /* number of failures */
100 static LONG todo_successes; /* number of successful tests inside todo block */
101 static LONG todo_failures; /* number of failures inside todo block */
102
103 /* The following data must be kept track of on a per-thread basis */
104 typedef struct
105 {
106 const char* current_file; /* file of current check */
107 int current_line; /* line of current check */
108 int todo_level; /* current todo nesting level */
109 int todo_do_loop;
110 } tls_data;
111 static DWORD tls_index;
112
113 static tls_data* get_tls_data(void)
114 {
115 tls_data* data;
116 DWORD last_error;
117
118 last_error=GetLastError();
119 data=TlsGetValue(tls_index);
120 if (!data)
121 {
122 data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
123 TlsSetValue(tls_index,data);
124 }
125 SetLastError(last_error);
126 return data;
127 }
128
129 static void exit_process( int code )
130 {
131 fflush( stdout );
132 ExitProcess( code );
133 }
134
135
136 void winetest_set_location( const char* file, int line )
137 {
138 tls_data* data=get_tls_data();
139 data->current_file=strrchr(file,'/');
140 if (data->current_file==NULL)
141 data->current_file=strrchr(file,'\\');
142 if (data->current_file==NULL)
143 data->current_file=file;
144 else
145 data->current_file++;
146 data->current_line=line;
147 }
148
149 /*
150 * Checks condition.
151 * Parameters:
152 * - condition - condition to check;
153 * - msg test description;
154 * - file - test application source code file name of the check
155 * - line - test application source code file line number of the check
156 * Return:
157 * 0 if condition does not have the expected value, 1 otherwise
158 */
159 int winetest_ok( int condition, const char *msg, ... )
160 {
161 va_list valist;
162 tls_data* data=get_tls_data();
163
164 if (data->todo_level)
165 {
166 if (condition)
167 {
168 fprintf( stdout, "%s:%d: Test succeeded inside todo block",
169 data->current_file, data->current_line );
170 if (msg && msg[0])
171 {
172 va_start(valist, msg);
173 fprintf(stdout,": ");
174 vfprintf(stdout, msg, valist);
175 va_end(valist);
176 }
177 fputc( '\n', stdout );
178 InterlockedIncrement(&todo_failures);
179 return 0;
180 }
181 else InterlockedIncrement(&todo_successes);
182 }
183 else
184 {
185 if (!condition)
186 {
187 fprintf( stdout, "%s:%d: Test failed",
188 data->current_file, data->current_line );
189 if (msg && msg[0])
190 {
191 char string[1024];
192 va_start(valist, msg);
193 fprintf( stdout,": ");
194 vfprintf(stdout, msg, valist);
195 vsprintf(string, msg, valist);
196 DbgPrint( "%s:%d: Test failed: %s\n",
197 data->current_file, data->current_line, string );
198 va_end(valist);
199 }
200 else
201 {
202 DbgPrint( "%s:%d: Test failed\n",
203 data->current_file, data->current_line );
204 }
205 fputc( '\n', stdout );
206 InterlockedIncrement(&failures);
207 return 0;
208 }
209 else
210 {
211 if (report_success)
212 fprintf( stdout, "%s:%d: Test succeeded\n",
213 data->current_file, data->current_line);
214 InterlockedIncrement(&successes);
215 }
216 }
217 return 1;
218 }
219
220 void winetest_trace( const char *msg, ... )
221 {
222 va_list valist;
223 tls_data* data=get_tls_data();
224
225 if (winetest_debug > 0)
226 {
227 char string[1024];
228 fprintf( stdout, "%s:%d:", data->current_file, data->current_line );
229 va_start(valist, msg);
230 vfprintf(stdout, msg, valist);
231 vsprintf(string, msg, valist);
232 DbgPrint( "%s:%d: %s", data->current_file, data->current_line, string);
233 va_end(valist);
234 }
235 }
236
237 void winetest_start_todo( const char* platform )
238 {
239 tls_data* data=get_tls_data();
240 if (strcmp(winetest_platform,platform)==0)
241 data->todo_level++;
242 data->todo_do_loop=1;
243 }
244
245 int winetest_loop_todo(void)
246 {
247 tls_data* data=get_tls_data();
248 int do_loop=data->todo_do_loop;
249 data->todo_do_loop=0;
250 return do_loop;
251 }
252
253 void winetest_end_todo( const char* platform )
254 {
255 if (strcmp(winetest_platform,platform)==0)
256 {
257 tls_data* data=get_tls_data();
258 data->todo_level--;
259 }
260 }
261
262 int winetest_get_mainargs( char*** pargv )
263 {
264 *pargv = winetest_argv;
265 return winetest_argc;
266 }
267
268 /* Find a test by name */
269 static const struct test *find_test( const char *name )
270 {
271 const struct test *test;
272 const char *p;
273 int len;
274
275 if ((p = strrchr( name, '/' ))) name = p + 1;
276 if ((p = strrchr( name, '\\' ))) name = p + 1;
277 len = strlen(name);
278 if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
279
280 for (test = winetest_testlist; test->name; test++)
281 {
282 if (!strncmp( test->name, name, len ) && !test->name[len]) break;
283 }
284 return test->name ? test : NULL;
285 }
286
287
288 /* Run a named test, and return exit status */
289 static int run_test( const char *name )
290 {
291 const struct test *test;
292 int status;
293
294 if (!(test = find_test( name )))
295 {
296 fprintf( stdout, "Fatal: test '%s' does not exist.\n", name );
297 exit_process(1);
298 }
299 successes = failures = todo_successes = todo_failures = 0;
300 tls_index=TlsAlloc();
301 current_test = test;
302 test->func();
303
304 if (winetest_debug)
305 {
306 fprintf( stdout, "%s: %ld tests executed, %ld marked as todo, %ld %s.\n",
307 name, successes + failures + todo_successes + todo_failures,
308 todo_successes, failures + todo_failures,
309 (failures + todo_failures != 1) ? "failures" : "failure" );
310 }
311 status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
312 return status;
313 }
314
315
316 /* Display usage and exit */
317 static void usage( const char *argv0 )
318 {
319 const struct test *test;
320
321 fprintf( stdout, "Usage: %s test_name\n", argv0 );
322 fprintf( stdout, "\nValid test names:\n" );
323 for (test = winetest_testlist; test->name; test++) fprintf( stdout, " %s\n", test->name );
324 exit_process(1);
325 }
326
327
328 /* main function */
329 int main( int argc, char **argv )
330 {
331 char *p;
332
333 winetest_argc = argc;
334 winetest_argv = argv;
335
336 if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = p;
337 if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
338 if ((p = getenv( "WINETEST_INTERACTIVE" ))) winetest_interactive = atoi(p);
339 if ((p = getenv( "WINETEST_REPORT_SUCCESS"))) report_success = atoi(p);
340 if (!argv[1]) usage( argv[0] );
341
342 return run_test(argv[1]);
343 }
344
345 #endif /* WINETEST_WANT_MAIN */
346
347 #endif /* __WINE_TEST_H */