File: test_exc.cpp

package info (click to toggle)
newmat 1.10.4-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,908 kB
  • sloc: cpp: 31,314; makefile: 56
file content (186 lines) | stat: -rw-r--r-- 6,463 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#define WANT_STREAM

#include "newmatap.h"
#include "newmatio.h"              // to help namespace with VC++ 5

#ifdef use_namespace
using namespace RBD_LIBRARIES;
#endif

//#include <except.h>             // if you want to use set_terminate

/**************************** test exceptions ******************************/


int main()
{
   // activate the next expression if you want to use compiler supported
   // exceptions and you want Terminate to catch uncaught exceptions
   // set_terminate(Terminate);
   Real* s1; Real* s2; Real* s3; Real* s4;
   // Forces cout to allocate memory at beginning
   cout << "\nThis tests the exception system, so you will get\n" <<
      "a long list of error messages\n\n";
   cout << "\nPrint a real number (may help lost memory test): " << 3.14159265 << "\n";
   // Throw exception to set up exception buffer
   Try { Throw(BaseException("Just a dummy\n")); }
   CatchAll {};
   { Matrix A1(40,200); s1 = A1.Store(); }
   { Matrix A1(1,1); s3 = A1.Store(); }
   {
      Tracer et("Test");

      Try
      {
         Tracer et("Try block");



         cout << "-----------------------------------------\n\n";
         Matrix A(2,3), B(4,5); A = 1; B = 2;
         cout << "Incompatible dimensions\n";
         et.ReName("Block A");
         Try { Matrix C = A + B; }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Bad index\n";
         et.ReName("Block B");
         Try { Real f = A(3,3); cout << f << endl; }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Illegal conversion\n";
         et.ReName("Block C");
         Try { UpperTriangularMatrix U = A; }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Invert non-square matrix - 1\n";
         et.ReName("Block D");
         Try { CroutMatrix X = A; }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Invert non-square matrix - 2\n";
         et.ReName("Block E");
         Try { Matrix X = A.i(); }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Non 1x1 matrix to scalar\n";
         et.ReName("Block F");
         Try { Real f = A.AsScalar(); cout << f << endl; }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Matrix to vector\n";
         et.ReName("Block G");
         Try { ColumnVector CV = A;}
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Invert singular matrix\n";
         et.ReName("Block H");
         Try { Matrix X(2,2); X<<1<<2<<2<<4; X = X.i(); }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "SubMatrix error\n";
         et.ReName("Block I");
         Try { Matrix X = A.Row(3); }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "SubMatrix error\n";
         et.ReName("Block J");
         Try { Matrix X = A.Row(0); }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Cholesky error\n";
         et.ReName("Block K");
         Try
         {
            SymmetricMatrix SM(50); SM = 10;
            LowerTriangularMatrix L = Cholesky(SM);
         }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Inequality error\n";
         et.ReName("Block L");
         Try
         {
            Matrix A(10,10), B(10,10); A = 10; B = 20;
            if ( A < B) A = B;
         }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Maximum of empty matrix\n";
         et.ReName("Block M");
         Try
         {
            Matrix A(10,20); A = 5; Matrix B=A.Rows(6,5);
            MaximumAbsoluteValue(B);
         }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Incorrectly ReSizing band matrix\n";
         et.ReName("Block N");
         Try
         {
            BandMatrix A(20,5,3); A = 5; UpperBandMatrix B;
            B.ReSize(A);
         }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "Incorrectly ReSizing symmetric band matrix\n";
         et.ReName("Block M");
         Try
         {
            BandMatrix A(20,5,3); A = 5; SymmetricBandMatrix B;
            B.ReSize(A);
         }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

         cout << "ReSize CroutMatrix\n";
         et.ReName("Block O");
         Try
         {
            Matrix A(3,3); A = 0; A(1,1) = A(2,2) = A(3,3) = 1;
            CroutMatrix B = A;;
            B.ReSize(A);
         }
         CatchAll { cout << BaseException::what() << endl; }
         cout << "-----------------------------------------\n\n";

      }
      CatchAll { cout << "\nException generated in test program\n\n"; }
   }

   cout << "\nEnd test\n";
   { Matrix A1(40,200); s2 = A1.Store(); }
   cout << "\n(The following memory checks are probably not valid with all\n";
   cout << "compilers - see documentation)\n";
   cout << "\nChecking for lost memory: "
      << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
   if (s1 != s2) cout << " - error\n"; else cout << " - ok\n";
   { Matrix A1(1,1); s4 = A1.Store(); }
   cout << "\nChecking for lost memory: "
      << (unsigned long)s3 << " " << (unsigned long)s4 << " ";
   if (s3 != s4) cout << " - error\n\n"; else cout << " - ok\n\n";


#ifdef DO_FREE_CHECK
   FreeCheck::Status();
#endif

//   Throw(Runtime_error("Exception outside try block"));

   return 0;
}