| 1 | #define WANT_STREAM | 
|---|
| 2 |  | 
|---|
| 3 | #include "newmatap.h" | 
|---|
| 4 | #include "newmatio.h"              // to help namespace with VC++ 5 | 
|---|
| 5 |  | 
|---|
| 6 | #ifdef use_namespace | 
|---|
| 7 | using namespace RBD_LIBRARIES; | 
|---|
| 8 | #endif | 
|---|
| 9 |  | 
|---|
| 10 | //#include <except.h>             // if you want to use set_terminate | 
|---|
| 11 |  | 
|---|
| 12 | /**************************** test exceptions ******************************/ | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 | int main() | 
|---|
| 16 | { | 
|---|
| 17 |    // activate the next expression if you want to use compiler supported | 
|---|
| 18 |    // exceptions and you want Terminate to catch uncaught exceptions | 
|---|
| 19 |    // set_terminate(Terminate); | 
|---|
| 20 |    Real* s1; Real* s2; Real* s3; Real* s4; | 
|---|
| 21 |    // Forces cout to allocate memory at beginning | 
|---|
| 22 |    cout << "\nThis tests the exception system, so you will get\n" << | 
|---|
| 23 |       "a long list of error messages\n\n"; | 
|---|
| 24 |    cout << "\nPrint a real number (may help lost memory test): " << 3.14159265 << "\n"; | 
|---|
| 25 |    // Throw exception to set up exception buffer | 
|---|
| 26 |    Try { Throw(Exception("Just a dummy\n")); } | 
|---|
| 27 |    CatchAll {}; | 
|---|
| 28 |    { Matrix A1(40,200); s1 = A1.Store(); } | 
|---|
| 29 |    { Matrix A1(1,1); s3 = A1.Store(); } | 
|---|
| 30 |    { | 
|---|
| 31 |       Tracer et("Test"); | 
|---|
| 32 |  | 
|---|
| 33 |       Try | 
|---|
| 34 |       { | 
|---|
| 35 |          Tracer et("Try block"); | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 40 |          Matrix A(2,3), B(4,5); A = 1; B = 2; | 
|---|
| 41 |          cout << "Incompatible dimensions\n"; | 
|---|
| 42 |          et.ReName("Block A"); | 
|---|
| 43 |          Try { Matrix C = A + B; } | 
|---|
| 44 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 45 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 46 |  | 
|---|
| 47 |          cout << "Bad index\n"; | 
|---|
| 48 |          et.ReName("Block B"); | 
|---|
| 49 |          Try { Real f = A(3,3); cout << f << endl; } | 
|---|
| 50 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 51 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 52 |  | 
|---|
| 53 |          cout << "Illegal conversion\n"; | 
|---|
| 54 |          et.ReName("Block C"); | 
|---|
| 55 |          Try { UpperTriangularMatrix U = A; } | 
|---|
| 56 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 57 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 58 |  | 
|---|
| 59 |          cout << "Invert non-square matrix - 1\n"; | 
|---|
| 60 |          et.ReName("Block D"); | 
|---|
| 61 |          Try { CroutMatrix X = A; } | 
|---|
| 62 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 63 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 64 |  | 
|---|
| 65 |          cout << "Invert non-square matrix - 2\n"; | 
|---|
| 66 |          et.ReName("Block E"); | 
|---|
| 67 |          Try { Matrix X = A.i(); } | 
|---|
| 68 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 69 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 70 |  | 
|---|
| 71 |          cout << "Non 1x1 matrix to scalar\n"; | 
|---|
| 72 |          et.ReName("Block F"); | 
|---|
| 73 |          Try { Real f = A.AsScalar(); cout << f << endl; } | 
|---|
| 74 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 75 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 76 |  | 
|---|
| 77 |          cout << "Matrix to vector\n"; | 
|---|
| 78 |          et.ReName("Block G"); | 
|---|
| 79 |          Try { ColumnVector CV = A;} | 
|---|
| 80 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 81 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 82 |  | 
|---|
| 83 |          cout << "Invert singular matrix\n"; | 
|---|
| 84 |          et.ReName("Block H"); | 
|---|
| 85 |          Try { Matrix X(2,2); X<<1<<2<<2<<4; X = X.i(); } | 
|---|
| 86 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 87 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 88 |  | 
|---|
| 89 |          cout << "SubMatrix error\n"; | 
|---|
| 90 |          et.ReName("Block I"); | 
|---|
| 91 |          Try { Matrix X = A.Row(3); } | 
|---|
| 92 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 93 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 94 |  | 
|---|
| 95 |          cout << "SubMatrix error\n"; | 
|---|
| 96 |          et.ReName("Block J"); | 
|---|
| 97 |          Try { Matrix X = A.Row(0); } | 
|---|
| 98 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 99 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 100 |  | 
|---|
| 101 |          cout << "Cholesky error\n"; | 
|---|
| 102 |          et.ReName("Block K"); | 
|---|
| 103 |          Try | 
|---|
| 104 |          { | 
|---|
| 105 |             SymmetricMatrix SM(50); SM = 10; | 
|---|
| 106 |             LowerTriangularMatrix L = Cholesky(SM); | 
|---|
| 107 |          } | 
|---|
| 108 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 109 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 110 |  | 
|---|
| 111 |          cout << "Inequality error\n"; | 
|---|
| 112 |          et.ReName("Block L"); | 
|---|
| 113 |          Try | 
|---|
| 114 |          { | 
|---|
| 115 |             Matrix A(10,10), B(10,10); A = 10; B = 20; | 
|---|
| 116 |             if ( A < B) A = B; | 
|---|
| 117 |          } | 
|---|
| 118 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 119 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 120 |  | 
|---|
| 121 |          cout << "Maximum of empty matrix\n"; | 
|---|
| 122 |          et.ReName("Block M"); | 
|---|
| 123 |          Try | 
|---|
| 124 |          { | 
|---|
| 125 |             Matrix A(10,20); A = 5; Matrix B=A.Rows(6,5); | 
|---|
| 126 |             MaximumAbsoluteValue(B); | 
|---|
| 127 |          } | 
|---|
| 128 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 129 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 130 |  | 
|---|
| 131 |          cout << "Incorrectly ReSizing band matrix\n"; | 
|---|
| 132 |          et.ReName("Block N"); | 
|---|
| 133 |          Try | 
|---|
| 134 |          { | 
|---|
| 135 |             BandMatrix A(20,5,3); A = 5; UpperBandMatrix B; | 
|---|
| 136 |             B.ReSize(A); | 
|---|
| 137 |          } | 
|---|
| 138 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 139 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 140 |  | 
|---|
| 141 |          cout << "Incorrectly ReSizing symmetric band matrix\n"; | 
|---|
| 142 |          et.ReName("Block M"); | 
|---|
| 143 |          Try | 
|---|
| 144 |          { | 
|---|
| 145 |             BandMatrix A(20,5,3); A = 5; SymmetricBandMatrix B; | 
|---|
| 146 |             B.ReSize(A); | 
|---|
| 147 |          } | 
|---|
| 148 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 149 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 150 |  | 
|---|
| 151 |          cout << "ReSize CroutMatrix\n"; | 
|---|
| 152 |          et.ReName("Block O"); | 
|---|
| 153 |          Try | 
|---|
| 154 |          { | 
|---|
| 155 |             Matrix A(3,3); A = 0; A(1,1) = A(2,2) = A(3,3) = 1; | 
|---|
| 156 |             CroutMatrix B = A;; | 
|---|
| 157 |             B.ReSize(A); | 
|---|
| 158 |          } | 
|---|
| 159 |          CatchAll { cout << Exception::what() << endl; } | 
|---|
| 160 |          cout << "-----------------------------------------\n\n"; | 
|---|
| 161 |  | 
|---|
| 162 |       } | 
|---|
| 163 |       CatchAll { cout << "\nException generated in test program\n\n"; } | 
|---|
| 164 |    } | 
|---|
| 165 |  | 
|---|
| 166 |    cout << "\nEnd test\n"; | 
|---|
| 167 |    { Matrix A1(40,200); s2 = A1.Store(); } | 
|---|
| 168 |    cout << "\n(The following memory checks are probably not valid with all\n"; | 
|---|
| 169 |    cout << "compilers - see documentation)\n"; | 
|---|
| 170 |    cout << "\nChecking for lost memory: " | 
|---|
| 171 |       << (unsigned long)s1 << " " << (unsigned long)s2 << " "; | 
|---|
| 172 |    if (s1 != s2) cout << " - error\n"; else cout << " - ok\n"; | 
|---|
| 173 |    { Matrix A1(1,1); s4 = A1.Store(); } | 
|---|
| 174 |    cout << "\nChecking for lost memory: " | 
|---|
| 175 |       << (unsigned long)s3 << " " << (unsigned long)s4 << " "; | 
|---|
| 176 |    if (s3 != s4) cout << " - error\n\n"; else cout << " - ok\n\n"; | 
|---|
| 177 |  | 
|---|
| 178 |  | 
|---|
| 179 | #ifdef DO_FREE_CHECK | 
|---|
| 180 |    FreeCheck::Status(); | 
|---|
| 181 | #endif | 
|---|
| 182 |  | 
|---|
| 183 | //   Throw(Runtime_error("Exception outside try block")); | 
|---|
| 184 |  | 
|---|
| 185 |    return 0; | 
|---|
| 186 | } | 
|---|