Debian Codegraph @

tests/CppUTest/TestHarness_cTest.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_c.h"
29
30 #include "CppUTest/TestHarness.h"
31 #include "CppUTest/TestRegistry.h"
32 #include "CppUTest/TestOutput.h"
33 #include "CppUTest/TestTestingFixture.h"
34 #include "CppUTest/PlatformSpecificFunctions.h"
35
36
37 extern "C" int setup_teardown_was_called_in_test_group_in_C;
38 extern "C" int test_was_called_in_test_group_in_C;
39 int setup_teardown_was_called_in_test_group_in_C = 0;
40 int test_was_called_in_test_group_in_C = 0;
41
42 TEST_GROUP_C_WRAPPER(TestGroupInC)
43 {
44 TEST_GROUP_C_SETUP_WRAPPER(TestGroupInC)
45 TEST_GROUP_C_TEARDOWN_WRAPPER(TestGroupInC)
46 };
47
48 TEST_C_WRAPPER(TestGroupInC, checkThatTheTestHasRun)
49 IGNORE_TEST_C_WRAPPER(TestGroupInC, ignoreMacroForCFile)
50
51 /*
52 * This test is a bit strange. They use the fact that you can do -r2 for repeating the same run.
53 * When you do so, the same statics will be shared and therefore we can test whether the setup/teardown is run
54 * correctly.
55 */
56
57 TEST(TestGroupInC, setupHasBeenCalled)
58 {
59 test_was_called_in_test_group_in_C++;
60 /* Increased in setup, decreased in teardown. So at this point it must be 1 also on a multiple run */
61 LONGS_EQUAL(1, setup_teardown_was_called_in_test_group_in_C);
62 }
63
64 static bool hasDestructorOfTheDestructorCheckedBeenCalled;
65
66 class HasTheDestructorBeenCalledChecker
67 {
68 public:
69 HasTheDestructorBeenCalledChecker(){}
70 ~HasTheDestructorBeenCalledChecker() { hasDestructorOfTheDestructorCheckedBeenCalled = true; }
71 };
72
73 TEST_GROUP(TestHarness_c)
74 {
75 TestTestingFixture* fixture;
76 TEST_SETUP()
77 {
78 hasDestructorOfTheDestructorCheckedBeenCalled = false;
79 fixture = new TestTestingFixture();
80 }
81 TEST_TEARDOWN()
82 {
83 delete fixture;
84 }
85 };
86
87 static void _failBoolMethod()
88 {
89 HasTheDestructorBeenCalledChecker checker;
90 CHECK_EQUAL_C_BOOL(1, 0);
91 } // LCOV_EXCL_LINE
92
93 TEST(TestHarness_c, checkBool)
94 {
95 CHECK_EQUAL_C_BOOL(1, 1);
96 CHECK_EQUAL_C_BOOL(1, 2);
97 fixture->setTestFunction(_failBoolMethod);
98 fixture->runAllTests();
99 fixture->assertPrintContains("expected <true>\n but was <false>");
100 fixture->assertPrintContains("arness_c");
101 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
102 }
103
104 static void _failBoolTextMethod()
105 {
106 HasTheDestructorBeenCalledChecker checker;
107 CHECK_EQUAL_C_BOOL_TEXT(1, 0, "BoolTestText");
108 } // LCOV_EXCL_LINE
109
110 TEST(TestHarness_c, checkBoolText)
111 {
112 CHECK_EQUAL_C_BOOL_TEXT(1, 1, "Text");
113 CHECK_EQUAL_C_BOOL_TEXT(1, 2, "Text");
114 fixture->setTestFunction(_failBoolTextMethod);
115 fixture->runAllTests();
116 fixture->assertPrintContains("expected <true>\n but was <false>");
117 fixture->assertPrintContains("arness_c");
118 fixture->assertPrintContains("Message: BoolTestText");
119 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
120 }
121
122 static void _failIntMethod()
123 {
124 HasTheDestructorBeenCalledChecker checker;
125 CHECK_EQUAL_C_INT(1, 2);
126 } // LCOV_EXCL_LINE
127
128 TEST(TestHarness_c, checkInt)
129 {
130 CHECK_EQUAL_C_INT(2, 2);
131 fixture->setTestFunction(_failIntMethod);
132 fixture->runAllTests();
133 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
134 fixture->assertPrintContains("arness_c");
135 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
136 }
137
138 static void _failIntTextMethod()
139 {
140 HasTheDestructorBeenCalledChecker checker;
141 CHECK_EQUAL_C_INT_TEXT(1, 2, "IntTestText");
142 } // LCOV_EXCL_LINE
143
144 TEST(TestHarness_c, checkIntText)
145 {
146 CHECK_EQUAL_C_INT_TEXT(2, 2, "Text");
147 fixture->setTestFunction(_failIntTextMethod);
148 fixture->runAllTests();
149 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
150 fixture->assertPrintContains("arness_c");
151 fixture->assertPrintContains("Message: IntTestText");
152 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
153 }
154
155 static void _failUnsignedIntMethod()
156 {
157 HasTheDestructorBeenCalledChecker checker;
158 CHECK_EQUAL_C_UINT(1, 2);
159 } // LCOV_EXCL_LINE
160
161 TEST(TestHarness_c, checkUnsignedInt)
162 {
163 CHECK_EQUAL_C_UINT(2, 2);
164 fixture->setTestFunction(_failUnsignedIntMethod);
165 fixture->runAllTests();
166 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
167 fixture->assertPrintContains("arness_c");
168 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
169 }
170
171 static void _failUnsignedIntTextMethod()
172 {
173 HasTheDestructorBeenCalledChecker checker;
174 CHECK_EQUAL_C_UINT_TEXT(1, 2, "UnsignedIntTestText");
175 } // LCOV_EXCL_LINE
176
177 TEST(TestHarness_c, checkUnsignedIntText)
178 {
179 CHECK_EQUAL_C_UINT_TEXT(2, 2, "Text");
180 fixture->setTestFunction(_failUnsignedIntTextMethod);
181 fixture->runAllTests();
182 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
183 fixture->assertPrintContains("arness_c");
184 fixture->assertPrintContains("Message: UnsignedIntTestText");
185 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
186 }
187
188 static void _failLongIntMethod()
189 {
190 HasTheDestructorBeenCalledChecker checker;
191 CHECK_EQUAL_C_LONG(1, 2);
192 } // LCOV_EXCL_LINE
193
194 TEST(TestHarness_c, checkLongInt)
195 {
196 CHECK_EQUAL_C_LONG(2, 2);
197 fixture->setTestFunction(_failLongIntMethod);
198 fixture->runAllTests();
199 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
200 fixture->assertPrintContains("arness_c");
201 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
202 }
203
204 static void _failLongIntTextMethod()
205 {
206 HasTheDestructorBeenCalledChecker checker;
207 CHECK_EQUAL_C_LONG_TEXT(1, 2, "LongIntTestText");
208 } // LCOV_EXCL_LINE
209
210 TEST(TestHarness_c, checkLongIntText)
211 {
212 CHECK_EQUAL_C_LONG_TEXT(2, 2, "Text");
213 fixture->setTestFunction(_failLongIntTextMethod);
214 fixture->runAllTests();
215 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
216 fixture->assertPrintContains("arness_c");
217 fixture->assertPrintContains("Message: LongIntTestText");
218 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
219 }
220
221 static void _failUnsignedLongIntMethod()
222 {
223 HasTheDestructorBeenCalledChecker checker;
224 CHECK_EQUAL_C_ULONG(1, 2);
225 } // LCOV_EXCL_LINE
226
227 TEST(TestHarness_c, checkUnsignedLongInt)
228 {
229 CHECK_EQUAL_C_ULONG(2, 2);
230 fixture->setTestFunction(_failUnsignedLongIntMethod);
231 fixture->runAllTests();
232 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
233 fixture->assertPrintContains("arness_c");
234 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
235 }
236
237 static void _failUnsignedLongIntTextMethod()
238 {
239 HasTheDestructorBeenCalledChecker checker;
240 CHECK_EQUAL_C_ULONG_TEXT(1, 2, "UnsignedLongIntTestText");
241 } // LCOV_EXCL_LINE
242
243 TEST(TestHarness_c, checkUnsignedLongIntText)
244 {
245 CHECK_EQUAL_C_ULONG_TEXT(2, 2, "Text");
246 fixture->setTestFunction(_failUnsignedLongIntTextMethod);
247 fixture->runAllTests();
248 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
249 fixture->assertPrintContains("arness_c");
250 fixture->assertPrintContains("Message: UnsignedLongIntTestText");
251 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
252 }
253
254 #ifdef CPPUTEST_USE_LONG_LONG
255
256 static void _failLongLongIntMethod()
257 {
258 HasTheDestructorBeenCalledChecker checker;
259 CHECK_EQUAL_C_LONGLONG(1, 2);
260 } // LCOV_EXCL_LINE
261
262 TEST(TestHarness_c, checkLongLongInt)
263 {
264 CHECK_EQUAL_C_LONGLONG(2, 2);
265 fixture->setTestFunction(_failLongLongIntMethod);
266 fixture->runAllTests();
267 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
268 fixture->assertPrintContains("arness_c");
269 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
270 }
271
272 static void _failLongLongIntTextMethod()
273 {
274 HasTheDestructorBeenCalledChecker checker;
275 CHECK_EQUAL_C_LONGLONG_TEXT(1, 2, "LongLongTestText");
276 } // LCOV_EXCL_LINE
277
278 TEST(TestHarness_c, checkLongLongIntText)
279 {
280 CHECK_EQUAL_C_LONGLONG_TEXT(2, 2, "Text");
281 fixture->setTestFunction(_failLongLongIntTextMethod);
282 fixture->runAllTests();
283 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
284 fixture->assertPrintContains("arness_c");
285 fixture->assertPrintContains("Message: LongLongTestText");
286 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
287 }
288
289 static void _failUnsignedLongLongIntMethod()
290 {
291 HasTheDestructorBeenCalledChecker checker;
292 CHECK_EQUAL_C_ULONGLONG(1, 2);
293 } // LCOV_EXCL_LINE
294
295 TEST(TestHarness_c, checkUnsignedLongLongInt)
296 {
297 CHECK_EQUAL_C_ULONGLONG(2, 2);
298 fixture->setTestFunction(_failUnsignedLongLongIntMethod);
299 fixture->runAllTests();
300 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
301 fixture->assertPrintContains("arness_c");
302 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
303 }
304
305 static void _failUnsignedLongLongIntTextMethod()
306 {
307 HasTheDestructorBeenCalledChecker checker;
308 CHECK_EQUAL_C_ULONGLONG_TEXT(1, 2, "UnsignedLongLongTestText");
309 } // LCOV_EXCL_LINE
310
311 TEST(TestHarness_c, checkUnsignedLongLongIntText)
312 {
313 CHECK_EQUAL_C_ULONGLONG_TEXT(2, 2, "Text");
314 fixture->setTestFunction(_failUnsignedLongLongIntTextMethod);
315 fixture->runAllTests();
316 fixture->assertPrintContains("expected <1 (0x1)>\n but was <2 (0x2)>");
317 fixture->assertPrintContains("arness_c");
318 fixture->assertPrintContains("Message: UnsignedLongLongTestText");
319 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
320 }
321
322 #else
323
324 static void _failLongLongIntMethod()
325 {
326 cpputest_longlong dummy_longlong;
327 CHECK_EQUAL_C_LONGLONG(dummy_longlong, dummy_longlong);
328 } // LCOV_EXCL_LINE
329
330 TEST(TestHarness_c, checkLongLongInt)
331 {
332 fixture->setTestFunction(_failLongLongIntMethod);
333 fixture->runAllTests();
334 fixture->assertPrintContains("is not supported");
335 fixture->assertPrintContains("arness_c");
336 }
337
338 static void _failLongLongIntTextMethod()
339 {
340 cpputest_longlong dummy_longlong;
341 CHECK_EQUAL_C_LONGLONG_TEXT(dummy_longlong, dummy_longlong, "Text");
342 } // LCOV_EXCL_LINE
343
344 TEST(TestHarness_c, checkLongLongIntText)
345 {
346 fixture->setTestFunction(_failLongLongIntTextMethod);
347 fixture->runAllTests();
348 fixture->assertPrintContains("is not supported");
349 fixture->assertPrintContains("arness_c");
350 }
351
352 static void _failUnsignedLongLongIntMethod()
353 {
354 cpputest_ulonglong dummy_ulonglong;
355 CHECK_EQUAL_C_ULONGLONG(dummy_ulonglong, dummy_ulonglong);
356 } // LCOV_EXCL_LINE
357
358 TEST(TestHarness_c, checkUnsignedLongLongInt)
359 {
360 fixture->setTestFunction(_failUnsignedLongLongIntMethod);
361 fixture->runAllTests();
362 fixture->assertPrintContains("is not supported");
363 fixture->assertPrintContains("arness_c");
364 }
365
366 static void _failUnsignedLongLongIntTextMethod()
367 {
368 cpputest_ulonglong dummy_ulonglong;
369 CHECK_EQUAL_C_ULONGLONG_TEXT(dummy_ulonglong, dummy_ulonglong, "Text");
370 } // LCOV_EXCL_LINE
371
372 TEST(TestHarness_c, checkUnsignedLongLongIntText)
373 {
374 fixture->setTestFunction(_failUnsignedLongLongIntTextMethod);
375 fixture->runAllTests();
376 fixture->assertPrintContains("is not supported");
377 fixture->assertPrintContains("arness_c");
378 }
379
380 #endif
381
382 static void _failRealMethod()
383 {
384 HasTheDestructorBeenCalledChecker checker;
385 CHECK_EQUAL_C_REAL(1.0, 2.0, 0.5);
386 } // LCOV_EXCL_LINE
387
388 TEST(TestHarness_c, checkReal)
389 {
390 CHECK_EQUAL_C_REAL(1.0, 1.1, 0.5);
391 fixture->setTestFunction(_failRealMethod);
392 fixture->runAllTests();
393 fixture->assertPrintContains("expected <1>\n but was <2>");
394 fixture->assertPrintContains("arness_c");
395 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
396 }
397
398 static void _failRealTextMethod()
399 {
400 HasTheDestructorBeenCalledChecker checker;
401 CHECK_EQUAL_C_REAL_TEXT(1.0, 2.0, 0.5, "RealTestText");
402 } // LCOV_EXCL_LINE
403
404 TEST(TestHarness_c, checkRealText)
405 {
406 CHECK_EQUAL_C_REAL_TEXT(1.0, 1.1, 0.5, "Text");
407 fixture->setTestFunction(_failRealTextMethod);
408 fixture->runAllTests();
409 fixture->assertPrintContains("expected <1>\n but was <2>");
410 fixture->assertPrintContains("arness_c");
411 fixture->assertPrintContains("Message: RealTestText");
412 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
413 }
414
415 static void _failCharMethod()
416 {
417 HasTheDestructorBeenCalledChecker checker;
418 CHECK_EQUAL_C_CHAR('a', 'c');
419 }
420
421 TEST(TestHarness_c, checkChar)
422 {
423 CHECK_EQUAL_C_CHAR('a', 'a');
424 fixture->setTestFunction(_failCharMethod);
425 fixture->runAllTests();
426 fixture->assertPrintContains("expected <a>\n but was <c>");
427 fixture->assertPrintContains("arness_c");
428 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
429 }
430
431 static void _failCharTextMethod()
432 {
433 HasTheDestructorBeenCalledChecker checker;
434 CHECK_EQUAL_C_CHAR_TEXT('a', 'c', "CharTestText");
435 }
436
437 TEST(TestHarness_c, checkCharText)
438 {
439 CHECK_EQUAL_C_CHAR_TEXT('a', 'a', "Text");
440 fixture->setTestFunction(_failCharTextMethod);
441 fixture->runAllTests();
442 fixture->assertPrintContains("expected <a>\n but was <c>");
443 fixture->assertPrintContains("arness_c");
444 fixture->assertPrintContains("Message: CharTestText");
445 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
446 }
447
448 static void _failUnsignedByteMethod()
449 {
450 HasTheDestructorBeenCalledChecker checker;
451 CHECK_EQUAL_C_UBYTE(254, 253);
452 }
453
454 TEST(TestHarness_c, checkUnsignedByte)
455 {
456 CHECK_EQUAL_C_UBYTE(254, 254);
457 fixture->setTestFunction(_failUnsignedByteMethod);
458 fixture->runAllTests();
459 fixture->assertPrintContains("expected <254>\n but was <253>");
460 fixture->assertPrintContains("arness_c");
461 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
462 }
463
464 static void _failUnsignedByteTextMethod()
465 {
466 HasTheDestructorBeenCalledChecker checker;
467 CHECK_EQUAL_C_UBYTE_TEXT(254, 253, "UnsignedByteTestText");
468 }
469
470 TEST(TestHarness_c, checkUnsignedByteText)
471 {
472 CHECK_EQUAL_C_UBYTE_TEXT(254, 254, "Text");
473 fixture->setTestFunction(_failUnsignedByteTextMethod);
474 fixture->runAllTests();
475 fixture->assertPrintContains("expected <254>\n but was <253>");
476 fixture->assertPrintContains("arness_c");
477 fixture->assertPrintContains("Message: UnsignedByteTestText");
478 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
479 }
480
481 static void _failSignedByteMethod()
482 {
483 HasTheDestructorBeenCalledChecker checker;
484 CHECK_EQUAL_C_SBYTE(-3, -5);
485 }
486
487 TEST(TestHarness_c, checkSignedByte)
488 {
489 CHECK_EQUAL_C_SBYTE(-3, -3);
490 fixture->setTestFunction(_failSignedByteMethod);
491 fixture->runAllTests();
492 fixture->assertPrintContains("expected <-3>\n but was <-5>");
493 fixture->assertPrintContains("arness_c");
494 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
495 }
496
497 static void _failSignedByteTextMethod()
498 {
499 HasTheDestructorBeenCalledChecker checker;
500 CHECK_EQUAL_C_SBYTE_TEXT(-3, -5, "SignedByteTestText");
501 }
502
503 TEST(TestHarness_c, checkSignedByteText)
504 {
505 CHECK_EQUAL_C_SBYTE_TEXT(-3, -3, "Text");
506 fixture->setTestFunction(_failSignedByteTextMethod);
507 fixture->runAllTests();
508 fixture->assertPrintContains("expected <-3>\n but was <-5>");
509 fixture->assertPrintContains("arness_c");
510 fixture->assertPrintContains("Message: SignedByteTestText");
511 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
512 }
513
514 static void _failStringMethod()
515 {
516 HasTheDestructorBeenCalledChecker checker;
517 CHECK_EQUAL_C_STRING("Hello", "Hello World");
518 } // LCOV_EXCL_LINE
519
520 TEST(TestHarness_c, checkString)
521 {
522 CHECK_EQUAL_C_STRING("Hello", "Hello");
523 fixture->setTestFunction(_failStringMethod);
524 fixture->runAllTests();
525
526 StringEqualFailure failure(UtestShell::getCurrent(), "file", 1, "Hello", "Hello World", "");
527 fixture->assertPrintContains(failure.getMessage());
528 fixture->assertPrintContains("arness_c");
529 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
530 }
531
532 static void _failStringTextMethod()
533 {
534 HasTheDestructorBeenCalledChecker checker;
535 CHECK_EQUAL_C_STRING_TEXT("Hello", "Hello World", "StringTestText");
536 } // LCOV_EXCL_LINE
537
538 TEST(TestHarness_c, checkStringText)
539 {
540 CHECK_EQUAL_C_STRING_TEXT("Hello", "Hello", "Text");
541 fixture->setTestFunction(_failStringTextMethod);
542 fixture->runAllTests();
543
544 StringEqualFailure failure(UtestShell::getCurrent(), "file", 1, "Hello", "Hello World", "");
545 fixture->assertPrintContains(failure.getMessage());
546 fixture->assertPrintContains("arness_c");
547 fixture->assertPrintContains("Message: StringTestText");
548 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
549 }
550
551 static void _failPointerMethod()
552 {
553 HasTheDestructorBeenCalledChecker checker;
554 CHECK_EQUAL_C_POINTER(NULLPTR, (void *)0x1);
555 }
556
557 TEST(TestHarness_c, checkPointer)
558 {
559 CHECK_EQUAL_C_POINTER(NULLPTR, NULLPTR);
560 fixture->setTestFunction(_failPointerMethod);
561 fixture->runAllTests();
562 fixture->assertPrintContains("expected <0x0>\n but was <0x1>");
563 fixture->assertPrintContains("arness_c");
564 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
565 }
566
567 static void _failPointerTextMethod()
568 {
569 HasTheDestructorBeenCalledChecker checker;
570 CHECK_EQUAL_C_POINTER_TEXT(NULLPTR, (void *)0x1, "PointerTestText");
571 }
572
573 TEST(TestHarness_c, checkPointerText)
574 {
575 CHECK_EQUAL_C_POINTER_TEXT(NULLPTR, NULLPTR, "Text");
576 fixture->setTestFunction(_failPointerTextMethod);
577 fixture->runAllTests();
578 fixture->assertPrintContains("expected <0x0>\n but was <0x1>");
579 fixture->assertPrintContains("arness_c");
580 fixture->assertPrintContains("Message: PointerTestText");
581 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
582 }
583
584 static void _failBitsMethod()
585 {
586 HasTheDestructorBeenCalledChecker checker;
587 CHECK_EQUAL_C_BITS(0x0001, (unsigned short)0x0003, 0xFFFF);
588 }
589
590 TEST(TestHarness_c, checkBits)
591 {
592 CHECK_EQUAL_C_BITS(0xABCD, (unsigned short)0xABCD, 0xFFFF);
593 fixture->setTestFunction(_failBitsMethod);
594 fixture->runAllTests();
595 fixture->assertPrintContains("expected <00000000 00000001>\n\tbut was <00000000 00000011>");
596 fixture->assertPrintContains("arness_c");
597 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
598 }
599
600 static void _failBitsTextMethod()
601 {
602 HasTheDestructorBeenCalledChecker checker;
603 CHECK_EQUAL_C_BITS_TEXT(0x0001, (unsigned short)0x0003, 0xFFFF, "BitsTestText");
604 }
605
606 TEST(TestHarness_c, checkBitsText)
607 {
608 CHECK_EQUAL_C_BITS_TEXT(0xABCD, (unsigned short)0xABCD, 0xFFFF, "Text");
609 fixture->setTestFunction(_failBitsTextMethod);
610 fixture->runAllTests();
611 fixture->assertPrintContains("expected <00000000 00000001>\n\tbut was <00000000 00000011>");
612 fixture->assertPrintContains("arness_c");
613 fixture->assertPrintContains("Message: BitsTestText");
614 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
615 }
616
617 static void _failTextMethod()
618 {
619 HasTheDestructorBeenCalledChecker checker;
620 FAIL_TEXT_C("Booo");
621 } // LCOV_EXCL_LINE
622
623 TEST(TestHarness_c, checkFailText)
624 {
625 fixture->setTestFunction(_failTextMethod);
626 fixture->runAllTests();
627 fixture->assertPrintContains("Booo");
628 fixture->assertPrintContains("arness_c");
629 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
630 }
631
632 static void _failMethod()
633 {
634 HasTheDestructorBeenCalledChecker checker;
635 FAIL_C();
636 } // LCOV_EXCL_LINE
637
638 TEST(TestHarness_c, checkFail)
639 {
640 fixture->setTestFunction(_failMethod);
641 fixture->runAllTests();
642 LONGS_EQUAL(1, fixture->getFailureCount());
643 fixture->assertPrintContains("arness_c");
644 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
645 }
646
647 static void _CheckMethod()
648 {
649 HasTheDestructorBeenCalledChecker checker;
650 CHECK_C(false);
651 } // LCOV_EXCL_LINE
652
653 TEST(TestHarness_c, checkCheck)
654 {
655 CHECK_C(true);
656 fixture->setTestFunction(_CheckMethod);
657 fixture->runAllTests();
658 LONGS_EQUAL(1, fixture->getFailureCount());
659 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
660 }
661
662 static void _CheckTextMethod()
663 {
664 HasTheDestructorBeenCalledChecker checker;
665 CHECK_C_TEXT(false, "CheckTestText");
666 } // LCOV_EXCL_LINE
667
668 TEST(TestHarness_c, checkCheckText)
669 {
670 CHECK_C_TEXT(true, "Text");
671 fixture->setTestFunction(_CheckTextMethod);
672 fixture->runAllTests();
673 LONGS_EQUAL(1, fixture->getFailureCount());
674 fixture->assertPrintContains("Message: CheckTestText");
675 CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled);
676 }
677
678 #if CPPUTEST_USE_MEM_LEAK_DETECTION
679
680 TEST(TestHarness_c, cpputest_malloc_out_of_memory)
681 {
682 cpputest_malloc_set_out_of_memory();
683 CHECK(NULLPTR == cpputest_malloc(100));
684
685 cpputest_malloc_set_not_out_of_memory();
686 void * mem = cpputest_malloc(100);
687 CHECK(NULLPTR != mem);
688 cpputest_free(mem);
689 }
690
691 TEST(TestHarness_c, cpputest_malloc_out_of_memory_after_n_mallocs)
692 {
693 cpputest_malloc_set_out_of_memory_countdown(3);
694 void * m1 = cpputest_malloc(10);
695 void * m2 = cpputest_malloc(11);
696 void * m3 = cpputest_malloc(12);
697 CHECK(m1 != NULLPTR);
698 CHECK(m2 != NULLPTR);
699 CHECK(m3 == NULLPTR);
700 cpputest_malloc_set_not_out_of_memory();
701 cpputest_free(m1);
702 cpputest_free(m2);
703 }
704
705 TEST(TestHarness_c, cpputest_malloc_out_of_memory_after_0_mallocs)
706 {
707 cpputest_malloc_set_out_of_memory_countdown(0);
708 void * m1 = cpputest_malloc(10);
709 CHECK(m1 == NULLPTR);
710 cpputest_malloc_set_not_out_of_memory();
711 }
712
713 TEST(TestHarness_c, count_mallocs)
714 {
715 cpputest_malloc_count_reset();
716 void * m1 = cpputest_malloc(10);
717 void * m2 = cpputest_malloc(11);
718 void * m3 = cpputest_malloc(12);
719 cpputest_free(m1);
720 cpputest_free(m2);
721 cpputest_free(m3);
722 LONGS_EQUAL(3, cpputest_malloc_get_count());
723 }
724
725 #ifdef CPPUTEST_USE_STRDUP_MACROS
726
727 TEST(TestHarness_c, cpputest_strdup)
728 {
729 char * mem = cpputest_strdup("0123456789");
730 CHECK(NULLPTR != mem);
731 STRCMP_EQUAL("0123456789", mem);
732 cpputest_free(mem);
733 }
734
735 TEST(TestHarness_c, cpputest_strndup)
736 {
737 char * mem = cpputest_strndup("0123456789", 3);
738 CHECK(NULLPTR != mem);
739 STRCMP_EQUAL("012", mem);
740 cpputest_free(mem);
741 }
742
743 #endif
744
745 TEST(TestHarness_c, cpputest_calloc)
746 {
747 void * mem = cpputest_calloc(10, 10);
748 CHECK(NULLPTR != mem);
749 cpputest_free(mem);
750 }
751
752 TEST(TestHarness_c, cpputest_realloc_larger)
753 {
754 const char* number_string = "123456789";
755
756 char* mem1 = (char*) cpputest_malloc(10);
757
758 SimpleString::StrNCpy(mem1, number_string, 10);
759
760 CHECK(mem1 != NULLPTR);
761
762 char* mem2 = (char*) cpputest_realloc(mem1, 1000);
763
764 CHECK(mem2 != NULLPTR);
765 STRCMP_EQUAL(number_string, mem2);
766
767 cpputest_free(mem2);
768 }
769
770 #include "CppUTest/MemoryLeakDetector.h"
771
772 TEST(TestHarness_c, macros)
773 {
774 #if CPPUTEST_USE_MALLOC_MACROS
775 MemoryLeakDetector* memLeakDetector = MemoryLeakWarningPlugin::getGlobalDetector();
776 size_t memLeaks = memLeakDetector->totalMemoryLeaks(mem_leak_period_checking);
777 #endif
778 void* mem1 = malloc(10);
779 void* mem2 = calloc(10, 20);
780 void* mem3 = realloc(mem2, 100);
781 #if CPPUTEST_USE_MALLOC_MACROS
782 LONGS_EQUAL(memLeaks + 2, memLeakDetector->totalMemoryLeaks(mem_leak_period_checking));
783 #endif
784 free(mem1);
785 free(mem3);
786 #if CPPUTEST_USE_MALLOC_MACROS
787 LONGS_EQUAL(memLeaks, memLeakDetector->totalMemoryLeaks(mem_leak_period_checking));
788 #endif
789
790 }
791
792 TEST(TestHarness_c, callocInitializedToZero)
793 {
794 char* mem = (char*) calloc(20, sizeof(char));
795 for (int i = 0; i < 20; i++)
796 CHECK(mem[i] == 0);
797 free(mem);
798 }
799
800 TEST(TestHarness_c, callocShouldReturnNULLWhenOutOfMemory)
801 {
802 cpputest_malloc_set_out_of_memory_countdown(0);
803 void * m = cpputest_calloc(1, 1);
804 CHECK(m == NULLPTR);
805 cpputest_malloc_set_not_out_of_memory();
806 }
807 #endif
808