diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2022-06-03 09:27:27 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-06-03 20:37:51 +0000 |
commit | 0681a2dd5a8095baddb5905fb21a58ce19b958c5 (patch) | |
tree | e9a8a67e50f817e3f48cebd5294fa4a96bee8945 /src/testlib/qtest_gui.h | |
parent | 1e36eedb7f6d312f439aa3d1b1529e7fd672d81a (diff) |
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>
Diffstat (limited to 'src/testlib/qtest_gui.h')
-rw-r--r-- | src/testlib/qtest_gui.h | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/testlib/qtest_gui.h b/src/testlib/qtest_gui.h index 68c13ee57b..26cf17c6ef 100644 --- a/src/testlib/qtest_gui.h +++ b/src/testlib/qtest_gui.h @@ -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); } |