• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cpputest / cpputest / 15141709115

20 May 2025 03:31PM UTC coverage: 99.281%. Remained the same
15141709115

push

github

web-flow
Merge pull request #1859 from mtfurlan/ci/run-docker

ci: actually run docker builds

6765 of 6814 relevant lines covered (99.28%)

46692.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.82
/src/CppUTest/Utest.cpp
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/TestRegistry.h"
30
#include "CppUTest/PlatformSpecificFunctions.h"
31
#include "CppUTest/TestOutput.h"
32

33
#if defined(__GNUC__) && __GNUC__ >= 11
34
# define NEEDS_DISABLE_NULL_WARNING
35
#endif /* GCC >= 11 */
36

37
bool doubles_equal(double d1, double d2, double threshold)
67✔
38
{
39
    if (PlatformSpecificIsNan(d1) || PlatformSpecificIsNan(d2) || PlatformSpecificIsNan(threshold))
67✔
40
        return false;
3✔
41

42
    if (PlatformSpecificIsInf(d1) && PlatformSpecificIsInf(d2))
64✔
43
    {
44
        return true;
2✔
45
    }
46

47
    return PlatformSpecificFabs(d1 - d2) <= threshold;
62✔
48
}
49

50
/* Sometimes stubs use the CppUTest assertions.
51
 * Its not correct to do so, but this small helper class will prevent a segmentation fault and instead
52
 * will give an error message and also the file/line of the check that was executed outside the tests.
53
 */
54
class OutsideTestRunnerUTest: public UtestShell
55
{
56
public:
57
    static OutsideTestRunnerUTest& instance();
58
    virtual TestResult& getTestResult()
184✔
59
    {
60
        return defaultTestResult;
184✔
61
    }
62
    virtual ~OutsideTestRunnerUTest() CPPUTEST_DESTRUCTOR_OVERRIDE
79✔
63
    {
79✔
64
    }
79✔
65
private:
66
    OutsideTestRunnerUTest() :
79✔
67
        UtestShell("\n\t NOTE: Assertion happened without being in a test run (perhaps in main?)", "\n\t       Something is very wrong. Check this assertion and fix", "unknown file", 0),
68
                defaultTestResult(defaultOutput)
79✔
69
    {
70
    }
79✔
71
    ConsoleTestOutput defaultOutput;
72
    TestResult defaultTestResult;
73
};
74

75
OutsideTestRunnerUTest& OutsideTestRunnerUTest::instance()
368✔
76
{
77
    static OutsideTestRunnerUTest instance_;
368✔
78
    return instance_;
368✔
79
}
80

81
/*
82
 * Below helpers are used for the PlatformSpecificSetJmp and LongJmp. They pass a method for what needs to happen after
83
 * the jump, so that the stack stays right.
84
 *
85
 */
86

87
extern "C" {
88

89
    static void helperDoTestSetup(void* data)
1,581✔
90
    {
91
        ((Utest*)data)->setup();
1,581✔
92
    }
1,578✔
93

94
    static void helperDoTestBody(void* data)
1,578✔
95
    {
96
        ((Utest*)data)->testBody();
1,578✔
97
    }
1,424✔
98

99
    static void helperDoTestTeardown(void* data)
1,577✔
100
    {
101
        ((Utest*)data)->teardown();
1,577✔
102
    }
1,575✔
103

104
    struct HelperTestRunInfo
105
    {
106
        HelperTestRunInfo(UtestShell* shell, TestPlugin* plugin, TestResult* result) : shell_(shell), plugin_(plugin), result_(result){}
1,598✔
107

108
        UtestShell* shell_;
109
        TestPlugin* plugin_;
110
        TestResult* result_;
111
    };
112

113
    static void helperDoRunOneTestInCurrentProcess(void* data)
1,579✔
114
    {
115
        HelperTestRunInfo* runInfo = (HelperTestRunInfo*) data;
1,579✔
116

117
        UtestShell* shell = runInfo->shell_;
1,579✔
118
        TestPlugin* plugin = runInfo->plugin_;
1,579✔
119
        TestResult* result = runInfo->result_;
1,579✔
120

121
        shell->runOneTestInCurrentProcess(plugin, *result);
1,579✔
122
    }
1,576✔
123

124
    static void helperDoRunOneTestSeperateProcess(void* data)
19✔
125
    {
126
        HelperTestRunInfo* runInfo = (HelperTestRunInfo*) data;
19✔
127

128
        UtestShell* shell = runInfo->shell_;
19✔
129
        TestPlugin* plugin = runInfo->plugin_;
19✔
130
        TestResult* result = runInfo->result_;
19✔
131
        PlatformSpecificRunTestInASeperateProcess(shell, plugin, result);
19✔
132
    }
18✔
133

134
}
135

136
/******************************** */
137

138
static const NormalTestTerminator normalTestTerminator = NormalTestTerminator();
139
static const CrashingTestTerminator crashingTestTerminator = CrashingTestTerminator();
140
static const TestTerminatorWithoutExceptions normalTestTerminatorWithoutExceptions = TestTerminatorWithoutExceptions();
141
static const CrashingTestTerminatorWithoutExceptions crashingTestTerminatorWithoutExceptions = CrashingTestTerminatorWithoutExceptions();
142

143
const TestTerminator *UtestShell::currentTestTerminator_ = &normalTestTerminator;
144
const TestTerminator *UtestShell::currentTestTerminatorWithoutExceptions_ = &normalTestTerminatorWithoutExceptions;
145

146
bool UtestShell::rethrowExceptions_ = false;
147

148
/******************************** */
149

150
UtestShell::UtestShell() :
61,771✔
151
    group_("UndefinedTestGroup"), name_("UndefinedTest"), file_("UndefinedFile"), lineNumber_(0), next_(NULLPTR), isRunAsSeperateProcess_(false), hasFailed_(false)
61,771✔
152
{
153
}
61,771✔
154

155
UtestShell::UtestShell(const char* groupName, const char* testName, const char* fileName, size_t lineNumber) :
771✔
156
    group_(groupName), name_(testName), file_(fileName), lineNumber_(lineNumber), next_(NULLPTR), isRunAsSeperateProcess_(false), hasFailed_(false)
771✔
157
{
158
}
771✔
159

160
UtestShell::UtestShell(const char* groupName, const char* testName, const char* fileName, size_t lineNumber, UtestShell* nextTest) :
4✔
161
    group_(groupName), name_(testName), file_(fileName), lineNumber_(lineNumber), next_(nextTest), isRunAsSeperateProcess_(false), hasFailed_(false)
4✔
162
{
163
}
4✔
164

165
UtestShell::~UtestShell()
62,757✔
166
{
167
}
62,757✔
168

169
static void (*pleaseCrashMeRightNow) () = PlatformSpecificAbort;
170

171
void UtestShell::setCrashMethod(void (*crashme)())
24✔
172
{
173
    pleaseCrashMeRightNow = crashme;
24✔
174
}
24✔
175

176
void UtestShell::resetCrashMethod()
24✔
177
{
178
    pleaseCrashMeRightNow = PlatformSpecificAbort;
24✔
179
}
24✔
180

181
void UtestShell::crash()
10✔
182
{
183
    pleaseCrashMeRightNow();
10✔
184
}
10✔
185

186
void UtestShell::runOneTest(TestPlugin* plugin, TestResult& result)
1,598✔
187
{
188
    hasFailed_ = false;
1,598✔
189
    result.countRun();
1,598✔
190
    HelperTestRunInfo runInfo(this, plugin, &result);
1,598✔
191
    if (isRunInSeperateProcess())
1,598✔
192
        PlatformSpecificSetJmp(helperDoRunOneTestSeperateProcess, &runInfo);
19✔
193
    else
194
        PlatformSpecificSetJmp(helperDoRunOneTestInCurrentProcess, &runInfo);
1,579✔
195
}
1,594✔
196

197
Utest* UtestShell::createTest()
19✔
198
{
199
    return new Utest();
19✔
200
}
201

202
void UtestShell::destroyTest(Utest* test)
1,579✔
203
{
204
    delete test;
1,579✔
205
}
1,579✔
206

207
void UtestShell::runOneTestInCurrentProcess(TestPlugin* plugin, TestResult& result)
1,581✔
208
{
209
    result.printVeryVerbose("\n-- before runAllPreTestAction: ");
1,581✔
210
    plugin->runAllPreTestAction(*this, result);
1,581✔
211
    result.printVeryVerbose("\n-- after runAllPreTestAction: ");
1,581✔
212

213
    //save test context, so that test class can be tested
214
    UtestShell* savedTest = UtestShell::getCurrent();
1,581✔
215
    TestResult* savedResult = UtestShell::getTestResult();
1,581✔
216

217
    UtestShell::setTestResult(&result);
1,581✔
218
    UtestShell::setCurrentTest(this);
1,581✔
219

220
    Utest* testToRun = NULLPTR;
1,581✔
221

222
#if CPPUTEST_HAVE_EXCEPTIONS
223
    try
224
    {
225
#endif
226
        result.printVeryVerbose("\n---- before createTest: ");
1,581✔
227
        testToRun = createTest();
1,581✔
228
        result.printVeryVerbose("\n---- after createTest: ");
1,581✔
229

230
        result.printVeryVerbose("\n------ before runTest: ");
1,581✔
231
        testToRun->run();
1,581✔
232
        result.printVeryVerbose("\n------ after runTest: ");
1,577✔
233

234
        UtestShell::setCurrentTest(savedTest);
1,577✔
235
        UtestShell::setTestResult(savedResult);
1,577✔
236
#if CPPUTEST_HAVE_EXCEPTIONS
237
    }
238
    catch(...)
2✔
239
    {
240
        destroyTest(testToRun);
2✔
241
        throw;
2✔
242
    }
2✔
243
#endif
244

245
    result.printVeryVerbose("\n---- before destroyTest: ");
1,577✔
246
    destroyTest(testToRun);
1,577✔
247
    result.printVeryVerbose("\n---- after destroyTest: ");
1,577✔
248

249
    result.printVeryVerbose("\n-- before runAllPostTestAction: ");
1,577✔
250
    plugin->runAllPostTestAction(*this, result);
1,577✔
251
    result.printVeryVerbose("\n-- after runAllPostTestAction: ");
1,577✔
252
}
1,577✔
253

254
UtestShell *UtestShell::getNext() const
201,690✔
255
{
256
    return next_;
201,690✔
257
}
258

259
UtestShell* UtestShell::addTest(UtestShell *test)
62,608✔
260
{
261
    next_ = test;
62,608✔
262
    return this;
62,608✔
263
}
264

265
size_t UtestShell::countTests()
33✔
266
{
267
    return next_ ? next_->countTests() + 1 : 1;
33✔
268
}
269

270
SimpleString UtestShell::getMacroName() const
1,892✔
271
{
272
    return "TEST";
1,892✔
273
}
274

275
const SimpleString UtestShell::getName() const
2,015✔
276
{
277
    return SimpleString(name_);
2,015✔
278
}
279

280
const SimpleString UtestShell::getGroup() const
120,422✔
281
{
282
    return SimpleString(group_);
120,422✔
283
}
284

285
SimpleString UtestShell::getFormattedName() const
1,951✔
286
{
287
    SimpleString formattedName(getMacroName());
1,951✔
288
    formattedName += "(";
1,951✔
289
    formattedName += group_;
1,951✔
290
    formattedName += ", ";
1,951✔
291
    formattedName += name_;
1,951✔
292
    formattedName += ")";
1,951✔
293

294
    return formattedName;
1,951✔
295
}
×
296

297
bool UtestShell::hasFailed() const
550✔
298
{
299
    return hasFailed_;
550✔
300
}
301

302
void UtestShell::countCheck()
414✔
303
{
304
    getTestResult()->countCheck();
414✔
305
}
414✔
306

307
bool UtestShell::willRun() const
1,656✔
308
{
309
    return true;
1,656✔
310
}
311

312
bool UtestShell::isRunInSeperateProcess() const
1,599✔
313
{
314
    return isRunAsSeperateProcess_;
1,599✔
315
}
316

317
void UtestShell::setRunInSeperateProcess()
20✔
318
{
319
    isRunAsSeperateProcess_ = true;
20✔
320
}
20✔
321

322

323
void UtestShell::setRunIgnored()
1✔
324
{
325

326
}
1✔
327

328
void UtestShell::setFileName(const char* fileName)
61,738✔
329
{
330
    file_ = fileName;
61,738✔
331
}
61,738✔
332

333
void UtestShell::setLineNumber(size_t lineNumber)
61,738✔
334
{
335
    lineNumber_ = lineNumber;
61,738✔
336
}
61,738✔
337

338
void UtestShell::setGroupName(const char* groupName)
61,750✔
339
{
340
    group_ = groupName;
61,750✔
341
}
61,750✔
342

343
void UtestShell::setTestName(const char* testName)
61,747✔
344
{
345
    name_ = testName;
61,747✔
346
}
61,747✔
347

348
const SimpleString UtestShell::getFile() const
2,182✔
349
{
350
    return SimpleString(file_);
2,182✔
351
}
352

353
size_t UtestShell::getLineNumber() const
2,184✔
354
{
355
    return lineNumber_;
2,184✔
356
}
357

358
bool UtestShell::match(const char* target, const TestFilter* filters) const
61,450✔
359
{
360
    if(filters == NULLPTR) return true;
61,450✔
361

362
    for(; filters != NULLPTR; filters = filters->getNext())
117,610✔
363
        if(filters->match(target)) return true;
59,514✔
364

365
    return false;
58,096✔
366
}
367

368
bool UtestShell::shouldRun(const TestFilter* groupFilters, const TestFilter* nameFilters) const
59,772✔
369
{
370
    return match(group_, groupFilters) && match(name_, nameFilters);
59,772✔
371
}
372

373
void UtestShell::failWith(const TestFailure& failure)
15✔
374
{
375
    failWith(failure, getCurrentTestTerminator());
15✔
376
} // LCOV_EXCL_LINE
377

378
void UtestShell::failWith(const TestFailure& failure, const TestTerminator& terminator)
152✔
379
{
380
    addFailure(failure);
152✔
381
    terminator.exitCurrentTest();
152✔
382
} // LCOV_EXCL_LINE
383

384
void UtestShell::addFailure(const TestFailure& failure)
156✔
385
{
386
    hasFailed_ = true;
156✔
387
    getTestResult()->addFailure(failure);
156✔
388
}
156✔
389

390
void UtestShell::exitTest(const TestTerminator& terminator)
1✔
391
{
392
    terminator.exitCurrentTest();
1✔
393
} // LCOV_EXCL_LINE
394

395
void UtestShell::assertTrue(bool condition, const char *checkString, const char *conditionString, const char* text, const char *fileName, size_t lineNumber, const TestTerminator& testTerminator)
710✔
396
{
397
    getTestResult()->countCheck();
710✔
398
    if (!condition)
710✔
399
        failWith(CheckFailure(this, fileName, lineNumber, checkString, conditionString, text), testTerminator);
37✔
400
}
701✔
401

402
void UtestShell::fail(const char *text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
32✔
403
{
404
    getTestResult()->countCheck();
32✔
405
    failWith(FailFailure(this, fileName, lineNumber, text), testTerminator);
88✔
406
} // LCOV_EXCL_LINE
407

408
void UtestShell::assertCstrEqual(const char* expected, const char* actual, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
912✔
409
{
410
    getTestResult()->countCheck();
912✔
411
    if (actual == NULLPTR && expected == NULLPTR) return;
912✔
412
    if (actual == NULLPTR || expected == NULLPTR)
912✔
413
        failWith(StringEqualFailure(this, fileName, lineNumber, expected, actual, text), testTerminator);
6✔
414
    if (SimpleString::StrCmp(expected, actual) != 0)
910✔
415
        failWith(StringEqualFailure(this, fileName, lineNumber, expected, actual, text), testTerminator);
8✔
416
}
417

418
void UtestShell::assertCstrNEqual(const char* expected, const char* actual, size_t length, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
15✔
419
{
420
    getTestResult()->countCheck();
15✔
421
    if (actual == NULLPTR && expected == NULLPTR) return;
15✔
422
    if (actual == NULLPTR || expected == NULLPTR)
15✔
423
        failWith(StringEqualFailure(this, fileName, lineNumber, expected, actual, text), testTerminator);
6✔
424
    if (SimpleString::StrNCmp(expected, actual, length) != 0)
13✔
425
        failWith(StringEqualFailure(this, fileName, lineNumber, expected, actual, text), testTerminator);
18✔
426
}
427

428
void UtestShell::assertCstrNoCaseEqual(const char* expected, const char* actual, const char* text, const char* fileName, size_t lineNumber)
7✔
429
{
430
    getTestResult()->countCheck();
7✔
431
    if (actual == NULLPTR && expected == NULLPTR) return;
7✔
432
    if (actual == NULLPTR || expected == NULLPTR)
7✔
433
        failWith(StringEqualNoCaseFailure(this, fileName, lineNumber, expected, actual, text));
6✔
434
    if (!SimpleString(expected).equalsNoCase(actual))
5✔
435
        failWith(StringEqualNoCaseFailure(this, fileName, lineNumber, expected, actual, text));
9✔
436
}
437

438
void UtestShell::assertCstrContains(const char* expected, const char* actual, const char* text, const char* fileName, size_t lineNumber)
406✔
439
{
440
    getTestResult()->countCheck();
406✔
441
    if (actual == NULLPTR && expected == NULLPTR) return;
406✔
442
    if (actual == NULLPTR || expected == NULLPTR)
406✔
443
        failWith(ContainsFailure(this, fileName, lineNumber, expected, actual, text));
10✔
444
    if (!SimpleString(actual).contains(expected))
404✔
445
        failWith(ContainsFailure(this, fileName, lineNumber, expected, actual, text));
10✔
446
}
447

448
void UtestShell::assertCstrNoCaseContains(const char* expected, const char* actual, const char* text, const char* fileName, size_t lineNumber)
15✔
449
{
450
    getTestResult()->countCheck();
15✔
451
    if (actual == NULLPTR && expected == NULLPTR) return;
15✔
452
    if (actual == NULLPTR || expected == NULLPTR)
15✔
453
        failWith(ContainsFailure(this, fileName, lineNumber, expected, actual, text));
10✔
454
    if (!SimpleString(actual).containsNoCase(expected))
13✔
455
        failWith(ContainsFailure(this, fileName, lineNumber, expected, actual, text));
10✔
456
}
457

458
void UtestShell::assertLongsEqual(long expected, long actual, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
668✔
459
{
460
    getTestResult()->countCheck();
668✔
461
    if (expected != actual)
668✔
462
        failWith(LongsEqualFailure (this, fileName, lineNumber, expected, actual, text), testTerminator);
19✔
463
}
659✔
464

465
void UtestShell::assertUnsignedLongsEqual(unsigned long expected, unsigned long actual, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
22✔
466
{
467
    getTestResult()->countCheck();
22✔
468
    if (expected != actual)
22✔
469
        failWith(UnsignedLongsEqualFailure (this, fileName, lineNumber, expected, actual, text), testTerminator);
10✔
470
}
16✔
471

472
void UtestShell::assertLongLongsEqual(cpputest_longlong expected, cpputest_longlong actual, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
34✔
473
{
474
    getTestResult()->countCheck();
34✔
475
#if CPPUTEST_USE_LONG_LONG
476
    if (expected != actual)
34✔
477
        failWith(LongLongsEqualFailure(this, fileName, lineNumber, expected, actual, text), testTerminator);
8✔
478
#else
479
    (void)expected;
480
    (void)actual;
481
    failWith(FeatureUnsupportedFailure(this, fileName, lineNumber, "CPPUTEST_USE_LONG_LONG", text), testTerminator);
482
#endif
483
}
30✔
484

485
void UtestShell::assertUnsignedLongLongsEqual(cpputest_ulonglong expected, cpputest_ulonglong actual, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
35✔
486
{
487
    getTestResult()->countCheck();
35✔
488
#if CPPUTEST_USE_LONG_LONG
489
    if (expected != actual)
35✔
490
        failWith(UnsignedLongLongsEqualFailure(this, fileName, lineNumber, expected, actual, text), testTerminator);
8✔
491
#else
492
    (void)expected;
493
    (void)actual;
494
    failWith(FeatureUnsupportedFailure(this, fileName, lineNumber, "CPPUTEST_USE_LONG_LONG", text), testTerminator);
495
#endif
496
}
31✔
497

498
void UtestShell::assertSignedBytesEqual(signed char expected, signed char actual, const char* text, const char *fileName, size_t lineNumber, const TestTerminator& testTerminator)
4✔
499
{
500
    getTestResult()->countCheck();
4✔
501
    if (expected != actual)
4✔
502
        failWith(SignedBytesEqualFailure (this, fileName, lineNumber, expected, actual, text), testTerminator);
6✔
503
}
2✔
504

505
void UtestShell::assertPointersEqual(const void* expected, const void* actual, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
108✔
506
{
507
    getTestResult()->countCheck();
108✔
508
    if (expected != actual)
108✔
509
        failWith(EqualsFailure(this, fileName, lineNumber, StringFrom(expected), StringFrom(actual), text), testTerminator);
12✔
510
}
104✔
511

512
void UtestShell::assertFunctionPointersEqual(void (*expected)(), void (*actual)(), const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
20✔
513
{
514
    getTestResult()->countCheck();
20✔
515
    if (expected != actual)
20✔
516
        failWith(EqualsFailure(this, fileName, lineNumber, StringFrom(expected), StringFrom(actual), text), testTerminator);
10✔
517
}
18✔
518

519
void UtestShell::assertDoublesEqual(double expected, double actual, double threshold, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
36✔
520
{
521
    getTestResult()->countCheck();
36✔
522
    if (!doubles_equal(expected, actual, threshold))
36✔
523
        failWith(DoublesEqualFailure(this, fileName, lineNumber, expected, actual, threshold, text), testTerminator);
8✔
524
}
32✔
525

526
void UtestShell::assertBinaryEqual(const void *expected, const void *actual, size_t length, const char* text, const char *fileName, size_t lineNumber, const TestTerminator& testTerminator)
16✔
527
{
528
    getTestResult()->countCheck();
16✔
529
        if (length == 0) return;
16✔
530
    if (actual == NULLPTR && expected == NULLPTR) return;
13✔
531
    if (actual == NULLPTR || expected == NULLPTR)
12✔
532
        failWith(BinaryEqualFailure(this, fileName, lineNumber, (const unsigned char *) expected, (const unsigned char *) actual, length, text), testTerminator);
6✔
533
    if (SimpleString::MemCmp(expected, actual, length) != 0)
10✔
534
        failWith(BinaryEqualFailure(this, fileName, lineNumber, (const unsigned char *) expected, (const unsigned char *) actual, length, text), testTerminator);
8✔
535
}
536

537
void UtestShell::assertBitsEqual(unsigned long expected, unsigned long actual, unsigned long mask, size_t byteCount, const char* text, const char *fileName, size_t lineNumber, const TestTerminator& testTerminator)
13✔
538
{
539
    getTestResult()->countCheck();
13✔
540
    if ((expected & mask) != (actual & mask))
13✔
541
        failWith(BitsEqualFailure(this, fileName, lineNumber, expected, actual, mask, byteCount, text), testTerminator);
8✔
542
}
9✔
543

544
void UtestShell::assertEquals(bool failed, const char* expected, const char* actual, const char* text, const char* file, size_t line, const TestTerminator& testTerminator)
33✔
545
{
546
    getTestResult()->countCheck();
33✔
547
    if (failed)
33✔
548
        failWith(CheckEqualFailure(this, file, line, expected, actual, text), testTerminator);
83✔
549
}
10✔
550

551
void UtestShell::assertCompare(bool comparison, const char *checkString, const char *comparisonString, const char *text, const char *fileName, size_t lineNumber, const TestTerminator &testTerminator)
2✔
552
{
553
    getTestResult()->countCheck();
2✔
554
    if (!comparison)
2✔
555
        failWith(ComparisonFailure(this, fileName, lineNumber, checkString, comparisonString, text), testTerminator);
10✔
556
}
×
557

558
void UtestShell::print(const char *text, const char* fileName, size_t lineNumber)
9✔
559
{
560
    SimpleString stringToPrint = "\n";
9✔
561
    stringToPrint += fileName;
9✔
562
    stringToPrint += ":";
9✔
563
    stringToPrint += StringFrom(lineNumber);
9✔
564
    stringToPrint += " ";
9✔
565
    stringToPrint += text;
9✔
566
    getTestResult()->print(stringToPrint.asCharString());
9✔
567
}
9✔
568

569
void UtestShell::print(const SimpleString& text, const char* fileName, size_t lineNumber)
1✔
570
{
571
    print(text.asCharString(), fileName, lineNumber);
1✔
572
}
1✔
573

574
void UtestShell::printVeryVerbose(const char* text)
9,356✔
575
{
576
    getTestResult()->printVeryVerbose(text);
9,356✔
577
}
9,356✔
578

579
TestResult* UtestShell::testResult_ = NULLPTR;
580
UtestShell* UtestShell::currentTest_ = NULLPTR;
581

582
void UtestShell::setTestResult(TestResult* result)
3,158✔
583
{
584
    testResult_ = result;
3,158✔
585
}
3,158✔
586

587
void UtestShell::setCurrentTest(UtestShell* test)
3,158✔
588
{
589
    currentTest_ = test;
3,158✔
590
}
3,158✔
591

592
TestResult* UtestShell::getTestResult()
14,604✔
593
{
594
    if (testResult_ == NULLPTR)
14,604✔
595
        return &OutsideTestRunnerUTest::instance().getTestResult();
184✔
596
    return testResult_;
14,420✔
597
}
598

599
UtestShell* UtestShell::getCurrent()
6,868✔
600
{
601
    if (currentTest_ == NULLPTR)
6,868✔
602
        return &OutsideTestRunnerUTest::instance();
184✔
603
    return currentTest_;
6,684✔
604
}
605

606
const TestTerminator &UtestShell::getCurrentTestTerminator()
2,606✔
607
{
608
    return *currentTestTerminator_;
2,606✔
609
}
610

611
const TestTerminator &UtestShell::getCurrentTestTerminatorWithoutExceptions()
82✔
612
{
613
    return *currentTestTerminatorWithoutExceptions_;
82✔
614
}
615

616
void UtestShell::setCrashOnFail()
4✔
617
{
618
    currentTestTerminator_ = &crashingTestTerminator;
4✔
619
    currentTestTerminatorWithoutExceptions_ = &crashingTestTerminatorWithoutExceptions;
4✔
620
}
4✔
621

622
void UtestShell::restoreDefaultTestTerminator()
4✔
623
{
624
    currentTestTerminator_ = &normalTestTerminator;
4✔
625
    currentTestTerminatorWithoutExceptions_ = &normalTestTerminatorWithoutExceptions;
4✔
626
}
4✔
627

628
void UtestShell::setRethrowExceptions(bool rethrowExceptions)
109✔
629
{
630
    rethrowExceptions_ = rethrowExceptions;
109✔
631
}
109✔
632

633
bool UtestShell::isRethrowingExceptions()
9✔
634
{
635
    return rethrowExceptions_;
9✔
636
}
637

638
ExecFunctionTestShell::~ExecFunctionTestShell()
688✔
639
{
640
}
688✔
641

642
////////////// Utest ////////////
643

644
Utest::Utest()
1,581✔
645
{
646
}
1,581✔
647

648
Utest::~Utest()
1,598✔
649
{
650
}
1,598✔
651

652
#if CPPUTEST_HAVE_EXCEPTIONS
653

654
void Utest::run()
1,581✔
655
{
656
    UtestShell* current = UtestShell::getCurrent();
1,581✔
657
    int jumpResult = 0;
1,581✔
658
    try {
659
        current->printVeryVerbose("\n-------- before setup: ");
1,581✔
660
        jumpResult = PlatformSpecificSetJmp(helperDoTestSetup, this);
1,581✔
661
        current->printVeryVerbose("\n-------- after  setup: ");
1,578✔
662

663
        if (jumpResult) {
1,578✔
664
            current->printVeryVerbose("\n----------  before body: ");
1,578✔
665
            PlatformSpecificSetJmp(helperDoTestBody, this);
1,578✔
666
            current->printVeryVerbose("\n----------  after body: ");
1,467✔
667
        }
668
    }
669
    catch (CppUTestFailedException&)
112✔
670
    {
671
        PlatformSpecificRestoreJumpBuffer();
108✔
672
    }
108✔
673
#if CPPUTEST_USE_STD_CPP_LIB
674
    catch (const std::exception &e)
2✔
675
    {
676
        current->addFailure(UnexpectedExceptionFailure(current, e));
2✔
677
        PlatformSpecificRestoreJumpBuffer();
2✔
678
        if (current->isRethrowingExceptions())
2✔
679
        {
680
            throw;
1✔
681
        }
682
    }
2✔
683
#endif
684
    catch (...)
2✔
685
    {
686
        current->addFailure(UnexpectedExceptionFailure(current));
2✔
687
        PlatformSpecificRestoreJumpBuffer();
2✔
688
        if (current->isRethrowingExceptions())
2✔
689
        {
690
            throw;
1✔
691
        }
692
    }
2✔
693

694
    try {
695
        current->printVeryVerbose("\n--------  before teardown: ");
1,577✔
696
        PlatformSpecificSetJmp(helperDoTestTeardown, this);
1,577✔
697
        current->printVeryVerbose("\n--------  after teardown: ");
1,575✔
698
    }
699
    catch (CppUTestFailedException&)
2✔
700
    {
701
        PlatformSpecificRestoreJumpBuffer();
2✔
702
    }
2✔
703
#if CPPUTEST_USE_STD_CPP_LIB
704
    catch (const std::exception &e)
×
705
    {
706
        current->addFailure(UnexpectedExceptionFailure(current, e));
×
707
        PlatformSpecificRestoreJumpBuffer();
×
708
        if (current->isRethrowingExceptions())
×
709
        {
710
            throw;
×
711
        }
712
    }
×
713
#endif
714
    catch (...)
×
715
    {
716
        current->addFailure(UnexpectedExceptionFailure(current));
×
717
        PlatformSpecificRestoreJumpBuffer();
×
718
        if (current->isRethrowingExceptions())
×
719
        {
720
            throw;
×
721
        }
722
    }
×
723
}
1,577✔
724
#else
725

726
void Utest::run()
727
{
728
    if (PlatformSpecificSetJmp(helperDoTestSetup, this)) {
729
        PlatformSpecificSetJmp(helperDoTestBody, this);
730
    }
731
    PlatformSpecificSetJmp(helperDoTestTeardown, this);
732
}
733

734
#endif
735

736
void Utest::setup()
574✔
737
{
738
}
574✔
739

740
void Utest::testBody()
19✔
741
{
742
}
19✔
743

744
void Utest::teardown()
293✔
745
{
746
}
293✔
747

748

749
/////////////////// Terminators
750

751
TestTerminator::~TestTerminator()
329✔
752
{
753
}
329✔
754

755
void NormalTestTerminator::exitCurrentTest() const
110✔
756
{
757
    #if CPPUTEST_HAVE_EXCEPTIONS
758
        throw CppUTestFailedException();
110✔
759
    #else
760
        TestTerminatorWithoutExceptions().exitCurrentTest();
761
    #endif
762
}
763

764
NormalTestTerminator::~NormalTestTerminator()
162✔
765
{
766
}
162✔
767

768
void TestTerminatorWithoutExceptions::exitCurrentTest() const
43✔
769
{
770
    PlatformSpecificLongJmp();
43✔
771
} // LCOV_EXCL_LINE
772

773
TestTerminatorWithoutExceptions::~TestTerminatorWithoutExceptions()
162✔
774
{
775
}
162✔
776

777
void CrashingTestTerminator::exitCurrentTest() const
2✔
778
{
779
    UtestShell::crash();
2✔
780
    NormalTestTerminator::exitCurrentTest();
2✔
781
}
×
782

783
CrashingTestTerminator::~CrashingTestTerminator()
81✔
784
{
785
}
81✔
786

787
void CrashingTestTerminatorWithoutExceptions::exitCurrentTest() const
2✔
788
{
789
    UtestShell::crash();
2✔
790
    TestTerminatorWithoutExceptions::exitCurrentTest();
2✔
791
}
×
792

793
CrashingTestTerminatorWithoutExceptions::~CrashingTestTerminatorWithoutExceptions()
81✔
794
{
795
}
81✔
796

797
//////////////////// ExecFunction
798
//
799
ExecFunction::ExecFunction()
227✔
800
{
801
}
227✔
802

803
ExecFunction::~ExecFunction()
226✔
804
{
805
}
226✔
806

807
ExecFunctionWithoutParameters::ExecFunctionWithoutParameters(void(*testFunction)())
176✔
808
    : testFunction_(testFunction)
176✔
809
{
810
}
176✔
811

812
ExecFunctionWithoutParameters::~ExecFunctionWithoutParameters()
350✔
813
{
814
}
350✔
815

816
void ExecFunctionWithoutParameters::exec()
163✔
817
{
818
    if (testFunction_)
163✔
819
        testFunction_();
163✔
820
}
20✔
821

822
//////////////////// ExecFunctionTest
823

824
ExecFunctionTest::ExecFunctionTest(ExecFunctionTestShell* shell)
200✔
825
    : shell_(shell)
200✔
826
{
827
}
200✔
828

829
void ExecFunctionTest::testBody()
198✔
830
{
831
    if (shell_->testFunction_) shell_->testFunction_->exec();
198✔
832
}
45✔
833

834
void ExecFunctionTest::setup()
200✔
835
{
836
    if (shell_->setup_) shell_->setup_();
200✔
837
}
198✔
838

839
void ExecFunctionTest::teardown()
197✔
840
{
841
    if (shell_->teardown_) shell_->teardown_();
197✔
842
}
195✔
843

844
/////////////// IgnoredUtestShell /////////////
845
IgnoredUtestShell::IgnoredUtestShell(): runIgnored_(false)
2,948✔
846
{
847
}
2,948✔
848

849
IgnoredUtestShell::IgnoredUtestShell(const char* groupName, const char* testName, const char* fileName, size_t lineNumber) :
5✔
850
   UtestShell(groupName, testName, fileName, lineNumber), runIgnored_(false)
5✔
851
{
852
}
5✔
853

854
IgnoredUtestShell::~IgnoredUtestShell()
2,976✔
855
{
856
}
2,976✔
857

858
bool IgnoredUtestShell::willRun() const
70✔
859
{
860
    if (runIgnored_) return UtestShell::willRun();
70✔
861

862
    return false;
66✔
863
}
864

865
SimpleString IgnoredUtestShell::getMacroName() const
59✔
866
{
867
    if (runIgnored_) return "TEST";
59✔
868

869
    return "IGNORE_TEST";
58✔
870
}
871

872
void IgnoredUtestShell::runOneTest(TestPlugin* plugin, TestResult& result)
65✔
873
{
874
    if (runIgnored_)
65✔
875
    {
876
        UtestShell::runOneTest(plugin, result);
3✔
877
        return;
3✔
878
    }
879

880
    result.countIgnored();
62✔
881
}
882

883
void IgnoredUtestShell::setRunIgnored()
4✔
884
{
885
    runIgnored_ = true;
4✔
886
}
4✔
887

888
//////////////////// UtestShellPointerArray
889

890
UtestShellPointerArray::UtestShellPointerArray(UtestShell* firstTest)
14✔
891
    : arrayOfTests_(NULLPTR), count_(0)
14✔
892
{
893
    count_ = (firstTest) ? firstTest->countTests() : 0;
14✔
894
    if (count_ == 0) return;
14✔
895

896
    arrayOfTests_ = new UtestShell*[count_];
11✔
897

898
    UtestShell*currentTest = firstTest;
11✔
899
    for (size_t i = 0; i < count_; i++)
37✔
900
    {
901
        arrayOfTests_[i] = currentTest;
26✔
902
        currentTest = currentTest->getNext();
26✔
903
    }
904
}
905

906
UtestShellPointerArray::~UtestShellPointerArray()
14✔
907
{
908
    delete [] arrayOfTests_;
14✔
909
}
14✔
910

911
void UtestShellPointerArray::swap(size_t index1, size_t index2)
10✔
912
{
913
        UtestShell* e2 = arrayOfTests_[index2];
10✔
914
        UtestShell* e1 = arrayOfTests_[index1];
10✔
915
        arrayOfTests_[index1] = e2;
10✔
916
        arrayOfTests_[index2] = e1;
10✔
917
}
10✔
918

919
void UtestShellPointerArray::shuffle(size_t seed)
8✔
920
{
921
    if (count_ == 0) return;
8✔
922

923
    PlatformSpecificSrand((unsigned int) seed);
6✔
924

925
    for (size_t i = count_ - 1; i >= 1; --i)
13✔
926
    {
927
        if (count_ == 0) return;
7✔
928

929
        const size_t j = ((size_t)PlatformSpecificRand()) % (i + 1); // distribution biased by modulo, but good enough for shuffling
7✔
930
        swap(i, j);
7✔
931
   }
932
   relinkTestsInOrder();
6✔
933
}
934

935
void UtestShellPointerArray::reverse()
4✔
936
{
937
    if (count_ == 0) return;
4✔
938

939
    size_t halfCount = count_ / 2;
3✔
940
    for (size_t i = 0; i < halfCount; i++)
6✔
941
    {
942
        size_t j = count_ - i - 1;
3✔
943
        swap(i, j);
3✔
944
   }
945
   relinkTestsInOrder();
3✔
946
}
947

948
void UtestShellPointerArray::relinkTestsInOrder()
10✔
949
{
950
    UtestShell *tests = NULLPTR;
10✔
951
    for (size_t i = 0; i < count_; i++)
33✔
952
        tests = arrayOfTests_[count_ - i - 1]->addTest(tests);
23✔
953
}
10✔
954

955
UtestShell* UtestShellPointerArray::getFirstTest() const
9✔
956
{
957
    return get(0);
9✔
958
}
959

960
UtestShell* UtestShellPointerArray::get(size_t index) const
24✔
961
{
962
    if (index >= count_) return NULLPTR;
24✔
963
    return arrayOfTests_[index];
21✔
964
}
965

966

967

968
////////////// TestInstaller ////////////
969

970
TestInstaller::TestInstaller(UtestShell& shell, const char* groupName, const char* testName, const char* fileName, size_t lineNumber)
61,299✔
971
{
972
    shell.setGroupName(groupName);
61,299✔
973
    shell.setTestName(testName);
61,299✔
974
    shell.setFileName(fileName);
61,299✔
975
    shell.setLineNumber(lineNumber);
61,299✔
976
    TestRegistry::getCurrentRegistry()->addTest(&shell);
61,299✔
977
}
61,299✔
978

979
TestInstaller::~TestInstaller()
61,300✔
980
{
981
}
61,300✔
982

983
void TestInstaller::unDo()
1✔
984
{
985
    TestRegistry::getCurrentRegistry()->unDoLastAddTest();
1✔
986
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc