matdebug-test.c:


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

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

#include "errors.h"
  /* Common error codes */

#include "mat.h"
  /* Matrix specific stuff */

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

#include "reportframe.h"
  /* Standard form for test results */

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

double test1[1][1];
double test5[5][5];

double R[4][2], A[4][3], B[3][2], BT[2][3];
double TR[4][2], TA[4][3], TB[3][2], TBT[2][3];
double TT[4][2];

int loopfail;

int main (argc, argv)
  int argc; char argv[];
{
  error problem;
  int i,j; /* Loop indices */

  double memtestarray[4][5], memtestaref[4][5];

  double size5a[5], size5b[5];

  size5a[0] = 1.0; size5a[1] = 2.0; size5a[2] = 3.0; size5a[3] = 4.0;
  size5a[4] = 5.0;
  size5b[0] = 1.0; size5b[1] = 10.0; size5b[2] = 10.0; 
  size5b[3] = 10.0; size5b[4] = 5.0;

 
  inittests ("matdebug");

/* ========================== internal routine tests ======================== */

  newsection ("Print interfaces");

  newtest ("matprint");

 if ( ERRsize != (problem = matprint (0, 0, 5, memtestarray))
   || ERRsize != (problem = matprint (0, 4, 0, memtestarray)) )
    goterrorstat ("bad sized arrays accepted.", problem);
 if ( ERRnil != (problem = matprint (0, 4, 5, 0)) )
    goterrorstat ("nil pointer accepted", problem);

 for (i=0;i<4;i++) for (j=0;j<5;j++)
    memtestarray[i][j] = (i+1)*10 + j + 1;

 if ((problem = matprint ("This should be a 4 by 5 array, a(ij) = ij",
                         4, 5, memtestarray
                        )))
    goterrorstat ("failed valid call?", problem);

  endtest();

  newtest("matcprint");

 if ( ERRsize != (problem = matcprint (0, 0, 5, memtestarray, memtestaref))
   || ERRsize != (problem = matcprint (0, 4, 0, memtestarray, memtestaref)))
    goterror ("bad sizes accepted");
 if (ERRnil != (problem = matprint (0, 4, 5, 0)) )
    goterror ("nil pointer accepted");

 for (i=0;i<4;i++) for (j=0;j<5;j++) {
    memtestarray[i][j] = (i+1)*10 + j + 1;
    memtestaref [i][j] = 100 + (i+1)*10 + j + 1;
 }

 if ((problem = matcprint ("Two matrices second 100 plus the first",
                           4, 5, memtestarray, memtestaref
                          ))) 
    goterrorstat ("valid call failed?", problem);


   endtest();
   endsection();

/* ===================== Equality Routines ================================== */

   newsection ("Equality routines");

/* ------------------------------------  matiseq ---------------------------- */

   newtest ("matiseq");

   for (i=0; i<4; i++) for (j=0; j<5; j++) {
       memtestarray[i][j] = 4.0; memtestaref[i][j] = 4.0;
   }
   if ( ERRnil != (problem = matiseq (4, 5,            0, memtestaref))
     || ERRnil != (problem = matiseq (4, 5, memtestarray,           0))
      ) goterrorstat ("Accepted nil pointer", problem);
   if ( ERRsize != (problem = matiseq (0, 5, memtestarray, memtestaref))
     || ERRsize != (problem = matiseq (4, 0, memtestarray, memtestaref))
      ) goterrorstat ("Accepted bad size", problem);

   /* We just set them equal, they better be equal. */
   if ((problem = matiseq (4, 5, memtestarray, memtestaref))) {
      if (problem == ERRfalse) problem = NoError;
      goterrorstat ("false negative", problem);
   }

   /* Does it notice inequality in all positions? */
   loopfail = 0;
   for (i=0; i<4; i++) for (j=0; j<5; j++) {
       memtestaref[i][j] = 3.0;
       if (!matiseq (4, 5, memtestarray, memtestaref)) {
          if (!loopfail) {
             goterror ("false positive");
             loopfail = 1;
          }
       }
       memtestaref[i][j] = 4.0;
   }
   /* Just paranoia...  they should still be equal when we are done. */
   if ((problem = matiseq (4, 5, memtestarray, memtestaref)))
      goterror ("false negative at end");

   endtest();

/* ------------------------------------  matisaeq ----------------------------- */

   newtest ("matisaeq");
   printf ("with fudge of %f\n", FUDGE);
   /* Note that this is the same test as for equal, followed by
    * a test for something "close")
    */
   for (i=0; i<4; i++) for (j=0; j<5; j++) {
       memtestarray[i][j] = 4.0; memtestaref[i][j] = 4.0;
   }

   if ( ERRnil != (problem = matisaeq (4, 5,            0, memtestaref, FUDGE))
     || ERRnil != (problem = matisaeq (4, 5, memtestarray,           0, FUDGE)) 
      ) goterrorstat ("Accepted nil pointer", problem);
   if ( ERRsize != (problem = matisaeq (0, 5, memtestarray, memtestaref, FUDGE))
     || ERRsize != (problem = matisaeq (4, 0, memtestarray, memtestaref, FUDGE))
      ) goterrorstat ("Accepted bad size", problem);

   /* We just set them equal, they better be equal. */
   if ((problem = matisaeq (4, 5, memtestarray, memtestaref, FUDGE))) {
      if (problem == ERRfalse) problem = NoError;
      goterrorstat ("false negative", problem);
   }
   /* Does it notice inequality in all positions? */
   loopfail = 0;
   for (i=0; i<4; i++) for (j=0; j<5; j++) {
       memtestaref[i][j] = 3.0;
       if (!(problem = matisaeq (4, 5, memtestarray, memtestaref, FUDGE))) {
          if (!loopfail) {
             if (problem == ERRfalse) problem = NoError;
             goterrorstat ("false positive in equality test", problem);
             printf ("   Location %d, %d\n", i, j);
             matprint ("Array first array", 4, 5, memtestarray);
             matprint ("Second (reference) array", 4, 5, memtestaref);
             loopfail = 1;
          }
       }
       memtestaref[i][j] = 4.0;
   }
   /* Just paranoia...  they should still be equal when we are done. */
   if ((problem = matisaeq (4, 5, memtestarray, memtestaref, FUDGE))) {
      if (problem == ERRfalse) problem = NoError;
      goterrorstat ("false negative at end of equality test", problem);
   }

   /* Does it notice "Near" in all positions? */
   loopfail = 0;
   for (i=0; i<4; i++) for (j=0; j<5; j++) {
       memtestaref[i][j] = 4.0 + (FUDGE / 2.0);
       if ((problem = matisaeq (4, 5, memtestarray, memtestaref, FUDGE))) {
          if (loopfail) {
             if (problem == ERRfalse) problem = NoError;
             goterrorstat ("false negative", problem);
             printf ("   Location %d, %d\n", i, j);
             matprint ("Array first array", 4, 5, memtestarray);
             matprint ("Second (reference) array", 4, 5, memtestaref); 
             loopfail = 1;
          }
       }
       memtestaref[i][j] = 4.0;
   }
   /* Just paranoia...  they should STILL be equal when we are done. */
   if ((problem = matisaeq (4, 5, memtestarray, memtestaref, FUDGE))) {
      if (problem == ERRfalse) problem = NoError;
      goterrorstat ("false negative at end", problem);
   }

   endtest();
   

   endsection (); /* Equality tests */

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

   finalizetests();
   return progerrors;
}

Go to ...


This page is http://www.cc.utah.edu/~nahaj/cave/survey/code/c/matdebug-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