00001 #ifndef UNITTEST_CHECKS_H
00002 #define UNITTEST_CHECKS_H
00003
00004 #include "Config.h"
00005 #include "TestResults.h"
00006 #include "MemoryOutStream.h"
00007
00008 namespace UnitTest {
00009
00010
00011 template< typename Value >
00012 bool Check(Value const value)
00013 {
00014 return !!value;
00015 }
00016
00017
00018 template< typename Expected, typename Actual >
00019 void CheckEqual(TestResults& results, Expected const& expected, Actual const& actual, TestDetails const& details)
00020 {
00021 if (!(expected == actual))
00022 {
00023 UnitTest::MemoryOutStream stream;
00024 stream << "Expected " << expected << " but was " << actual;
00025
00026 results.OnTestFailure(details, stream.GetText());
00027 }
00028 }
00029
00030 void CheckEqual(TestResults& results, char const* expected, char const* actual, TestDetails const& details);
00031
00032 void CheckEqual(TestResults& results, char* expected, char* actual, TestDetails const& details);
00033
00034 void CheckEqual(TestResults& results, char* expected, char const* actual, TestDetails const& details);
00035
00036 void CheckEqual(TestResults& results, char const* expected, char* actual, TestDetails const& details);
00037
00038 template< typename Expected, typename Actual, typename Tolerance >
00039 bool AreClose(Expected const& expected, Actual const& actual, Tolerance const& tolerance)
00040 {
00041 return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance));
00042 }
00043
00044 template< typename Expected, typename Actual, typename Tolerance >
00045 void CheckClose(TestResults& results, Expected const& expected, Actual const& actual, Tolerance const& tolerance,
00046 TestDetails const& details)
00047 {
00048 if (!AreClose(expected, actual, tolerance))
00049 {
00050 UnitTest::MemoryOutStream stream;
00051 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
00052
00053 results.OnTestFailure(details, stream.GetText());
00054 }
00055 }
00056
00057
00058 template< typename Expected, typename Actual >
00059 void CheckArrayEqual(TestResults& results, Expected const& expected, Actual const& actual,
00060 int const count, TestDetails const& details)
00061 {
00062 bool equal = true;
00063 for (int i = 0; i < count; ++i)
00064 equal &= (expected[i] == actual[i]);
00065
00066 if (!equal)
00067 {
00068 UnitTest::MemoryOutStream stream;
00069
00070 stream << "Expected [ ";
00071
00072 for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex)
00073 stream << expected[expectedIndex] << " ";
00074
00075 stream << "] but was [ ";
00076
00077 for (int actualIndex = 0; actualIndex < count; ++actualIndex)
00078 stream << actual[actualIndex] << " ";
00079
00080 stream << "]";
00081
00082 results.OnTestFailure(details, stream.GetText());
00083 }
00084 }
00085
00086 template< typename Expected, typename Actual, typename Tolerance >
00087 bool ArrayAreClose(Expected const& expected, Actual const& actual, int const count, Tolerance const& tolerance)
00088 {
00089 bool equal = true;
00090 for (int i = 0; i < count; ++i)
00091 equal &= AreClose(expected[i], actual[i], tolerance);
00092 return equal;
00093 }
00094
00095 template< typename Expected, typename Actual, typename Tolerance >
00096 void CheckArrayClose(TestResults& results, Expected const& expected, Actual const& actual,
00097 int const count, Tolerance const& tolerance, TestDetails const& details)
00098 {
00099 bool equal = ArrayAreClose(expected, actual, count, tolerance);
00100
00101 if (!equal)
00102 {
00103 UnitTest::MemoryOutStream stream;
00104
00105 stream << "Expected [ ";
00106 for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex)
00107 stream << expected[expectedIndex] << " ";
00108 stream << "] +/- " << tolerance << " but was [ ";
00109
00110 for (int actualIndex = 0; actualIndex < count; ++actualIndex)
00111 stream << actual[actualIndex] << " ";
00112 stream << "]";
00113
00114 results.OnTestFailure(details, stream.GetText());
00115 }
00116 }
00117
00118 template< typename Expected, typename Actual, typename Tolerance >
00119 void CheckArray2DClose(TestResults& results, Expected const& expected, Actual const& actual,
00120 int const rows, int const columns, Tolerance const& tolerance, TestDetails const& details)
00121 {
00122 bool equal = true;
00123 for (int i = 0; i < rows; ++i)
00124 equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance);
00125
00126 if (!equal)
00127 {
00128 UnitTest::MemoryOutStream stream;
00129
00130 stream << "Expected [ ";
00131
00132 for (int expectedRow = 0; expectedRow < rows; ++expectedRow)
00133 {
00134 stream << "[ ";
00135 for (int expectedColumn = 0; expectedColumn < columns; ++expectedColumn)
00136 stream << expected[expectedRow][expectedColumn] << " ";
00137 stream << "] ";
00138 }
00139
00140 stream << "] +/- " << tolerance << " but was [ ";
00141
00142 for (int actualRow = 0; actualRow < rows; ++actualRow)
00143 {
00144 stream << "[ ";
00145 for (int actualColumn = 0; actualColumn < columns; ++actualColumn)
00146 stream << actual[actualRow][actualColumn] << " ";
00147 stream << "] ";
00148 }
00149
00150 stream << "]";
00151
00152 results.OnTestFailure(details, stream.GetText());
00153 }
00154 }
00155
00156 }
00157
00158 #endif