1 /* err/test_results.c
2 *
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include <config.h>
21 #include <math.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <gsl/gsl_sys.h>
26 #include <gsl/gsl_machine.h>
27
28 #if HAVE_VPRINTF
29 #ifdef STDC_HEADERS
30 #include <stdarg.h>
31 #else
32 #include <varargs.h>
33 #endif
34 #endif
35
36 #include <gsl/gsl_test.h>
37
38 static unsigned int tests = 0;
39 static unsigned int passed = 0;
40 static unsigned int failed = 0;
41
42 static unsigned int verbose = 0;
43
44 static void
initialise(void)45 initialise (void)
46 {
47 const char * p = getenv("GSL_TEST_VERBOSE");
48
49 /* 0 = show failures only (we always want to see these) */
50 /* 1 = show passes and failures */
51
52 if (p == 0) /* environment variable is not set */
53 return ;
54
55 if (*p == '\0') /* environment variable is empty */
56 return ;
57
58 verbose = strtoul (p, 0, 0);
59
60 return;
61 }
62
63 static void
update(int s)64 update (int s)
65 {
66 tests++;
67
68 if (s == 0)
69 {
70 passed++;
71 }
72 else
73 {
74 failed++;
75 }
76 }
77
78 void
gsl_test(int status,const char * test_description,...)79 gsl_test (int status, const char *test_description,...)
80 {
81 if (!tests) initialise();
82
83 update (status);
84
85 if (status || verbose)
86 {
87 printf (status ? "FAIL: " : "PASS: ");
88
89 #if HAVE_VPRINTF
90 {
91 va_list ap;
92 #ifdef STDC_HEADERS
93 va_start (ap, test_description);
94 #else
95 va_start (ap);
96 #endif
97 vprintf (test_description, ap);
98 va_end (ap);
99 }
100 #endif
101
102 if (status && !verbose)
103 printf(" [%u]", tests);
104
105 printf("\n");
106 fflush (stdout);
107 }
108 }
109
110
111 void
gsl_test_rel(double result,double expected,double relative_error,const char * test_description,...)112 gsl_test_rel (double result, double expected, double relative_error,
113 const char *test_description,...)
114 {
115 int status ;
116
117 if (!tests) initialise();
118
119 /* Check for NaN vs inf vs number */
120
121 if (gsl_isnan(result) || gsl_isnan(expected))
122 {
123 status = gsl_isnan(result) != gsl_isnan(expected);
124 }
125 else if (gsl_isinf(result) || gsl_isinf(expected))
126 {
127 status = gsl_isinf(result) != gsl_isinf(expected);
128 }
129 else if ((expected > 0 && expected < GSL_DBL_MIN)
130 || (expected < 0 && expected > -(GSL_DBL_MIN)))
131 {
132 status = -1;
133 }
134 else if (expected != 0 )
135 {
136 status = (fabs(result-expected)/fabs(expected) > relative_error) ;
137 }
138 else
139 {
140 status = (fabs(result) > relative_error) ;
141 }
142
143 update (status);
144
145 if (status || verbose)
146 {
147 printf (status ? "FAIL: " : "PASS: ");
148
149 #if HAVE_VPRINTF
150 {
151 va_list ap;
152 #ifdef STDC_HEADERS
153 va_start (ap, test_description);
154 #else
155 va_start (ap);
156 #endif
157 vprintf (test_description, ap);
158 va_end (ap);
159 }
160 #endif
161
162 if (status == 0)
163 {
164 if (strlen(test_description) < 45)
165 {
166 printf(" (%g observed vs %g expected)", result, expected) ;
167 }
168 else
169 {
170 printf(" (%g obs vs %g exp)", result, expected) ;
171 }
172 }
173 else
174 {
175 printf(" (%.18g observed vs %.18g expected)", result, expected) ;
176 }
177
178 if (status == -1)
179 {
180 printf(" [test uses subnormal value]") ;
181 }
182
183 if (status && !verbose)
184 printf(" [%u]", tests);
185
186 printf ("\n") ;
187 fflush (stdout);
188 }
189 }
190
191 void
gsl_test_abs(double result,double expected,double absolute_error,const char * test_description,...)192 gsl_test_abs (double result, double expected, double absolute_error,
193 const char *test_description,...)
194 {
195 int status ;
196
197 if (!tests) initialise();
198
199 /* Check for NaN vs inf vs number */
200
201 if (gsl_isnan(result) || gsl_isnan(expected))
202 {
203 status = gsl_isnan(result) != gsl_isnan(expected);
204 }
205 else if (gsl_isinf(result) || gsl_isinf(expected))
206 {
207 status = gsl_isinf(result) != gsl_isinf(expected);
208 }
209 else if ((expected > 0 && expected < GSL_DBL_MIN)
210 || (expected < 0 && expected > -(GSL_DBL_MIN)))
211 {
212 status = -1;
213 }
214 else
215 {
216 status = fabs(result-expected) > absolute_error ;
217 }
218
219 update (status);
220
221 if (status || verbose)
222 {
223 printf (status ? "FAIL: " : "PASS: ");
224
225 #if HAVE_VPRINTF
226 {
227 va_list ap;
228
229 #ifdef STDC_HEADERS
230 va_start (ap, test_description);
231 #else
232 va_start (ap);
233 #endif
234 vprintf (test_description, ap);
235 va_end (ap);
236 }
237 #endif
238
239 if (status == 0)
240 {
241 if (strlen(test_description) < 45)
242 {
243 printf(" (%g observed vs %g expected)", result, expected) ;
244 }
245 else
246 {
247 printf(" (%g obs vs %g exp)", result, expected) ;
248 }
249 }
250 else
251 {
252 printf(" (%.18g observed vs %.18g expected)", result, expected) ;
253 }
254
255 if (status == -1)
256 {
257 printf(" [test uses subnormal value]") ;
258 }
259
260 if (status && !verbose)
261 printf(" [%u]", tests);
262
263 printf ("\n") ;
264 fflush (stdout);
265 }
266 }
267
268
269 void
gsl_test_factor(double result,double expected,double factor,const char * test_description,...)270 gsl_test_factor (double result, double expected, double factor,
271 const char *test_description,...)
272 {
273 int status;
274
275 if (!tests) initialise();
276
277 if ((expected > 0 && expected < GSL_DBL_MIN)
278 || (expected < 0 && expected > -(GSL_DBL_MIN)))
279 {
280 status = -1;
281 }
282 else if (result == expected)
283 {
284 status = 0;
285 }
286 else if (expected == 0.0)
287 {
288 status = (result > expected || result < expected);
289 }
290 else
291 {
292 double u = result / expected;
293 status = (u > factor || u < 1.0 / factor) ;
294 }
295
296 update (status);
297
298 if (status || verbose)
299 {
300 printf (status ? "FAIL: " : "PASS: ");
301
302 #if HAVE_VPRINTF
303 {
304 va_list ap;
305
306 #ifdef STDC_HEADERS
307 va_start (ap, test_description);
308 #else
309 va_start (ap);
310 #endif
311 vprintf (test_description, ap);
312 va_end (ap);
313 }
314 #endif
315 if (status == 0)
316 {
317 if (strlen(test_description) < 45)
318 {
319 printf(" (%g observed vs %g expected)", result, expected) ;
320 }
321 else
322 {
323 printf(" (%g obs vs %g exp)", result, expected) ;
324 }
325 }
326 else
327 {
328 printf(" (%.18g observed vs %.18g expected)", result, expected) ;
329 }
330
331 if (status == -1)
332 {
333 printf(" [test uses subnormal value]") ;
334 }
335
336 if (status && !verbose)
337 printf(" [%u]", tests);
338
339 printf ("\n") ;
340 fflush (stdout);
341 }
342 }
343
344 void
gsl_test_int(int result,int expected,const char * test_description,...)345 gsl_test_int (int result, int expected, const char *test_description,...)
346 {
347 int status = (result != expected) ;
348
349 if (!tests) initialise();
350
351 update (status);
352
353 if (status || verbose)
354 {
355 printf (status ? "FAIL: " : "PASS: ");
356
357 #if HAVE_VPRINTF
358 {
359 va_list ap;
360
361 #ifdef STDC_HEADERS
362 va_start (ap, test_description);
363 #else
364 va_start (ap);
365 #endif
366 vprintf (test_description, ap);
367 va_end (ap);
368 }
369 #endif
370 if (status == 0)
371 {
372 printf(" (%d observed vs %d expected)", result, expected) ;
373 }
374 else
375 {
376 printf(" (%d observed vs %d expected)", result, expected) ;
377 }
378
379 if (status && !verbose)
380 printf(" [%u]", tests);
381
382 printf ("\n");
383 fflush (stdout);
384 }
385 }
386
387 void
gsl_test_str(const char * result,const char * expected,const char * test_description,...)388 gsl_test_str (const char * result, const char * expected,
389 const char *test_description,...)
390 {
391 int status = strcmp(result,expected) ;
392
393 if (!tests) initialise();
394
395 update (status);
396
397 if (status || verbose)
398 {
399 printf (status ? "FAIL: " : "PASS: ");
400
401 #if HAVE_VPRINTF
402 {
403 va_list ap;
404
405 #ifdef STDC_HEADERS
406 va_start (ap, test_description);
407 #else
408 va_start (ap);
409 #endif
410 vprintf (test_description, ap);
411 va_end (ap);
412 }
413 #endif
414 if (status)
415 {
416 printf(" (%s observed vs %s expected)", result, expected) ;
417 }
418
419 if (status && !verbose)
420 printf(" [%u]", tests);
421
422 printf ("\n");
423 fflush (stdout);
424 }
425 }
426
427 void
gsl_test_verbose(int v)428 gsl_test_verbose (int v)
429 {
430 verbose = v;
431 }
432
433 int
gsl_test_summary(void)434 gsl_test_summary (void)
435 {
436 if (verbose && 0) /* FIXME: turned it off, this annoys me */
437 printf ("%d tests, passed %d, failed %d.\n", tests, passed, failed);
438
439 if (failed != 0)
440 {
441 return EXIT_FAILURE;
442 }
443
444 if (tests != passed + failed)
445 {
446 if (verbose)
447 printf ("TEST RESULTS DO NOT ADD UP %d != %d + %d\n",
448 tests, passed, failed);
449 return EXIT_FAILURE;
450 }
451
452 if (passed == tests)
453 {
454 if (!verbose) /* display a summary of passed tests */
455 printf ("Completed [%d/%d]\n", passed, tests);
456
457 return EXIT_SUCCESS;
458 }
459
460 return EXIT_FAILURE;
461 }
462