vecdebug-test.c:


/* vecdebug-test.c test routines for the vector package.
 * © Copyright 1999 by John Halleck
 * All rights reserved.
 */
/* Version of August 14th, 1999 */

#include <stdio.h>
  /* Standard IO */

#include "errors.h"
  /* Common error codes */
#include "vec.h"
  /* Vector specific stuff */

#include "vecdebug.h"
  /* Matrix debugging help */

#include "reportframe.h"
  /* Standard testing output form */

#define FUDGE (1.0 / 1000000.0)
/* Fudge value for the near equality tests */


#define ARRAYSIZE 8
/* How big an array do we need to test? (At least 3) */

double test1[ARRAYSIZE],
       test2[ARRAYSIZE]; /* Vectors to test with. */



/* Report an error */
static error haserrorstat (char *reason, error problem) {
   error result;
   result = goterrorstat (reason, problem);
   if (problem == ERRfalse) {
      result = vecprint ("test1: ", 9, test1);
      if (result) return result;
      result = vecprint ("test2: ", 9, test2);
      if (result) return result; 
   }
   return result;
}

int main (argc, argv)
  int argc; char argv[];
{
  error problem      = NoError;

  int i; /* Loop index */

  double temp;

  inittests("vecdebug");

/* ---------------------------------------------------------------- */

  newsection ("print routine interfaces");

  newtest("vecprint");

  if (ERRsize != (problem = vecprint ("... Vector size test", 0, test1)) )
     goterrorstat ("zero length vector passed", problem);
  if (ERRnil != (problem = vecprint ("... Nil vector test", ARRAYSIZE, 0)) )
     goterrorstat ("nil vector pointer passed", problem);

  for (i=0; i<ARRAYSIZE; i++) test1[i] = (double) i;
  printf ("The next line should be the numbers zero through seven\n");
  if ((problem = vecprint ("Digits:", ARRAYSIZE, test1))) 
     goterrorstat ("perfectly good call rejected", problem);

  endtest();

  endsection ();

  /* =============================================================== */

  newsection ("Equality");

  /* --------------------------------------------------------------- */
  newtest("veciseq");

   for (i=0; i<ARRAYSIZE; i++) { test1[i] = 1.0; test2[i] = 1.0; }
   test1[0] = 2.0; test1[ARRAYSIZE-1] = 2.0;
   /* The arrays are equal in locations 1-6, and unequal in 0 and 8 
    * So if we do equality tests in just 1-6 we can catch fencepoint
    * errors.
    */

   if (ERRsize != (problem = veciseq (0, test1, test1)) )
      goterrorstat ("failed to notice zero length vector", problem);
   if ( ERRnil != (problem = veciseq (ARRAYSIZE, 0, test1))
     || ERRnil != (problem = veciseq (ARRAYSIZE, test1, 0)) )
      goterrorstat ("failed to notice nil vector pointer", problem);

   if ((problem = veciseq (ARRAYSIZE, test1, test1)))
      goterrorstat ("failed on valid (identity) call", problem);

   if ((problem = veciseq (ARRAYSIZE-2, &test1[1], &test2[1])))
      haserrorstat ("failed on bracketed equal test", problem);

   for (i=1;i<(ARRAYSIZE-1);i++) {
       temp = test1[i];
       test1[i] = 5.0; /* not something in either... */
       if ((problem = veciseq (ARRAYSIZE-1, &test1[1], &test2[1])) != ERRfalse
           && !testerrors) {
           haserrorstat ("*** veciseq gives false positive", problem);
           printf ("(At location %d)\n", i);
       }
       test1[i] = temp;
   }

   endtest();

   /* ------------------------------------------------------------------ */
   newtest("vecisaeq");

   if (ERRsize != (problem = vecisaeq (0, test1, test1, FUDGE)))
      goterrorstat ("failed to notice zero length vector", problem);
   if ( ERRnil != (problem = vecisaeq (ARRAYSIZE, 0, test1, FUDGE))
     || ERRnil != (problem = vecisaeq (ARRAYSIZE, test1, 0, FUDGE)) )
      goterrorstat ("failed to notice nil vector pointer", problem);

   if ((problem = vecisaeq (ARRAYSIZE, test1, test1, FUDGE)))
      haserrorstat ("failed on valid (identity) call", problem);

   if ((problem = vecisaeq (ARRAYSIZE-2, &test1[1], &test2[1], FUDGE)))
      haserrorstat ("failed on bracketed equal test", problem);

   /* Should fail with totally unequal stuff */
   for (i=1;i<ARRAYSIZE-1;i++) {
       temp = test1[i];
       test1[i] = 5.0; /* not something in either... */
       if ((problem = vecisaeq (ARRAYSIZE-2, &test1[1], &test2[1], FUDGE))
          != ERRfalse) {
           if (!testerrors) {
              haserrorstat ("false equal positive", problem);
              printf ("(at %d)\n", i);
           }
       }
       test1[i] = temp;
   }

   /* Should pass with stuff close enough */
   for (i=1;i<ARRAYSIZE-1;i++) {
       temp = test1[i];
       test1[i] = temp + FUDGE/2.0; /* Something close enough, but not zero */
       if ((problem = vecisaeq (ARRAYSIZE-2, &test1[1], &test2[1], FUDGE))
         != NoError) {
           if (!testerrors) {
              haserrorstat ("false cloose negative", problem);
              printf ("(at %d)\n", i);
           }
       }
       test1[i] = temp;
   }

   endtest();

   /* -------------------------------------------------------------------- */

endsection();
finalizetests();
return progerrors;
}

Go to ...


This page is http://www.cc.utah.edu/~nahaj/cave/survey/code/c/vecdebug-test.c.html
© Copyright 2000 by John Halleck, All Rights Reserved.
This snapshot was last modified on August 23rd, 2000
And the underlying file was last modified on May 11th, 2000