vec-test.c:


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

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

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

#include "vecdebug.h"
  /* Standard vector debugging routines. */

#include "reportframe.h"
  /* Standard testing frame */


#define SIZE 8

/* ========================== MAIN ========================================== */

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

  int i; /* Loop index */

  double test1[SIZE], test2[SIZE], reference[SIZE];
  double                           ref2[SIZE];
  double test3[SIZE];

  double dotresult;


  /* We do tests on parts of vectors so that we can catch the most
   * common fence post errors if the programs overstep their bounds
   */

  inittests("vec");


/* ------------------------------ test veczero ------------------------------ */

  newtest("veczero");

  for (i=0; i<6; i++) {
      test1[i] = 1.0;  /* Fill with an obvious non-zero. */
      test2[i] = (i==0 || i==6) ? 1.0 : 0.0;
  }
  if (ERRsize != (problem = veczero (0, test1)))
     goterrorstat ("bad size accepted?", problem);
  if (ERRnil != (problem = veczero (SIZE-2, 0)) )
     goterrorstat ("nil pointer accepted?", problem);
  if ((problem = veczero (4, &test1[1])))
     goterrorstat ("valid call failed?", problem);

  if ((problem == veciseq (6, test1, test2))) {
     goterror ("incorrect fillin?");
     vecprint ("Result vector", SIZE, test1);
     vecprint ("Should be",     SIZE, test2);
  }

  endtest();

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

  newsection ("Data Movement");

/* --------------------------- test veccpy ---------------------------------- */

  newtest("veccpy");

  if (ERRsize != (problem = veccpy (0, test1, test2)) )
     goterrorstat ("bad size accepted?", problem);
  if (ERRnil != (problem = veccpy (SIZE, 0, test2))
    ||ERRnil != (problem = veccpy (4, test1, 0)) )
     goterrorstat ("nil pointer accepted?", problem);

  for (i=1;i<SIZE-1;i++) {
     test1 [i] = 32.0;
     test2 [i] = (float) i; reference[i] = (float) i;
  }
  reference[0] = 16.0; reference [SIZE-1] = 16.0;
  test1    [0] = 16.0; test1     [SIZE-1] = 16.0;

  if ((problem = veccpy (SIZE-2,  &test1[1], &test2[1])))
     goterrorstat ("valid call rejected?", problem);

  if ((problem = veciseq (SIZE, test1, reference) )) {
     if (problem == ERRfalse) problem = NoError;
     goterrorstat ("wrong result", problem);
     vecprint ("Was      :", SIZE, test1);
     vecprint ("Should be:", SIZE, reference);
  }

  endtest();

/* -----------------------  test vector swap -------------------------------- */

/* swap two vectors test. */
  newtest ("vecswp");

  if (ERRsize != (problem = vecswp (0, test1, test2)))
     goterrorstat ("bad size accepted?", problem);
  if (ERRnil != (problem = vecswp (SIZE,     0, test2))
    ||ERRnil != (problem = vecswp (   4, test1,     0)) )
     goterrorstat ("nil pointer accepted?", problem);

  for (i=1;i<SIZE-1;i++) {
     test1 [i] = (float)(i+10); ref2      [i] = (float) (i+10);
     test2 [i] = (float) i;     reference [i] = (float) i;
  }
  test1     [0] = 32.0; test1     [SIZE-1] = 32.0;
  reference [0] = 32.0; reference [SIZE-1] = 32.0;
  test2     [0] = 16.0; test2     [SIZE-1] = 16.0;
  ref2      [0] = 16.0; ref2      [SIZE-1] = 16.0;

  if ((problem = vecswp (SIZE-2,  &test1[1], &test2[1])))
     goterrorstat ("valid call rejected?", problem);

  if ((problem = veciseq (SIZE, test1, reference) )) {
     if (problem == ERRfalse) problem = NoError;
     goterrorstat ("wrong first vector", problem);
     vecprint ("Was      :", SIZE, test1);
     vecprint ("Should be:", SIZE, reference);
  }
  if ((problem = veciseq (SIZE, test2, ref2) )) {
     if (problem == ERRfalse) problem = NoError;
     goterrorstat ("wrong second vector", problem);
     vecprint ("Was      :", SIZE, test2);
     vecprint ("Should be:", SIZE, ref2);
  }

  endtest();
  endsection();

/* ===================== End of copy tests ================================== */

  newsection ("Vector scaling");
  newtest ("vecscl");

  if (ERRsize != (problem = vecscl (0, test1, test2,  4.0)))
     goterrorstat ("bad size accepted?", problem);
  if (ERRnil != (problem = vecscl (SIZE,     0, test2, 4.0))
   || ERRnil != (problem = vecscl (SIZE, test1,     0, 4.0)) )
     goterrorstat ("nil pointer accepted?", problem);
  if (ERRmeaning != (problem = vecscl (SIZE, test1, test2,  0.0))) 
     goterrorstat ("meaningless scale accepted?", problem);

  for (i=1;i<SIZE-1;i++) {
     test1     [i] = (float) i; 
     test2     [i] = 99.0;
     reference [i] = (float) (i*2);
  }
  test1    [0] = 32.0; test1     [SIZE-1] = 32.0;
  test2    [0] = 32.0; test2     [SIZE-1] = 32.0;
  reference[0] = 32.0; reference [SIZE-1] = 32.0;

  if ((problem = vecscl (SIZE-2,  &test2[1], &test1[1], 2.0)))
     goterrorstat ("valid call rejected?", problem);

  if ((problem = veciseq (SIZE, test2, reference) )) {
     if (problem == ERRfalse) problem = NoError;
     goterrorstat ("wrong first vector", problem);
     vecprint ("Was      :", SIZE, test1);
     vecprint ("Should be:", SIZE, reference);
  }

  endtest();
  endsection();

/* ===================== Vector addition tests ============================== */

  newsection ("Addition");

/* ----------------------- Straight add ------------------------------- */
  newtest ("vecadd");

  if (ERRsize != (problem = vecadd (0, test1, test2, test3)))
     goterrorstat ("bad size accepted?", problem);
  if (   ERRnil != (problem = vecadd (SIZE, 0, test2, test3))
      || ERRnil != (problem = vecadd (SIZE, test1, 0, test3))
      || ERRnil != (problem = vecadd (SIZE, test1, test2, 0)))
     goterrorstat ("nil pointer accepted?", problem);

  for (i=1;i<SIZE-1;i++) {
     test1     [i] = (float) i;
     test2     [i] = (float) i*i;
     test3     [i] = -1.0;
     reference [i] = (float) (i+i*i);
  }
  test1    [0] = 16.0; test1     [SIZE-1] = 16.0;
  test2    [0] = 32.0; test2     [SIZE-1] = 32.0;
  test3    [0] = 64.0; test3     [SIZE-1] = 64.0;
  reference[0] = 64.0; reference [SIZE-1] = 64.0;

  if ((problem = vecadd (SIZE-2,  &test3[1], &test1[1], &test2[1])))
     goterrorstat ("valid call rejected?", problem);

  if ((problem = veciseq (SIZE, test3, reference) )) {
     if (problem == ERRfalse) problem = NoError;
     goterrorstat ("wrong result", problem);
     vecprint ("First:",     SIZE, test1);
     vecprint ("Second:", SIZE, test2);
     vecprint ("Sum:", SIZE, test3);
     vecprint ("Should have been:", SIZE, reference);
  }

  endtest();

/* ----------------------- Straight add ------------------------------- */
  newtest ("vecsub");

  if (ERRsize != (problem = vecsub (0, test1, test2, test3)))
     goterrorstat ("bad size accepted?", problem);
  if (   ERRnil != (problem = vecsub (SIZE,     0, test2, test3))
      || ERRnil != (problem = vecsub (SIZE, test1,     0, test3))
      || ERRnil != (problem = vecsub (SIZE, test1, test2,     0)))
     goterrorstat ("nil pointer accepted?", problem);

  for (i=1;i<SIZE-1;i++) {
     test1     [i] = (float) i*i;
     test2     [i] = (float) i;
     test3     [i] = -1.0;
     reference [i] = (float) (i*i-i);
  }
  test1    [0] = 16.0; test1     [SIZE-1] = 16.0;
  test2    [0] = 32.0; test2     [SIZE-1] = 32.0;
  test3    [0] = 64.0; test3     [SIZE-1] = 64.0;
  reference[0] = 64.0; reference [SIZE-1] = 64.0;

  if ((problem = vecsub (SIZE-2,  &test3[1], &test1[1], &test2[1])))
     goterrorstat ("valid call rejected?", problem);

  if ((problem = veciseq (SIZE, test3, reference) )) {
     if (problem == ERRfalse) problem = NoError;
     goterrorstat ("wrong result", problem);
     vecprint ("First:",     SIZE, test1);
     vecprint ("Second:", SIZE, test2);
     vecprint ("difference:", SIZE, test3);
     vecprint ("Should have been:", SIZE, reference);
  }

  endtest();


/* ----------------- Scaled addition ---------------------------- */

  newtest("vecadds");
  if (ERRsize != (vecadds (0, test1, test2, test3, 4.0)))
     goterrorstat ("bad size accepted?", problem);
  if (   ERRnil != (vecadds (SIZE, 0, test2, test1, 4.0))
      || ERRnil != (vecadds (SIZE, test3, 0, test1, 4.0))
      || ERRnil != (vecadds (SIZE, test3, test2, 0, 4.0)) )
     goterrorstat ("nil pointer accepted?", problem);
  if (ERRmeaning != (problem = vecadds (SIZE, test3, test2, test1, 0.0)) )
     goterrorstat ("meaningless scale accepted?", problem);

  for (i=1;i<SIZE-1;i++) {
     test1     [i] = (float) i;
     test2     [i] = (float) i*i;
     test3     [i] = -1.0;
     reference [i] = (float) (4*i+i*i);
  }
  test1    [0] = 16.0; test1     [SIZE-1] = 16.0;
  test2    [0] = 32.0; test2     [SIZE-1] = 32.0;
  test3    [0] = 64.0; test3     [SIZE-1] = 64.0;
  reference[0] = 64.0; reference [SIZE-1] = 64.0;

  if ((problem = vecadds (SIZE-2,  &test3[1], &test2[1], &test1[1], 4.0)))
     goterrorstat ("valid call rejected?", problem);

  if ((problem = veciseq (SIZE, test3, reference) )) {
     if (problem == ERRfalse) problem = NoError;
     goterrorstat ("wrong result", problem);
     vecprint ("First:",     SIZE, test1);
     vecprint ("Second:", SIZE, test2);
     vecprint ("First * scale + Second:", SIZE, test3);
     vecprint ("Should have been:", SIZE, reference);
  }

  endtest();
  endsection();

/* ===================== Multiply tests. ==================================== */

  newsection ("product");
  newtest ("vecdot");

  if (ERRsize != (problem = vecdot (0, &dotresult, test1, test2)))
     goterrorstat ("bad size accepted?", problem);
  if (    ERRnil != (problem = vecdot (3,          0, test1, test2))
       || ERRnil != (problem = vecdot (3, &dotresult,     0, test2))
       || ERRnil != (vecdot (3, &dotresult, test1,     0)) )
     goterrorstat ("nil pointer accepted?", problem);

  for (i=1;i<4;i++) {
     test1[i] = (float) i;
     test2[i] = (float) (i+9);
  }
  test1[0] = 99.0;  test1[4] = 999.0;
  test2[0] = 99.0;  test2[4] = 999.0;

  if ((problem = vecdot (3, &dotresult, &test1[1], &test2[1])))
     goterrorstat ("valid call rejected?", problem);

  if (dotresult != 68.0) {
     goterror ("wrong result");
     printf ("Result was %f, should have been 68\n", dotresult);
     vecprint ("First was:  ", 3, &test1[1]);
     vecprint ("second was: ", 3, &test2[1]);
  }

  endtest();
  endsection();

/* ===================== End of all  tests ================================== */


finalizetests();
return progerrors;
}

Go to ...


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