Moved imports specification from the .spec into the Makefile so that
[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 /*
83  * Checks condition.
84  * Parameters:
85  *   - condition - condition to check;
86  *   - msg test description;
87  *   - file - test application source code file name of the check
88  *   - line - test application source code file line number of the check
89  * Return:
90  *   0 if condition does not have the expected value, 1 otherwise
91  */
92 int winetest_ok( int condition, const char *msg, ... )
93 {
94     va_list valist;
95     tls_data* data=get_tls_data();
96
97     if (data->todo_level)
98     {
99         if (condition)
100         {
101             fprintf( stderr, "%s:%d: Test succeeded inside todo block",
102                      data->current_file, data->current_line );
103             if (msg && msg[0])
104             {
105                 va_start(valist, msg);
106                 fprintf(stderr,": ");
107                 vfprintf(stderr, msg, valist);
108                 va_end(valist);
109             }
110             fputc( '\n', stderr );
111             InterlockedIncrement(&todo_failures);
112             return 0;
113         }
114         else InterlockedIncrement(&todo_successes);
115     }
116     else
117     {
118         if (!condition)
119         {
120             fprintf( stderr, "%s:%d: Test failed",
121                      data->current_file, data->current_line );
122             if (msg && msg[0])
123             {
124                 va_start(valist, msg);
125                 fprintf( stderr,": ");
126                 vfprintf(stderr, msg, valist);
127                 va_end(valist);
128             }
129             fputc( '\n', stderr );
130             InterlockedIncrement(&failures);
131             return 0;
132         }
133         else
134         {
135             if( winetest_report_success)
136                 fprintf( stderr, "%s:%d: Test succeeded\n",
137                          data->current_file, data->current_line);
138             InterlockedIncrement(&successes);
139         }
140     }
141     return 1;
142 }
143
144 winetest_ok_funcptr winetest_set_ok_location( const char* file, int line )
145 {
146     tls_data* data=get_tls_data();
147     data->current_file=file;
148     data->current_line=line;
149     return &winetest_ok;
150 }
151
152 void winetest_trace( const char *msg, ... )
153 {
154     va_list valist;
155     tls_data* data=get_tls_data();
156
157     if (winetest_debug > 0)
158     {
159         fprintf( stderr, "%s:%d:", data->current_file, data->current_line );
160         va_start(valist, msg);
161         vfprintf(stderr, msg, valist);
162         va_end(valist);
163     }
164 }
165
166 winetest_trace_funcptr winetest_set_trace_location( const char* file, int line )
167 {
168     tls_data* data=get_tls_data();
169     data->current_file=file;
170     data->current_line=line;
171     return &winetest_trace;
172 }
173
174 void winetest_start_todo( const char* platform )
175 {
176     tls_data* data=get_tls_data();
177     if (strcmp(winetest_platform,platform)==0)
178         data->todo_level++;
179     data->todo_do_loop=1;
180 }
181
182 int winetest_loop_todo(void)
183 {
184     tls_data* data=get_tls_data();
185     int do_loop=data->todo_do_loop;
186     data->todo_do_loop=0;
187     return do_loop;
188 }
189
190 void winetest_end_todo( const char* platform )
191 {
192     if (strcmp(winetest_platform,platform)==0)
193     {
194         tls_data* data=get_tls_data();
195         data->todo_level--;
196     }
197 }
198
199 int winetest_get_mainargs( char*** pargv )
200 {
201     *pargv = winetest_argv;
202     return winetest_argc;
203 }
204
205 /* Find a test by name */
206 static const struct test *find_test( const char *name )
207 {
208     const struct test *test;
209     const char *p;
210     int len;
211
212     if ((p = strrchr( name, '/' ))) name = p + 1;
213     if ((p = strrchr( name, '\\' ))) name = p + 1;
214     len = strlen(name);
215     if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
216
217     for (test = winetest_testlist; test->name; test++)
218     {
219         if (!strncmp( test->name, name, len ) && !test->name[len]) break;
220     }
221     return test->name ? test : NULL;
222 }
223
224
225 /* Run a named test, and return exit status */
226 static int run_test( const char *name )
227 {
228     const struct test *test;
229     int status;
230
231     if (!(test = find_test( name )))
232     {
233         fprintf( stderr, "Fatal: test '%s' does not exist.\n", name );
234         ExitProcess(1);
235     }
236     successes = failures = todo_successes = todo_failures = 0;
237     tls_index=TlsAlloc();
238     current_test = test;
239     test->func();
240
241     if (winetest_debug)
242     {
243         fprintf( stderr, "%s: %ld tests executed, %ld marked as todo, %ld %s.\n",
244                  name, successes + failures + todo_successes + todo_failures,
245                  todo_successes, failures + todo_failures,
246                  (failures + todo_failures != 1) ? "failures" : "failure" );
247     }
248     status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
249     return status;
250 }
251
252
253 /* main function */
254 int main( int argc, char **argv )
255 {
256     char *p;
257
258     winetest_argc = argc;
259     winetest_argv = argv;
260
261     if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = p;
262     if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
263     if ((p = getenv( "WINETEST_REPORT_SUCCESS"))) winetest_report_success = \
264                 atoi(p);
265     if (!argv[1])
266     {
267         fprintf( stderr, "Usage: %s test_name\n", argv[0] );
268         ExitProcess(1);
269     }
270     ExitProcess( run_test(argv[1]) );
271 }