/[escript]/trunk/esysUtils/test/EsysExceptionTestCase.cpp
ViewVC logotype

Contents of /trunk/esysUtils/test/EsysExceptionTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1388 - (show annotations)
Fri Jan 11 07:45:58 2008 UTC (13 years, 3 months ago) by trankine
File size: 6515 byte(s)
And get the *(&(*&(* name right
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 /// Return the exception name
38 virtual string exceptionName() const {
39 return "DerivedException";
40 }
41 };
42
43 void EsysExceptionTestCase::testCase0() {
44
45 //
46 // dummy test case
47 //
48 // just test the UnitTesting framework is OK
49 //
50
51 assert(true);
52
53 }
54
55 void EsysExceptionTestCase::testCase1() {
56
57 EsysException defEx;
58
59 //
60 // default exception text should have contents of exceptionName near start
61 //
62 string defString = defEx.toString();
63 assert(defString.find(defEx.exceptionName()) != string::npos);
64 assert(defString.find(defEx.exceptionName()) < 4);
65
66 //
67 // default exception text shouldn't be much longer than contents of exception name
68 //
69 assert(defString.size() > defEx.exceptionName().size());
70 assert((defString.size() - defEx.exceptionName().size()) < 10);
71
72 string ex1Text("My first funny exception message.");
73 EsysException ex1(ex1Text);
74
75 //
76 // exception text should have contents of exceptionName near start
77 //
78 string ex1String = ex1.toString();
79 assert(ex1String.find(ex1.exceptionName()) != string::npos);
80 assert(defString.find(ex1.exceptionName()) < 4);
81
82 //
83 // exception text should contain entered exception message
84 //
85 assert(ex1String.find(ex1Text) != string::npos);
86
87 //
88 // copy constructed exception should match original
89 //
90 EsysException copyEx(ex1);
91 string copyString = copyEx.toString();
92 assert(ex1String == copyString);
93
94 //
95 // copy assigned exception should match original
96 //
97 EsysException assEx;
98 assEx = ex1;
99 string assString = assEx.toString();
100 assert(ex1String == assString);
101
102 //
103 // check throw/catch mechanism
104 //
105 string ex2Text("My second funny exception message.");
106 try {
107
108 EsysException ex2(ex2Text);
109 throw(ex2);
110
111 }
112
113 catch(EsysException& e) {
114
115 //
116 // exception text should have contents of exceptionName near start
117 //
118 string eString = e.toString();
119 assert(eString.find(e.exceptionName()) != string::npos);
120 assert(defString.find(e.exceptionName()) < 4);
121
122 //
123 // exception text should contain entered exception message
124 //
125 assert(eString.find(ex2Text) != string::npos);
126
127 }
128
129 }
130
131 //
132 // test derived EsysException
133 //
134 void EsysExceptionTestCase::testCase2() {
135
136 DerivedEx defEx;
137 //
138 // default exception text should have contents of exceptionName near start
139 //
140 string defString = defEx.toString();
141 assert(defString.find(defEx.exceptionName()) != string::npos);
142 assert(defString.find(defEx.exceptionName()) < 4);
143
144 //
145 // default exception text shouldn't be much longer than contents of exception name
146 //
147 assert(defString.size() > defEx.exceptionName().size());
148 assert((defString.size() - defEx.exceptionName().size()) < 10);
149
150 string ex1Text("asdjhieurncidhfjsnfkjefkjndfjkhsdrdfjksdhfweh");
151 DerivedEx ex1(ex1Text);
152 //
153 // exception text should have contents of exceptionName near start
154 //
155 string ex1String = ex1.toString();
156 assert(ex1String.find(ex1.exceptionName()) != string::npos);
157 assert(defString.find(ex1.exceptionName()) < 4);
158
159 //
160 // exception text should contain entered exception message
161 //
162 assert(ex1String.find(ex1Text) != string::npos);
163
164 //
165 // copy constructed exception should match original
166 //
167 DerivedEx copyEx(ex1);
168 string copyString = copyEx.toString();
169 assert(ex1String == copyString);
170
171 //
172 // copy assigned exception should match original
173 //
174 DerivedEx assEx;
175 assEx = ex1;
176 string assString = assEx.toString();
177 assert(ex1String == assString);
178
179 //
180 // check throw/catch mechanism
181 //
182 string ex2Text("pjkkjhdfbnkjerbkjsduflfkjahalkgjlklhjhj");
183 try {
184
185 DerivedEx ex2(ex2Text);
186 throw(ex2);
187
188 }
189
190 catch(DerivedEx& e) {
191
192 //
193 // exception text should have contents of exceptionName near start
194 //
195 string eString = e.toString();
196 assert(eString.find(e.exceptionName()) != string::npos);
197 assert(defString.find(e.exceptionName()) < 4);
198
199 //
200 // exception text should contain entered exception message
201 //
202 assert(eString.find(ex2Text) != string::npos);
203 }
204
205 //
206 // check throw/catch mechanism
207 //
208 string ex3Text("irfjvniouf;iarhglAKSDIghlAKSDghladg");
209 try {
210
211 DerivedEx ex3(ex3Text);
212 throw(ex3);
213
214 }
215 catch(EsysException& e) {
216
217 //
218 // exception text should have contents of exceptionName near start
219 //
220 DerivedEx ex4;
221 std::string eString = e.toString();
222 assert(eString.find(ex4.exceptionName()) != string::npos);
223 assert(defString.find(ex4.exceptionName()) < 4);
224
225 //
226 // exception text should contain entered exception message
227 //
228 assert(eString.find(ex3Text) != string::npos);
229
230 }
231
232 //
233 // test to see if exception name gets lost on rethrow
234 //
235 try {
236 try {
237 DerivedEx ex4("D ex4 text.");
238 throw ex4;
239 }
240 catch (EsysException& e) {
241 cout << endl << e.toString() << endl;
242 throw;
243 }
244 }
245 catch (EsysException& e) {
246 cout << e.toString() << endl;
247 }
248
249 cout << "Test EsysException may be caught as a std::exception" << endl;
250 try {
251 DerivedEx ex4("Exception caught as std::exception");
252 throw ex4;
253 }
254 catch (exception& e) {
255 cout << e.what() << endl;
256 }
257 catch (...) {
258 //
259 // if the exception is caught here there is a problem
260 assert(false);
261 }
262 }
263
264 TestSuite* EsysExceptionTestCase::suite ()
265 {
266 //
267 // create the suite of tests to perform.
268 TestSuite *testSuite = new TestSuite ("EsysExceptionTestCase");
269
270 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase0",&EsysExceptionTestCase::testCase0));
271 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase1",&EsysExceptionTestCase::testCase1));
272 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase2",&EsysExceptionTestCase::testCase2));
273 return testSuite;
274 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26