diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-10 11:41:33 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-21 22:01:20 +0200 |
commit | d25589e0529732996e405aaff8d6c46b012e1601 (patch) | |
tree | 29b596d0c2ca0e36aa9aea65f426ed393c660e78 /src/testlib/qtest.h | |
parent | b50daef9771d8829fc7f808898cbe051a5464b79 (diff) |
QTestlib: Enable comparing QList against initializer lists/arrays
It is unnecessary to create a QList container just for comparison.
Split out helpers for comparing sequence sizes and sequences from
qCompare(QList) and add a template for an array with a non-type template
parameter for the size.
One can then write something like:
const int expected[] = {10, 12,...};
QCOMPARE(QFontDatabase.pointSizes(...), expected)
Unfortunately, any commas in such an array will be misread by macro expansion
as macro argument separators, so any expected array with more than one entry
needs an extra macro expanding __VA_ARGS__.
Change-Id: Ie7c8dc20bf669bbb25f6d7f8562455f8d03968c8
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/testlib/qtest.h')
-rw-r--r-- | src/testlib/qtest.h | 69 |
1 files changed, 54 insertions, 15 deletions
diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index 9455a66fdd..0fbef0b7c2 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -64,6 +64,7 @@ #include <QtCore/qsize.h> #include <QtCore/qrect.h> +#include <initializer_list> #include <memory> QT_BEGIN_NAMESPACE @@ -276,27 +277,36 @@ inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *act return qCompare(QString(t1), t2, actual, expected, file, line); } -template <typename T> -inline bool qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const char *expected, - const char *file, int line) +namespace Internal { + +// Compare sequences of equal size +template <typename ActualIterator, typename ExpectedIterator> +bool compareSequence(ActualIterator actualIt, ActualIterator actualEnd, + ExpectedIterator expectedBegin, ExpectedIterator expectedEnd, + const char *actual, const char *expected, + const char *file, int line) { char msg[1024]; msg[0] = '\0'; - bool isOk = true; - const int actualSize = t1.count(); - const int expectedSize = t2.count(); - if (actualSize != expectedSize) { + + const qsizetype actualSize = actualEnd - actualIt; + const qsizetype expectedSize = expectedEnd - expectedBegin; + bool isOk = actualSize == expectedSize; + + if (!isOk) { qsnprintf(msg, sizeof(msg), "Compared lists have different sizes.\n" - " Actual (%s) size: %d\n" - " Expected (%s) size: %d", actual, actualSize, expected, expectedSize); - isOk = false; + " Actual (%s) size: %zd\n" + " Expected (%s) size: %zd", actual, actualSize, + expected, expectedSize); } - for (int i = 0; isOk && i < actualSize; ++i) { - if (!(t1.at(i) == t2.at(i))) { - char *val1 = toString(t1.at(i)); - char *val2 = toString(t2.at(i)); - qsnprintf(msg, sizeof(msg), "Compared lists differ at index %d.\n" + for (auto expectedIt = expectedBegin; isOk && expectedIt < expectedEnd; ++actualIt, ++expectedIt) { + if (!(*actualIt == *expectedIt)) { + const qsizetype i = qsizetype(expectedIt - expectedBegin); + char *val1 = QTest::toString(*actualIt); + char *val2 = QTest::toString(*expectedIt); + + qsnprintf(msg, sizeof(msg), "Compared lists differ at index %zd.\n" " Actual (%s): %s\n" " Expected (%s): %s", i, actual, val1 ? val1 : "<null>", expected, val2 ? val2 : "<null>"); @@ -309,6 +319,35 @@ inline bool qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, return compare_helper(isOk, msg, nullptr, nullptr, actual, expected, file, line); } +} // namespace Internal + +template <typename T> +inline bool qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const char *expected, + const char *file, int line) +{ + return Internal::compareSequence(t1.cbegin(), t1.cend(), t2.cbegin(), t2.cend(), + actual, expected, file, line); +} + +template <typename T, int N> +bool qCompare(QList<T> const &t1, std::initializer_list<T> t2, + const char *actual, const char *expected, + const char *file, int line) +{ + return Internal::compareSequence(t1.cbegin(), t1.cend(), t2.cbegin(), t2.cend(), + actual, expected, file, line); +} + +// Compare QList against array +template <typename T, int N> +bool qCompare(QList<T> const &t1, const T (& t2)[N], + const char *actual, const char *expected, + const char *file, int line) +{ + return Internal::compareSequence(t1.cbegin(), t1.cend(), t2, t2 + N, + actual, expected, file, line); +} + template <> inline bool qCompare(QStringList const &t1, QStringList const &t2, const char *actual, const char *expected, const char *file, int line) |