Skip to content

Commit

Permalink
Don't use Q_ASSERT in tests
Browse files Browse the repository at this point in the history
Prefer VERIFY_OR_THROW to ensure the checks are also done in
release builds without assertions enabled.
  • Loading branch information
milianw committed Oct 21, 2023
1 parent fc96bf0 commit deb59a5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion tests/integrationtests/tst_perfparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct ComparableSymbol

bool operator==(const ComparableSymbol& rhs) const
{
Q_ASSERT(isPattern != rhs.isPattern);
VERIFY_OR_THROW(isPattern != rhs.isPattern);
auto cmp = [](const Data::Symbol& symbol, const QVector<QPair<QString, QString>>& pattern) {
return std::any_of(pattern.begin(), pattern.end(), [&symbol](const QPair<QString, QString>& pattern) {
return symbol.symbol.contains(pattern.first) && symbol.binary.contains(pattern.second);
Expand Down
10 changes: 5 additions & 5 deletions tests/modeltests/tst_models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Data::BottomUpResults buildBottomUpTree(const QByteArray& stacks)
const auto& frame = *it;
const auto symbol = Data::Symbol {QString::fromUtf8(frame), {}};
auto node = parent->entryForSymbol(symbol, &maxId);
Q_ASSERT(!ids.contains(node->id) || ids[node->id] == symbol);
VERIFY_OR_THROW(!ids.contains(node->id) || ids[node->id] == symbol);
ids[node->id] = symbol;
ret.costs.increment(0, node->id);
parent = node;
Expand Down Expand Up @@ -417,17 +417,17 @@ private slots:
readelf.waitForFinished();

const auto output = readelf.readAllStandardOutput();
Q_ASSERT(!output.isEmpty());
QVERIFY(!output.isEmpty());

auto match = regex.match(QString::fromUtf8(output));

Q_ASSERT(match.hasMatch());
QVERIFY(match.hasMatch());

bool ok = false;
const quint64 address = match.captured(1).toInt(&ok, 16);
Q_ASSERT(ok);
QVERIFY(ok);
const quint64 size = match.captured(2).toInt(&ok, 10);
Q_ASSERT(ok);
QVERIFY(ok);

Data::Symbol symbol = {QStringLiteral("main"), address, size, QStringLiteral("cpp-recursion"), {}, binary};

Expand Down
38 changes: 19 additions & 19 deletions tests/testutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@

#include <algorithm>

#define VERIFY_OR_THROW(statement) \
do { \
if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__)) \
throw std::logic_error("verify failed: " #statement); \
} while (false)

#define VERIFY_OR_THROW2(statement, description) \
do { \
if (!QTest::qVerify(static_cast<bool>(statement), #statement, description, __FILE__, __LINE__)) \
throw std::logic_error(description); \
} while (false)

#define COMPARE_OR_THROW(actual, expected) \
do { \
if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__)) \
throw std::logic_error("compare failed: " #actual #expected); \
} while (false)

template<typename Data, typename Results>
QString printCost(const Data& node, const Results& results)
{
Expand Down Expand Up @@ -75,7 +93,7 @@ inline QStringList printMap(const Data::CallerCalleeResults& results)
list.reserve(results.entries.size());
QSet<quint32> ids;
for (auto it = results.entries.begin(), end = results.entries.end(); it != end; ++it) {
Q_ASSERT(!ids.contains(it->id));
VERIFY_OR_THROW(!ids.contains(it->id));
ids.insert(it->id);
list.push_back(it.key().symbol + QLatin1Char('=') + printCost(it.value(), results));
QStringList subList;
Expand Down Expand Up @@ -153,24 +171,6 @@ QStringList printModel(const QAbstractItemModel* model)
return ret;
}

#define VERIFY_OR_THROW(statement) \
do { \
if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__)) \
throw std::logic_error("verify failed: " #statement); \
} while (false)

#define VERIFY_OR_THROW2(statement, description) \
do { \
if (!QTest::qVerify(static_cast<bool>(statement), #statement, description, __FILE__, __LINE__)) \
throw std::logic_error(description); \
} while (false)

#define COMPARE_OR_THROW(actual, expected) \
do { \
if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__)) \
throw std::logic_error("compare failed: " #actual #expected); \
} while (false)

inline QString findExe(const QString& name)
{
QFileInfo exe(QCoreApplication::applicationDirPath() + QLatin1String("/../tests/test-clients/%1/%1").arg(name));
Expand Down

0 comments on commit deb59a5

Please sign in to comment.