test_exc.cc
Go to the documentation of this file.
00001 #define WANT_STREAM
00002 
00003 #include "newmatap.h"
00004 #include "newmatio.h"              // to help namespace with VC++ 5
00005 
00006 #ifdef use_namespace
00007 using namespace RBD_LIBRARIES;
00008 #endif
00009 
00010 //#include <except.h>             // if you want to use set_terminate
00011 
00012 /**************************** test exceptions ******************************/
00013 
00014 
00015 int main()
00016 {
00017    // activate the next expression if you want to use compiler supported
00018    // exceptions and you want Terminate to catch uncaught exceptions
00019    // set_terminate(Terminate);
00020    Real* s1; Real* s2; Real* s3; Real* s4;
00021    // Forces cout to allocate memory at beginning
00022    cout << "\nThis tests the exception system, so you will get\n" <<
00023       "a long list of error messages\n\n";
00024    cout << "\nPrint a real number (may help lost memory test): " << 3.14159265 << "\n";
00025    // Throw exception to set up exception buffer
00026    Try { Throw(BaseException("Just a dummy\n")); }
00027    CatchAll {};
00028    { Matrix A1(40,200); s1 = A1.Store(); }
00029    { Matrix A1(1,1); s3 = A1.Store(); }
00030    {
00031       Tracer et("Test");
00032 
00033       Try
00034       {
00035          Tracer et("Try block");
00036 
00037 
00038 
00039          cout << "-----------------------------------------\n\n";
00040          Matrix A(2,3), B(4,5); A = 1; B = 2;
00041          cout << "Incompatible dimensions\n";
00042          et.ReName("Block A");
00043          Try { Matrix C = A + B; }
00044          CatchAll { cout << BaseException::what() << endl; }
00045          cout << "-----------------------------------------\n\n";
00046 
00047          cout << "Bad index\n";
00048          et.ReName("Block B");
00049          Try { Real f = A(3,3); cout << f << endl; }
00050          CatchAll { cout << BaseException::what() << endl; }
00051          cout << "-----------------------------------------\n\n";
00052 
00053          cout << "Illegal conversion\n";
00054          et.ReName("Block C");
00055          Try { UpperTriangularMatrix U = A; }
00056          CatchAll { cout << BaseException::what() << endl; }
00057          cout << "-----------------------------------------\n\n";
00058 
00059          cout << "Invert non-square matrix - 1\n";
00060          et.ReName("Block D");
00061          Try { CroutMatrix X = A; }
00062          CatchAll { cout << BaseException::what() << endl; }
00063          cout << "-----------------------------------------\n\n";
00064 
00065          cout << "Invert non-square matrix - 2\n";
00066          et.ReName("Block E");
00067          Try { Matrix X = A.i(); }
00068          CatchAll { cout << BaseException::what() << endl; }
00069          cout << "-----------------------------------------\n\n";
00070 
00071          cout << "Non 1x1 matrix to scalar\n";
00072          et.ReName("Block F");
00073          Try { Real f = A.AsScalar(); cout << f << endl; }
00074          CatchAll { cout << BaseException::what() << endl; }
00075          cout << "-----------------------------------------\n\n";
00076 
00077          cout << "Matrix to vector\n";
00078          et.ReName("Block G");
00079          Try { ColumnVector CV = A;}
00080          CatchAll { cout << BaseException::what() << endl; }
00081          cout << "-----------------------------------------\n\n";
00082 
00083          cout << "Invert singular matrix\n";
00084          et.ReName("Block H");
00085          Try { Matrix X(2,2); X<<1<<2<<2<<4; X = X.i(); }
00086          CatchAll { cout << BaseException::what() << endl; }
00087          cout << "-----------------------------------------\n\n";
00088 
00089          cout << "SubMatrix error\n";
00090          et.ReName("Block I");
00091          Try { Matrix X = A.Row(3); }
00092          CatchAll { cout << BaseException::what() << endl; }
00093          cout << "-----------------------------------------\n\n";
00094 
00095          cout << "SubMatrix error\n";
00096          et.ReName("Block J");
00097          Try { Matrix X = A.Row(0); }
00098          CatchAll { cout << BaseException::what() << endl; }
00099          cout << "-----------------------------------------\n\n";
00100 
00101          cout << "Cholesky error\n";
00102          et.ReName("Block K");
00103          Try
00104          {
00105             SymmetricMatrix SM(50); SM = 10;
00106             LowerTriangularMatrix L = Cholesky(SM);
00107          }
00108          CatchAll { cout << BaseException::what() << endl; }
00109          cout << "-----------------------------------------\n\n";
00110 
00111          cout << "Inequality error\n";
00112          et.ReName("Block L");
00113          Try
00114          {
00115             Matrix A(10,10), B(10,10); A = 10; B = 20;
00116             if ( A < B) A = B;
00117          }
00118          CatchAll { cout << BaseException::what() << endl; }
00119          cout << "-----------------------------------------\n\n";
00120 
00121          cout << "Maximum of empty matrix\n";
00122          et.ReName("Block M");
00123          Try
00124          {
00125             Matrix A(10,20); A = 5; Matrix B=A.Rows(6,5);
00126             MaximumAbsoluteValue(B);
00127          }
00128          CatchAll { cout << BaseException::what() << endl; }
00129          cout << "-----------------------------------------\n\n";
00130 
00131          cout << "Incorrectly ReSizing band matrix\n";
00132          et.ReName("Block N");
00133          Try
00134          {
00135             BandMatrix A(20,5,3); A = 5; UpperBandMatrix B;
00136             B.ReSize(A);
00137          }
00138          CatchAll { cout << BaseException::what() << endl; }
00139          cout << "-----------------------------------------\n\n";
00140 
00141          cout << "Incorrectly ReSizing symmetric band matrix\n";
00142          et.ReName("Block M");
00143          Try
00144          {
00145             BandMatrix A(20,5,3); A = 5; SymmetricBandMatrix B;
00146             B.ReSize(A);
00147          }
00148          CatchAll { cout << BaseException::what() << endl; }
00149          cout << "-----------------------------------------\n\n";
00150 
00151          cout << "ReSize CroutMatrix\n";
00152          et.ReName("Block O");
00153          Try
00154          {
00155             Matrix A(3,3); A = 0; A(1,1) = A(2,2) = A(3,3) = 1;
00156             CroutMatrix B = A;;
00157             B.ReSize(A);
00158          }
00159          CatchAll { cout << BaseException::what() << endl; }
00160          cout << "-----------------------------------------\n\n";
00161 
00162       }
00163       CatchAll { cout << "\nException generated in test program\n\n"; }
00164    }
00165 
00166    cout << "\nEnd test\n";
00167    { Matrix A1(40,200); s2 = A1.Store(); }
00168    cout << "\n(The following memory checks are probably not valid with all\n";
00169    cout << "compilers - see documentation)\n";
00170    cout << "\nChecking for lost memory: "
00171       << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
00172    if (s1 != s2) cout << " - error\n"; else cout << " - ok\n";
00173    { Matrix A1(1,1); s4 = A1.Store(); }
00174    cout << "\nChecking for lost memory: "
00175       << (unsigned long)s3 << " " << (unsigned long)s4 << " ";
00176    if (s3 != s4) cout << " - error\n\n"; else cout << " - ok\n\n";
00177 
00178 
00179 #ifdef DO_FREE_CHECK
00180    FreeCheck::Status();
00181 #endif
00182 
00183 //   Throw(Runtime_error("Exception outside try block"));
00184 
00185    return 0;
00186 }


rl_agent
Author(s): Todd Hester
autogenerated on Thu Jun 6 2019 22:00:13