From 3711bf85ae85c9398642ae276cbd90b5bfaa6688 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Sun, 28 Jun 2020 22:06:50 +0200 Subject: [PATCH] Help qtestlib with int -> qsizetype changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure we can handle qsizetype as an integer when appending test data. This is required for backwards compatibility with Qt 5, so people don't have to rewrite all their test cases. QCOMPARE can handle mixed types, tthe only method that requires manual changes now is QTEST(list.size(), "testrow_expecting_int"). Change-Id: I40723b239e0160cefc05745aa35a75de8599ac08 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Edward Welbourne Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot --- src/testlib/qtestdata.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/testlib/qtestdata.cpp b/src/testlib/qtestdata.cpp index 3a1c6d7e7b..4930954a9e 100644 --- a/src/testlib/qtestdata.cpp +++ b/src/testlib/qtestdata.cpp @@ -82,9 +82,24 @@ QTestData::~QTestData() void QTestData::append(int type, const void *data) { QTEST_ASSERT(d->dataCount < d->parent->elementCount()); - if (d->parent->elementTypeId(d->dataCount) != type) { + int expectedType = d->parent->elementTypeId(d->dataCount); + int dd = 0; + if constexpr (sizeof(qsizetype) == 8) { + // Compatibility with Qt 5. Passing a qsizetype to a test function expecting + // an int will work. This is required, as methods returning a qsizetype in Qt 6 + // used to return an int in Qt 5. + if (type == QMetaType::LongLong && expectedType == QMetaType::Int) { + qlonglong d = *static_cast(data); + if (d >= std::numeric_limits::min() && d <= std::numeric_limits::max()) { + dd = d; + data = ⅆ + type = QMetaType::Int; + } + } + } + if (expectedType != type) { qDebug("expected data of type '%s', got '%s' for element %d of data with tag '%s'", - QMetaType::typeName(d->parent->elementTypeId(d->dataCount)), + QMetaType::typeName(expectedType), QMetaType::typeName(type), d->dataCount, d->tag); QTEST_ASSERT(false);