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
|
#include <iostream>
#include <string>
using namespace std;
#include <stdio.h> // f�r formatierte Ausgabe im C-Stil
#define BUFLEN 160 // f�r Ausgabe
#include "TestCase.h"
// Konstruktor
TestCase::TestCase( char* name )
: testsRun(0), testsFailed(0), testsOK(0)
{
if (name != 0)
strncpy( tcName, name, sizeof(tcName)-1 );
else
tcName[0] = 0; // leerer String
}
// Hinzuf�gen von Testfunktionen
void TestCase::addTest( TESTFUNC f )
{
vecFkt.push_back( f );
}
// Test-Run f�r Test Case
void TestCase::run()
{
testsRun = 0; // Z�hler initialisieren
testsFailed = 0;
testsOK = 0;
// Setup f�r Test
setUp();
for (int i = 0; i < vecFkt.size(); i++)
{
tL = 0; // falls nicht gesetzt durch Fkt
tN = ""; // falls nicht gesetzt durch Fkt
bool success = true; // init
try {
testsRun++; // Tests mitz�hlen
(this->*vecFkt[i])(); // Testfunktion aufrufen
}
catch ( string msg ) // Fehlerbehandlung
{
success = false; // Fehler Markieren
cout << "Fehler " << tcName << '.' << tN
<< " Zeile " << tL << " "
<< msg << endl;
}
// Auswertung des Tests
if (success)
{
testsOK++;
cout << "OK: " << tcName << '.' << tN << endl;
}
else
{
testsFailed++;
}
} // end for
// Tear Down f�r Test
tearDown();
cout << ">>> " << tcName << ":Tests durchgefuehrt: " << testsRun
<< ", o.k: " << testsOK
<< ", fehlgeschlagen: " << testsFailed
<< endl;
}
void TestCase::assertEquals( int a, int b )
{
if (a != b)
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %d, ist: %d",
"assertEquals(int, int)", a, b);
throw ( *(new string( buf )) );
}
}
void TestCase::assertEquals( long a, long b)
{
if (a != b)
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %d, ist: %d",
"assertEquals(long,long)", a, b);
throw ( *(new string( buf )) );
}
}
void TestCase::assertEquals( float a, float b)
{
if (a != b)
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %f, ist: %f",
"assertEquals(float,float)", a, b);
throw ( *(new string( buf )) );
}
}
void TestCase::assertEquals( double a, double b )
{
if (a != b)
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %f, ist: %f",
"assertEquals(double,double)", a, b);
throw ( *(new string( buf )) );
}
}
void TestCase::assertEquals( double a, double b, double epsilon )
{
double diff = ( a >= b ) ? (a-b) : (b-a);
if ( diff > epsilon )
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %f, ist: %f, epsilon: %e",
"assertEquals(double,double,double)", a, b, epsilon);
throw ( *(new string( buf )) );
}
}
void TestCase::assertEquals( bool a, bool b )
{
if (a != b)
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %s, ist: %s",
"assertEquals(bool,bool)",
((a==true) ? "true" : "false"), ((b==true) ? "true" : "false"));
throw ( *(new string( buf )) );
}
}
void TestCase::assertEquals( void* a, void* b)
{
if (a != b)
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %x, ist: %x",
"assertEquals(void*,void*)", a, b );
throw ( *(new string( buf )) );
}
}
void TestCase::assertEquals( char* a, char* b )
{
if ( strcmp( a, b ) != 0 )
{
char buf[BUFLEN]; // Achtung L�nge hier kritisch!
const int StrMaxLen = 48; //!< Ausgabe Einzelstring begrenzen
string sa( a ); // Zuweisung auf Klasse string
string sb( b ); // erleichtert Manipulationen
if (sa.length() > StrMaxLen)
sa = sa.substr( 0, StrMaxLen );
if (sb.length() > StrMaxLen)
sb = sb.substr( 0, StrMaxLen );
sprintf( buf, "%s: soll: %s, ist: %s",
"assertEquals(char*,char*)", sa.c_str(), sb.c_str() );
throw ( *(new string( buf )) );
}
}
void TestCase::assertTrue( bool a )
{
if (!a)
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %s",
"assertTrue(bool,bool)",
((a==true) ? "true" : "false") );
throw ( *(new string( buf )) );
}
}
void TestCase::assertFalse( bool a)
{
if (a)
{
char buf[BUFLEN];
sprintf( buf, "%s: soll: %s",
"assertFalse(bool,bool)",
((a==true) ? "true" : "false") );
throw ( *(new string( buf )) );
}
}
void TestCase::assertMessage( char* msg )
{
char buf[BUFLEN];
sprintf( buf, "%s: Meldung: %s",
"assertMessage( char* )", msg );
throw ( *(new string( buf )) );
}
|