test_exc.cpp
Go to the documentation of this file.
1 
6 
7 
8 #define WANT_STREAM
9 
10 #include "newmatap.h"
11 #include "newmatio.h" // to help namespace with VC++ 5
12 
13 #ifdef use_namespace
14 using namespace RBD_LIBRARIES;
15 #endif
16 
17 //#include <except.h> // if you want to use set_terminate
18 
19 /**************************** test exceptions ******************************/
20 
21 
22 
23 int main()
24 {
25  // activate the next expression if you want to use compiler supported
26  // exceptions and you want Terminate to catch uncaught exceptions
27  // set_terminate(Terminate);
28  Real* s1; Real* s2; Real* s3; Real* s4;
29  // Forces cout to allocate memory at beginning
30  cout << "\nThis tests the exception system, so you will get\n" <<
31  "a long list of error messages\n\n";
32  cout << "\nPrint a real number (may help lost memory test): "
33  << 3.14159265 << "\n";
34  // Throw exception to set up exception buffer
35  Try { Throw(BaseException("Just a dummy\n")); }
36  CatchAll {};
37  { Matrix A1(40,200); s1 = A1.data(); }
38  { Matrix A1(1,1); s3 = A1.data(); }
39  {
40  Tracer et("Test");
41 
42  Try
43  {
44  Tracer et("Try block");
45 
46 
47 
48  cout << "-----------------------------------------\n\n";
49  Matrix A(2,3), B(4,5); A = 1; B = 2;
50  cout << "Incompatible dimensions\n";
51  et.ReName("Block A");
52  Try { Matrix C = A + B; }
53  CatchAll { cout << BaseException::what() << endl; }
54  cout << "-----------------------------------------\n\n";
55 
56  cout << "Bad index\n";
57  et.ReName("Block B");
58  Try { Real f = A(3,3); cout << f << endl; }
59  CatchAll { cout << BaseException::what() << endl; }
60  cout << "-----------------------------------------\n\n";
61 
62  cout << "Illegal conversion\n";
63  et.ReName("Block C");
64  Try { UpperTriangularMatrix U = A; }
65  CatchAll { cout << BaseException::what() << endl; }
66  cout << "-----------------------------------------\n\n";
67 
68  cout << "Invert non-square matrix - 1\n";
69  et.ReName("Block D");
70  Try { CroutMatrix X = A; }
71  CatchAll { cout << BaseException::what() << endl; }
72  cout << "-----------------------------------------\n\n";
73 
74  cout << "Invert non-square matrix - 2\n";
75  et.ReName("Block E");
76  Try { Matrix X = A.i(); }
77  CatchAll { cout << BaseException::what() << endl; }
78  cout << "-----------------------------------------\n\n";
79 
80  cout << "Non 1x1 matrix to scalar\n";
81  et.ReName("Block F");
82  Try { Real f = A.as_scalar(); cout << f << endl; }
83  CatchAll { cout << BaseException::what() << endl; }
84  cout << "-----------------------------------------\n\n";
85 
86  cout << "Matrix to vector\n";
87  et.ReName("Block G");
88  Try { ColumnVector CV = A;}
89  CatchAll { cout << BaseException::what() << endl; }
90  cout << "-----------------------------------------\n\n";
91 
92  cout << "Invert singular matrix\n";
93  et.ReName("Block H");
94  Try { Matrix X(2,2); X<<1<<2<<2<<4; X = X.i(); }
95  CatchAll { cout << BaseException::what() << endl; }
96  cout << "-----------------------------------------\n\n";
97 
98  cout << "SubMatrix error\n";
99  et.ReName("Block I");
100  Try { Matrix X = A.Row(3); }
101  CatchAll { cout << BaseException::what() << endl; }
102  cout << "-----------------------------------------\n\n";
103 
104  cout << "SubMatrix error\n";
105  et.ReName("Block J");
106  Try { Matrix X = A.Row(0); }
107  CatchAll { cout << BaseException::what() << endl; }
108  cout << "-----------------------------------------\n\n";
109 
110  cout << "Cholesky error\n";
111  et.ReName("Block K");
112  Try
113  {
114  SymmetricMatrix SM(50); SM = 10;
116  }
117  CatchAll { cout << BaseException::what() << endl; }
118  cout << "-----------------------------------------\n\n";
119 
120  cout << "Inequality error\n";
121  et.ReName("Block L");
122  Try
123  {
124  Matrix A(10,10), B(10,10); A = 10; B = 20;
125  if ( A < B) A = B;
126  }
127  CatchAll { cout << BaseException::what() << endl; }
128  cout << "-----------------------------------------\n\n";
129 
130  cout << "Maximum of empty matrix\n";
131  et.ReName("Block M");
132  Try
133  {
134  Matrix A(10,20); A = 5; Matrix B=A.Rows(6,5);
136  }
137  CatchAll { cout << BaseException::what() << endl; }
138  cout << "-----------------------------------------\n\n";
139 
140  cout << "Incorrectly ReSizing band matrix\n";
141  et.ReName("Block N");
142  Try
143  {
144  BandMatrix A(20,5,3); A = 5; UpperBandMatrix B;
145  B.resize(A);
146  }
147  CatchAll { cout << BaseException::what() << endl; }
148  cout << "-----------------------------------------\n\n";
149 
150  cout << "Incorrectly resizing symmetric band matrix\n";
151  et.ReName("Block M");
152  Try
153  {
154  BandMatrix A(20,5,3); A = 5; SymmetricBandMatrix B;
155  B.ReSize(A);
156  }
157  CatchAll { cout << BaseException::what() << endl; }
158  cout << "-----------------------------------------\n\n";
159 
160  cout << "ReSize CroutMatrix\n";
161  et.ReName("Block O");
162  Try
163  {
164  Matrix A(3,3); A = 0; A(1,1) = A(2,2) = A(3,3) = 1;
165  CroutMatrix B = A;
166  B.resize(A);
167  }
168  CatchAll { cout << BaseException::what() << endl; }
169  cout << "-----------------------------------------\n\n";
170 
171  cout << "Manipulate CroutMatrix\n";
172  et.ReName("Block P");
173  Try
174  {
175  Matrix A(3,3); A = 0; A(1,1) = A(2,2) = A(3,3) = 1;
176  CroutMatrix B = A;
177  Matrix C = B;
178  }
179  CatchAll { cout << BaseException::what() << endl; }
180  cout << "-----------------------------------------\n\n";
181 
182  cout << "Manipulate BandLUMatrix\n";
183  et.ReName("Block Q");
184  Try
185  {
186  SymmetricBandMatrix A(3,1); A = 0; A(1,1) = A(2,2) = A(3,3) = 1;
187  BandLUMatrix B = A;
188  Matrix C = B;
189  }
190  CatchAll { cout << BaseException::what() << endl; }
191  cout << "-----------------------------------------\n\n";
192 
193 
194  }
195  CatchAll { cout << "\nException generated in test program\n\n"; }
196  }
197 
198  cout << "\nEnd test\n";
199  { Matrix A1(40,200); s2 = A1.data(); }
200  cout << "\n(The following memory checks are probably not valid with all\n";
201  cout << "compilers - see documentation)\n";
202  cout << "\nChecking for lost memory (large block): "
203  << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
204  if (s1 != s2) cout << " - see section 2.8\n"; else cout << " - ok\n";
205  { Matrix A1(1,1); s4 = A1.data(); }
206  cout << "\nChecking for lost memory (small block): "
207  << (unsigned long)s3 << " " << (unsigned long)s4 << " ";
208  if (s3 != s4) cout << " - see section 2.8\n\n"; else cout << " - ok\n\n";
209 
210 
211 #ifdef DO_FREE_CHECK
212  FreeCheck::Status();
213 #endif
214 
215 // Throw(Runtime_error("Exception outside try block"));
216 
217  return 0;
218 }
219 
#define Try
Definition: myexcept.h:190
int main()
Definition: test_exc.cpp:23
double Real
Definition: include.h:307
void resize(int, int, int)
resize UpperBandMatrix
Definition: bandmat.cpp:99
Upper triangular band matrix.
Definition: newmat.h:1167
#define CatchAll
Definition: myexcept.h:194
Real * data()
Definition: newmat.h:502
Upper triangular matrix.
Definition: newmat.h:799
void resize(int, int, int)
Definition: newmat4.cpp:272
Band matrix.
Definition: newmat.h:1096
void ReSize(int m, int b)
Definition: newmat.h:1289
#define Throw(E)
Definition: myexcept.h:191
The usual rectangular matrix.
Definition: newmat.h:625
InvertedMatrix i() const
Definition: newmat6.cpp:329
ReturnMatrix Cholesky(const SymmetricMatrix &S)
Definition: cholesky.cpp:36
LU decomposition of a band matrix.
Definition: newmat.h:1306
static const char * what()
Definition: myexcept.h:93
Lower triangular matrix.
Definition: newmat.h:848
GetSubMatrix Row(int f) const
Definition: newmat.h:2150
Symmetric band matrix.
Definition: newmat.h:1245
Column vector.
Definition: newmat.h:1008
void ReName(const char *)
Definition: myexcept.h:106
GetSubMatrix Rows(int f, int l) const
Definition: newmat.h:2151
Real maximum_absolute_value(const BaseMatrix &B)
Definition: newmat.h:2109
Symmetric matrix.
Definition: newmat.h:753


kni
Author(s): Martin Günther
autogenerated on Fri Jan 3 2020 04:01:17