Debian Codegraph @

tests/CppUTest/TestFailureTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/TestOutput.h"
30
31 namespace
32 {
33 const int failLineNumber = 2;
34 const char* failFileName = "fail.cpp";
35 }
36
37 TEST_GROUP(TestFailure)
38 {
39 UtestShell* test;
40
41 void setup()
42 {
43 test = new UtestShell("groupname", "testname", failFileName, failLineNumber-1);
44 }
45 void teardown()
46 {
47 delete test;
48 }
49 };
50 #define FAILURE_EQUAL(a, b) STRCMP_EQUAL_LOCATION(a, b.getMessage().asCharString(), "", __FILE__, __LINE__)
51
52 TEST(TestFailure, CreateFailure)
53 {
54 TestFailure f1(test, failFileName, failLineNumber, "the failure message");
55 TestFailure f2(test, "the failure message");
56 TestFailure f3(test, failFileName, failLineNumber);
57 }
58
59 TEST(TestFailure, GetTestFileAndLineFromFailure)
60 {
61 TestFailure f1(test, failFileName, failLineNumber, "the failure message");
62 STRCMP_EQUAL(failFileName, f1.getTestFileName().asCharString());
63 LONGS_EQUAL(1, f1.getTestLineNumber());
64 }
65
66 TEST(TestFailure, EqualsFailureWithText)
67 {
68 EqualsFailure f(test, failFileName, failLineNumber, "expected", "actual", "text");
69 FAILURE_EQUAL("Message: text\n"
70 "\texpected <expected>\n\tbut was <actual>", f);
71 }
72
73 TEST(TestFailure, EqualsFailure)
74 {
75 EqualsFailure f(test, failFileName, failLineNumber, "expected", "actual", "");
76 FAILURE_EQUAL("expected <expected>\n\tbut was <actual>", f);
77 }
78
79 TEST(TestFailure, EqualsFailureWithNullAsActual)
80 {
81 EqualsFailure f(test, failFileName, failLineNumber, "expected", NULLPTR, "");
82 FAILURE_EQUAL("expected <expected>\n\tbut was <(null)>", f);
83 }
84
85 TEST(TestFailure, EqualsFailureWithNullAsExpected)
86 {
87 EqualsFailure f(test, failFileName, failLineNumber, NULLPTR, "actual", "");
88 FAILURE_EQUAL("expected <(null)>\n\tbut was <actual>", f);
89 }
90
91 TEST(TestFailure, CheckEqualFailureWithText)
92 {
93 CheckEqualFailure f(test, failFileName, failLineNumber, "expected", "actual", "text");
94 FAILURE_EQUAL("Message: text\n"
95 "\texpected <expected>\n"
96 "\tbut was <actual>\n"
97 "\tdifference starts at position 0 at: < actual >\n"
98 "\t ^", f);
99 }
100
101 TEST(TestFailure, CheckEqualFailure)
102 {
103 CheckEqualFailure f(test, failFileName, failLineNumber, "expected", "actual", "");
104 FAILURE_EQUAL("expected <expected>\n"
105 "\tbut was <actual>\n"
106 "\tdifference starts at position 0 at: < actual >\n"
107 "\t ^", f);
108 }
109
110 TEST(TestFailure, CheckFailure)
111 {
112 CheckFailure f(test, failFileName, failLineNumber, "CHECK", "chk");
113 FAILURE_EQUAL("CHECK(chk) failed", f);
114 }
115
116 TEST(TestFailure, CheckFailureWithText)
117 {
118 CheckFailure f(test, failFileName, failLineNumber, "CHECK", "chk", "text");
119 FAILURE_EQUAL("Message: text\n"
120 "\tCHECK(chk) failed", f);
121 }
122
123 TEST(TestFailure, FailFailure)
124 {
125 FailFailure f(test, failFileName, failLineNumber, "chk");
126 FAILURE_EQUAL("chk", f);
127 }
128
129 TEST(TestFailure, LongsEqualFailureWithText)
130 {
131 LongsEqualFailure f(test, failFileName, failLineNumber, 1, 2, "text");
132 FAILURE_EQUAL("Message: text\n"
133 "\texpected <1 (0x1)>\n\tbut was <2 (0x2)>", f);
134 }
135
136 TEST(TestFailure, LongsEqualFailure)
137 {
138 LongsEqualFailure f(test, failFileName, failLineNumber, 1, 2, "");
139 FAILURE_EQUAL("expected <1 (0x1)>\n\tbut was <2 (0x2)>", f);
140 }
141
142 TEST(TestFailure, LongLongsEqualFailure)
143 {
144 #ifdef CPPUTEST_USE_LONG_LONG
145 LongLongsEqualFailure f(test, failFileName, failLineNumber, 1, 2, "");
146 FAILURE_EQUAL("expected <1 (0x1)>\n\tbut was <2 (0x2)>", f);
147 #else
148 cpputest_longlong dummy_longlong;
149 LongLongsEqualFailure f(test, failFileName, failLineNumber, dummy_longlong, dummy_longlong, "");
150 FAILURE_EQUAL("expected <<longlong_unsupported> >\n\tbut was <<longlong_unsupported> >", f);
151 #endif
152 }
153
154 TEST(TestFailure, UnsignedLongLongsEqualFailure)
155 {
156 #ifdef CPPUTEST_USE_LONG_LONG
157 UnsignedLongLongsEqualFailure f(test, failFileName, failLineNumber, 1, 2, "");
158 FAILURE_EQUAL("expected <1 (0x1)>\n\tbut was <2 (0x2)>", f);
159 #else
160 cpputest_ulonglong dummy_ulonglong;
161 UnsignedLongLongsEqualFailure f(test, failFileName, failLineNumber, dummy_ulonglong, dummy_ulonglong, "");
162 FAILURE_EQUAL("expected <<ulonglong_unsupported> >\n\tbut was <<ulonglong_unsupported> >", f);
163 #endif
164 }
165
166 TEST(TestFailure, SignedBytesEqualFailure)
167 {
168 SignedBytesEqualFailure f(test, failFileName, failLineNumber, (signed char)-1, (signed char)2, "");
169 FAILURE_EQUAL("expected <-1 (0xff)>\n\tbut was < 2 (0x2)>", f);
170 }
171
172 TEST(TestFailure, StringsEqualFailureWithText)
173 {
174 StringEqualFailure f(test, failFileName, failLineNumber, "abc", "abd", "text");
175 FAILURE_EQUAL("Message: text\n"
176 "\texpected <abc>\n"
177 "\tbut was <abd>\n"
178 "\tdifference starts at position 2 at: < abd >\n"
179 "\t ^", f);
180 }
181
182 TEST(TestFailure, StringsEqualFailure)
183 {
184 StringEqualFailure f(test, failFileName, failLineNumber, "abc", "abd", "");
185 FAILURE_EQUAL("expected <abc>\n"
186 "\tbut was <abd>\n"
187 "\tdifference starts at position 2 at: < abd >\n"
188 "\t ^", f);
189 }
190
191 TEST(TestFailure, StringsEqualFailureAtTheEnd)
192 {
193 StringEqualFailure f(test, failFileName, failLineNumber, "abc", "ab", "");
194 FAILURE_EQUAL("expected <abc>\n"
195 "\tbut was <ab>\n"
196 "\tdifference starts at position 2 at: < ab >\n"
197 "\t ^", f);
198 }
199
200 TEST(TestFailure, StringsEqualFailureNewVariantAtTheEnd)
201 {
202 StringEqualFailure f(test, failFileName, failLineNumber, "EndOfALongerString", "EndOfALongerStrinG", "");
203 FAILURE_EQUAL("expected <EndOfALongerString>\n"
204 "\tbut was <EndOfALongerStrinG>\n"
205 "\tdifference starts at position 17 at: <ongerStrinG >\n"
206 "\t ^", f);
207 }
208
209 TEST(TestFailure, StringsEqualFailureWithNewLinesAndTabs)
210 {
211 StringEqualFailure f(test, failFileName, failLineNumber,
212 "StringWith\t\nDifferentString",
213 "StringWith\t\ndifferentString", "");
214
215 FAILURE_EQUAL("expected <StringWith\t\nDifferentString>\n"
216 "\tbut was <StringWith\t\ndifferentString>\n"
217 "\tdifference starts at position 12 at: <ringWith\t\ndifferentS>\n"
218 "\t \t\n^", f);
219 }
220
221 TEST(TestFailure, StringsEqualFailureInTheMiddle)
222 {
223 StringEqualFailure f(test, failFileName, failLineNumber, "aa", "ab", "");
224 FAILURE_EQUAL("expected <aa>\n"
225 "\tbut was <ab>\n"
226 "\tdifference starts at position 1 at: < ab >\n"
227 "\t ^", f);
228 }
229
230 TEST(TestFailure, StringsEqualFailureAtTheBeginning)
231 {
232 StringEqualFailure f(test, failFileName, failLineNumber, "aaa", "bbb", "");
233 FAILURE_EQUAL("expected <aaa>\n"
234 "\tbut was <bbb>\n"
235 "\tdifference starts at position 0 at: < bbb >\n"
236 "\t ^", f);
237 }
238
239 TEST(TestFailure, StringsEqualFailureWithNullAsActual)
240 {
241 StringEqualFailure f(test, failFileName, failLineNumber, "abc", NULLPTR, "");
242 FAILURE_EQUAL("expected <abc>\n"
243 "\tbut was <(null)>", f);
244 }
245
246 TEST(TestFailure, StringsEqualFailureWithNullAsExpected)
247 {
248 StringEqualFailure f(test, failFileName, failLineNumber, NULLPTR, "abd", "");
249 FAILURE_EQUAL("expected <(null)>\n"
250 "\tbut was <abd>", f);
251 }
252
253 TEST(TestFailure, StringsEqualNoCaseFailureWithText)
254 {
255 StringEqualNoCaseFailure f(test, failFileName, failLineNumber, "ABC", "abd", "text");
256 FAILURE_EQUAL("Message: text\n"
257 "\texpected <ABC>\n"
258 "\tbut was <abd>\n"
259 "\tdifference starts at position 2 at: < abd >\n"
260 "\t ^", f);
261 }
262
263 TEST(TestFailure, StringsEqualNoCaseFailure)
264 {
265 StringEqualNoCaseFailure f(test, failFileName, failLineNumber, "ABC", "abd", "");
266 FAILURE_EQUAL("expected <ABC>\n"
267 "\tbut was <abd>\n"
268 "\tdifference starts at position 2 at: < abd >\n"
269 "\t ^", f);
270 }
271
272 TEST(TestFailure, StringsEqualNoCaseFailureWithActualAsNull)
273 {
274 StringEqualNoCaseFailure f(test, failFileName, failLineNumber, "ABC", NULLPTR, "");
275 FAILURE_EQUAL("expected <ABC>\n"
276 "\tbut was <(null)>", f);
277 }
278
279 TEST(TestFailure, StringsEqualNoCaseFailureWithExpectedAsNull)
280 {
281 StringEqualNoCaseFailure f(test, failFileName, failLineNumber, NULLPTR, "abd", "");
282 FAILURE_EQUAL("expected <(null)>\n"
283 "\tbut was <abd>", f);
284 }
285
286 TEST(TestFailure, StringsEqualNoCaseFailure2)
287 {
288 StringEqualNoCaseFailure f(test, failFileName, failLineNumber, "ac", "AB", "");
289 FAILURE_EQUAL("expected <ac>\n"
290 "\tbut was <AB>\n"
291 "\tdifference starts at position 1 at: < AB >\n"
292 "\t ^", f);
293 }
294
295 TEST(TestFailure, DoublesEqualNormalWithText)
296 {
297 DoublesEqualFailure f(test, failFileName, failLineNumber, 1.0, 2.0, 3.0, "text");
298 FAILURE_EQUAL("Message: text\n"
299 "\texpected <1>\n"
300 "\tbut was <2> threshold used was <3>", f);
301 }
302
303 TEST(TestFailure, DoublesEqualNormal)
304 {
305 DoublesEqualFailure f(test, failFileName, failLineNumber, 1.0, 2.0, 3.0, "");
306 FAILURE_EQUAL("expected <1>\n"
307 "\tbut was <2> threshold used was <3>", f);
308 }
309
310 TEST(TestFailure, BinaryEqualWithText)
311 {
312 const unsigned char expectedData[] = { 0x00 };
313 const unsigned char actualData[] = { 0x01 };
314 BinaryEqualFailure f(test, failFileName, failLineNumber, expectedData, actualData, sizeof(expectedData), "text");
315 FAILURE_EQUAL("Message: text\n"
316 "\texpected <00>\n"
317 "\tbut was <01>\n"
318 "\tdifference starts at position 0 at: < 01 >\n"
319 "\t ^", f);
320 }
321
322 TEST(TestFailure, BinaryEqualOneByte)
323 {
324 const unsigned char expectedData[] = { 0x00 };
325 const unsigned char actualData[] = { 0x01 };
326 BinaryEqualFailure f(test, failFileName, failLineNumber, expectedData, actualData, sizeof(expectedData), "");
327 FAILURE_EQUAL("expected <00>\n"
328 "\tbut was <01>\n"
329 "\tdifference starts at position 0 at: < 01 >\n"
330 "\t ^", f);
331 }
332
333 TEST(TestFailure, BinaryEqualTwoBytes)
334 {
335 const unsigned char expectedData[] = {0x00, 0x01};
336 const unsigned char actualData[] = {0x00, 0x02};
337 BinaryEqualFailure f(test, failFileName, failLineNumber, expectedData, actualData, sizeof(expectedData), "");
338 FAILURE_EQUAL("expected <00 01>\n"
339 "\tbut was <00 02>\n"
340 "\tdifference starts at position 1 at: < 00 02 >\n"
341 "\t ^", f);
342 }
343
344 TEST(TestFailure, BinaryEqualThreeBytes)
345 {
346 const unsigned char expectedData[] = {0x00, 0x01, 0x00};
347 const unsigned char actualData[] = {0x00, 0x02, 0x00};
348 BinaryEqualFailure f(test, failFileName, failLineNumber, expectedData, actualData, sizeof(expectedData), "");
349 FAILURE_EQUAL("expected <00 01 00>\n"
350 "\tbut was <00 02 00>\n"
351 "\tdifference starts at position 1 at: < 00 02 00 >\n"
352 "\t ^", f);
353 }
354
355 TEST(TestFailure, BinaryEqualFullWidth)
356 {
357 const unsigned char expectedData[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00};
358 const unsigned char actualData[] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00};
359 BinaryEqualFailure f(test, failFileName, failLineNumber, expectedData, actualData, sizeof(expectedData), "");
360 FAILURE_EQUAL("expected <00 00 00 01 00 00 00>\n"
361 "\tbut was <00 00 00 02 00 00 00>\n"
362 "\tdifference starts at position 3 at: <00 00 00 02 00 00 00>\n"
363 "\t ^", f);
364 }
365
366 TEST(TestFailure, BinaryEqualLast)
367 {
368 const unsigned char expectedData[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
369 const unsigned char actualData[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
370 BinaryEqualFailure f(test, failFileName, failLineNumber, expectedData, actualData, sizeof(expectedData), "");
371 FAILURE_EQUAL("expected <00 00 00 00 00 00 00>\n"
372 "\tbut was <00 00 00 00 00 00 01>\n"
373 "\tdifference starts at position 6 at: <00 00 00 01 >\n"
374 "\t ^", f);
375 }
376
377 TEST(TestFailure, BinaryEqualActualNull)
378 {
379 const unsigned char expectedData[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
380 BinaryEqualFailure f(test, failFileName, failLineNumber, expectedData, NULLPTR, sizeof(expectedData), "");
381 FAILURE_EQUAL("expected <00 00 00 00 00 00 00>\n\tbut was <(null)>", f);
382 }
383
384 TEST(TestFailure, BinaryEqualExpectedNull)
385 {
386 const unsigned char actualData[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
387 BinaryEqualFailure f(test, failFileName, failLineNumber, NULLPTR, actualData, sizeof(actualData), "");
388 FAILURE_EQUAL("expected <(null)>\n\tbut was <00 00 00 00 00 00 01>", f);
389 }
390
391 TEST(TestFailure, BitsEqualWithText)
392 {
393 BitsEqualFailure f(test, failFileName, failLineNumber, 0x0001, 0x0003, 0x00FF, 2*8/CPPUTEST_CHAR_BIT, "text");
394 FAILURE_EQUAL("Message: text\n"
395 "\texpected <xxxxxxxx 00000001>\n\tbut was <xxxxxxxx 00000011>", f);
396 }
397
398 #if (CPPUTEST_CHAR_BIT == 16)
399 TEST(TestFailure, BitsEqualChar)
400 {
401 BitsEqualFailure f(test, failFileName, failLineNumber, 0x01, 0x03, 0xFF, sizeof(char), "");
402 FAILURE_EQUAL("expected <xxxxxxxx 00000001>\n\tbut was <xxxxxxxx 00000011>", f);
403 }
404 #else
405 TEST(TestFailure, BitsEqualChar)
406 {
407 BitsEqualFailure f(test, failFileName, failLineNumber, 0x01, 0x03, 0xFF, sizeof(char), "");
408 FAILURE_EQUAL("expected <00000001>\n\tbut was <00000011>", f);
409 }
410 #endif
411
412 TEST(TestFailure, BitsEqual16Bit)
413 {
414 BitsEqualFailure f(test, failFileName, failLineNumber, 0x0001, 0x0003, 0xFFFF, 2*8/CPPUTEST_CHAR_BIT, "");
415 FAILURE_EQUAL("expected <00000000 00000001>\n\tbut was <00000000 00000011>", f);
416 }
417
418 TEST(TestFailure, BitsEqual32Bit)
419 {
420 BitsEqualFailure f(test, failFileName, failLineNumber, 0x00000001, 0x00000003, 0xFFFFFFFF, 4*8/CPPUTEST_CHAR_BIT, "");
421 FAILURE_EQUAL("expected <00000000 00000000 00000000 00000001>\n\tbut was <00000000 00000000 00000000 00000011>", f);
422 }
423
424 TEST(TestFailure, FeatureUnsupported)
425 {
426 FeatureUnsupportedFailure f(test, failFileName, failLineNumber, "SOME_FEATURE", "");
427 FAILURE_EQUAL("The feature \"SOME_FEATURE\" is not supported in this environment or with the feature set selected when building the library.", f);
428 }