From 728a7c2e1fc20c482b737a7070bd71afdf8e7d64 Mon Sep 17 00:00:00 2001 From: Thierry Bastian Date: Mon, 27 Apr 2009 14:17:39 +0200 Subject: Fixed to float support in QVariant also added autotests --- src/corelib/kernel/qvariant.cpp | 58 ++++++++++++++++++++---------------- tests/auto/qvariant/tst_qvariant.cpp | 50 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 25 deletions(-) diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index ea66658..ff59540 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -325,6 +325,7 @@ static bool isNull(const QVariant::Private *d) case QVariant::ULongLong: case QVariant::Bool: case QVariant::Double: + case QMetaType::Float: break; } return d->is_null; @@ -416,6 +417,8 @@ static bool compare(const QVariant::Private *a, const QVariant::Private *b) return a->data.b == b->data.b; case QVariant::Double: return a->data.d == b->data.d; + case QMetaType::Float: + return a->data.f == b->data.f; case QVariant::Date: return *v_cast(a) == *v_cast(b); case QVariant::Time: @@ -2554,57 +2557,62 @@ static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] = */ bool QVariant::canConvert(Type t) const { - if (d.type == uint(t)) + //we can treat floats as double + //the reason for not doing it the "proper" way is that QMetaTye Float's value is 135, + //which can't be handled by qCanConvertMatrix + const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type); + if (t == QMetaType::Float) t = QVariant::Double; + + if (currentType == uint(t)) return true; - if (d.type > QVariant::LastCoreType || t > QVariant::LastCoreType) { + if (currentType > QVariant::LastCoreType || t > QVariant::LastCoreType) { switch (uint(t)) { case QVariant::Int: - return d.type == QVariant::KeySequence - || d.type == QMetaType::ULong - || d.type == QMetaType::Long - || d.type == QMetaType::UShort - || d.type == QMetaType::UChar - || d.type == QMetaType::Char - || d.type == QMetaType::Short; + return currentType == QVariant::KeySequence + || currentType == QMetaType::ULong + || currentType == QMetaType::Long + || currentType == QMetaType::UShort + || currentType == QMetaType::UChar + || currentType == QMetaType::Char + || currentType == QMetaType::Short; case QVariant::Image: - return d.type == QVariant::Pixmap || d.type == QVariant::Bitmap; + return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap; case QVariant::Pixmap: - return d.type == QVariant::Image || d.type == QVariant::Bitmap - || d.type == QVariant::Brush; + return currentType == QVariant::Image || currentType == QVariant::Bitmap + || currentType == QVariant::Brush; case QVariant::Bitmap: - return d.type == QVariant::Pixmap || d.type == QVariant::Image; + return currentType == QVariant::Pixmap || currentType == QVariant::Image; case QVariant::ByteArray: - return d.type == QVariant::Color; + return currentType == QVariant::Color; case QVariant::String: - return d.type == QVariant::KeySequence || d.type == QVariant::Font - || d.type == QVariant::Color; + return currentType == QVariant::KeySequence || currentType == QVariant::Font + || currentType == QVariant::Color; case QVariant::KeySequence: - return d.type == QVariant::String || d.type == QVariant::Int; + return currentType == QVariant::String || currentType == QVariant::Int; case QVariant::Font: - return d.type == QVariant::String; + return currentType == QVariant::String; case QVariant::Color: - return d.type == QVariant::String || d.type == QVariant::ByteArray - || d.type == QVariant::Brush; + return currentType == QVariant::String || currentType == QVariant::ByteArray + || currentType == QVariant::Brush; case QVariant::Brush: - return d.type == QVariant::Color || d.type == QVariant::Pixmap; + return currentType == QVariant::Color || currentType == QVariant::Pixmap; case QMetaType::Long: case QMetaType::Char: case QMetaType::UChar: case QMetaType::ULong: case QMetaType::Short: case QMetaType::UShort: - case QMetaType::Float: - return qCanConvertMatrix[QVariant::Int] & (1 << d.type) || d.type == QVariant::Int; + return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int; default: return false; } } - if(t == String && d.type == StringList) + if(t == String && currentType == StringList) return v_cast(&d)->count() == 1; else - return qCanConvertMatrix[t] & (1 << d.type); + return qCanConvertMatrix[t] & (1 << currentType); } /*! diff --git a/tests/auto/qvariant/tst_qvariant.cpp b/tests/auto/qvariant/tst_qvariant.cpp index d15f9c8..0ede920 100644 --- a/tests/auto/qvariant/tst_qvariant.cpp +++ b/tests/auto/qvariant/tst_qvariant.cpp @@ -440,6 +440,9 @@ void tst_QVariant::canConvert_data() var = QVariant((double)0.1); QTest::newRow("Double") << var << N << N << Y << N << Y << Y << N << N << N << N << N << Y << N << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + var = QVariant(0.1f); + QTest::newRow("Float") + << var << N << N << Y << N << Y << Y << N << N << N << N << N << Y << N << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; var = qVariantFromValue(QFont()); QTest::newRow("Font") << var << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N; @@ -573,6 +576,7 @@ void tst_QVariant::canConvert() QCOMPARE(val.canConvert(QVariant::Date), DateCast); QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast); QCOMPARE(val.canConvert(QVariant::Double), DoubleCast); + QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast); QCOMPARE(val.canConvert(QVariant::Font), FontCast); #ifdef QT3_SUPPORT QCOMPARE(val.canConvert(QVariant::IconSet), IconSetCast); @@ -615,6 +619,7 @@ void tst_QVariant::toInt_data() QTest::newRow( "invalid" ) << QVariant() << 0 << false; QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true; + QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true; #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 42 ) << 1 << true; @@ -627,6 +632,7 @@ void tst_QVariant::toInt_data() QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true; QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true; QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true; + QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true; QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true; QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true; QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true; @@ -666,6 +672,7 @@ void tst_QVariant::toUInt_data() QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true; + QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true; #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 42 ) << (uint)1 << true; @@ -679,6 +686,7 @@ void tst_QVariant::toUInt_data() QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true; QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true; QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true; + QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true; QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false; QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true; QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true; @@ -860,7 +868,9 @@ void tst_QVariant::toBool_data() QTest::newRow( "uint0" ) << QVariant( 0u ) << false; QTest::newRow( "uint1" ) << QVariant( 123u ) << true; QTest::newRow( "double0" ) << QVariant( 0.0 ) << false; + QTest::newRow( "float0" ) << QVariant( 0.0f ) << false; QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true; + QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true; #ifdef QT3_SUPPORT QTest::newRow( "bool0" ) << QVariant( false, 42 ) << false; QTest::newRow( "bool1" ) << QVariant( true, 42 ) << true; @@ -1090,6 +1100,7 @@ void tst_QVariant::toLongLong_data() QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true; + QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true; #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 42 ) << (qlonglong)1 << true; @@ -1130,6 +1141,7 @@ void tst_QVariant::toULongLong_data() QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true; + QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true; #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 42 ) << (qulonglong)1 << true; @@ -1218,6 +1230,7 @@ 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.456" ); QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" ); QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" ); } @@ -1243,6 +1256,7 @@ 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.456" ); #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 0 ) << QString( "true" ); #else @@ -1450,6 +1464,7 @@ void tst_QVariant::writeToReadFromDataStream_data() QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true; QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false; QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false; + QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false; QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false; QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true; QPixmap pixmap( 10, 10 ); @@ -1476,6 +1491,7 @@ void tst_QVariant::writeToReadFromDataStream_data() vMap.insert( "int", QVariant( 1 ) ); vMap.insert( "string", QVariant( QString("Two") ) ); vMap.insert( "double", QVariant( 3.45 ) ); + vMap.insert( "float", QVariant( 3.45f ) ); QTest::newRow( "map_valid" ) << QVariant( vMap ) << false; QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false; QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false; @@ -1669,6 +1685,10 @@ void tst_QVariant::operator_eq_eq_data() QVariant mDoubleString(QByteArray("42.11")); QVariant mDoubleQString(QString("42.11")); + QVariant mFloat(42.11f); + QVariant mFloatString(QByteArray("42.11")); + QVariant mFloatQString(QString("42.11")); + QVariant mLongLong((qlonglong)-42); QVariant mLongLongString(QByteArray("-42")); QVariant mLongLongQString(QString("-42")); @@ -1686,6 +1706,7 @@ void tst_QVariant::operator_eq_eq_data() QVariant mBoolQString(QString("false")); QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true; + QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true; QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true; QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true; QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true; @@ -1701,6 +1722,11 @@ void tst_QVariant::operator_eq_eq_data() QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true; QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true; + QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true; + QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true; + QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true; + QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true; + QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true; QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true; QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true; @@ -1900,6 +1926,7 @@ void tst_QVariant::typeName_data() QTest::newRow("17") << int(QVariant::UInt) << QByteArray("uint"); QTest::newRow("18") << int(QVariant::Bool) << QByteArray("bool"); QTest::newRow("19") << int(QVariant::Double) << QByteArray("double"); + QTest::newRow("20") << int(QMetaType::Float) << QByteArray("float"); QTest::newRow("21") << int(QVariant::Polygon) << QByteArray("QPolygon"); QTest::newRow("22") << int(QVariant::Region) << QByteArray("QRegion"); QTest::newRow("23") << int(QVariant::Bitmap) << QByteArray("QBitmap"); @@ -2233,6 +2260,13 @@ void tst_QVariant::basicUserType() QCOMPARE(v.toDouble(), 4.4); { + float f = 4.5f; + v = QVariant(QMetaType::Float, &f); + } + QCOMPARE(v.userType(), int(QMetaType::Float)); + QCOMPARE(v.toDouble(), 4.5); + + { QByteArray ba("bar"); v = QVariant(QMetaType::QByteArray, &ba); } @@ -2246,6 +2280,7 @@ void tst_QVariant::data_() QVariant i = 1; QVariant d = 1.12; + QVariant f = 1.12f; QVariant ll = (qlonglong)2; QVariant ull = (qulonglong)3; QVariant s(QString("hallo")); @@ -2259,6 +2294,10 @@ void tst_QVariant::data_() QVERIFY(v.data()); QCOMPARE(*static_cast(v.data()), d.toDouble()); + v = f; + QVERIFY(v.data()); + QCOMPARE(*static_cast(v.data()), qVariantValue(v)); + v = ll; QVERIFY(v.data()); QCOMPARE(*static_cast(v.data()), ll.toLongLong()); @@ -2282,6 +2321,7 @@ void tst_QVariant::constData() int i = 1; double d = 1.12; + float f = 1.12f; qlonglong ll = 2; qulonglong ull = 3; QString s("hallo"); @@ -2295,6 +2335,10 @@ void tst_QVariant::constData() QVERIFY(v.constData()); QCOMPARE(*static_cast(v.constData()), d); + v = QVariant(f); + QVERIFY(v.constData()); + QCOMPARE(*static_cast(v.constData()), f); + v = QVariant(ll); QVERIFY(v.constData()); QCOMPARE(*static_cast(v.constData()), ll); @@ -2339,6 +2383,7 @@ void tst_QVariant::variant_to() qVariantSetValue(v4, foo); QCOMPARE(qvariant_cast(v1), 4.2); + QCOMPARE(qvariant_cast(v1), 4.2f); QCOMPARE(qvariant_cast(v2), 5); QCOMPARE(qvariant_cast(v3), sl); QCOMPARE(qvariant_cast(v3), QString::fromLatin1("blah")); @@ -2354,6 +2399,7 @@ void tst_QVariant::variant_to() QCOMPARE(qvariant_cast(n), 42); QCOMPARE(qvariant_cast(n), 42u); QCOMPARE(qvariant_cast(n), 42.0); + QCOMPARE(qvariant_cast(n), 42.f); QCOMPARE(qvariant_cast(n), short(42)); QCOMPARE(qvariant_cast(n), ushort(42)); @@ -2361,6 +2407,7 @@ void tst_QVariant::variant_to() QCOMPARE(qvariant_cast(n), 43); QCOMPARE(qvariant_cast(n), 43u); QCOMPARE(qvariant_cast(n), 43.0); + QCOMPARE(qvariant_cast(n), 43.f); QCOMPARE(qvariant_cast(n), 43l); n = QLatin1String("44"); @@ -2742,6 +2789,9 @@ void tst_QVariant::task172061_invalidDate() const variant = foo; QVERIFY(!variant.convert(QVariant::Double)); + + variant = foo; + QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float))); } struct WontCompare -- cgit v0.12