comparison test/automated/SDL_at.c @ 3741:808fad5fb593 gsoc2009_unit_tests

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