From deb59a5d550fdc2a2a964fd853f7cb1945f14c6b Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Sat, 21 Oct 2023 14:09:36 +0200 Subject: [PATCH] Don't use Q_ASSERT in tests Prefer VERIFY_OR_THROW to ensure the checks are also done in release builds without assertions enabled. --- tests/integrationtests/tst_perfparser.cpp | 2 +- tests/modeltests/tst_models.cpp | 10 +++--- tests/testutils.h | 38 +++++++++++------------ 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/tests/integrationtests/tst_perfparser.cpp b/tests/integrationtests/tst_perfparser.cpp index 73f65fdf8..184bff7ed 100644 --- a/tests/integrationtests/tst_perfparser.cpp +++ b/tests/integrationtests/tst_perfparser.cpp @@ -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>& pattern) { return std::any_of(pattern.begin(), pattern.end(), [&symbol](const QPair& pattern) { return symbol.symbol.contains(pattern.first) && symbol.binary.contains(pattern.second); diff --git a/tests/modeltests/tst_models.cpp b/tests/modeltests/tst_models.cpp index 3a43ed4d1..30d8defbf 100644 --- a/tests/modeltests/tst_models.cpp +++ b/tests/modeltests/tst_models.cpp @@ -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; @@ -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}; diff --git a/tests/testutils.h b/tests/testutils.h index 0c7d60488..b9b548841 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -20,6 +20,24 @@ #include +#define VERIFY_OR_THROW(statement) \ + do { \ + if (!QTest::qVerify(static_cast(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(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 QString printCost(const Data& node, const Results& results) { @@ -75,7 +93,7 @@ inline QStringList printMap(const Data::CallerCalleeResults& results) list.reserve(results.entries.size()); QSet 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; @@ -153,24 +171,6 @@ QStringList printModel(const QAbstractItemModel* model) return ret; } -#define VERIFY_OR_THROW(statement) \ - do { \ - if (!QTest::qVerify(static_cast(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(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));