QTestLib: rework QTest::compare_helper()

[ChangeLog][QTestLib] QCOMPARE now evaluates toString() on its
arguments lazily, speeding up the general case where the comparison
doesn't fail. This is true for the QCOMPARE functionality provided
by Qt. If you specialized qCompare() for your own types, then you
need to change its implementation in line with Qt's own qCompare()
specializations in order to enable this feature.

[ChangeLog][QTestLib] QCOMPARE calls with nullptr argument(s) will
now print the actual and expected values upon failure.

Previously it was not like that because of the compareHelper()
overload in qtestresult.cpp that treated the presence of
nullptr-arguments as a reason to ignore formatFailMessage() call.
New implementation does not have this check, and correctly
executes formatFailMessage() for all arguments.

Note that the qCompare() overloads that call QTestResult::compare()
internally were not affected by this patch, because they already
defer toString() invocation until the comparison fails.

Some numbers, collected against shared release developer build.
I checked how this change affects the test execution. The idea was
to pick some tests for types that do not have a specific
QTestResult::compare overload, so I picked a couple of QByteArray
tests.
The comparison is done by running a test 10 times and taking the
average execution duration, as reported in the log.

tst_qbytearrayapisymmetry:
 Before: 15.6 ms
 After:  14.2 ms

tst_qbytearray:
 Before: 41 ms
 After:  36 ms

The benefit is around 9% and 12% respectively.

Fixes: QTBUG-98874
Change-Id: I7d59ddc760168b15974e7720930f629fb34efa13
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Ivan Solovev 2022-06-03 09:27:27 +02:00 committed by Marc Mutz
parent 1e36eedb7f
commit 0681a2dd5a
14 changed files with 187 additions and 35 deletions

View File

@ -421,7 +421,7 @@ bool _q_compareSequence(ActualIterator actualIt, ActualIterator actualEnd,
delete [] val2;
}
}
return compare_helper(isOk, msg, nullptr, nullptr, actual, expected, file, line);
return compare_helper(isOk, msg, actual, expected, file, line);
}
namespace Internal {

View File

@ -122,17 +122,17 @@ inline bool qCompare(QImage const &t1, QImage const &t2,
qsnprintf(msg, 1024, "Compared QImages differ.\n"
" Actual (%s).isNull(): %d\n"
" Expected (%s).isNull(): %d", actual, t1Null, expected, t2Null);
return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
return compare_helper(false, msg, actual, expected, file, line);
}
if (t1Null && t2Null)
return compare_helper(true, nullptr, nullptr, nullptr, actual, expected, file, line);
return compare_helper(true, nullptr, actual, expected, file, line);
if (!qFuzzyCompare(t1.devicePixelRatio(), t2.devicePixelRatio())) {
qsnprintf(msg, 1024, "Compared QImages differ in device pixel ratio.\n"
" Actual (%s): %g\n"
" Expected (%s): %g",
actual, t1.devicePixelRatio(),
expected, t2.devicePixelRatio());
return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
return compare_helper(false, msg, actual, expected, file, line);
}
if (t1.width() != t2.width() || t1.height() != t2.height()) {
qsnprintf(msg, 1024, "Compared QImages differ in size.\n"
@ -140,17 +140,17 @@ inline bool qCompare(QImage const &t1, QImage const &t2,
" Expected (%s): %dx%d",
actual, t1.width(), t1.height(),
expected, t2.width(), t2.height());
return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
return compare_helper(false, msg, actual, expected, file, line);
}
if (t1.format() != t2.format()) {
qsnprintf(msg, 1024, "Compared QImages differ in format.\n"
" Actual (%s): %d\n"
" Expected (%s): %d",
actual, t1.format(), expected, t2.format());
return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
return compare_helper(false, msg, actual, expected, file, line);
}
return compare_helper(t1 == t2, "Compared values are not the same",
nullptr, nullptr, actual, expected, file, line);
actual, expected, file, line);
}
inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected,
@ -164,17 +164,17 @@ inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, c
qsnprintf(msg, 1024, "Compared QPixmaps differ.\n"
" Actual (%s).isNull(): %d\n"
" Expected (%s).isNull(): %d", actual, t1Null, expected, t2Null);
return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
return compare_helper(false, msg, actual, expected, file, line);
}
if (t1Null && t2Null)
return compare_helper(true, nullptr, nullptr, nullptr, actual, expected, file, line);
return compare_helper(true, nullptr, actual, expected, file, line);
if (!qFuzzyCompare(t1.devicePixelRatio(), t2.devicePixelRatio())) {
qsnprintf(msg, 1024, "Compared QPixmaps differ in device pixel ratio.\n"
" Actual (%s): %g\n"
" Expected (%s): %g",
actual, t1.devicePixelRatio(),
expected, t2.devicePixelRatio());
return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
return compare_helper(false, msg, actual, expected, file, line);
}
if (t1.width() != t2.width() || t1.height() != t2.height()) {
qsnprintf(msg, 1024, "Compared QPixmaps differ in size.\n"
@ -182,7 +182,7 @@ inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, c
" Expected (%s): %dx%d",
actual, t1.width(), t1.height(),
expected, t2.width(), t2.height());
return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
return compare_helper(false, msg, actual, expected, file, line);
}
return qCompare(t1.toImage(), t2.toImage(), actual, expected, file, line);
}

View File

@ -2904,7 +2904,9 @@ void QTest::setMainSourcePath(const char *file, const char *builddir)
QTest::mainSourcePath = fi.absolutePath();
}
#if QT_DEPRECATED_SINCE(6, 4)
/*! \internal
\deprecated [6.4]
This function is called by various specializations of QTest::qCompare
to decide whether to report a failure and to produce verbose test output.
@ -2912,15 +2914,61 @@ void QTest::setMainSourcePath(const char *file, const char *builddir)
will be output if the compare fails. If the compare succeeds, failureMsg
will not be output.
If the caller has already passed a failure message showing the compared
values, or if those values cannot be stringified, val1 and val2 can be null.
Using this function is not optimal, because it requires the string
representations of \a actualVal and \a expectedVal to be pre-calculated,
even though they will be used only if the comparison fails. Prefer using the
\l compare_helper() overload that takes qxp::function_ref() for such cases.
If the caller creates a custom failure message showing the compared values,
or if those values cannot be stringified, use the overload of the function
that takes no \a actualVal and \a expecetedVal parameters.
*/
bool QTest::compare_helper(bool success, const char *failureMsg,
char *val1, char *val2,
char *actualVal, char *expectedVal,
const char *actual, const char *expected,
const char *file, int line)
{
return QTestResult::compare(success, failureMsg, val1, val2, actual, expected, file, line);
return QTestResult::compare(success, failureMsg, actualVal, expectedVal,
actual, expected, file, line);
}
#endif // QT_DEPRECATED_SINCE(6, 4)
/*! \internal
This function is called by various specializations of QTest::qCompare
to decide whether to report a failure and to produce verbose test output.
The \a failureMsg parameter can be \c {nullptr}, in which case a default
message will be output if the compare fails. If the comparison succeeds,
\a failureMsg will not be output.
This overload of the function uses qxp::function_ref to defer conversion of
\a actualVal and \a expectedVal to strings until that is really needed
(when the comparison fails). This speeds up test case execution on success.
*/
bool QTest::compare_helper(bool success, const char *failureMsg,
qxp::function_ref<const char *()> actualVal,
qxp::function_ref<const char *()> expectedVal,
const char *actual, const char *expected,
const char *file, int line)
{
return QTestResult::reportResult(success, actualVal, expectedVal, actual, expected,
QTest::ComparisonOperation::CustomCompare,
file, line, failureMsg);
}
/*! \internal
This function is called by various specializations of QTest::qCompare
to decide whether to report a failure and to produce verbose test output.
This overload should be used when there is no string representation of
actual and expected values, so only the \a failureMsg is shown when the
comparison fails. Because of that, \a failureMsg can't be \c {nullptr}.
If the comparison succeeds, \a failureMsg will not be output.
*/
bool QTest::compare_helper(bool success, const char *failureMsg, const char *actual,
const char *expected, const char *file, int line)
{
return QTestResult::compare(success, failureMsg, actual, expected, file, line);
}
template <typename T>
@ -2950,7 +2998,8 @@ bool QTest::qCompare(qfloat16 const &t1, qfloat16 const &t2, const char *actual,
{
return compare_helper(floatingCompare(t1, t2),
"Compared qfloat16s are not the same (fuzzy compare)",
toString(t1), toString(t2), actual, expected, file, line);
[&t1] { return toString(t1); }, [&t2] { return toString(t2); },
actual, expected, file, line);
}
/*! \fn bool QTest::qCompare(const float &t1, const float &t2, const char *actual, const char *expected, const char *file, int line)
@ -3272,7 +3321,8 @@ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *ac
const char *expected, const char *file, int line)
{
return compare_helper(qstrcmp(t1, t2) == 0, "Compared strings are not the same",
toString(t1), toString(t2), actual, expected, file, line);
[t1] { return toString(t1); }, [t2] { return toString(t2); },
actual, expected, file, line);
}
/*!

View File

@ -405,10 +405,26 @@ namespace QTest
Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
// ### TODO: remove QTestResult::compare() overload that takes char * values
// when this overload is removed.
#if QT_DEPRECATED_SINCE(6, 4)
QT_DEPRECATED_VERSION_X_6_4("use an overload that takes function_ref as parameters, "
"or an overload that takes only failure message, if you "
"do not need to stringify the values")
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
char *val1, char *val2,
char *actualVal, char *expectedVal,
const char *actual, const char *expected,
const char *file, int line);
#endif // QT_DEPRECATED_SINCE(6, 4)
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
qxp::function_ref<const char*()> actualVal,
qxp::function_ref<const char*()> expectedVal,
const char *actual, const char *expected,
const char *file, int line);
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
const char *actual, const char *expected,
const char *file, int line);
Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
template <typename T>
@ -484,42 +500,48 @@ namespace QTest
const char *expected, const char *file, int line)
{
return compare_helper(t1 == t2, "Compared pointers are not the same",
toString(t1), toString(t2), actual, expected, file, line);
[t1] { return toString(t1); }, [t2] { return toString(t2); },
actual, expected, file, line);
}
inline bool compare_ptr_helper(const volatile QObject *t1, const volatile QObject *t2, const char *actual,
const char *expected, const char *file, int line)
{
return compare_helper(t1 == t2, "Compared QObject pointers are not the same",
toString(t1), toString(t2), actual, expected, file, line);
[t1] { return toString(t1); }, [t2] { return toString(t2); },
actual, expected, file, line);
}
inline bool compare_ptr_helper(const volatile QObject *t1, std::nullptr_t, const char *actual,
const char *expected, const char *file, int line)
{
return compare_helper(t1 == nullptr, "Compared QObject pointers are not the same",
toString(t1), toString(nullptr), actual, expected, file, line);
[t1] { return toString(t1); }, [] { return toString(nullptr); },
actual, expected, file, line);
}
inline bool compare_ptr_helper(std::nullptr_t, const volatile QObject *t2, const char *actual,
const char *expected, const char *file, int line)
{
return compare_helper(nullptr == t2, "Compared QObject pointers are not the same",
toString(nullptr), toString(t2), actual, expected, file, line);
[] { return toString(nullptr); }, [t2] { return toString(t2); },
actual, expected, file, line);
}
inline bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual,
const char *expected, const char *file, int line)
{
return compare_helper(t1 == nullptr, "Compared pointers are not the same",
toString(t1), toString(nullptr), actual, expected, file, line);
[t1] { return toString(t1); }, [] { return toString(nullptr); },
actual, expected, file, line);
}
inline bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual,
const char *expected, const char *file, int line)
{
return compare_helper(nullptr == t2, "Compared pointers are not the same",
toString(nullptr), toString(t2), actual, expected, file, line);
[] { return toString(nullptr); }, [t2] { return toString(t2); },
actual, expected, file, line);
}
Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
@ -551,7 +573,8 @@ namespace QTest
const char *file, int line)
{
return compare_helper(t1 == t2, "Compared values are not the same",
toString(t1), toString(t2), actual, expected, file, line);
[&t1] { return toString(t1); }, [&t2] { return toString(t2); },
actual, expected, file, line);
}
inline bool qCompare(double const &t1, float const &t2, const char *actual,

View File

@ -402,6 +402,38 @@ static bool compareHelper(bool success, const char *failureMsg,
return checkStatement(success, msg, file, line);
}
// A simplified version of compareHelper that does not use string
// representations of the values, and prints only failureMsg when the
// comparison fails.
static bool compareHelper(bool success, const char *failureMsg,
const char *actual, const char *expected,
const char *file, int line)
{
const size_t maxMsgLen = 1024;
char msg[maxMsgLen];
msg[0] = '\0';
QTEST_ASSERT(expected);
QTEST_ASSERT(actual);
// failureMsg can be null, if we do not use it
QTEST_ASSERT(success || failureMsg);
if (QTestLog::verboseLevel() >= 2) {
qsnprintf(msg, maxMsgLen, "QCOMPARE(%s, %s)", actual, expected);
QTestLog::info(msg, file, line);
}
if (success) {
if (QTest::expectFailMode) {
qsnprintf(msg, maxMsgLen, "QCOMPARE(%s, %s) returned TRUE unexpectedly.",
actual, expected);
}
return checkStatement(success, msg, file, line);
}
return checkStatement(success, failureMsg, file, line);
}
bool QTestResult::compare(bool success, const char *failureMsg,
char *val1, char *val2,
const char *actual, const char *expected,
@ -486,6 +518,15 @@ bool QTestResult::compare(bool success, const char *failureMsg,
return compareHelper(success, failureMsg, val1, val2, actual, expected, file, line);
}
// Simplified version of compare() that does not take the values, because they
// can't be converted to strings (or the user didn't want to do that).
bool QTestResult::compare(bool success, const char *failureMsg,
const char *actual, const char *expeceted,
const char *file, int line)
{
return compareHelper(success, failureMsg, actual, expeceted, file, line);
}
void QTestResult::addFailure(const char *message, const char *file, int line)
{
clearExpectFail();
@ -584,7 +625,8 @@ static const char *failureMessageForOp(QTest::ComparisonOperation op)
bool QTestResult::reportResult(bool success, qxp::function_ref<const char *()> lhs,
qxp::function_ref<const char *()> rhs,
const char *lhsExpr, const char *rhsExpr,
QTest::ComparisonOperation op, const char *file, int line)
QTest::ComparisonOperation op, const char *file, int line,
const char *failureMessage)
{
const size_t maxMsgLen = 1024;
char msg[maxMsgLen] = {'\0'};
@ -608,7 +650,10 @@ bool QTestResult::reportResult(bool success, qxp::function_ref<const char *()> l
const std::unique_ptr<const char[]> lhsPtr{ lhs() };
const std::unique_ptr<const char[]> rhsPtr{ rhs() };
formatFailMessage(msg, maxMsgLen, failureMessageForOp(op), lhsPtr.get(), rhsPtr.get(),
if (!failureMessage)
failureMessage = failureMessageForOp(op);
formatFailMessage(msg, maxMsgLen, failureMessage, lhsPtr.get(), rhsPtr.get(),
lhsExpr, rhsExpr, op);
return checkStatement(success, msg, file, line);

View File

@ -42,6 +42,9 @@ public:
static void setBlacklistCurrentTest(bool b);
static void addFailure(const char *message, const char *file = nullptr, int line = 0);
// ### TODO: Remove this overload when deprecated QTest::compare_overload
// is removed. Can't declare it deprecated, because it will unconditionally
// provide warnings.
static bool compare(bool success, const char *failureMsg,
char *val1, char *val2,
const char *actual, const char *expected,
@ -80,6 +83,9 @@ public:
QStringView val1, const QLatin1StringView &val2,
const char *actual, const char *expected,
const char *file, int line);
static bool compare(bool success, const char *failureMsg,
const char *actual, const char *expeceted,
const char *file, int line);
static void setCurrentGlobalTestData(QTestData *data);
static void setCurrentTestData(QTestData *data);
static void setCurrentTestFunction(const char *func);
@ -99,7 +105,8 @@ public:
static bool reportResult(bool success, qxp::function_ref<const char *()> lhs,
qxp::function_ref<const char *()> rhs,
const char *lhsExpr, const char *rhsExpr,
QTest::ComparisonOperation op, const char *file, int line);
QTest::ComparisonOperation op, const char *file, int line,
const char *failureMessage = nullptr);
private:
Q_DISABLE_COPY(QTestResult)

View File

@ -69,7 +69,8 @@ static bool compare(const QueryItems &actual, const QueryItems &expected,
const char *actualStr, const char *expectedStr, const char *file, int line)
{
return QTest::compare_helper(actual == expected, "Compared values are not the same",
qstrdup(prettyList(actual)), qstrdup(prettyList(expected).data()),
[&actual] { return qstrdup(prettyList(actual).constData()); },
[&expected] { return qstrdup(prettyList(expected).constData()); },
actualStr, expectedStr, file, line);
}

View File

@ -7,7 +7,10 @@
</properties>
<testcase name="initTestCase" classname="tst_TestLib" time="@TEST_DURATION@"/>
<testcase name="basics" classname="tst_TestLib" time="@TEST_DURATION@">
<failure type="fail" message="Compared QObject pointers are not the same"/>
<failure type="fail" message="Compared QObject pointers are not the same">
<![CDATA[ Actual (QTest::testObject()): tst_TestLib/"TestObject"
Expected (nullptr) : <null>]]>
</failure>
</testcase>
<testcase name="delays" classname="tst_TestLib" time="@TEST_DURATION@"/>
<testcase name="reals(zero)" classname="tst_TestLib" time="@TEST_DURATION@"/>

View File

@ -9,7 +9,9 @@
</TestFunction>
<TestFunction name="basics">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/testlib/tst_testlib.cpp" line="0">
<Description><![CDATA[Compared QObject pointers are not the same]]></Description>
<Description><![CDATA[Compared QObject pointers are not the same
Actual (QTest::testObject()): tst_TestLib/"TestObject"
Expected (nullptr) : <null>]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>

View File

@ -3,7 +3,12 @@ TAP version 13
ok 1 - initTestCase()
not ok 2 - basics()
---
# Compared QObject pointers are not the same
type: QCOMPARE
message: Compared QObject pointers are not the same
wanted: <null> (nullptr)
found: tst_TestLib/"TestObject" (QTest::testObject())
expected: <null> (nullptr)
actual: tst_TestLib/"TestObject" (QTest::testObject())
at: tst_TestLib::basics() (qtbase/tests/auto/testlib/selftests/testlib/tst_testlib.cpp:0)
file: qtbase/tests/auto/testlib/selftests/testlib/tst_testlib.cpp
line: 0

View File

@ -2,7 +2,7 @@
##teamcity[testStarted name='initTestCase()' flowId='tst_TestLib']
##teamcity[testFinished name='initTestCase()' flowId='tst_TestLib']
##teamcity[testStarted name='basics()' flowId='tst_TestLib']
##teamcity[testFailed name='basics()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/testlib/tst_testlib.cpp(0)|]' details='Compared QObject pointers are not the same' flowId='tst_TestLib']
##teamcity[testFailed name='basics()' message='Failure! |[Loc: qtbase/tests/auto/testlib/selftests/testlib/tst_testlib.cpp(0)|]' details='Compared QObject pointers are not the same|n Actual (QTest::testObject()): tst_TestLib/"TestObject"|n Expected (nullptr) : <null>' flowId='tst_TestLib']
##teamcity[testFinished name='basics()' flowId='tst_TestLib']
##teamcity[testStarted name='delays()' flowId='tst_TestLib']
##teamcity[testFinished name='delays()' flowId='tst_TestLib']

View File

@ -2,6 +2,8 @@
Config: Using QtTest library
PASS : tst_TestLib::initTestCase()
FAIL! : tst_TestLib::basics() Compared QObject pointers are not the same
Actual (QTest::testObject()): tst_TestLib/"TestObject"
Expected (nullptr) : <null>
Loc: [qtbase/tests/auto/testlib/selftests/testlib/tst_testlib.cpp(0)]
PASS : tst_TestLib::delays()
PASS : tst_TestLib::reals(zero)

View File

@ -11,7 +11,9 @@
</TestFunction>
<TestFunction name="basics">
<Incident type="fail" file="qtbase/tests/auto/testlib/selftests/testlib/tst_testlib.cpp" line="0">
<Description><![CDATA[Compared QObject pointers are not the same]]></Description>
<Description><![CDATA[Compared QObject pointers are not the same
Actual (QTest::testObject()): tst_TestLib/"TestObject"
Expected (nullptr) : <null>]]></Description>
</Incident>
<Duration msecs="0"/>
</TestFunction>

View File

@ -14,7 +14,11 @@
class tst_TestLib : public QObject
{
Q_OBJECT
Q_OBJECT
public:
tst_TestLib();
private slots:
void basics() const;
void delays() const;
@ -22,6 +26,14 @@ private slots:
void reals() const;
};
tst_TestLib::tst_TestLib()
{
// Set object name, so that it's printed out when some comparison fails.
// Othewise object address will be printed, which will not allow
// tst_sefltest to compare the output with expected.
setObjectName("TestObject");
}
void tst_TestLib::basics() const
{
QVERIFY(QByteArray(QTest::currentAppName()).contains("testlib"));