1 | #ifndef UNITTEST_CHECKS_H |
---|
2 | #define UNITTEST_CHECKS_H |
---|
3 | |
---|
4 | #include "Config.h" |
---|
5 | #include "TestResults.h" |
---|
6 | #include "MemoryOutStream.h" |
---|
7 | |
---|
8 | namespace UnitTest { |
---|
9 | |
---|
10 | |
---|
11 | template< typename Value > |
---|
12 | bool Check ( Value const value ) { |
---|
13 | return !!value; // doing double negative to avoid silly VS warnings |
---|
14 | } |
---|
15 | |
---|
16 | |
---|
17 | template< typename Expected, typename Actual > |
---|
18 | void CheckEqual ( TestResults& results, Expected const& expected, Actual const& actual, TestDetails const& details ) { |
---|
19 | if ( ! ( expected == actual ) ) { |
---|
20 | UnitTest::MemoryOutStream stream; |
---|
21 | stream << "Expected " << expected << " but was " << actual; |
---|
22 | |
---|
23 | results.OnTestFailure ( details, stream.GetText() ); |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | void CheckEqual ( TestResults& results, char const* expected, char const* actual, TestDetails const& details ); |
---|
28 | |
---|
29 | void CheckEqual ( TestResults& results, char* expected, char* actual, TestDetails const& details ); |
---|
30 | |
---|
31 | void CheckEqual ( TestResults& results, char* expected, char const* actual, TestDetails const& details ); |
---|
32 | |
---|
33 | void CheckEqual ( TestResults& results, char const* expected, char* actual, TestDetails const& details ); |
---|
34 | |
---|
35 | template< typename Expected, typename Actual, typename Tolerance > |
---|
36 | bool AreClose ( Expected const& expected, Actual const& actual, Tolerance const& tolerance ) { |
---|
37 | return ( actual >= ( expected - tolerance ) ) && ( actual <= ( expected + tolerance ) ); |
---|
38 | } |
---|
39 | |
---|
40 | template< typename Expected, typename Actual, typename Tolerance > |
---|
41 | void CheckClose ( TestResults& results, Expected const& expected, Actual const& actual, Tolerance const& tolerance, |
---|
42 | TestDetails const& details ) { |
---|
43 | if ( !AreClose ( expected, actual, tolerance ) ) { |
---|
44 | UnitTest::MemoryOutStream stream; |
---|
45 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
46 | |
---|
47 | results.OnTestFailure ( details, stream.GetText() ); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | template< typename Expected, typename Actual > |
---|
53 | void CheckArrayEqual ( TestResults& results, Expected const& expected, Actual const& actual, |
---|
54 | int const count, TestDetails const& details ) { |
---|
55 | bool equal = true; |
---|
56 | for ( int i = 0; i < count; ++i ) |
---|
57 | equal &= ( expected[i] == actual[i] ); |
---|
58 | |
---|
59 | if ( !equal ) { |
---|
60 | UnitTest::MemoryOutStream stream; |
---|
61 | |
---|
62 | stream << "Expected [ "; |
---|
63 | |
---|
64 | for ( int expectedIndex = 0; expectedIndex < count; ++expectedIndex ) |
---|
65 | stream << expected[expectedIndex] << " "; |
---|
66 | |
---|
67 | stream << "] but was [ "; |
---|
68 | |
---|
69 | for ( int actualIndex = 0; actualIndex < count; ++actualIndex ) |
---|
70 | stream << actual[actualIndex] << " "; |
---|
71 | |
---|
72 | stream << "]"; |
---|
73 | |
---|
74 | results.OnTestFailure ( details, stream.GetText() ); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | template< typename Expected, typename Actual, typename Tolerance > |
---|
79 | bool ArrayAreClose ( Expected const& expected, Actual const& actual, int const count, Tolerance const& tolerance ) { |
---|
80 | bool equal = true; |
---|
81 | for ( int i = 0; i < count; ++i ) |
---|
82 | equal &= AreClose ( expected[i], actual[i], tolerance ); |
---|
83 | return equal; |
---|
84 | } |
---|
85 | |
---|
86 | template< typename Expected, typename Actual, typename Tolerance > |
---|
87 | void CheckArrayClose ( TestResults& results, Expected const& expected, Actual const& actual, |
---|
88 | int const count, Tolerance const& tolerance, TestDetails const& details ) { |
---|
89 | bool equal = ArrayAreClose ( expected, actual, count, tolerance ); |
---|
90 | |
---|
91 | if ( !equal ) { |
---|
92 | UnitTest::MemoryOutStream stream; |
---|
93 | |
---|
94 | stream << "Expected [ "; |
---|
95 | for ( int expectedIndex = 0; expectedIndex < count; ++expectedIndex ) |
---|
96 | stream << expected[expectedIndex] << " "; |
---|
97 | stream << "] +/- " << tolerance << " but was [ "; |
---|
98 | |
---|
99 | for ( int actualIndex = 0; actualIndex < count; ++actualIndex ) |
---|
100 | stream << actual[actualIndex] << " "; |
---|
101 | stream << "]"; |
---|
102 | |
---|
103 | results.OnTestFailure ( details, stream.GetText() ); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | template< typename Expected, typename Actual, typename Tolerance > |
---|
108 | void CheckArray2DClose ( TestResults& results, Expected const& expected, Actual const& actual, |
---|
109 | int const rows, int const columns, Tolerance const& tolerance, TestDetails const& details ) { |
---|
110 | bool equal = true; |
---|
111 | for ( int i = 0; i < rows; ++i ) |
---|
112 | equal &= ArrayAreClose ( expected[i], actual[i], columns, tolerance ); |
---|
113 | |
---|
114 | if ( !equal ) { |
---|
115 | UnitTest::MemoryOutStream stream; |
---|
116 | |
---|
117 | stream << "Expected [ "; |
---|
118 | |
---|
119 | for ( int expectedRow = 0; expectedRow < rows; ++expectedRow ) { |
---|
120 | stream << "[ "; |
---|
121 | for ( int expectedColumn = 0; expectedColumn < columns; ++expectedColumn ) |
---|
122 | stream << expected[expectedRow][expectedColumn] << " "; |
---|
123 | stream << "] "; |
---|
124 | } |
---|
125 | |
---|
126 | stream << "] +/- " << tolerance << " but was [ "; |
---|
127 | |
---|
128 | for ( int actualRow = 0; actualRow < rows; ++actualRow ) { |
---|
129 | stream << "[ "; |
---|
130 | for ( int actualColumn = 0; actualColumn < columns; ++actualColumn ) |
---|
131 | stream << actual[actualRow][actualColumn] << " "; |
---|
132 | stream << "] "; |
---|
133 | } |
---|
134 | |
---|
135 | stream << "]"; |
---|
136 | |
---|
137 | results.OnTestFailure ( details, stream.GetText() ); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | #endif |
---|