Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test/IntSerialDense/cxx_main.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Epetra: Linear Algebra Services Package
5 // Copyright 2011 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 
43 // Epetra_IntSerialDense Test routine
44 
47 #include "../epetra_test_err.h"
48 #include "Epetra_ConfigDefs.h"
49 #include "Epetra_DataAccess.h"
50 #include "Epetra_Version.h"
51 #ifdef EPETRA_MPI
52 #include "Epetra_MpiComm.h"
53 #include <mpi.h>
54 #else
55 #include "Epetra_SerialComm.h"
56 #endif
57 
58 // matrix-testing prototypes
59 int matrixCoverage(bool verbose, bool debug);
60 int matrixCtr(bool verbose, bool debug);
61 int matrixCpyCtr(bool verbose, bool debug);
62 int matrixAssignment(bool verbose, bool debug);
63 int matrixExceptions(bool verbose, bool debug);
64 // vector-testing prototypes
65 int vectorCoverage(bool verbose, bool debug);
66 int vectorCtr(bool verbose, bool debug);
67 int vectorCpyCtr(bool verbose, bool debug);
68 int vectorAssignment(bool verbose, bool debug);
69 int vectorExceptions(bool verbose, bool debug);
70 // helper function prototypes
73 int* getRandArray(int length);
74 int randomInt();
75 void printArray(int* array, int length);
76 void printMat(const char* name, Epetra_IntSerialDenseMatrix& matrix);
77 void printHeading(const char* heading);
78 
79 int main(int argc, char *argv[]) {
80 //============================
81 // Check for verbose or debug
82 
83 bool verbose = false;
84 bool debug = false;
85 if(argc > 1) {
86  if((argv[1][0] == '-') && (argv[1][1] == 'v'))
87  verbose = true;
88  if((argv[1][0] == '-') && (argv[1][1] == 'd')) {
89  debug = true;
90  verbose = true;
91  }
92 }
93 
94 //============================
95 // Initialize Comm
96 
97 #ifdef EPETRA_MPI
98  // Initialize MPI
99  MPI_Init(&argc,&argv);
100  Epetra_MpiComm Comm(MPI_COMM_WORLD);
101 #else
102  Epetra_SerialComm Comm;
103 #endif
104 
105  if (verbose && Comm.MyPID()==0)
106  cout << Epetra_Version() << endl << endl;
107 
108 //============================
109 // other initial setup
110 
111  if (!verbose) Comm.SetTracebackMode(0); // This should shut down any error traceback reporting
112 
113  int ierr = 0;
114  int returnierr = 0;
115 
116 //============================
117 // Test vector
118 
119  ierr = vectorExceptions(verbose, debug);
120  EPETRA_TEST_ERR(ierr, returnierr);
121 
122  ierr = vectorCtr(verbose, debug);
123  EPETRA_TEST_ERR(ierr, returnierr);
124 
125  ierr = vectorCpyCtr(verbose, debug);
126  EPETRA_TEST_ERR(ierr, returnierr);
127 
128  ierr = vectorAssignment(verbose, debug);
129  EPETRA_TEST_ERR(ierr, returnierr);
130 
131  ierr = vectorCoverage(verbose, debug);
132  EPETRA_TEST_ERR(ierr, returnierr);
133 
134 //============================
135 // Test matrix
136 
137  ierr = matrixExceptions(verbose, debug);
138  EPETRA_TEST_ERR(ierr, returnierr);
139 
140  ierr = matrixCtr(verbose, debug);
141  EPETRA_TEST_ERR(ierr, returnierr);
142 
143  ierr = matrixCpyCtr(verbose, debug);
144  EPETRA_TEST_ERR(ierr, returnierr);
145 
146  ierr = matrixAssignment(verbose, debug);
147  EPETRA_TEST_ERR(ierr, returnierr);
148 
149  ierr = matrixCoverage(verbose, debug);
150  EPETRA_TEST_ERR(ierr, returnierr);
151 
152 //============================
153 // end of program cleanup
154 
155 #ifdef EPETRA_MPI
156  MPI_Finalize();
157 #endif
158 
159  return(returnierr);
160 }
161 
162 //=========================================================================
163 //=========================================================================
164 // Matrix-testing functions
165 //=========================================================================
166 //=========================================================================
167 // call functions we don't have tests for, for sake of code coverage.
168 // (the functions are called, but their output isn't checked for correctness).
169 int matrixCoverage(bool verbose, bool debug) {
170  if(verbose) printHeading("Testing other matrix functions");
171 
172  int* rand1 = getRandArray(9);
173  if(debug) printArray(rand1, 9);
174  Epetra_IntSerialDenseMatrix m1(Copy, rand1, 3, 3, 3);
175  if(debug) printMat("m1",m1);
176  delete[] rand1;
177 
178  if(verbose) cout << "calling one norm" << endl;
179  int onenorm = m1.OneNorm();
180  if(debug) cout << "m1.OneNorm() = " << onenorm << endl;
181 
182  if(verbose) cout << "calling infinity norm" << endl;
183  int infnorm = m1.InfNorm();
184  if(debug) cout << "m1.InfNorm() = " << infnorm << endl;
185 
186  if(verbose) cout << "calling random" << endl;
188  if(debug) printMat("m2 (before)",m2);
189  m2.Random();
190  if(debug) printMat("m2 (after)",m2);
191 
192  if(verbose) cout << "Checked OK." << endl;
193 
194  return(0);
195 }
196 
197 //=========================================================================
198 // test matrix default constructor, user constructor (copy & view),
199 // () [] operators (read & write), shape (larger), reshape (smaller)
200 int matrixCtr(bool verbose, bool debug) {
201  const int m1rows = 5;
202  const int m1cols = 4;
203  const int m1arows = 4;
204  const int m1acols = 6;
205  const int m2rows = 2;
206  const int m2cols = 7;
207  const int m3rows = 8;
208  const int m3cols = 3;
209  const int m3Rrows = 5; // should be smaller than m3rows
210  const int m3Rcols = 2; // should be smaller than m3cols
211 
212  int ierr = 0;
213  int returnierr = 0;
214  if(verbose) printHeading("Testing matrix constructors");
215 
216  if(verbose) cout << "default constructor" << endl;
218  EPETRA_TEST_ERR(!(m1.CV() == Copy), ierr);
219  if(verbose) cout << "shaping" << endl;
220  m1.Shape(m1rows, m1cols);
221  EPETRA_TEST_ERR(!(m1.M() == m1rows), ierr);
222  for(int i = 0; i < m1rows; i++)
223  for(int j = 0; j < m1cols; j++)
224  EPETRA_TEST_ERR(!(m1(i,j) == 0), ierr);
225  if(debug) printMat("m1",m1);
226  returnierr += ierr;
227  if(ierr == 0)
228  if(verbose) cout << "Checked OK." << endl;
229  ierr = 0;
230  if(verbose) cout << "\nmanually setting values" << endl;
231  int* m1rand = getRandArray(m1rows * m1cols);
232  for(int i = 0; i < m1rows; i++)
233  for(int j = 0; j < m1cols; j++)
234  m1(i,j) = m1rand[i*m1cols + j];
235  for(int i = 0; i < m1rows; i++)
236  for(int j = 0; j < m1cols; j++)
237  EPETRA_TEST_ERR(!(m1[j][i] == m1rand[i*m1cols + j]), ierr);
238  if(debug) {
239  printArray(m1rand, m1rows * m1cols);
240  printMat("m1",m1);
241  }
242  delete[] m1rand;
243  returnierr += ierr;
244  if(ierr == 0)
245  if(verbose) cout << "Checked OK." << endl;
246  ierr = 0;
247 
248  if(verbose) cout << "\nshaped constructor" << endl;
249  Epetra_IntSerialDenseMatrix m1a(m1arows, m1acols);
250  EPETRA_TEST_ERR(!(m1a.M() == m1arows), ierr);
251  EPETRA_TEST_ERR(!(m1a.N() == m1acols), ierr);
252  EPETRA_TEST_ERR(!(m1a.LDA() == m1arows), ierr);
253  EPETRA_TEST_ERR(!(m1a.CV() == Copy),ierr);
254  if(debug) printMat("m1a", m1a);
255  returnierr += ierr;
256  if(ierr == 0)
257  if(verbose) cout << "Checked OK." << endl;
258  ierr = 0;
259 
260  if(verbose) cout << "\nuser data constructor (view)" << endl;
261  int* m2rand = getRandArray(m2rows * m2cols);
262  if(debug) printArray(m2rand, m2rows * m2cols);
263  Epetra_IntSerialDenseMatrix m2(View, m2rand, m2rows, m2rows, m2cols);
264  EPETRA_TEST_ERR(!(m2.CV() == View), ierr);
265  EPETRA_TEST_ERR(!(m2.M() == m2rows), ierr);
266  EPETRA_TEST_ERR(!(m2.N() == m2cols), ierr);
267  EPETRA_TEST_ERR(!(m2.LDA() == m2rows), ierr);
268  EPETRA_TEST_ERR(!(m2.A() == m2rand), ierr);
269  if(debug) printMat("m2",m2);
270  returnierr += ierr;
271  if(ierr == 0)
272  if(verbose) cout << "Checked OK." << endl;
273  ierr = 0;
274 
275  if(verbose) cout << "\nchanging value, checking for correct behavior" << endl;
276  int* m2randcopy = new int[m2rows * m2cols]; // backup of original values
277  for(int i = 0; i < m2rows * m2cols; i++)
278  m2randcopy[i] = m2rand[i];
279  m2(0,4) = m2(0,4) + 1; // magic numbers for which element to change
280  m2randcopy[4 * m2rows] = m2randcopy[4 * m2rows] + 1;
281  for(int i = 0; i < m2rows * m2cols; i++)
282  EPETRA_TEST_ERR(!(m2rand[i] == m2randcopy[i]), ierr); // m2rand should have updated correctly
283  if(debug) {
284  printArray(m2rand, m2rows * m2cols);
285  printMat("m2",m2);
286  }
287  delete[] m2rand;
288  delete[] m2randcopy;
289  returnierr += ierr;
290  if(ierr == 0)
291  if(verbose) cout << "Checked OK." << endl;
292  ierr = 0;
293 
294  if(verbose) cout << "\nuser data constructor (copy)" << endl;
295  int* m3rand = getRandArray(m3rows * m3cols);
296  if(debug) printArray(m3rand, m3rows * m3cols);
297  int* m3randcopy = new int[m3rows * m3cols];
298  for(int i = 0; i < m3rows * m3cols; i++)
299  m3randcopy[i] = m3rand[i];
300  Epetra_IntSerialDenseMatrix m3(Copy, m3rand, m3rows, m3rows, m3cols);
301  if(debug) printMat("m3",m3);
302  if(verbose) cout << "checking to see if data initialized correctly" << endl;
303  EPETRA_TEST_ERR(!(m3.CV() == Copy), ierr);
304  EPETRA_TEST_ERR(!(m3.M() == m3rows), ierr);
305  EPETRA_TEST_ERR(!(m3.N() == m3cols), ierr);
306  EPETRA_TEST_ERR(!(m3.LDA() == m3rows), ierr);
307  EPETRA_TEST_ERR(!(m3.A() != m3rand), ierr); // should not be a view
308  for(int i = 0; i < m3rows; i++)
309  for(int j = 0; j < m3cols; j++)
310  EPETRA_TEST_ERR(!(m3[j][i] == m3rand[j * m3rows + i]), ierr); // data should be identical to user array
311  returnierr += ierr;
312  if(ierr == 0)
313  if(verbose) cout << "Checked OK." << endl;
314  ierr = 0;
315 
316  if(verbose) cout << "\nmodifying entry" << endl;
317  m3[1][5] = m3[1][5] + 3; // magic numbers for which element to change
318  for(int i = 0; i < m3rows * m3cols; i++)
319  EPETRA_TEST_ERR(!(m3rand[i] == m3randcopy[i]), ierr); // user array should be unchanged
320  m3rand[13] = m3rand[13] + 3; // same magic modification performed to user's array
321  for(int i = 0; i < m3rows; i++)
322  for(int j = 0; j < m3cols; j++)
323  EPETRA_TEST_ERR(!(m3[j][i] == m3rand[j * m3rows + i]), ierr); // should equal user array with same modification performed
324  if(debug) {
325  printArray(m3rand, m3rows * m3cols);
326  printMat("m3",m3);
327  }
328  returnierr += ierr;
329  if(ierr == 0)
330  if(verbose) cout << "Checked OK." << endl;
331  ierr = 0;
332 
333  if(verbose) cout << "\nreshaping" << endl;
334  m3.Reshape(m3Rrows, m3Rcols);
335  EPETRA_TEST_ERR(!(m3.M() == m3Rrows), ierr);
336  EPETRA_TEST_ERR(!(m3.N() == m3Rcols), ierr);
337  EPETRA_TEST_ERR(!(m3.LDA() == m3Rrows), ierr);
338  for(int i = 0; i < m3Rrows; i++)
339  for(int j = 0; j < m3Rcols; j++)
340  EPETRA_TEST_ERR(!(m3[j][i] == m3rand[j * m3rows + i]),ierr);
341  if(debug) printMat("m3",m3);
342 
343  delete[] m3rand;
344  delete[] m3randcopy;
345  returnierr += ierr;
346  if(ierr == 0)
347  if(verbose) cout << "Checked OK." << endl;
348  ierr = 0;
349 
350  if(verbose) cout << "\nChecking pointer on zero-sized matrix" << endl;
351  int* before = m3.A();
352  if(verbose) cout << "Reshaping to (4,0)" << endl;
353  if(debug) cout << "Before = " << before << endl;
354  EPETRA_TEST_ERR(!(before != 0), ierr);
355  m3.Reshape(4,0);
356  int* after = m3.A();
357  EPETRA_TEST_ERR(!(after == 0), ierr);
358  if(debug) cout << "After = " << after << endl;
359  m3.Shape(3,3);
360  before = m3.A();
361  if(verbose) cout << "Shaping to (0,3)" << endl;
362  if(debug) cout << "Before = " << before << endl;
363  EPETRA_TEST_ERR(!(before != 0), ierr);
364  m3.Shape(0,3);
365  after = m3.A();
366  EPETRA_TEST_ERR(!(after == 0), ierr);
367  if(debug) cout << "After = " << after << endl;
368  returnierr += ierr;
369  if(ierr == 0)
370  if(verbose) cout << "Checked OK." << endl;
371  ierr = 0;
372 
373  return(returnierr);
374 }
375 
376 //=========================================================================
377 // test matrix copy constructor (copy & view)
378 int matrixCpyCtr(bool verbose, bool debug) {
379  const int m1rows = 5;
380  const int m1cols = 4;
381  const int m2rows = 2;
382  const int m2cols = 6;
383 
384  int ierr = 0;
385  int returnierr = 0;
386  if(verbose) printHeading("Testing matrix copy constructors");
387 
388  if(verbose) cout << "checking copy constructor (view)" << endl;
389  int* m1rand = getRandArray(m1rows * m1cols);
390  if(debug) printArray(m1rand, m1rows * m1cols);
391  Epetra_IntSerialDenseMatrix m1(View, m1rand, m1rows, m1rows, m1cols);
392  if(debug) {
393  cout << "original matrix:" << endl;
394  printMat("m1",m1);
395  }
396  Epetra_IntSerialDenseMatrix m1clone(m1);
397  if(debug) {
398  cout << "clone matrix:" << endl;
399  printMat("m1clone",m1clone);
400  }
401  if(verbose) cout << "making sure signatures match" << endl;
402  EPETRA_TEST_ERR(!identicalSignatures(m1, m1clone), ierr);
403  delete[] m1rand;
404  returnierr += ierr;
405  if(ierr == 0)
406  if(verbose) cout << "Checked OK." << endl;
407  ierr = 0;
408 
409  if(verbose) cout << "\nchecking copy constructor (copy)" << endl;
410  int* m2rand = getRandArray(m2rows * m2cols);
411  if(debug) printArray(m2rand, m2rows * m2cols);
412  Epetra_IntSerialDenseMatrix m2(Copy, m2rand, m2rows, m2rows, m2cols);
413  if(debug) {
414  cout << "original matrix:" << endl;
415  printMat("m2",m2);
416  }
417  Epetra_IntSerialDenseMatrix m2clone(m2);
418  if(debug) {
419  cout << "clone matrix:" << endl;
420  printMat("m2clone",m2clone);
421  }
422  if(verbose) cout << "checking that signatures match" << endl;
423  EPETRA_TEST_ERR(!identicalSignatures(m2, m2clone), ierr);
424  returnierr += ierr;
425  if(ierr == 0)
426  if(verbose) cout << "Checked OK." << endl;
427  ierr = 0;
428 
429  if(verbose) cout << "\nmodifying entry in m2, m2clone should be unchanged" << endl;
430  EPETRA_TEST_ERR(!seperateData(m2, m2clone), ierr);
431  if(debug) {
432  printArray(m2rand, m2rows * m2cols);
433  cout << "orig:" << endl;
434  printMat("m2",m2);
435  cout << "clone:" << endl;
436  printMat("m2clone",m2clone);
437  }
438  delete[] m2rand;
439  returnierr += ierr;
440  if(ierr == 0)
441  if(verbose) cout << "Checked OK." << endl;
442  ierr = 0;
443 
444  return(returnierr);
445 }
446 
447 //=========================================================================
448 // test matrix error-reporting
449 int matrixExceptions(bool verbose, bool debug) {
450  int returnierr = 0;
451  int ierr = 0;
452  bool caught = false;
453  Epetra_IntSerialDenseMatrix* matrix = NULL;
454 
455  if(verbose) printHeading("Testing matrix error-reporting.\nExpect error messages if EPETRA_NO_ERROR_REPORTS is not defined.");
456 
457  // invalid dimension to sized ctr (2 cases)
458  try {
459  caught = false;
460  if(verbose) cout << "Checking Epetra_IntSerialDenseMatrix(-1, 6) - invalid rows";
461  matrix = new Epetra_IntSerialDenseMatrix(-1, 6);
462  }
463  catch(int error) {
464  caught = true;
465  EPETRA_TEST_ERR(error != -1, returnierr);
466  if(error == -1)
467  if(verbose) cout << "Checked OK." << endl;
468  }
469  EPETRA_TEST_ERR(!caught, returnierr);
470  try {
471  caught = false;
472  if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(3, -5) - invalid cols";
473  matrix = new Epetra_IntSerialDenseMatrix(3, -5);
474  }
475  catch(int error) {
476  caught = true;
477  EPETRA_TEST_ERR(error != -1, returnierr);
478  if(error == -1)
479  if(verbose) cout << "Checked OK." << endl;
480  }
481  EPETRA_TEST_ERR(!caught, returnierr);
482 
483  // invalid dimension to user-data ctr (3 cases)
484  int* rand2 = getRandArray(2);
485  try {
486  caught = false;
487  if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(Copy, int*, -1, 2, 2) - invalid lda";
488  matrix = new Epetra_IntSerialDenseMatrix(Copy, rand2, -1, 2, 2);
489  }
490  catch(int error) {
491  caught = true;
492  EPETRA_TEST_ERR(error != -1, returnierr);
493  if(error == -1)
494  if(verbose) cout << "Checked OK." << endl;
495  }
496  EPETRA_TEST_ERR(!caught, returnierr);
497  try {
498  caught = false;
499  if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(Copy, int*, 3, -2, 3) - invalid rows";
500  matrix = new Epetra_IntSerialDenseMatrix(Copy, rand2, 3, -2, 3);
501  }
502  catch(int error) {
503  caught = true;
504  EPETRA_TEST_ERR(error != -1, returnierr);
505  if(error == -1)
506  if(verbose) cout << "Checked OK." << endl;
507  }
508  EPETRA_TEST_ERR(!caught, returnierr);
509  try {
510  caught = false;
511  if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(Copy, int*, 4, 4, -4) - invalid cols";
512  matrix = new Epetra_IntSerialDenseMatrix(Copy, rand2, -4, 4, -4);
513  }
514  catch(int error) {
515  caught = true;
516  EPETRA_TEST_ERR(error != -1, returnierr);
517  if(error == -1)
518  if(verbose) cout << "Checked OK." << endl;
519  }
520  EPETRA_TEST_ERR(!caught, returnierr);
521  delete [] rand2;
522 
523  // null pointer to user-data ctr
524  try {
525  caught = false;
526  if(verbose) cout << "\nChecking Epetra_IntSerialDenseMatrix(Copy, 0, 5, 5, 5) - null pointer";
527  matrix = new Epetra_IntSerialDenseMatrix(Copy, 0, 5, 5, 5);
528  }
529  catch(int error) {
530  caught = true;
531  EPETRA_TEST_ERR(error != -3, returnierr);
532  if(error == -3)
533  if(verbose) cout << "Checked OK." << endl;
534  }
535  EPETRA_TEST_ERR(!caught, returnierr);
536 
537  // invalid parameter to shape (2 cases)
539  if(verbose) cout << "\nChecking Shape(-2, 2) - invalid rows" << endl;
540  ierr = m1.Shape(-2, 2);
541  EPETRA_TEST_ERR(!(ierr == -1), returnierr);
542  if(ierr == -1)
543  if(verbose) cout << "Checked OK." << endl;
544  if(verbose) cout << "\nChecking Shape(3, -2) - invalid cols" << endl;
545  ierr = m1.Shape(3, -2);
546  EPETRA_TEST_ERR(!(ierr == -1), returnierr);
547  if(ierr == -1)
548  if(verbose) cout << "Checked OK." << endl;
549 
550  // invalid parameter to reshape (2 cases)
551  m1.Shape(5, 5);
552  if(verbose) cout << "\nChecking Reshape(-4, 3) - invalid rows" << endl;
553  ierr = m1.Reshape(-4, 3);
554  EPETRA_TEST_ERR(!(ierr == -1), returnierr);
555  if(ierr == -1)
556  if(verbose) cout << "Checked OK." << endl;
557  if(verbose) cout << "\nChecking Reshape(4, -3) - invalid cols" << endl;
558  ierr = m1.Reshape(4, -3);
559  EPETRA_TEST_ERR(!(ierr == -1), returnierr);
560  if(ierr == -1)
561  if(verbose) cout << "Checked OK." << endl;
562 
563 #ifdef HAVE_EPETRA_ARRAY_BOUNDS_CHECK // only test op() and op[] exceptions if macro is defined.
564  // out of range index to op() & op[] (6 cases)
565  int* rand16 = getRandArray(16);
566  Epetra_IntSerialDenseMatrix m2(View, rand16, 4, 4, 4);
567 
568  // op() too high
569  try {
570  caught = false;
571  if(verbose) cout << "\nChecking operator () - row index too high";
572  ierr = m2(5, 2);
573  }
574  catch(int error) {
575  caught = true;
576  EPETRA_TEST_ERR(error != -1, returnierr);
577  if(error == -1)
578  if(verbose) cout << "Checked OK." << endl;
579  }
580  EPETRA_TEST_ERR(!caught, returnierr);
581  try {
582  caught = false;
583  if(verbose) cout << "\nChecking operator () - col index too high";
584  ierr = m2(3, 4);
585  }
586  catch(int error) {
587  caught = true;
588  EPETRA_TEST_ERR(error != -2, returnierr);
589  if(error == -2)
590  if(verbose) cout << "Checked OK." << endl;
591  }
592  EPETRA_TEST_ERR(!caught, returnierr);
593 
594  // op() too low
595  try {
596  caught = false;
597  if(verbose) cout << "\nChecking operator () - row index too low";
598  ierr = m2(-1, 0);
599  }
600  catch(int error) {
601  caught = true;
602  EPETRA_TEST_ERR(error != -1, returnierr);
603  if(error == -1)
604  if(verbose) cout << "Checked OK." << endl;
605  }
606  EPETRA_TEST_ERR(!caught, returnierr);
607  try {
608  caught = false;
609  if(verbose) cout << "\nChecking operator () - col index too low";
610  ierr = m2(0, -1);
611  }
612  catch(int error) {
613  caught = true;
614  EPETRA_TEST_ERR(error != -2, returnierr);
615  if(error == -2)
616  if(verbose) cout << "Checked OK." << endl;
617  }
618  EPETRA_TEST_ERR(!caught, returnierr);
619 
620  // op[] too high
621  try {
622  caught = false;
623  if(verbose) cout << "\nChecking operator [] - col index too high";
624  ierr = m2[4][2];
625  }
626  catch(int error) {
627  caught = true;
628  EPETRA_TEST_ERR(error != -2, returnierr);
629  if(error == -2)
630  if(verbose) cout << "Checked OK." << endl;
631  }
632 
633  // op[] too low
634  try {
635  caught = false;
636  if(verbose) cout << "\nChecking operator [] - col index too low";
637  ierr = m2[-1][0];
638  }
639  catch(int error) {
640  caught = true;
641  EPETRA_TEST_ERR(error != -2, returnierr);
642  if(error == -2)
643  if(verbose) cout << "Checked OK." << endl;
644  }
645  EPETRA_TEST_ERR(!caught, returnierr);
646  delete [] rand16;
647 #endif // end of HAVE_EPETRA_ARRAY_BOUNDS_CHECK conditional
648 
649  // ISDM = ISDV
652  try {
653  caught = false;
654  if(verbose) cout << "\nChecking op = - assigning ISDV to ISDM";
655  m3 = v1;
656  }
657  catch(int error) {
658  caught = true;
659  EPETRA_TEST_ERR(error != -5, returnierr);
660  if(error == -5)
661  if(verbose) cout << "Checked OK." << endl;
662  }
663  EPETRA_TEST_ERR(!caught, returnierr);
664  if (matrix != NULL) delete matrix;
665  return(returnierr);
666 }
667 
668 //=========================================================================
669 // test matrix operator= (copy & view)
670 int matrixAssignment(bool verbose, bool debug) {
671  int ierr = 0;
672  int returnierr = 0;
673  if(verbose) printHeading("Testing matrix operator=");
674 
675  // each section is in its own block so we can reuse variable names
676  // lhs = left hand side, rhs = right hand side
677 
678  {
679  // copy->copy (more space needed)
680  // orig and dup should have same signature
681  // modifying orig or dup should have no effect on the other
682  if(verbose) cout << "Checking copy->copy (new alloc)" << endl;
684  int* rand1 = getRandArray(25);
685  Epetra_IntSerialDenseMatrix rhs(Copy, rand1, 5, 5, 5);
686  if(debug) {
687  cout << "before assignment:" << endl;
688  printMat("rhs",rhs);
689  printMat("lhs",lhs);
690  }
691  lhs = rhs;
692  if(debug) {
693  cout << "after assignment:" << endl;
694  printMat("rhs",rhs);
695  printMat("lhs",lhs);
696  }
697  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
698  EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
699  delete[] rand1;
700  }
701  returnierr += ierr;
702  if(ierr == 0)
703  if(verbose) cout << "Checked OK." << endl;
704  ierr = 0;
705  {
706  // copy->copy (have enough space)
707  // orig and dup should have same signature
708  // modifying orig or dup should have no effect on the other
709  if(verbose) cout << "\nChecking copy->copy (no alloc)" << endl;
710  int* rand1 = getRandArray(25);
711  int* rand2 = getRandArray(20);
712  Epetra_IntSerialDenseMatrix lhs(Copy, rand1, 5, 5, 5);
713  Epetra_IntSerialDenseMatrix rhs(Copy, rand2, 4, 4, 5);
714  int* origA = lhs.A();
715  int origLDA = lhs.LDA();
716  if(debug) {
717  cout << "before assignment:" << endl;
718  printMat("rhs",rhs);
719  printMat("lhs",lhs);
720  }
721  lhs = rhs;
722  if(debug) {
723  cout << "after assignment:" << endl;
724  printMat("rhs",rhs);
725  printMat("lhs",lhs);
726  }
727  // in this case, instead of doing a "normal" LDA test in identSig,
728  // we do our own. Since we had enough space already, A and LDA should
729  // not have been changed by the assignment. (The extra parameter to
730  // identicalSignatures tells it not to test LDA).
731  EPETRA_TEST_ERR((lhs.A() != origA) || (lhs.LDA() != origLDA), ierr);
732  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs,false), ierr);
733  EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
734  delete [] rand1;
735  delete [] rand2;
736  }
737  returnierr += ierr;
738  if(ierr == 0)
739  if(verbose) cout << "Checked OK." << endl;
740  ierr = 0;
741  {
742  // view->copy
743  // orig and dup should have same signature
744  // modifying orig or dup should have no effect on the other
745  if(verbose) cout << "\nChecking view->copy" << endl;
746  int* rand1 = getRandArray(25);
747  int* rand2 = getRandArray(64);
748  Epetra_IntSerialDenseMatrix lhs(View, rand1, 5, 5, 5);
749  Epetra_IntSerialDenseMatrix rhs(Copy, rand2, 8, 8, 8);
750  if(debug) {
751  cout << "before assignment:" << endl;
752  printMat("rhs",rhs);
753  printMat("lhs",lhs);
754  }
755  lhs = rhs;
756  if(debug) {
757  cout << "after assignment:" << endl;
758  printMat("rhs",rhs);
759  printMat("lhs",lhs);
760  }
761  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
762  EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
763  delete[] rand1;
764  delete[] rand2;
765  }
766  returnierr += ierr;
767  if(ierr == 0)
768  if(verbose) cout << "Checked OK." << endl;
769  ierr = 0;
770  {
771  // copy->view
772  // orig and dup should have same signature
773  // modifying orig or dup should change the other
774  if(verbose) cout << "\nChecking copy->view" << endl;
775  int* rand1 = getRandArray(10);
777  Epetra_IntSerialDenseMatrix rhs(View, rand1, 2, 2, 5);
778  if(debug) {
779  cout << "before assignment:" << endl;
780  printMat("rhs",rhs);
781  printMat("lhs",lhs);
782  }
783  lhs = rhs;
784  if(debug) {
785  cout << "after assignment:" << endl;
786  printMat("rhs",rhs);
787  printMat("lhs",lhs);
788  }
789  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
790  EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
791  delete[] rand1;
792  }
793  returnierr += ierr;
794  if(ierr == 0)
795  if(verbose) cout << "Checked OK." << endl;
796  ierr = 0;
797  {
798  // view->view
799  // orig and dup should have same signature
800  // modifying orig or dup should change the other
801  if(verbose) cout << "\nChecking view->view" << endl;
802  int* rand1 = getRandArray(9);
803  int* rand2 = getRandArray(18);
804  Epetra_IntSerialDenseMatrix lhs(View, rand1, 3, 3, 3);
805  Epetra_IntSerialDenseMatrix rhs(View, rand2, 3, 3, 6);
806  if(debug) {
807  cout << "before assignment:" << endl;
808  printMat("rhs",rhs);
809  printMat("lhs",lhs);
810  }
811  lhs = rhs;
812  if(debug) {
813  cout << "after assignment:" << endl;
814  printMat("rhs",rhs);
815  printMat("lhs",lhs);
816  }
817  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
818  EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
819  delete[] rand1;
820  delete[] rand2;
821  }
822  returnierr += ierr;
823  if(ierr == 0)
824  if(verbose) cout << "Checked OK." << endl;
825  ierr = 0;
826  {
827  // test MakeViewOf
828  // orig and dup should have same signature except for CV_
829  // modifying orig or dup should change the other
830  if(verbose) cout << "\nChecking CrsGraph's usage of MakeViewOf" << endl;
831  int* rand1 = getRandArray(10);
832  int* rand2 = getRandArray(10);
833  Epetra_IntSerialDenseMatrix lhs(Copy, rand1, 5, 5, 2);
834  Epetra_IntSerialDenseMatrix rhs(Copy, rand2, 5, 5, 2);
835  if(debug) {
836  cout << "before assignment:" << endl;
837  printMat("rhs",rhs);
838  printMat("lhs",lhs);
839  }
840  lhs.MakeViewOf(rhs);
841  if(debug) {
842  cout << "after assignment:" << endl;
843  printMat("rhs",rhs);
844  printMat("lhs",lhs);
845  }
846  EPETRA_TEST_ERR(!(lhs.CV() == View), ierr);
847  EPETRA_TEST_ERR(!(lhs.M() == rhs.M()), ierr);
848  EPETRA_TEST_ERR(!(lhs.N() == rhs.N()), ierr);
849  EPETRA_TEST_ERR(!(lhs.LDA() == rhs.LDA()), ierr);
850  EPETRA_TEST_ERR(!(lhs.A() == rhs.A()), ierr);
851  EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
852  delete[] rand1;
853  delete[] rand2;
854  }
855  returnierr += ierr;
856  if(ierr == 0)
857  if(verbose) cout << "Checked OK." << endl;
858  ierr = 0;
859 
860  return(returnierr);
861 }
862 
863 //=========================================================================
864 //=========================================================================
865 // Vector-testing functions
866 //=========================================================================
867 //=========================================================================
868 // call functions we don't have tests for, for sake of code coverage.
869 // (the functions are called, but their output isn't checked for correctness).
870 int vectorCoverage(bool verbose, bool debug) {
871  if(verbose) printHeading("Testing other vector functions");
872 
873  int* rand1 = getRandArray(9);
874  if(debug) printArray(rand1, 9);
875  Epetra_IntSerialDenseVector v1(Copy, rand1, 9);
876  if(debug) printMat("v1",v1);
877  delete[] rand1;
878 
879  if(verbose) cout << "calling one norm" << endl;
880  int onenorm = v1.OneNorm();
881  if(debug) cout << "v1.OneNorm() = " << onenorm << endl;
882 
883  if(verbose) cout << "calling infinity norm" << endl;
884  int infnorm = v1.InfNorm();
885  if(debug) cout << "v1.InfNorm() = " << infnorm << endl;
886 
887  if(verbose) cout << "calling random" << endl;
889  if(debug) printMat("v2 (before)",v2);
890  v2.Random();
891  if(debug) printMat("v2 (after)",v2);
892 
893  if(verbose) cout << "Checked OK (I think)." << endl;
894 
895  return(0);
896 }
897 //=========================================================================
898 // test vector default constructor, user constructor (copy & view),
899 // () [] operators (read & write), size (larger), resize (smaller)
900 int vectorCtr(bool verbose, bool debug) {
901  const int v1size = 5;
902  const int v1asize = 15;
903  const int v2size = 10;
904  const int v3size = 8;
905  const int v3resize = 5; // should be smaller than v3size
906 
907  int ierr = 0;
908  int returnierr = 0;
909  if(verbose) printHeading("Testing vector constructors");
910 
911  if(verbose) cout << "default constructor" << endl;
913  EPETRA_TEST_ERR(!(v1.CV() == Copy), ierr);
914  if(verbose) cout << "sizing" << endl;
915  v1.Size(v1size);
916  EPETRA_TEST_ERR(!(v1.Length() == v1size), ierr);
917  for(int i = 0; i < v1size; i++) {
918  EPETRA_TEST_ERR(!(v1(i) == 0), ierr);
919  }
920  if(debug) printMat("v1",v1);
921  returnierr += ierr;
922  if(ierr == 0)
923  if(verbose) cout << "Checked OK." << endl;
924  ierr = 0;
925  if(verbose) cout << "\nmanually setting values" << endl;
926  int* v1rand = getRandArray(v1size);
927  for(int i = 0; i < v1size; i++)
928  v1(i) = v1rand[i];
929  for(int i = 0; i < v1size; i++)
930  EPETRA_TEST_ERR(!(v1[i] == v1rand[i]), ierr);
931  if(debug) {
932  printArray(v1rand, v1size);
933  printMat("v1",v1);
934  }
935  delete[] v1rand;
936  returnierr += ierr;
937  if(ierr == 0)
938  if(verbose) cout << "Checked OK." << endl;
939  ierr = 0;
940 
941  if(verbose) cout << "\nsized constructor" << endl;
942  Epetra_IntSerialDenseVector v1a(v1asize);
943  EPETRA_TEST_ERR(!(v1a.Length() == v1asize), ierr);
944  EPETRA_TEST_ERR(!(v1a.CV() == Copy),ierr);
945  if(debug) printMat("v1a", v1a);
946  returnierr += ierr;
947  if(ierr == 0)
948  if(verbose) cout << "Checked OK." << endl;
949  ierr = 0;
950 
951  if(verbose) cout << "\nuser data constructor (view)" << endl;
952  int* v2rand = getRandArray(v2size);
953  if(debug) printArray(v2rand, v2size);
954  Epetra_IntSerialDenseVector v2(View, v2rand, v2size);
955  EPETRA_TEST_ERR(!(v2.CV() == View), ierr);
956  EPETRA_TEST_ERR(!(v2.Length() == v2size), ierr);
957  EPETRA_TEST_ERR(!(v2.Values() == v2rand), ierr);
958  if(debug) printMat("v2",v2);
959  returnierr += ierr;
960  if(ierr == 0)
961  if(verbose) cout << "Checked OK." << endl;
962  ierr = 0;
963 
964  if(verbose) cout << "\nchanging value, checking for correct behavior" << endl;
965  int* v2randcopy = new int[v2size]; // backup of original values
966  for(int i = 0; i < v2size; i++)
967  v2randcopy[i] = v2rand[i];
968  v2(4) = v2(4) + 1; // magic number for which element to change
969  v2randcopy[4] = v2randcopy[4] +1;
970  for(int i = 0; i < v2size; i++)
971  EPETRA_TEST_ERR(!(v2rand[i] == v2randcopy[i]), ierr); // v2rand should have updated correctly
972  if(debug) {
973  printArray(v2rand, v2size);
974  printMat("v2",v2);
975  }
976  delete[] v2rand;
977  delete[] v2randcopy;
978  returnierr += ierr;
979  if(ierr == 0)
980  if(verbose) cout << "Checked OK." << endl;
981  ierr = 0;
982 
983  if(verbose) cout << "\nuser data constructor (copy)" << endl;
984  int* v3rand = getRandArray(v3size);
985  if(debug) printArray(v3rand, v3size);
986  int* v3randcopy = new int[v3size];
987  for(int i = 0; i < v3size; i++)
988  v3randcopy[i] = v3rand[i];
989  Epetra_IntSerialDenseVector v3(Copy, v3rand, v3size);
990  if(debug) printMat("v3",v3);
991  if(verbose) cout << "checking to see if data initialized correctly" << endl;
992  EPETRA_TEST_ERR(!(v3.CV() == Copy), ierr);
993  EPETRA_TEST_ERR(!(v3.Length() == v3size), ierr);
994  EPETRA_TEST_ERR(!(v3.Values() != v3rand), ierr); // should not be a view
995  for(int i = 0; i < v3size; i++)
996  EPETRA_TEST_ERR(!(v3[i] == v3rand[i]), ierr); // data should be identical to user array
997  returnierr += ierr;
998  if(ierr == 0)
999  if(verbose) cout << "Checked OK." << endl;
1000  ierr = 0;
1001 
1002  if(verbose) cout << "\nmodifying entry" << endl;
1003  v3[5] = v3[5] + 3; // magic number for which element to change
1004  for(int i = 0; i < v3size; i++)
1005  EPETRA_TEST_ERR(!(v3rand[i] == v3randcopy[i]), ierr); // user array should be unchanged
1006  v3rand[5] = v3rand[5] + 3; // same magic modification performed to user's array
1007  for(int i = 0; i < v3size; i++)
1008  EPETRA_TEST_ERR(!(v3[i] == v3rand[i]), ierr); // should equal user array with same modification performed
1009  if(debug) {
1010  printArray(v3rand, v3size);
1011  printMat("v3",v3);
1012  }
1013  returnierr += ierr;
1014  if(ierr == 0)
1015  if(verbose) cout << "Checked OK." << endl;
1016  ierr = 0;
1017 
1018  if(verbose) cout << "\nresizing" << endl;
1019  v3.Resize(v3resize);
1020  EPETRA_TEST_ERR(!(v3.Length() == v3resize), ierr);
1021  for(int i = 0; i < v3resize; i++)
1022  EPETRA_TEST_ERR(!(v3[i] == v3rand[i]),ierr);
1023  if(debug) printMat("v3",v3);
1024  delete[] v3rand;
1025  delete[] v3randcopy;
1026  returnierr += ierr;
1027  if(ierr == 0)
1028  if(verbose) cout << "Checked OK." << endl;
1029  ierr = 0;
1030 
1031  if(verbose) cout << "\nChecking pointer on zero-sized vector" << endl;
1032  int* before = v3.Values();
1033  if(verbose) cout << "Resizing to 0" << endl;
1034  if(debug) cout << "Before = " << before << endl;
1035  EPETRA_TEST_ERR(!(before != 0), ierr);
1036  v3.Resize(0);
1037  int* after = v3.Values();
1038  EPETRA_TEST_ERR(!(after == 0), ierr);
1039  if(debug) cout << "After = " << after << endl;
1040  v3.Size(3);
1041  before = v3.Values();
1042  if(verbose) cout << "Sizing to 0" << endl;
1043  if(debug) cout << "Before = " << before << endl;
1044  EPETRA_TEST_ERR(!(before != 0), ierr);
1045  v3.Size(0);
1046  after = v3.Values();
1047  EPETRA_TEST_ERR(!(after == 0), ierr);
1048  if(debug) cout << "After = " << after << endl;
1049  returnierr += ierr;
1050  if(ierr == 0)
1051  if(verbose) cout << "Checked OK." << endl;
1052  ierr = 0;
1053 
1054  return(returnierr);
1055 }
1056 //=========================================================================
1057 // test vector copy constructor (copy & view)
1058 int vectorCpyCtr(bool verbose, bool debug) {
1059  const int v1size = 15;
1060  const int v2size = 12;
1061 
1062  int ierr = 0;
1063  int returnierr = 0;
1064  if(verbose) printHeading("Testing vector copy constructors");
1065 
1066  if(verbose) cout << "checking copy constructor (view)" << endl;
1067  int* v1rand = getRandArray(v1size);
1068  if(debug) printArray(v1rand, v1size);
1069  Epetra_IntSerialDenseVector v1(View, v1rand, v1size);
1070  if(debug) {
1071  cout << "original vector:" << endl;
1072  printMat("v1",v1);
1073  }
1074  Epetra_IntSerialDenseVector v1clone(v1);
1075  if(debug) {
1076  cout << "clone vector:" << endl;
1077  printMat("v1clone",v1clone);
1078  }
1079  if(verbose) cout << "making sure signatures match" << endl;
1080  EPETRA_TEST_ERR(!identicalSignatures(v1, v1clone), ierr);
1081  delete[] v1rand;
1082  returnierr += ierr;
1083  if(ierr == 0)
1084  if(verbose) cout << "Checked OK." << endl;
1085  ierr = 0;
1086 
1087  if(verbose) cout << "\nchecking copy constructor (copy)" << endl;
1088  int* v2rand = getRandArray(v2size);
1089  if(debug) printArray(v2rand, v2size);
1090  Epetra_IntSerialDenseVector v2(Copy, v2rand, v2size);
1091  if(debug) {
1092  cout << "original vector:" << endl;
1093  printMat("v2",v2);
1094  }
1095  Epetra_IntSerialDenseVector v2clone(v2);
1096  if(debug) {
1097  cout << "clone vector:" << endl;
1098  printMat("v2clone",v2clone);
1099  }
1100  if(verbose) cout << "checking that signatures match" << endl;
1101  EPETRA_TEST_ERR(!identicalSignatures(v2, v2clone), ierr);
1102  returnierr += ierr;
1103  if(ierr == 0)
1104  if(verbose) cout << "Checked OK." << endl;
1105  ierr = 0;
1106 
1107  if(verbose) cout << "\nmodifying entry in v2, v2clone should be unchanged" << endl;
1108  EPETRA_TEST_ERR(!seperateData(v2, v2clone), ierr);
1109  if(debug) {
1110  printArray(v2rand, v2size);
1111  cout << "orig:" << endl;
1112  printMat("v2",v2);
1113  cout << "clone:" << endl;
1114  printMat("v2clone",v2clone);
1115  }
1116  delete[] v2rand;
1117  returnierr += ierr;
1118  if(ierr == 0)
1119  if(verbose) cout << "Checked OK." << endl;
1120  ierr = 0;
1121 
1122  return(returnierr);
1123 }
1124 //=========================================================================
1125 // test vector operator= (copy & view)
1126 int vectorAssignment(bool verbose, bool debug) {
1127  int ierr = 0;
1128  int returnierr = 0;
1129  if(verbose) printHeading("Testing vector operator=");
1130 
1131  // each section is in its own block so we can reuse variable names
1132  // lhs = left hand side, rhs = right hand side
1133 
1134  {
1135  // copy->copy (more space needed)
1136  // orig and dup should have same signature
1137  // modifying orig or dup should have no effect on the other
1138  if(verbose) cout << "Checking copy->copy (new alloc)" << endl;
1140  int* rand1 = getRandArray(10);
1141  Epetra_IntSerialDenseVector rhs(Copy, rand1, 10);
1142  if(debug) {
1143  cout << "before assignment:" << endl;
1144  printMat("rhs",rhs);
1145  printMat("lhs",lhs);
1146  }
1147  lhs = rhs;
1148  if(debug) {
1149  cout << "after assignment:" << endl;
1150  printMat("rhs",rhs);
1151  printMat("lhs",lhs);
1152  }
1153  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
1154  EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
1155  delete[] rand1;
1156  }
1157  returnierr += ierr;
1158  if(ierr == 0)
1159  if(verbose) cout << "Checked OK." << endl;
1160  ierr = 0;
1161  {
1162  // copy->copy (have enough space)
1163  // orig and dup should have same signature
1164  // modifying orig or dup should have no effect on the other
1165  if(verbose) cout << "\nChecking copy->copy (no alloc)" << endl;
1166  int* rand1 = getRandArray(20);
1167  int* rand2 = getRandArray(15);
1168  Epetra_IntSerialDenseVector lhs(Copy, rand1, 20);
1169  Epetra_IntSerialDenseVector rhs(Copy, rand2, 15);
1170  int* origA = lhs.A();
1171  int origLDA = lhs.LDA();
1172  if(debug) {
1173  cout << "before assignment:" << endl;
1174  printMat("rhs",rhs);
1175  printMat("lhs",lhs);
1176  }
1177  lhs = rhs;
1178  if(debug) {
1179  cout << "after assignment:" << endl;
1180  printMat("rhs",rhs);
1181  printMat("lhs",lhs);
1182  }
1183  // in this case, instead of doing a "normal" LDA test in identSig,
1184  // we do our own. Since we had enough space already, A and LDA should
1185  // not have been changed by the assignment. (The extra parameter to
1186  // identicalSignatures tells it not to test LDA).
1187  EPETRA_TEST_ERR((lhs.A() != origA) || (lhs.LDA() != origLDA), ierr);
1188  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs,false), ierr);
1189  EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
1190  delete[] rand1;
1191  delete[] rand2;
1192  }
1193  returnierr += ierr;
1194  if(ierr == 0)
1195  if(verbose) cout << "Checked OK." << endl;
1196  ierr = 0;
1197  {
1198  // view->copy
1199  // orig and dup should have same signature
1200  // modifying orig or dup should have no effect on the other
1201  if(verbose) cout << "\nChecking view->copy" << endl;
1202  int* rand1 = getRandArray(5);
1203  int* rand2 = getRandArray(8);
1204  Epetra_IntSerialDenseVector lhs(View, rand1, 5);
1205  Epetra_IntSerialDenseVector rhs(Copy, rand2, 8);
1206  if(debug) {
1207  cout << "before assignment:" << endl;
1208  printMat("rhs",rhs);
1209  printMat("lhs",lhs);
1210  }
1211  lhs = rhs;
1212  if(debug) {
1213  cout << "after assignment:" << endl;
1214  printMat("rhs",rhs);
1215  printMat("lhs",lhs);
1216  }
1217  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
1218  EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
1219  delete[] rand1;
1220  delete[] rand2;
1221  }
1222  returnierr += ierr;
1223  if(ierr == 0)
1224  if(verbose) cout << "Checked OK." << endl;
1225  ierr = 0;
1226  {
1227  // copy->view
1228  // orig and dup should have same signature
1229  // modifying orig or dup should change the other
1230  if(verbose) cout << "\nChecking copy->view" << endl;
1231  int* rand1 = getRandArray(10);
1233  Epetra_IntSerialDenseVector rhs(View, rand1, 10);
1234  if(debug) {
1235  cout << "before assignment:" << endl;
1236  printMat("rhs",rhs);
1237  printMat("lhs",lhs);
1238  }
1239  lhs = rhs;
1240  if(debug) {
1241  cout << "after assignment:" << endl;
1242  printMat("rhs",rhs);
1243  printMat("lhs",lhs);
1244  }
1245  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
1246  EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
1247  delete[] rand1;
1248  }
1249  returnierr += ierr;
1250  if(ierr == 0)
1251  if(verbose) cout << "Checked OK." << endl;
1252  ierr = 0;
1253  {
1254  // view->view
1255  // orig and dup should have same signature
1256  // modifying orig or dup should change the other
1257  if(verbose) cout << "\nChecking view->view" << endl;
1258  int* rand1 = getRandArray(9);
1259  int* rand2 = getRandArray(11);
1260  Epetra_IntSerialDenseVector lhs(View, rand1, 9);
1261  Epetra_IntSerialDenseVector rhs(View, rand2, 11);
1262  if(debug) {
1263  cout << "before assignment:" << endl;
1264  printMat("rhs",rhs);
1265  printMat("lhs",lhs);
1266  }
1267  lhs = rhs;
1268  if(debug) {
1269  cout << "after assignment:" << endl;
1270  printMat("rhs",rhs);
1271  printMat("lhs",lhs);
1272  }
1273  EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
1274  EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
1275  delete[] rand1;
1276  delete[] rand2;
1277  }
1278  returnierr += ierr;
1279  if(ierr == 0)
1280  if(verbose) cout << "Checked OK." << endl;
1281  ierr = 0;
1282  {
1283  // test MakeViewOf
1284  // orig and dup should have same signature except for CV_
1285  // modifying orig or dup should change the other
1286  if(verbose) cout << "\nChecking CrsGraph's usage of MakeViewOf" << endl;
1287  int* rand1 = getRandArray(10);
1288  int* rand2 = getRandArray(10);
1289  Epetra_IntSerialDenseVector lhs(Copy, rand1, 10);
1290  Epetra_IntSerialDenseVector rhs(Copy, rand2, 10);
1291  if(debug) {
1292  cout << "before assignment:" << endl;
1293  printMat("rhs",rhs);
1294  printMat("lhs",lhs);
1295  }
1296  lhs.MakeViewOf(rhs);
1297  if(debug) {
1298  cout << "after assignment:" << endl;
1299  printMat("rhs",rhs);
1300  printMat("lhs",lhs);
1301  }
1302  EPETRA_TEST_ERR(!(lhs.CV() == View), ierr);
1303  EPETRA_TEST_ERR(!(lhs.M() == rhs.M()), ierr);
1304  EPETRA_TEST_ERR(!(lhs.N() == rhs.N()), ierr);
1305  EPETRA_TEST_ERR(!(lhs.LDA() == rhs.LDA()), ierr);
1306  EPETRA_TEST_ERR(!(lhs.A() == rhs.A()), ierr);
1307  EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
1308  delete[] rand1;
1309  delete[] rand2;
1310  }
1311  returnierr += ierr;
1312  if(ierr == 0)
1313  if(verbose) cout << "Checked OK." << endl;
1314  ierr = 0;
1315 
1316  return(returnierr);
1317 }
1318 
1319 //=========================================================================
1320 // test vector error-reporting
1321 int vectorExceptions(bool verbose, bool debug) {
1322  int returnierr = 0;
1323  int ierr = 0;
1324  bool caught = false;
1326  vector = NULL;
1327 
1328  if(verbose) printHeading("Testing vector error-reporting.\nExpect error messages if EPETRA_NO_ERROR_REPORTS is not defined.");
1329 
1330  try { // invalid dimension to sized ctr
1331  caught = false;
1332  if(verbose) cout << "Checking Epetra_IntSerialDenseVector(-1)";
1333  vector = new Epetra_IntSerialDenseVector(-1);
1334  }
1335  catch(int error) {
1336  caught = true;
1337  EPETRA_TEST_ERR(error != -1, returnierr);
1338  if(error == -1)
1339  if(verbose) cout << "Checked OK." << endl;
1340  }
1341  EPETRA_TEST_ERR(!caught, returnierr);
1342 
1343  int* rand2 = getRandArray(2);
1344  try { // invalid dimension to user-data ctr
1345  caught = false;
1346  if(verbose) cout << "\nChecking Epetra_IntSerialDenseVector(Copy, int*, -3)";
1347  vector = new Epetra_IntSerialDenseVector(Copy, rand2, -3);
1348  }
1349  catch(int error) {
1350  caught = true;
1351  EPETRA_TEST_ERR(error != -1, returnierr);
1352  if(error == -1)
1353  if(verbose) cout << "Checked OK." << endl;
1354  }
1355  EPETRA_TEST_ERR(!caught, returnierr);
1356  delete[] rand2;
1357 
1358  try { // null pointer to user-data ctr
1359  caught = false;
1360  if(verbose) cout << "\nChecking Epetra_IntSerialDenseVector(Copy, 0, 5)";
1361  vector = new Epetra_IntSerialDenseVector(Copy, 0, 5);
1362  }
1363  catch(int error) {
1364  caught = true;
1365  EPETRA_TEST_ERR(error != -3, returnierr);
1366  if(error == -3)
1367  if(verbose) cout << "Checked OK." << endl;
1368  }
1369  EPETRA_TEST_ERR(!caught, returnierr);
1370 
1371  // invalid parameter to size
1372  if(verbose) cout << "\nChecking Size(-2)" << endl;
1374  ierr = v1.Size(-2);
1375  EPETRA_TEST_ERR(!(ierr == -1), returnierr);
1376  if(ierr == -1)
1377  if(verbose) cout << "Checked OK." << endl;
1378 
1379  // invalid parameter to resize
1380  if(verbose) cout << "\nChecking Resize(-4)" << endl;
1381  v1.Size(5);
1382  ierr = v1.Resize(-4);
1383  EPETRA_TEST_ERR(!(ierr == -1), returnierr);
1384  if(ierr == -1)
1385  if(verbose) cout << "Checked OK." << endl;
1386 
1387 #ifdef HAVE_EPETRA_ARRAY_BOUNDS_CHECK // only test op() and op[] exceptions if macro is defined.
1388  // out of range index to op() & op[]
1389  int* rand17 = getRandArray(17);
1390  Epetra_IntSerialDenseVector v2(View, rand17, 17);
1391  try { // op() too high
1392  caught = false;
1393  if(verbose) cout << "\nChecking operator () - index too high";
1394  ierr = v2(17);
1395  }
1396  catch(int error) {
1397  caught = true;
1398  EPETRA_TEST_ERR(error != -1, returnierr);
1399  if(error == -1)
1400  if(verbose) cout << "Checked OK." << endl;
1401  }
1402  EPETRA_TEST_ERR(!caught, returnierr);
1403 
1404  try { // op() too low
1405  caught = false;
1406  if(verbose) cout << "\nChecking operator () - index too low";
1407  ierr = v2(-1);
1408  }
1409  catch(int error) {
1410  caught = true;
1411  EPETRA_TEST_ERR(error != -1, returnierr);
1412  if(error == -1)
1413  if(verbose) cout << "Checked OK." << endl;
1414  }
1415  EPETRA_TEST_ERR(!caught, returnierr);
1416 
1417  try { // op[] too high
1418  caught = false;
1419  if(verbose) cout << "\nChecking operator [] - index too high";
1420  ierr = v2[17];
1421  }
1422  catch(int error) {
1423  caught = true;
1424  EPETRA_TEST_ERR(error != -1, returnierr);
1425  if(error == -1)
1426  if(verbose) cout << "Checked OK." << endl;
1427  }
1428  EPETRA_TEST_ERR(!caught, returnierr);
1429 
1430  try { // op[] too low
1431  caught = false;
1432  if(verbose) cout << "\nChecking operator [] - index too low";
1433  ierr = v2[-1];
1434  }
1435  catch(int error) {
1436  caught = true;
1437  EPETRA_TEST_ERR(error != -1, returnierr);
1438  if(error == -1)
1439  if(verbose) cout << "Checked OK." << endl;
1440  }
1441  EPETRA_TEST_ERR(!caught, returnierr);
1442  delete[] rand17;
1443 #endif // end of HAVE_EPETRA_ARRAY_BOUNDS_CHECK conditional
1444 
1445  // we don't need to check for ISDV = ISDM, as that is a compile-time error
1446  if (vector != NULL) delete vector;
1447  return(returnierr);
1448 }
1449 
1450 //=========================================================================
1451 //=========================================================================
1452 // helper functions
1453 //=========================================================================
1454 //=========================================================================
1455 // checks the signatures of two matrices
1457  /*cout << "M: " << a.M() << " " << b.M() << endl;
1458  cout << "N: " << a.N() << " " << b.N() << endl;
1459  cout << "LDA: " << a.LDA() << " " << b.LDA() << endl;
1460  cout << "CV: " << a.CV() << " " << b.CV() << endl;
1461  cout << "A: " << a.A() << " " << b.A() << endl;*/
1462 
1463  if((a.M() != b.M() )|| // check properties first
1464  (a.N() != b.N() )||
1465  (a.CV() != b.CV() ))
1466  return(false);
1467 
1468  if(testLDA == true) // if we are coming from op= c->c #2 (have enough space)
1469  if(a.LDA() != b.LDA()) // then we don't check LDA (but we do check it in the test function)
1470  return(false);
1471 
1472  if(a.CV() == View) { // if we're still here, we need to check the data
1473  if(a.A() != b.A()) // for a view, this just means checking the pointers
1474  return(false); // for a copy, this means checking each element
1475  }
1476  else { // CV == Copy
1477  const int m = a.M();
1478  const int n = a.N();
1479  for(int i = 0; i < m; i++)
1480  for(int j = 0; j < n; j++) {
1481  if(a(i,j) != b(i,j))
1482  return(false);
1483  }
1484  }
1485 
1486  return(true); // if we're still here, signatures are identical
1487 }
1488 //=========================================================================
1489 // checks if two matrices are independent or not
1491  bool seperate;
1492 
1493  int r = EPETRA_MIN(a.M(),b.M()) / 2; // ensures (r,c) is valid
1494  int c = EPETRA_MIN(a.N(),b.N()) / 2; // in both matrices
1495 
1496  int orig_a = a(r,c);
1497  int new_value = a(r,c) + 1;
1498  if(b(r,c) == new_value) // there's a chance b could be independent, but
1499  new_value++; // already have new_value in (r,c).
1500 
1501  a(r,c) = new_value;
1502  if(b(r,c) == new_value)
1503  seperate = false;
1504  else
1505  seperate = true;
1506 
1507  a(r,c) = orig_a; // undo change we made to a
1508 
1509  return(seperate);
1510 }
1511 //=========================================================================
1512 // returns a int* array of a given length, with random values on interval [0,100]
1513 int* getRandArray(int length) {
1514  int* array = new int[length];
1515  for(int i = 0; i < length; i++)
1516  array[i] = randomInt();
1517 
1518  return(array);
1519 }
1520 //=========================================================================
1521 // returns a random integer on the interval [0-maxint).
1522 // this is the same generator used in IntSerialDenseMatrix
1523 int randomInt() {
1524  const int maxint = 100;
1525 
1526  const double a = 16807.0;
1527  const double BigInt = 2147483647.0;
1528  double seed = rand(); // Use POSIX standard random function;
1529 
1530  seed = fmod(a * seed, BigInt); // fmod returns remainder of floating point division
1531  // (a * seed) - (floor(a * seed / BigInt) * BigInt)
1532  double randdouble = (seed / BigInt); // should be [0,1)
1533  int randint = int(randdouble * maxint);
1534 
1535  return(randint);
1536 }
1537 //=========================================================================
1538 // prints int* array with formatting
1539 void printArray(int* array, int length) {
1540  cout << "user array (size " << length << "): ";
1541  for(int i = 0; i < length; i++)
1542  cout << array[i] << " ";
1543  cout << endl;
1544 }
1545 //=========================================================================
1546 // prints IntSerialDenseMatrix/Vector with formatting
1547 void printMat(const char* name, Epetra_IntSerialDenseMatrix& matrix) {
1548  //cout << "--------------------" << endl;
1549  cout << "*** " << name << " ***" << endl;
1550  cout << matrix;
1551  //cout << "--------------------" << endl;
1552 }
1553 
1554 //=========================================================================
1555 // prints section heading with spacers/formatting
1556 void printHeading(const char* heading) {
1557  cout << "\n==================================================================\n";
1558  cout << heading << endl;
1559  cout << "==================================================================\n";
1560 }
int MakeViewOf(const Epetra_IntSerialDenseVector &Source)
Reset an existing IntSerialDenseVector to point to another Vector.
Epetra_IntSerialDenseVector: A class for constructing and using dense vectors.
int vectorCpyCtr(bool verbose, bool debug)
int vectorAssignment(bool verbose, bool debug)
int matrixCoverage(bool verbose, bool debug)
void printMat(const char *name, Epetra_IntSerialDenseMatrix &matrix)
int matrixAssignment(bool verbose, bool debug)
int Resize(int Length_in)
Resize a Epetra_IntSerialDenseVector object.
int Length() const
Returns length of vector.
int Shape(int NumRows, int NumCols)
Set dimensions of a Epetra_IntSerialDenseMatrix object; init values to zero.
int matrixCtr(bool verbose, bool debug)
Epetra_DataAccess CV() const
Returns the data access mode of the this vector.
int N() const
Returns column dimension of system.
#define EPETRA_TEST_ERR(a, b)
int vectorCtr(bool verbose, bool debug)
int Size(int Length_in)
Set length of a Epetra_IntSerialDenseVector object; init values to zero.
static void SetTracebackMode(int TracebackModeValue)
Set the value of the Epetra_Object error traceback report mode.
Epetra_DataAccess CV() const
Returns the data access mode of the this matrix.
bool seperateData(Epetra_IntSerialDenseMatrix &a, Epetra_IntSerialDenseMatrix &b)
#define EPETRA_MIN(x, y)
int LDA() const
Returns the leading dimension of the this matrix.
int MyPID() const
Return my process ID.
Epetra_MpiComm: The Epetra MPI Communication Class.
std::string Epetra_Version()
int matrixExceptions(bool verbose, bool debug)
void printHeading(const char *heading)
const int * A() const
Returns const pointer to the this matrix.
int M() const
Returns row dimension of system.
int Random()
Set matrix values to random numbers.
int matrixCpyCtr(bool verbose, bool debug)
int vectorExceptions(bool verbose, bool debug)
int Random()
Set vector values to random numbers.
void printArray(int *array, int length)
Epetra_IntSerialDenseMatrix: A class for constructing and using general dense integer matrices...
std::string error
Epetra_SerialComm: The Epetra Serial Communication Class.
virtual int InfNorm()
Computes the Infinity-Norm of the this matrix.
int Reshape(int NumRows, int NumCols)
Reshape a Epetra_IntSerialDenseMatrix object.
int main(int argc, char *argv[])
int vectorCoverage(bool verbose, bool debug)
virtual int OneNorm()
Computes the 1-Norm of the this matrix.
int * Values()
Returns pointer to the values in vector.
int * getRandArray(int length)
bool identicalSignatures(Epetra_IntSerialDenseMatrix &a, Epetra_IntSerialDenseMatrix &b, bool testLDA=true)
int MakeViewOf(const Epetra_IntSerialDenseMatrix &Source)
Reset an existing IntSerialDenseMatrix to point to another Matrix.
int n
Epetra_DataAccess Mode enumerable type.