- Implement CCM_{GET|SET}COLORSCHEME.
[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
28 /* debug level */
29 int winetest_debug = 1;
30
31 /* current platform */
32 const char *winetest_platform = "windows";
33
34 struct test
35 {
36     const char  *name;
37     void       (*func)(void);
38 };
39
40 extern const struct test winetest_testlist[];
41 static const struct test *current_test; /* test currently being run */
42 /* FIXME: Access to all the following variables must be protected in a 
43  * multithread test. Either via thread local storage or via critical sections
44  */
45 static const char* current_file;        /* file of current check */
46 static int current_line;                /* line of current check */
47
48 static int successes;         /* number of successful tests */
49 static int failures;          /* number of failures */
50 static int todo_successes;    /* number of successful tests inside todo block */
51 static int todo_failures;     /* number of failures inside todo block */
52 static int todo_level;        /* current todo nesting level */
53 static int todo_do_loop;
54
55 /*
56  * Checks condition.
57  * Parameters:
58  *   - condition - condition to check;
59  *   - msg test description;
60  *   - file - test application source code file name of the check
61  *   - line - test application source code file line number of the check
62  * Return:
63  *   0 if condition does not have the expected value, 1 otherwise
64  */
65 int winetest_ok( int condition, const char *msg, ... )
66 {
67     va_list valist;
68
69     if (todo_level)
70     {
71         if (condition)
72         {
73             fprintf( stderr, "%s:%d: Test succeeded inside todo block",
74                      current_file, current_line );
75             if (msg && msg[0])
76             {
77                 va_start(valist, msg);
78                 fprintf(stderr,": ");
79                 vfprintf(stderr, msg, valist);
80                 va_end(valist);
81             }
82             fputc( '\n', stderr );
83             todo_failures++;
84             return 0;
85         }
86         else todo_successes++;
87     }
88     else
89     {
90         if (!condition)
91         {
92             fprintf( stderr, "%s:%d: Test failed",
93                      current_file, current_line );
94             if (msg && msg[0])
95             {
96                 va_start(valist, msg);
97                 fprintf( stderr,": ");
98                 vfprintf(stderr, msg, valist);
99                 va_end(valist);
100             }
101             fputc( '\n', stderr );
102             failures++;
103             return 0;
104         }
105         else successes++;
106     }
107     return 1;
108 }
109
110 winetest_ok_funcptr winetest_set_ok_location( const char* file, int line )
111 {
112     current_file=file;
113     current_line=line;
114     return &winetest_ok;
115 }
116
117 void winetest_trace( const char *msg, ... )
118 {
119     va_list valist;
120
121     if (winetest_debug > 0)
122     {
123         va_start(valist, msg);
124         vfprintf(stderr, msg, valist);
125         va_end(valist);
126     }
127 }
128
129 winetest_trace_funcptr winetest_set_trace_location( const char* file, int line )
130 {
131     current_file=file;
132     current_line=line;
133     return &winetest_trace;
134 }
135
136 void winetest_start_todo( const char* platform )
137 {
138     if (strcmp(winetest_platform,platform)==0)
139         todo_level++;
140     todo_do_loop=1;
141 }
142
143 int winetest_loop_todo(void)
144 {
145     int do_loop=todo_do_loop;
146     todo_do_loop=0;
147     return do_loop;
148 }
149
150 void winetest_end_todo( const char* platform )
151 {
152     if (strcmp(winetest_platform,platform)==0)
153         todo_level--;
154 }
155
156 /* Find a test by name */
157 static const struct test *find_test( const char *name )
158 {
159     const struct test *test;
160     const char *p;
161     int len;
162
163     if ((p = strrchr( name, '/' ))) name = p + 1;
164     if ((p = strrchr( name, '\\' ))) name = p + 1;
165     len = strlen(name);
166     if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
167
168     for (test = winetest_testlist; test->name; test++)
169     {
170         if (!strncmp( test->name, name, len ) && !test->name[len]) break;
171     }
172     return test->name ? test : NULL;
173 }
174
175
176 /* Run a named test, and return exit status */
177 static int run_test( const char *name )
178 {
179     const struct test *test;
180     int status;
181
182     if (!(test = find_test( name )))
183     {
184         fprintf( stderr, "Fatal: test '%s' does not exist.\n", name );
185         exit(1);
186     }
187     successes = failures = todo_successes = todo_failures = 0;
188     todo_level = 0;
189     current_test = test;
190     test->func();
191
192     if (winetest_debug)
193     {
194         fprintf( stderr, "%s: %d tests executed, %d marked as todo, %d %s.\n",
195                  name, successes + failures + todo_successes + todo_failures,
196                  todo_successes, failures + todo_failures,
197                  (failures + todo_failures != 1) ? "failures" : "failure" );
198     }
199     status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
200     return status;
201 }
202
203
204 /* main function */
205 int main( int argc, char **argv )
206 {
207     char *p;
208
209     if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = p;
210     if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
211     if (!argv[1])
212     {
213         fprintf( stderr, "Usage: %s test_name\n", argv[0] );
214         exit(1);
215     }
216     exit( run_test(argv[1]) );
217 }