Added support for BTrees in file header reading.
[wine] / programs / winetest / wtmain.c
1 /*
2  * Main routine for Wine C unit tests.
3  *
4  * Copyright 2002 Alexandre Julliard
5  * Copyright 2002 Andriy Palamarchuk
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "wine/test.h"
27 #include "winbase.h"
28
29 /* debug level */
30 int winetest_debug = 1;
31
32 /* current platform */
33 const char *winetest_platform = "windows";
34
35 /* report successful tests (BOOL) */
36 int winetest_report_success = 0;
37
38 /* passing arguments around */
39 static int winetest_argc;
40 static char** winetest_argv;
41
42 struct test
43 {
44     const char  *name;
45     void       (*func)(void);
46 };
47
48 extern const struct test winetest_testlist[];
49 static const struct test *current_test; /* test currently being run */
50
51 static LONG successes;       /* number of successful tests */
52 static LONG failures;        /* number of failures */
53 static LONG todo_successes;  /* number of successful tests inside todo block */
54 static LONG todo_failures;   /* number of failures inside todo block */
55
56 /* The following data must be kept track of on a per-thread basis */
57 typedef struct
58 {
59     const char* current_file;        /* file of current check */
60     int current_line;                /* line of current check */
61     int todo_level;                  /* current todo nesting level */
62     int todo_do_loop;
63 } tls_data;
64 static DWORD tls_index;
65
66 static tls_data* get_tls_data(void)
67 {
68     tls_data* data;
69     DWORD last_error;
70
71     last_error=GetLastError();
72     data=TlsGetValue(tls_index);
73     if (!data)
74     {
75         data=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(tls_data));
76         TlsSetValue(tls_index,data);
77     }
78     SetLastError(last_error);
79     return data;
80 }
81
82 static void exit_process( int code )
83 {
84     fflush( stderr );
85     ExitProcess( code );
86 }
87
88
89 /*
90  * Checks condition.
91  * Parameters:
92  *   - condition - condition to check;
93  *   - msg test description;
94  *   - file - test application source code file name of the check
95  *   - line - test application source code file line number of the check
96  * Return:
97  *   0 if condition does not have the expected value, 1 otherwise
98  */
99 int winetest_ok( int condition, const char *msg, ... )
100 {
101     va_list valist;
102     tls_data* data=get_tls_data();
103
104     if (data->todo_level)
105     {
106         if (condition)
107         {
108             fprintf( stderr, "%s:%d: Test succeeded inside todo block",
109                      data->current_file, data->current_line );
110             if (msg && msg[0])
111             {
112                 va_start(valist, msg);
113                 fprintf(stderr,": ");
114                 vfprintf(stderr, msg, valist);
115                 va_end(valist);
116             }
117             fputc( '\n', stderr );
118             InterlockedIncrement(&todo_failures);
119             return 0;
120         }
121         else InterlockedIncrement(&todo_successes);
122     }
123     else
124     {
125         if (!condition)
126         {
127             fprintf( stderr, "%s:%d: Test failed",
128                      data->current_file, data->current_line );
129             if (msg && msg[0])
130             {
131                 va_start(valist, msg);
132                 fprintf( stderr,": ");
133                 vfprintf(stderr, msg, valist);
134                 va_end(valist);
135             }
136             fputc( '\n', stderr );
137             InterlockedIncrement(&failures);
138             return 0;
139         }
140         else
141         {
142             if( winetest_report_success)
143                 fprintf( stderr, "%s:%d: Test succeeded\n",
144                          data->current_file, data->current_line);
145             InterlockedIncrement(&successes);
146         }
147     }
148     return 1;
149 }
150
151 void winetest_set_ok_location( const char* file, int line )
152 {
153     tls_data* data=get_tls_data();
154     data->current_file=file;
155     data->current_line=line;
156 }
157
158 void winetest_trace( const char *msg, ... )
159 {
160     va_list valist;
161     tls_data* data=get_tls_data();
162
163     if (winetest_debug > 0)
164     {
165         fprintf( stderr, "%s:%d:", data->current_file, data->current_line );
166         va_start(valist, msg);
167         vfprintf(stderr, msg, valist);
168         va_end(valist);
169     }
170 }
171
172 void winetest_set_trace_location( const char* file, int line )
173 {
174     tls_data* data=get_tls_data();
175     data->current_file=file;
176     data->current_line=line;
177 }
178
179 void winetest_start_todo( const char* platform )
180 {
181     tls_data* data=get_tls_data();
182     if (strcmp(winetest_platform,platform)==0)
183         data->todo_level++;
184     data->todo_do_loop=1;
185 }
186
187 int winetest_loop_todo(void)
188 {
189     tls_data* data=get_tls_data();
190     int do_loop=data->todo_do_loop;
191     data->todo_do_loop=0;
192     return do_loop;
193 }
194
195 void winetest_end_todo( const char* platform )
196 {
197     if (strcmp(winetest_platform,platform)==0)
198     {
199         tls_data* data=get_tls_data();
200         data->todo_level--;
201     }
202 }
203
204 int winetest_get_mainargs( char*** pargv )
205 {
206     *pargv = winetest_argv;
207     return winetest_argc;
208 }
209
210 /* Find a test by name */
211 static const struct test *find_test( const char *name )
212 {
213     const struct test *test;
214     const char *p;
215     int len;
216
217     if ((p = strrchr( name, '/' ))) name = p + 1;
218     if ((p = strrchr( name, '\\' ))) name = p + 1;
219     len = strlen(name);
220     if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
221
222     for (test = winetest_testlist; test->name; test++)
223     {
224         if (!strncmp( test->name, name, len ) && !test->name[len]) break;
225     }
226     return test->name ? test : NULL;
227 }
228
229
230 /* Run a named test, and return exit status */
231 static int run_test( const char *name )
232 {
233     const struct test *test;
234     int status;
235
236     if (!(test = find_test( name )))
237     {
238         fprintf( stderr, "Fatal: test '%s' does not exist.\n", name );
239         exit_process(1);
240     }
241     successes = failures = todo_successes = todo_failures = 0;
242     tls_index=TlsAlloc();
243     current_test = test;
244     test->func();
245
246     if (winetest_debug)
247     {
248         fprintf( stderr, "%s: %ld tests executed, %ld marked as todo, %ld %s.\n",
249                  name, successes + failures + todo_successes + todo_failures,
250                  todo_successes, failures + todo_failures,
251                  (failures + todo_failures != 1) ? "failures" : "failure" );
252     }
253     status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
254     return status;
255 }
256
257
258 /* Display usage and exit */
259 static void usage( const char *argv0 )
260 {
261     const struct test *test;
262
263     fprintf( stderr, "Usage: %s test_name\n", argv0 );
264     fprintf( stderr, "\nValid test names:\n" );
265     for (test = winetest_testlist; test->name; test++) fprintf( stderr, "    %s\n", test->name );
266     exit_process(1);
267 }
268
269
270 /* main function */
271 int main( int argc, char **argv )
272 {
273     char *p;
274
275     winetest_argc = argc;
276     winetest_argv = argv;
277
278     if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = p;
279     if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
280     if ((p = getenv( "WINETEST_REPORT_SUCCESS"))) winetest_report_success = \
281                 atoi(p);
282     if (!argv[1]) usage( argv[0] );
283
284     return run_test(argv[1]);
285 }