- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

# Checks.h

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