test/IntSerialDense/cxx_main.cpp

Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //               Epetra: Linear Algebra Services Package 
00005 //                 Copyright (2001) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ************************************************************************
00027 //@HEADER
00028 
00029 // Epetra_IntSerialDense Test routine
00030 
00031 #include "Epetra_IntSerialDenseMatrix.h"
00032 #include "Epetra_IntSerialDenseVector.h"
00033 #include "../epetra_test_err.h"
00034 #include "Epetra_ConfigDefs.h"
00035 #include "Epetra_DataAccess.h"
00036 #include "Epetra_Version.h"
00037 #ifdef EPETRA_MPI
00038 #include "Epetra_MpiComm.h"
00039 #include <mpi.h>
00040 #else
00041 #include "Epetra_SerialComm.h"
00042 #endif
00043 
00044 // matrix-testing prototypes
00045 int matrixCoverage(bool verbose, bool debug);
00046 int matrixCtr(bool verbose, bool debug);
00047 int matrixCpyCtr(bool verbose, bool debug);
00048 int matrixAssignment(bool verbose, bool debug);
00049 int matrixExceptions(bool verbose, bool debug);
00050 // vector-testing prototypes
00051 int vectorCoverage(bool verbose, bool debug);
00052 int vectorCtr(bool verbose, bool debug);
00053 int vectorCpyCtr(bool verbose, bool debug);
00054 int vectorAssignment(bool verbose, bool debug);
00055 int vectorExceptions(bool verbose, bool debug);
00056 // helper function prototypes
00057 bool identicalSignatures(Epetra_IntSerialDenseMatrix& a, Epetra_IntSerialDenseMatrix& b, bool testLDA = true);
00058 bool seperateData(Epetra_IntSerialDenseMatrix& a, Epetra_IntSerialDenseMatrix& b);
00059 int* getRandArray(int length);
00060 int randomInt();
00061 void printArray(int* array, int length);
00062 void printMat(const char* name, Epetra_IntSerialDenseMatrix& matrix);
00063 void printHeading(const char* heading);
00064 
00065 int main(int argc, char *argv[]) {
00066 //============================
00067 // Check for verbose or debug
00068 
00069 bool verbose = false;
00070 bool debug = false;
00071 if(argc > 1) {
00072     if((argv[1][0] == '-') && (argv[1][1] == 'v'))
00073         verbose = true;
00074     if((argv[1][0] == '-') && (argv[1][1] == 'd')) {
00075         debug = true;
00076         verbose = true;
00077     }
00078 }
00079 
00080 //============================
00081 // Initialize Comm
00082 
00083 #ifdef EPETRA_MPI
00084   // Initialize MPI
00085   MPI_Init(&argc,&argv);
00086   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00087 #else
00088   Epetra_SerialComm Comm;
00089 #endif
00090 
00091   if (verbose && Comm.MyPID()==0)
00092     cout << Epetra_Version() << endl << endl;
00093   
00094 //============================
00095 // other initial setup
00096 
00097   if (!verbose) Comm.SetTracebackMode(0); // This should shut down any error traceback reporting
00098 
00099   int ierr = 0;
00100   int returnierr = 0;
00101 
00102 //============================
00103 // Test vector
00104 
00105   ierr = vectorExceptions(verbose, debug);
00106   EPETRA_TEST_ERR(ierr, returnierr);
00107 
00108   ierr = vectorCtr(verbose, debug);
00109   EPETRA_TEST_ERR(ierr, returnierr);
00110   
00111   ierr = vectorCpyCtr(verbose, debug);
00112   EPETRA_TEST_ERR(ierr, returnierr);
00113   
00114   ierr = vectorAssignment(verbose, debug);
00115   EPETRA_TEST_ERR(ierr, returnierr);
00116 
00117   ierr = vectorCoverage(verbose, debug);
00118   EPETRA_TEST_ERR(ierr, returnierr);
00119 
00120 //============================
00121 // Test matrix
00122 
00123   ierr = matrixExceptions(verbose, debug);
00124   EPETRA_TEST_ERR(ierr, returnierr);
00125 
00126   ierr = matrixCtr(verbose, debug);
00127   EPETRA_TEST_ERR(ierr, returnierr);
00128 
00129   ierr = matrixCpyCtr(verbose, debug);
00130   EPETRA_TEST_ERR(ierr, returnierr);
00131 
00132   ierr = matrixAssignment(verbose, debug);
00133   EPETRA_TEST_ERR(ierr, returnierr);
00134 
00135   ierr = matrixCoverage(verbose, debug);
00136   EPETRA_TEST_ERR(ierr, returnierr);
00137 
00138 //============================
00139 // end of program cleanup
00140 
00141 #ifdef EPETRA_MPI
00142   MPI_Finalize();
00143 #endif
00144   
00145   return(returnierr);
00146 }
00147 
00148 //=========================================================================
00149 //=========================================================================
00150 // Matrix-testing functions
00151 //=========================================================================
00152 //=========================================================================
00153 // call functions we don't have tests for, for sake of code coverage.
00154 // (the functions are called, but their output isn't checked for correctness).
00155 int matrixCoverage(bool verbose, bool debug) {
00156   if(verbose) printHeading("Testing other matrix functions");
00157 
00158   int* rand1 = getRandArray(9);
00159   if(debug) printArray(rand1, 9);
00160   Epetra_IntSerialDenseMatrix m1(Copy, rand1, 3, 3, 3);
00161   if(debug) printMat("m1",m1);
00162   delete[] rand1;
00163 
00164   if(verbose) cout << "calling one norm" << endl;
00165   int onenorm = m1.OneNorm();
00166   if(debug) cout << "m1.OneNorm() = " << onenorm << endl;
00167 
00168   if(verbose) cout << "calling infinity norm" << endl;
00169   int infnorm = m1.InfNorm();
00170   if(debug) cout << "m1.InfNorm() = " << infnorm << endl;
00171 
00172   if(verbose) cout << "calling random" << endl;
00173   Epetra_IntSerialDenseMatrix m2(6,6);
00174   if(debug) printMat("m2 (before)",m2);
00175   m2.Random();
00176   if(debug) printMat("m2 (after)",m2);
00177 
00178   if(verbose) cout << "Checked OK." << endl;
00179 
00180   return(0);
00181 }
00182 
00183 //=========================================================================
00184 // test matrix default constructor, user constructor (copy & view),
00185 // () [] operators (read & write), shape (larger), reshape (smaller)
00186 int matrixCtr(bool verbose, bool debug) {
00187   const int m1rows = 5;
00188   const int m1cols = 4;
00189   const int m1arows = 4;
00190   const int m1acols = 6;
00191   const int m2rows = 2;
00192   const int m2cols = 7;
00193   const int m3rows = 8;
00194   const int m3cols = 3;
00195   const int m3Rrows = 5; // should be smaller than m3rows
00196   const int m3Rcols = 2; // should be smaller than m3cols
00197 
00198   int ierr = 0;
00199   int returnierr = 0;
00200   if(verbose) printHeading("Testing matrix constructors");
00201 
00202   if(verbose) cout << "default constructor" << endl;
00203   Epetra_IntSerialDenseMatrix m1;
00204   EPETRA_TEST_ERR(!(m1.CV() == Copy), ierr);
00205   if(verbose) cout << "shaping" << endl;
00206   m1.Shape(m1rows, m1cols);
00207   EPETRA_TEST_ERR(!(m1.M() == m1rows), ierr);
00208   for(int i = 0; i < m1rows; i++)
00209     for(int j = 0; j < m1cols; j++)
00210       EPETRA_TEST_ERR(!(m1(i,j) == 0), ierr);
00211   if(debug) printMat("m1",m1);
00212   returnierr += ierr;
00213   if(ierr == 0) 
00214      if(verbose) cout << "Checked OK." << endl;
00215   ierr = 0;
00216   if(verbose) cout << "\nmanually setting values" << endl;
00217   int* m1rand = getRandArray(m1rows * m1cols);
00218   for(int i = 0; i < m1rows; i++)
00219     for(int j = 0; j < m1cols; j++)
00220       m1(i,j) = m1rand[i*m1cols + j];
00221   for(int i = 0; i < m1rows; i++)
00222     for(int j = 0; j < m1cols; j++)
00223     EPETRA_TEST_ERR(!(m1[j][i] == m1rand[i*m1cols + j]), ierr);
00224   if(debug) {
00225     printArray(m1rand, m1rows * m1cols);
00226     printMat("m1",m1);
00227   }
00228   delete[] m1rand;
00229   returnierr += ierr;
00230   if(ierr == 0) 
00231      if(verbose) cout << "Checked OK." << endl;
00232   ierr = 0;
00233   
00234   if(verbose) cout << "\nshaped constructor" << endl;
00235   Epetra_IntSerialDenseMatrix m1a(m1arows, m1acols);
00236   EPETRA_TEST_ERR(!(m1a.M() == m1arows), ierr);
00237   EPETRA_TEST_ERR(!(m1a.N() == m1acols), ierr);
00238   EPETRA_TEST_ERR(!(m1a.LDA() == m1arows), ierr);
00239   EPETRA_TEST_ERR(!(m1a.CV() == Copy),ierr);
00240   if(debug) printMat("m1a", m1a);
00241   returnierr += ierr;
00242   if(ierr == 0) 
00243      if(verbose) cout << "Checked OK." << endl;
00244   ierr = 0;
00245   
00246   if(verbose) cout << "\nuser data constructor (view)" << endl;
00247   int* m2rand = getRandArray(m2rows * m2cols);
00248   if(debug) printArray(m2rand, m2rows * m2cols);
00249   Epetra_IntSerialDenseMatrix m2(View, m2rand, m2rows, m2rows, m2cols);
00250   EPETRA_TEST_ERR(!(m2.CV() == View), ierr);
00251   EPETRA_TEST_ERR(!(m2.M() == m2rows), ierr);
00252   EPETRA_TEST_ERR(!(m2.N() == m2cols), ierr);
00253   EPETRA_TEST_ERR(!(m2.LDA() == m2rows), ierr);
00254   EPETRA_TEST_ERR(!(m2.A() == m2rand), ierr);
00255   if(debug) printMat("m2",m2);
00256   returnierr += ierr;
00257   if(ierr == 0) 
00258      if(verbose) cout << "Checked OK." << endl;
00259   ierr = 0;
00260 
00261   if(verbose) cout << "\nchanging value, checking for correct behavior" << endl;
00262   int* m2randcopy = new int[m2rows * m2cols]; // backup of original values
00263   for(int i = 0; i < m2rows * m2cols; i++)
00264     m2randcopy[i] = m2rand[i];
00265   m2(0,4) = m2(0,4) + 1; // magic numbers for which element to change
00266   m2randcopy[4 * m2rows] = m2randcopy[4 * m2rows] + 1;
00267   for(int i = 0; i < m2rows * m2cols; i++)
00268     EPETRA_TEST_ERR(!(m2rand[i] == m2randcopy[i]), ierr); // m2rand should have updated correctly
00269   if(debug) {
00270     printArray(m2rand, m2rows * m2cols);
00271     printMat("m2",m2);
00272   }
00273   delete[] m2rand;
00274   delete[] m2randcopy;
00275   returnierr += ierr;
00276   if(ierr == 0) 
00277      if(verbose) cout << "Checked OK." << endl;
00278   ierr = 0;
00279 
00280   if(verbose) cout << "\nuser data constructor (copy)" << endl;
00281   int* m3rand = getRandArray(m3rows * m3cols);
00282   if(debug) printArray(m3rand, m3rows * m3cols);
00283   int* m3randcopy = new int[m3rows * m3cols];
00284   for(int i = 0; i < m3rows * m3cols; i++)
00285     m3randcopy[i] = m3rand[i];
00286   Epetra_IntSerialDenseMatrix m3(Copy, m3rand, m3rows, m3rows, m3cols);
00287   if(debug) printMat("m3",m3);
00288   if(verbose) cout << "checking to see if data initialized correctly" << endl;
00289   EPETRA_TEST_ERR(!(m3.CV() == Copy), ierr);
00290   EPETRA_TEST_ERR(!(m3.M() == m3rows), ierr);
00291   EPETRA_TEST_ERR(!(m3.N() == m3cols), ierr);
00292   EPETRA_TEST_ERR(!(m3.LDA() == m3rows), ierr);
00293   EPETRA_TEST_ERR(!(m3.A() != m3rand), ierr); // should not be a view
00294   for(int i = 0; i < m3rows; i++)
00295     for(int j = 0; j < m3cols; j++)
00296       EPETRA_TEST_ERR(!(m3[j][i] == m3rand[j * m3rows + i]), ierr); // data should be identical to user array
00297   returnierr += ierr;
00298   if(ierr == 0) 
00299      if(verbose) cout << "Checked OK." << endl;
00300   ierr = 0;
00301 
00302   if(verbose) cout << "\nmodifying entry" << endl;
00303   m3[1][5] = m3[1][5] + 3; // magic numbers for which element to change
00304   for(int i = 0; i < m3rows * m3cols; i++)
00305     EPETRA_TEST_ERR(!(m3rand[i] == m3randcopy[i]), ierr); // user array should be unchanged
00306   m3rand[13] = m3rand[13] + 3; // same magic modification performed to user's array
00307   for(int i = 0; i < m3rows; i++)
00308     for(int j = 0; j < m3cols; j++)
00309       EPETRA_TEST_ERR(!(m3[j][i] == m3rand[j * m3rows + i]), ierr); // should equal user array with same modification performed
00310   if(debug) {
00311     printArray(m3rand, m3rows * m3cols);
00312     printMat("m3",m3);
00313   }
00314   returnierr += ierr;
00315   if(ierr == 0) 
00316      if(verbose) cout << "Checked OK." << endl;
00317   ierr = 0;
00318   
00319   if(verbose) cout << "\nreshaping" << endl;
00320   m3.Reshape(m3Rrows, m3Rcols);
00321   EPETRA_TEST_ERR(!(m3.M() == m3Rrows), ierr);
00322   EPETRA_TEST_ERR(!(m3.N() == m3Rcols), ierr);
00323   EPETRA_TEST_ERR(!(m3.LDA() == m3Rrows), ierr);
00324   for(int i = 0; i < m3Rrows; i++)
00325     for(int j = 0; j < m3Rcols; j++)
00326       EPETRA_TEST_ERR(!(m3[j][i] == m3rand[j * m3rows + i]),ierr);
00327   if(debug) printMat("m3",m3);
00328 
00329   delete[] m3rand;
00330   delete[] m3randcopy;
00331   returnierr += ierr;
00332   if(ierr == 0) 
00333      if(verbose) cout << "Checked OK." << endl;
00334   ierr = 0;
00335   
00336   if(verbose) cout << "\nChecking pointer on zero-sized matrix" << endl;
00337   int* before = m3.A();
00338   if(verbose) cout << "Reshaping to (4,0)" << endl;
00339   if(debug) cout << "Before = " << before << endl;
00340   EPETRA_TEST_ERR(!(before != 0), ierr);
00341   m3.Reshape(4,0);
00342   int* after = m3.A();
00343   EPETRA_TEST_ERR(!(after == 0), ierr);
00344   if(debug) cout << "After = " << after << endl;
00345   m3.Shape(3,3);
00346   before = m3.A();
00347   if(verbose) cout << "Shaping to (0,3)" << endl;
00348   if(debug) cout << "Before = " << before << endl;
00349   EPETRA_TEST_ERR(!(before != 0), ierr);
00350   m3.Shape(0,3);
00351   after = m3.A();
00352   EPETRA_TEST_ERR(!(after == 0), ierr);
00353   if(debug) cout << "After = " << after << endl;
00354   returnierr += ierr;
00355   if(ierr == 0) 
00356      if(verbose) cout << "Checked OK." << endl;
00357   ierr = 0;
00358 
00359   return(returnierr);
00360 }
00361 
00362 //=========================================================================
00363 // test matrix copy constructor (copy & view)
00364 int matrixCpyCtr(bool verbose, bool debug) {
00365   const int m1rows = 5;
00366   const int m1cols = 4;
00367   const int m2rows = 2;
00368   const int m2cols = 6;
00369 
00370   int ierr = 0;
00371   int returnierr = 0;
00372   if(verbose) printHeading("Testing matrix copy constructors");
00373 
00374   if(verbose) cout << "checking copy constructor (view)" << endl;
00375   int* m1rand = getRandArray(m1rows * m1cols);
00376   if(debug) printArray(m1rand, m1rows * m1cols);
00377   Epetra_IntSerialDenseMatrix m1(View, m1rand, m1rows, m1rows, m1cols);
00378   if(debug) {
00379     cout << "original matrix:" << endl;
00380     printMat("m1",m1);
00381   }
00382   Epetra_IntSerialDenseMatrix m1clone(m1);
00383   if(debug) {
00384     cout << "clone matrix:" << endl;
00385     printMat("m1clone",m1clone);
00386   }
00387   if(verbose) cout << "making sure signatures match" << endl;
00388   EPETRA_TEST_ERR(!identicalSignatures(m1, m1clone), ierr);
00389   delete[] m1rand;
00390   returnierr += ierr;
00391   if(ierr == 0)
00392     if(verbose) cout << "Checked OK." << endl;
00393   ierr = 0;
00394   
00395   if(verbose) cout << "\nchecking copy constructor (copy)" << endl;
00396   int* m2rand = getRandArray(m2rows * m2cols);
00397   if(debug) printArray(m2rand, m2rows * m2cols);
00398   Epetra_IntSerialDenseMatrix m2(Copy, m2rand, m2rows, m2rows, m2cols);
00399   if(debug) {
00400     cout << "original matrix:" << endl;
00401     printMat("m2",m2);
00402   }
00403   Epetra_IntSerialDenseMatrix m2clone(m2);
00404   if(debug) {
00405     cout << "clone matrix:" << endl;
00406     printMat("m2clone",m2clone);
00407   }
00408   if(verbose) cout << "checking that signatures match" << endl;
00409   EPETRA_TEST_ERR(!identicalSignatures(m2, m2clone), ierr);
00410   returnierr += ierr;
00411   if(ierr == 0)
00412     if(verbose) cout << "Checked OK." << endl;
00413   ierr = 0;
00414 
00415   if(verbose) cout << "\nmodifying entry in m2, m2clone should be unchanged" << endl;
00416   EPETRA_TEST_ERR(!seperateData(m2, m2clone), ierr);
00417   if(debug) {
00418     printArray(m2rand, m2rows * m2cols);
00419     cout << "orig:" << endl;
00420     printMat("m2",m2);
00421     cout << "clone:" << endl;
00422     printMat("m2clone",m2clone);
00423   }
00424   delete[] m2rand;
00425   returnierr += ierr;
00426   if(ierr == 0)
00427     if(verbose) cout << "Checked OK." << endl;
00428   ierr = 0;
00429   
00430   return(returnierr);
00431 }
00432 
00433 //=========================================================================
00434 // test matrix error-reporting
00435 int matrixExceptions(bool verbose, bool debug) {
00436   int returnierr = 0;
00437   int ierr = 0;
00438   bool caught = false;
00439   Epetra_IntSerialDenseMatrix* matrix;
00440 
00441   if(verbose) printHeading("Testing matrix error-reporting.\nExpect error messages if EPETRA_NO_ERROR_REPORTS is not defined.");
00442   
00443   // invalid dimension to sized ctr (2 cases)
00444   try {
00445     caught = false;
00446     if(verbose) cout << "Checking Epetra_IntSerialDenseMatrix(-1, 6) - invalid rows";
00447     matrix = new Epetra_IntSerialDenseMatrix(-1, 6);
00448   }
00449   catch(int error) {
00450     caught = true;
00451     EPETRA_TEST_ERR(error != -1, returnierr);
00452     if(error == -1)
00453       if(verbose) cout << "Checked OK." << endl;
00454   }
00455   EPETRA_TEST_ERR(!caught, returnierr);
00456   try {
00457     caught = false;
00458     if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(3, -5) - invalid cols";
00459     matrix = new Epetra_IntSerialDenseMatrix(3, -5);
00460   }
00461   catch(int error) {
00462     caught = true;
00463     EPETRA_TEST_ERR(error != -1, returnierr);
00464     if(error == -1)
00465       if(verbose) cout << "Checked OK." << endl;
00466   }
00467   EPETRA_TEST_ERR(!caught, returnierr);
00468 
00469   // invalid dimension to user-data ctr (3 cases)
00470      int* rand2 = getRandArray(2);
00471   try {
00472     caught = false;
00473     if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(Copy, int*, -1, 2, 2) - invalid lda";
00474     matrix = new Epetra_IntSerialDenseMatrix(Copy, rand2, -1, 2, 2);
00475   }
00476   catch(int error) {
00477     caught = true;
00478     EPETRA_TEST_ERR(error != -1, returnierr);
00479     if(error == -1)
00480       if(verbose) cout << "Checked OK." << endl;
00481   }
00482   EPETRA_TEST_ERR(!caught, returnierr);
00483   try {
00484     caught = false;
00485     if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(Copy, int*, 3, -2, 3) - invalid rows";
00486     matrix = new Epetra_IntSerialDenseMatrix(Copy, rand2, 3, -2, 3);
00487   }
00488   catch(int error) {
00489     caught = true;
00490     EPETRA_TEST_ERR(error != -1, returnierr);
00491     if(error == -1)
00492       if(verbose) cout << "Checked OK." << endl;
00493   }
00494   EPETRA_TEST_ERR(!caught, returnierr);
00495   try {
00496     caught = false;
00497     if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(Copy, int*, 4, 4, -4) - invalid cols";
00498     matrix = new Epetra_IntSerialDenseMatrix(Copy, rand2, -4, 4, -4);
00499   }
00500   catch(int error) {
00501     caught = true;
00502     EPETRA_TEST_ERR(error != -1, returnierr);
00503     if(error == -1)
00504       if(verbose) cout << "Checked OK." << endl;
00505   }
00506   EPETRA_TEST_ERR(!caught, returnierr);
00507      delete [] rand2;
00508   
00509   // null pointer to user-data ctr
00510   try {
00511     caught = false;
00512     if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(Copy, 0, 5, 5, 5) - null pointer";
00513     matrix = new Epetra_IntSerialDenseMatrix(Copy, 0, 5, 5, 5);
00514   }
00515   catch(int error) {
00516     caught = true;
00517     EPETRA_TEST_ERR(error != -3, returnierr);
00518     if(error == -3)
00519       if(verbose) cout << "Checked OK." << endl;
00520   }
00521   EPETRA_TEST_ERR(!caught, returnierr);
00522 
00523   // invalid parameter to shape (2 cases)
00524   Epetra_IntSerialDenseMatrix m1;
00525   if(verbose) cout << "\nChecking Shape(-2, 2) - invalid rows" << endl;
00526   ierr = m1.Shape(-2, 2);
00527   EPETRA_TEST_ERR(!(ierr == -1), returnierr);
00528   if(ierr == -1)
00529     if(verbose) cout << "Checked OK." << endl;
00530   if(verbose) cout << "\nChecking Shape(3, -2) - invalid cols" << endl;
00531   ierr = m1.Shape(3, -2);
00532   EPETRA_TEST_ERR(!(ierr == -1), returnierr);
00533   if(ierr == -1)
00534     if(verbose) cout << "Checked OK." << endl;
00535   
00536   // invalid parameter to reshape (2 cases)
00537   m1.Shape(5, 5);
00538   if(verbose) cout << "\nChecking Reshape(-4, 3) - invalid rows" << endl;
00539   ierr = m1.Reshape(-4, 3);
00540   EPETRA_TEST_ERR(!(ierr == -1), returnierr);
00541   if(ierr == -1)
00542     if(verbose) cout << "Checked OK." << endl;
00543   if(verbose) cout << "\nChecking Reshape(4, -3) - invalid cols" << endl;
00544   ierr = m1.Reshape(4, -3);
00545   EPETRA_TEST_ERR(!(ierr == -1), returnierr);
00546   if(ierr == -1)
00547     if(verbose) cout << "Checked OK." << endl;
00548 
00549 #ifdef HAVE_EPETRA_ARRAY_BOUNDS_CHECK // only test op() and op[] exceptions if macro is defined.
00550   // out of range index to op() & op[] (6 cases)
00551   int* rand16 = getRandArray(16);
00552   Epetra_IntSerialDenseMatrix m2(View, rand16, 4, 4, 4);
00553 
00554   // op() too high
00555   try {
00556     caught = false;
00557     if(verbose) cout << "\nChecking operator () - row index too high";
00558     ierr = m2(5, 2);
00559   }
00560   catch(int error) {
00561     caught = true;
00562     EPETRA_TEST_ERR(error != -1, returnierr);
00563     if(error == -1)
00564       if(verbose) cout << "Checked OK." << endl;
00565   }
00566   EPETRA_TEST_ERR(!caught, returnierr);
00567   try {
00568     caught = false;
00569     if(verbose) cout << "\nChecking operator () - col index too high";
00570     ierr = m2(3, 4);
00571   }
00572   catch(int error) {
00573     caught = true;
00574     EPETRA_TEST_ERR(error != -2, returnierr);
00575     if(error == -2)
00576       if(verbose) cout << "Checked OK." << endl;
00577   }
00578   EPETRA_TEST_ERR(!caught, returnierr);
00579 
00580   // op() too low
00581   try {
00582     caught = false;
00583     if(verbose) cout << "\nChecking operator () - row index too low";
00584     ierr = m2(-1, 0);
00585   }
00586   catch(int error) {
00587     caught = true;
00588     EPETRA_TEST_ERR(error != -1, returnierr);
00589     if(error == -1)
00590       if(verbose) cout << "Checked OK." << endl;
00591   }
00592   EPETRA_TEST_ERR(!caught, returnierr);
00593   try {
00594     caught = false;
00595     if(verbose) cout << "\nChecking operator () - col index too low";
00596     ierr = m2(0, -1);
00597   }
00598   catch(int error) {
00599     caught = true;
00600     EPETRA_TEST_ERR(error != -2, returnierr);
00601     if(error == -2)
00602       if(verbose) cout << "Checked OK." << endl;
00603   }
00604   EPETRA_TEST_ERR(!caught, returnierr);
00605 
00606   // op[] too high
00607   try { 
00608     caught = false;
00609     if(verbose) cout << "\nChecking operator [] - col index too high";
00610     ierr = m2[4][2];
00611   }
00612   catch(int error) {
00613     caught = true;
00614     EPETRA_TEST_ERR(error != -2, returnierr);
00615     if(error == -2)
00616       if(verbose) cout << "Checked OK." << endl;
00617   }
00618 
00619   // op[] too low
00620   try {
00621     caught = false;
00622     if(verbose) cout << "\nChecking operator [] - col index too low";
00623     ierr = m2[-1][0];
00624   }
00625   catch(int error) {
00626     caught = true;
00627     EPETRA_TEST_ERR(error != -2, returnierr);
00628     if(error == -2)
00629       if(verbose) cout << "Checked OK." << endl;
00630   }
00631   EPETRA_TEST_ERR(!caught, returnierr);
00632      delete [] rand16;
00633 #endif // end of HAVE_EPETRA_ARRAY_BOUNDS_CHECK conditional
00634   
00635   // ISDM = ISDV
00636   Epetra_IntSerialDenseMatrix m3;
00637   Epetra_IntSerialDenseVector v1;
00638   try {
00639     caught = false;
00640     if(verbose) cout << "\nChecking op = - assigning ISDV to ISDM";
00641     m3 = v1;
00642   }
00643   catch(int error) {
00644     caught = true;
00645     EPETRA_TEST_ERR(error != -5, returnierr);
00646     if(error == -5)
00647       if(verbose) cout << "Checked OK." << endl;
00648   }
00649   EPETRA_TEST_ERR(!caught, returnierr);
00650   
00651   return(returnierr);
00652 }
00653 
00654 //=========================================================================
00655 // test matrix operator= (copy & view)
00656 int matrixAssignment(bool verbose, bool debug) {
00657   int ierr = 0;
00658   int returnierr = 0;
00659   if(verbose) printHeading("Testing matrix operator=");
00660 
00661   // each section is in its own block so we can reuse variable names
00662   // lhs = left hand side, rhs = right hand side
00663   
00664   {
00665     // copy->copy (more space needed)
00666     // orig and dup should have same signature
00667     // modifying orig or dup should have no effect on the other
00668     if(verbose) cout << "Checking copy->copy (new alloc)" << endl;
00669     Epetra_IntSerialDenseMatrix lhs(2,2);
00670     int* rand1 = getRandArray(25);
00671     Epetra_IntSerialDenseMatrix rhs(Copy, rand1, 5, 5, 5);
00672     if(debug) {
00673       cout << "before assignment:" << endl;
00674       printMat("rhs",rhs);
00675       printMat("lhs",lhs);
00676     }
00677     lhs = rhs;
00678     if(debug) {
00679       cout << "after assignment:" << endl;
00680       printMat("rhs",rhs);
00681       printMat("lhs",lhs);
00682     }
00683     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
00684     EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
00685     delete[] rand1;
00686   }
00687   returnierr += ierr;
00688   if(ierr == 0)
00689     if(verbose) cout << "Checked OK." << endl;
00690   ierr = 0;
00691   {
00692     // copy->copy (have enough space)
00693     // orig and dup should have same signature
00694     // modifying orig or dup should have no effect on the other
00695     if(verbose) cout << "\nChecking copy->copy (no alloc)" << endl;
00696     int* rand1 = getRandArray(25);
00697     int* rand2 = getRandArray(20);
00698     Epetra_IntSerialDenseMatrix lhs(Copy, rand1, 5, 5, 5);
00699     Epetra_IntSerialDenseMatrix rhs(Copy, rand2, 4, 4, 5);
00700     int* origA = lhs.A();
00701     int origLDA = lhs.LDA();
00702     if(debug) {
00703       cout << "before assignment:" << endl;
00704       printMat("rhs",rhs);
00705       printMat("lhs",lhs);
00706     }
00707     lhs = rhs;
00708     if(debug) {
00709       cout << "after assignment:" << endl;
00710       printMat("rhs",rhs);
00711       printMat("lhs",lhs);
00712     }
00713     // in this case, instead of doing a "normal" LDA test in identSig,
00714     // we do our own. Since we had enough space already, A and LDA should
00715     // not have been changed by the assignment. (The extra parameter to
00716     // identicalSignatures tells it not to test LDA).
00717     EPETRA_TEST_ERR((lhs.A() != origA) || (lhs.LDA() != origLDA), ierr);
00718     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs,false), ierr);
00719     EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
00720      delete [] rand1;
00721      delete [] rand2;
00722   }
00723   returnierr += ierr;
00724   if(ierr == 0)
00725     if(verbose) cout << "Checked OK." << endl;
00726   ierr = 0;
00727   {
00728     // view->copy
00729     // orig and dup should have same signature
00730     // modifying orig or dup should have no effect on the other
00731     if(verbose) cout << "\nChecking view->copy" << endl;
00732     int* rand1 = getRandArray(25);
00733     int* rand2 = getRandArray(64);
00734     Epetra_IntSerialDenseMatrix lhs(View, rand1, 5, 5, 5);
00735     Epetra_IntSerialDenseMatrix rhs(Copy, rand2, 8, 8, 8);
00736     if(debug) {
00737       cout << "before assignment:" << endl;
00738       printMat("rhs",rhs);
00739       printMat("lhs",lhs);
00740     }
00741     lhs = rhs;
00742     if(debug) {
00743       cout << "after assignment:" << endl;
00744       printMat("rhs",rhs);
00745       printMat("lhs",lhs);
00746     }
00747     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
00748     EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
00749     delete[] rand1;
00750     delete[] rand2;
00751   }
00752   returnierr += ierr;
00753   if(ierr == 0)
00754     if(verbose) cout << "Checked OK." << endl;
00755   ierr = 0;
00756   {
00757     // copy->view
00758     // orig and dup should have same signature
00759     // modifying orig or dup should change the other
00760     if(verbose) cout << "\nChecking copy->view" << endl;
00761     int* rand1 = getRandArray(10);
00762     Epetra_IntSerialDenseMatrix lhs(4,4);
00763     Epetra_IntSerialDenseMatrix rhs(View, rand1, 2, 2, 5);
00764     if(debug) {
00765       cout << "before assignment:" << endl;
00766       printMat("rhs",rhs);
00767       printMat("lhs",lhs);
00768     }
00769     lhs = rhs;
00770     if(debug) {
00771       cout << "after assignment:" << endl;
00772       printMat("rhs",rhs);
00773       printMat("lhs",lhs);
00774     }
00775     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
00776     EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
00777     delete[] rand1;
00778   }
00779   returnierr += ierr;
00780   if(ierr == 0)
00781     if(verbose) cout << "Checked OK." << endl;
00782   ierr = 0; 
00783   {
00784     // view->view
00785     // orig and dup should have same signature
00786     // modifying orig or dup should change the other
00787     if(verbose) cout << "\nChecking view->view" << endl;
00788     int* rand1 = getRandArray(9);
00789     int* rand2 = getRandArray(18);
00790     Epetra_IntSerialDenseMatrix lhs(View, rand1, 3, 3, 3);
00791     Epetra_IntSerialDenseMatrix rhs(View, rand2, 3, 3, 6);
00792     if(debug) {
00793       cout << "before assignment:" << endl;
00794       printMat("rhs",rhs);
00795       printMat("lhs",lhs);
00796     }
00797     lhs = rhs;
00798     if(debug) {
00799       cout << "after assignment:" << endl;
00800       printMat("rhs",rhs);
00801       printMat("lhs",lhs);
00802     }
00803     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
00804     EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
00805     delete[] rand1;
00806     delete[] rand2;
00807   }
00808   returnierr += ierr;
00809   if(ierr == 0)
00810     if(verbose) cout << "Checked OK." << endl;
00811   ierr = 0;
00812   {
00813     // test MakeViewOf
00814     // orig and dup should have same signature except for CV_
00815     // modifying orig or dup should change the other
00816     if(verbose) cout << "\nChecking CrsGraph's usage of MakeViewOf" << endl;
00817     int* rand1 = getRandArray(10);
00818     int* rand2 = getRandArray(10);
00819     Epetra_IntSerialDenseMatrix lhs(Copy, rand1, 5, 5, 2);
00820     Epetra_IntSerialDenseMatrix rhs(Copy, rand2, 5, 5, 2);
00821     if(debug) {
00822       cout << "before assignment:" << endl;
00823       printMat("rhs",rhs);
00824       printMat("lhs",lhs);
00825     }
00826     lhs.MakeViewOf(rhs);
00827     if(debug) {
00828       cout << "after assignment:" << endl;
00829       printMat("rhs",rhs);
00830       printMat("lhs",lhs);
00831     }
00832     EPETRA_TEST_ERR(!(lhs.CV() == View), ierr);
00833     EPETRA_TEST_ERR(!(lhs.M() == rhs.M()), ierr);
00834     EPETRA_TEST_ERR(!(lhs.N() == rhs.N()), ierr);
00835     EPETRA_TEST_ERR(!(lhs.LDA() == rhs.LDA()), ierr);
00836     EPETRA_TEST_ERR(!(lhs.A() == rhs.A()), ierr);
00837     EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
00838     delete[] rand1;
00839     delete[] rand2;
00840   }
00841   returnierr += ierr;
00842   if(ierr == 0)
00843     if(verbose) cout << "Checked OK." << endl;
00844   ierr = 0;
00845 
00846   return(returnierr);
00847 }
00848 
00849 //=========================================================================
00850 //=========================================================================
00851 // Vector-testing functions
00852 //=========================================================================
00853 //=========================================================================
00854 // call functions we don't have tests for, for sake of code coverage.
00855 // (the functions are called, but their output isn't checked for correctness).
00856 int vectorCoverage(bool verbose, bool debug) {
00857   if(verbose) printHeading("Testing other vector functions");
00858 
00859   int* rand1 = getRandArray(9);
00860   if(debug) printArray(rand1, 9);
00861   Epetra_IntSerialDenseVector v1(Copy, rand1, 9);
00862   if(debug) printMat("v1",v1);
00863   delete[] rand1;
00864 
00865   if(verbose) cout << "calling one norm" << endl;
00866   int onenorm = v1.OneNorm();
00867   if(debug) cout << "v1.OneNorm() = " << onenorm << endl;
00868 
00869   if(verbose) cout << "calling infinity norm" << endl;
00870   int infnorm = v1.InfNorm();
00871   if(debug) cout << "v1.InfNorm() = " << infnorm << endl;
00872 
00873   if(verbose) cout << "calling random" << endl;
00874   Epetra_IntSerialDenseVector v2(12);
00875   if(debug) printMat("v2 (before)",v2);
00876   v2.Random();
00877   if(debug) printMat("v2 (after)",v2);
00878 
00879   if(verbose) cout << "Checked OK (I think)." << endl;
00880 
00881   return(0);
00882 }
00883 //=========================================================================
00884 // test vector default constructor, user constructor (copy & view),
00885 // () [] operators (read & write), size (larger), resize (smaller)
00886 int vectorCtr(bool verbose, bool debug) {
00887   const int v1size = 5;
00888   const int v1asize = 15;
00889   const int v2size = 10;
00890   const int v3size = 8;
00891   const int v3resize = 5; // should be smaller than v3size
00892 
00893   int ierr = 0;
00894   int returnierr = 0;
00895   if(verbose) printHeading("Testing vector constructors");
00896 
00897   if(verbose) cout << "default constructor" << endl;
00898   Epetra_IntSerialDenseVector v1;
00899   EPETRA_TEST_ERR(!(v1.CV() == Copy), ierr);
00900   if(verbose) cout << "sizing" << endl;
00901   v1.Size(v1size);
00902   EPETRA_TEST_ERR(!(v1.Length() == v1size), ierr);
00903   for(int i = 0; i < v1size; i++) {
00904     EPETRA_TEST_ERR(!(v1(i) == 0), ierr);
00905   }
00906   if(debug) printMat("v1",v1);
00907   returnierr += ierr;
00908   if(ierr == 0) 
00909      if(verbose) cout << "Checked OK." << endl;
00910   ierr = 0;
00911   if(verbose) cout << "\nmanually setting values" << endl;
00912   int* v1rand = getRandArray(v1size);
00913   for(int i = 0; i < v1size; i++)
00914     v1(i) = v1rand[i];
00915   for(int i = 0; i < v1size; i++)
00916     EPETRA_TEST_ERR(!(v1[i] == v1rand[i]), ierr);
00917   if(debug) {
00918     printArray(v1rand, v1size);
00919     printMat("v1",v1);
00920   }
00921   delete[] v1rand;
00922   returnierr += ierr;
00923   if(ierr == 0) 
00924      if(verbose) cout << "Checked OK." << endl;
00925   ierr = 0;
00926 
00927   if(verbose) cout << "\nsized constructor" << endl;
00928   Epetra_IntSerialDenseVector v1a(v1asize);
00929   EPETRA_TEST_ERR(!(v1a.Length() == v1asize), ierr);
00930   EPETRA_TEST_ERR(!(v1a.CV() == Copy),ierr);
00931   if(debug) printMat("v1a", v1a);
00932   returnierr += ierr;
00933   if(ierr == 0) 
00934      if(verbose) cout << "Checked OK." << endl;
00935   ierr = 0;
00936 
00937   if(verbose) cout << "\nuser data constructor (view)" << endl;
00938   int* v2rand = getRandArray(v2size);
00939   if(debug) printArray(v2rand, v2size);
00940   Epetra_IntSerialDenseVector v2(View, v2rand, v2size);
00941   EPETRA_TEST_ERR(!(v2.CV() == View), ierr);
00942   EPETRA_TEST_ERR(!(v2.Length() == v2size), ierr);
00943   EPETRA_TEST_ERR(!(v2.Values() == v2rand), ierr);
00944   if(debug) printMat("v2",v2);
00945   returnierr += ierr;
00946   if(ierr == 0) 
00947      if(verbose) cout << "Checked OK." << endl;
00948   ierr = 0;
00949 
00950   if(verbose) cout << "\nchanging value, checking for correct behavior" << endl;
00951   int* v2randcopy = new int[v2size]; // backup of original values
00952   for(int i = 0; i < v2size; i++)
00953     v2randcopy[i] = v2rand[i];
00954   v2(4) = v2(4) + 1; // magic number for which element to change
00955   v2randcopy[4] = v2randcopy[4] +1;
00956   for(int i = 0; i < v2size; i++)
00957     EPETRA_TEST_ERR(!(v2rand[i] == v2randcopy[i]), ierr); // v2rand should have updated correctly
00958   if(debug) {
00959     printArray(v2rand, v2size);
00960     printMat("v2",v2);
00961   }
00962   delete[] v2rand;
00963   delete[] v2randcopy;
00964   returnierr += ierr;
00965   if(ierr == 0) 
00966      if(verbose) cout << "Checked OK." << endl;
00967   ierr = 0;
00968 
00969   if(verbose) cout << "\nuser data constructor (copy)" << endl;
00970   int* v3rand = getRandArray(v3size);
00971   if(debug) printArray(v3rand, v3size);
00972   int* v3randcopy = new int[v3size];
00973   for(int i = 0; i < v3size; i++)
00974     v3randcopy[i] = v3rand[i];
00975   Epetra_IntSerialDenseVector v3(Copy, v3rand, v3size);
00976   if(debug) printMat("v3",v3);
00977   if(verbose) cout << "checking to see if data initialized correctly" << endl;
00978   EPETRA_TEST_ERR(!(v3.CV() == Copy), ierr);
00979   EPETRA_TEST_ERR(!(v3.Length() == v3size), ierr);
00980   EPETRA_TEST_ERR(!(v3.Values() != v3rand), ierr); // should not be a view
00981   for(int i = 0; i < v3size; i++)
00982     EPETRA_TEST_ERR(!(v3[i] == v3rand[i]), ierr); // data should be identical to user array
00983   returnierr += ierr;
00984   if(ierr == 0) 
00985      if(verbose) cout << "Checked OK." << endl;
00986   ierr = 0;
00987 
00988   if(verbose) cout << "\nmodifying entry" << endl;
00989   v3[5] = v3[5] + 3; // magic number for which element to change
00990   for(int i = 0; i < v3size; i++)
00991     EPETRA_TEST_ERR(!(v3rand[i] == v3randcopy[i]), ierr); // user array should be unchanged
00992   v3rand[5] = v3rand[5] + 3; // same magic modification performed to user's array
00993   for(int i = 0; i < v3size; i++)
00994     EPETRA_TEST_ERR(!(v3[i] == v3rand[i]), ierr); // should equal user array with same modification performed
00995   if(debug) {
00996     printArray(v3rand, v3size);
00997     printMat("v3",v3);
00998   }
00999   returnierr += ierr;
01000   if(ierr == 0) 
01001      if(verbose) cout << "Checked OK." << endl;
01002   ierr = 0;
01003 
01004   if(verbose) cout << "\nresizing" << endl;
01005   v3.Resize(v3resize);
01006   EPETRA_TEST_ERR(!(v3.Length() == v3resize), ierr);
01007   for(int i = 0; i < v3resize; i++)
01008     EPETRA_TEST_ERR(!(v3[i] == v3rand[i]),ierr);
01009   if(debug) printMat("v3",v3);
01010   delete[] v3rand;
01011   delete[] v3randcopy;
01012   returnierr += ierr;
01013   if(ierr == 0) 
01014      if(verbose) cout << "Checked OK." << endl;
01015   ierr = 0;
01016 
01017   if(verbose) cout << "\nChecking pointer on zero-sized vector" << endl;
01018   int* before = v3.Values();
01019   if(verbose) cout << "Resizing to 0" << endl;
01020   if(debug) cout << "Before = " << before << endl;
01021   EPETRA_TEST_ERR(!(before != 0), ierr);
01022   v3.Resize(0);
01023   int* after = v3.Values();
01024   EPETRA_TEST_ERR(!(after == 0), ierr);
01025   if(debug) cout << "After = " << after << endl;
01026   v3.Size(3);
01027   before = v3.Values();
01028   if(verbose) cout << "Sizing to 0" << endl;
01029   if(debug) cout << "Before = " << before << endl;
01030   EPETRA_TEST_ERR(!(before != 0), ierr);
01031   v3.Size(0);
01032   after = v3.Values();
01033   EPETRA_TEST_ERR(!(after == 0), ierr);
01034   if(debug) cout << "After = " << after << endl;
01035   returnierr += ierr;
01036   if(ierr == 0) 
01037      if(verbose) cout << "Checked OK." << endl;
01038   ierr = 0;
01039 
01040   return(returnierr);
01041 }
01042 //=========================================================================
01043 // test vector copy constructor (copy & view)
01044 int vectorCpyCtr(bool verbose, bool debug) {
01045   const int v1size = 15;
01046   const int v2size = 12;
01047 
01048   int ierr = 0;
01049   int returnierr = 0;
01050   if(verbose) printHeading("Testing vector copy constructors");
01051 
01052   if(verbose) cout << "checking copy constructor (view)" << endl;
01053   int* v1rand = getRandArray(v1size);
01054   if(debug) printArray(v1rand, v1size);
01055   Epetra_IntSerialDenseVector v1(View, v1rand, v1size);
01056   if(debug) {
01057     cout << "original vector:" << endl;
01058     printMat("v1",v1);
01059   }
01060   Epetra_IntSerialDenseVector v1clone(v1);
01061   if(debug) {
01062     cout << "clone vector:" << endl;
01063     printMat("v1clone",v1clone);
01064   }
01065   if(verbose) cout << "making sure signatures match" << endl;
01066   EPETRA_TEST_ERR(!identicalSignatures(v1, v1clone), ierr);
01067   delete[] v1rand;
01068   returnierr += ierr;
01069   if(ierr == 0)
01070     if(verbose) cout << "Checked OK." << endl;
01071   ierr = 0;
01072 
01073   if(verbose) cout << "\nchecking copy constructor (copy)" << endl;
01074   int* v2rand = getRandArray(v2size);
01075   if(debug) printArray(v2rand, v2size);
01076   Epetra_IntSerialDenseVector v2(Copy, v2rand, v2size);
01077   if(debug) {
01078     cout << "original vector:" << endl;
01079     printMat("v2",v2);
01080   }
01081   Epetra_IntSerialDenseVector v2clone(v2);
01082   if(debug) {
01083     cout << "clone vector:" << endl;
01084     printMat("v2clone",v2clone);
01085   }
01086   if(verbose) cout << "checking that signatures match" << endl;
01087   EPETRA_TEST_ERR(!identicalSignatures(v2, v2clone), ierr);
01088   returnierr += ierr;
01089   if(ierr == 0)
01090     if(verbose) cout << "Checked OK." << endl;
01091   ierr = 0;
01092 
01093   if(verbose) cout << "\nmodifying entry in v2, v2clone should be unchanged" << endl;
01094   EPETRA_TEST_ERR(!seperateData(v2, v2clone), ierr);
01095   if(debug) {
01096     printArray(v2rand, v2size);
01097     cout << "orig:" << endl;
01098     printMat("v2",v2);
01099     cout << "clone:" << endl;
01100     printMat("v2clone",v2clone);
01101   }
01102   delete[] v2rand;
01103   returnierr += ierr;
01104   if(ierr == 0)
01105     if(verbose) cout << "Checked OK." << endl;
01106   ierr = 0;
01107 
01108   return(returnierr);
01109 }
01110 //=========================================================================
01111 // test vector operator= (copy & view)
01112 int vectorAssignment(bool verbose, bool debug) {
01113   int ierr = 0;
01114   int returnierr = 0;
01115   if(verbose) printHeading("Testing vector operator=");
01116 
01117   // each section is in its own block so we can reuse variable names
01118   // lhs = left hand side, rhs = right hand side
01119   
01120   {
01121     // copy->copy (more space needed)
01122     // orig and dup should have same signature
01123     // modifying orig or dup should have no effect on the other
01124     if(verbose) cout << "Checking copy->copy (new alloc)" << endl;
01125     Epetra_IntSerialDenseVector lhs(2);
01126     int* rand1 = getRandArray(10);
01127     Epetra_IntSerialDenseVector rhs(Copy, rand1, 10);
01128     if(debug) {
01129       cout << "before assignment:" << endl;
01130       printMat("rhs",rhs);
01131       printMat("lhs",lhs);
01132     }
01133     lhs = rhs;
01134     if(debug) {
01135       cout << "after assignment:" << endl;
01136       printMat("rhs",rhs);
01137       printMat("lhs",lhs);
01138     }
01139     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
01140     EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
01141     delete[] rand1;
01142   }
01143   returnierr += ierr;
01144   if(ierr == 0)
01145     if(verbose) cout << "Checked OK." << endl;
01146   ierr = 0;
01147   {
01148     // copy->copy (have enough space)
01149     // orig and dup should have same signature
01150     // modifying orig or dup should have no effect on the other
01151     if(verbose) cout << "\nChecking copy->copy (no alloc)" << endl;
01152     int* rand1 = getRandArray(20);
01153     int* rand2 = getRandArray(15);
01154     Epetra_IntSerialDenseVector lhs(Copy, rand1, 20);
01155     Epetra_IntSerialDenseVector rhs(Copy, rand2, 15);
01156     int* origA = lhs.A();
01157     int origLDA = lhs.LDA();
01158     if(debug) {
01159       cout << "before assignment:" << endl;
01160       printMat("rhs",rhs);
01161       printMat("lhs",lhs);
01162     }
01163     lhs = rhs;
01164     if(debug) {
01165       cout << "after assignment:" << endl;
01166       printMat("rhs",rhs);
01167       printMat("lhs",lhs);
01168     }
01169     // in this case, instead of doing a "normal" LDA test in identSig,
01170     // we do our own. Since we had enough space already, A and LDA should
01171     // not have been changed by the assignment. (The extra parameter to
01172     // identicalSignatures tells it not to test LDA).
01173     EPETRA_TEST_ERR((lhs.A() != origA) || (lhs.LDA() != origLDA), ierr);
01174     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs,false), ierr);
01175     EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
01176     delete[] rand1;
01177     delete[] rand2;
01178   }
01179   returnierr += ierr;
01180   if(ierr == 0)
01181     if(verbose) cout << "Checked OK." << endl;
01182   ierr = 0;
01183   {
01184     // view->copy
01185     // orig and dup should have same signature
01186     // modifying orig or dup should have no effect on the other
01187     if(verbose) cout << "\nChecking view->copy" << endl;
01188     int* rand1 = getRandArray(5);
01189     int* rand2 = getRandArray(8);
01190     Epetra_IntSerialDenseVector lhs(View, rand1, 5);
01191     Epetra_IntSerialDenseVector rhs(Copy, rand2, 8);
01192     if(debug) {
01193       cout << "before assignment:" << endl;
01194       printMat("rhs",rhs);
01195       printMat("lhs",lhs);
01196     }
01197     lhs = rhs;
01198     if(debug) {
01199       cout << "after assignment:" << endl;
01200       printMat("rhs",rhs);
01201       printMat("lhs",lhs);
01202     }
01203     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
01204     EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
01205     delete[] rand1;
01206     delete[] rand2;
01207   }
01208   returnierr += ierr;
01209   if(ierr == 0)
01210     if(verbose) cout << "Checked OK." << endl;
01211   ierr = 0;
01212   {
01213     // copy->view
01214     // orig and dup should have same signature
01215     // modifying orig or dup should change the other
01216     if(verbose) cout << "\nChecking copy->view" << endl;
01217     int* rand1 = getRandArray(10);
01218     Epetra_IntSerialDenseVector lhs(4);
01219     Epetra_IntSerialDenseVector rhs(View, rand1, 10);
01220     if(debug) {
01221       cout << "before assignment:" << endl;
01222       printMat("rhs",rhs);
01223       printMat("lhs",lhs);
01224     }
01225     lhs = rhs;
01226     if(debug) {
01227       cout << "after assignment:" << endl;
01228       printMat("rhs",rhs);
01229       printMat("lhs",lhs);
01230     }
01231     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
01232     EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
01233     delete[] rand1;
01234   }
01235   returnierr += ierr;
01236   if(ierr == 0)
01237     if(verbose) cout << "Checked OK." << endl;
01238   ierr = 0;
01239   {
01240     // view->view
01241     // orig and dup should have same signature
01242     // modifying orig or dup should change the other
01243     if(verbose) cout << "\nChecking view->view" << endl;
01244     int* rand1 = getRandArray(9);
01245     int* rand2 = getRandArray(11);
01246     Epetra_IntSerialDenseVector lhs(View, rand1, 9);
01247     Epetra_IntSerialDenseVector rhs(View, rand2, 11);
01248     if(debug) {
01249       cout << "before assignment:" << endl;
01250       printMat("rhs",rhs);
01251       printMat("lhs",lhs);
01252     }
01253     lhs = rhs;
01254     if(debug) {
01255       cout << "after assignment:" << endl;
01256       printMat("rhs",rhs);
01257       printMat("lhs",lhs);
01258     }
01259     EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
01260     EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
01261     delete[] rand1;
01262     delete[] rand2;
01263   }
01264   returnierr += ierr;
01265   if(ierr == 0)
01266     if(verbose) cout << "Checked OK." << endl;
01267   ierr = 0;
01268   {
01269     // test MakeViewOf
01270     // orig and dup should have same signature except for CV_
01271     // modifying orig or dup should change the other
01272     if(verbose) cout << "\nChecking CrsGraph's usage of MakeViewOf" << endl;
01273     int* rand1 = getRandArray(10);
01274     int* rand2 = getRandArray(10);
01275     Epetra_IntSerialDenseVector lhs(Copy, rand1, 10);
01276     Epetra_IntSerialDenseVector rhs(Copy, rand2, 10);
01277     if(debug) {
01278       cout << "before assignment:" << endl;
01279       printMat("rhs",rhs);
01280       printMat("lhs",lhs);
01281     }
01282     lhs.MakeViewOf(rhs);
01283     if(debug) {
01284       cout << "after assignment:" << endl;
01285       printMat("rhs",rhs);
01286       printMat("lhs",lhs);
01287     }
01288     EPETRA_TEST_ERR(!(lhs.CV() == View), ierr);
01289     EPETRA_TEST_ERR(!(lhs.M() == rhs.M()), ierr);
01290     EPETRA_TEST_ERR(!(lhs.N() == rhs.N()), ierr);
01291     EPETRA_TEST_ERR(!(lhs.LDA() == rhs.LDA()), ierr);
01292     EPETRA_TEST_ERR(!(lhs.A() == rhs.A()), ierr);
01293     EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
01294     delete[] rand1;
01295     delete[] rand2;
01296   }
01297   returnierr += ierr;
01298   if(ierr == 0)
01299     if(verbose) cout << "Checked OK." << endl;
01300   ierr = 0;
01301 
01302   return(returnierr);
01303 }
01304 
01305 //=========================================================================
01306 // test vector error-reporting
01307 int vectorExceptions(bool verbose, bool debug) {
01308   int returnierr = 0;
01309   int ierr = 0;
01310   bool caught = false;
01311   Epetra_IntSerialDenseVector* vector;
01312 
01313   if(verbose) printHeading("Testing vector error-reporting.\nExpect error messages if EPETRA_NO_ERROR_REPORTS is not defined.");
01314   
01315   try { // invalid dimension to sized ctr
01316     caught = false;
01317     if(verbose) cout << "Checking Epetra_IntSerialDenseVector(-1)";
01318     vector = new Epetra_IntSerialDenseVector(-1);
01319   }
01320   catch(int error) {
01321     caught = true;
01322     EPETRA_TEST_ERR(error != -1, returnierr);
01323     if(error == -1)
01324       if(verbose) cout << "Checked OK." << endl;
01325   }
01326   EPETRA_TEST_ERR(!caught, returnierr);
01327 
01328      int* rand2 = getRandArray(2);
01329   try { // invalid dimension to user-data ctr
01330     caught = false;
01331     if(verbose) cout << "\nChecking Epetra_IntSerialDenseVector(Copy, int*, -3)";
01332     vector = new Epetra_IntSerialDenseVector(Copy, rand2, -3);
01333   }
01334   catch(int error) {
01335     caught = true;
01336     EPETRA_TEST_ERR(error != -1, returnierr);
01337     if(error == -1)
01338       if(verbose) cout << "Checked OK." << endl;
01339   }
01340   EPETRA_TEST_ERR(!caught, returnierr);
01341      delete[] rand2;
01342 
01343   try { // null pointer to user-data ctr
01344     caught = false;
01345     if(verbose) cout << "\nChecking Epetra_IntSerialDenseVector(Copy, 0, 5)";
01346     vector = new Epetra_IntSerialDenseVector(Copy, 0, 5);
01347   }
01348   catch(int error) {
01349     caught = true;
01350     EPETRA_TEST_ERR(error != -3, returnierr);
01351     if(error == -3)
01352       if(verbose) cout << "Checked OK." << endl;
01353   }
01354   EPETRA_TEST_ERR(!caught, returnierr);
01355 
01356   // invalid parameter to size
01357   if(verbose) cout << "\nChecking Size(-2)" << endl;
01358   Epetra_IntSerialDenseVector v1;
01359   ierr = v1.Size(-2);
01360   EPETRA_TEST_ERR(!(ierr == -1), returnierr);
01361   if(ierr == -1)
01362     if(verbose) cout << "Checked OK." << endl;
01363 
01364   // invalid parameter to resize
01365   if(verbose) cout << "\nChecking Resize(-4)" << endl;
01366   v1.Size(5);
01367   ierr = v1.Resize(-4);
01368   EPETRA_TEST_ERR(!(ierr == -1), returnierr);
01369   if(ierr == -1)
01370     if(verbose) cout << "Checked OK." << endl;
01371 
01372 #ifdef HAVE_EPETRA_ARRAY_BOUNDS_CHECK // only test op() and op[] exceptions if macro is defined.
01373   // out of range index to op() & op[]
01374   int* rand17 = getRandArray(17);
01375   Epetra_IntSerialDenseVector v2(View, rand17, 17);
01376   try { // op() too high
01377     caught = false;
01378     if(verbose) cout << "\nChecking operator () - index too high";
01379     ierr = v2(17);
01380   }
01381   catch(int error) {
01382     caught = true;
01383     EPETRA_TEST_ERR(error != -1, returnierr);
01384     if(error == -1)
01385       if(verbose) cout << "Checked OK." << endl;
01386   }
01387   EPETRA_TEST_ERR(!caught, returnierr);
01388   
01389   try { // op() too low
01390     caught = false;
01391     if(verbose) cout << "\nChecking operator () - index too low";
01392     ierr = v2(-1);
01393   }
01394   catch(int error) {
01395     caught = true;
01396     EPETRA_TEST_ERR(error != -1, returnierr);
01397     if(error == -1)
01398       if(verbose) cout << "Checked OK." << endl;
01399   }
01400   EPETRA_TEST_ERR(!caught, returnierr);
01401 
01402   try { // op[] too high
01403     caught = false;
01404     if(verbose) cout << "\nChecking operator [] - index too high";
01405     ierr = v2[17];
01406   }
01407   catch(int error) {
01408     caught = true;
01409     EPETRA_TEST_ERR(error != -1, returnierr);
01410     if(error == -1)
01411       if(verbose) cout << "Checked OK." << endl;
01412   }
01413   EPETRA_TEST_ERR(!caught, returnierr);
01414   
01415   try { // op[] too low
01416     caught = false;
01417     if(verbose) cout << "\nChecking operator [] - index too low";
01418     ierr = v2[-1];
01419   }
01420   catch(int error) {
01421     caught = true;
01422     EPETRA_TEST_ERR(error != -1, returnierr);
01423     if(error == -1)
01424       if(verbose) cout << "Checked OK." << endl;
01425   }
01426   EPETRA_TEST_ERR(!caught, returnierr);
01427   delete[] rand17;
01428 #endif // end of HAVE_EPETRA_ARRAY_BOUNDS_CHECK conditional
01429 
01430   // we don't need to check for ISDV = ISDM, as that is a compile-time error
01431   
01432   return(returnierr);
01433 }
01434 
01435 //=========================================================================
01436 //=========================================================================
01437 // helper functions
01438 //=========================================================================
01439 //=========================================================================
01440 // checks the signatures of two matrices
01441 bool identicalSignatures(Epetra_IntSerialDenseMatrix& a, Epetra_IntSerialDenseMatrix& b, bool testLDA) {
01442   /*cout << "M: " << a.M() << "  " << b.M() << endl;
01443   cout << "N: " << a.N() << "  " << b.N() << endl;
01444   cout << "LDA: " << a.LDA() << "  " << b.LDA() << endl;
01445   cout << "CV: " << a.CV() << "  " << b.CV() << endl;
01446   cout << "A: " << a.A() << "  " << b.A() << endl;*/
01447 
01448   if((a.M()  != b.M()  )|| // check properties first
01449      (a.N()  != b.N()  )||
01450      (a.CV() != b.CV() ))
01451     return(false);
01452 
01453   if(testLDA == true)      // if we are coming from op= c->c #2 (have enough space)
01454     if(a.LDA() != b.LDA()) // then we don't check LDA (but we do check it in the test function)
01455       return(false);
01456 
01457   if(a.CV() == View) { // if we're still here, we need to check the data
01458     if(a.A() != b.A()) // for a view, this just means checking the pointers
01459       return(false);   // for a copy, this means checking each element
01460   }
01461   else { // CV == Copy
01462     const int m = a.M();
01463     const int n = a.N();
01464     for(int i = 0; i < m; i++)
01465       for(int j = 0; j < n; j++) {
01466         if(a(i,j) != b(i,j))
01467           return(false);
01468       }
01469   }
01470 
01471   return(true); // if we're still here, signatures are identical
01472 }
01473 //=========================================================================
01474 // checks if two matrices are independent or not
01475 bool seperateData(Epetra_IntSerialDenseMatrix& a, Epetra_IntSerialDenseMatrix& b) {
01476   bool seperate;
01477 
01478   int r = EPETRA_MIN(a.M(),b.M()) / 2; // ensures (r,c) is valid
01479   int c = EPETRA_MIN(a.N(),b.N()) / 2; // in both matrices
01480 
01481   int orig_a = a(r,c);
01482   int new_value = a(r,c) + 1;
01483   if(b(r,c) == new_value) // there's a chance b could be independent, but
01484     new_value++;          // already have new_value in (r,c).
01485   
01486   a(r,c) = new_value;
01487   if(b(r,c) == new_value)
01488     seperate = false;
01489   else
01490     seperate = true;
01491 
01492   a(r,c) = orig_a; // undo change we made to a
01493 
01494   return(seperate);
01495 }
01496 //=========================================================================
01497 // returns a int* array of a given length, with random values on interval [0,100]
01498 int* getRandArray(int length) {
01499   int* array = new int[length];
01500   for(int i = 0; i < length; i++)
01501     array[i] = randomInt();
01502 
01503   return(array);
01504 }
01505 //=========================================================================
01506 // returns a random integer on the interval [0-maxint).
01507 // this is the same generator used in IntSerialDenseMatrix
01508 int randomInt() {
01509   const int maxint = 100;
01510 
01511   const double a = 16807.0;
01512   const double BigInt = 2147483647.0;
01513   double seed = rand(); // Use POSIX standard random function;
01514 
01515   seed = fmod(a * seed, BigInt); // fmod returns remainder of floating point division
01516                                  // (a * seed) - (floor(a * seed / BigInt) * BigInt)
01517   double randdouble = (seed / BigInt); // should be [0,1)
01518   int randint = int(randdouble * maxint);
01519   
01520   return(randint);
01521 }
01522 //=========================================================================
01523 // prints int* array with formatting
01524 void printArray(int* array, int length) {
01525   cout << "user array (size " << length << "): ";
01526   for(int i = 0; i < length; i++)
01527     cout << array[i] << "  ";
01528   cout << endl;
01529 }
01530 //=========================================================================
01531 // prints IntSerialDenseMatrix/Vector with formatting
01532 void printMat(const char* name, Epetra_IntSerialDenseMatrix& matrix) {
01533   //cout << "--------------------" << endl;
01534   cout << "*** " << name << " ***" << endl;
01535   cout << matrix;
01536   //cout << "--------------------" << endl;
01537 }
01538 
01539 //=========================================================================
01540 // prints section heading with spacers/formatting
01541 void printHeading(const char* heading) {
01542   cout << "\n==================================================================\n";
01543   cout << heading << endl;
01544   cout << "==================================================================\n";
01545 }

Generated on Wed May 12 21:41:04 2010 for Epetra Package Browser (Single Doxygen Collection) by  doxygen 1.4.7