Blame test/results.c

Packit 67cb25
/* err/test_results.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <math.h>
Packit 67cb25
#include <string.h>
Packit 67cb25
#include <stdio.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <gsl/gsl_sys.h>
Packit 67cb25
#include <gsl/gsl_machine.h>
Packit 67cb25
Packit 67cb25
#if HAVE_VPRINTF
Packit 67cb25
#ifdef STDC_HEADERS
Packit 67cb25
#include <stdarg.h>
Packit 67cb25
#else
Packit 67cb25
#include <varargs.h>
Packit 67cb25
#endif
Packit 67cb25
#endif
Packit 67cb25
Packit 67cb25
#include <gsl/gsl_test.h>
Packit 67cb25
Packit 67cb25
static unsigned int tests = 0;
Packit 67cb25
static unsigned int passed = 0;
Packit 67cb25
static unsigned int failed = 0;
Packit 67cb25
Packit 67cb25
static unsigned int verbose = 0;
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
initialise (void)
Packit 67cb25
{
Packit 67cb25
  const char * p = getenv("GSL_TEST_VERBOSE");
Packit 67cb25
Packit 67cb25
  /* 0 = show failures only (we always want to see these) */
Packit 67cb25
  /* 1 = show passes and failures */
Packit 67cb25
Packit 67cb25
  if (p == 0)  /* environment variable is not set */
Packit 67cb25
    return ;
Packit 67cb25
Packit 67cb25
  if (*p == '\0') /* environment variable is empty */
Packit 67cb25
    return ;
Packit 67cb25
Packit 67cb25
  verbose = strtoul (p, 0, 0);  
Packit 67cb25
Packit 67cb25
  return;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void 
Packit 67cb25
update (int s)
Packit 67cb25
{
Packit 67cb25
  tests++;
Packit 67cb25
Packit 67cb25
  if (s == 0) 
Packit 67cb25
    {
Packit 67cb25
      passed++;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      failed++;
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_test (int status, const char *test_description,...)
Packit 67cb25
{
Packit 67cb25
  if (!tests) initialise();
Packit 67cb25
Packit 67cb25
  update (status);
Packit 67cb25
Packit 67cb25
  if (status || verbose)
Packit 67cb25
    {
Packit 67cb25
      printf (status ? "FAIL: " : "PASS: ");
Packit 67cb25
Packit 67cb25
#if HAVE_VPRINTF
Packit 67cb25
      {
Packit 67cb25
        va_list ap;
Packit 67cb25
#ifdef STDC_HEADERS
Packit 67cb25
        va_start (ap, test_description);
Packit 67cb25
#else
Packit 67cb25
        va_start (ap);
Packit 67cb25
#endif
Packit 67cb25
        vprintf (test_description, ap);
Packit 67cb25
        va_end (ap);
Packit 67cb25
      }
Packit 67cb25
#endif
Packit 67cb25
Packit 67cb25
      if (status && !verbose)
Packit 67cb25
        printf(" [%u]", tests);
Packit 67cb25
Packit 67cb25
      printf("\n");
Packit 67cb25
      fflush (stdout);
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_test_rel (double result, double expected, double relative_error,
Packit 67cb25
              const char *test_description,...)
Packit 67cb25
{
Packit 67cb25
  int status ;
Packit 67cb25
Packit 67cb25
  if (!tests) initialise();
Packit 67cb25
Packit 67cb25
  /* Check for NaN vs inf vs number */
Packit 67cb25
Packit 67cb25
  if (gsl_isnan(result) || gsl_isnan(expected)) 
Packit 67cb25
    {
Packit 67cb25
      status = gsl_isnan(result) != gsl_isnan(expected); 
Packit 67cb25
    }
Packit 67cb25
  else if (gsl_isinf(result) || gsl_isinf(expected)) 
Packit 67cb25
    {
Packit 67cb25
      status = gsl_isinf(result) != gsl_isinf(expected); 
Packit 67cb25
    }
Packit 67cb25
  else if ((expected > 0 && expected < GSL_DBL_MIN)
Packit 67cb25
           || (expected < 0 && expected > -(GSL_DBL_MIN)))
Packit 67cb25
    {
Packit 67cb25
      status = -1;
Packit 67cb25
    }
Packit 67cb25
  else if (expected != 0 ) 
Packit 67cb25
    {
Packit 67cb25
      status = (fabs(result-expected)/fabs(expected) > relative_error) ;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      status = (fabs(result) > relative_error) ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  update (status);
Packit 67cb25
Packit 67cb25
  if (status || verbose)
Packit 67cb25
    {
Packit 67cb25
      printf (status ? "FAIL: " : "PASS: ");
Packit 67cb25
Packit 67cb25
#if HAVE_VPRINTF
Packit 67cb25
      {
Packit 67cb25
        va_list ap;
Packit 67cb25
#ifdef STDC_HEADERS
Packit 67cb25
        va_start (ap, test_description);
Packit 67cb25
#else
Packit 67cb25
        va_start (ap);
Packit 67cb25
#endif
Packit 67cb25
        vprintf (test_description, ap);
Packit 67cb25
        va_end (ap);
Packit 67cb25
      }
Packit 67cb25
#endif
Packit 67cb25
Packit 67cb25
      if (status == 0)
Packit 67cb25
        {
Packit 67cb25
          if (strlen(test_description) < 45)
Packit 67cb25
            {
Packit 67cb25
              printf(" (%g observed vs %g expected)", result, expected) ;
Packit 67cb25
            }
Packit 67cb25
          else
Packit 67cb25
            {
Packit 67cb25
              printf(" (%g obs vs %g exp)", result, expected) ;
Packit 67cb25
            }
Packit 67cb25
        }
Packit 67cb25
      else 
Packit 67cb25
        {
Packit 67cb25
          printf(" (%.18g observed vs %.18g expected)", result, expected) ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (status == -1)
Packit 67cb25
        {
Packit 67cb25
          printf(" [test uses subnormal value]") ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (status && !verbose)
Packit 67cb25
        printf(" [%u]", tests);
Packit 67cb25
Packit 67cb25
      printf ("\n") ;
Packit 67cb25
      fflush (stdout);
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_test_abs (double result, double expected, double absolute_error,
Packit 67cb25
              const char *test_description,...)
Packit 67cb25
{
Packit 67cb25
  int status ;
Packit 67cb25
Packit 67cb25
  if (!tests) initialise();
Packit 67cb25
Packit 67cb25
  /* Check for NaN vs inf vs number */
Packit 67cb25
Packit 67cb25
  if (gsl_isnan(result) || gsl_isnan(expected)) 
Packit 67cb25
    {
Packit 67cb25
      status = gsl_isnan(result) != gsl_isnan(expected); 
Packit 67cb25
    }
Packit 67cb25
  else if (gsl_isinf(result) || gsl_isinf(expected)) 
Packit 67cb25
    {
Packit 67cb25
      status = gsl_isinf(result) != gsl_isinf(expected); 
Packit 67cb25
    }
Packit 67cb25
  else if ((expected > 0 && expected < GSL_DBL_MIN)
Packit 67cb25
           || (expected < 0 && expected > -(GSL_DBL_MIN)))
Packit 67cb25
    {
Packit 67cb25
      status = -1;
Packit 67cb25
    }
Packit 67cb25
  else 
Packit 67cb25
    {
Packit 67cb25
      status = fabs(result-expected) > absolute_error ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  update (status);
Packit 67cb25
Packit 67cb25
  if (status || verbose)
Packit 67cb25
    {
Packit 67cb25
      printf (status ? "FAIL: " : "PASS: ");
Packit 67cb25
Packit 67cb25
#if HAVE_VPRINTF
Packit 67cb25
      {
Packit 67cb25
        va_list ap;
Packit 67cb25
        
Packit 67cb25
#ifdef STDC_HEADERS
Packit 67cb25
        va_start (ap, test_description);
Packit 67cb25
#else
Packit 67cb25
        va_start (ap);
Packit 67cb25
#endif
Packit 67cb25
        vprintf (test_description, ap);
Packit 67cb25
        va_end (ap);
Packit 67cb25
      }
Packit 67cb25
#endif
Packit 67cb25
Packit 67cb25
      if (status == 0)
Packit 67cb25
        {
Packit 67cb25
          if (strlen(test_description) < 45)
Packit 67cb25
            {
Packit 67cb25
              printf(" (%g observed vs %g expected)", result, expected) ;
Packit 67cb25
            }
Packit 67cb25
          else
Packit 67cb25
            {
Packit 67cb25
              printf(" (%g obs vs %g exp)", result, expected) ;
Packit 67cb25
            }
Packit 67cb25
        }
Packit 67cb25
      else 
Packit 67cb25
        {
Packit 67cb25
          printf(" (%.18g observed vs %.18g expected)", result, expected) ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (status == -1)
Packit 67cb25
        {
Packit 67cb25
          printf(" [test uses subnormal value]") ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (status && !verbose)
Packit 67cb25
        printf(" [%u]", tests);
Packit 67cb25
Packit 67cb25
      printf ("\n") ;
Packit 67cb25
      fflush (stdout);
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_test_factor (double result, double expected, double factor,
Packit 67cb25
                 const char *test_description,...)
Packit 67cb25
{
Packit 67cb25
  int status;
Packit 67cb25
Packit 67cb25
  if (!tests) initialise();
Packit 67cb25
  
Packit 67cb25
  if ((expected > 0 && expected < GSL_DBL_MIN)
Packit 67cb25
      || (expected < 0 && expected > -(GSL_DBL_MIN)))
Packit 67cb25
    {
Packit 67cb25
      status = -1;
Packit 67cb25
    }
Packit 67cb25
  else if (result == expected) 
Packit 67cb25
    {
Packit 67cb25
      status = 0;
Packit 67cb25
    }
Packit 67cb25
  else if (expected == 0.0) 
Packit 67cb25
    {
Packit 67cb25
      status = (result > expected || result < expected);
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      double u = result / expected; 
Packit 67cb25
      status = (u > factor || u < 1.0 / factor) ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  update (status);
Packit 67cb25
Packit 67cb25
  if (status || verbose)
Packit 67cb25
    {
Packit 67cb25
      printf (status ? "FAIL: " : "PASS: ");
Packit 67cb25
Packit 67cb25
#if HAVE_VPRINTF
Packit 67cb25
      {
Packit 67cb25
        va_list ap;
Packit 67cb25
        
Packit 67cb25
#ifdef STDC_HEADERS
Packit 67cb25
        va_start (ap, test_description);
Packit 67cb25
#else
Packit 67cb25
        va_start (ap);
Packit 67cb25
#endif
Packit 67cb25
        vprintf (test_description, ap);
Packit 67cb25
        va_end (ap);
Packit 67cb25
      }
Packit 67cb25
#endif
Packit 67cb25
      if (status == 0)
Packit 67cb25
        {
Packit 67cb25
          if (strlen(test_description) < 45)
Packit 67cb25
            {
Packit 67cb25
              printf(" (%g observed vs %g expected)", result, expected) ;
Packit 67cb25
            }
Packit 67cb25
          else
Packit 67cb25
            {
Packit 67cb25
              printf(" (%g obs vs %g exp)", result, expected) ;
Packit 67cb25
            }
Packit 67cb25
        }
Packit 67cb25
      else 
Packit 67cb25
        {
Packit 67cb25
          printf(" (%.18g observed vs %.18g expected)", result, expected) ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (status == -1)
Packit 67cb25
        {
Packit 67cb25
          printf(" [test uses subnormal value]") ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (status && !verbose)
Packit 67cb25
        printf(" [%u]", tests);
Packit 67cb25
Packit 67cb25
      printf ("\n") ;
Packit 67cb25
      fflush (stdout);
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_test_int (int result, int expected, const char *test_description,...)
Packit 67cb25
{
Packit 67cb25
  int status = (result != expected) ;
Packit 67cb25
Packit 67cb25
  if (!tests) initialise();
Packit 67cb25
Packit 67cb25
  update (status);
Packit 67cb25
Packit 67cb25
  if (status || verbose)
Packit 67cb25
    {
Packit 67cb25
      printf (status ? "FAIL: " : "PASS: ");
Packit 67cb25
Packit 67cb25
#if HAVE_VPRINTF
Packit 67cb25
      {
Packit 67cb25
        va_list ap;
Packit 67cb25
        
Packit 67cb25
#ifdef STDC_HEADERS
Packit 67cb25
        va_start (ap, test_description);
Packit 67cb25
#else
Packit 67cb25
        va_start (ap);
Packit 67cb25
#endif
Packit 67cb25
        vprintf (test_description, ap);
Packit 67cb25
        va_end (ap);
Packit 67cb25
      }
Packit 67cb25
#endif
Packit 67cb25
      if (status == 0)
Packit 67cb25
        {
Packit 67cb25
          printf(" (%d observed vs %d expected)", result, expected) ;
Packit 67cb25
        }
Packit 67cb25
      else 
Packit 67cb25
        {
Packit 67cb25
          printf(" (%d observed vs %d expected)", result, expected) ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (status && !verbose)
Packit 67cb25
        printf(" [%u]", tests);
Packit 67cb25
Packit 67cb25
      printf ("\n");
Packit 67cb25
      fflush (stdout);
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_test_str (const char * result, const char * expected, 
Packit 67cb25
              const char *test_description,...)
Packit 67cb25
{
Packit 67cb25
  int status = strcmp(result,expected) ;
Packit 67cb25
Packit 67cb25
  if (!tests) initialise();
Packit 67cb25
Packit 67cb25
  update (status);
Packit 67cb25
Packit 67cb25
  if (status || verbose)
Packit 67cb25
    {
Packit 67cb25
      printf (status ? "FAIL: " : "PASS: ");
Packit 67cb25
Packit 67cb25
#if HAVE_VPRINTF
Packit 67cb25
      {
Packit 67cb25
        va_list ap;
Packit 67cb25
        
Packit 67cb25
#ifdef STDC_HEADERS
Packit 67cb25
        va_start (ap, test_description);
Packit 67cb25
#else
Packit 67cb25
        va_start (ap);
Packit 67cb25
#endif
Packit 67cb25
        vprintf (test_description, ap);
Packit 67cb25
        va_end (ap);
Packit 67cb25
      }
Packit 67cb25
#endif
Packit 67cb25
      if (status)
Packit 67cb25
        {
Packit 67cb25
          printf(" (%s observed vs %s expected)", result, expected) ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (status && !verbose)
Packit 67cb25
        printf(" [%u]", tests);
Packit 67cb25
Packit 67cb25
      printf ("\n");
Packit 67cb25
      fflush (stdout);
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_test_verbose (int v)
Packit 67cb25
{
Packit 67cb25
  verbose = v;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_test_summary (void)
Packit 67cb25
{
Packit 67cb25
  if (verbose && 0)             /* FIXME: turned it off, this annoys me */
Packit 67cb25
    printf ("%d tests, passed %d, failed %d.\n", tests, passed, failed);
Packit 67cb25
Packit 67cb25
  if (failed != 0)
Packit 67cb25
    {
Packit 67cb25
      return EXIT_FAILURE;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  if (tests != passed + failed)
Packit 67cb25
    {
Packit 67cb25
      if (verbose)
Packit 67cb25
        printf ("TEST RESULTS DO NOT ADD UP %d != %d + %d\n",
Packit 67cb25
                tests, passed, failed);
Packit 67cb25
      return EXIT_FAILURE;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  if (passed == tests)
Packit 67cb25
    {
Packit 67cb25
      if (!verbose)         /* display a summary of passed tests */
Packit 67cb25
        printf ("Completed [%d/%d]\n", passed, tests);
Packit 67cb25
Packit 67cb25
      return EXIT_SUCCESS;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return EXIT_FAILURE;
Packit 67cb25
}