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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26