comparison test/automated/SDL_at.c @ 3259:22ac66da0765

Merged Edgar's code changes from Google Summer of Code 2009
author Sam Lantinga <slouken@libsdl.org>
date Mon, 07 Sep 2009 05:06:34 +0000
parents
children 1f1a41879fe4
comparison
equal deleted inserted replaced
3258:e786366ea23b 3259:22ac66da0765
1 /*
2 * Common code for automated test suite.
3 *
4 * Written by Edgar Simo "bobbens"
5 *
6 * Released under Public Domain.
7 */
8
9
10 #include "SDL_at.h"
11
12 #include <stdio.h> /* printf/fprintf */
13 #include <stdarg.h> /* va_list */
14 #include <string.h> /* strdup */
15 #include <stdlib.h> /* free */
16
17
18 /*
19 * Internal usage SDL_AT variables.
20 */
21 static char *at_suite_msg = NULL; /**< Testsuite message. */
22 static char *at_test_msg = NULL; /**< Testcase message. */
23 static int at_success = 0; /**< Number of successful testcases. */
24 static int at_failure = 0; /**< Number of failed testcases. */
25
26
27 /*
28 * Global properties.
29 */
30 static int at_verbose = 0; /**< Verbosity. */
31 static int at_quiet = 0; /**< Quietness. */
32
33
34 /*
35 * Prototypes.
36 */
37 static void SDL_ATcleanup (void);
38 static void SDL_ATendWith( int success );
39 static void SDL_ATassertFailed( const char *msg );
40
41
42 /**
43 * @brief Cleans up the automated testsuite state.
44 */
45 static void SDL_ATcleanup (void)
46 {
47 if (at_suite_msg != NULL)
48 free(at_suite_msg);
49 at_suite_msg = NULL;
50 if (at_test_msg != NULL)
51 free(at_test_msg);
52 at_test_msg = NULL;
53 at_success = 0;
54 at_failure = 0;
55 }
56
57
58 /**
59 * @brief Begin testsuite.
60 */
61 void SDL_ATinit( const char *suite )
62 {
63 /* Do not open twice. */
64 if (at_suite_msg) {
65 SDL_ATprintErr( "AT suite '%s' not closed before opening suite '%s'\n",
66 at_suite_msg, suite );
67 }
68 /* Must have a name. */
69 if (suite == NULL) {
70 SDL_ATprintErr( "AT testsuite does not have a name.\n");
71 }
72 SDL_ATcleanup();
73 at_suite_msg = strdup(suite);
74
75 /* Verbose message. */
76 SDL_ATprintVerbose( 2, "--+---> Started Test Suite '%s'\n", at_suite_msg );
77 }
78
79
80 /**
81 * @brief Finish testsuite.
82 */
83 int SDL_ATfinish (void)
84 {
85 int failed;
86
87 /* Make sure initialized. */
88 if (at_suite_msg == NULL) {
89 SDL_ATprintErr("Ended testcase without initializing.\n");
90 return 1;
91 }
92
93 /* Finished without closing testcase. */
94 if (at_test_msg) {
95 SDL_ATprintErr( "AT suite '%s' finished without closing testcase '%s'\n",
96 at_suite_msg, at_test_msg );
97 }
98
99 /* Verbose message. */
100 SDL_ATprintVerbose( 2, "<-+---- Finished Test Suite '%s'\n", at_suite_msg );
101
102 /* Display message if verbose on failed. */
103 failed = at_failure;
104 if (at_failure > 0) {
105 SDL_ATprintErr( "%s : Failed %d out of %d testcases!\n",
106 at_suite_msg, at_failure, at_failure+at_success );
107 }
108 else {
109 SDL_ATprint( "%s : All tests successful (%d)\n",
110 at_suite_msg, at_success );
111 }
112
113 /* Clean up. */
114 SDL_ATcleanup();
115
116 /* Return failed. */
117 return failed;
118 }
119
120
121 /**
122 * @brief Sets a property.
123 */
124 void SDL_ATseti( int property, int value )
125 {
126 switch (property) {
127 case SDL_AT_VERBOSE:
128 at_verbose = value;
129 break;
130
131 case SDL_AT_QUIET:
132 at_quiet = value;
133 break;
134 }
135 }
136
137
138 /**
139 * @brief Gets a property.
140 */
141 void SDL_ATgeti( int property, int *value )
142 {
143 switch (property) {
144 case SDL_AT_VERBOSE:
145 *value = at_verbose;
146 break;
147
148 case SDL_AT_QUIET:
149 *value = at_quiet;
150 break;
151 }
152 }
153
154
155 /**
156 * @brief Begin testcase.
157 */
158 void SDL_ATbegin( const char *testcase )
159 {
160 /* Do not open twice. */
161 if (at_test_msg) {
162 SDL_ATprintErr( "AT testcase '%s' not closed before opening testcase '%s'\n",
163 at_test_msg, testcase );
164 }
165 /* Must have a name. */
166 if (testcase == NULL) {
167 SDL_ATprintErr( "AT testcase does not have a name.\n");
168 }
169 at_test_msg = strdup(testcase);
170
171 /* Verbose message. */
172 SDL_ATprintVerbose( 2, " +---> StartedTest Case '%s'\n", testcase );
173 }
174
175
176 /**
177 * @brief Ends the testcase with a succes or failure.
178 */
179 static void SDL_ATendWith( int success )
180 {
181 /* Make sure initialized. */
182 if (at_test_msg == NULL) {
183 SDL_ATprintErr("Ended testcase without initializing.\n");
184 return;
185 }
186
187 /* Mark as success or failure. */
188 if (success)
189 at_success++;
190 else
191 at_failure++;
192
193 /* Verbose message. */
194 SDL_ATprintVerbose( 2, " +---- Finished Test Case '%s'\n", at_test_msg );
195
196 /* Clean up. */
197 if (at_test_msg != NULL)
198 free(at_test_msg);
199 at_test_msg = NULL;
200 }
201
202
203 /**
204 * @brief Display failed assert message.
205 */
206 static void SDL_ATassertFailed( const char *msg )
207 {
208 /* Print. */
209 SDL_ATprintErr( "Assert Failed!\n" );
210 SDL_ATprintErr( " %s\n", msg );
211 SDL_ATprintErr( " Test Case '%s'\n", at_test_msg );
212 SDL_ATprintErr( " Test Suite '%s'\n", at_suite_msg );
213 /* End. */
214 SDL_ATendWith(0);
215 }
216
217
218 /**
219 * @brief Testcase test.
220 */
221 int SDL_ATassert( const char *msg, int condition )
222 {
223 /* Condition failed. */
224 if (!condition) {
225 /* Failed message. */
226 SDL_ATassertFailed(msg);
227 }
228 return !condition;
229 }
230
231
232 /**
233 * @brief Testcase test.
234 */
235 int SDL_ATvassert( int condition, const char *msg, ... )
236 {
237 va_list args;
238 char buf[256];
239
240 /* Condition failed. */
241 if (!condition) {
242 /* Get message. */
243 va_start( args, msg );
244 vsnprintf( buf, sizeof(buf), msg, args );
245 va_end( args );
246 /* Failed message. */
247 SDL_ATassertFailed( buf );
248 }
249 return !condition;
250 }
251
252
253 /**
254 * @brief End testcase.
255 */
256 void SDL_ATend (void)
257 {
258 SDL_ATendWith(1);
259 }
260
261
262 /**
263 * @brief Displays an error.
264 */
265 int SDL_ATprintErr( const char *msg, ... )
266 {
267 va_list ap;
268 int ret;
269
270 /* Make sure there is something to print. */
271 if (msg == NULL)
272 return 0;
273 else {
274 va_start(ap, msg);
275 ret = vfprintf( stderr, msg, ap );
276 va_end(ap);
277 }
278
279 return ret;
280 }
281
282
283 /**
284 * @brief Displays a verbose message.
285 */
286 int SDL_ATprintVerbose( int level, const char *msg, ... )
287 {
288 va_list ap;
289 int ret;
290
291 /* Only print if not quiet. */
292 if (at_quiet || (at_verbose < level))
293 return 0;
294
295 /* Make sure there is something to print. */
296 if (msg == NULL)
297 return 0;
298 else {
299 va_start(ap, msg);
300 ret = vfprintf( stdout, msg, ap );
301 va_end(ap);
302 }
303
304 return ret;
305 }
306
307
308