• 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

99.54
/src/CppUTest/TestFailure.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/TestFailure.h"
30
#include "CppUTest/TestOutput.h"
31
#include "CppUTest/SimpleString.h"
32
#include "CppUTest/PlatformSpecificFunctions.h"
33

34
#if CPPUTEST_USE_STD_CPP_LIB
35
#include <typeinfo>
36
#if defined(__GNUC__)
37
#include <cxxabi.h>
38
#include <memory>
39
#endif
40
#endif
41

42
TestFailure::TestFailure(UtestShell* test, const char* fileName, size_t lineNumber, const SimpleString& theMessage) :
133✔
43
    testName_(test->getFormattedName()), testNameOnly_(test->getName()), fileName_(fileName), lineNumber_(lineNumber), testFileName_(test->getFile()), testLineNumber_(test->getLineNumber()), message_(theMessage)
133✔
44
{
45
}
133✔
46

47
TestFailure::TestFailure(UtestShell* test, const SimpleString& theMessage) :
190✔
48
    testName_(test->getFormattedName()), testNameOnly_(test->getName()), fileName_(test->getFile()), lineNumber_(test->getLineNumber()), testFileName_(test->getFile()), testLineNumber_(test->getLineNumber()), message_(theMessage)
190✔
49
{
50
}
190✔
51

52
TestFailure::TestFailure(UtestShell* test, const char* fileName, size_t lineNum) :
198✔
53
    testName_(test->getFormattedName()), testNameOnly_(test->getName()), fileName_(fileName), lineNumber_(lineNum), testFileName_(test->getFile()), testLineNumber_(test->getLineNumber()), message_("no message")
198✔
54
{
55
}
198✔
56

57
TestFailure::TestFailure(const TestFailure& f) :
12✔
58
    testName_(f.testName_), testNameOnly_(f.testNameOnly_), fileName_(f.fileName_), lineNumber_(f.lineNumber_), testFileName_(f.testFileName_), testLineNumber_(f.testLineNumber_), message_(f.message_)
12✔
59
{
60
}
12✔
61

62

63
TestFailure::~TestFailure()
631✔
64
{
65
}
631✔
66

67
SimpleString TestFailure::getFileName() const
201✔
68
{
69
    return fileName_;
201✔
70
}
71

72
SimpleString TestFailure::getTestFileName() const
152✔
73
{
74
    return testFileName_;
152✔
75
}
76

77
SimpleString TestFailure::getTestName() const
185✔
78
{
79
    return testName_;
185✔
80
}
81

82
SimpleString TestFailure::getTestNameOnly() const
4✔
83
{
84
    return testNameOnly_;
4✔
85
}
86

87
size_t TestFailure::getFailureLineNumber() const
201✔
88
{
89
    return lineNumber_;
201✔
90
}
91

92
size_t TestFailure::getTestLineNumber() const
152✔
93
{
94
    return testLineNumber_;
152✔
95
}
96

97
SimpleString TestFailure::getMessage() const
413✔
98
{
99
    return message_;
413✔
100
}
101

102
bool TestFailure::isOutsideTestFile() const
189✔
103
{
104
    return testFileName_ != fileName_;
189✔
105
}
106

107
bool TestFailure::isInHelperFunction() const
40✔
108
{
109
    return lineNumber_ < testLineNumber_;
40✔
110
}
111

112
SimpleString TestFailure::createButWasString(const SimpleString& expected, const SimpleString& actual)
134✔
113
{
114
    return StringFromFormat("expected <%s>\n\tbut was  <%s>", expected.asCharString(), actual.asCharString());
134✔
115
}
116

117
SimpleString TestFailure::createDifferenceAtPosString(const SimpleString& actual, size_t offset, size_t reportedPosition)
60✔
118
{
119
    SimpleString result;
60✔
120
    const size_t extraCharactersWindow = 20;
60✔
121
    const size_t halfOfExtraCharactersWindow = extraCharactersWindow / 2;
60✔
122

123
    SimpleString paddingForPreventingOutOfBounds (" ", halfOfExtraCharactersWindow);
60✔
124
    SimpleString actualString = paddingForPreventingOutOfBounds + actual + paddingForPreventingOutOfBounds;
60✔
125
    SimpleString differentString = StringFromFormat("difference starts at position %lu at: <", (unsigned long) reportedPosition);
60✔
126

127
    result += "\n";
60✔
128
    result += StringFromFormat("\t%s%s>\n", differentString.asCharString(), actualString.subString(offset, extraCharactersWindow).asCharString());
60✔
129

130

131
    result += StringFromFormat("\t%s^", SimpleString(" ", (differentString.size() + halfOfExtraCharactersWindow)).asCharString());
60✔
132
    return result;
120✔
133
}
60✔
134

135
SimpleString TestFailure::createUserText(const SimpleString& text)
162✔
136
{
137
    SimpleString userMessage = "";
162✔
138
    if (!text.isEmpty())
162✔
139
    {
140
        //This is a kludge to turn off "Message: " for this case.
141
        //I don't think "Message: " adds anything, as you get to see the
142
        //message. I propose we remove "Message: " lead in
143
        if (!text.startsWith("LONGS_EQUAL"))
53✔
144
            userMessage += "Message: ";
50✔
145
        userMessage += text;
53✔
146
        userMessage += "\n\t";
53✔
147
    }
148
    return userMessage;
162✔
149
}
×
150

151
EqualsFailure::EqualsFailure(UtestShell* test, const char* fileName, size_t lineNumber, const char* expected, const char* actual, const SimpleString& text) :
4✔
152
    TestFailure(test, fileName, lineNumber)
4✔
153
{
154
    message_ = createUserText(text);
4✔
155

156
    message_ += createButWasString(StringFromOrNull(expected), StringFromOrNull(actual));
4✔
157
}
4✔
158

159
EqualsFailure::EqualsFailure(UtestShell* test, const char* fileName, size_t lineNumber, const SimpleString& expected, const SimpleString& actual, const SimpleString& text)
6✔
160
    : TestFailure(test, fileName, lineNumber)
6✔
161
{
162
    message_ = createUserText(text);
6✔
163

164
    message_ += createButWasString(expected, actual);
6✔
165
}
6✔
166

167
DoublesEqualFailure::DoublesEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, double expected, double actual, double threshold, const SimpleString& text)
12✔
168
: TestFailure(test, fileName, lineNumber)
12✔
169
{
170
    message_ = createUserText(text);
12✔
171

172
    message_ += createButWasString(StringFrom(expected, 7), StringFrom(actual, 7));
12✔
173
    message_ += " threshold used was <";
12✔
174
    message_ += StringFrom(threshold, 7);
12✔
175
    message_ += ">";
12✔
176

177
    if (PlatformSpecificIsNan(expected) || PlatformSpecificIsNan(actual) || PlatformSpecificIsNan(threshold))
12✔
178
        message_ += "\n\tCannot make comparisons with Nan";
4✔
179
}
12✔
180

181
CheckEqualFailure::CheckEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, const SimpleString& expected, const SimpleString& actual, const SimpleString& text)
25✔
182
: TestFailure(test, fileName, lineNumber)
25✔
183
{
184
    message_ = createUserText(text);
25✔
185

186
    SimpleString printableExpected = PrintableStringFromOrNull(expected.asCharString());
25✔
187
    SimpleString printableActual = PrintableStringFromOrNull(actual.asCharString());
25✔
188

189
    message_ += createButWasString(printableExpected, printableActual);
25✔
190

191
    size_t failStart;
192
    for (failStart = 0; actual.at(failStart) == expected.at(failStart); failStart++)
31✔
193
        ;
194
    size_t failStartPrintable;
195
    for (failStartPrintable = 0; printableActual.at(failStartPrintable) == printableExpected.at(failStartPrintable); failStartPrintable++)
31✔
196
        ;
197
    message_ += createDifferenceAtPosString(printableActual, failStartPrintable, failStart);
25✔
198
}
25✔
199

200
ComparisonFailure::ComparisonFailure(UtestShell *test, const char *fileName, size_t lineNumber, const SimpleString& checkString, const SimpleString &comparisonString, const SimpleString &text)
2✔
201
: TestFailure(test, fileName, lineNumber)
2✔
202
{
203
    message_ = createUserText(text);
2✔
204
    message_ += checkString;
2✔
205
    message_ += "(";
2✔
206
    message_ += comparisonString;
2✔
207
    message_ += ") failed";
2✔
208
}
2✔
209

210
ContainsFailure::ContainsFailure(UtestShell* test, const char* fileName, size_t lineNumber, const SimpleString& expected, const SimpleString& actual, const SimpleString& text)
8✔
211
: TestFailure(test, fileName, lineNumber)
8✔
212
{
213
    message_ = createUserText(text);
8✔
214

215
    message_ += StringFromFormat("actual <%s>\n\tdid not contain  <%s>", actual.asCharString(), expected.asCharString());
8✔
216
}
8✔
217

218
CheckFailure::CheckFailure(UtestShell* test, const char* fileName, size_t lineNumber, const SimpleString& checkString, const SimpleString& conditionString, const SimpleString& text)
17✔
219
: TestFailure(test, fileName, lineNumber)
17✔
220
{
221
    message_ = createUserText(text);
17✔
222

223
    message_ += checkString;
17✔
224
    message_ += "(";
17✔
225
    message_ += conditionString;
17✔
226
    message_ += ") failed";
17✔
227
}
17✔
228

229
FailFailure::FailFailure(UtestShell* test, const char* fileName, size_t lineNumber, const SimpleString& message) : TestFailure(test, fileName, lineNumber)
35✔
230
{
231
    message_ = message;
35✔
232
}
35✔
233

234
LongsEqualFailure::LongsEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, long expected, long actual, const SimpleString& text)
11✔
235
: TestFailure(test, fileName, lineNumber)
11✔
236
{
237
    message_ = createUserText(text);
11✔
238

239
    SimpleString aDecimal = StringFrom(actual);
11✔
240
    SimpleString eDecimal = StringFrom(expected);
11✔
241

242
    SimpleString::padStringsToSameLength(aDecimal, eDecimal, ' ');
11✔
243

244
    SimpleString actualReported = aDecimal + " " + BracketsFormattedHexStringFrom(actual);
11✔
245
    SimpleString expectedReported = eDecimal + " " + BracketsFormattedHexStringFrom(expected);
11✔
246
    message_ += createButWasString(expectedReported, actualReported);
11✔
247
}
11✔
248

249
UnsignedLongsEqualFailure::UnsignedLongsEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, unsigned long expected, unsigned long actual, const SimpleString& text)
6✔
250
: TestFailure(test, fileName, lineNumber)
6✔
251
{
252
    message_ = createUserText(text);
6✔
253

254
    SimpleString aDecimal = StringFrom(actual);
6✔
255
    SimpleString eDecimal = StringFrom(expected);
6✔
256

257
    SimpleString::padStringsToSameLength(aDecimal, eDecimal, ' ');
6✔
258

259
    SimpleString actualReported = aDecimal + " " + BracketsFormattedHexStringFrom(actual);
6✔
260
    SimpleString expectedReported = eDecimal + " " + BracketsFormattedHexStringFrom(expected);
6✔
261

262
    message_ += createButWasString(expectedReported, actualReported);
6✔
263
}
6✔
264

265
LongLongsEqualFailure::LongLongsEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, cpputest_longlong expected, cpputest_longlong actual, const SimpleString& text)
5✔
266
: TestFailure(test, fileName, lineNumber)
5✔
267
{
268
    message_ = createUserText(text);
5✔
269

270
    SimpleString aDecimal = StringFrom(actual);
5✔
271
    SimpleString eDecimal = StringFrom(expected);
5✔
272

273
    SimpleString::padStringsToSameLength(aDecimal, eDecimal, ' ');
5✔
274

275
    SimpleString actualReported = aDecimal + " " + BracketsFormattedHexStringFrom(actual);
5✔
276
    SimpleString expectedReported = eDecimal + " " + BracketsFormattedHexStringFrom(expected);
5✔
277
    message_ += createButWasString(expectedReported, actualReported);
5✔
278
}
5✔
279

280
UnsignedLongLongsEqualFailure::UnsignedLongLongsEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, cpputest_ulonglong expected, cpputest_ulonglong actual, const SimpleString& text)
5✔
281
: TestFailure(test, fileName, lineNumber)
5✔
282
{
283
    message_ = createUserText(text);
5✔
284

285
    SimpleString aDecimal = StringFrom(actual);
5✔
286
    SimpleString eDecimal = StringFrom(expected);
5✔
287

288
    SimpleString::padStringsToSameLength(aDecimal, eDecimal, ' ');
5✔
289

290
    SimpleString actualReported = aDecimal + " " + BracketsFormattedHexStringFrom(actual);
5✔
291
    SimpleString expectedReported = eDecimal + " " + BracketsFormattedHexStringFrom(expected);
5✔
292
    message_ += createButWasString(expectedReported, actualReported);
5✔
293
}
5✔
294

295
SignedBytesEqualFailure::SignedBytesEqualFailure (UtestShell* test, const char* fileName, size_t lineNumber, signed char expected, signed char actual, const SimpleString& text)
3✔
296
: TestFailure(test, fileName, lineNumber)
3✔
297
{
298
    message_ = createUserText(text);
3✔
299

300
    SimpleString aDecimal = StringFrom((int)actual);
3✔
301
    SimpleString eDecimal = StringFrom((int)expected);
3✔
302

303
    SimpleString::padStringsToSameLength(aDecimal, eDecimal, ' ');
3✔
304

305
    SimpleString actualReported = aDecimal + " " + BracketsFormattedHexStringFrom(actual);
3✔
306
    SimpleString expectedReported = eDecimal + " " + BracketsFormattedHexStringFrom(expected);
3✔
307
    message_ += createButWasString(expectedReported, actualReported);
3✔
308
}
3✔
309

310
StringEqualFailure::StringEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, const char* expected, const char* actual, const SimpleString& text)
25✔
311
: TestFailure(test, fileName, lineNumber)
25✔
312
{
313
    message_ = createUserText(text);
25✔
314

315
    SimpleString printableExpected = PrintableStringFromOrNull(expected);
25✔
316
    SimpleString printableActual = PrintableStringFromOrNull(actual);
25✔
317

318
    message_ += createButWasString(printableExpected, printableActual);
25✔
319
    if((expected) && (actual))
25✔
320
    {
321
        size_t failStart;
322
        for (failStart = 0; actual[failStart] == expected[failStart]; failStart++)
107✔
323
            ;
324
        size_t failStartPrintable;
325
        for (failStartPrintable = 0; printableActual.at(failStartPrintable) == printableExpected.at(failStartPrintable); failStartPrintable++)
109✔
326
            ;
327
        message_ += createDifferenceAtPosString(printableActual, failStartPrintable, failStart);
19✔
328
    }
329
}
25✔
330

331
StringEqualNoCaseFailure::StringEqualNoCaseFailure(UtestShell* test, const char* fileName, size_t lineNumber, const char* expected, const char* actual, const SimpleString& text)
10✔
332
: TestFailure(test, fileName, lineNumber)
10✔
333
{
334
    message_ = createUserText(text);
10✔
335

336
    SimpleString printableExpected = PrintableStringFromOrNull(expected);
10✔
337
    SimpleString printableActual = PrintableStringFromOrNull(actual);
10✔
338

339
    message_ += createButWasString(printableExpected, printableActual);
10✔
340
    if((expected) && (actual))
10✔
341
    {
342
        size_t failStart;
343
        for (failStart = 0; SimpleString::ToLower(actual[failStart]) == SimpleString::ToLower(expected[failStart]); failStart++)
19✔
344
            ;
345
        size_t failStartPrintable;
346
        for (failStartPrintable = 0;
19✔
347
             SimpleString::ToLower(printableActual.at(failStartPrintable)) == SimpleString::ToLower(printableExpected.at(failStartPrintable));
19✔
348
             failStartPrintable++)
349
            ;
350
        message_ += createDifferenceAtPosString(printableActual, failStartPrintable, failStart);
6✔
351
    }
352
}
10✔
353

354
BinaryEqualFailure::BinaryEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, const unsigned char* expected,
14✔
355
                                       const unsigned char* actual, size_t size, const SimpleString& text)
14✔
356
: TestFailure(test, fileName, lineNumber)
14✔
357
{
358
    message_ = createUserText(text);
14✔
359

360
    SimpleString actualHex = StringFromBinaryOrNull(actual, size);
14✔
361

362
        message_ += createButWasString(StringFromBinaryOrNull(expected, size), actualHex);
14✔
363
        if ((expected) && (actual))
14✔
364
        {
365
                size_t failStart;
366
                for (failStart = 0; actual[failStart] == expected[failStart]; failStart++)
29✔
367
                        ;
368
                message_ += createDifferenceAtPosString(actualHex, (failStart * 3 + 1), failStart);
10✔
369
        }
370
}
14✔
371

372
BitsEqualFailure::BitsEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, unsigned long expected, unsigned long actual,
8✔
373
                                   unsigned long mask, size_t byteCount, const SimpleString& text)
8✔
374
: TestFailure(test, fileName, lineNumber)
8✔
375
{
376
    message_ = createUserText(text);
8✔
377

378
    message_ += createButWasString(StringFromMaskedBits(expected, mask, byteCount), StringFromMaskedBits(actual, mask, byteCount));
8✔
379
}
8✔
380

381
FeatureUnsupportedFailure::FeatureUnsupportedFailure(UtestShell* test, const char* fileName, size_t lineNumber,
1✔
382
                                                     const SimpleString& featureName, const SimpleString& text)
1✔
383
: TestFailure(test, fileName, lineNumber)
1✔
384
{
385
    message_ = createUserText(text);
1✔
386

387
    message_ += StringFromFormat("The feature \"%s\" is not supported in this environment or with the feature set selected when building the library.", featureName.asCharString());
1✔
388
}
1✔
389

390
#if CPPUTEST_HAVE_EXCEPTIONS
391
UnexpectedExceptionFailure::UnexpectedExceptionFailure(UtestShell* test)
3✔
392
: TestFailure(test, "Unexpected exception of unknown type was thrown.")
3✔
393
{
394
}
3✔
395

396
#if CPPUTEST_USE_STD_CPP_LIB
397
#if CPPUTEST_HAVE_RTTI
398
static SimpleString getExceptionTypeName(const std::exception &e)
3✔
399
{
400
    const char *name = typeid(e).name();
3✔
401
#if defined(__GNUC__) && (__cplusplus >= 201103L)
402
    int status = -1;
3✔
403

404
    std::unique_ptr<char, void(*)(void*)> demangledName(
405
        abi::__cxa_demangle(name, NULLPTR, NULLPTR, &status),
406
        std::free );
3✔
407

408
    return (status==0) ? demangledName.get() : name;
6✔
409
#else
410
    return name;
411
#endif
412
}
3✔
413
#endif // CPPUTEST_HAVE_RTTI
414

415
UnexpectedExceptionFailure::UnexpectedExceptionFailure(UtestShell* test, const std::exception &e)
3✔
416
: TestFailure(
417
    test,
418
#if CPPUTEST_HAVE_RTTI
419
    StringFromFormat(
6✔
420
        "Unexpected exception of type '%s' was thrown: %s",
421
        getExceptionTypeName(e).asCharString(),
6✔
422
        e.what()
3✔
423
    )
424
#else
425
    "Unexpected exception of unknown type was thrown."
426
#endif
427
)
6✔
428
{
429
    (void) e;
430
}
3✔
431
#endif // CPPUTEST_USE_STD_CPP_LIB
432
#endif // CPPUTEST_HAVE_EXCEPTIONS
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