1 #ifndef UNITTEST_CHECKS_H
2 #define UNITTEST_CHECKS_H
5 #include "TestResults.h"
6 #include "MemoryOutStream.h"
11 template<
typename Value >
12 bool Check(Value
const value)
18 template<
typename Expected,
typename Actual >
19 void CheckEqual(TestResults& results, Expected
const& expected, Actual
const& actual, TestDetails
const& details)
21 if (!(expected == actual))
23 UnitTest::MemoryOutStream stream;
24 stream <<
"Expected " << expected <<
" but was " << actual;
26 results.OnTestFailure(details, stream.GetText());
30 UNITTEST_LINKAGE
void CheckEqual(TestResults& results,
char const* expected,
char const* actual, TestDetails
const& details);
32 UNITTEST_LINKAGE
void CheckEqual(TestResults& results,
char* expected,
char* actual, TestDetails
const& details);
34 UNITTEST_LINKAGE
void CheckEqual(TestResults& results,
char* expected,
char const* actual, TestDetails
const& details);
36 UNITTEST_LINKAGE
void CheckEqual(TestResults& results,
char const* expected,
char* actual, TestDetails
const& details);
38 template<
typename Expected,
typename Actual,
typename Tolerance >
39 bool AreClose(Expected
const& expected, Actual
const& actual, Tolerance
const& tolerance)
41 return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance));
44 template<
typename Expected,
typename Actual,
typename Tolerance >
45 void CheckClose(TestResults& results, Expected
const& expected, Actual
const& actual, Tolerance
const& tolerance,
46 TestDetails
const& details)
48 if (!AreClose(expected, actual, tolerance))
50 UnitTest::MemoryOutStream stream;
51 stream <<
"Expected " << expected <<
" +/- " << tolerance <<
" but was " << actual;
53 results.OnTestFailure(details, stream.GetText());
58 template<
typename Expected,
typename Actual >
59 void CheckArrayEqual(TestResults& results, Expected
const& expected, Actual
const& actual,
60 int const count, TestDetails
const& details)
63 for (
int i = 0; i < count; ++i)
64 equal &= (expected[i] == actual[i]);
68 UnitTest::MemoryOutStream stream;
70 stream <<
"Expected [ ";
72 for (
int expectedIndex = 0; expectedIndex < count; ++expectedIndex)
73 stream << expected[expectedIndex] <<
" ";
75 stream <<
"] but was [ ";
77 for (
int actualIndex = 0; actualIndex < count; ++actualIndex)
78 stream << actual[actualIndex] <<
" ";
82 results.OnTestFailure(details, stream.GetText());
86 template<
typename Expected,
typename Actual,
typename Tolerance >
87 bool ArrayAreClose(Expected
const& expected, Actual
const& actual,
int const count, Tolerance
const& tolerance)
90 for (
int i = 0; i < count; ++i)
91 equal &= AreClose(expected[i], actual[i], tolerance);
95 template<
typename Expected,
typename Actual,
typename Tolerance >
96 void CheckArrayClose(TestResults& results, Expected
const& expected, Actual
const& actual,
97 int const count, Tolerance
const& tolerance, TestDetails
const& details)
99 bool equal = ArrayAreClose(expected, actual, count, tolerance);
103 UnitTest::MemoryOutStream stream;
105 stream <<
"Expected [ ";
106 for (
int expectedIndex = 0; expectedIndex < count; ++expectedIndex)
107 stream << expected[expectedIndex] <<
" ";
108 stream <<
"] +/- " << tolerance <<
" but was [ ";
110 for (
int actualIndex = 0; actualIndex < count; ++actualIndex)
111 stream << actual[actualIndex] <<
" ";
114 results.OnTestFailure(details, stream.GetText());
118 template<
typename Expected,
typename Actual,
typename Tolerance >
119 void CheckArray2DClose(TestResults& results, Expected
const& expected, Actual
const& actual,
120 int const rows,
int const columns, Tolerance
const& tolerance, TestDetails
const& details)
123 for (
int i = 0; i < rows; ++i)
124 equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance);
128 UnitTest::MemoryOutStream stream;
130 stream <<
"Expected [ ";
132 for (
int expectedRow = 0; expectedRow < rows; ++expectedRow)
135 for (
int expectedColumn = 0; expectedColumn < columns; ++expectedColumn)
136 stream << expected[expectedRow][expectedColumn] <<
" ";
140 stream <<
"] +/- " << tolerance <<
" but was [ ";
142 for (
int actualRow = 0; actualRow < rows; ++actualRow)
145 for (
int actualColumn = 0; actualColumn < columns; ++actualColumn)
146 stream << actual[actualRow][actualColumn] <<
" ";
152 results.OnTestFailure(details, stream.GetText());