Debian Codegraph @

tests/CppUTest/UtestTest.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 #include "CppUTest/TestTestingFixture.h"
31 #include "CppUTest/PlatformSpecificFunctions.h"
32
33 TEST_GROUP(UtestShell)
34 {
35 TestTestingFixture fixture;
36 };
37
38 static void _failMethod()
39 {
40 FAIL("This test fails");
41 }
42
43 static void _passingTestMethod()
44 {
45 CHECK(true);
46 }
47
48 static void _passingCheckEqualTestMethod()
49 {
50 CHECK_EQUAL(1, 1);
51 }
52
53 static void _exitTestMethod()
54 {
55 TEST_EXIT;
56 FAIL("Should not get here");
57 }
58
59 static volatile double zero = 0.0;
60
61 TEST(UtestShell, compareDoubles)
62 {
63 double not_a_number = zero / zero;
64 double infinity = 1 / zero;
65 CHECK(doubles_equal(1.0, 1.001, 0.01));
66 CHECK(!doubles_equal(not_a_number, 1.001, 0.01));
67 CHECK(!doubles_equal(1.0, not_a_number, 0.01));
68 CHECK(!doubles_equal(1.0, 1.001, not_a_number));
69 CHECK(!doubles_equal(1.0, 1.1, 0.05));
70 CHECK(!doubles_equal(infinity, 1.0, 0.01));
71 CHECK(!doubles_equal(1.0, infinity, 0.01));
72 CHECK(doubles_equal(1.0, -1.0, infinity));
73 CHECK(doubles_equal(infinity, infinity, 0.01));
74 CHECK(doubles_equal(infinity, infinity, infinity));
75 double a = 1.2345678;
76 CHECK(doubles_equal(a, a, 0.000000001));
77 }
78
79 TEST(UtestShell, FailWillIncreaseTheAmountOfChecks)
80 {
81 fixture.setTestFunction(_failMethod);
82 fixture.runAllTests();
83 LONGS_EQUAL(1, fixture.getCheckCount());
84 }
85
86 TEST(UtestShell, PassedCheckEqualWillIncreaseTheAmountOfChecks)
87 {
88 fixture.setTestFunction(_passingCheckEqualTestMethod);
89 fixture.runAllTests();
90 LONGS_EQUAL(1, fixture.getCheckCount());
91 }
92
93 IGNORE_TEST(UtestShell, IgnoreTestAccessingFixture)
94 {
95 CHECK(&fixture != NULLPTR);
96 }
97
98 TEST(UtestShell, MacrosUsedInSetup)
99 {
100 IGNORE_ALL_LEAKS_IN_TEST();
101 fixture.setSetup(_failMethod);
102 fixture.setTestFunction(_passingTestMethod);
103 fixture.runAllTests();
104 LONGS_EQUAL(1, fixture.getFailureCount());
105 }
106
107 TEST(UtestShell, MacrosUsedInTearDown)
108 {
109 IGNORE_ALL_LEAKS_IN_TEST();
110 fixture.setTeardown(_failMethod);
111 fixture.setTestFunction(_passingTestMethod);
112 fixture.runAllTests();
113 LONGS_EQUAL(1, fixture.getFailureCount());
114 }
115
116 TEST(UtestShell, ExitLeavesQuietly)
117 {
118 fixture.setTestFunction(_exitTestMethod);
119 fixture.runAllTests();
120 LONGS_EQUAL(0, fixture.getFailureCount());
121 }
122
123
124
125 static int teardownCalled = 0;
126
127 static void _teardownMethod()
128 {
129 teardownCalled++;
130 }
131
132 TEST(UtestShell, TeardownCalledAfterTestFailure)
133 {
134 teardownCalled = 0;
135 IGNORE_ALL_LEAKS_IN_TEST();
136 fixture.setTeardown(_teardownMethod);
137 fixture.setTestFunction(_failMethod);
138 fixture.runAllTests();
139 LONGS_EQUAL(1, fixture.getFailureCount());
140 LONGS_EQUAL(1, teardownCalled);
141 }
142
143 static int stopAfterFailure = 0;
144 static void _stopAfterFailureMethod()
145 {
146 FAIL("fail");
147 stopAfterFailure++;
148 }
149
150 TEST(UtestShell, TestStopsAfterTestFailure)
151 {
152 IGNORE_ALL_LEAKS_IN_TEST();
153 stopAfterFailure = 0;
154 fixture.setTestFunction(_stopAfterFailureMethod);
155 fixture.runAllTests();
156 CHECK(fixture.hasTestFailed());
157 LONGS_EQUAL(1, fixture.getFailureCount());
158 LONGS_EQUAL(0, stopAfterFailure);
159 }
160
161 TEST(UtestShell, TestStopsAfterSetupFailure)
162 {
163 stopAfterFailure = 0;
164 fixture.setSetup(_stopAfterFailureMethod);
165 fixture.setTeardown(_stopAfterFailureMethod);
166 fixture.setTestFunction(_failMethod);
167 fixture.runAllTests();
168 LONGS_EQUAL(2, fixture.getFailureCount());
169 LONGS_EQUAL(0, stopAfterFailure);
170 }
171
172 TEST(UtestShell, veryVebose)
173 {
174 UtestShell shell("Group", "name", __FILE__, __LINE__);
175 StringBufferTestOutput normalOutput;
176 normalOutput.verbose(TestOutput::level_veryVerbose);
177 NullTestPlugin plugin;
178
179 TestResult result(normalOutput);
180 shell.runOneTestInCurrentProcess(&plugin, result);
181 STRCMP_CONTAINS("\n------ before runTest", normalOutput.getOutput().asCharString());
182 }
183
184 class defaultUtestShell: public UtestShell
185 {
186 };
187
188 TEST(UtestShell, this_test_covers_the_UtestShell_createTest_and_Utest_testBody_methods)
189 {
190 defaultUtestShell shell;
191 fixture.addTest(&shell);
192 fixture.runAllTests();
193 LONGS_EQUAL(2, fixture.getTestCount());
194 }
195
196
197 static void StubPlatformSpecificRunTestInASeperateProcess(UtestShell* shell, TestPlugin*, TestResult* result)
198 {
199 result->addFailure(TestFailure(shell, "Failed in separate process"));
200 }
201
202 TEST(UtestShell, RunInSeparateProcessTest)
203 {
204 UT_PTR_SET(PlatformSpecificRunTestInASeperateProcess, StubPlatformSpecificRunTestInASeperateProcess);
205 fixture.getRegistry()->setRunTestsInSeperateProcess();
206 fixture.runAllTests();
207 fixture.assertPrintContains("Failed in separate process");
208 }
209
210 #ifndef CPPUTEST_HAVE_FORK
211
212 IGNORE_TEST(UtestShell, TestDefaultCrashMethodInSeparateProcessTest) {}
213
214 #else
215
216 #if !CPPUTEST_SANITIZE_ADDRESS
217
218 TEST(UtestShell, TestDefaultCrashMethodInSeparateProcessTest)
219 {
220 fixture.setTestFunction(UtestShell::crash);
221 fixture.setRunTestsInSeperateProcess();
222 fixture.runAllTests();
223 fixture.assertPrintContains("Failed in separate process - killed by signal");
224
225 /* Signal 11 usually happens, but with clang3.7 on Linux, it produced signal 4 */
226 CHECK(fixture.getOutput().contains("signal 11") || fixture.getOutput().contains("signal 4"));
227 }
228
229 #endif
230
231 #endif
232
233 #if CPPUTEST_USE_STD_CPP_LIB
234
235 static bool destructorWasCalledOnFailedTest = false;
236
237 static void _destructorCalledForLocalObjects()
238 {
239 SetBooleanOnDestructorCall pleaseCallTheDestructor(destructorWasCalledOnFailedTest);
240 destructorWasCalledOnFailedTest = false;
241 FAIL("fail");
242 }
243
244 TEST(UtestShell, DestructorIsCalledForLocalObjectsWhenTheTestFails)
245 {
246 fixture.setTestFunction(_destructorCalledForLocalObjects);
247 fixture.runAllTests();
248 CHECK(destructorWasCalledOnFailedTest);
249 }
250
251 #endif
252
253 TEST_GROUP(IgnoredUtestShell)
254 {
255 TestTestingFixture fixture;
256 IgnoredUtestShell ignoredTest;
257 ExecFunctionTestShell normalUtestShell;
258
259 void setup() _override
260 {
261 fixture.addTest(&ignoredTest);
262 fixture.addTest(&normalUtestShell);
263 }
264 };
265
266 TEST(IgnoredUtestShell, doesIgnoreCount)
267 {
268 fixture.runAllTests();
269 LONGS_EQUAL(1, fixture.getIgnoreCount());
270 }
271
272 TEST(IgnoredUtestShell, printsIGNORE_TESTwhenVerbose)
273 {
274 fixture.setOutputVerbose();
275 fixture.runAllTests();
276 fixture.assertPrintContains("IGNORE_TEST");
277 }
278
279 TEST(IgnoredUtestShell, runIgnoredOptionSpecifiedThenIncreaseRunCount)
280 {
281 ignoredTest.setRunIgnored();
282 fixture.runAllTests();
283 LONGS_EQUAL(3, fixture.getRunCount());
284 LONGS_EQUAL(0, fixture.getIgnoreCount());
285 }
286
287 TEST(IgnoredUtestShell, runIgnoredOptionNotSpecifiedThenIncreaseIgnoredCount)
288 {
289 fixture.runAllTests();
290 LONGS_EQUAL(2, fixture.getRunCount());
291 LONGS_EQUAL(1, fixture.getIgnoreCount());
292 }
293
294 TEST(IgnoredUtestShell, runIgnoredOptionSpecifiedWillNotInfluenceNormalTestCount)
295 {
296 normalUtestShell.setRunIgnored();
297 fixture.runAllTests();
298 LONGS_EQUAL(2, fixture.getRunCount());
299 LONGS_EQUAL(1, fixture.getIgnoreCount());
300 }
301
302 TEST(IgnoredUtestShell, runIgnoredOptionSpecifiedThenReturnTESTInFormattedName)
303 {
304 ignoredTest.setGroupName("TestGroup");
305 ignoredTest.setTestName("TestName");
306 ignoredTest.setRunIgnored();
307 fixture.runAllTests();
308 STRCMP_EQUAL("TEST(TestGroup, TestName)", ignoredTest.getFormattedName().asCharString());
309 }
310
311 TEST(IgnoredUtestShell, runIgnoredOptionNotSpecifiedThenReturnIGNORETESTInFormattedName)
312 {
313 ignoredTest.setGroupName("TestGroup");
314 ignoredTest.setTestName("TestName");
315 fixture.runAllTests();
316 STRCMP_EQUAL("IGNORE_TEST(TestGroup, TestName)", ignoredTest.getFormattedName().asCharString());
317 }
318
319 TEST(IgnoredUtestShell, runIgnoredOptionNotSpecifiedThenWillRunReturnFalse)
320 {
321 CHECK_FALSE(ignoredTest.willRun());
322 }
323
324 TEST(IgnoredUtestShell, runIgnoredOptionSpecifiedThenWillRunReturnTrue)
325 {
326 ignoredTest.setRunIgnored();
327 CHECK_TRUE(ignoredTest.willRun());
328 }
329
330 TEST_BASE(MyOwnTest)
331 {
332 MyOwnTest() :
333 inTest(false)
334 {
335 }
336 bool inTest;
337
338 void setup()
339 {
340 CHECK(!inTest);
341 inTest = true;
342 }
343 void teardown()
344 {
345 CHECK(inTest);
346 inTest = false;
347 }
348 };
349
350 TEST_GROUP_BASE(UtestMyOwn, MyOwnTest)
351 {
352 };
353
354 TEST(UtestMyOwn, test)
355 {
356 CHECK(inTest);
357 }
358
359 class NullParameterTest: public UtestShell
360 {
361 };
362
363 TEST(UtestMyOwn, NullParameters)
364 {
365 NullParameterTest nullTest; /* Bug fix tests for creating a test without a name, fix in SimpleString */
366 TestFilter emptyFilter;
367 CHECK(nullTest.shouldRun(&emptyFilter, &emptyFilter));
368 }
369
370 class AllocateAndDeallocateInConstructorAndDestructor
371 {
372 char* memory_;
373 char* morememory_;
374 public:
375 AllocateAndDeallocateInConstructorAndDestructor()
376 {
377 memory_ = new char[100];
378 morememory_ = NULLPTR;
379 }
380 void allocateMoreMemory()
381 {
382 morememory_ = new char[123];
383 }
384
385 ~AllocateAndDeallocateInConstructorAndDestructor()
386 {
387 delete [] memory_;
388 delete [] morememory_;
389 }
390 };
391
392 TEST_GROUP(CanHaveMemberVariablesInTestGroupThatAllocateMemoryWithoutCausingMemoryLeaks)
393 {
394 AllocateAndDeallocateInConstructorAndDestructor dummy;
395 };
396
397 TEST(CanHaveMemberVariablesInTestGroupThatAllocateMemoryWithoutCausingMemoryLeaks, testInTestGroupName)
398 {
399 dummy.allocateMoreMemory();
400 }
401
402 static int getZero()
403 {
404 return 0;
405 }
406
407 static int getOne()
408 {
409 return 1;
410 }
411
412 TEST_GROUP(UtestShellPointerArrayTest)
413 {
414 UtestShell* test0;
415 UtestShell* test1;
416 UtestShell* test2;
417
418 void setup()
419 {
420 test0 = new IgnoredUtestShell();
421 test1 = new IgnoredUtestShell();
422 test2 = new IgnoredUtestShell();
423
424 test0->addTest(test1);
425 test1->addTest(test2);
426 }
427
428 void teardown()
429 {
430 delete test0;
431 delete test1;
432 delete test2;
433 }
434 };
435
436
437 TEST(UtestShellPointerArrayTest, empty)
438 {
439 UtestShellPointerArray tests(NULLPTR);
440 tests.shuffle(0);
441 CHECK(NULLPTR == tests.getFirstTest());
442 }
443
444 TEST(UtestShellPointerArrayTest, testsAreInOrder)
445 {
446 UtestShellPointerArray tests(test0);
447 CHECK(tests.get(0) == test0);
448 CHECK(tests.get(1) == test1);
449 CHECK(tests.get(2) == test2);
450 }
451
452 TEST(UtestShellPointerArrayTest, relinkingTestsWillKeepThemTheSameWhenNothingWasDone)
453 {
454 UtestShellPointerArray tests(test0);
455 tests.relinkTestsInOrder();
456 CHECK(tests.get(0) == test0);
457 CHECK(tests.get(1) == test1);
458 CHECK(tests.get(2) == test2);
459 }
460
461
462 TEST(UtestShellPointerArrayTest, firstTestisNotTheFirstTestWithSeed1234)
463 {
464 UtestShellPointerArray tests(test0);
465 tests.shuffle(1234);
466 CHECK(tests.getFirstTest() != test0);
467 }
468
469 TEST(UtestShellPointerArrayTest, ShuffleListTestWithRandomAlwaysReturningZero)
470 {
471 UT_PTR_SET(PlatformSpecificRand, getZero);
472
473 UtestShellPointerArray tests(test0);
474 tests.shuffle(3);
475 CHECK(tests.get(0) == test1);
476 CHECK(tests.get(1) == test2);
477 CHECK(tests.get(2) == test0);
478 }
479
480 // swaps with 4 mod 3 (1) then 4 mod 2 (0): 1, [2], [0] --> [1], [0], 2 --> 0, 1, 2
481 TEST(UtestShellPointerArrayTest, ShuffleListTestWithRandomAlwaysReturningOne)
482 {
483 UT_PTR_SET(PlatformSpecificRand, getOne);
484
485 UtestShellPointerArray tests(test0);
486 tests.shuffle(3);
487 CHECK(tests.get(0) == test0);
488 CHECK(tests.get(1) == test2);
489 CHECK(tests.get(2) == test1);
490 }
491
492 TEST(UtestShellPointerArrayTest, reverse)
493 {
494 UT_PTR_SET(PlatformSpecificRand, getOne);
495
496 UtestShellPointerArray tests(test0);
497 tests.reverse();
498 CHECK(tests.get(0) == test2);
499 CHECK(tests.get(1) == test1);
500 CHECK(tests.get(2) == test0);
501 }