Skip to content

Commit 0fe775c

Browse files
authored
testrunner: stop comparing mismatching types in generic assertEquals() / added ASSERT_EQUALS_ENUM (danmar#6035)
If only one case explicitly specified the enum it would have compiled with `ASSERT_EQUALS` but we would be have an implicit conversion and would not have detected a type mismatch.
1 parent 8063d54 commit 0fe775c

8 files changed

Lines changed: 267 additions & 271 deletions

test/fixture.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class TestFixture : public ErrorLogger {
6868

6969
bool assert_(const char * const filename, const unsigned int linenr, const bool condition) const;
7070

71-
template<typename T, typename U>
72-
bool assertEquals(const char* const filename, const unsigned int linenr, const T& expected, const U& actual, const std::string& msg = emptyString) const {
71+
template<typename T>
72+
bool assertEquals(const char* const filename, const unsigned int linenr, const T& expected, const T& actual, const std::string& msg = emptyString) const {
7373
if (expected != actual) {
7474
std::ostringstream expectedStr;
7575
expectedStr << expected;
@@ -81,6 +81,13 @@ class TestFixture : public ErrorLogger {
8181
return expected == actual;
8282
}
8383

84+
template<typename T>
85+
bool assertEqualsEnum(const char* const filename, const unsigned int linenr, const T& expected, const T& actual, const std::string& msg = emptyString) const {
86+
if (std::is_unsigned<T>())
87+
return assertEquals(filename, linenr, static_cast<std::uint64_t>(expected), static_cast<std::uint64_t>(actual), msg);
88+
return assertEquals(filename, linenr, static_cast<std::int64_t>(expected), static_cast<std::int64_t>(actual), msg);
89+
}
90+
8491
//Helper function to be called when an assertEquals assertion fails.
8592
//Writes the appropriate failure message to errmsg and increments fails_counter
8693
void assertEqualsFailed(const char* const filename, const unsigned int linenr, const std::string& expected, const std::string& actual, const std::string& msg) const;
@@ -271,6 +278,7 @@ class TestFixture : public ErrorLogger {
271278
#define ASSERT_EQUALS_WITHOUT_LINENUMBERS( EXPECTED, ACTUAL ) assertEqualsWithoutLineNumbers(__FILE__, __LINE__, EXPECTED, ACTUAL)
272279
#define ASSERT_EQUALS_DOUBLE( EXPECTED, ACTUAL, TOLERANCE ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE)
273280
#define ASSERT_EQUALS_MSG( EXPECTED, ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
281+
#define ASSERT_EQUALS_ENUM( EXPECTED, ACTUAL ) if (!assertEqualsEnum(__FILE__, __LINE__, (EXPECTED), (ACTUAL))) return
274282
#define ASSERT_THROW( CMD, EXCEPTION ) do { try { CMD; assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&) {} catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
275283
#define ASSERT_THROW_EQUALS( CMD, EXCEPTION, EXPECTED ) do { try { CMD; assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.errorMessage); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
276284
#define ASSERT_THROW_EQUALS_2( CMD, EXCEPTION, EXPECTED ) do { try { CMD; assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.what()); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)

test/testcmdlineparser.cpp

Lines changed: 235 additions & 247 deletions
Large diffs are not rendered by default.

test/testerrorlogger.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ class TestErrorLogger : public TestFixture {
280280
ASSERT(doc.Parse(xmldata, sizeof(xmldata)) == tinyxml2::XML_SUCCESS);
281281
ErrorMessage msg(doc.FirstChildElement());
282282
ASSERT_EQUALS("errorId", msg.id);
283-
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg.severity));
283+
ASSERT_EQUALS_ENUM(Severity::error, msg.severity);
284284
ASSERT_EQUALS(123u, msg.cwe.id);
285-
ASSERT_EQUALS(static_cast<int>(Certainty::inconclusive), static_cast<int>(msg.certainty));
285+
ASSERT_EQUALS_ENUM(Certainty::inconclusive, msg.certainty);
286286
ASSERT_EQUALS("Programming error.", msg.shortMessage());
287287
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
288288
ASSERT_EQUALS(456u, msg.hash);
@@ -329,9 +329,9 @@ class TestErrorLogger : public TestFixture {
329329
ErrorMessage msg2;
330330
ASSERT_NO_THROW(msg2.deserialize(msg_str));
331331
ASSERT_EQUALS("errorId", msg2.id);
332-
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg2.severity));
332+
ASSERT_EQUALS_ENUM(Severity::error, msg2.severity);
333333
ASSERT_EQUALS("test.cpp", msg2.file0);
334-
ASSERT_EQUALS(static_cast<int>(Certainty::inconclusive), static_cast<int>(msg2.certainty));
334+
ASSERT_EQUALS_ENUM(Certainty::inconclusive, msg2.certainty);
335335
ASSERT_EQUALS("Programming error", msg2.shortMessage());
336336
ASSERT_EQUALS("Programming error", msg2.verboseMessage());
337337
}
@@ -417,7 +417,7 @@ class TestErrorLogger : public TestFixture {
417417
ErrorMessage msg2;
418418
ASSERT_NO_THROW(msg2.deserialize(msg_str));
419419
ASSERT_EQUALS("errorId", msg2.id);
420-
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg2.severity));
420+
ASSERT_EQUALS_ENUM(Severity::error, msg2.severity);
421421
ASSERT_EQUALS("1.c", msg2.file0);
422422
ASSERT_EQUALS("Illegal character in \"foo\\001bar\"", msg2.shortMessage());
423423
ASSERT_EQUALS("Illegal character in \"foo\\001bar\"", msg2.verboseMessage());

test/testlibrary.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,12 +646,12 @@ class TestLibrary : public TestFixture {
646646
ASSERT(a && b);
647647
if (a && b) {
648648
ASSERT_EQUALS("Message", a->message);
649-
ASSERT_EQUALS(static_cast<int>(Severity::style), static_cast<int>(a->severity));
649+
ASSERT_EQUALS_ENUM(Severity::style, a->severity);
650650
ASSERT_EQUALS(Standards::C99, a->standards.c);
651651
ASSERT_EQUALS(Standards::CPP03, a->standards.cpp);
652652

653653
ASSERT_EQUALS("Obsolescent function 'b' called. It is recommended to use 'c', 'd' or 'e' instead.", b->message);
654-
ASSERT_EQUALS(static_cast<int>(Severity::performance), static_cast<int>(b->severity));
654+
ASSERT_EQUALS_ENUM(Severity::performance, b->severity);
655655
ASSERT_EQUALS(Standards::C89, b->standards.c);
656656
ASSERT_EQUALS(Standards::CPP11, b->standards.cpp);
657657
}
@@ -827,8 +827,8 @@ class TestLibrary : public TestFixture {
827827
Library library;
828828
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
829829

830-
Library::Container& A = library.containers["A"];
831-
Library::Container& B = library.containers["B"];
830+
const Library::Container& A = library.containers["A"];
831+
const Library::Container& B = library.containers["B"];
832832
const Library::Container& C = library.containers["C"];
833833

834834
ASSERT_EQUALS(A.type_templateArgNo, 1);

test/testsingleexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class TestSingleExecutorBase : public TestFixture {
276276
$.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_FILE));
277277
const std::string output_s = GET_REDIRECT_OUTPUT;
278278
// for each file: top5 results + overall + empty line
279-
ASSERT_EQUALS((5 + 1 + 1) * 2, cppcheck::count_all_of(output_s, '\n'));
279+
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
280280
}
281281

282282
void showtime_top5_summary() {

test/testthreadexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class TestThreadExecutorBase : public TestFixture {
281281
// for each file: top5 results + overall + empty line
282282
const std::string output_s = GET_REDIRECT_OUTPUT;
283283
// for each file: top5 results + overall + empty line
284-
ASSERT_EQUALS((5 + 1 + 1) * 2, cppcheck::count_all_of(output_s, '\n'));
284+
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
285285
}
286286

287287
void showtime_top5_summary() {

test/testtoken.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,42 +173,42 @@ class TestToken : public TestFixture {
173173
ASSERT_EQUALS(0, Token::multiCompare(&notfound, "one|two|", 0));
174174

175175
// Test for not found
176-
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&notfound, "one|two", 0)));
176+
ASSERT_EQUALS(-1, Token::multiCompare(&notfound, "one|two", 0));
177177
}
178178

179179
{
180180
TokensFrontBack tokensFrontBack(list);
181181
Token s(tokensFrontBack);
182182
s.str("s");
183-
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&s, "verybig|two", 0)));
183+
ASSERT_EQUALS(-1, Token::multiCompare(&s, "verybig|two", 0));
184184
}
185185

186186
{
187187
TokensFrontBack tokensFrontBack(list);
188188
Token ne(tokensFrontBack);
189189
ne.str("ne");
190-
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&ne, "one|two", 0)));
190+
ASSERT_EQUALS(-1, Token::multiCompare(&ne, "one|two", 0));
191191
}
192192

193193
{
194194
TokensFrontBack tokensFrontBack(list);
195195
Token a(tokensFrontBack);
196196
a.str("a");
197-
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&a, "abc|def", 0)));
197+
ASSERT_EQUALS(-1, Token::multiCompare(&a, "abc|def", 0));
198198
}
199199

200200
{
201201
TokensFrontBack tokensFrontBack(list);
202202
Token abcd(tokensFrontBack);
203203
abcd.str("abcd");
204-
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&abcd, "abc|def", 0)));
204+
ASSERT_EQUALS(-1, Token::multiCompare(&abcd, "abc|def", 0));
205205
}
206206

207207
{
208208
TokensFrontBack tokensFrontBack(list);
209209
Token def(tokensFrontBack);
210210
def.str("default");
211-
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&def, "abc|def", 0)));
211+
ASSERT_EQUALS(-1, Token::multiCompare(&def, "abc|def", 0));
212212
}
213213

214214
// %op%

test/testvalueflow.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,10 +1205,10 @@ class TestValueFlow : public TestFixture {
12051205

12061206
// string/char literals
12071207
CHECK("\"asdf\"", 5);
1208-
CHECK("L\"asdf\"", 5 * settings.platform.sizeof_wchar_t);
1208+
CHECK("L\"asdf\"", 5LL * settings.platform.sizeof_wchar_t);
12091209
CHECK("u8\"asdf\"", 5); // char8_t
1210-
CHECK("u\"asdf\"", 5 * 2); // char16_t
1211-
CHECK("U\"asdf\"", 5 * 4); // char32_t
1210+
CHECK("u\"asdf\"", 5LL * 2); // char16_t
1211+
CHECK("U\"asdf\"", 5LL * 4); // char32_t
12121212
CHECK("'a'", 1U);
12131213
CHECK("'ab'", settings.platform.sizeof_int);
12141214
CHECK("L'a'", settings.platform.sizeof_wchar_t);
@@ -1320,7 +1320,7 @@ class TestValueFlow : public TestFixture {
13201320
"}"; \
13211321
values = tokenValues(code,"( arrE )"); \
13221322
ASSERT_EQUALS(1U, values.size()); \
1323-
ASSERT_EQUALS(B * 2U, values.back().intvalue); \
1323+
ASSERT_EQUALS(B * 2ULL, values.back().intvalue); \
13241324
} while (false)
13251325

13261326
// enum array
@@ -1355,7 +1355,7 @@ class TestValueFlow : public TestFixture {
13551355
"}"; \
13561356
values = tokenValues(code,"( arrE )"); \
13571357
ASSERT_EQUALS(1U, values.size()); \
1358-
ASSERT_EQUALS(B * 2U, values.back().intvalue); \
1358+
ASSERT_EQUALS(B * 2ULL, values.back().intvalue); \
13591359
} while (false)
13601360

13611361
// enum array

0 commit comments

Comments
 (0)