diff options
Diffstat (limited to 'tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp')
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 128 |
1 files changed, 79 insertions, 49 deletions
diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index c91bb21399..32c2154da6 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -1,33 +1,28 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com> -** Copyright (C) 2015 Intel Corporation. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com> +** Copyright (C) 2016 Intel Corporation. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -57,6 +52,7 @@ #include <QBuffer> #include "qnumeric.h" +#include <private/qlocale_p.h> #include "tst_qvariant_common.h" class CustomNonQObject; @@ -352,13 +348,13 @@ void tst_QVariant::constructor_invalid() QTest::ignoreMessage(QtWarningMsg, QRegularExpression("^Trying to construct an instance of an invalid type, type id:")); QVariant variant(static_cast<QVariant::Type>(typeId)); QVERIFY(!variant.isValid()); - QVERIFY(variant.userType() == QMetaType::UnknownType); + QCOMPARE(variant.userType(), int(QMetaType::UnknownType)); } { QTest::ignoreMessage(QtWarningMsg, QRegularExpression("^Trying to construct an instance of an invalid type, type id:")); QVariant variant(typeId, /* copy */ 0); QVERIFY(!variant.isValid()); - QVERIFY(variant.userType() == QMetaType::UnknownType); + QCOMPARE(variant.userType(), int(QMetaType::UnknownType)); } } @@ -902,7 +898,7 @@ void tst_QVariant::toFloat() bool ok; float d = value.toFloat(&ok); QCOMPARE(d, result); - QVERIFY(ok == valueOK); + QCOMPARE(ok, valueOK); } void tst_QVariant::toLongLong_data() @@ -1029,7 +1025,11 @@ void tst_QVariant::toByteArray_data() QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" ); QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" ); QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" ); - QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456001" ); + + // Conversion from float to double adds bits of which the double-to-string converter doesn't + // know they're insignificant + QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.45600128173828" ); + QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" ); QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" ); } @@ -1055,7 +1055,11 @@ void tst_QVariant::toString_data() QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" ); QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" ); QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" ); - QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456001" ); + + // Conversion from float to double adds bits of which the double-to-string converter doesn't + // know they're insignificant + QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.45600128173828" ); + QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" ); QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" ); QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" ); @@ -1324,9 +1328,9 @@ void tst_QVariant::writeToReadFromDataStream() // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i) float readFloat = qvariant_cast<float>(readVariant); float writtenFloat = qvariant_cast<float>(writeVariant); - QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat)); + QCOMPARE(qIsNaN(readFloat), qIsNaN(writtenFloat)); if (!qIsNaN(readFloat)) - QVERIFY(readFloat == writtenFloat); + QCOMPARE(readFloat, writtenFloat); } break; } @@ -1412,12 +1416,28 @@ void tst_QVariant::operator_eq_eq_data() QVariant mUIntQString(QString("42")); QVariant mDouble(42.11); +#ifdef QT_NO_DOUBLECONVERSION + // Without libdouble-conversion we don't get the shortest possible representation. QVariant mDoubleString(QByteArray("42.109999999999999")); - QVariant mDoubleQString(QString("42.109999999999999")); + QVariant mDoubleQString(QByteArray("42.109999999999999")); +#else + // You cannot fool the double-to-string conversion into producing insignificant digits with + // libdouble-conversion. You can, of course, add insignificant digits to the string and fool + // the double-to-double comparison after converting the string to a double. + QVariant mDoubleString(QByteArray("42.11")); + QVariant mDoubleQString(QString("42.11")); +#endif + // Float-to-double conversion produces insignificant extra bits. QVariant mFloat(42.11f); - QVariant mFloatString(QByteArray("42.1100006")); - QVariant mFloatQString(QString("42.1100006")); +#ifdef QT_NO_DOUBLECONVERSION + // The trailing '2' is not significant, but snprintf doesn't know this. + QVariant mFloatString(QByteArray("42.110000610351562")); + QVariant mFloatQString(QString("42.110000610351562")); +#else + QVariant mFloatString(QByteArray("42.11000061035156")); + QVariant mFloatQString(QString("42.11000061035156")); +#endif QVariant mLongLong((qlonglong)-42); QVariant mLongLongString(QByteArray("-42")); @@ -2123,7 +2143,7 @@ void tst_QVariant::userType() QVERIFY(!userVar.canConvert(QVariant::String)); QVariant userVar2(userVar); - QVERIFY(userVar == userVar2); + QCOMPARE(userVar, userVar2); userVar2.setValue(data2); QVERIFY(userVar != userVar2); @@ -2137,7 +2157,7 @@ void tst_QVariant::userType() userVar3.setValue(data2); userVar3 = userVar2; - QVERIFY(userVar2 == userVar3); + QCOMPARE(userVar2, userVar3); } // At this point all QVariants got destroyed but we have 2 MyType instances. QCOMPARE(instanceCount, 2); @@ -2152,7 +2172,7 @@ void tst_QVariant::userType() QVERIFY(!userVar.canConvert(QVariant::String)); QVariant userVar2(userVar); - QVERIFY(userVar == userVar2); + QCOMPARE(userVar, userVar2); userVar2.setValue(&data2); QVERIFY(userVar != userVar2); @@ -2166,10 +2186,10 @@ void tst_QVariant::userType() /* This check is correct now. userVar2 contains a pointer to data2 and so * does userVar3. */ - QVERIFY(userVar2 == userVar3); + QCOMPARE(userVar2, userVar3); userVar3 = userVar2; - QVERIFY(userVar2 == userVar3); + QCOMPARE(userVar2, userVar3); } QCOMPARE(instanceCount, 2); @@ -2454,7 +2474,7 @@ void tst_QVariant::saveLoadCustomTypes() qRegisterMetaTypeStreamOperators<Blah>("Blah"); QCOMPARE(v.userType(), tp); - QVERIFY(v.type() == QVariant::UserType); + QCOMPARE(v.type(), QVariant::UserType); { QDataStream stream(&data, QIODevice::WriteOnly); stream << v; @@ -2642,7 +2662,7 @@ void tst_QVariant::qvariant_cast_QObject_derived() { CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this); QVariant data = QVariant::fromValue(object); - QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>()); + QCOMPARE(data.userType(), qMetaTypeId<CustomQObjectDerivedNoMetaType*>()); QCOMPARE(data.value<QObject *>(), object); QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object); QCOMPARE(data.value<CustomQObject *>(), object); @@ -2651,7 +2671,7 @@ void tst_QVariant::qvariant_cast_QObject_derived() CustomQObjectDerived *object = new CustomQObjectDerived(this); QVariant data = QVariant::fromValue(object); - QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>()); + QCOMPARE(data.userType(), qMetaTypeId<CustomQObjectDerived*>()); QCOMPARE(data.value<QObject *>(), object); QCOMPARE(data.value<CustomQObjectDerived *>(), object); @@ -2918,10 +2938,10 @@ void tst_QVariant::voidStar() const QVariant v1, v2; v1 = QVariant::fromValue(p1); v2 = v1; - QVERIFY(v1 == v2); + QCOMPARE(v1, v2); v2 = QVariant::fromValue(p2); - QVERIFY(v1 == v2); + QCOMPARE(v1, v2); p2 = 0; v2 = QVariant::fromValue(p2); @@ -2938,10 +2958,10 @@ void tst_QVariant::dataStar() const QCOMPARE(qvariant_cast<Data*>(v1), p1); QVariant v2 = v1; - QVERIFY(v1 == v2); + QCOMPARE(v1, v2); v2 = QVariant::fromValue(p1); - QVERIFY(v1 == v2); + QCOMPARE(v1, v2); delete p1; } @@ -3113,8 +3133,8 @@ void tst_QVariant::compareCustomTypes() const /* We compare pointers. */ QVERIFY(variant1 != variant2); - QVERIFY(variant1 == variant1); - QVERIFY(variant2 == variant2); + QCOMPARE(variant1, variant1); + QCOMPARE(variant2, variant2); } void tst_QVariant::timeToDateTime() const @@ -3337,7 +3357,7 @@ void tst_QVariant::toIntFromQString() const void tst_QVariant::toIntFromDouble() const { double d = 2147483630; // max int 2147483647 - QVERIFY((int)d == 2147483630); + QCOMPARE((int)d, 2147483630); QVariant var(d); QVERIFY( var.canConvert( QVariant::Int ) ); @@ -3422,10 +3442,11 @@ void tst_QVariant::numericalConvert() switch (v.userType()) { case QVariant::Double: - QCOMPARE(v.toString() , QString::number(num, 'g', DBL_MANT_DIG * std::log10(2.) + 2)); + QCOMPARE(v.toString() , QString::number(num, 'g', QLocale::FloatingPointShortest)); break; case QMetaType::Float: - QCOMPARE(v.toString() , QString::number(float(num), 'g', FLT_MANT_DIG * std::log10(2.) + 2)); + QCOMPARE(v.toString() , + QString::number(float(num), 'g', QLocale::FloatingPointShortest)); break; } } @@ -3661,8 +3682,17 @@ void tst_QVariant::moreCustomTypes() QCOMPARE(MyNotMovable::count, 0); { +#ifdef QT_NO_DOUBLECONVERSION + // snprintf cannot do "shortest" conversion and always adds noise. PLAY_WITH_VARIANT(12.12, false, "12.119999999999999", 12.12, true); - PLAY_WITH_VARIANT(12.12f, false, "12.1199999", 12.12f, true); +#else + // Double can be printed exactly with libdouble-conversion + PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true); +#endif + + // Float is converted to double, adding insignificant bits + PLAY_WITH_VARIANT(12.12f, false, "12.119999885559082", 12.12f, true); + PLAY_WITH_VARIANT('a', false, "a", 'a', true); PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true); PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true); @@ -4167,7 +4197,7 @@ void tst_QVariant::saveInvalid() QDataStream stream(&data, QIODevice::WriteOnly); stream.setVersion(version); stream << QVariant(); - QVERIFY(stream.status() == QDataStream::Ok); + QCOMPARE(stream.status(), QDataStream::Ok); QVERIFY(data.size() >= 4); QCOMPARE(int(data.constData()[0]), 0); QCOMPARE(int(data.constData()[1]), 0); @@ -4181,7 +4211,7 @@ void tst_QVariant::saveNewBuiltinWithOldStream() QDataStream stream(&data, QIODevice::WriteOnly); stream.setVersion(QDataStream::Qt_3_1); stream << QVariant::fromValue<QJsonValue>(123); // QJsonValue class was introduced in Qt5 - QVERIFY(stream.status() == QDataStream::Ok); + QCOMPARE(stream.status(), QDataStream::Ok); QVERIFY(data.size() >= 4); QCOMPARE(int(data.constData()[0]), 0); QCOMPARE(int(data.constData()[1]), 0); |