/[escript]/trunk/tools/CppUnitTest/src/TestCase.cpp
ViewVC logotype

Contents of /trunk/tools/CppUnitTest/src/TestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3150 - (show annotations)
Fri Sep 3 05:40:27 2010 UTC (9 years, 1 month ago) by caltinay
File size: 7479 byte(s)
Removed executable prop from non-executables.

1 //
2 // Permission to reproduce and create derivative works from the Software ("Software Derivative Works")
3 // is hereby granted to you under the copyright of Michael Feathers. Michael Feathers also grants you
4 // the right to distribute the Software and Software Derivative Works.
5 //
6 // Michael Feathers licenses the Software to you on an "AS IS" basis, without warranty of any kind.
7 // Michael Feathers HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES OR CONDITIONS, EITHER EXPRESS OR IMPLIED,
8 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, NON INFRINGEMENT
9 // AND FITNESS FOR A PARTICULAR PURPOSE.  You are solely responsible for determining the appropriateness
10 // of using the Software and assume all risks associated with the use and distribution of this Software,
11 // including but not limited to the risks of program errors, damage to or loss of data, programs or
12 // equipment, and unavailability or interruption of operations.  MICHAEL FEATHERS WILL NOT BE
13 // LIABLE FOR ANY DIRECT DAMAGES OR FOR ANY SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC
14 // CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF MICHAEL FEATHERS HAD BEEN ADVISED
15 // OF THE POSSIBILITY OF SUCH DAMAGE.  Michael Feathers will not be liable for the loss of, or damage
16 // to, your records or data, or any damages claimed by you based on a third party claim.
17 //
18 // You agree to distribute the Software and any Software Derivatives under a license agreement that:
19 //
20 // 1) is sufficient to notify all licensees of the Software and Software Derivatives that Michael
21 // Feathers assumes no liability for any claim that may arise regarding the Software or
22 // Software Derivatives, and
23 // 2) that disclaims all warranties, both express and implied, from Michael Feathers regarding the
24 // Software and Software Derivatives.  (If you include this Agreement with any distribution
25 // of the Software and Software Derivatives you will have meet this requirement) You agree that
26 // you will not delete any copyright notices in the Software.
27 //
28 // This Agreement is the exclusive statement of your rights in the Software as provided by Michael
29 // Feathers. Except for the licenses granted to you in the second paragraph above, no other licenses
30 // are granted hereunder, by estoppel, implication or otherwise.
31 //
32 #include <iostream>
33 #include <sstream>
34 #include <stdexcept>
35 #if (defined _WIN32) && (defined __INTEL_COMPILER)
36 #include <mathimf.h>
37 #else
38 #include <math.h>
39 #endif
40
41 #include "TestCase.h"
42 #include "TestResult.h"
43 #include "estring.h"
44
45 USING_NAMESPACE_CPPUNITTEST
46
47 // Create a default TestResult
48 TestResult *TestCase::defaultResult ()
49 { return new TestResult; }
50
51
52 // Check for a failed general assertion
53 void TestCase::assertImplementation (bool condition,
54 std::string conditionExpression,
55 long lineNumber,
56 std::string fileName)
57 {
58 if (!condition)
59 throw CppUnitException (conditionExpression, lineNumber, fileName);
60 }
61
62 void TestCase::setCheckErrors(const std::vector<std::string>& checkErrors) {
63 m_errorLog=checkErrors;
64 }
65
66 const std::vector<std::string>& TestCase::getCheckErrors() {
67 return(m_errorLog);
68 }
69
70 // Check for a failed check
71 void TestCase::checkImplementation (bool condition,
72 std::string conditionExpression,
73 long lineNumber,
74 std::string fileName)
75 {
76 if (!condition) {
77 std::ostringstream errorLog;
78 errorLog << "Check Failed - " << std::endl
79 << "File : " << fileName << std::endl
80 << "Line Number : " << lineNumber << std::endl
81 << "Condition : " << conditionExpression << std::endl
82 << std::endl;
83 m_errorLog.push_back(errorLog.str());
84 }
85 }
86
87
88 // Check for a failed equality assertion
89 void TestCase::assertEquals (long expected,
90 long actual,
91 long lineNumber,
92 std::string fileName)
93 {
94 if (expected != actual)
95 assertImplementation (false, notEqualsMessage(expected, actual), lineNumber, fileName);
96 }
97
98
99 // Check for a failed equality assertion
100 void TestCase::assertEquals (double expected,
101 double actual,
102 double delta,
103 long lineNumber,
104 std::string fileName)
105 {
106 if (fabs (expected - actual) > delta)
107 assertImplementation (false, notEqualsMessage(expected, actual), lineNumber, fileName);
108
109 }
110
111
112 // Run the test and catch any exceptions that are triggered by it
113 void TestCase::run (TestResult *result)
114 {
115 //
116 // flag, if true the test has been a success
117 bool success=false;
118 result->startTest (this);
119 try {
120 m_errorLog.clear();
121 setUp ();
122 runTest ();
123 success=true;
124 }
125
126 catch (CppUnitException& e) {
127 CppUnitException *copy = new CppUnitException (e);
128 result->addFailure (this, copy);
129
130 }
131 catch (std::exception& e) {
132 std::cout << "TestCase Caught exception: " << e.what() << std::endl;
133 result->addError (this, new CppUnitException (e.what ()));
134
135 }
136
137 catch (...) {
138 CppUnitException *e = new CppUnitException ("unknown exception");
139 result->addError (this, e);
140 }
141
142 if(m_errorLog.size()>0) {
143 std::ostringstream errorLog;
144 errorLog << std::endl;
145 for(unsigned int i=0; i<m_errorLog.size(); ++i) {
146 errorLog << m_errorLog[i];
147 }
148 errorLog << std::endl;
149 result->addFailure (this, new CppUnitException (errorLog.str()));
150 }
151
152 try {
153 tearDown ();
154 }
155
156 catch (CppUnitException& e) {
157 std::ostringstream temp;
158 temp << "Exception from tearDown: " << e.what () << std::flush;
159 CppUnitException *copy = new CppUnitException (temp.str().c_str());
160 result->addFailure (this, copy);
161 success=false;
162 }
163 catch (std::exception& e) {
164 std::ostringstream temp;
165 temp << "Exception from tearDown: " << e.what () << std::flush;
166 result->addError (this, new CppUnitException (temp.str().c_str()));
167 success=false;
168 }
169
170 catch (...) {
171 std::ostringstream temp;
172 temp << "Exception from tearDown: Unknown exception." << std::flush;
173 CppUnitException *e = new CppUnitException (temp.str().c_str());
174 result->addError (this, e);
175 success=false;
176 }
177
178 result->endTest (this);
179 if (success) {
180 std::cerr << " OK" << std::endl;
181 }
182 else {
183 setFailure();
184 }
185 }
186
187
188 // A default run method
189 TestResult *TestCase::run ()
190 {
191 TestResult *result = defaultResult ();
192
193 run (result);
194 return result;
195
196 }
197
198
199 // All the work for runTest is deferred to subclasses
200 void TestCase::runTest ()
201 {
202 }
203
204
205 // Build a message about a failed equality check
206 std::string TestCase::notEqualsMessage (long expected, long actual)
207 {
208 return "expected: " + estring (expected) + " but was: " + estring (actual);
209 }
210
211
212 // Build a message about a failed equality check
213 std::string TestCase::notEqualsMessage (double expected, double actual)
214 {
215 return "expected: " + estring (expected) + " but was: " + estring (actual);
216 }
217

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26