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
|
#ifndef UNITTEST_CHECKS_H
#define UNITTEST_CHECKS_H
#include "Config.h"
#include "TestResults.h"
#include "MemoryOutStream.h"
namespace UnitTest {
template< typename Value >
bool Check(Value const value)
{
return !!value; // doing double negative to avoid silly VS warnings
}
template< typename Expected, typename Actual >
void CheckEqual(TestResults& results, Expected const expected, Actual const actual, TestDetails const& details)
{
if (!(expected == actual))
{
UnitTest::MemoryOutStream stream;
stream << "Expected " << expected << " but was " << actual;
results.OnTestFailure(details, stream.GetText());
}
}
void CheckEqual(TestResults& results, char const* expected, char const* actual, TestDetails const& details);
void CheckEqual(TestResults& results, char* expected, char* actual, TestDetails const& details);
void CheckEqual(TestResults& results, char* expected, char const* actual, TestDetails const& details);
void CheckEqual(TestResults& results, char const* expected, char* actual, TestDetails const& details);
template< typename Expected, typename Actual, typename Tolerance >
bool AreClose(Expected const expected, Actual const actual, Tolerance const tolerance)
{
return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance));
}
template< typename Expected, typename Actual, typename Tolerance >
void CheckClose(TestResults& results, Expected const expected, Actual const actual, Tolerance const tolerance,
TestDetails const& details)
{
if (!AreClose(expected, actual, tolerance))
{
UnitTest::MemoryOutStream stream;
stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
results.OnTestFailure(details, stream.GetText());
}
}
template< typename Expected, typename Actual >
void CheckArrayEqual(TestResults& results, Expected const expected, Actual const actual,
int const count, TestDetails const& details)
{
bool equal = true;
for (int i = 0; i < count; ++i)
equal &= (expected[i] == actual[i]);
if (!equal)
{
UnitTest::MemoryOutStream stream;
stream << "Expected [ ";
for (int i = 0; i < count; ++i)
stream << expected[i] << " ";
stream << "] but was [ ";
for (int i = 0; i < count; ++i)
stream << actual[i] << " ";
stream << "]";
results.OnTestFailure(details, stream.GetText());
}
}
template< typename Expected, typename Actual, typename Tolerance >
bool ArrayAreClose(Expected const expected, Actual const actual, int const count, Tolerance const tolerance)
{
bool equal = true;
for (int i = 0; i < count; ++i)
equal &= AreClose(expected[i], actual[i], tolerance);
return equal;
}
template< typename Expected, typename Actual, typename Tolerance >
void CheckArrayClose(TestResults& results, Expected const expected, Actual const actual,
int const count, Tolerance const tolerance, TestDetails const& details)
{
bool equal = ArrayAreClose(expected, actual, count, tolerance);
if (!equal)
{
UnitTest::MemoryOutStream stream;
stream << "Expected [ ";
for (int i = 0; i < count; ++i)
stream << expected[i] << " ";
stream << "] +/- " << tolerance << " but was [ ";
for (int i = 0; i < count; ++i)
stream << actual[i] << " ";
stream << "]";
results.OnTestFailure(details, stream.GetText());
}
}
template< typename Expected, typename Actual, typename Tolerance >
void CheckArray2DClose(TestResults& results, Expected const expected, Actual const actual,
int const rows, int const columns, Tolerance const tolerance, TestDetails const& details)
{
bool equal = true;
for (int i = 0; i < rows; ++i)
equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance);
if (!equal)
{
UnitTest::MemoryOutStream stream;
stream << "Expected [ ";
for (int i = 0; i < rows; ++i)
{
stream << "[ ";
for (int j = 0; j < columns; ++j)
stream << expected[i][j] << " ";
stream << "] ";
}
stream << "] +/- " << tolerance << " but was [ ";
for (int i = 0; i < rows; ++i)
{
stream << "[ ";
for (int j = 0; j < columns; ++j)
stream << actual[i][j] << " ";
stream << "] ";
}
stream << "]";
results.OnTestFailure(details, stream.GetText());
}
}
}
#endif
|