3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
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.
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.
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.
25 #include <gsl/gsl_sys.h>
26 #include <gsl/gsl_machine.h>
36 #include <gsl/gsl_test.h>
38 static unsigned int tests = 0;
39 static unsigned int passed = 0;
40 static unsigned int failed = 0;
42 static unsigned int verbose = 0;
47 const char * p = getenv("GSL_TEST_VERBOSE");
49 /* 0 = show failures only (we always want to see these) */
50 /* 1 = show passes and failures */
52 if (p == 0) /* environment variable is not set */
55 if (*p == '\0') /* environment variable is empty */
58 verbose = strtoul (p, 0, 0);
79 gsl_test (int status, const char *test_description,...)
81 if (!tests) initialise();
85 if (status || verbose)
87 printf (status ? "FAIL: " : "PASS: ");
93 va_start (ap, test_description);
97 vprintf (test_description, ap);
102 if (status && !verbose)
103 printf(" [%u]", tests);
112 gsl_test_rel (double result, double expected, double relative_error,
113 const char *test_description,...)
117 if (!tests) initialise();
119 /* Check for NaN vs inf vs number */
121 if (gsl_isnan(result) || gsl_isnan(expected))
123 status = gsl_isnan(result) != gsl_isnan(expected);
125 else if (gsl_isinf(result) || gsl_isinf(expected))
127 status = gsl_isinf(result) != gsl_isinf(expected);
129 else if ((expected > 0 && expected < GSL_DBL_MIN)
130 || (expected < 0 && expected > -(GSL_DBL_MIN)))
134 else if (expected != 0 )
136 status = (fabs(result-expected)/fabs(expected) > relative_error) ;
140 status = (fabs(result) > relative_error) ;
145 if (status || verbose)
147 printf (status ? "FAIL: " : "PASS: ");
153 va_start (ap, test_description);
157 vprintf (test_description, ap);
164 if (strlen(test_description) < 45)
166 printf(" (%g observed vs %g expected)", result, expected) ;
170 printf(" (%g obs vs %g exp)", result, expected) ;
175 printf(" (%.18g observed vs %.18g expected)", result, expected) ;
180 printf(" [test uses subnormal value]") ;
183 if (status && !verbose)
184 printf(" [%u]", tests);
192 gsl_test_abs (double result, double expected, double absolute_error,
193 const char *test_description,...)
197 if (!tests) initialise();
199 /* Check for NaN vs inf vs number */
201 if (gsl_isnan(result) || gsl_isnan(expected))
203 status = gsl_isnan(result) != gsl_isnan(expected);
205 else if (gsl_isinf(result) || gsl_isinf(expected))
207 status = gsl_isinf(result) != gsl_isinf(expected);
209 else if ((expected > 0 && expected < GSL_DBL_MIN)
210 || (expected < 0 && expected > -(GSL_DBL_MIN)))
216 status = fabs(result-expected) > absolute_error ;
221 if (status || verbose)
223 printf (status ? "FAIL: " : "PASS: ");
230 va_start (ap, test_description);
234 vprintf (test_description, ap);
241 if (strlen(test_description) < 45)
243 printf(" (%g observed vs %g expected)", result, expected) ;
247 printf(" (%g obs vs %g exp)", result, expected) ;
252 printf(" (%.18g observed vs %.18g expected)", result, expected) ;
257 printf(" [test uses subnormal value]") ;
260 if (status && !verbose)
261 printf(" [%u]", tests);
270 gsl_test_factor (double result, double expected, double factor,
271 const char *test_description,...)
275 if (!tests) initialise();
277 if ((expected > 0 && expected < GSL_DBL_MIN)
278 || (expected < 0 && expected > -(GSL_DBL_MIN)))
282 else if (result == expected)
286 else if (expected == 0.0)
288 status = (result > expected || result < expected);
292 double u = result / expected;
293 status = (u > factor || u < 1.0 / factor) ;
298 if (status || verbose)
300 printf (status ? "FAIL: " : "PASS: ");
307 va_start (ap, test_description);
311 vprintf (test_description, ap);
317 if (strlen(test_description) < 45)
319 printf(" (%g observed vs %g expected)", result, expected) ;
323 printf(" (%g obs vs %g exp)", result, expected) ;
328 printf(" (%.18g observed vs %.18g expected)", result, expected) ;
333 printf(" [test uses subnormal value]") ;
336 if (status && !verbose)
337 printf(" [%u]", tests);
345 gsl_test_int (int result, int expected, const char *test_description,...)
347 int status = (result != expected) ;
349 if (!tests) initialise();
353 if (status || verbose)
355 printf (status ? "FAIL: " : "PASS: ");
362 va_start (ap, test_description);
366 vprintf (test_description, ap);
372 printf(" (%d observed vs %d expected)", result, expected) ;
376 printf(" (%d observed vs %d expected)", result, expected) ;
379 if (status && !verbose)
380 printf(" [%u]", tests);
388 gsl_test_str (const char * result, const char * expected,
389 const char *test_description,...)
391 int status = strcmp(result,expected) ;
393 if (!tests) initialise();
397 if (status || verbose)
399 printf (status ? "FAIL: " : "PASS: ");
406 va_start (ap, test_description);
410 vprintf (test_description, ap);
416 printf(" (%s observed vs %s expected)", result, expected) ;
419 if (status && !verbose)
420 printf(" [%u]", tests);
428 gsl_test_verbose (int v)
434 gsl_test_summary (void)
436 if (verbose && 0) /* FIXME: turned it off, this annoys me */
437 printf ("%d tests, passed %d, failed %d.\n", tests, passed, failed);
444 if (tests != passed + failed)
447 printf ("TEST RESULTS DO NOT ADD UP %d != %d + %d\n",
448 tests, passed, failed);
454 if (!verbose) /* display a summary of passed tests */
455 printf ("Completed [%d/%d]\n", passed, tests);