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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3597 - (show annotations)
Thu Sep 15 06:03:17 2011 UTC (7 years, 7 months ago) by caltinay
File size: 7340 byte(s)
Switching to external cppunit:
-removed tools/CppUnitTest
-updated scons script and files
-updated tests to changed API
-updated readme

Bumped scons options file version to 201. Additions:
-cppunit*
-boomeramg*
-vsl_random


1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 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 "EsysExceptionTestCase.h"
16 #include "esysUtils/EsysException.h"
17
18 #include <cppunit/TestCaller.h>
19 #include <iostream>
20
21 using namespace std;
22 using namespace CppUnit;
23 using namespace esysUtils;
24
25 class DerivedEx : public EsysException {
26
27 typedef EsysException Parent;
28
29 public:
30
31 /// Default Constructor for Exception
32 DerivedEx() : Parent() { updateMessage(); }
33
34 /// Constructor for Exception
35 DerivedEx(const char *cstr) : Parent(cstr) { updateMessage(); }
36
37 /// Constructor for Exception
38 DerivedEx(const string &str) : Parent(str) { updateMessage(); }
39
40 // Copy Constructor.
41 DerivedEx(const DerivedEx &other): Parent(other) { updateMessage(); }
42
43 inline virtual DerivedEx &
44 operator=(const DerivedEx &other) THROW(NO_ARG)
45 {
46 Parent::operator=(other);
47 updateMessage();
48 return *this;
49 }
50
51 /// Return the exception name
52 virtual const string & exceptionName() const
53 {
54 return rhubarb;
55 }
56
57 static const string rhubarb;
58 };
59
60 const string DerivedEx::rhubarb("DerivedException");
61
62 void EsysExceptionTestCase::testCase0()
63 {
64 CPPUNIT_ASSERT(true);
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 CPPUNIT_ASSERT(defString.find(defEx.exceptionName()) != string::npos);
76 CPPUNIT_ASSERT(defString.find(defEx.exceptionName()) < 4);
77
78 //
79 // default exception text shouldn't be much longer than contents of exception name
80 //
81 CPPUNIT_ASSERT(defString.size() > defEx.exceptionName().size());
82 CPPUNIT_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 CPPUNIT_ASSERT(ex1String.find(ex1.exceptionName()) != string::npos);
92 CPPUNIT_ASSERT(defString.find(ex1.exceptionName()) < 4);
93
94 //
95 // exception text should contain entered exception message
96 //
97 CPPUNIT_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 CPPUNIT_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 CPPUNIT_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 CPPUNIT_ASSERT(eString.find(e.exceptionName()) != string::npos);
132 CPPUNIT_ASSERT(defString.find(e.exceptionName()) < 4);
133
134 //
135 // exception text should contain entered exception message
136 //
137 CPPUNIT_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 CPPUNIT_ASSERT(defString.find(defEx.exceptionName()) != string::npos);
154 CPPUNIT_ASSERT(defString.find(defEx.exceptionName()) < 4);
155
156 //
157 // default exception text shouldn't be much longer than contents of exception name
158 //
159 CPPUNIT_ASSERT(defString.size() > defEx.exceptionName().size());
160 CPPUNIT_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 CPPUNIT_ASSERT(ex1String.find(ex1.exceptionName()) != string::npos);
169 CPPUNIT_ASSERT(defString.find(ex1.exceptionName()) < 4);
170
171 //
172 // exception text should contain entered exception message
173 //
174 CPPUNIT_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 CPPUNIT_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 CPPUNIT_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 CPPUNIT_ASSERT(eString.find(e.exceptionName()) != string::npos);
209 CPPUNIT_ASSERT(defString.find(e.exceptionName()) < 4);
210
211 //
212 // exception text should contain entered exception message
213 //
214 CPPUNIT_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 CPPUNIT_ASSERT(eString.find(ex4.exceptionName()) != string::npos);
235 CPPUNIT_ASSERT(defString.find(ex4.exceptionName()) < 4);
236
237 //
238 // exception text should contain entered exception message
239 //
240 CPPUNIT_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 CPPUNIT_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 CPPUNIT_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>(
286 "testCase0",&EsysExceptionTestCase::testCase0));
287 testSuite->addTest(new TestCaller<EsysExceptionTestCase>(
288 "testCase1",&EsysExceptionTestCase::testCase1));
289 testSuite->addTest(new TestCaller<EsysExceptionTestCase>(
290 "testCase2",&EsysExceptionTestCase::testCase2));
291 return testSuite;
292 }
293

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26