/[escript]/branches/schroedinger_upto1946/esysUtils/test/EsysExceptionTestCase.cpp
ViewVC logotype

Contents of /branches/schroedinger_upto1946/esysUtils/test/EsysExceptionTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1998 - (show annotations)
Sat Nov 8 08:47:54 2008 UTC (10 years, 7 months ago) by phornby
File size: 7029 byte(s)
Please ignore the last checkin. A trick I used in
LazyData (which worked) when applied to THROW() failed horribly.
Why it should work in one case and not the other....... who knows?

I have reverted to the THROW_ANY macro for throw().


1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #include <iostream>
16
17 #include "EsysExceptionTestCase.h"
18 #include "esysUtils/EsysException.h"
19
20 using namespace std;
21 using namespace CppUnitTest;
22 using namespace esysUtils;
23
24 class DerivedEx : public EsysException {
25
26 typedef EsysException Parent;
27
28 public:
29
30 /// Default Constructor for Exception
31 DerivedEx() : Parent() { updateMessage(); }
32
33 /// Constructor for Exception
34 DerivedEx(const char *cstr) : Parent(cstr) { updateMessage(); }
35
36 /// Constructor for Exception
37 DerivedEx(const string &str) : Parent(str) { updateMessage(); }
38
39 // Copy Constructor.
40 DerivedEx(const DerivedEx &other): Parent(other) { updateMessage(); }
41
42 inline virtual DerivedEx &
43 operator=(const DerivedEx &other) THROW_ANY
44 {
45 Parent::operator=(other);
46 updateMessage();
47 return *this;
48 }
49
50 /// Return the exception name
51 virtual const string & exceptionName() const
52 {
53 return rhubarb;
54 }
55
56 static const string rhubarb;
57 };
58
59 const string DerivedEx::rhubarb("DerivedException");
60
61 void EsysExceptionTestCase::testCase0() {
62
63 assert(true);
64
65 }
66
67 void EsysExceptionTestCase::testCase1() {
68
69 EsysException defEx;
70
71 //
72 // default exception text should have contents of exceptionName near start
73 //
74 string defString = defEx.toString();
75 assert(defString.find(defEx.exceptionName()) != string::npos);
76 assert(defString.find(defEx.exceptionName()) < 4);
77
78 //
79 // default exception text shouldn't be much longer than contents of exception name
80 //
81 assert(defString.size() > defEx.exceptionName().size());
82 assert((defString.size() - defEx.exceptionName().size()) < 10);
83
84 string ex1Text("My first funny exception message.");
85 EsysException ex1(ex1Text);
86
87 //
88 // exception text should have contents of exceptionName near start
89 //
90 string ex1String = ex1.toString();
91 assert(ex1String.find(ex1.exceptionName()) != string::npos);
92 assert(defString.find(ex1.exceptionName()) < 4);
93
94 //
95 // exception text should contain entered exception message
96 //
97 assert(ex1String.find(ex1Text) != string::npos);
98
99 //
100 // copy constructed exception should match original
101 //
102 EsysException copyEx(ex1);
103 string copyString = copyEx.toString();
104 assert(ex1String == copyString);
105
106 //
107 // copy assigned exception should match original
108 //
109 EsysException assEx;
110 assEx = ex1;
111 string assString = assEx.toString();
112 assert(ex1String == assString);
113
114 //
115 // check throw/catch mechanism
116 //
117 string ex2Text("My second funny exception message.");
118 try {
119
120 EsysException ex2(ex2Text);
121 throw(ex2);
122
123 }
124
125 catch(EsysException& e) {
126
127 //
128 // exception text should have contents of exceptionName near start
129 //
130 string eString = e.toString();
131 assert(eString.find(e.exceptionName()) != string::npos);
132 assert(defString.find(e.exceptionName()) < 4);
133
134 //
135 // exception text should contain entered exception message
136 //
137 assert(eString.find(ex2Text) != string::npos);
138
139 }
140
141 }
142
143 //
144 // test derived EsysException
145 //
146 void EsysExceptionTestCase::testCase2() {
147
148 DerivedEx defEx;
149 //
150 // default exception text should have contents of exceptionName near start
151 //
152 string defString = defEx.toString();
153 assert(defString.find(defEx.exceptionName()) != string::npos);
154 assert(defString.find(defEx.exceptionName()) < 4);
155
156 //
157 // default exception text shouldn't be much longer than contents of exception name
158 //
159 assert(defString.size() > defEx.exceptionName().size());
160 assert((defString.size() - defEx.exceptionName().size()) < 10);
161
162 string ex1Text("asdjhieurncidhfjsnfkjefkjndfjkhsdrdfjksdhfweh");
163 DerivedEx ex1(ex1Text);
164 //
165 // exception text should have contents of exceptionName near start
166 //
167 string ex1String = ex1.toString();
168 assert(ex1String.find(ex1.exceptionName()) != string::npos);
169 assert(defString.find(ex1.exceptionName()) < 4);
170
171 //
172 // exception text should contain entered exception message
173 //
174 assert(ex1String.find(ex1Text) != string::npos);
175
176 //
177 // copy constructed exception should match original
178 //
179 DerivedEx copyEx(ex1);
180 string copyString = copyEx.toString();
181 assert(ex1String == copyString);
182
183 //
184 // copy assigned exception should match original
185 //
186 DerivedEx assEx;
187 assEx = ex1;
188 string assString = assEx.toString();
189 assert(ex1String == assString);
190
191 //
192 // check throw/catch mechanism
193 //
194 string ex2Text("pjkkjhdfbnkjerbkjsduflfkjahalkgjlklhjhj");
195 try {
196
197 DerivedEx ex2(ex2Text);
198 throw(ex2);
199
200 }
201
202 catch(DerivedEx& e) {
203
204 //
205 // exception text should have contents of exceptionName near start
206 //
207 string eString = e.toString();
208 assert(eString.find(e.exceptionName()) != string::npos);
209 assert(defString.find(e.exceptionName()) < 4);
210
211 //
212 // exception text should contain entered exception message
213 //
214 assert(eString.find(ex2Text) != string::npos);
215 }
216
217 //
218 // check throw/catch mechanism
219 //
220 string ex3Text("irfjvniouf;iarhglAKSDIghlAKSDghladg");
221 try {
222
223 DerivedEx ex3(ex3Text);
224 throw(ex3);
225
226 }
227 catch(EsysException& e) {
228
229 //
230 // exception text should have contents of exceptionName near start
231 //
232 DerivedEx ex4;
233 std::string eString = e.toString();
234 assert(eString.find(ex4.exceptionName()) != string::npos);
235 assert(defString.find(ex4.exceptionName()) < 4);
236
237 //
238 // exception text should contain entered exception message
239 //
240 assert(eString.find(ex3Text) != string::npos);
241
242 }
243
244 //
245 // test to see if exception name gets lost on rethrow
246 //
247 try {
248 try {
249 DerivedEx ex4("D ex4 text.");
250 throw ex4;
251 }
252 catch (EsysException& e) {
253 cout << endl << e.toString() << endl;
254 throw;
255 }
256 }
257 catch (EsysException& e) {
258 cout << e.toString() << endl;
259 }
260
261 cout << "Test EsysException may be caught as a std::exception" << endl;
262 try {
263 DerivedEx ex4("Exception caught as std::exception");
264 throw ex4;
265 }
266 catch (exception& e) {
267 // cout << e.what() << endl;
268 assert(e.what() == string("DerivedException: Exception caught"
269 " as std::exception")
270 );
271 }
272 catch (...) {
273 //
274 // if the exception is caught here there is a problem
275 assert(false);
276 }
277 }
278
279 TestSuite* EsysExceptionTestCase::suite ()
280 {
281 //
282 // create the suite of tests to perform.
283 TestSuite *testSuite = new TestSuite ("EsysExceptionTestCase");
284
285 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase0",&EsysExceptionTestCase::testCase0));
286 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase1",&EsysExceptionTestCase::testCase1));
287 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase2",&EsysExceptionTestCase::testCase2));
288 return testSuite;
289 }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26