-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniCppUnit.cpp
236 lines (211 loc) · 6.34 KB
/
MiniCppUnit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (c) 2003-2004 Pau Arumí & David García
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "MiniCppUnit.hpp"
#include <cmath>
#include <algorithm>
TestsListener& TestsListener::theInstance()
{
static TestsListener instancia;
return instancia;
}
std::stringstream& TestsListener::errorsLog()
{
if (_currentTestName)
_log << "\n" << errmsgTag_nameOfTest() << (*_currentTestName) << "\n";
return _log;
}
std::string TestsListener::logString()
{
std::string aRetornar = _log.str();
_log.str("");
return aRetornar;
}
void TestsListener::currentTestName( std::string& name)
{
_currentTestName = &name;
}
void TestsListener::testHasRun()
{
std::cout << ".";
theInstance()._executed++;
}
void TestsListener::testHasFailed()
{
std::cout << "F";
theInstance()._failed++;
throw TestFailedException();
}
void TestsListener::testHasThrown()
{
std::cout << "E";
theInstance()._exceptions++;
}
std::string TestsListener::summary()
{
std::ostringstream os;
os << "\nSummary:\n"
<< Assert::bold() << "\tExecuted Tests: "
<< _executed << Assert::normal() << std::endl
<< Assert::green() << "\tPassed Tests: "
<< (_executed-_failed-_exceptions)
<< Assert::normal() << std::endl;
if (_failed > 0)
{
os << Assert::red() << "\tFailed Tests: "
<< _failed << Assert::normal() << std::endl;
}
if (_exceptions > 0)
{
os << Assert::yellow() << "\tUnexpected exceptions: "
<< _exceptions << Assert::normal() << std::endl;
}
os << std::endl;
return os.str();
}
bool TestsListener::allTestsPassed()
{
return !theInstance()._exceptions && !theInstance()._failed;
}
void Assert::assertTrue(const char* strExpression, bool expression,
const char* file, int line)
{
if (!expression)
{
TestsListener::theInstance().errorsLog() << "\n"
<< errmsgTag_testFailedIn() << file
<< errmsgTag_inLine() << line << "\n"
<< errmsgTag_failedExpression()
<< bold() << strExpression << normal() << "\n";
TestsListener::theInstance().testHasFailed();
}
}
void Assert::assertTrueMessage(const char* strExpression, bool expression,
const char* missatge, const char* file, int line)
{
if (!expression)
{
TestsListener::theInstance().errorsLog() << "\n"
<< errmsgTag_testFailedIn() << file
<< errmsgTag_inLine() << line << "\n"
<< errmsgTag_failedExpression()
<< bold() << strExpression << "\n"
<< missatge<< normal() << "\n";
TestsListener::theInstance().testHasFailed();
}
}
void Assert::assertEquals( const char * expected, const char * result,
const char* file, int line )
{
assertEquals(std::string(expected), std::string(result),
file, line);
}
void Assert::assertEquals( const bool& expected, const bool& result,
const char* file, int line )
{
assertEquals(
(expected?"true":"false"),
(result?"true":"false"),
file, line);
}
// floating point numbers comparisons taken
// from c/c++ users journal. dec 04 pag 10
bool isNaN(double x)
{
bool b1 = (x < 0.0);
bool b2 = (x >= 0.0);
return !(b1 || b2);
}
double scaledEpsilon(const double& expected, const double& fuzzyEpsilon )
{
const double aa = fabs(expected)+1;
return (std::isinf(aa))? fuzzyEpsilon: fuzzyEpsilon * aa;
}
bool fuzzyEquals(double expected, double result, double fuzzyEpsilon)
{
return (expected==result) || ( fabs(expected-result) <= scaledEpsilon(expected, fuzzyEpsilon) );
}
void Assert::assertEquals( const double& expected, const double& result,
const char* file, int line )
{
const double fuzzyEpsilon = 0.000001;
assertEqualsEpsilon( expected, result, fuzzyEpsilon, file, line );
}
void Assert::assertEquals( const float& expected, const float& result,
const char* file, int line )
{
assertEquals((double)expected, (double)result, file, line);
}
void Assert::assertEquals( const long double& expected, const long double& result,
const char* file, int line )
{
assertEquals((double)expected, (double)result, file, line);
}
void Assert::assertEqualsEpsilon( const double& expected, const double& result, const double& epsilon,
const char* file, int line )
{
if (isNaN(expected) && isNaN(result) ) return;
if (!isNaN(expected) && !isNaN(result) && fuzzyEquals(expected, result, epsilon) ) return;
TestsListener::theInstance().errorsLog()
<< errmsgTag_testFailedIn() << file
<< errmsgTag_inLine() << line << "\n"
<< errmsgTag_expected()
<< bold() << expected << normal() << " "
<< errmsgTag_butWas()
<< bold() << result << normal() << "\n";
TestsListener::theInstance().testHasFailed();
}
int Assert::notEqualIndex( const std::string & one, const std::string & other )
{
int end = static_cast<int>(std::min(one.length(), other.length()));
for ( int index = 0; index < end; index++ )
if (one[index] != other[index] )
return index;
return end;
}
/**
* we overload the assert with string doing colored diffs
*
* MS Visual6 doesn't allow string by reference :-(
*/
void Assert::assertEquals( const std::string expected, const std::string result,
const char* file, int line )
{
if(expected == result)
return;
int indexDiferent = notEqualIndex(expected, result);
TestsListener::theInstance().errorsLog()
<< file << ", line: " << line << "\n"
<< errmsgTag_expected() << "\n" << blue()
<< expected.substr(0,indexDiferent)
<< green() << expected.substr(indexDiferent)
<< normal() << "\n"
<< errmsgTag_butWas() << blue() << "\n"
<< result.substr(0,indexDiferent)
<< red() << result.substr(indexDiferent)
<< normal() << std::endl;
TestsListener::theInstance().testHasFailed();
}
void Assert::fail(const char* motiu, const char* file, int line)
{
TestsListener::theInstance().errorsLog() <<
file << errmsgTag_inLine() << line << "\n" <<
"Reason: " << motiu << "\n";
TestsListener::theInstance().testHasFailed();
}