Mercurial > fife-parpg
comparison ext/UnitTest++/src/tests/TestChecks.cpp @ 37:0d325e9d5953
added unittest++ files into ext. Not hooked into build scripts yet
author | jasoka@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sat, 12 Jul 2008 12:00:57 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
36:6f1227f4932b | 37:0d325e9d5953 |
---|---|
1 #include "../UnitTest++.h" | |
2 #include "RecordingReporter.h" | |
3 | |
4 using namespace UnitTest; | |
5 | |
6 | |
7 namespace { | |
8 | |
9 | |
10 TEST(CheckEqualWithUnsignedLong) | |
11 { | |
12 TestResults results; | |
13 unsigned long something = 2; | |
14 CHECK_EQUAL( something, something ); | |
15 } | |
16 | |
17 TEST(CheckEqualsWithStringsFailsOnDifferentStrings) | |
18 { | |
19 char txt1[] = "Hello"; | |
20 char txt2[] = "Hallo"; | |
21 TestResults results; | |
22 CheckEqual(results, txt1, txt2, TestDetails("", "", "", 0)); | |
23 CHECK_EQUAL (1, results.GetFailureCount()); | |
24 } | |
25 | |
26 char txt1[] = "Hello"; // non-const on purpose so no folding of duplicate data | |
27 char txt2[] = "Hello"; | |
28 | |
29 TEST(CheckEqualsWithStringsWorksOnContentsNonConstNonConst) | |
30 { | |
31 char const* const p1 = txt1; | |
32 char const* const p2 = txt2; | |
33 TestResults results; | |
34 CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); | |
35 CHECK_EQUAL (0, results.GetFailureCount()); | |
36 } | |
37 | |
38 TEST(CheckEqualsWithStringsWorksOnContentsConstConst) | |
39 { | |
40 char* const p1 = txt1; | |
41 char* const p2 = txt2; | |
42 TestResults results; | |
43 CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); | |
44 CHECK_EQUAL (0, results.GetFailureCount()); | |
45 } | |
46 | |
47 TEST(CheckEqualsWithStringsWorksOnContentsNonConstConst) | |
48 { | |
49 char* const p1 = txt1; | |
50 char const* const p2 = txt2; | |
51 TestResults results; | |
52 CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); | |
53 CHECK_EQUAL (0, results.GetFailureCount()); | |
54 } | |
55 | |
56 TEST(CheckEqualsWithStringsWorksOnContentsConstNonConst) | |
57 { | |
58 char const* const p1 = txt1; | |
59 char* const p2 = txt2; | |
60 TestResults results; | |
61 CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); | |
62 CHECK_EQUAL (0, results.GetFailureCount()); | |
63 } | |
64 | |
65 TEST(CheckEqualsWithStringsWorksOnContentsWithALiteral) | |
66 { | |
67 char const* const p1 = txt1; | |
68 TestResults results; | |
69 CheckEqual(results, "Hello", p1, TestDetails("", "", "", 0)); | |
70 CHECK_EQUAL (0, results.GetFailureCount()); | |
71 } | |
72 | |
73 TEST(CheckEqualFailureIncludesCheckExpectedAndActual) | |
74 { | |
75 RecordingReporter reporter; | |
76 TestResults results(&reporter); | |
77 const int something = 2; | |
78 CheckEqual (results, 1, something, TestDetails("", "", "", 0)); | |
79 | |
80 CHECK (std::strstr(reporter.lastFailedMessage, "xpected 1")); | |
81 CHECK (std::strstr(reporter.lastFailedMessage, "was 2")); | |
82 } | |
83 | |
84 TEST(CheckEqualFailureIncludesDetails) | |
85 { | |
86 RecordingReporter reporter; | |
87 TestResults results(&reporter); | |
88 TestDetails const details("mytest", "mysuite", "file.h", 101); | |
89 | |
90 CheckEqual (results, 1, 2, details); | |
91 | |
92 CHECK_EQUAL("mytest", reporter.lastFailedTest); | |
93 CHECK_EQUAL("mysuite", reporter.lastFailedSuite); | |
94 CHECK_EQUAL("file.h", reporter.lastFailedFile); | |
95 CHECK_EQUAL(101, reporter.lastFailedLine); | |
96 } | |
97 | |
98 TEST(CheckCloseTrue) | |
99 { | |
100 TestResults results; | |
101 CheckClose(results, 3.001f, 3.0f, 0.1f, TestDetails("", "", "", 0)); | |
102 CHECK_EQUAL (0, results.GetFailureCount()); | |
103 } | |
104 | |
105 TEST(CheckCloseFalse) | |
106 { | |
107 TestResults results; | |
108 CheckClose(results, 3.12f, 3.0f, 0.1f, TestDetails("", "", "", 0)); | |
109 CHECK_EQUAL (1, results.GetFailureCount()); | |
110 } | |
111 | |
112 TEST(CheckCloseWithZeroEpsilonWorksForSameNumber) | |
113 { | |
114 TestResults results; | |
115 CheckClose(results, 0.1f, 0.1f, 0, TestDetails("", "", "", 0)); | |
116 CHECK_EQUAL (0, results.GetFailureCount()); | |
117 } | |
118 | |
119 TEST(CheckCloseWithNaNFails) | |
120 { | |
121 union | |
122 { | |
123 unsigned int bitpattern; | |
124 float nan; | |
125 }; | |
126 bitpattern = 0xFFFFFFFF; | |
127 TestResults results; | |
128 CheckClose(results, 3.0f, nan, 0.1f, TestDetails("", "", "", 0)); | |
129 CHECK_EQUAL (1, results.GetFailureCount()); | |
130 } | |
131 | |
132 TEST(CheckCloseWithNaNAgainstItselfFails) | |
133 { | |
134 union | |
135 { | |
136 unsigned int bitpattern; | |
137 float nan; | |
138 }; | |
139 bitpattern = 0xFFFFFFFF; | |
140 TestResults results; | |
141 CheckClose(results, nan, nan, 0.1f, TestDetails("", "", "", 0)); | |
142 CHECK_EQUAL (1, results.GetFailureCount()); | |
143 } | |
144 | |
145 TEST(CheckCloseFailureIncludesCheckExpectedAndActual) | |
146 { | |
147 RecordingReporter reporter; | |
148 TestResults results(&reporter); | |
149 const float expected = 0.9f; | |
150 const float actual = 1.1f; | |
151 CheckClose (results, expected, actual, 0.01f, TestDetails("", "", "", 0)); | |
152 | |
153 CHECK (std::strstr(reporter.lastFailedMessage, "xpected 0.9")); | |
154 CHECK (std::strstr(reporter.lastFailedMessage, "was 1.1")); | |
155 } | |
156 | |
157 TEST(CheckCloseFailureIncludesTolerance) | |
158 { | |
159 RecordingReporter reporter; | |
160 TestResults results(&reporter); | |
161 CheckClose (results, 2, 3, 0.01f, TestDetails("", "", "", 0)); | |
162 | |
163 CHECK (std::strstr(reporter.lastFailedMessage, "0.01")); | |
164 } | |
165 | |
166 TEST(CheckCloseFailureIncludesDetails) | |
167 { | |
168 RecordingReporter reporter; | |
169 TestResults results(&reporter); | |
170 TestDetails const details("mytest", "mysuite", "header.h", 10); | |
171 | |
172 CheckClose (results, 2, 3, 0.01f, details); | |
173 | |
174 CHECK_EQUAL("mytest", reporter.lastFailedTest); | |
175 CHECK_EQUAL("mysuite", reporter.lastFailedSuite); | |
176 CHECK_EQUAL("header.h", reporter.lastFailedFile); | |
177 CHECK_EQUAL(10, reporter.lastFailedLine); | |
178 } | |
179 | |
180 | |
181 TEST(CheckArrayEqualTrue) | |
182 { | |
183 TestResults results; | |
184 | |
185 int const array[3] = { 1, 2, 3 }; | |
186 CheckArrayEqual(results, array, array, 3, TestDetails("", "", "", 0)); | |
187 CHECK_EQUAL (0, results.GetFailureCount()); | |
188 } | |
189 | |
190 TEST(CheckArrayEqualFalse) | |
191 { | |
192 TestResults results; | |
193 | |
194 int const array1[3] = { 1, 2, 3 }; | |
195 int const array2[3] = { 1, 2, 2 }; | |
196 CheckArrayEqual(results, array1, array2, 3, TestDetails("", "", "", 0)); | |
197 CHECK_EQUAL (1, results.GetFailureCount()); | |
198 } | |
199 | |
200 TEST(CheckArrayCloseTrue) | |
201 { | |
202 TestResults results; | |
203 | |
204 float const array1[3] = { 1.0f, 1.5f, 2.0f }; | |
205 float const array2[3] = { 1.01f, 1.51f, 2.01f }; | |
206 CheckArrayClose(results, array1, array2, 3, 0.02f, TestDetails("", "", "", 0)); | |
207 CHECK_EQUAL (0, results.GetFailureCount()); | |
208 } | |
209 | |
210 TEST(CheckArrayCloseFalse) | |
211 { | |
212 TestResults results; | |
213 | |
214 float const array1[3] = { 1.0f, 1.5f, 2.0f }; | |
215 float const array2[3] = { 1.01f, 1.51f, 2.01f }; | |
216 CheckArrayClose(results, array1, array2, 3, 0.001f, TestDetails("", "", "", 0)); | |
217 CHECK_EQUAL (1, results.GetFailureCount()); | |
218 } | |
219 | |
220 TEST(CheckArrayCloseFailureIncludesDetails) | |
221 { | |
222 RecordingReporter reporter; | |
223 TestResults results(&reporter); | |
224 TestDetails const details("arrayCloseTest", "arrayCloseSuite", "file", 1337); | |
225 | |
226 float const array1[3] = { 1.0f, 1.5f, 2.0f }; | |
227 float const array2[3] = { 1.01f, 1.51f, 2.01f }; | |
228 CheckArrayClose(results, array1, array2, 3, 0.001f, details); | |
229 | |
230 CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); | |
231 CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); | |
232 CHECK_EQUAL("file", reporter.lastFailedFile); | |
233 CHECK_EQUAL(1337, reporter.lastFailedLine); | |
234 } | |
235 | |
236 | |
237 TEST(CheckArray2DCloseTrue) | |
238 { | |
239 TestResults results; | |
240 | |
241 float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, | |
242 { 2.0f, 2.5f, 3.0f }, | |
243 { 3.0f, 3.5f, 4.0f } }; | |
244 float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, | |
245 { 2.01f, 2.51f, 3.01f }, | |
246 { 3.01f, 3.51f, 4.01f } }; | |
247 CheckArray2DClose(results, array1, array2, 3, 3, 0.02f, TestDetails("", "", "", 0)); | |
248 CHECK_EQUAL (0, results.GetFailureCount()); | |
249 } | |
250 | |
251 TEST(CheckArray2DCloseFalse) | |
252 { | |
253 TestResults results; | |
254 | |
255 float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, | |
256 { 2.0f, 2.5f, 3.0f }, | |
257 { 3.0f, 3.5f, 4.0f } }; | |
258 float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, | |
259 { 2.01f, 2.51f, 3.01f }, | |
260 { 3.01f, 3.51f, 4.01f } }; | |
261 CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, TestDetails("", "", "", 0)); | |
262 CHECK_EQUAL (1, results.GetFailureCount()); | |
263 } | |
264 | |
265 TEST(CheckCloseWithDoublesSucceeds) | |
266 { | |
267 CHECK_CLOSE(0.5, 0.5, 0.0001); | |
268 } | |
269 | |
270 TEST(CheckArray2DCloseFailureIncludesDetails) | |
271 { | |
272 RecordingReporter reporter; | |
273 TestResults results(&reporter); | |
274 TestDetails const details("array2DCloseTest", "array2DCloseSuite", "file", 1234); | |
275 | |
276 float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, | |
277 { 2.0f, 2.5f, 3.0f }, | |
278 { 3.0f, 3.5f, 4.0f } }; | |
279 float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, | |
280 { 2.01f, 2.51f, 3.01f }, | |
281 { 3.01f, 3.51f, 4.01f } }; | |
282 CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, details); | |
283 | |
284 CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); | |
285 CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); | |
286 CHECK_EQUAL("file", reporter.lastFailedFile); | |
287 CHECK_EQUAL(1234, reporter.lastFailedLine); | |
288 } | |
289 | |
290 } |