/[projet1]/public/pc/shared_libraries/unittestcpp/v1.4/src/Checks.h
Defence Force logotype

Contents of /public/pc/shared_libraries/unittestcpp/v1.4/src/Checks.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 172 - (show annotations)
Sat Nov 28 13:10:37 2009 UTC (10 years, 2 months ago) by dbug
File MIME type: text/plain
File size: 5126 byte(s)
Added the source code of "Save The Earth", the Atari STe demo presented at Kindergarden 2009
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 {
14 return !!value; // doing double negative to avoid silly VS warnings
15 }
16
17
18 template< typename Expected, typename Actual >
19 void CheckEqual(TestResults& results, Expected const& expected, Actual const& actual, TestDetails const& details)
20 {
21 if (!(expected == actual))
22 {
23 UnitTest::MemoryOutStream stream;
24 stream << "Expected ";
25 stream << expected;
26 stream << " but was ";
27 stream << actual;
28
29 results.OnTestFailure(details, stream.GetText());
30 }
31 }
32
33 void CheckEqual(TestResults& results, char const* expected, char const* actual, TestDetails const& details);
34
35 void CheckEqual(TestResults& results, char* expected, char* actual, TestDetails const& details);
36
37 void CheckEqual(TestResults& results, char* expected, char const* actual, TestDetails const& details);
38
39 void CheckEqual(TestResults& results, char const* expected, char* actual, TestDetails const& details);
40
41 template< typename Expected, typename Actual, typename Tolerance >
42 bool AreClose(Expected const& expected, Actual const& actual, Tolerance const& tolerance)
43 {
44 return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance));
45 }
46
47 template< typename Expected, typename Actual, typename Tolerance >
48 void CheckClose(TestResults& results, Expected const& expected, Actual const& actual, Tolerance const& tolerance,
49 TestDetails const& details)
50 {
51 if (!AreClose(expected, actual, tolerance))
52 {
53 UnitTest::MemoryOutStream stream;
54 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
55
56 results.OnTestFailure(details, stream.GetText());
57 }
58 }
59
60
61 template< typename Expected, typename Actual >
62 void CheckArrayEqual(TestResults& results, Expected const& expected, Actual const& actual,
63 int const count, TestDetails const& details)
64 {
65 bool equal = true;
66 for (int i = 0; i < count; ++i)
67 equal &= (expected[i] == actual[i]);
68
69 if (!equal)
70 {
71 UnitTest::MemoryOutStream stream;
72
73 stream << "Expected [ ";
74
75 for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex)
76 stream << expected[expectedIndex] << " ";
77
78 stream << "] but was [ ";
79
80 for (int actualIndex = 0; actualIndex < count; ++actualIndex)
81 stream << actual[actualIndex] << " ";
82
83 stream << "]";
84
85 results.OnTestFailure(details, stream.GetText());
86 }
87 }
88
89 template< typename Expected, typename Actual, typename Tolerance >
90 bool ArrayAreClose(Expected const& expected, Actual const& actual, int const count, Tolerance const& tolerance)
91 {
92 bool equal = true;
93 for (int i = 0; i < count; ++i)
94 equal &= AreClose(expected[i], actual[i], tolerance);
95 return equal;
96 }
97
98 template< typename Expected, typename Actual, typename Tolerance >
99 void CheckArrayClose(TestResults& results, Expected const& expected, Actual const& actual,
100 int const count, Tolerance const& tolerance, TestDetails const& details)
101 {
102 bool equal = ArrayAreClose(expected, actual, count, tolerance);
103
104 if (!equal)
105 {
106 UnitTest::MemoryOutStream stream;
107
108 stream << "Expected [ ";
109 for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex)
110 stream << expected[expectedIndex] << " ";
111 stream << "] +/- " << tolerance << " but was [ ";
112
113 for (int actualIndex = 0; actualIndex < count; ++actualIndex)
114 stream << actual[actualIndex] << " ";
115 stream << "]";
116
117 results.OnTestFailure(details, stream.GetText());
118 }
119 }
120
121 template< typename Expected, typename Actual, typename Tolerance >
122 void CheckArray2DClose(TestResults& results, Expected const& expected, Actual const& actual,
123 int const rows, int const columns, Tolerance const& tolerance, TestDetails const& details)
124 {
125 bool equal = true;
126 for (int i = 0; i < rows; ++i)
127 equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance);
128
129 if (!equal)
130 {
131 UnitTest::MemoryOutStream stream;
132
133 stream << "Expected [ ";
134
135 for (int expectedRow = 0; expectedRow < rows; ++expectedRow)
136 {
137 stream << "[ ";
138 for (int expectedColumn = 0; expectedColumn < columns; ++expectedColumn)
139 stream << expected[expectedRow][expectedColumn] << " ";
140 stream << "] ";
141 }
142
143 stream << "] +/- " << tolerance << " but was [ ";
144
145 for (int actualRow = 0; actualRow < rows; ++actualRow)
146 {
147 stream << "[ ";
148 for (int actualColumn = 0; actualColumn < columns; ++actualColumn)
149 stream << actual[actualRow][actualColumn] << " ";
150 stream << "] ";
151 }
152
153 stream << "]";
154
155 results.OnTestFailure(details, stream.GetText());
156 }
157 }
158
159 }
160
161 #endif

  ViewVC Help
Powered by ViewVC 1.1.26