Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testrunner: stop comparing mismatching types in generic assertEquals() / added ASSERT_EQUALS_ENUM #6035

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions test/fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class TestFixture : public ErrorLogger {

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

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

template<typename T>
bool assertEqualsEnum(const char* const filename, const unsigned int linenr, const T& expected, const T& actual, const std::string& msg = emptyString) const {
if (std::is_unsigned<T>())
return assertEquals(filename, linenr, static_cast<std::uint64_t>(expected), static_cast<std::uint64_t>(actual), msg);
return assertEquals(filename, linenr, static_cast<std::int64_t>(expected), static_cast<std::int64_t>(actual), msg);
}

//Helper function to be called when an assertEquals assertion fails.
//Writes the appropriate failure message to errmsg and increments fails_counter
void assertEqualsFailed(const char* const filename, const unsigned int linenr, const std::string& expected, const std::string& actual, const std::string& msg) const;
Expand Down Expand Up @@ -271,6 +278,7 @@ class TestFixture : public ErrorLogger {
#define ASSERT_EQUALS_WITHOUT_LINENUMBERS( EXPECTED, ACTUAL ) assertEqualsWithoutLineNumbers(__FILE__, __LINE__, EXPECTED, ACTUAL)
#define ASSERT_EQUALS_DOUBLE( EXPECTED, ACTUAL, TOLERANCE ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE)
#define ASSERT_EQUALS_MSG( EXPECTED, ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
#define ASSERT_EQUALS_ENUM( EXPECTED, ACTUAL ) if (!assertEqualsEnum(__FILE__, __LINE__, (EXPECTED), (ACTUAL))) return
#define ASSERT_THROW( CMD, EXCEPTION ) do { try { CMD; assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&) {} catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
#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)
#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)
Expand Down
482 changes: 235 additions & 247 deletions test/testcmdlineparser.cpp

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions test/testerrorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ class TestErrorLogger : public TestFixture {
ASSERT(doc.Parse(xmldata, sizeof(xmldata)) == tinyxml2::XML_SUCCESS);
ErrorMessage msg(doc.FirstChildElement());
ASSERT_EQUALS("errorId", msg.id);
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg.severity));
ASSERT_EQUALS_ENUM(Severity::error, msg.severity);
ASSERT_EQUALS(123u, msg.cwe.id);
ASSERT_EQUALS(static_cast<int>(Certainty::inconclusive), static_cast<int>(msg.certainty));
ASSERT_EQUALS_ENUM(Certainty::inconclusive, msg.certainty);
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
ASSERT_EQUALS(456u, msg.hash);
Expand Down Expand Up @@ -329,9 +329,9 @@ class TestErrorLogger : public TestFixture {
ErrorMessage msg2;
ASSERT_NO_THROW(msg2.deserialize(msg_str));
ASSERT_EQUALS("errorId", msg2.id);
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg2.severity));
ASSERT_EQUALS_ENUM(Severity::error, msg2.severity);
ASSERT_EQUALS("test.cpp", msg2.file0);
ASSERT_EQUALS(static_cast<int>(Certainty::inconclusive), static_cast<int>(msg2.certainty));
ASSERT_EQUALS_ENUM(Certainty::inconclusive, msg2.certainty);
ASSERT_EQUALS("Programming error", msg2.shortMessage());
ASSERT_EQUALS("Programming error", msg2.verboseMessage());
}
Expand Down Expand Up @@ -417,7 +417,7 @@ class TestErrorLogger : public TestFixture {
ErrorMessage msg2;
ASSERT_NO_THROW(msg2.deserialize(msg_str));
ASSERT_EQUALS("errorId", msg2.id);
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg2.severity));
ASSERT_EQUALS_ENUM(Severity::error, msg2.severity);
ASSERT_EQUALS("1.c", msg2.file0);
ASSERT_EQUALS("Illegal character in \"foo\\001bar\"", msg2.shortMessage());
ASSERT_EQUALS("Illegal character in \"foo\\001bar\"", msg2.verboseMessage());
Expand Down
8 changes: 4 additions & 4 deletions test/testlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,12 @@ class TestLibrary : public TestFixture {
ASSERT(a && b);
if (a && b) {
ASSERT_EQUALS("Message", a->message);
ASSERT_EQUALS(static_cast<int>(Severity::style), static_cast<int>(a->severity));
ASSERT_EQUALS_ENUM(Severity::style, a->severity);
ASSERT_EQUALS(Standards::C99, a->standards.c);
ASSERT_EQUALS(Standards::CPP03, a->standards.cpp);

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

Library::Container& A = library.containers["A"];
Library::Container& B = library.containers["B"];
const Library::Container& A = library.containers["A"];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this false negative is already tracked in https://trac.cppcheck.net/ticket/12211.

const Library::Container& B = library.containers["B"];
const Library::Container& C = library.containers["C"];

ASSERT_EQUALS(A.type_templateArgNo, 1);
Expand Down
2 changes: 1 addition & 1 deletion test/testsingleexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class TestSingleExecutorBase : public TestFixture {
$.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_FILE));
const std::string output_s = GET_REDIRECT_OUTPUT;
// for each file: top5 results + overall + empty line
ASSERT_EQUALS((5 + 1 + 1) * 2, cppcheck::count_all_of(output_s, '\n'));
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
}

void showtime_top5_summary() {
Expand Down
2 changes: 1 addition & 1 deletion test/testthreadexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class TestThreadExecutorBase : public TestFixture {
// for each file: top5 results + overall + empty line
const std::string output_s = GET_REDIRECT_OUTPUT;
// for each file: top5 results + overall + empty line
ASSERT_EQUALS((5 + 1 + 1) * 2, cppcheck::count_all_of(output_s, '\n'));
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
}

void showtime_top5_summary() {
Expand Down
12 changes: 6 additions & 6 deletions test/testtoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,42 +173,42 @@ class TestToken : public TestFixture {
ASSERT_EQUALS(0, Token::multiCompare(&notfound, "one|two|", 0));

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

{
TokensFrontBack tokensFrontBack(list);
Token s(tokensFrontBack);
s.str("s");
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&s, "verybig|two", 0)));
ASSERT_EQUALS(-1, Token::multiCompare(&s, "verybig|two", 0));
}

{
TokensFrontBack tokensFrontBack(list);
Token ne(tokensFrontBack);
ne.str("ne");
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&ne, "one|two", 0)));
ASSERT_EQUALS(-1, Token::multiCompare(&ne, "one|two", 0));
}

{
TokensFrontBack tokensFrontBack(list);
Token a(tokensFrontBack);
a.str("a");
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&a, "abc|def", 0)));
ASSERT_EQUALS(-1, Token::multiCompare(&a, "abc|def", 0));
}

{
TokensFrontBack tokensFrontBack(list);
Token abcd(tokensFrontBack);
abcd.str("abcd");
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&abcd, "abc|def", 0)));
ASSERT_EQUALS(-1, Token::multiCompare(&abcd, "abc|def", 0));
}

{
TokensFrontBack tokensFrontBack(list);
Token def(tokensFrontBack);
def.str("default");
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare(&def, "abc|def", 0)));
ASSERT_EQUALS(-1, Token::multiCompare(&def, "abc|def", 0));
}

// %op%
Expand Down
10 changes: 5 additions & 5 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,10 +1205,10 @@ class TestValueFlow : public TestFixture {

// string/char literals
CHECK("\"asdf\"", 5);
CHECK("L\"asdf\"", 5 * settings.platform.sizeof_wchar_t);
CHECK("L\"asdf\"", 5LL * settings.platform.sizeof_wchar_t);
CHECK("u8\"asdf\"", 5); // char8_t
CHECK("u\"asdf\"", 5 * 2); // char16_t
CHECK("U\"asdf\"", 5 * 4); // char32_t
CHECK("u\"asdf\"", 5LL * 2); // char16_t
CHECK("U\"asdf\"", 5LL * 4); // char32_t
CHECK("'a'", 1U);
CHECK("'ab'", settings.platform.sizeof_int);
CHECK("L'a'", settings.platform.sizeof_wchar_t);
Expand Down Expand Up @@ -1320,7 +1320,7 @@ class TestValueFlow : public TestFixture {
"}"; \
values = tokenValues(code,"( arrE )"); \
ASSERT_EQUALS(1U, values.size()); \
ASSERT_EQUALS(B * 2U, values.back().intvalue); \
ASSERT_EQUALS(B * 2ULL, values.back().intvalue); \
} while (false)

// enum array
Expand Down Expand Up @@ -1355,7 +1355,7 @@ class TestValueFlow : public TestFixture {
"}"; \
values = tokenValues(code,"( arrE )"); \
ASSERT_EQUALS(1U, values.size()); \
ASSERT_EQUALS(B * 2U, values.back().intvalue); \
ASSERT_EQUALS(B * 2ULL, values.back().intvalue); \
} while (false)

// enum array
Expand Down
Loading