1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
45 #include <qbitarray.h>
46 #include <qhostaddress.h>
47 #include <qdatetime.h>
49 #include <qiodevice.h>
52 #include <qkeysequence.h>
58 #include <qmatrix4x4.h>
62 #include <qtransform.h>
63 #include <qvector2d.h>
64 #include <qvector3d.h>
65 #include <qvector4d.h>
66 #include <qquaternion.h>
71 Q_DECLARE_METATYPE(qlonglong)
72 Q_DECLARE_METATYPE(qulonglong)
73 Q_DECLARE_METATYPE(QPointF)
74 Q_DECLARE_METATYPE(QRectF)
75 Q_DECLARE_METATYPE(QSize)
76 Q_DECLARE_METATYPE(QSizeF)
77 Q_DECLARE_METATYPE(QLine)
78 Q_DECLARE_METATYPE(QLineF)
79 Q_DECLARE_METATYPE(QPoint)
80 Q_DECLARE_METATYPE(QRect)
81 Q_DECLARE_METATYPE(QPixmap)
82 Q_DECLARE_METATYPE(QBrush)
83 Q_DECLARE_METATYPE(QFont)
84 Q_DECLARE_METATYPE(QColor)
85 Q_DECLARE_METATYPE(QKeySequence)
87 class CustomNonQObject;
89 class tst_QVariant : public QObject
94 tst_QVariant(QObject *parent = 0)
95 : QObject(parent), customNonQObjectPointer(0)
101 void cleanupTestCase();
104 void copy_constructor();
105 void constructor_invalid_data();
106 void constructor_invalid();
110 void canConvert_data();
143 void toLongLong_data();
146 void toULongLong_data();
149 void toByteArray_data();
152 void toString_data();
161 void toDateTime_data();
164 void toDouble_data();
167 void toPointF_data();
173 void toKeySequence_data();
174 void toKeySequence();
182 void toPixmap_data();
191 void qvariant_cast_QObject_data();
192 void qvariant_cast_QObject();
193 void qvariant_cast_QObject_derived();
198 void toRegularExpression();
213 void basicUserType();
217 void writeToReadFromDataStream_data();
218 void writeToReadFromDataStream();
219 void writeToReadFromOldDataStream();
220 void checkDataStream();
222 void operator_eq_eq_data();
223 void operator_eq_eq();
225 void operator_eq_eq_rhs();
227 void typeName_data();
231 void streamInvalidVariant();
238 void saveLoadCustomTypes();
243 void convertToQUint8() const;
244 void invalidQColor() const;
245 void comparePointers() const;
246 void voidStar() const;
247 void dataStar() const;
248 void canConvertQStringList() const;
249 void canConvertQStringList_data() const;
250 void canConvertMetaTypeToInt() const;
251 void variantToDateTimeWithoutWarnings() const;
252 void invalidDateTime() const;
254 void loadUnknownUserType();
255 void loadBrokenUserType();
257 void invalidDate() const;
258 void compareCustomTypes() const;
259 void timeToDateTime() const;
260 void copyingUserTypes() const;
261 void convertBoolToByteArray() const;
262 void convertBoolToByteArray_data() const;
263 void convertByteArrayToBool() const;
264 void convertByteArrayToBool_data() const;
265 void toIntFromQString() const;
266 void toIntFromDouble() const;
269 void numericalConvert();
270 void moreCustomTypes();
271 void movabilityTest();
272 void variantInVariant();
276 void forwardDeclare();
277 void debugStream_data();
279 void debugStreamType_data();
280 void debugStreamType();
282 void loadQt4Stream_data();
283 void loadQt4Stream();
284 void saveQt4Stream_data();
285 void saveQt4Stream();
286 void loadQt5Stream_data();
287 void loadQt5Stream();
288 void saveQt5Stream_data();
289 void saveQt5Stream();
291 void guiVariantAtExit();
292 void widgetsVariantAtExit();
294 void dataStream_data(QDataStream::Version version);
295 void loadQVariantFromDataStream(QDataStream::Version version);
296 void saveQVariantFromDataStream(QDataStream::Version version);
298 CustomNonQObject *customNonQObjectPointer;
299 QVector<QObject*> objectPointerTestData;
302 Q_DECLARE_METATYPE(QDate)
303 Q_DECLARE_METATYPE(QTime)
304 Q_DECLARE_METATYPE(QDateTime)
305 Q_DECLARE_METATYPE(QVariant)
307 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
308 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
310 void tst_QVariant::constructor()
313 QVERIFY( !variant.isValid() );
314 QVERIFY( variant.isNull() );
316 QVariant var2(variant);
317 QVERIFY( !var2.isValid() );
318 QVERIFY( variant.isNull() );
320 QVariant varll(intMax1);
321 QVariant varll2(varll);
322 QCOMPARE(varll2, varll);
324 QVariant var3(QVariant::String);
325 QCOMPARE(var3.typeName(), "QString");
326 QVERIFY(var3.isNull());
327 QVERIFY(var3.isValid());
329 QVariant var4(QVariant::Invalid);
330 QCOMPARE(var4.type(), QVariant::Invalid);
331 QVERIFY(var4.isNull());
332 QVERIFY(!var4.isValid());
334 QVariant var5(QLatin1String("hallo"));
335 QCOMPARE(var5.type(), QVariant::String);
336 QCOMPARE(var5.typeName(), "QString");
338 QVariant var6(qlonglong(0));
339 QCOMPARE(var6.type(), QVariant::LongLong);
340 QCOMPARE(var6.typeName(), "qlonglong");
343 QVERIFY(var7.isValid());
344 QVERIFY(!var7.isNull());
346 var8.setValue<int>(5);
347 QVERIFY(var8.isValid());
348 QVERIFY(!var8.isNull());
351 void tst_QVariant::constructor_invalid_data()
353 QTest::addColumn<uint>("typeId");
355 QTest::newRow("-1") << uint(-1);
356 QTest::newRow("-122234567") << uint(-122234567);
357 QTest::newRow("0xfffffffff") << uint(0xfffffffff);
358 QTest::newRow("LastCoreType + 1") << uint(QMetaType::LastCoreType + 1);
359 QVERIFY(!QMetaType::isRegistered(QMetaType::LastCoreType + 1));
360 QTest::newRow("LastGuiType + 1") << uint(QMetaType::LastGuiType + 1);
361 QVERIFY(!QMetaType::isRegistered(QMetaType::LastGuiType + 1));
362 QTest::newRow("LastWidgetsType + 1") << uint(QMetaType::LastWidgetsType + 1);
363 QVERIFY(!QMetaType::isRegistered(QMetaType::LastWidgetsType + 1));
366 struct MessageHandlerInvalidType
368 MessageHandlerInvalidType()
369 : oldMsgHandler(qInstallMsgHandler(handler))
374 ~MessageHandlerInvalidType()
376 qInstallMsgHandler(oldMsgHandler);
379 QtMsgHandler oldMsgHandler;
381 static void handler(QtMsgType type, const char *txt)
383 QString msg = QString::fromLatin1(txt);
384 // uint(-1) can be platform dependent so we check only beginning of the message.
385 ok = msg.startsWith("Trying to construct an instance of an invalid type, type id:");
386 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
390 bool MessageHandlerInvalidType::ok;
392 void tst_QVariant::constructor_invalid()
395 QFETCH(uint, typeId);
397 MessageHandlerInvalidType msg;
398 QVariant variant(static_cast<QVariant::Type>(typeId));
399 QVERIFY(!variant.isValid());
400 QVERIFY(variant.userType() == QMetaType::UnknownType);
404 MessageHandlerInvalidType msg;
405 QVariant variant(typeId, /* copy */ 0);
406 QVERIFY(!variant.isValid());
407 QVERIFY(variant.userType() == QMetaType::UnknownType);
412 void tst_QVariant::copy_constructor()
414 QVariant var7(QVariant::Int);
416 QCOMPARE(var8.type(), QVariant::Int);
417 QVERIFY(var8.isNull());
420 void tst_QVariant::isNull()
423 QVERIFY( var.isNull() );
426 QVariant var1( str1 );
427 QVERIFY( var1.isNull() );
429 QVariant var2( QString::null );
430 QVERIFY( var2.isNull() );
432 QVariant var3( QString( "blah" ) );
433 QVERIFY( !var3.isNull() );
436 QVERIFY( !var4.isNull() );
438 QVariant var5 = QString();
439 QVERIFY( var5.isNull() );
441 QVariant var6( QString( "blah" ) );
442 QVERIFY( !var6.isNull() );
444 QVERIFY( var6.isNull() );
445 var6.convert( QVariant::String );
446 QVERIFY( var6.isNull() );
447 QVariant varLL( (qlonglong)0 );
448 QVERIFY( !varLL.isNull() );
449 QVariant var7(QString::null);
450 QVERIFY(var7.isNull());
453 void tst_QVariant::swap()
455 QVariant v1 = 1, v2 = 2.0;
457 QCOMPARE(v1.type(),QVariant::Double);
458 QCOMPARE(v1.toDouble(),2.0);
459 QCOMPARE(v2.type(),QVariant::Int);
460 QCOMPARE(v2.toInt(),1);
463 void tst_QVariant::canConvert_data()
465 QTest::addColumn<QVariant>("val");
466 QTest::addColumn<bool>("BitArrayCast");
467 QTest::addColumn<bool>("BitmapCast");
468 QTest::addColumn<bool>("BoolCast");
469 QTest::addColumn<bool>("BrushCast");
470 QTest::addColumn<bool>("ByteArrayCast");
471 QTest::addColumn<bool>("ColorCast");
472 QTest::addColumn<bool>("CursorCast");
473 QTest::addColumn<bool>("DateCast");
474 QTest::addColumn<bool>("DateTimeCast");
475 QTest::addColumn<bool>("DoubleCast");
476 QTest::addColumn<bool>("FontCast");
477 QTest::addColumn<bool>("ImageCast");
478 QTest::addColumn<bool>("IntCast");
479 QTest::addColumn<bool>("InvalidCast");
480 QTest::addColumn<bool>("KeySequenceCast");
481 QTest::addColumn<bool>("ListCast");
482 QTest::addColumn<bool>("LongLongCast");
483 QTest::addColumn<bool>("MapCast");
484 QTest::addColumn<bool>("PaletteCast");
485 QTest::addColumn<bool>("PenCast");
486 QTest::addColumn<bool>("PixmapCast");
487 QTest::addColumn<bool>("PointCast");
488 QTest::addColumn<bool>("RectCast");
489 QTest::addColumn<bool>("RegionCast");
490 QTest::addColumn<bool>("SizeCast");
491 QTest::addColumn<bool>("SizePolicyCast");
492 QTest::addColumn<bool>("StringCast");
493 QTest::addColumn<bool>("StringListCast");
494 QTest::addColumn<bool>("TimeCast");
495 QTest::addColumn<bool>("UIntCast");
496 QTest::addColumn<bool>("ULongLongCast");
507 // bita bitm bool brsh byta col curs date dt dbl font img int inv kseq list ll map pal pen pix pnt rect reg size sp str strl time uint ull
510 QVariant var(QBitArray(0));
511 QTest::newRow("BitArray")
512 << var << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
513 var = QVariant::fromValue(QBitmap());
514 QTest::newRow("Bitmap")
515 << var << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
516 var = QVariant::fromValue(QBrush());
517 QTest::newRow("Brush")
518 << var << N << N << N << Y << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
519 var = QVariant(QByteArray());
520 QTest::newRow("ByteArray")
521 << var << N << N << Y << N << Y << Y << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
522 var = QVariant::fromValue(QColor());
523 QTest::newRow("Color")
524 << var << N << N << N << Y << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
526 var = QVariant::fromValue(QCursor());
527 QTest::newRow("Cursor")
528 << var << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
530 var = QVariant(QDate());
531 QTest::newRow("Date")
532 << var << N << N << N << N << N << N << N << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
533 var = QVariant(QDateTime());
534 QTest::newRow("DateTime")
535 << var << N << N << N << N << N << N << N << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N;
536 var = QVariant((double)0.1);
537 QTest::newRow("Double")
538 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
539 var = QVariant(0.1f);
540 QTest::newRow("Float")
541 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
542 var = QVariant::fromValue(QFont());
543 QTest::newRow("Font")
544 << var << 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 << Y << N << N << N << N;
545 var = QVariant::fromValue(QIcon());
546 QTest::newRow("Icon")
547 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
548 var = QVariant::fromValue(QImage());
549 QTest::newRow("Image")
550 << var << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
551 var = QVariant((int)1);
553 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
555 QTest::newRow("Invalid")
556 << var << N << 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;
557 var = QVariant::fromValue(QKeySequence());
558 QTest::newRow("KeySequence")
559 << var << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
560 var = QVariant(QList<QVariant>());
561 QTest::newRow("List")
562 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N;
563 var = QVariant((qlonglong)1);
564 QTest::newRow("LongLong")
565 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
566 var = QVariant(QMap<QString,QVariant>());
568 << var << N << N << N << N << N << 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;
569 var = QVariant::fromValue(QPalette());
570 QTest::newRow("Palette")
571 << var << N << N << N << N << N << N << 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;
572 var = QVariant::fromValue(QPen());
574 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N;
575 var = QVariant::fromValue(QPixmap());
576 QTest::newRow("Pixmap")
577 << var << N << Y << N << Y << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
578 var = QVariant::fromValue(QPolygon());
579 QTest::newRow("PointArray")
580 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
581 var = QVariant(QPoint());
582 QTest::newRow("Point")
583 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N;
584 var = QVariant(QRect());
585 QTest::newRow("Rect")
586 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N;
587 var = QVariant::fromValue(QRegion());
588 QTest::newRow("Region")
589 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N;
590 var = QVariant(QSize());
591 QTest::newRow("Size")
592 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N;
593 var = QVariant::fromValue(QSizePolicy());
594 QTest::newRow("SizePolicy")
595 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N;
596 var = QVariant(QString());
597 QTest::newRow("String")
598 << var << N << N << Y << N << Y << Y << N << Y << Y << Y << Y << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << Y << Y << Y << Y;
599 var = QVariant(QStringList("entry"));
600 QTest::newRow("StringList")
601 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << Y << Y << N << N << N;
602 var = QVariant(QTime());
603 QTest::newRow("Time")
604 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N;
605 var = QVariant((uint)1);
606 QTest::newRow("UInt")
607 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
608 var = QVariant((qulonglong)1);
609 QTest::newRow("ULongLong")
610 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
616 void tst_QVariant::canConvert()
618 QFETCH(QVariant, val);
619 QFETCH(bool, BitArrayCast);
620 QFETCH(bool, BitmapCast);
621 QFETCH(bool, BoolCast);
622 QFETCH(bool, BrushCast);
623 QFETCH(bool, ByteArrayCast);
624 QFETCH(bool, ColorCast);
625 QFETCH(bool, CursorCast);
626 QFETCH(bool, DateCast);
627 QFETCH(bool, DateTimeCast);
628 QFETCH(bool, DoubleCast);
629 QFETCH(bool, FontCast);
630 QFETCH(bool, ImageCast);
631 QFETCH(bool, IntCast);
632 QFETCH(bool, InvalidCast);
633 QFETCH(bool, KeySequenceCast);
634 QFETCH(bool, ListCast);
635 QFETCH(bool, LongLongCast);
636 QFETCH(bool, MapCast);
637 QFETCH(bool, PaletteCast);
638 QFETCH(bool, PenCast);
639 QFETCH(bool, PixmapCast);
640 QFETCH(bool, PointCast);
641 QFETCH(bool, RectCast);
642 QFETCH(bool, RegionCast);
643 QFETCH(bool, SizeCast);
644 QFETCH(bool, SizePolicyCast);
645 QFETCH(bool, StringCast);
646 QFETCH(bool, StringListCast);
647 QFETCH(bool, TimeCast);
648 QFETCH(bool, UIntCast);
649 QFETCH(bool, ULongLongCast);
651 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
652 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
653 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
654 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
655 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
656 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
657 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
658 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
659 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
660 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
661 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
662 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
663 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
664 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
665 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
666 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
667 QCOMPARE(val.canConvert(QVariant::List), ListCast);
668 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
669 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
670 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
671 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
672 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
673 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
674 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
675 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
676 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
677 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
678 QCOMPARE(val.canConvert(QVariant::String), StringCast);
679 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
680 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
681 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
682 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
685 QCOMPARE(val.canConvert(-1), false);
686 QCOMPARE(val.canConvert(-23), false);
687 QCOMPARE(val.canConvert(-23876), false);
688 QCOMPARE(val.canConvert(23876), false);
691 void tst_QVariant::toInt_data()
693 QTest::addColumn<QVariant>("value");
694 QTest::addColumn<int>("result");
695 QTest::addColumn<bool>("valueOK");
697 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
698 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
699 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
700 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
701 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
702 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
703 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
704 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
705 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
706 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
707 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
708 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
709 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
710 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
711 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
712 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
713 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
714 QTest::newRow( "keysequence" ) << QVariant::fromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
715 QByteArray bytearray(4, ' ');
720 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
725 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
728 void tst_QVariant::toInt()
730 QFETCH( QVariant, value );
731 QFETCH( int, result );
732 QFETCH( bool, valueOK );
733 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
735 int i = value.toInt( &ok );
736 QCOMPARE( i, result );
737 QVERIFY( ok == valueOK );
740 void tst_QVariant::toUInt_data()
742 QTest::addColumn<QVariant>("value");
743 QTest::addColumn<uint>("result");
744 QTest::addColumn<bool>("valueOK");
746 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
747 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
748 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
749 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
750 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
751 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
752 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
753 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
754 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
755 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
756 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
757 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
758 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
759 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
760 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
761 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
762 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
763 QByteArray bytearray(4, ' ');
768 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
771 void tst_QVariant::toUInt()
773 QFETCH( QVariant, value );
774 QFETCH( uint, result );
775 QFETCH( bool, valueOK );
776 QVERIFY( value.isValid() );
777 QVERIFY( value.canConvert( QVariant::UInt ) );
780 uint i = value.toUInt( &ok );
781 QVERIFY( ok == valueOK );
782 QCOMPARE( i, result );
786 void tst_QVariant::toSize_data()
788 QTest::addColumn<QVariant>("value");
789 QTest::addColumn<QSize>("result");
790 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
791 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
792 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
793 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
796 void tst_QVariant::toSize()
798 QFETCH( QVariant, value );
799 QFETCH( QSize, result );
800 QVERIFY( value.isValid() );
801 QVERIFY( value.canConvert( QVariant::Size ) );
803 QSize i = value.toSize();
804 QCOMPARE( i, result );
807 void tst_QVariant::toSizeF_data()
809 QTest::addColumn<QVariant>("value");
810 QTest::addColumn<QSizeF>("result");
811 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
812 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
813 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
814 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
817 void tst_QVariant::toSizeF()
819 QFETCH( QVariant, value );
820 QFETCH( QSizeF, result );
821 QVERIFY( value.isValid() );
822 QVERIFY( value.canConvert( QVariant::SizeF ) );
824 QSizeF i = value.toSizeF();
825 QCOMPARE( i, result );
828 void tst_QVariant::toLine_data()
830 QTest::addColumn<QVariant>("value");
831 QTest::addColumn<QLine>("result");
832 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
833 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
834 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
835 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
838 void tst_QVariant::toLine()
840 QFETCH( QVariant, value );
841 QFETCH( QLine, result );
842 QVERIFY( value.isValid() );
843 QVERIFY( value.canConvert( QVariant::Line ) );
845 QLine i = value.toLine();
846 QCOMPARE( i, result );
849 void tst_QVariant::toLineF_data()
851 QTest::addColumn<QVariant>("value");
852 QTest::addColumn<QLineF>("result");
853 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
854 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
855 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
856 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
859 void tst_QVariant::toLineF()
861 QFETCH( QVariant, value );
862 QFETCH( QLineF, result );
863 QVERIFY( value.isValid() );
864 QVERIFY( value.canConvert( QVariant::LineF ) );
866 QLineF i = value.toLineF();
867 QCOMPARE( i, result );
870 void tst_QVariant::toPoint_data()
872 QTest::addColumn<QVariant>("value");
873 QTest::addColumn<QPoint>("result");
874 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
875 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
876 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
877 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
878 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
881 void tst_QVariant::toPoint()
883 QFETCH( QVariant, value );
884 QFETCH( QPoint, result );
885 QVERIFY( value.isValid() );
886 QVERIFY( value.canConvert( QVariant::Point ) );
887 QPoint i = value.toPoint();
888 QCOMPARE( i, result );
891 void tst_QVariant::toRect_data()
893 QTest::addColumn<QVariant>("value");
894 QTest::addColumn<QRect>("result");
895 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
896 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
897 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
898 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
899 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
902 void tst_QVariant::toRect()
904 QFETCH( QVariant, value );
905 QFETCH( QRect, result );
906 QVERIFY( value.isValid() );
907 QVERIFY( value.canConvert( QVariant::Rect ) );
908 QRect i = value.toRect();
909 QCOMPARE( i, result );
912 void tst_QVariant::toChar_data()
914 QTest::addColumn<QVariant>("value");
915 QTest::addColumn<QChar>("result");
916 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
917 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
920 void tst_QVariant::toChar()
922 QFETCH( QVariant, value );
923 QFETCH( QChar, result );
924 QVERIFY( value.isValid() );
925 QVERIFY( value.canConvert( QVariant::Char ) );
927 QChar i = value.toChar();
928 QCOMPARE( i, result );
931 void tst_QVariant::toBool_data()
933 QTest::addColumn<QVariant>("value");
934 QTest::addColumn<bool>("result");
936 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
937 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
938 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
939 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
940 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
941 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
942 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
943 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
944 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
945 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
946 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
947 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
948 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
949 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
950 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
951 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
952 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
953 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
956 void tst_QVariant::toBool()
958 QFETCH( QVariant, value );
959 QFETCH( bool, result );
960 QVERIFY( value.isValid() );
961 QVERIFY( value.canConvert( QVariant::Bool ) );
963 bool i = value.toBool();
964 QCOMPARE( i, result );
967 void tst_QVariant::toPointF_data()
969 QTest::addColumn<QVariant>("value");
970 QTest::addColumn<QPointF>("result");
972 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
975 void tst_QVariant::toPointF()
977 QFETCH( QVariant, value );
978 QFETCH( QPointF, result );
979 QVERIFY( value.isValid() );
980 QVERIFY( value.canConvert( QVariant::PointF ) );
981 QPointF d = value.toPointF();
982 QCOMPARE( d, result );
985 void tst_QVariant::toRectF_data()
987 QTest::addColumn<QVariant>("value");
988 QTest::addColumn<QRectF>("result");
990 QRect r( 1, 9, 8, 4 );
991 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
992 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
995 void tst_QVariant::toRectF()
997 QFETCH( QVariant, value );
998 QFETCH( QRectF, result );
999 QVERIFY( value.isValid() );
1000 QVERIFY( value.canConvert( QVariant::RectF ) );
1001 QRectF d = value.toRectF();
1002 QCOMPARE( d, result );
1005 void tst_QVariant::toColor_data()
1007 QTest::addColumn<QVariant>("value");
1008 QTest::addColumn<QColor>("result");
1011 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
1012 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
1015 void tst_QVariant::toColor()
1017 QFETCH( QVariant, value );
1018 QFETCH( QColor, result );
1019 QVERIFY( value.isValid() );
1020 QVERIFY( value.canConvert( QVariant::Color ) );
1021 QColor d = qvariant_cast<QColor>(value);
1022 QCOMPARE( d, result );
1025 void tst_QVariant::toPixmap_data()
1027 QTest::addColumn<QVariant>("value");
1028 QTest::addColumn<QPixmap>("result");
1032 QTest::newRow( "image" ) << QVariant( pm ) << pm;
1035 bm.fill(Qt::color1);
1036 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
1039 void tst_QVariant::toPixmap()
1041 QFETCH( QVariant, value );
1042 QFETCH( QPixmap, result );
1043 QVERIFY( value.isValid() );
1044 QVERIFY( value.canConvert( QVariant::Pixmap ) );
1045 QPixmap d = qvariant_cast<QPixmap>(value);
1046 QCOMPARE( d, result );
1049 void tst_QVariant::toImage_data()
1051 QTest::addColumn<QVariant>("value");
1052 QTest::addColumn<QImage>("result");
1054 QImage im(30, 30, QImage::Format_ARGB32);
1055 im.fill(0x7fff0000);
1056 QTest::newRow( "image" ) << QVariant( im ) << im;
1059 void tst_QVariant::toImage()
1061 QFETCH( QVariant, value );
1062 QFETCH( QImage, result );
1063 QVERIFY( value.isValid() );
1064 QVERIFY( value.canConvert( QVariant::Image ) );
1065 QImage d = qvariant_cast<QImage>(value);
1066 QCOMPARE( d, result );
1069 void tst_QVariant::toBrush_data()
1071 QTest::addColumn<QVariant>("value");
1072 QTest::addColumn<QBrush>("result");
1075 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
1078 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
1081 void tst_QVariant::toBrush()
1083 QFETCH( QVariant, value );
1084 QFETCH( QBrush, result );
1085 QVERIFY( value.isValid() );
1086 QVERIFY( value.canConvert( QVariant::Brush ) );
1087 QBrush d = qvariant_cast<QBrush>(value);
1088 QCOMPARE( d, result );
1091 void tst_QVariant::toFont_data()
1093 QTest::addColumn<QVariant>("value");
1094 QTest::addColumn<QFont>("result");
1096 QFont f("times",12,-1,false);
1097 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1100 void tst_QVariant::toFont()
1102 QFETCH( QVariant, value );
1103 QFETCH( QFont, result );
1104 QVERIFY( value.isValid() );
1105 QVERIFY( value.canConvert( QVariant::Font ) );
1106 QFont d = qvariant_cast<QFont>(value);
1107 QCOMPARE( d, result );
1110 void tst_QVariant::toKeySequence_data()
1112 QTest::addColumn<QVariant>("value");
1113 QTest::addColumn<QKeySequence>("result");
1116 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1119 QTest::newRow( "qstring" )
1120 << QVariant( QString( "Ctrl+A" ) )
1121 << QKeySequence( Qt::CTRL + Qt::Key_A );
1124 void tst_QVariant::toKeySequence()
1126 QFETCH( QVariant, value );
1127 QFETCH( QKeySequence, result );
1128 QVERIFY( value.isValid() );
1129 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1130 QKeySequence d = qvariant_cast<QKeySequence>(value);
1131 QCOMPARE( d, result );
1134 void tst_QVariant::toDouble_data()
1136 QTest::addColumn<QVariant>("value");
1137 QTest::addColumn<double>("result");
1138 QTest::addColumn<bool>("valueOK");
1140 QByteArray bytearray(4, ' ');
1145 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1148 void tst_QVariant::toDouble()
1150 QFETCH( QVariant, value );
1151 QFETCH( double, result );
1152 QFETCH( bool, valueOK );
1153 QVERIFY( value.isValid() );
1154 QVERIFY( value.canConvert( QVariant::Double ) );
1156 double d = value.toDouble( &ok );
1157 QCOMPARE( d, result );
1158 QVERIFY( ok == valueOK );
1161 void tst_QVariant::toLongLong_data()
1163 QTest::addColumn<QVariant>("value");
1164 QTest::addColumn<qlonglong>("result");
1165 QTest::addColumn<bool>("valueOK");
1167 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1168 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1169 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1170 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1171 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1172 << (qlonglong)123 << true;
1173 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1175 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1176 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1177 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1178 QByteArray bytearray(4, ' ');
1183 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1186 void tst_QVariant::toLongLong()
1188 QFETCH( QVariant, value );
1189 QFETCH( qlonglong, result );
1190 QFETCH( bool, valueOK );
1191 QVERIFY( value.isValid() );
1192 QVERIFY( value.canConvert( QVariant::LongLong ) );
1194 qlonglong ll = value.toLongLong( &ok );
1195 QCOMPARE( ll, result );
1196 QVERIFY( ok == valueOK );
1199 void tst_QVariant::toULongLong_data()
1201 QTest::addColumn<QVariant>("value");
1202 QTest::addColumn<qulonglong>("result");
1203 QTest::addColumn<bool>("valueOK");
1205 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1206 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1207 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1208 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1209 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1210 << (qulonglong)123 << true;
1211 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1213 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1214 << Q_UINT64_C(18446744073709551615)
1216 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1217 << Q_UINT64_C(18446744073709551615)
1219 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1220 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1221 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1222 QByteArray bytearray(4, ' ');
1227 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1230 void tst_QVariant::toULongLong()
1232 QFETCH( QVariant, value );
1233 QFETCH( qulonglong, result );
1234 QFETCH( bool, valueOK );
1235 QVERIFY( value.isValid() );
1236 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1238 qulonglong ll = value.toULongLong( &ok );
1239 QCOMPARE( ll, result );
1240 QVERIFY( ok == valueOK );
1243 void tst_QVariant::toByteArray_data()
1245 QTest::addColumn<QVariant>("value");
1246 QTest::addColumn<QByteArray>("result");
1248 QByteArray ba(5, ' ');
1255 QByteArray variantBa = ba;
1257 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1258 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1259 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1260 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1261 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1262 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1263 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1266 void tst_QVariant::toByteArray()
1268 QFETCH( QVariant, value );
1269 QFETCH( QByteArray, result );
1270 QVERIFY( value.isValid() );
1271 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1272 QByteArray ba = value.toByteArray();
1273 QCOMPARE( ba, result );
1276 void tst_QVariant::toString_data()
1278 QTest::addColumn<QVariant>("value");
1279 QTest::addColumn<QString>("result");
1281 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1282 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1283 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1284 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1285 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1286 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1287 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1288 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1289 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1290 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1291 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1292 QTest::newRow( "qkeysequence" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1294 << QString( "Ctrl+A" );
1296 << QString(QChar(0x2318)) + "A";
1299 QFont font( "times", 12 );
1300 QTest::newRow( "qfont" ) << QVariant::fromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1301 QTest::newRow( "qcolor" ) << QVariant::fromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1302 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1303 QString( "123456789012" );
1306 void tst_QVariant::toString()
1308 QFETCH( QVariant, value );
1309 QFETCH( QString, result );
1310 QVERIFY( value.isValid() );
1311 QVERIFY( value.canConvert( QVariant::String ) );
1312 QString str = value.toString();
1313 QCOMPARE( str, result );
1316 void tst_QVariant::toDate_data()
1318 QTest::addColumn<QVariant>("value");
1319 QTest::addColumn<QDate>("result");
1321 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1322 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1323 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1326 void tst_QVariant::toDate()
1328 QFETCH( QVariant, value );
1329 QFETCH( QDate, result );
1330 QVERIFY( value.isValid() );
1331 QVERIFY( value.canConvert( QVariant::Date ) );
1332 QCOMPARE( value.toDate(), result );
1335 void tst_QVariant::toTime_data()
1337 QTest::addColumn<QVariant>("value");
1338 QTest::addColumn<QTime>("result");
1340 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1341 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1342 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1345 void tst_QVariant::toTime()
1347 QFETCH( QVariant, value );
1348 QFETCH( QTime, result );
1349 QVERIFY( value.isValid() );
1350 QVERIFY( value.canConvert( QVariant::Time ) );
1351 QCOMPARE( value.toTime(), result );
1354 void tst_QVariant::toDateTime_data()
1356 QTest::addColumn<QVariant>("value");
1357 QTest::addColumn<QDateTime>("result");
1359 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1360 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1361 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1362 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1365 void tst_QVariant::toDateTime()
1367 QFETCH( QVariant, value );
1368 QFETCH( QDateTime, result );
1369 QVERIFY( value.isValid() );
1370 QVERIFY( value.canConvert( QVariant::DateTime ) );
1371 QCOMPARE( value.toDateTime(), result );
1374 void tst_QVariant::toLocale()
1377 QLocale loc = variant.toLocale();
1378 variant = QLocale::system();
1379 loc = variant.toLocale();
1382 void tst_QVariant::toRegExp()
1385 QRegExp rx = variant.toRegExp();
1386 variant = QRegExp("foo");
1387 rx = variant.toRegExp();
1390 void tst_QVariant::toRegularExpression()
1393 QRegularExpression re = variant.toRegularExpression();
1394 QCOMPARE(re, QRegularExpression());
1396 variant = QRegularExpression("abc.*def");
1397 re = variant.toRegularExpression();
1398 QCOMPARE(re, QRegularExpression("abc.*def"));
1400 variant = QVariant::fromValue(QRegularExpression("[ab]\\w+"));
1401 re = variant.value<QRegularExpression>();
1402 QCOMPARE(re, QRegularExpression("[ab]\\w+"));
1405 void tst_QVariant::matrix()
1408 QMatrix matrix = qvariant_cast<QMatrix>(variant);
1409 QVERIFY(matrix.isIdentity());
1410 variant.setValue(QMatrix().rotate(90));
1411 QCOMPARE(QMatrix().rotate(90), qvariant_cast<QMatrix>(variant));
1413 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1415 QMetaType::destroy(QVariant::Matrix, mmatrix);
1418 void tst_QVariant::matrix4x4()
1421 QMatrix4x4 matrix = qvariant_cast<QMatrix4x4>(variant);
1422 QVERIFY(matrix.isIdentity());
1425 variant.setValue(m);
1426 QCOMPARE(m, qvariant_cast<QMatrix4x4>(variant));
1428 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1430 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1433 void tst_QVariant::transform()
1436 QTransform matrix = qvariant_cast<QTransform>(variant);
1437 QVERIFY(matrix.isIdentity());
1438 variant.setValue(QTransform().rotate(90));
1439 QCOMPARE(QTransform().rotate(90), qvariant_cast<QTransform>(variant));
1441 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1443 QMetaType::destroy(QVariant::Transform, mmatrix);
1447 void tst_QVariant::vector2D()
1450 QVector2D vector = qvariant_cast<QVector2D>(variant);
1451 QVERIFY(vector.isNull());
1452 variant.setValue(QVector2D(0.1, 0.2));
1453 QCOMPARE(QVector2D(0.1, 0.2), qvariant_cast<QVector2D>(variant));
1455 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1457 QMetaType::destroy(QVariant::Vector2D, pvector);
1460 void tst_QVariant::vector3D()
1463 QVector3D vector = qvariant_cast<QVector3D>(variant);
1464 QVERIFY(vector.isNull());
1465 variant.setValue(QVector3D(0.1, 0.2, 0.3));
1466 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qvariant_cast<QVector3D>(variant));
1468 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1470 QMetaType::destroy(QVariant::Vector3D, pvector);
1473 void tst_QVariant::vector4D()
1476 QVector4D vector = qvariant_cast<QVector4D>(variant);
1477 QVERIFY(vector.isNull());
1478 variant.setValue(QVector4D(0.1, 0.2, 0.3, 0.4));
1479 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qvariant_cast<QVector4D>(variant));
1481 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1483 QMetaType::destroy(QVariant::Vector4D, pvector);
1486 void tst_QVariant::quaternion()
1489 QQuaternion quaternion = qvariant_cast<QQuaternion>(variant);
1490 QVERIFY(quaternion.isIdentity());
1491 variant.setValue(QQuaternion(0.1, 0.2, 0.3, 0.4));
1492 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qvariant_cast<QQuaternion>(variant));
1494 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1495 QVERIFY(pquaternion);
1496 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1499 struct CustomStreamableClass
1502 bool operator==(const CustomStreamableClass& other) const
1504 return i == other.i;
1507 Q_DECLARE_METATYPE(CustomStreamableClass);
1509 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1511 return out << myObj.i;
1514 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1516 return in >> myObj.i;
1519 void tst_QVariant::writeToReadFromDataStream_data()
1521 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1523 QTest::addColumn<QVariant>("writeVariant");
1524 QTest::addColumn<bool>("isNull");
1526 typedef QList<QVariant> variantsList;
1527 variantsList valuelist;
1528 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1529 QVariant var(valuelist);
1530 QTest::newRow( "list_valid" ) << var << false;
1533 QTest::newRow( "invalid" ) << QVariant() << true;
1534 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1535 QBitArray bitarray( 3 );
1539 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1540 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1541 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1542 QByteArray bytearray(5, ' ');
1547 bytearray[4] = '\0';
1548 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1549 QTest::newRow( "bitmap_invalid" ) << QVariant::fromValue( QBitmap() ) << true;
1550 QBitmap bitmap( 10, 10 );
1551 bitmap.fill( Qt::red );
1552 QTest::newRow( "bitmap_valid" ) << QVariant::fromValue( bitmap ) << false;
1553 QTest::newRow( "brush_valid" ) << QVariant::fromValue( QBrush( Qt::red ) ) << false;
1554 QTest::newRow( "color_valid" ) << QVariant::fromValue( QColor( Qt::red ) ) << false;
1555 #ifndef QT_NO_CURSOR
1556 QTest::newRow( "cursor_valid" ) << QVariant::fromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1558 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1559 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1560 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1561 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1562 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1563 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1564 QTest::newRow( "font_valid" ) << QVariant::fromValue( QFont( "times", 12 ) ) << false;
1565 QTest::newRow( "pixmap_invalid" ) << QVariant::fromValue( QPixmap() ) << true;
1566 QPixmap pixmap( 10, 10 );
1567 pixmap.fill( Qt::red );
1568 QTest::newRow( "pixmap_valid" ) << QVariant::fromValue( pixmap ) << false;
1569 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1570 QTest::newRow( "image_invalid" ) << QVariant::fromValue( QImage() ) << true;
1571 QTest::newRow( "keysequence_valid" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1572 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1573 typedef QMap<QString, QVariant> variantsMap;
1575 vMap.insert( "int", QVariant( 1 ) );
1576 vMap.insert( "string", QVariant( QString("Two") ) );
1577 vMap.insert( "double", QVariant( 3.45 ) );
1578 vMap.insert( "float", QVariant( 3.45f ) );
1579 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1580 QTest::newRow( "palette_valid" ) << QVariant::fromValue(QPalette(QColor("turquoise"))) << false;
1581 QTest::newRow( "pen_valid" ) << QVariant::fromValue( QPen( Qt::red ) ) << false;
1582 QTest::newRow( "pointarray_invalid" ) << QVariant::fromValue( QPolygon() ) << true;
1583 QTest::newRow( "pointarray_valid" ) << QVariant::fromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1584 QTest::newRow( "region_invalid" ) << QVariant::fromValue( QRegion() ) << true;
1585 QTest::newRow( "region_valid" ) << QVariant::fromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1586 QTest::newRow( "sizepolicy_valid" ) << QVariant::fromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1587 QTest::newRow( "point_invalid" ) << QVariant::fromValue( QPoint() ) << true;
1588 QTest::newRow( "point_valid" ) << QVariant::fromValue( QPoint( 10, 10 ) ) << false;
1589 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1590 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1591 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1592 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1593 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1594 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1595 QStringList stringlist;
1596 stringlist << "One" << "Two" << "Three";
1597 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1598 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1599 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1600 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1601 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1602 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1603 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1604 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1605 QTest::newRow( "regularexpression" ) << QVariant(QRegularExpression("abc.*def")) << false;
1606 QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
1608 // types known to QMetaType, but not part of QVariant::Type
1609 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1611 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1612 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1614 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1615 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1617 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1618 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1619 ulong ulongInt = 1ul;
1620 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1621 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1622 ushort ushortInt = 1u;
1623 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1624 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1626 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1627 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1629 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1630 CustomStreamableClass custom = {123};
1631 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1634 void tst_QVariant::writeToReadFromDataStream()
1636 QFETCH( QVariant, writeVariant );
1637 QFETCH( bool, isNull );
1640 QDataStream writeStream( &data, QIODevice::WriteOnly );
1641 writeStream << writeVariant;
1643 QVariant readVariant;
1644 QDataStream readStream( &data, QIODevice::ReadOnly );
1645 readStream >> readVariant;
1646 QVERIFY( readVariant.isNull() == isNull );
1647 // Best way to confirm the readVariant contains the same data?
1648 // Since only a few won't match since the serial numbers are different
1649 // I won't bother adding another bool in the data test.
1650 const int writeType = writeVariant.userType();
1651 if (writeType == qMetaTypeId<CustomStreamableClass>())
1652 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1653 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1654 && writeType != QVariant::Image) {
1655 switch (writeType) {
1657 QCOMPARE( readVariant, writeVariant );
1660 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1661 case QMetaType::Long:
1662 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1664 case QMetaType::ULong:
1665 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1667 case QMetaType::Short:
1668 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1670 case QMetaType::UShort:
1671 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1673 case QMetaType::Char:
1674 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1676 case QMetaType::UChar:
1677 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1679 case QMetaType::Float:
1681 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1682 float readFloat = qvariant_cast<float>(readVariant);
1683 float writtenFloat = qvariant_cast<float>(writeVariant);
1684 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1685 if (!qIsNaN(readFloat))
1686 QVERIFY(readFloat == writtenFloat);
1693 void tst_QVariant::writeToReadFromOldDataStream()
1695 QVariant writeVariant = QString("hello");
1698 QDataStream writeStream(&data, QIODevice::WriteOnly);
1699 writeStream.setVersion(QDataStream::Qt_2_1);
1700 writeStream << writeVariant;
1702 QVariant readVariant;
1703 QDataStream readStream(&data, QIODevice::ReadOnly);
1704 readStream.setVersion(QDataStream::Qt_2_1);
1705 readStream >> readVariant;
1707 QCOMPARE(writeVariant.userType(), readVariant.userType());
1708 QCOMPARE(writeVariant, readVariant);
1711 void tst_QVariant::checkDataStream()
1713 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 49");
1714 const QByteArray settingsHex("00000031ffffffffff");
1715 const QByteArray settings = QByteArray::fromHex(settingsHex);
1716 QDataStream in(settings);
1719 // the line below has been left out for now since the data stream
1720 // is not necessarily considered corrupt when an invalid QVariant is
1721 // constructed. However, it might be worth considering changing that behavior
1723 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1724 QCOMPARE(v.type(), QVariant::Invalid);
1727 void tst_QVariant::operator_eq_eq_data()
1729 QTest::addColumn<QVariant>("left");
1730 QTest::addColumn<QVariant>("right");
1731 QTest::addColumn<bool>("equal"); // left == right ?
1734 QVariant i0( int(0) );
1735 QVariant i1( int(1) );
1737 QTest::newRow( "invinv" ) << inv << inv << true;
1739 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1740 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1741 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1743 // LongLong and ULongLong
1744 QVariant ll1( (qlonglong)1 );
1745 QVariant lln2( (qlonglong)-2 );
1746 QVariant ull1( (qulonglong)1 );
1747 QVariant ull3( (qulonglong)3 );
1748 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1749 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1750 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1751 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1752 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1753 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1756 QVariant mIntString(QByteArray("-42"));
1757 QVariant mIntQString(QString("-42"));
1759 QVariant mUInt(42u);
1760 QVariant mUIntString(QByteArray("42"));
1761 QVariant mUIntQString(QString("42"));
1763 QVariant mDouble(42.11);
1764 QVariant mDoubleString(QByteArray("42.11"));
1765 QVariant mDoubleQString(QString("42.11"));
1767 QVariant mFloat(42.11f);
1768 QVariant mFloatString(QByteArray("42.11"));
1769 QVariant mFloatQString(QString("42.11"));
1771 QVariant mLongLong((qlonglong)-42);
1772 QVariant mLongLongString(QByteArray("-42"));
1773 QVariant mLongLongQString(QString("-42"));
1775 QVariant mULongLong((qulonglong)42);
1776 QVariant mULongLongString(QByteArray("42"));
1777 QVariant mULongLongQString(QString("42"));
1779 QVariant mBool(false);
1780 QVariant mBoolString(QByteArray("false"));
1781 QVariant mBoolQString(QString("false"));
1783 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1784 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1785 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1786 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1787 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1788 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1790 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1791 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1792 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1793 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1795 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1796 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1797 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1798 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1800 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1801 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1802 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1803 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1805 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1806 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1807 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1808 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1810 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1811 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1812 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1813 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1815 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1816 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1817 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1818 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1820 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1821 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1822 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1823 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1825 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1826 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1827 // ### many other combinations missing
1830 QMap<QString, QVariant> map1;
1831 map1.insert( "X", 1 );
1833 QMap<QString, QVariant> map2;
1834 map2.insert( "Y", 1 );
1836 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1841 QMap<QString, QVariant> map1;
1842 map1.insert( "X", 1 );
1844 QMap<QString, QVariant> map2;
1845 map2.insert( "X", 1 );
1847 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1851 QMap<QString, QVariant> map1;
1852 map1.insert( "X", 1 );
1854 QMap<QString, QVariant> map2;
1856 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1860 QMap<QString, QVariant> map1;
1862 QMap<QString, QVariant> map2;
1863 map2.insert( "X", 1 );
1865 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1869 QMap<QString, QVariant> map1;
1870 map1.insert( "X", 1 );
1871 map1.insert( "Y", 1 );
1873 QMap<QString, QVariant> map2;
1874 map2.insert( "X", 1 );
1876 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1880 QMap<QString, QVariant> map1;
1881 map1.insert( "X", 1 );
1883 QMap<QString, QVariant> map2;
1884 map2.insert( "X", 1 );
1885 map2.insert( "Y", 1 );
1887 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1890 // same thing with hash
1892 QHash<QString, QVariant> hash1;
1893 hash1.insert( "X", 1 );
1895 QHash<QString, QVariant> hash2;
1896 hash2.insert( "Y", 1 );
1898 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1903 QHash<QString, QVariant> hash1;
1904 hash1.insert( "X", 1 );
1906 QHash<QString, QVariant> hash2;
1907 hash2.insert( "X", 1 );
1909 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1913 QHash<QString, QVariant> hash1;
1914 hash1.insert( "X", 1 );
1916 QHash<QString, QVariant> hash2;
1918 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1922 QHash<QString, QVariant> hash1;
1924 QHash<QString, QVariant> hash2;
1925 hash2.insert( "X", 1 );
1927 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1931 QHash<QString, QVariant> hash1;
1932 hash1.insert( "X", 1 );
1933 hash1.insert( "Y", 1 );
1935 QHash<QString, QVariant> hash2;
1936 hash2.insert( "X", 1 );
1938 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1942 QHash<QString, QVariant> hash1;
1943 hash1.insert( "X", 1 );
1945 QHash<QString, QVariant> hash2;
1946 hash2.insert( "X", 1 );
1947 hash2.insert( "Y", 1 );
1949 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1953 void tst_QVariant::operator_eq_eq()
1955 QFETCH( QVariant, left );
1956 QFETCH( QVariant, right );
1957 QFETCH( bool, equal );
1958 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1959 QCOMPARE( left == right, equal );
1962 void tst_QVariant::operator_eq_eq_rhs()
1970 /* This should _not_ compile */
1974 QVERIFY(dt == list);
1978 void tst_QVariant::typeName_data()
1980 QTest::addColumn<int>("type");
1981 QTest::addColumn<QByteArray>("res");
1982 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1983 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1984 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1985 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1986 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1987 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1988 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1989 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1990 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1991 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1992 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1993 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1994 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1995 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1996 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1997 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1998 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1999 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
2000 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
2001 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
2002 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
2003 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
2004 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
2005 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
2006 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
2007 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
2008 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
2009 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
2010 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
2011 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
2012 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
2013 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
2014 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
2015 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
2016 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
2017 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
2018 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
2019 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
2020 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
2021 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
2022 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
2023 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
2024 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
2025 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
2026 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
2027 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
2028 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
2029 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
2030 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
2031 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
2032 QTest::newRow("51") << int(QVariant::RegularExpression) << QByteArray("QRegularExpression");
2035 void tst_QVariant::typeName()
2037 QFETCH( int, type );
2038 QFETCH( QByteArray, res );
2039 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
2040 QString::fromLatin1(res.constData()));
2043 // test nameToType as well
2044 void tst_QVariant::typeToName()
2047 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
2048 // assumes that QVariant::Type contains consecutive values
2050 int max = QVariant::LastGuiType;
2051 for ( int t = 1; t <= max; t++ ) {
2052 const char *n = QVariant::typeToName( (QVariant::Type)t );
2054 QCOMPARE( int(QVariant::nameToType( n )), t );
2057 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
2058 // not documented but we return 0 if the type is out of range
2059 // by testing this we catch cases where QVariant is extended
2060 // but type_map is not updated accordingly
2061 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
2062 // invalid type names
2063 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
2064 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
2065 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
2067 QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
2069 // We don't support these old (Qt3) types anymore.
2070 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
2071 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
2072 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
2073 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
2076 void tst_QVariant::streamInvalidVariant()
2082 QVariant writeVariant;
2083 QVariant readVariant;
2085 QVERIFY( writeVariant.type() == QVariant::Invalid );
2088 QDataStream writeStream( &data, QIODevice::WriteOnly );
2089 writeStream << writeX << writeVariant << writeY;
2091 QDataStream readStream( &data, QIODevice::ReadOnly );
2092 readStream >> readX >> readVariant >> readY;
2094 QVERIFY( readX == writeX );
2095 // Two invalid QVariant's aren't necessarily the same, so == will
2096 // return false if one is invalid, so check the type() instead
2097 QVERIFY( readVariant.type() == QVariant::Invalid );
2098 QVERIFY( readY == writeY );
2101 static int instanceCount = 0;
2105 MyType(int n = 0, const char *t=0): number(n), text(t)
2109 MyType(const MyType &other)
2110 : number(other.number), text(other.text)
2122 Q_DECLARE_METATYPE(MyType)
2123 Q_DECLARE_METATYPE(MyType*)
2125 void tst_QVariant::userType()
2128 MyType data(1, "eins");
2129 MyType data2(2, "zwei");
2133 userVar.setValue(data);
2135 QCOMPARE(userVar.type(), QVariant::UserType);
2136 QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2137 QCOMPARE(userVar.typeName(), "MyType");
2138 QVERIFY(!userVar.isNull());
2139 QVERIFY(!userVar.canConvert(QVariant::String));
2141 QVariant userVar2(userVar);
2142 QVERIFY(userVar == userVar2);
2144 userVar2.setValue(data2);
2145 QVERIFY(userVar != userVar2);
2147 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2149 QCOMPARE(varData->number, data.number);
2150 QCOMPARE(varData->text, data.text);
2153 userVar3.setValue(data2);
2155 userVar3 = userVar2;
2156 QVERIFY(userVar2 == userVar3);
2158 // At this point all QVariants got destroyed but we have 2 MyType instances.
2159 QCOMPARE(instanceCount, 2);
2162 userVar.setValue(&data);
2164 QCOMPARE(userVar.type(), QVariant::UserType);
2165 QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2166 QCOMPARE(userVar.typeName(), "MyType*");
2167 QVERIFY(!userVar.isNull());
2168 QVERIFY(!userVar.canConvert(QVariant::String));
2170 QVariant userVar2(userVar);
2171 QVERIFY(userVar == userVar2);
2173 userVar2.setValue(&data2);
2174 QVERIFY(userVar != userVar2);
2176 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2178 QCOMPARE(*varData, &data);
2181 userVar3.setValue(&data2);
2183 /* This check is correct now. userVar2 contains a pointer to data2 and so
2185 QVERIFY(userVar2 == userVar3);
2187 userVar3 = userVar2;
2188 QVERIFY(userVar2 == userVar3);
2191 QCOMPARE(instanceCount, 2);
2193 myCarrier.setValue(data);
2194 QCOMPARE(instanceCount, 3);
2196 QVariant second = myCarrier;
2197 QCOMPARE(instanceCount, 3);
2199 QCOMPARE(instanceCount, 4);
2201 QCOMPARE(instanceCount, 3);
2203 MyType data3(0, "null");
2204 data3 = qvariant_cast<MyType>(myCarrier);
2205 QCOMPARE(data3.number, 1);
2206 QCOMPARE(data3.text, (const char *)"eins");
2208 QCOMPARE(instanceCount, 4);
2214 const MyType data(3, "drei");
2217 myCarrier.setValue(data);
2218 QCOMPARE(myCarrier.typeName(), "MyType");
2220 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2221 QCOMPARE(data2.number, 3);
2222 QCOMPARE(data2.text, (const char *)"drei");
2229 myCarrier.setValue(s);
2230 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2234 qlonglong ll = Q_INT64_C(42);
2237 myCarrier.setValue(ll);
2238 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2241 // At this point all QVariants got destroyed and MyType objects too.
2242 QCOMPARE(instanceCount, 0);
2250 Q_DECLARE_METATYPE(MyTypePOD)
2252 void tst_QVariant::podUserType()
2258 QVariant pod_as_variant = QVariant::fromValue(pod);
2259 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2261 QCOMPARE(pod.a, pod2.a);
2262 QCOMPARE(pod.b, pod2.b);
2264 pod_as_variant.setValue(pod);
2265 pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2267 QCOMPARE(pod.a, pod2.a);
2268 QCOMPARE(pod.b, pod2.b);
2271 void tst_QVariant::basicUserType()
2276 v = QVariant(QMetaType::Int, &i);
2278 QCOMPARE(v.type(), QVariant::Int);
2279 QCOMPARE(v.toInt(), 7);
2283 v = QVariant(QMetaType::QString, &s);
2285 QCOMPARE(v.type(), QVariant::String);
2286 QCOMPARE(v.toString(), QString("foo"));
2290 v = QVariant(QMetaType::Double, &d);
2292 QCOMPARE(v.type(), QVariant::Double);
2293 QCOMPARE(v.toDouble(), 4.4);
2297 v = QVariant(QMetaType::Float, &f);
2299 QCOMPARE(v.userType(), int(QMetaType::Float));
2300 QCOMPARE(v.toDouble(), 4.5);
2303 QByteArray ba("bar");
2304 v = QVariant(QMetaType::QByteArray, &ba);
2306 QCOMPARE(v.type(), QVariant::ByteArray);
2307 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2310 void tst_QVariant::data()
2317 QVariant ll = (qlonglong)2;
2318 QVariant ull = (qulonglong)3;
2319 QVariant s(QString("hallo"));
2320 QVariant r(QRect(1,2,3,4));
2324 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2328 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2332 QCOMPARE(*static_cast<float *>(v.data()), qvariant_cast<float>(v));
2336 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2340 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2344 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2348 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2351 void tst_QVariant::constData()
2364 QVERIFY(v.constData());
2365 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2368 QVERIFY(v.constData());
2369 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2372 QVERIFY(v.constData());
2373 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2376 QVERIFY(v.constData());
2377 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2380 QVERIFY(v.constData());
2381 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2384 QVERIFY(v.constData());
2385 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2388 QVERIFY(v.constData());
2389 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2398 Q_DECLARE_METATYPE(Foo)
2400 void tst_QVariant::variant_to()
2409 sl << QLatin1String("blah");
2418 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2419 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2420 QCOMPARE(qvariant_cast<int>(v2), 5);
2421 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2422 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2424 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2427 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2429 QCOMPARE(qvariant_cast<int>(v1), 4);
2431 QVariant n = QVariant::fromValue<short>(42);
2432 QCOMPARE(qvariant_cast<int>(n), 42);
2433 QCOMPARE(qvariant_cast<uint>(n), 42u);
2434 QCOMPARE(qvariant_cast<double>(n), 42.0);
2435 QCOMPARE(qvariant_cast<float>(n), 42.f);
2436 QCOMPARE(qvariant_cast<short>(n), short(42));
2437 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2439 n = QVariant::fromValue(43l);
2440 QCOMPARE(qvariant_cast<int>(n), 43);
2441 QCOMPARE(qvariant_cast<uint>(n), 43u);
2442 QCOMPARE(qvariant_cast<double>(n), 43.0);
2443 QCOMPARE(qvariant_cast<float>(n), 43.f);
2444 QCOMPARE(qvariant_cast<long>(n), 43l);
2446 n = QLatin1String("44");
2447 QCOMPARE(qvariant_cast<int>(n), 44);
2448 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2449 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2451 QCOMPARE(QVariant::fromValue(0.25f).toDouble(), 0.25);
2454 struct Blah { int i; };
2456 QDataStream& operator>>(QDataStream& s, Blah& c)
2457 { return (s >> c.i); }
2459 QDataStream& operator<<(QDataStream& s, const Blah& c)
2460 { return (s << c.i); }
2462 void tst_QVariant::saveLoadCustomTypes()
2467 int tp = qRegisterMetaType<Blah>("Blah");
2468 QVariant v = QVariant(tp, &i);
2470 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2472 QCOMPARE(v.userType(), tp);
2473 QVERIFY(v.type() == QVariant::UserType);
2475 QDataStream stream(&data, QIODevice::WriteOnly);
2482 QDataStream stream(data);
2486 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2487 int value = *(int*)v.constData();
2488 QCOMPARE(value, 42);
2491 void tst_QVariant::url()
2493 QString str("http://qt.nokia.com");
2496 QVariant v(url); //built with a QUrl
2500 QVariant v3(str); //built with a QString
2502 QCOMPARE(v2.toUrl(), url);
2503 QVERIFY(v3.canConvert<QUrl>());
2504 QCOMPARE(v2.toUrl(), v3.toUrl());
2506 QVERIFY(v2.canConvert<QString>());
2507 QCOMPARE(v2.toString(), str);
2508 QCOMPARE(v3.toString(), str);
2511 void tst_QVariant::variantMap()
2513 QMap<QString, QVariant> map;
2517 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2519 QCOMPARE(map2.value("test").toInt(), 42);
2521 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2522 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2524 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2525 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2528 void tst_QVariant::variantHash()
2530 QHash<QString, QVariant> hash;
2534 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2536 QCOMPARE(hash2.value("test").toInt(), 42);
2538 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2539 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2541 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2542 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2545 void tst_QVariant::invalidQColor() const
2547 QVariant va("An invalid QColor::name() value.");
2548 QVERIFY(va.canConvert(QVariant::Color));
2550 QVERIFY(!va.convert(QVariant::Color));
2552 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2555 class CustomQObject : public QObject {
2558 CustomQObject(QObject *parent = 0) : QObject(parent) {}
2560 class CustomQWidget : public QWidget {
2563 CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
2565 Q_DECLARE_METATYPE(CustomQObject*)
2566 Q_DECLARE_METATYPE(CustomQWidget*)
2568 class CustomNonQObject { };
2569 Q_DECLARE_METATYPE(CustomNonQObject)
2570 Q_DECLARE_METATYPE(CustomNonQObject*)
2572 void tst_QVariant::cleanupTestCase()
2574 delete customNonQObjectPointer;
2575 qDeleteAll(objectPointerTestData);
2578 void tst_QVariant::qvariant_cast_QObject_data()
2580 QTest::addColumn<QVariant>("data");
2581 QTest::addColumn<bool>("success");
2582 QObject *obj = new QObject;
2583 obj->setObjectName(QString::fromLatin1("Hello"));
2584 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2585 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2586 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2587 QTest::newRow("from int") << QVariant((int) 123) << false;
2588 QWidget *widget = new QWidget;
2589 widget->setObjectName(QString::fromLatin1("Hello"));
2590 QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true;
2591 CustomQObject *customObject = new CustomQObject(this);
2592 customObject->setObjectName(QString::fromLatin1("Hello"));
2593 QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2594 CustomQWidget *customWidget = new CustomQWidget;
2595 customWidget->setObjectName(QString::fromLatin1("Hello"));
2596 QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true;
2597 QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2599 // Deleted in cleanupTestCase.
2600 customNonQObjectPointer = new CustomNonQObject;
2601 QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2603 // Deleted in cleanupTestCase.
2604 objectPointerTestData.push_back(obj);
2605 objectPointerTestData.push_back(widget);
2606 objectPointerTestData.push_back(customObject);
2607 objectPointerTestData.push_back(customWidget);
2610 void tst_QVariant::qvariant_cast_QObject()
2612 QFETCH(QVariant, data);
2613 QFETCH(bool, success);
2615 QObject *o = qvariant_cast<QObject *>(data);
2616 QCOMPARE(o != 0, success);
2618 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2622 class CustomQObjectDerived : public CustomQObject {
2625 CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2627 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2629 class CustomQObjectDerivedNoMetaType : public CustomQObject {
2632 CustomQObjectDerivedNoMetaType(QObject *parent = 0) : CustomQObject(parent) {}
2635 void tst_QVariant::qvariant_cast_QObject_derived()
2638 CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this);
2639 QVariant data = QVariant::fromValue(object);
2640 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>());
2641 QCOMPARE(data.value<QObject *>(), object);
2642 QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object);
2643 QCOMPARE(data.value<CustomQObject *>(), object);
2644 QVERIFY(data.value<CustomQWidget*>() == 0);
2647 CustomQObjectDerived *object = new CustomQObjectDerived(this);
2648 QVariant data = QVariant::fromValue(object);
2650 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2652 QCOMPARE(data.value<QObject *>(), object);
2653 QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2654 QCOMPARE(data.value<CustomQObject *>(), object);
2655 QVERIFY(data.value<CustomQWidget*>() == 0);
2658 CustomQWidget customWidget;
2659 QWidget *widget = &customWidget;
2660 QVariant data = QVariant::fromValue(widget);
2661 QVERIFY(data.userType() == QMetaType::QWidgetStar);
2663 QCOMPARE(data.value<QObject*>(), widget);
2664 QCOMPARE(data.value<QWidget*>(), widget);
2665 QCOMPARE(data.value<CustomQWidget*>(), widget);
2669 Q_DECLARE_METATYPE(qint8);
2671 void tst_QVariant::convertToQUint8() const
2675 const qint8 anInt = 32;
2677 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2678 * It's of type QVariant::Int. */
2679 const QVariant v0 = anInt;
2681 QVERIFY(v0.canConvert<qint8>());
2682 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2683 QCOMPARE(int(v0.toInt()), 32);
2684 QCOMPARE(v0.toString(), QString("32"));
2686 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2687 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2688 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2689 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2690 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2691 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2696 const quint8 anInt = 32;
2697 const QVariant v0 = anInt;
2699 QVERIFY(v0.canConvert<quint8>());
2700 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2701 QCOMPARE(int(v0.toUInt()), 32);
2702 QCOMPARE(v0.toString(), QString("32"));
2707 const qint16 anInt = 32;
2708 const QVariant v0 = anInt;
2710 QVERIFY(v0.canConvert<qint16>());
2711 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2712 QCOMPARE(int(v0.toInt()), 32);
2713 QCOMPARE(v0.toString(), QString("32"));
2718 const quint16 anInt = 32;
2719 const QVariant v0 = anInt;
2721 QVERIFY(v0.canConvert<quint16>());
2722 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2723 QCOMPARE(int(v0.toUInt()), 32);
2724 QCOMPARE(v0.toString(), QString("32"));
2728 void tst_QVariant::comparePointers() const
2736 QVariant v = QVariant::fromValue<void *>(&myClass);
2737 QVariant v2 = QVariant::fromValue<void *>(&myClass);
2743 Q_DECLARE_METATYPE(Data*)
2745 void tst_QVariant::voidStar() const
2752 v1 = QVariant::fromValue(p1);
2756 v2 = QVariant::fromValue(p2);
2760 v2 = QVariant::fromValue(p2);
2764 void tst_QVariant::dataStar() const
2766 qRegisterMetaType<Data*>();
2767 Data *p1 = new Data;
2769 QVariant v1 = QVariant::fromValue(p1);
2770 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2771 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2776 v2 = QVariant::fromValue(p1);
2781 void tst_QVariant::canConvertQStringList() const
2783 QFETCH(bool, canConvert);
2784 QFETCH(QStringList, input);
2785 QFETCH(QString, result);
2789 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2790 QCOMPARE(v.toString(), result);
2793 void tst_QVariant::canConvertQStringList_data() const
2795 QTest::addColumn<bool>("canConvert");
2796 QTest::addColumn<QStringList>("input");
2797 QTest::addColumn<QString>("result");
2799 QTest::newRow("An empty list") << false << QStringList() << QString();
2800 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2801 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2806 QTest::newRow("Two items") << false << l << QString();
2809 QTest::newRow("Three items") << false << l << QString();
2812 template<typename T> void convertMetaType()
2814 QVERIFY(QVariant::fromValue<T>(10).isValid());
2815 QVERIFY(QVariant::fromValue<T>(10).canConvert(QVariant::Int));
2816 QCOMPARE(QVariant::fromValue<T>(10).toInt(), 10);
2817 QCOMPARE(QVariant::fromValue<T>(10), QVariant::fromValue<T>(10));
2820 #define CONVERT_META_TYPE(Type) \
2821 convertMetaType<Type>(); \
2822 if (QTest::currentTestFailed()) \
2823 QFAIL("convertMetaType<" #Type "> failed");
2825 void tst_QVariant::canConvertMetaTypeToInt() const
2827 CONVERT_META_TYPE(long);
2828 CONVERT_META_TYPE(short);
2829 CONVERT_META_TYPE(short);
2830 CONVERT_META_TYPE(unsigned short);
2831 CONVERT_META_TYPE(ushort);
2832 CONVERT_META_TYPE(ulong);
2833 CONVERT_META_TYPE(unsigned long);
2834 CONVERT_META_TYPE(uchar);
2835 CONVERT_META_TYPE(unsigned char);
2836 CONVERT_META_TYPE(char);
2837 CONVERT_META_TYPE(uint);
2838 CONVERT_META_TYPE(unsigned int);
2841 #undef CONVERT_META_TYPE
2844 These calls should not produce any warnings.
2846 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2849 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2850 const QDateTime dateTime(variant.toDateTime());
2851 QVERIFY(!dateTime.isValid());
2855 QVariant v1(QLatin1String("xyz"));
2856 v1.convert(QVariant::DateTime);
2858 QVariant v2(QLatin1String("xyz"));
2859 QDateTime dt1(v2.toDateTime());
2861 const QVariant v3(QLatin1String("xyz"));
2862 const QDateTime dt2(v3.toDateTime());
2866 void tst_QVariant::invalidDateTime() const
2868 QVariant variant(QString::fromLatin1("Invalid date time string"));
2869 QVERIFY(!variant.toDateTime().isValid());
2870 QVERIFY(!variant.convert(QVariant::DateTime));
2875 MyClass() : myValue(0) {}
2879 Q_DECLARE_METATYPE( MyClass )
2881 void tst_QVariant::loadUnknownUserType()
2883 qRegisterMetaType<MyClass>("MyClass");
2884 QTest::ignoreMessage(QtWarningMsg, "QVariant::load: unable to load type "
2885 + QByteArray::number(qMetaTypeId<MyClass>()) +".");
2886 char data[] = {0, 0, QMetaType::User >> 8 , char(QMetaType::User), 0, 0, 0, 0, 8, 'M', 'y', 'C', 'l', 'a', 's', 's', 0};
2888 QByteArray ba(data, sizeof(data));
2889 QDataStream ds(&ba, QIODevice::ReadOnly);
2892 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2895 void tst_QVariant::loadBrokenUserType()
2897 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 127");
2898 char data[] = {0, 0, 0, 127, 0 };
2900 QByteArray ba(data, sizeof(data));
2901 QDataStream ds(&ba, QIODevice::ReadOnly);
2904 QCOMPARE(ds.status(), QDataStream::Ok);
2907 void tst_QVariant::invalidDate() const
2909 QString foo("Hello");
2910 QVariant variant(foo);
2911 QVERIFY(!variant.convert(QVariant::Date));
2914 QVERIFY(!variant.convert(QVariant::DateTime));
2917 QVERIFY(!variant.convert(QVariant::Time));
2920 QVERIFY(!variant.convert(QVariant::Int));
2923 QVERIFY(!variant.convert(QVariant::Double));
2926 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2931 int x,y,z,q,w,e,r,t;
2933 Q_DECLARE_METATYPE(WontCompare);
2935 void tst_QVariant::compareCustomTypes() const
2937 qRegisterMetaType<WontCompare>("WontCompare");
2941 const QVariant variant1(QVariant::fromValue(f1));
2945 const QVariant variant2(QVariant::fromValue(f2));
2947 /* We compare pointers. */
2948 QVERIFY(variant1 != variant2);
2949 QVERIFY(variant1 == variant1);
2950 QVERIFY(variant2 == variant2);
2953 void tst_QVariant::timeToDateTime() const
2955 const QVariant val(QTime::currentTime());
2956 QVERIFY(!val.canConvert(QVariant::DateTime));
2957 QVERIFY(!val.toDateTime().isValid());
2960 Q_DECLARE_METATYPE(QHostAddress)
2962 void tst_QVariant::copyingUserTypes() const
2966 const QHostAddress ha("127.0.0.1");
2970 QCOMPARE(qvariant_cast<QHostAddress>(var3), ha);
2973 void tst_QVariant::convertBoolToByteArray() const
2975 QFETCH(QByteArray, input);
2976 QFETCH(bool, canConvert);
2977 QFETCH(bool, value);
2979 const QVariant variant(input);
2981 QCOMPARE(variant.canConvert<bool>(), canConvert);
2984 /* Just call this function so we run the code path. */
2985 QCOMPARE(variant.toBool(), value);
2989 void tst_QVariant::convertBoolToByteArray_data() const
2991 QTest::addColumn<QByteArray>("input");
2992 QTest::addColumn<bool>("canConvert");
2993 QTest::addColumn<bool>("value");
2995 QTest::newRow("false")
2996 << QByteArray("false")
3000 QTest::newRow("FALSE")
3001 << QByteArray("FALSE")
3005 QTest::newRow("falSE")
3006 << QByteArray("FALSE")
3015 QTest::newRow("null QByteArray")
3020 QTest::newRow("any-content")
3021 << QByteArray("any-content")
3025 QTest::newRow("true")
3026 << QByteArray("true")
3030 QTest::newRow("TRUE")
3031 << QByteArray("TRUE")
3035 QTest::newRow("trUE")
3036 << QByteArray("trUE")
3041 void tst_QVariant::convertByteArrayToBool() const
3043 QFETCH(bool, input);
3044 QFETCH(QByteArray, output);
3046 const QVariant variant(input);
3047 QCOMPARE(variant.type(), QVariant::Bool);
3048 QCOMPARE(variant.toBool(), input);
3049 QVERIFY(variant.canConvert<bool>());
3051 QCOMPARE(variant.toByteArray(), output);
3054 void tst_QVariant::convertByteArrayToBool_data() const
3056 QTest::addColumn<bool>("input");
3057 QTest::addColumn<QByteArray>("output");
3059 QTest::newRow("false")
3061 << QByteArray("false");
3063 QTest::newRow("true")
3065 << QByteArray("true");
3070 1. Converting the string "9.9" to int fails. This is the behavior of
3071 toLongLong() and hence also QVariant, since it uses it.
3072 2. Converting the QVariant containing the double 9.9 to int works.
3074 Rationale: "9.9" is not a valid int. However, doubles are by definition not
3075 ints and therefore it makes more sense to perform conversion for those.
3077 void tst_QVariant::toIntFromQString() const
3079 QVariant first("9.9");
3081 QCOMPARE(first.toInt(&ok), 0);
3084 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
3088 QCOMPARE(v.toInt(&ok), 10);
3094 1. Conversion from (64 bit) double to int works (no overflow).
3095 2. Same conversion works for QVariant::convert.
3097 Rationale: if 2147483630 is set in float and then converted to int,
3098 there will be overflow and the result will be -2147483648.
3100 void tst_QVariant::toIntFromDouble() const
3102 double d = 2147483630; // max int 2147483647
3103 QVERIFY((int)d == 2147483630);
3106 QVERIFY( var.canConvert( QVariant::Int ) );
3109 int result = var.toInt(&ok);
3111 QVERIFY( ok == true );
3112 QCOMPARE(result, 2147483630);
3115 void tst_QVariant::setValue()
3117 QTransform t; //we just take a value so that we're sure that it will be shared
3119 QVERIFY( v1.isDetached() );
3121 QVERIFY( !v1.isDetached() );
3122 QVERIFY( !v2.isDetached() );
3124 v2.setValue(3); //set an integer value
3126 QVERIFY( v1.isDetached() );
3127 QVERIFY( v2.isDetached() );
3130 void tst_QVariant::numericalConvert()
3132 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3133 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3135 QVariant vfloat(float(5.3));
3136 QVariant vdouble(double(5.3));
3137 QVariant vreal(qreal(5.3));
3138 QVariant vint(int(5));
3139 QVariant vuint(uint(5));
3140 QVariant vshort(short(5));
3141 QVariant vlonglong(quint64(5));
3142 QVariant vstringint(QString::fromLatin1("5"));
3143 QVariant vstring(QString::fromLatin1("5.3"));
3145 QVector<QVariant *> vect;
3146 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3148 for(int i = 0; i < vect.size(); i++) {
3150 if (i >= 3 && i <= 7)
3152 QVariant *v = vect.at(i);
3153 QCOMPARE(v->toFloat() , float(num));
3154 QCOMPARE(float(v->toReal()) , float(num));
3155 QCOMPARE(float(v->toDouble()) , float(num));
3157 QCOMPARE(v->toInt() , int(num));
3158 QCOMPARE(v->toUInt() , uint(num));
3159 QCOMPARE(v->toULongLong() , quint64(num));
3161 QCOMPARE(v->toString() , QString::number(num));
3166 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3168 QVariant v = QVariant::fromValue(orig);
3169 QVERIFY(v.isValid());
3170 QCOMPARE(v.isNull(), isNull);
3171 QCOMPARE(v.toString(), toString);
3172 QCOMPARE(v.toDouble(), toDouble);
3173 QCOMPARE(v.toBool(), toBool);
3174 QCOMPARE(qvariant_cast<T>(v), orig);
3178 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3179 // Type is movable so standard comparison algorithm in QVariant should work
3180 // In a custom type QVariant is not aware of ==operator so it won't be called,
3181 // which may cause problems especially visible when using a not-movable type
3184 QVERIFY(v2.isValid());
3185 QCOMPARE(v2.isNull(), isNull);
3186 QCOMPARE(v2.toString(), toString);
3187 QCOMPARE(v2.toDouble(), toDouble);
3188 QCOMPARE(v2.toBool(), toBool);
3189 QCOMPARE(qvariant_cast<T>(v2), orig);
3195 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3196 // Type is movable so standard comparison algorithm in QVariant should work
3197 // In a custom type QVariant is not aware of ==operator so it won't be called,
3198 // which may cause problems especially visible when using a not-movable type
3201 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3202 QCOMPARE(v2.toString(), toString);
3203 v3 = QVariant::fromValue(orig);
3205 QVERIFY(v3.isValid());
3206 QCOMPARE(v3.isNull(), isNull);
3207 QCOMPARE(v3.toString(), toString);
3208 QCOMPARE(v3.toDouble(), toDouble);
3209 QCOMPARE(v3.toBool(), toBool);
3210 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3213 QVERIFY(v.isValid());
3214 QCOMPARE(v.isNull(), isNull);
3215 QCOMPARE(v.toString(), toString);
3216 QCOMPARE(v.toDouble(), toDouble);
3217 QCOMPARE(v.toBool(), toBool);
3218 QCOMPARE(qvariant_cast<T>(v), orig);
3220 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3221 QCOMPARE(v.userType(), qMetaTypeId<T>());
3222 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3226 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3227 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3228 if (QTest::currentTestFailed())\
3229 QFAIL("playWithVariant failed");
3234 bool operator==(const MyPrimitive &o) const
3236 return x == o.x && y == o.y;
3241 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3247 MyData() : ptr(this) {}
3250 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3252 MyData(const MyData& o) : ptr(this)
3254 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3256 MyData &operator=(const MyData &o)
3258 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3259 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3262 bool operator==(const MyData &o) const
3264 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3265 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3274 MyMovable() { v = count++; }
3275 ~MyMovable() { count--; }
3276 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3278 bool operator==(const MyMovable &o) const
3284 int MyMovable::count = 0;
3290 MyNotMovable() : that(this) { count++; }
3291 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3292 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3293 MyNotMovable &operator=(const MyNotMovable &o) {
3294 bool ok = that == this && o.that == &o;
3295 if (!ok) qFatal("MyNotMovable has been moved");
3299 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3300 // so it would be fine just to always return true
3301 bool operator==(const MyNotMovable &o) const
3303 bool ok = that == this && o.that == &o;
3304 if (!ok) qFatal("MyNotMovable has been moved");
3309 int MyNotMovable::count = 0;
3311 struct MyShared : QSharedData {
3316 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3319 Q_DECLARE_METATYPE(QList<QSize>)
3320 Q_DECLARE_METATYPE(MyPrimitive)
3321 Q_DECLARE_METATYPE(MyData)
3322 Q_DECLARE_METATYPE(MyMovable)
3323 Q_DECLARE_METATYPE(MyNotMovable)
3324 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3325 Q_DECLARE_METATYPE(QList<MyData>)
3326 Q_DECLARE_METATYPE(QList<MyMovable>)
3327 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3328 Q_DECLARE_METATYPE(MyPrimitive *)
3329 Q_DECLARE_METATYPE(MyData *)
3330 Q_DECLARE_METATYPE(MyMovable *)
3331 Q_DECLARE_METATYPE(MyNotMovable *)
3332 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3335 void tst_QVariant::moreCustomTypes()
3338 QList<QSize> listSize;
3339 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3340 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3341 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3346 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3347 str = QString::fromLatin1("123456789.123");
3348 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3353 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3354 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3359 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3360 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3362 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3363 l << MyData() << MyData();
3364 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3368 MyPrimitive d = { 4, 5 };
3369 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3370 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3371 QList<MyPrimitive> l;
3372 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3374 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3379 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3380 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3382 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3383 l << MyMovable() << d;
3384 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3386 QCOMPARE(MyMovable::count, 0);
3388 QCOMPARE(MyNotMovable::count, 0);
3391 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3392 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3393 QList<MyNotMovable> l;
3394 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3395 l << MyNotMovable() << d;
3396 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3398 QCOMPARE(MyNotMovable::count, 0);
3401 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3402 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3403 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3404 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3405 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3406 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3407 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3408 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3409 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3410 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3411 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3412 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3413 PLAY_WITH_VARIANT( qint8(-12), false, QLatin1String("\xf4"), -12, true); // qint8 is char, so it gets converted via QChar
3414 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3415 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3416 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3417 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3418 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3419 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3421 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3426 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3427 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3431 QVariant v1 = QVariant::fromValue(5);
3432 QVariant v2 = QVariant::fromValue(5.0);
3433 QVariant v3 = QVariant::fromValue(quint16(5));
3435 QVariant v5 = QVariant::fromValue(MyPrimitive());
3436 QVariant v6 = QVariant::fromValue(MyMovable());
3437 QVariant v7 = QVariant::fromValue(MyData());
3438 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3439 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3440 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3441 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3443 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3446 QCOMPARE(MyMovable::count, 0);
3448 QSharedDataPointer<MyShared> d(new MyShared);
3449 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3451 QCOMPARE(MyMovable::count, 0);
3454 QList<QList<int> > data;
3455 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3456 data << (QList<int>() << 42);
3457 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3461 QList<QVector<int> > data;
3462 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3463 data << (QVector<int>() << 42);
3464 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3468 QList<QSet<int> > data;
3469 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3470 data << (QSet<int>() << 42);
3471 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3475 QList<QLinkedList<int> > data;
3476 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3477 data << (QLinkedList<int>() << 42);
3478 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3482 void tst_QVariant::movabilityTest()
3484 // This test checks if QVariant is movable even if an internal data is not movable.
3485 QVERIFY(!MyNotMovable::count);
3487 QVariant variant = QVariant::fromValue(MyNotMovable());
3488 QVERIFY(MyNotMovable::count);
3490 // prepare destination memory space to which variant will be moved
3492 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3493 buffer[0].~QVariant();
3495 memcpy(buffer, &variant, sizeof(QVariant));
3496 QCOMPARE(buffer[0].type(), QVariant::UserType);
3497 QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3498 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3500 new (&variant) QVariant();
3502 QVERIFY(!MyNotMovable::count);
3505 void tst_QVariant::variantInVariant()
3508 QCOMPARE(var1.type(), QVariant::Int);
3509 QVariant var2 = var1;
3510 QCOMPARE(var2, var1);
3511 QCOMPARE(var2.type(), QVariant::Int);
3512 QVariant var3 = QVariant::fromValue(var1);
3513 QCOMPARE(var3, var1);
3514 QCOMPARE(var3.type(), QVariant::Int);
3515 QVariant var4 = qvariant_cast<QVariant>(var1);
3516 QCOMPARE(var4, var1);
3517 QCOMPARE(var4.type(), QVariant::Int);
3520 QCOMPARE(var5, var1);
3521 QCOMPARE(var5.type(), QVariant::Int);
3523 var6.setValue(var1);
3524 QCOMPARE(var6, var1);
3525 QCOMPARE(var6.type(), QVariant::Int);
3527 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3528 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3529 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3531 QString str("hello");
3532 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3533 QCOMPARE((int)var8.type(), (int)QVariant::String);
3534 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3536 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3537 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3538 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3541 void tst_QVariant::colorInteger()
3543 QVariant v = QColor(Qt::red);
3544 QCOMPARE(v.type(), QVariant::Color);
3545 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3548 QCOMPARE(v.type(), QVariant::Int);
3549 QCOMPARE(v.toInt(), 1000);
3551 v.setValue(QColor(Qt::yellow));
3552 QCOMPARE(v.type(), QVariant::Color);
3553 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3557 Q_DECLARE_OPAQUE_POINTER(Forward*)
3558 Q_DECLARE_METATYPE(Forward*)
3560 void tst_QVariant::forwardDeclare()
3563 QVariant v = QVariant::fromValue(f);
3564 QCOMPARE(qvariant_cast<Forward*>(v), f);
3567 void tst_QVariant::loadQt5Stream_data()
3569 dataStream_data(QDataStream::Qt_5_0);
3572 void tst_QVariant::loadQt5Stream()
3574 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3577 void tst_QVariant::saveQt5Stream_data()
3579 dataStream_data(QDataStream::Qt_5_0);
3582 void tst_QVariant::saveQt5Stream()
3584 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3587 void tst_QVariant::loadQt4Stream_data()
3589 dataStream_data(QDataStream::Qt_4_9);
3592 void tst_QVariant::loadQt4Stream()
3594 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3597 void tst_QVariant::saveQt4Stream_data()
3599 dataStream_data(QDataStream::Qt_4_9);
3602 void tst_QVariant::saveQt4Stream()
3604 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3607 void tst_QVariant::dataStream_data(QDataStream::Version version)
3609 QTest::addColumn<QString>("fileName");
3613 case QDataStream::Qt_4_9:
3614 path = QString::fromLatin1("qt4.9");
3616 case QDataStream::Qt_5_0:
3617 path = QString::fromLatin1("qt5.0");
3623 path = path.prepend(":/stream/").append("/");
3626 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3627 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3633 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3635 QFETCH(QString, fileName);
3637 QFile file(fileName);
3638 QVERIFY(file.open(QIODevice::ReadOnly));
3640 QDataStream stream(&file);
3641 stream.setVersion(version);
3644 QVariant loadedVariant;
3645 stream >> typeName >> loadedVariant;
3647 const int id = QMetaType::type(typeName.toLatin1());
3648 if (id == QMetaType::Void) {
3649 // Void type is not supported by QVariant
3653 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3654 QCOMPARE(constructedVariant.userType(), id);
3655 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3656 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3659 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3661 QFETCH(QString, fileName);
3663 QFile file(fileName);
3664 QVERIFY(file.open(QIODevice::ReadOnly));
3665 QDataStream dataFileStream(&file);
3668 dataFileStream >> typeName;
3669 QByteArray data = file.readAll();
3670 const int id = QMetaType::type(typeName.toLatin1());
3671 if (id == QMetaType::Void) {
3672 // Void type is not supported by QVariant
3677 buffer.open(QIODevice::ReadWrite);
3678 QDataStream stream(&buffer);
3679 stream.setVersion(version);
3681 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3682 QCOMPARE(constructedVariant.userType(), id);
3683 stream << constructedVariant;
3685 // We are testing QVariant there is no point in testing full array.
3686 QCOMPARE(buffer.data().left(5), data.left(5));
3689 QVariant recunstructedVariant;
3690 stream >> recunstructedVariant;
3691 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3694 class MessageHandler {
3696 MessageHandler(const int typeId, QtMsgHandler msgHandler = handler)
3697 : oldMsgHandler(qInstallMsgHandler(msgHandler))
3704 qInstallMsgHandler(oldMsgHandler);
3707 bool testPassed() const
3712 static void handler(QtMsgType, const char *txt)
3714 QString msg = QString::fromLatin1(txt);
3715 // Format itself is not important, but basic data as a type name should be included in the output
3716 ok = msg.startsWith("QVariant(");
3717 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3718 ok &= (currentId == QMetaType::UnknownType
3719 ? msg.contains("Invalid")
3720 : msg.contains(QMetaType::typeName(currentId)));
3721 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3722 if (currentId == QMetaType::Char || currentId == QMetaType::QChar) {
3723 // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length
3726 if (QMetaType::typeFlags(currentId) & QMetaType::PointerToQObject) {
3727 QByteArray currentName = QMetaType::typeName(currentId);
3728 currentName.chop(1);
3729 ok &= (msg.contains(", " + currentName) || msg.contains(", 0x0"));
3731 ok &= msg.endsWith(") ");
3732 QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData());
3736 QtMsgHandler oldMsgHandler;
3737 static int currentId;
3740 bool MessageHandler::ok;
3741 int MessageHandler::currentId;
3743 void tst_QVariant::debugStream_data()
3745 QTest::addColumn<QVariant>("variant");
3746 QTest::addColumn<int>("typeId");
3747 for (int id = 0; id < QMetaType::User; ++id) {
3748 const char *tagName = QMetaType::typeName(id);
3751 if (id != QMetaType::Void) {
3752 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3755 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3756 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3757 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3758 QTest::newRow("InvalidVariant") << QVariant() << int(QMetaType::UnknownType);
3759 QTest::newRow("CustomQObject") << QVariant::fromValue(this) << qMetaTypeId<tst_QVariant*>();
3762 void tst_QVariant::debugStream()
3764 QFETCH(QVariant, variant);
3765 QFETCH(int, typeId);
3767 MessageHandler msgHandler(typeId);
3768 qDebug() << variant;
3769 QVERIFY(msgHandler.testPassed());
3772 struct MessageHandlerType : public MessageHandler
3774 MessageHandlerType(const int typeId)
3775 : MessageHandler(typeId, handler)
3777 static void handler(QtMsgType, const char *txt)
3779 QString msg = QString::fromLatin1(txt);
3780 // Format itself is not important, but basic data as a type name should be included in the output
3781 ok = msg.startsWith("QVariant::");
3782 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3783 ok &= (currentId == QMetaType::UnknownType
3784 ? msg.contains("Invalid")
3785 : msg.contains(QMetaType::typeName(currentId)));
3786 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3790 void tst_QVariant::debugStreamType_data()
3795 void tst_QVariant::debugStreamType()
3797 QFETCH(QVariant, variant);
3798 QFETCH(int, typeId);
3800 MessageHandlerType msgHandler(typeId);
3801 qDebug() << QVariant::Type(typeId);
3802 QVERIFY(msgHandler.testPassed());
3805 void tst_QVariant::guiVariantAtExit()
3807 // crash test, it should not crash at QGuiApplication exit
3808 static QVariant cursor = QCursor();
3809 static QVariant point = QPoint();
3810 static QVariant image = QImage();
3811 static QVariant pallete = QPalette();
3819 void tst_QVariant::widgetsVariantAtExit()
3821 // crash test, it should not crash at QGuiApplication exit
3822 static QVariant icon= QIcon();
3823 static QVariant sizePolicy = QSizePolicy();
3825 Q_UNUSED(sizePolicy);
3829 QTEST_MAIN(tst_QVariant)
3830 #include "tst_qvariant.moc"