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 class CustomNonQObject;
73 class tst_QVariant : public QObject
78 tst_QVariant(QObject *parent = 0)
79 : QObject(parent), customNonQObjectPointer(0)
85 void cleanupTestCase();
88 void copy_constructor();
89 void constructor_invalid_data();
90 void constructor_invalid();
94 void canConvert_data();
127 void toLongLong_data();
130 void toULongLong_data();
133 void toByteArray_data();
136 void toString_data();
145 void toDateTime_data();
148 void toDouble_data();
151 void toPointF_data();
157 void toKeySequence_data();
158 void toKeySequence();
166 void toPixmap_data();
175 void qvariant_cast_QObject_data();
176 void qvariant_cast_QObject();
177 void qvariant_cast_QObject_derived();
182 void toRegularExpression();
197 void basicUserType();
201 void writeToReadFromDataStream_data();
202 void writeToReadFromDataStream();
203 void writeToReadFromOldDataStream();
204 void checkDataStream();
206 void operator_eq_eq_data();
207 void operator_eq_eq();
209 void operator_eq_eq_rhs();
211 void typeName_data();
215 void streamInvalidVariant();
222 void saveLoadCustomTypes();
227 void convertToQUint8() const;
228 void invalidQColor() const;
229 void comparePointers() const;
230 void voidStar() const;
231 void dataStar() const;
232 void canConvertQStringList() const;
233 void canConvertQStringList_data() const;
234 void canConvertMetaTypeToInt() const;
235 void variantToDateTimeWithoutWarnings() const;
236 void invalidDateTime() const;
238 void loadUnknownUserType();
239 void loadBrokenUserType();
241 void invalidDate() const;
242 void compareCustomTypes() const;
243 void timeToDateTime() const;
244 void copyingUserTypes() const;
245 void convertBoolToByteArray() const;
246 void convertBoolToByteArray_data() const;
247 void convertByteArrayToBool() const;
248 void convertByteArrayToBool_data() const;
249 void toIntFromQString() const;
250 void toIntFromDouble() const;
253 void numericalConvert();
254 void moreCustomTypes();
255 void movabilityTest();
256 void variantInVariant();
260 void forwardDeclare();
261 void debugStream_data();
263 void debugStreamType_data();
264 void debugStreamType();
266 void loadQt4Stream_data();
267 void loadQt4Stream();
268 void saveQt4Stream_data();
269 void saveQt4Stream();
270 void loadQt5Stream_data();
271 void loadQt5Stream();
272 void saveQt5Stream_data();
273 void saveQt5Stream();
275 void guiVariantAtExit();
276 void widgetsVariantAtExit();
278 void dataStream_data(QDataStream::Version version);
279 void loadQVariantFromDataStream(QDataStream::Version version);
280 void saveQVariantFromDataStream(QDataStream::Version version);
282 CustomNonQObject *customNonQObjectPointer;
283 QVector<QObject*> objectPointerTestData;
286 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
287 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
289 void tst_QVariant::constructor()
292 QVERIFY( !variant.isValid() );
293 QVERIFY( variant.isNull() );
295 QVariant var2(variant);
296 QVERIFY( !var2.isValid() );
297 QVERIFY( variant.isNull() );
299 QVariant varll(intMax1);
300 QVariant varll2(varll);
301 QCOMPARE(varll2, varll);
303 QVariant var3(QVariant::String);
304 QCOMPARE(var3.typeName(), "QString");
305 QVERIFY(var3.isNull());
306 QVERIFY(var3.isValid());
308 QVariant var4(QVariant::Invalid);
309 QCOMPARE(var4.type(), QVariant::Invalid);
310 QVERIFY(var4.isNull());
311 QVERIFY(!var4.isValid());
313 QVariant var5(QLatin1String("hallo"));
314 QCOMPARE(var5.type(), QVariant::String);
315 QCOMPARE(var5.typeName(), "QString");
317 QVariant var6(qlonglong(0));
318 QCOMPARE(var6.type(), QVariant::LongLong);
319 QCOMPARE(var6.typeName(), "qlonglong");
322 QVERIFY(var7.isValid());
323 QVERIFY(!var7.isNull());
325 var8.setValue<int>(5);
326 QVERIFY(var8.isValid());
327 QVERIFY(!var8.isNull());
330 void tst_QVariant::constructor_invalid_data()
332 QTest::addColumn<uint>("typeId");
334 QTest::newRow("-1") << uint(-1);
335 QTest::newRow("-122234567") << uint(-122234567);
336 QTest::newRow("0xfffffffff") << uint(0xfffffffff);
337 QTest::newRow("LastCoreType + 1") << uint(QMetaType::LastCoreType + 1);
338 QVERIFY(!QMetaType::isRegistered(QMetaType::LastCoreType + 1));
339 QTest::newRow("LastGuiType + 1") << uint(QMetaType::LastGuiType + 1);
340 QVERIFY(!QMetaType::isRegistered(QMetaType::LastGuiType + 1));
341 QTest::newRow("LastWidgetsType + 1") << uint(QMetaType::LastWidgetsType + 1);
342 QVERIFY(!QMetaType::isRegistered(QMetaType::LastWidgetsType + 1));
345 struct MessageHandlerInvalidType
347 MessageHandlerInvalidType()
348 : oldMsgHandler(qInstallMsgHandler(handler))
353 ~MessageHandlerInvalidType()
355 qInstallMsgHandler(oldMsgHandler);
358 QtMsgHandler oldMsgHandler;
360 static void handler(QtMsgType type, const char *txt)
363 QString msg = QString::fromLatin1(txt);
364 // uint(-1) can be platform dependent so we check only beginning of the message.
365 ok = msg.startsWith("Trying to construct an instance of an invalid type, type id:");
366 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
370 bool MessageHandlerInvalidType::ok;
372 void tst_QVariant::constructor_invalid()
375 QFETCH(uint, typeId);
377 MessageHandlerInvalidType msg;
378 QVariant variant(static_cast<QVariant::Type>(typeId));
379 QVERIFY(!variant.isValid());
380 QVERIFY(variant.userType() == QMetaType::UnknownType);
384 MessageHandlerInvalidType msg;
385 QVariant variant(typeId, /* copy */ 0);
386 QVERIFY(!variant.isValid());
387 QVERIFY(variant.userType() == QMetaType::UnknownType);
392 void tst_QVariant::copy_constructor()
394 QVariant var7(QVariant::Int);
396 QCOMPARE(var8.type(), QVariant::Int);
397 QVERIFY(var8.isNull());
400 void tst_QVariant::isNull()
403 QVERIFY( var.isNull() );
406 QVariant var1( str1 );
407 QVERIFY( var1.isNull() );
409 QVariant var2( QString::null );
410 QVERIFY( var2.isNull() );
412 QVariant var3( QString( "blah" ) );
413 QVERIFY( !var3.isNull() );
416 QVERIFY( !var4.isNull() );
418 QVariant var5 = QString();
419 QVERIFY( var5.isNull() );
421 QVariant var6( QString( "blah" ) );
422 QVERIFY( !var6.isNull() );
424 QVERIFY( var6.isNull() );
425 var6.convert( QVariant::String );
426 QVERIFY( var6.isNull() );
427 QVariant varLL( (qlonglong)0 );
428 QVERIFY( !varLL.isNull() );
429 QVariant var7(QString::null);
430 QVERIFY(var7.isNull());
433 void tst_QVariant::swap()
435 QVariant v1 = 1, v2 = 2.0;
437 QCOMPARE(v1.type(),QVariant::Double);
438 QCOMPARE(v1.toDouble(),2.0);
439 QCOMPARE(v2.type(),QVariant::Int);
440 QCOMPARE(v2.toInt(),1);
443 void tst_QVariant::canConvert_data()
445 QTest::addColumn<QVariant>("val");
446 QTest::addColumn<bool>("BitArrayCast");
447 QTest::addColumn<bool>("BitmapCast");
448 QTest::addColumn<bool>("BoolCast");
449 QTest::addColumn<bool>("BrushCast");
450 QTest::addColumn<bool>("ByteArrayCast");
451 QTest::addColumn<bool>("ColorCast");
452 QTest::addColumn<bool>("CursorCast");
453 QTest::addColumn<bool>("DateCast");
454 QTest::addColumn<bool>("DateTimeCast");
455 QTest::addColumn<bool>("DoubleCast");
456 QTest::addColumn<bool>("FontCast");
457 QTest::addColumn<bool>("ImageCast");
458 QTest::addColumn<bool>("IntCast");
459 QTest::addColumn<bool>("InvalidCast");
460 QTest::addColumn<bool>("KeySequenceCast");
461 QTest::addColumn<bool>("ListCast");
462 QTest::addColumn<bool>("LongLongCast");
463 QTest::addColumn<bool>("MapCast");
464 QTest::addColumn<bool>("PaletteCast");
465 QTest::addColumn<bool>("PenCast");
466 QTest::addColumn<bool>("PixmapCast");
467 QTest::addColumn<bool>("PointCast");
468 QTest::addColumn<bool>("RectCast");
469 QTest::addColumn<bool>("RegionCast");
470 QTest::addColumn<bool>("SizeCast");
471 QTest::addColumn<bool>("SizePolicyCast");
472 QTest::addColumn<bool>("StringCast");
473 QTest::addColumn<bool>("StringListCast");
474 QTest::addColumn<bool>("TimeCast");
475 QTest::addColumn<bool>("UIntCast");
476 QTest::addColumn<bool>("ULongLongCast");
487 // 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
490 QVariant var(QBitArray(0));
491 QTest::newRow("BitArray")
492 << 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;
493 var = QVariant::fromValue(QBitmap());
494 QTest::newRow("Bitmap")
495 << 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;
496 var = QVariant::fromValue(QBrush());
497 QTest::newRow("Brush")
498 << 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;
499 var = QVariant(QByteArray());
500 QTest::newRow("ByteArray")
501 << 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;
502 var = QVariant::fromValue(QColor());
503 QTest::newRow("Color")
504 << 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;
506 var = QVariant::fromValue(QCursor());
507 QTest::newRow("Cursor")
508 << 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;
510 var = QVariant(QDate());
511 QTest::newRow("Date")
512 << 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;
513 var = QVariant(QDateTime());
514 QTest::newRow("DateTime")
515 << 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;
516 var = QVariant((double)0.1);
517 QTest::newRow("Double")
518 << 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;
519 var = QVariant(0.1f);
520 QTest::newRow("Float")
521 << 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;
522 var = QVariant::fromValue(QFont());
523 QTest::newRow("Font")
524 << 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;
525 var = QVariant::fromValue(QIcon());
526 QTest::newRow("Icon")
527 << 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;
528 var = QVariant::fromValue(QImage());
529 QTest::newRow("Image")
530 << 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;
531 var = QVariant((int)1);
533 << 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;
535 QTest::newRow("Invalid")
536 << 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;
537 var = QVariant::fromValue(QKeySequence());
538 QTest::newRow("KeySequence")
539 << 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;
540 var = QVariant(QList<QVariant>());
541 QTest::newRow("List")
542 << 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;
543 var = QVariant((qlonglong)1);
544 QTest::newRow("LongLong")
545 << 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;
546 var = QVariant(QMap<QString,QVariant>());
548 << 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;
549 var = QVariant::fromValue(QPalette());
550 QTest::newRow("Palette")
551 << 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;
552 var = QVariant::fromValue(QPen());
554 << 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;
555 var = QVariant::fromValue(QPixmap());
556 QTest::newRow("Pixmap")
557 << 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;
558 var = QVariant::fromValue(QPolygon());
559 QTest::newRow("PointArray")
560 << 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;
561 var = QVariant(QPoint());
562 QTest::newRow("Point")
563 << 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;
564 var = QVariant(QRect());
565 QTest::newRow("Rect")
566 << 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;
567 var = QVariant::fromValue(QRegion());
568 QTest::newRow("Region")
569 << 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;
570 var = QVariant(QSize());
571 QTest::newRow("Size")
572 << 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;
573 var = QVariant::fromValue(QSizePolicy());
574 QTest::newRow("SizePolicy")
575 << 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;
576 var = QVariant(QString());
577 QTest::newRow("String")
578 << 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;
579 var = QVariant(QStringList("entry"));
580 QTest::newRow("StringList")
581 << 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;
582 var = QVariant(QTime());
583 QTest::newRow("Time")
584 << 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;
585 var = QVariant((uint)1);
586 QTest::newRow("UInt")
587 << 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;
588 var = QVariant((int)1);
590 << 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;
591 var = QVariant((qulonglong)1);
592 QTest::newRow("ULongLong")
593 << 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;
594 var = QVariant::fromValue('a');
595 QTest::newRow("Char")
596 << 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;
597 var = QVariant::fromValue<signed char>(-1);
598 QTest::newRow("SChar")
599 << 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;
605 void tst_QVariant::canConvert()
607 QFETCH(QVariant, val);
608 QFETCH(bool, BitArrayCast);
609 QFETCH(bool, BitmapCast);
610 QFETCH(bool, BoolCast);
611 QFETCH(bool, BrushCast);
612 QFETCH(bool, ByteArrayCast);
613 QFETCH(bool, ColorCast);
614 QFETCH(bool, CursorCast);
615 QFETCH(bool, DateCast);
616 QFETCH(bool, DateTimeCast);
617 QFETCH(bool, DoubleCast);
618 QFETCH(bool, FontCast);
619 QFETCH(bool, ImageCast);
620 QFETCH(bool, IntCast);
621 QFETCH(bool, InvalidCast);
622 QFETCH(bool, KeySequenceCast);
623 QFETCH(bool, ListCast);
624 QFETCH(bool, LongLongCast);
625 QFETCH(bool, MapCast);
626 QFETCH(bool, PaletteCast);
627 QFETCH(bool, PenCast);
628 QFETCH(bool, PixmapCast);
629 QFETCH(bool, PointCast);
630 QFETCH(bool, RectCast);
631 QFETCH(bool, RegionCast);
632 QFETCH(bool, SizeCast);
633 QFETCH(bool, SizePolicyCast);
634 QFETCH(bool, StringCast);
635 QFETCH(bool, StringListCast);
636 QFETCH(bool, TimeCast);
637 QFETCH(bool, UIntCast);
638 QFETCH(bool, ULongLongCast);
640 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
641 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
642 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
643 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
644 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
645 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
646 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
647 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
648 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
649 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
650 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
651 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
652 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
653 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
654 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
655 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
656 QCOMPARE(val.canConvert(QVariant::List), ListCast);
657 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
658 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
659 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
660 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
661 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
662 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
663 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
664 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
665 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
666 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
667 QCOMPARE(val.canConvert(QVariant::String), StringCast);
668 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
669 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
670 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
671 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
674 QCOMPARE(val.canConvert(-1), false);
675 QCOMPARE(val.canConvert(-23), false);
676 QCOMPARE(val.canConvert(-23876), false);
677 QCOMPARE(val.canConvert(23876), false);
680 void tst_QVariant::toInt_data()
682 QTest::addColumn<QVariant>("value");
683 QTest::addColumn<int>("result");
684 QTest::addColumn<bool>("valueOK");
686 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
687 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
688 QTest::newRow( "char" ) << QVariant::fromValue('a') << int('a') << true;
689 signed char signedChar = -13;
690 QTest::newRow( "signed char" ) << QVariant::fromValue(signedChar) << -13 << true;
691 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
692 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
693 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
694 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
695 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
696 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
697 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
698 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
699 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
700 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
701 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
702 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
703 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
704 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
705 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
706 QTest::newRow( "keysequence" ) << QVariant::fromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
707 QByteArray bytearray(4, ' ');
712 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
717 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
720 void tst_QVariant::toInt()
722 QFETCH( QVariant, value );
723 QFETCH( int, result );
724 QFETCH( bool, valueOK );
725 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
727 int i = value.toInt( &ok );
728 QCOMPARE( i, result );
729 QVERIFY( ok == valueOK );
732 void tst_QVariant::toUInt_data()
734 QTest::addColumn<QVariant>("value");
735 QTest::addColumn<uint>("result");
736 QTest::addColumn<bool>("valueOK");
738 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
739 QTest::newRow( "char" ) << QVariant::fromValue('a') << uint('a') << true;
740 signed char signedChar = 12;
741 QTest::newRow( "signed char" ) << QVariant::fromValue(signedChar) << uint(12) << true;
742 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
743 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
744 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
745 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
746 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
747 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
748 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
749 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
750 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
751 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
752 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
753 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
754 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
755 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
756 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
757 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
758 QByteArray bytearray(4, ' ');
763 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
766 void tst_QVariant::toUInt()
768 QFETCH( QVariant, value );
769 QFETCH( uint, result );
770 QFETCH( bool, valueOK );
771 QVERIFY( value.isValid() );
772 QVERIFY( value.canConvert( QVariant::UInt ) );
775 uint i = value.toUInt( &ok );
776 QVERIFY( ok == valueOK );
777 QCOMPARE( i, result );
781 void tst_QVariant::toSize_data()
783 QTest::addColumn<QVariant>("value");
784 QTest::addColumn<QSize>("result");
785 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
786 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
787 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
788 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
791 void tst_QVariant::toSize()
793 QFETCH( QVariant, value );
794 QFETCH( QSize, result );
795 QVERIFY( value.isValid() );
796 QVERIFY( value.canConvert( QVariant::Size ) );
798 QSize i = value.toSize();
799 QCOMPARE( i, result );
802 void tst_QVariant::toSizeF_data()
804 QTest::addColumn<QVariant>("value");
805 QTest::addColumn<QSizeF>("result");
806 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
807 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
808 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
809 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
812 void tst_QVariant::toSizeF()
814 QFETCH( QVariant, value );
815 QFETCH( QSizeF, result );
816 QVERIFY( value.isValid() );
817 QVERIFY( value.canConvert( QVariant::SizeF ) );
819 QSizeF i = value.toSizeF();
820 QCOMPARE( i, result );
823 void tst_QVariant::toLine_data()
825 QTest::addColumn<QVariant>("value");
826 QTest::addColumn<QLine>("result");
827 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
828 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
829 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
830 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
833 void tst_QVariant::toLine()
835 QFETCH( QVariant, value );
836 QFETCH( QLine, result );
837 QVERIFY( value.isValid() );
838 QVERIFY( value.canConvert( QVariant::Line ) );
840 QLine i = value.toLine();
841 QCOMPARE( i, result );
844 void tst_QVariant::toLineF_data()
846 QTest::addColumn<QVariant>("value");
847 QTest::addColumn<QLineF>("result");
848 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
849 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
850 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
851 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
854 void tst_QVariant::toLineF()
856 QFETCH( QVariant, value );
857 QFETCH( QLineF, result );
858 QVERIFY( value.isValid() );
859 QVERIFY( value.canConvert( QVariant::LineF ) );
861 QLineF i = value.toLineF();
862 QCOMPARE( i, result );
865 void tst_QVariant::toPoint_data()
867 QTest::addColumn<QVariant>("value");
868 QTest::addColumn<QPoint>("result");
869 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
870 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
871 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
872 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
873 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
876 void tst_QVariant::toPoint()
878 QFETCH( QVariant, value );
879 QFETCH( QPoint, result );
880 QVERIFY( value.isValid() );
881 QVERIFY( value.canConvert( QVariant::Point ) );
882 QPoint i = value.toPoint();
883 QCOMPARE( i, result );
886 void tst_QVariant::toRect_data()
888 QTest::addColumn<QVariant>("value");
889 QTest::addColumn<QRect>("result");
890 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
891 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
892 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
893 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
894 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
897 void tst_QVariant::toRect()
899 QFETCH( QVariant, value );
900 QFETCH( QRect, result );
901 QVERIFY( value.isValid() );
902 QVERIFY( value.canConvert( QVariant::Rect ) );
903 QRect i = value.toRect();
904 QCOMPARE( i, result );
907 void tst_QVariant::toChar_data()
909 QTest::addColumn<QVariant>("value");
910 QTest::addColumn<QChar>("result");
911 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
912 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
915 void tst_QVariant::toChar()
917 QFETCH( QVariant, value );
918 QFETCH( QChar, result );
919 QVERIFY( value.isValid() );
920 QVERIFY( value.canConvert( QVariant::Char ) );
922 QChar i = value.toChar();
923 QCOMPARE( i, result );
926 void tst_QVariant::toBool_data()
928 QTest::addColumn<QVariant>("value");
929 QTest::addColumn<bool>("result");
931 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
932 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
933 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
934 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
935 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
936 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
937 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
938 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
939 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
940 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
941 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
942 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
943 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
944 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
945 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
946 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
947 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
948 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
951 void tst_QVariant::toBool()
953 QFETCH( QVariant, value );
954 QFETCH( bool, result );
955 QVERIFY( value.isValid() );
956 QVERIFY( value.canConvert( QVariant::Bool ) );
958 bool i = value.toBool();
959 QCOMPARE( i, result );
962 void tst_QVariant::toPointF_data()
964 QTest::addColumn<QVariant>("value");
965 QTest::addColumn<QPointF>("result");
967 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
970 void tst_QVariant::toPointF()
972 QFETCH( QVariant, value );
973 QFETCH( QPointF, result );
974 QVERIFY( value.isValid() );
975 QVERIFY( value.canConvert( QVariant::PointF ) );
976 QPointF d = value.toPointF();
977 QCOMPARE( d, result );
980 void tst_QVariant::toRectF_data()
982 QTest::addColumn<QVariant>("value");
983 QTest::addColumn<QRectF>("result");
985 QRect r( 1, 9, 8, 4 );
986 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
987 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
990 void tst_QVariant::toRectF()
992 QFETCH( QVariant, value );
993 QFETCH( QRectF, result );
994 QVERIFY( value.isValid() );
995 QVERIFY( value.canConvert( QVariant::RectF ) );
996 QRectF d = value.toRectF();
997 QCOMPARE( d, result );
1000 void tst_QVariant::toColor_data()
1002 QTest::addColumn<QVariant>("value");
1003 QTest::addColumn<QColor>("result");
1006 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
1007 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
1010 void tst_QVariant::toColor()
1012 QFETCH( QVariant, value );
1013 QFETCH( QColor, result );
1014 QVERIFY( value.isValid() );
1015 QVERIFY( value.canConvert( QVariant::Color ) );
1016 QColor d = qvariant_cast<QColor>(value);
1017 QCOMPARE( d, result );
1020 void tst_QVariant::toPixmap_data()
1022 QTest::addColumn<QVariant>("value");
1023 QTest::addColumn<QPixmap>("result");
1027 QTest::newRow( "image" ) << QVariant( pm ) << pm;
1030 bm.fill(Qt::color1);
1031 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
1034 void tst_QVariant::toPixmap()
1036 QFETCH( QVariant, value );
1037 QFETCH( QPixmap, result );
1038 QVERIFY( value.isValid() );
1039 QVERIFY( value.canConvert( QVariant::Pixmap ) );
1040 QPixmap d = qvariant_cast<QPixmap>(value);
1041 QCOMPARE( d, result );
1044 void tst_QVariant::toImage_data()
1046 QTest::addColumn<QVariant>("value");
1047 QTest::addColumn<QImage>("result");
1049 QImage im(30, 30, QImage::Format_ARGB32);
1050 im.fill(0x7fff0000);
1051 QTest::newRow( "image" ) << QVariant( im ) << im;
1054 void tst_QVariant::toImage()
1056 QFETCH( QVariant, value );
1057 QFETCH( QImage, result );
1058 QVERIFY( value.isValid() );
1059 QVERIFY( value.canConvert( QVariant::Image ) );
1060 QImage d = qvariant_cast<QImage>(value);
1061 QCOMPARE( d, result );
1064 void tst_QVariant::toBrush_data()
1066 QTest::addColumn<QVariant>("value");
1067 QTest::addColumn<QBrush>("result");
1070 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
1073 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
1076 void tst_QVariant::toBrush()
1078 QFETCH( QVariant, value );
1079 QFETCH( QBrush, result );
1080 QVERIFY( value.isValid() );
1081 QVERIFY( value.canConvert( QVariant::Brush ) );
1082 QBrush d = qvariant_cast<QBrush>(value);
1083 QCOMPARE( d, result );
1086 void tst_QVariant::toFont_data()
1088 QTest::addColumn<QVariant>("value");
1089 QTest::addColumn<QFont>("result");
1091 QFont f("times",12,-1,false);
1092 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1095 void tst_QVariant::toFont()
1097 QFETCH( QVariant, value );
1098 QFETCH( QFont, result );
1099 QVERIFY( value.isValid() );
1100 QVERIFY( value.canConvert( QVariant::Font ) );
1101 QFont d = qvariant_cast<QFont>(value);
1102 QCOMPARE( d, result );
1105 void tst_QVariant::toKeySequence_data()
1107 QTest::addColumn<QVariant>("value");
1108 QTest::addColumn<QKeySequence>("result");
1111 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1114 QTest::newRow( "qstring" )
1115 << QVariant( QString( "Ctrl+A" ) )
1116 << QKeySequence( Qt::CTRL + Qt::Key_A );
1119 void tst_QVariant::toKeySequence()
1121 QFETCH( QVariant, value );
1122 QFETCH( QKeySequence, result );
1123 QVERIFY( value.isValid() );
1124 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1125 QKeySequence d = qvariant_cast<QKeySequence>(value);
1126 QCOMPARE( d, result );
1129 void tst_QVariant::toDouble_data()
1131 QTest::addColumn<QVariant>("value");
1132 QTest::addColumn<double>("result");
1133 QTest::addColumn<bool>("valueOK");
1135 QByteArray bytearray(4, ' ');
1140 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1143 void tst_QVariant::toDouble()
1145 QFETCH( QVariant, value );
1146 QFETCH( double, result );
1147 QFETCH( bool, valueOK );
1148 QVERIFY( value.isValid() );
1149 QVERIFY( value.canConvert( QVariant::Double ) );
1151 double d = value.toDouble( &ok );
1152 QCOMPARE( d, result );
1153 QVERIFY( ok == valueOK );
1156 void tst_QVariant::toLongLong_data()
1158 QTest::addColumn<QVariant>("value");
1159 QTest::addColumn<qlonglong>("result");
1160 QTest::addColumn<bool>("valueOK");
1162 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1163 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1164 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1165 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1166 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1167 << (qlonglong)123 << true;
1168 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1170 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1171 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1172 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1173 QByteArray bytearray(4, ' ');
1178 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1181 void tst_QVariant::toLongLong()
1183 QFETCH( QVariant, value );
1184 QFETCH( qlonglong, result );
1185 QFETCH( bool, valueOK );
1186 QVERIFY( value.isValid() );
1187 QVERIFY( value.canConvert( QVariant::LongLong ) );
1189 qlonglong ll = value.toLongLong( &ok );
1190 QCOMPARE( ll, result );
1191 QVERIFY( ok == valueOK );
1194 void tst_QVariant::toULongLong_data()
1196 QTest::addColumn<QVariant>("value");
1197 QTest::addColumn<qulonglong>("result");
1198 QTest::addColumn<bool>("valueOK");
1200 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1201 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1202 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1203 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1204 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1205 << (qulonglong)123 << true;
1206 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1208 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1209 << Q_UINT64_C(18446744073709551615)
1211 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1212 << Q_UINT64_C(18446744073709551615)
1214 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1215 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1216 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1217 QByteArray bytearray(4, ' ');
1222 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1225 void tst_QVariant::toULongLong()
1227 QFETCH( QVariant, value );
1228 QFETCH( qulonglong, result );
1229 QFETCH( bool, valueOK );
1230 QVERIFY( value.isValid() );
1231 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1233 qulonglong ll = value.toULongLong( &ok );
1234 QCOMPARE( ll, result );
1235 QVERIFY( ok == valueOK );
1238 void tst_QVariant::toByteArray_data()
1240 QTest::addColumn<QVariant>("value");
1241 QTest::addColumn<QByteArray>("result");
1243 QByteArray ba(5, ' ');
1250 QByteArray variantBa = ba;
1252 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1253 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1254 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1255 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1256 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1257 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1258 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1261 void tst_QVariant::toByteArray()
1263 QFETCH( QVariant, value );
1264 QFETCH( QByteArray, result );
1265 QVERIFY( value.isValid() );
1266 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1267 QByteArray ba = value.toByteArray();
1268 QCOMPARE( ba, result );
1271 void tst_QVariant::toString_data()
1273 QTest::addColumn<QVariant>("value");
1274 QTest::addColumn<QString>("result");
1276 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1277 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1278 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1279 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1280 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1281 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1282 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1283 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1284 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1285 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1286 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1287 QTest::newRow( "qkeysequence" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1289 << QString( "Ctrl+A" );
1291 << QString(QChar(0x2318)) + "A";
1294 QFont font( "times", 12 );
1295 QTest::newRow( "qfont" ) << QVariant::fromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1296 QTest::newRow( "qcolor" ) << QVariant::fromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1297 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1298 QString( "123456789012" );
1301 void tst_QVariant::toString()
1303 QFETCH( QVariant, value );
1304 QFETCH( QString, result );
1305 QVERIFY( value.isValid() );
1306 QVERIFY( value.canConvert( QVariant::String ) );
1307 QString str = value.toString();
1308 QCOMPARE( str, result );
1311 void tst_QVariant::toDate_data()
1313 QTest::addColumn<QVariant>("value");
1314 QTest::addColumn<QDate>("result");
1316 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1317 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1318 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1321 void tst_QVariant::toDate()
1323 QFETCH( QVariant, value );
1324 QFETCH( QDate, result );
1325 QVERIFY( value.isValid() );
1326 QVERIFY( value.canConvert( QVariant::Date ) );
1327 QCOMPARE( value.toDate(), result );
1330 void tst_QVariant::toTime_data()
1332 QTest::addColumn<QVariant>("value");
1333 QTest::addColumn<QTime>("result");
1335 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1336 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1337 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1340 void tst_QVariant::toTime()
1342 QFETCH( QVariant, value );
1343 QFETCH( QTime, result );
1344 QVERIFY( value.isValid() );
1345 QVERIFY( value.canConvert( QVariant::Time ) );
1346 QCOMPARE( value.toTime(), result );
1349 void tst_QVariant::toDateTime_data()
1351 QTest::addColumn<QVariant>("value");
1352 QTest::addColumn<QDateTime>("result");
1354 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1355 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1356 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1357 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1360 void tst_QVariant::toDateTime()
1362 QFETCH( QVariant, value );
1363 QFETCH( QDateTime, result );
1364 QVERIFY( value.isValid() );
1365 QVERIFY( value.canConvert( QVariant::DateTime ) );
1366 QCOMPARE( value.toDateTime(), result );
1369 void tst_QVariant::toLocale()
1372 QLocale loc = variant.toLocale();
1373 variant = QLocale::system();
1374 loc = variant.toLocale();
1377 void tst_QVariant::toRegExp()
1380 QRegExp rx = variant.toRegExp();
1381 variant = QRegExp("foo");
1382 rx = variant.toRegExp();
1385 void tst_QVariant::toRegularExpression()
1388 QRegularExpression re = variant.toRegularExpression();
1389 QCOMPARE(re, QRegularExpression());
1391 variant = QRegularExpression("abc.*def");
1392 re = variant.toRegularExpression();
1393 QCOMPARE(re, QRegularExpression("abc.*def"));
1395 variant = QVariant::fromValue(QRegularExpression("[ab]\\w+"));
1396 re = variant.value<QRegularExpression>();
1397 QCOMPARE(re, QRegularExpression("[ab]\\w+"));
1400 void tst_QVariant::matrix()
1403 QMatrix matrix = qvariant_cast<QMatrix>(variant);
1404 QVERIFY(matrix.isIdentity());
1405 variant.setValue(QMatrix().rotate(90));
1406 QCOMPARE(QMatrix().rotate(90), qvariant_cast<QMatrix>(variant));
1408 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1410 QMetaType::destroy(QVariant::Matrix, mmatrix);
1413 void tst_QVariant::matrix4x4()
1416 QMatrix4x4 matrix = qvariant_cast<QMatrix4x4>(variant);
1417 QVERIFY(matrix.isIdentity());
1420 variant.setValue(m);
1421 QCOMPARE(m, qvariant_cast<QMatrix4x4>(variant));
1423 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1425 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1428 void tst_QVariant::transform()
1431 QTransform matrix = qvariant_cast<QTransform>(variant);
1432 QVERIFY(matrix.isIdentity());
1433 variant.setValue(QTransform().rotate(90));
1434 QCOMPARE(QTransform().rotate(90), qvariant_cast<QTransform>(variant));
1436 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1438 QMetaType::destroy(QVariant::Transform, mmatrix);
1442 void tst_QVariant::vector2D()
1445 QVector2D vector = qvariant_cast<QVector2D>(variant);
1446 QVERIFY(vector.isNull());
1447 variant.setValue(QVector2D(0.1, 0.2));
1448 QCOMPARE(QVector2D(0.1, 0.2), qvariant_cast<QVector2D>(variant));
1450 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1452 QMetaType::destroy(QVariant::Vector2D, pvector);
1455 void tst_QVariant::vector3D()
1458 QVector3D vector = qvariant_cast<QVector3D>(variant);
1459 QVERIFY(vector.isNull());
1460 variant.setValue(QVector3D(0.1, 0.2, 0.3));
1461 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qvariant_cast<QVector3D>(variant));
1463 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1465 QMetaType::destroy(QVariant::Vector3D, pvector);
1468 void tst_QVariant::vector4D()
1471 QVector4D vector = qvariant_cast<QVector4D>(variant);
1472 QVERIFY(vector.isNull());
1473 variant.setValue(QVector4D(0.1, 0.2, 0.3, 0.4));
1474 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qvariant_cast<QVector4D>(variant));
1476 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1478 QMetaType::destroy(QVariant::Vector4D, pvector);
1481 void tst_QVariant::quaternion()
1484 QQuaternion quaternion = qvariant_cast<QQuaternion>(variant);
1485 QVERIFY(quaternion.isIdentity());
1486 variant.setValue(QQuaternion(0.1, 0.2, 0.3, 0.4));
1487 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qvariant_cast<QQuaternion>(variant));
1489 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1490 QVERIFY(pquaternion);
1491 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1494 struct CustomStreamableClass
1497 bool operator==(const CustomStreamableClass& other) const
1499 return i == other.i;
1502 Q_DECLARE_METATYPE(CustomStreamableClass);
1504 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1506 return out << myObj.i;
1509 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1511 return in >> myObj.i;
1514 void tst_QVariant::writeToReadFromDataStream_data()
1516 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1518 QTest::addColumn<QVariant>("writeVariant");
1519 QTest::addColumn<bool>("isNull");
1521 typedef QList<QVariant> variantsList;
1522 variantsList valuelist;
1523 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1524 QVariant var(valuelist);
1525 QTest::newRow( "list_valid" ) << var << false;
1528 QTest::newRow( "invalid" ) << QVariant() << true;
1529 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1530 QBitArray bitarray( 3 );
1534 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1535 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1536 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1537 QByteArray bytearray(5, ' ');
1542 bytearray[4] = '\0';
1543 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1544 QTest::newRow( "bitmap_invalid" ) << QVariant::fromValue( QBitmap() ) << true;
1545 QBitmap bitmap( 10, 10 );
1546 bitmap.fill( Qt::red );
1547 QTest::newRow( "bitmap_valid" ) << QVariant::fromValue( bitmap ) << false;
1548 QTest::newRow( "brush_valid" ) << QVariant::fromValue( QBrush( Qt::red ) ) << false;
1549 QTest::newRow( "color_valid" ) << QVariant::fromValue( QColor( Qt::red ) ) << false;
1550 #ifndef QT_NO_CURSOR
1551 QTest::newRow( "cursor_valid" ) << QVariant::fromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1553 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1554 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1555 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1556 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1557 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1558 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1559 QTest::newRow( "font_valid" ) << QVariant::fromValue( QFont( "times", 12 ) ) << false;
1560 QTest::newRow( "pixmap_invalid" ) << QVariant::fromValue( QPixmap() ) << true;
1561 QPixmap pixmap( 10, 10 );
1562 pixmap.fill( Qt::red );
1563 QTest::newRow( "pixmap_valid" ) << QVariant::fromValue( pixmap ) << false;
1564 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1565 QTest::newRow( "image_invalid" ) << QVariant::fromValue( QImage() ) << true;
1566 QTest::newRow( "keysequence_valid" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1567 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1568 typedef QMap<QString, QVariant> variantsMap;
1570 vMap.insert( "int", QVariant( 1 ) );
1571 vMap.insert( "string", QVariant( QString("Two") ) );
1572 vMap.insert( "double", QVariant( 3.45 ) );
1573 vMap.insert( "float", QVariant( 3.45f ) );
1574 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1575 QTest::newRow( "palette_valid" ) << QVariant::fromValue(QPalette(QColor("turquoise"))) << false;
1576 QTest::newRow( "pen_valid" ) << QVariant::fromValue( QPen( Qt::red ) ) << false;
1577 QTest::newRow( "pointarray_invalid" ) << QVariant::fromValue( QPolygon() ) << true;
1578 QTest::newRow( "pointarray_valid" ) << QVariant::fromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1579 QTest::newRow( "region_invalid" ) << QVariant::fromValue( QRegion() ) << true;
1580 QTest::newRow( "region_valid" ) << QVariant::fromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1581 QTest::newRow( "sizepolicy_valid" ) << QVariant::fromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1582 QTest::newRow( "point_invalid" ) << QVariant::fromValue( QPoint() ) << true;
1583 QTest::newRow( "point_valid" ) << QVariant::fromValue( QPoint( 10, 10 ) ) << false;
1584 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1585 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1586 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1587 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1588 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1589 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1590 QStringList stringlist;
1591 stringlist << "One" << "Two" << "Three";
1592 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1593 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1594 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1595 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1596 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1597 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1598 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1599 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1600 QTest::newRow( "regularexpression" ) << QVariant(QRegularExpression("abc.*def")) << false;
1601 QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
1603 // types known to QMetaType, but not part of QVariant::Type
1604 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1606 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1607 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1609 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1610 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1612 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1613 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1614 ulong ulongInt = 1ul;
1615 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1616 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1617 ushort ushortInt = 1u;
1618 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1619 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1621 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1622 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1624 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1625 CustomStreamableClass custom = {123};
1626 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1629 void tst_QVariant::writeToReadFromDataStream()
1631 QFETCH( QVariant, writeVariant );
1632 QFETCH( bool, isNull );
1635 QDataStream writeStream( &data, QIODevice::WriteOnly );
1636 writeStream << writeVariant;
1638 QVariant readVariant;
1639 QDataStream readStream( &data, QIODevice::ReadOnly );
1640 readStream >> readVariant;
1641 QVERIFY( readVariant.isNull() == isNull );
1642 // Best way to confirm the readVariant contains the same data?
1643 // Since only a few won't match since the serial numbers are different
1644 // I won't bother adding another bool in the data test.
1645 const int writeType = writeVariant.userType();
1646 if (writeType == qMetaTypeId<CustomStreamableClass>())
1647 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1648 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1649 && writeType != QVariant::Image) {
1650 switch (writeType) {
1652 QCOMPARE( readVariant, writeVariant );
1655 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1656 case QMetaType::Long:
1657 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1659 case QMetaType::ULong:
1660 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1662 case QMetaType::Short:
1663 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1665 case QMetaType::UShort:
1666 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1668 case QMetaType::Char:
1669 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1671 case QMetaType::UChar:
1672 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1674 case QMetaType::Float:
1676 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1677 float readFloat = qvariant_cast<float>(readVariant);
1678 float writtenFloat = qvariant_cast<float>(writeVariant);
1679 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1680 if (!qIsNaN(readFloat))
1681 QVERIFY(readFloat == writtenFloat);
1688 void tst_QVariant::writeToReadFromOldDataStream()
1690 QVariant writeVariant = QString("hello");
1693 QDataStream writeStream(&data, QIODevice::WriteOnly);
1694 writeStream.setVersion(QDataStream::Qt_2_1);
1695 writeStream << writeVariant;
1697 QVariant readVariant;
1698 QDataStream readStream(&data, QIODevice::ReadOnly);
1699 readStream.setVersion(QDataStream::Qt_2_1);
1700 readStream >> readVariant;
1702 QCOMPARE(writeVariant.userType(), readVariant.userType());
1703 QCOMPARE(writeVariant, readVariant);
1706 void tst_QVariant::checkDataStream()
1708 const int typeId = QMetaType::LastCoreType + 1;
1709 QVERIFY(!QMetaType::isRegistered(typeId));
1711 QByteArray errorMessage("Trying to construct an instance of an invalid type, type id: ");
1712 errorMessage.append(QString::number(typeId, 10));
1714 QTest::ignoreMessage(QtWarningMsg, errorMessage.constData());
1715 QByteArray settingsHex("000000");
1716 settingsHex.append(QString::number(typeId, 16));
1717 settingsHex.append("ffffffffff");
1718 const QByteArray settings = QByteArray::fromHex(settingsHex);
1719 QDataStream in(settings);
1722 // the line below has been left out for now since the data stream
1723 // is not necessarily considered corrupt when an invalid QVariant is
1724 // constructed. However, it might be worth considering changing that behavior
1726 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1727 QCOMPARE(v.type(), QVariant::Invalid);
1730 void tst_QVariant::operator_eq_eq_data()
1732 QTest::addColumn<QVariant>("left");
1733 QTest::addColumn<QVariant>("right");
1734 QTest::addColumn<bool>("equal"); // left == right ?
1737 QVariant i0( int(0) );
1738 QVariant i1( int(1) );
1740 QTest::newRow( "invinv" ) << inv << inv << true;
1742 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1743 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1744 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1746 // LongLong and ULongLong
1747 QVariant ll1( (qlonglong)1 );
1748 QVariant lln2( (qlonglong)-2 );
1749 QVariant ull1( (qulonglong)1 );
1750 QVariant ull3( (qulonglong)3 );
1751 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1752 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1753 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1754 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1755 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1756 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1759 QVariant mIntString(QByteArray("-42"));
1760 QVariant mIntQString(QString("-42"));
1762 QVariant mUInt(42u);
1763 QVariant mUIntString(QByteArray("42"));
1764 QVariant mUIntQString(QString("42"));
1766 QVariant mDouble(42.11);
1767 QVariant mDoubleString(QByteArray("42.11"));
1768 QVariant mDoubleQString(QString("42.11"));
1770 QVariant mFloat(42.11f);
1771 QVariant mFloatString(QByteArray("42.11"));
1772 QVariant mFloatQString(QString("42.11"));
1774 QVariant mLongLong((qlonglong)-42);
1775 QVariant mLongLongString(QByteArray("-42"));
1776 QVariant mLongLongQString(QString("-42"));
1778 QVariant mULongLong((qulonglong)42);
1779 QVariant mULongLongString(QByteArray("42"));
1780 QVariant mULongLongQString(QString("42"));
1782 QVariant mBool(false);
1783 QVariant mBoolString(QByteArray("false"));
1784 QVariant mBoolQString(QString("false"));
1786 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1787 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1788 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1789 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1790 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1791 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1793 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1794 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1795 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1796 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1798 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1799 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1800 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1801 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1803 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1804 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1805 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1806 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1808 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1809 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1810 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1811 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1813 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1814 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1815 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1816 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1818 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1819 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1820 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1821 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1823 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1824 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1825 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1826 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1828 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1829 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1830 // ### many other combinations missing
1833 QMap<QString, QVariant> map1;
1834 map1.insert( "X", 1 );
1836 QMap<QString, QVariant> map2;
1837 map2.insert( "Y", 1 );
1839 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1844 QMap<QString, QVariant> map1;
1845 map1.insert( "X", 1 );
1847 QMap<QString, QVariant> map2;
1848 map2.insert( "X", 1 );
1850 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1854 QMap<QString, QVariant> map1;
1855 map1.insert( "X", 1 );
1857 QMap<QString, QVariant> map2;
1859 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1863 QMap<QString, QVariant> map1;
1865 QMap<QString, QVariant> map2;
1866 map2.insert( "X", 1 );
1868 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1872 QMap<QString, QVariant> map1;
1873 map1.insert( "X", 1 );
1874 map1.insert( "Y", 1 );
1876 QMap<QString, QVariant> map2;
1877 map2.insert( "X", 1 );
1879 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1883 QMap<QString, QVariant> map1;
1884 map1.insert( "X", 1 );
1886 QMap<QString, QVariant> map2;
1887 map2.insert( "X", 1 );
1888 map2.insert( "Y", 1 );
1890 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1893 // same thing with hash
1895 QHash<QString, QVariant> hash1;
1896 hash1.insert( "X", 1 );
1898 QHash<QString, QVariant> hash2;
1899 hash2.insert( "Y", 1 );
1901 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1906 QHash<QString, QVariant> hash1;
1907 hash1.insert( "X", 1 );
1909 QHash<QString, QVariant> hash2;
1910 hash2.insert( "X", 1 );
1912 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1916 QHash<QString, QVariant> hash1;
1917 hash1.insert( "X", 1 );
1919 QHash<QString, QVariant> hash2;
1921 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1925 QHash<QString, QVariant> hash1;
1927 QHash<QString, QVariant> hash2;
1928 hash2.insert( "X", 1 );
1930 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1934 QHash<QString, QVariant> hash1;
1935 hash1.insert( "X", 1 );
1936 hash1.insert( "Y", 1 );
1938 QHash<QString, QVariant> hash2;
1939 hash2.insert( "X", 1 );
1941 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1945 QHash<QString, QVariant> hash1;
1946 hash1.insert( "X", 1 );
1948 QHash<QString, QVariant> hash2;
1949 hash2.insert( "X", 1 );
1950 hash2.insert( "Y", 1 );
1952 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1956 void tst_QVariant::operator_eq_eq()
1958 QFETCH( QVariant, left );
1959 QFETCH( QVariant, right );
1960 QFETCH( bool, equal );
1961 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1962 QCOMPARE( left == right, equal );
1965 void tst_QVariant::operator_eq_eq_rhs()
1973 /* This should _not_ compile */
1977 QVERIFY(dt == list);
1981 void tst_QVariant::typeName_data()
1983 QTest::addColumn<int>("type");
1984 QTest::addColumn<QByteArray>("res");
1985 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1986 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1987 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1988 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1989 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1990 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1991 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1992 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1993 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1994 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1995 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1996 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1997 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1998 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1999 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
2000 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
2001 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
2002 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
2003 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
2004 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
2005 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
2006 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
2007 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
2008 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
2009 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
2010 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
2011 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
2012 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
2013 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
2014 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
2015 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
2016 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
2017 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
2018 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
2019 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
2020 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
2021 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
2022 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
2023 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
2024 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
2025 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
2026 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
2027 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
2028 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
2029 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
2030 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
2031 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
2032 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
2033 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
2034 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
2035 QTest::newRow("51") << int(QVariant::RegularExpression) << QByteArray("QRegularExpression");
2038 void tst_QVariant::typeName()
2040 QFETCH( int, type );
2041 QFETCH( QByteArray, res );
2042 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
2043 QString::fromLatin1(res.constData()));
2046 // test nameToType as well
2047 void tst_QVariant::typeToName()
2050 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
2051 // assumes that QVariant::Type contains consecutive values
2053 int max = QVariant::LastGuiType;
2054 for ( int t = 1; t <= max; t++ ) {
2055 const char *n = QVariant::typeToName( (QVariant::Type)t );
2057 QCOMPARE( int(QVariant::nameToType( n )), t );
2060 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
2061 // not documented but we return 0 if the type is out of range
2062 // by testing this we catch cases where QVariant is extended
2063 // but type_map is not updated accordingly
2064 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
2065 // invalid type names
2066 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
2067 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
2068 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
2070 QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
2072 // We don't support these old (Qt3) types anymore.
2073 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
2074 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
2075 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
2076 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
2079 void tst_QVariant::streamInvalidVariant()
2085 QVariant writeVariant;
2086 QVariant readVariant;
2088 QVERIFY( writeVariant.type() == QVariant::Invalid );
2091 QDataStream writeStream( &data, QIODevice::WriteOnly );
2092 writeStream << writeX << writeVariant << writeY;
2094 QDataStream readStream( &data, QIODevice::ReadOnly );
2095 readStream >> readX >> readVariant >> readY;
2097 QVERIFY( readX == writeX );
2098 // Two invalid QVariant's aren't necessarily the same, so == will
2099 // return false if one is invalid, so check the type() instead
2100 QVERIFY( readVariant.type() == QVariant::Invalid );
2101 QVERIFY( readY == writeY );
2104 static int instanceCount = 0;
2108 MyType(int n = 0, const char *t=0): number(n), text(t)
2112 MyType(const MyType &other)
2113 : number(other.number), text(other.text)
2125 Q_DECLARE_METATYPE(MyType)
2126 Q_DECLARE_METATYPE(MyType*)
2128 void tst_QVariant::userType()
2131 MyType data(1, "eins");
2132 MyType data2(2, "zwei");
2136 userVar.setValue(data);
2138 QCOMPARE(userVar.type(), QVariant::UserType);
2139 QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2140 QCOMPARE(userVar.typeName(), "MyType");
2141 QVERIFY(!userVar.isNull());
2142 QVERIFY(!userVar.canConvert(QVariant::String));
2144 QVariant userVar2(userVar);
2145 QVERIFY(userVar == userVar2);
2147 userVar2.setValue(data2);
2148 QVERIFY(userVar != userVar2);
2150 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2152 QCOMPARE(varData->number, data.number);
2153 QCOMPARE(varData->text, data.text);
2156 userVar3.setValue(data2);
2158 userVar3 = userVar2;
2159 QVERIFY(userVar2 == userVar3);
2161 // At this point all QVariants got destroyed but we have 2 MyType instances.
2162 QCOMPARE(instanceCount, 2);
2165 userVar.setValue(&data);
2167 QCOMPARE(userVar.type(), QVariant::UserType);
2168 QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2169 QCOMPARE(userVar.typeName(), "MyType*");
2170 QVERIFY(!userVar.isNull());
2171 QVERIFY(!userVar.canConvert(QVariant::String));
2173 QVariant userVar2(userVar);
2174 QVERIFY(userVar == userVar2);
2176 userVar2.setValue(&data2);
2177 QVERIFY(userVar != userVar2);
2179 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2181 QCOMPARE(*varData, &data);
2184 userVar3.setValue(&data2);
2186 /* This check is correct now. userVar2 contains a pointer to data2 and so
2188 QVERIFY(userVar2 == userVar3);
2190 userVar3 = userVar2;
2191 QVERIFY(userVar2 == userVar3);
2194 QCOMPARE(instanceCount, 2);
2196 myCarrier.setValue(data);
2197 QCOMPARE(instanceCount, 3);
2199 QVariant second = myCarrier;
2200 QCOMPARE(instanceCount, 3);
2202 QCOMPARE(instanceCount, 4);
2204 QCOMPARE(instanceCount, 3);
2206 MyType data3(0, "null");
2207 data3 = qvariant_cast<MyType>(myCarrier);
2208 QCOMPARE(data3.number, 1);
2209 QCOMPARE(data3.text, (const char *)"eins");
2211 QCOMPARE(instanceCount, 4);
2217 const MyType data(3, "drei");
2220 myCarrier.setValue(data);
2221 QCOMPARE(myCarrier.typeName(), "MyType");
2223 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2224 QCOMPARE(data2.number, 3);
2225 QCOMPARE(data2.text, (const char *)"drei");
2232 myCarrier.setValue(s);
2233 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2237 qlonglong ll = Q_INT64_C(42);
2240 myCarrier.setValue(ll);
2241 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2244 // At this point all QVariants got destroyed and MyType objects too.
2245 QCOMPARE(instanceCount, 0);
2253 Q_DECLARE_METATYPE(MyTypePOD)
2255 void tst_QVariant::podUserType()
2261 QVariant pod_as_variant = QVariant::fromValue(pod);
2262 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2264 QCOMPARE(pod.a, pod2.a);
2265 QCOMPARE(pod.b, pod2.b);
2267 pod_as_variant.setValue(pod);
2268 pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2270 QCOMPARE(pod.a, pod2.a);
2271 QCOMPARE(pod.b, pod2.b);
2274 void tst_QVariant::basicUserType()
2279 v = QVariant(QMetaType::Int, &i);
2281 QCOMPARE(v.type(), QVariant::Int);
2282 QCOMPARE(v.toInt(), 7);
2286 v = QVariant(QMetaType::QString, &s);
2288 QCOMPARE(v.type(), QVariant::String);
2289 QCOMPARE(v.toString(), QString("foo"));
2293 v = QVariant(QMetaType::Double, &d);
2295 QCOMPARE(v.type(), QVariant::Double);
2296 QCOMPARE(v.toDouble(), 4.4);
2300 v = QVariant(QMetaType::Float, &f);
2302 QCOMPARE(v.userType(), int(QMetaType::Float));
2303 QCOMPARE(v.toDouble(), 4.5);
2306 QByteArray ba("bar");
2307 v = QVariant(QMetaType::QByteArray, &ba);
2309 QCOMPARE(v.type(), QVariant::ByteArray);
2310 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2313 void tst_QVariant::data()
2320 QVariant ll = (qlonglong)2;
2321 QVariant ull = (qulonglong)3;
2322 QVariant s(QString("hallo"));
2323 QVariant r(QRect(1,2,3,4));
2327 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2331 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2335 QCOMPARE(*static_cast<float *>(v.data()), qvariant_cast<float>(v));
2339 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2343 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2347 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2351 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2354 void tst_QVariant::constData()
2367 QVERIFY(v.constData());
2368 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2371 QVERIFY(v.constData());
2372 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2375 QVERIFY(v.constData());
2376 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2379 QVERIFY(v.constData());
2380 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2383 QVERIFY(v.constData());
2384 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2387 QVERIFY(v.constData());
2388 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2391 QVERIFY(v.constData());
2392 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2401 Q_DECLARE_METATYPE(Foo)
2403 void tst_QVariant::variant_to()
2412 sl << QLatin1String("blah");
2421 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2422 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2423 QCOMPARE(qvariant_cast<int>(v2), 5);
2424 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2425 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2427 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2430 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2432 QCOMPARE(qvariant_cast<int>(v1), 4);
2434 QVariant n = QVariant::fromValue<short>(42);
2435 QCOMPARE(qvariant_cast<int>(n), 42);
2436 QCOMPARE(qvariant_cast<uint>(n), 42u);
2437 QCOMPARE(qvariant_cast<double>(n), 42.0);
2438 QCOMPARE(qvariant_cast<float>(n), 42.f);
2439 QCOMPARE(qvariant_cast<short>(n), short(42));
2440 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2442 n = QVariant::fromValue(43l);
2443 QCOMPARE(qvariant_cast<int>(n), 43);
2444 QCOMPARE(qvariant_cast<uint>(n), 43u);
2445 QCOMPARE(qvariant_cast<double>(n), 43.0);
2446 QCOMPARE(qvariant_cast<float>(n), 43.f);
2447 QCOMPARE(qvariant_cast<long>(n), 43l);
2449 n = QLatin1String("44");
2450 QCOMPARE(qvariant_cast<int>(n), 44);
2451 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2452 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2454 QCOMPARE(QVariant::fromValue(0.25f).toDouble(), 0.25);
2457 struct Blah { int i; };
2459 QDataStream& operator>>(QDataStream& s, Blah& c)
2460 { return (s >> c.i); }
2462 QDataStream& operator<<(QDataStream& s, const Blah& c)
2463 { return (s << c.i); }
2465 void tst_QVariant::saveLoadCustomTypes()
2470 int tp = qRegisterMetaType<Blah>("Blah");
2471 QVariant v = QVariant(tp, &i);
2473 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2475 QCOMPARE(v.userType(), tp);
2476 QVERIFY(v.type() == QVariant::UserType);
2478 QDataStream stream(&data, QIODevice::WriteOnly);
2485 QDataStream stream(data);
2489 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2490 int value = *(int*)v.constData();
2491 QCOMPARE(value, 42);
2494 void tst_QVariant::url()
2496 QString str("http://qt.nokia.com");
2499 QVariant v(url); //built with a QUrl
2503 QVariant v3(str); //built with a QString
2505 QCOMPARE(v2.toUrl(), url);
2506 QVERIFY(v3.canConvert<QUrl>());
2507 QCOMPARE(v2.toUrl(), v3.toUrl());
2509 QVERIFY(v2.canConvert<QString>());
2510 QCOMPARE(v2.toString(), str);
2511 QCOMPARE(v3.toString(), str);
2514 void tst_QVariant::variantMap()
2516 QMap<QString, QVariant> map;
2520 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2522 QCOMPARE(map2.value("test").toInt(), 42);
2524 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2525 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2527 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2528 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2531 void tst_QVariant::variantHash()
2533 QHash<QString, QVariant> hash;
2537 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2539 QCOMPARE(hash2.value("test").toInt(), 42);
2541 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2542 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2544 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2545 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2548 void tst_QVariant::invalidQColor() const
2550 QVariant va("An invalid QColor::name() value.");
2551 QVERIFY(va.canConvert(QVariant::Color));
2553 QVERIFY(!va.convert(QVariant::Color));
2555 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2558 class CustomQObject : public QObject {
2561 CustomQObject(QObject *parent = 0) : QObject(parent) {}
2563 class CustomQWidget : public QWidget {
2566 CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
2568 Q_DECLARE_METATYPE(CustomQObject*)
2569 Q_DECLARE_METATYPE(CustomQWidget*)
2571 class CustomNonQObject { };
2572 Q_DECLARE_METATYPE(CustomNonQObject)
2573 Q_DECLARE_METATYPE(CustomNonQObject*)
2575 void tst_QVariant::cleanupTestCase()
2577 delete customNonQObjectPointer;
2578 qDeleteAll(objectPointerTestData);
2581 void tst_QVariant::qvariant_cast_QObject_data()
2583 QTest::addColumn<QVariant>("data");
2584 QTest::addColumn<bool>("success");
2585 QObject *obj = new QObject;
2586 obj->setObjectName(QString::fromLatin1("Hello"));
2587 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2588 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2589 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2590 QTest::newRow("from int") << QVariant((int) 123) << false;
2591 QWidget *widget = new QWidget;
2592 widget->setObjectName(QString::fromLatin1("Hello"));
2593 QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true;
2594 CustomQObject *customObject = new CustomQObject(this);
2595 customObject->setObjectName(QString::fromLatin1("Hello"));
2596 QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2597 CustomQWidget *customWidget = new CustomQWidget;
2598 customWidget->setObjectName(QString::fromLatin1("Hello"));
2599 QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true;
2600 QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2602 // Deleted in cleanupTestCase.
2603 customNonQObjectPointer = new CustomNonQObject;
2604 QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2606 // Deleted in cleanupTestCase.
2607 objectPointerTestData.push_back(obj);
2608 objectPointerTestData.push_back(widget);
2609 objectPointerTestData.push_back(customObject);
2610 objectPointerTestData.push_back(customWidget);
2613 void tst_QVariant::qvariant_cast_QObject()
2615 QFETCH(QVariant, data);
2616 QFETCH(bool, success);
2618 QObject *o = qvariant_cast<QObject *>(data);
2619 QCOMPARE(o != 0, success);
2621 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2622 QVERIFY(data.canConvert<QObject*>());
2623 QVERIFY(data.canConvert(QMetaType::QObjectStar));
2624 QVERIFY(data.canConvert(::qMetaTypeId<QObject*>()));
2625 QVERIFY(data.value<QObject*>());
2626 QVERIFY(data.convert(QMetaType::QObjectStar));
2627 QCOMPARE(data.userType(), int(QMetaType::QObjectStar));
2629 QVERIFY(!data.canConvert<QObject*>());
2630 QVERIFY(!data.canConvert(QMetaType::QObjectStar));
2631 QVERIFY(!data.canConvert(::qMetaTypeId<QObject*>()));
2632 QVERIFY(!data.value<QObject*>());
2633 QVERIFY(!data.convert(QMetaType::QObjectStar));
2634 QVERIFY(data.userType() != QMetaType::QObjectStar);
2638 class CustomQObjectDerived : public CustomQObject {
2641 CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2643 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2645 class CustomQObjectDerivedNoMetaType : public CustomQObject {
2648 CustomQObjectDerivedNoMetaType(QObject *parent = 0) : CustomQObject(parent) {}
2651 void tst_QVariant::qvariant_cast_QObject_derived()
2654 CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this);
2655 QVariant data = QVariant::fromValue(object);
2656 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>());
2657 QCOMPARE(data.value<QObject *>(), object);
2658 QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object);
2659 QCOMPARE(data.value<CustomQObject *>(), object);
2660 QVERIFY(data.value<CustomQWidget*>() == 0);
2663 CustomQObjectDerived *object = new CustomQObjectDerived(this);
2664 QVariant data = QVariant::fromValue(object);
2666 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2668 QCOMPARE(data.value<QObject *>(), object);
2669 QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2670 QCOMPARE(data.value<CustomQObject *>(), object);
2671 QVERIFY(data.value<CustomQWidget*>() == 0);
2674 CustomQWidget customWidget;
2675 QWidget *widget = &customWidget;
2676 QVariant data = QVariant::fromValue(widget);
2677 QVERIFY(data.userType() == QMetaType::QWidgetStar);
2679 QCOMPARE(data.value<QObject*>(), widget);
2680 QCOMPARE(data.value<QWidget*>(), widget);
2681 QCOMPARE(data.value<CustomQWidget*>(), widget);
2685 void tst_QVariant::convertToQUint8() const
2689 const qint8 anInt = 32;
2691 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2692 * It's of type QVariant::Int. */
2693 const QVariant v0 = anInt;
2695 QVERIFY(v0.canConvert<qint8>());
2696 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2697 QCOMPARE(int(v0.toInt()), 32);
2698 QCOMPARE(v0.toString(), QString("32"));
2700 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2701 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2702 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2703 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2704 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2705 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2710 const quint8 anInt = 32;
2711 const QVariant v0 = anInt;
2713 QVERIFY(v0.canConvert<quint8>());
2714 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2715 QCOMPARE(int(v0.toUInt()), 32);
2716 QCOMPARE(v0.toString(), QString("32"));
2721 const qint16 anInt = 32;
2722 const QVariant v0 = anInt;
2724 QVERIFY(v0.canConvert<qint16>());
2725 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2726 QCOMPARE(int(v0.toInt()), 32);
2727 QCOMPARE(v0.toString(), QString("32"));
2732 const quint16 anInt = 32;
2733 const QVariant v0 = anInt;
2735 QVERIFY(v0.canConvert<quint16>());
2736 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2737 QCOMPARE(int(v0.toUInt()), 32);
2738 QCOMPARE(v0.toString(), QString("32"));
2742 void tst_QVariant::comparePointers() const
2750 QVariant v = QVariant::fromValue<void *>(&myClass);
2751 QVariant v2 = QVariant::fromValue<void *>(&myClass);
2757 Q_DECLARE_METATYPE(Data*)
2759 void tst_QVariant::voidStar() const
2766 v1 = QVariant::fromValue(p1);
2770 v2 = QVariant::fromValue(p2);
2774 v2 = QVariant::fromValue(p2);
2778 void tst_QVariant::dataStar() const
2780 qRegisterMetaType<Data*>();
2781 Data *p1 = new Data;
2783 QVariant v1 = QVariant::fromValue(p1);
2784 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2785 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2790 v2 = QVariant::fromValue(p1);
2795 void tst_QVariant::canConvertQStringList() const
2797 QFETCH(bool, canConvert);
2798 QFETCH(QStringList, input);
2799 QFETCH(QString, result);
2803 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2804 QCOMPARE(v.toString(), result);
2807 void tst_QVariant::canConvertQStringList_data() const
2809 QTest::addColumn<bool>("canConvert");
2810 QTest::addColumn<QStringList>("input");
2811 QTest::addColumn<QString>("result");
2813 QTest::newRow("An empty list") << false << QStringList() << QString();
2814 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2815 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2820 QTest::newRow("Two items") << false << l << QString();
2823 QTest::newRow("Three items") << false << l << QString();
2826 template<typename T> void convertMetaType()
2828 QVERIFY(QVariant::fromValue<T>(10).isValid());
2829 QVERIFY(QVariant::fromValue<T>(10).canConvert(QVariant::Int));
2830 QCOMPARE(QVariant::fromValue<T>(10).toInt(), 10);
2831 QCOMPARE(QVariant::fromValue<T>(10), QVariant::fromValue<T>(10));
2834 #define CONVERT_META_TYPE(Type) \
2835 convertMetaType<Type>(); \
2836 if (QTest::currentTestFailed()) \
2837 QFAIL("convertMetaType<" #Type "> failed");
2839 void tst_QVariant::canConvertMetaTypeToInt() const
2841 CONVERT_META_TYPE(long);
2842 CONVERT_META_TYPE(short);
2843 CONVERT_META_TYPE(short);
2844 CONVERT_META_TYPE(unsigned short);
2845 CONVERT_META_TYPE(ushort);
2846 CONVERT_META_TYPE(ulong);
2847 CONVERT_META_TYPE(unsigned long);
2848 CONVERT_META_TYPE(uchar);
2849 CONVERT_META_TYPE(unsigned char);
2850 CONVERT_META_TYPE(char);
2851 CONVERT_META_TYPE(uint);
2852 CONVERT_META_TYPE(unsigned int);
2855 #undef CONVERT_META_TYPE
2858 These calls should not produce any warnings.
2860 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2863 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2864 const QDateTime dateTime(variant.toDateTime());
2865 QVERIFY(!dateTime.isValid());
2869 QVariant v1(QLatin1String("xyz"));
2870 v1.convert(QVariant::DateTime);
2872 QVariant v2(QLatin1String("xyz"));
2873 QDateTime dt1(v2.toDateTime());
2875 const QVariant v3(QLatin1String("xyz"));
2876 const QDateTime dt2(v3.toDateTime());
2880 void tst_QVariant::invalidDateTime() const
2882 QVariant variant(QString::fromLatin1("Invalid date time string"));
2883 QVERIFY(!variant.toDateTime().isValid());
2884 QVERIFY(!variant.convert(QVariant::DateTime));
2889 MyClass() : myValue(0) {}
2893 Q_DECLARE_METATYPE( MyClass )
2895 void tst_QVariant::loadUnknownUserType()
2897 qRegisterMetaType<MyClass>("MyClass");
2898 QTest::ignoreMessage(QtWarningMsg, "QVariant::load: unable to load type "
2899 + QByteArray::number(qMetaTypeId<MyClass>()) +".");
2900 char data[] = {0, 0, QMetaType::User >> 8 , char(QMetaType::User), 0, 0, 0, 0, 8, 'M', 'y', 'C', 'l', 'a', 's', 's', 0};
2902 QByteArray ba(data, sizeof(data));
2903 QDataStream ds(&ba, QIODevice::ReadOnly);
2906 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2909 void tst_QVariant::loadBrokenUserType()
2911 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 127");
2912 char data[] = {0, 0, 0, 127, 0 };
2914 QByteArray ba(data, sizeof(data));
2915 QDataStream ds(&ba, QIODevice::ReadOnly);
2918 QCOMPARE(ds.status(), QDataStream::Ok);
2921 void tst_QVariant::invalidDate() const
2923 QString foo("Hello");
2924 QVariant variant(foo);
2925 QVERIFY(!variant.convert(QVariant::Date));
2928 QVERIFY(!variant.convert(QVariant::DateTime));
2931 QVERIFY(!variant.convert(QVariant::Time));
2934 QVERIFY(!variant.convert(QVariant::Int));
2937 QVERIFY(!variant.convert(QVariant::Double));
2940 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2945 int x,y,z,q,w,e,r,t;
2947 Q_DECLARE_METATYPE(WontCompare);
2949 void tst_QVariant::compareCustomTypes() const
2951 qRegisterMetaType<WontCompare>("WontCompare");
2955 const QVariant variant1(QVariant::fromValue(f1));
2959 const QVariant variant2(QVariant::fromValue(f2));
2961 /* We compare pointers. */
2962 QVERIFY(variant1 != variant2);
2963 QVERIFY(variant1 == variant1);
2964 QVERIFY(variant2 == variant2);
2967 void tst_QVariant::timeToDateTime() const
2969 const QVariant val(QTime::currentTime());
2970 QVERIFY(!val.canConvert(QVariant::DateTime));
2971 QVERIFY(!val.toDateTime().isValid());
2974 Q_DECLARE_METATYPE(QHostAddress)
2976 void tst_QVariant::copyingUserTypes() const
2980 const QHostAddress ha("127.0.0.1");
2984 QCOMPARE(qvariant_cast<QHostAddress>(var3), ha);
2987 void tst_QVariant::convertBoolToByteArray() const
2989 QFETCH(QByteArray, input);
2990 QFETCH(bool, canConvert);
2991 QFETCH(bool, value);
2993 const QVariant variant(input);
2995 QCOMPARE(variant.canConvert<bool>(), canConvert);
2998 /* Just call this function so we run the code path. */
2999 QCOMPARE(variant.toBool(), value);
3003 void tst_QVariant::convertBoolToByteArray_data() const
3005 QTest::addColumn<QByteArray>("input");
3006 QTest::addColumn<bool>("canConvert");
3007 QTest::addColumn<bool>("value");
3009 QTest::newRow("false")
3010 << QByteArray("false")
3014 QTest::newRow("FALSE")
3015 << QByteArray("FALSE")
3019 QTest::newRow("falSE")
3020 << QByteArray("FALSE")
3029 QTest::newRow("null QByteArray")
3034 QTest::newRow("any-content")
3035 << QByteArray("any-content")
3039 QTest::newRow("true")
3040 << QByteArray("true")
3044 QTest::newRow("TRUE")
3045 << QByteArray("TRUE")
3049 QTest::newRow("trUE")
3050 << QByteArray("trUE")
3055 void tst_QVariant::convertByteArrayToBool() const
3057 QFETCH(bool, input);
3058 QFETCH(QByteArray, output);
3060 const QVariant variant(input);
3061 QCOMPARE(variant.type(), QVariant::Bool);
3062 QCOMPARE(variant.toBool(), input);
3063 QVERIFY(variant.canConvert<bool>());
3065 QCOMPARE(variant.toByteArray(), output);
3068 void tst_QVariant::convertByteArrayToBool_data() const
3070 QTest::addColumn<bool>("input");
3071 QTest::addColumn<QByteArray>("output");
3073 QTest::newRow("false")
3075 << QByteArray("false");
3077 QTest::newRow("true")
3079 << QByteArray("true");
3084 1. Converting the string "9.9" to int fails. This is the behavior of
3085 toLongLong() and hence also QVariant, since it uses it.
3086 2. Converting the QVariant containing the double 9.9 to int works.
3088 Rationale: "9.9" is not a valid int. However, doubles are by definition not
3089 ints and therefore it makes more sense to perform conversion for those.
3091 void tst_QVariant::toIntFromQString() const
3093 QVariant first("9.9");
3095 QCOMPARE(first.toInt(&ok), 0);
3098 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
3102 QCOMPARE(v.toInt(&ok), 10);
3108 1. Conversion from (64 bit) double to int works (no overflow).
3109 2. Same conversion works for QVariant::convert.
3111 Rationale: if 2147483630 is set in float and then converted to int,
3112 there will be overflow and the result will be -2147483648.
3114 void tst_QVariant::toIntFromDouble() const
3116 double d = 2147483630; // max int 2147483647
3117 QVERIFY((int)d == 2147483630);
3120 QVERIFY( var.canConvert( QVariant::Int ) );
3123 int result = var.toInt(&ok);
3125 QVERIFY( ok == true );
3126 QCOMPARE(result, 2147483630);
3129 void tst_QVariant::setValue()
3131 QTransform t; //we just take a value so that we're sure that it will be shared
3133 QVERIFY( v1.isDetached() );
3135 QVERIFY( !v1.isDetached() );
3136 QVERIFY( !v2.isDetached() );
3138 v2.setValue(3); //set an integer value
3140 QVERIFY( v1.isDetached() );
3141 QVERIFY( v2.isDetached() );
3144 void tst_QVariant::numericalConvert()
3146 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3147 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3149 QVariant vfloat(float(5.3));
3150 QVariant vdouble(double(5.3));
3151 QVariant vreal(qreal(5.3));
3152 QVariant vint(int(5));
3153 QVariant vuint(uint(5));
3154 QVariant vshort(short(5));
3155 QVariant vlonglong(quint64(5));
3156 QVariant vstringint(QString::fromLatin1("5"));
3157 QVariant vstring(QString::fromLatin1("5.3"));
3159 QVector<QVariant *> vect;
3160 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3162 for(int i = 0; i < vect.size(); i++) {
3164 if (i >= 3 && i <= 7)
3166 QVariant *v = vect.at(i);
3167 QCOMPARE(v->toFloat() , float(num));
3168 QCOMPARE(float(v->toReal()) , float(num));
3169 QCOMPARE(float(v->toDouble()) , float(num));
3171 QCOMPARE(v->toInt() , int(num));
3172 QCOMPARE(v->toUInt() , uint(num));
3173 QCOMPARE(v->toULongLong() , quint64(num));
3175 QCOMPARE(v->toString() , QString::number(num));
3180 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3182 QVariant v = QVariant::fromValue(orig);
3183 QVERIFY(v.isValid());
3184 QCOMPARE(v.isNull(), isNull);
3185 QCOMPARE(v.toString(), toString);
3186 QCOMPARE(v.toDouble(), toDouble);
3187 QCOMPARE(v.toBool(), toBool);
3188 QCOMPARE(qvariant_cast<T>(v), orig);
3192 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3193 // Type is movable so standard comparison algorithm in QVariant should work
3194 // In a custom type QVariant is not aware of ==operator so it won't be called,
3195 // which may cause problems especially visible when using a not-movable type
3198 QVERIFY(v2.isValid());
3199 QCOMPARE(v2.isNull(), isNull);
3200 QCOMPARE(v2.toString(), toString);
3201 QCOMPARE(v2.toDouble(), toDouble);
3202 QCOMPARE(v2.toBool(), toBool);
3203 QCOMPARE(qvariant_cast<T>(v2), orig);
3209 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3210 // Type is movable so standard comparison algorithm in QVariant should work
3211 // In a custom type QVariant is not aware of ==operator so it won't be called,
3212 // which may cause problems especially visible when using a not-movable type
3215 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3216 QCOMPARE(v2.toString(), toString);
3217 v3 = QVariant::fromValue(orig);
3219 QVERIFY(v3.isValid());
3220 QCOMPARE(v3.isNull(), isNull);
3221 QCOMPARE(v3.toString(), toString);
3222 QCOMPARE(v3.toDouble(), toDouble);
3223 QCOMPARE(v3.toBool(), toBool);
3224 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3227 QVERIFY(v.isValid());
3228 QCOMPARE(v.isNull(), isNull);
3229 QCOMPARE(v.toString(), toString);
3230 QCOMPARE(v.toDouble(), toDouble);
3231 QCOMPARE(v.toBool(), toBool);
3232 QCOMPARE(qvariant_cast<T>(v), orig);
3234 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3235 QCOMPARE(v.userType(), qMetaTypeId<T>());
3236 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3240 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3241 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3242 if (QTest::currentTestFailed())\
3243 QFAIL("playWithVariant failed");
3248 bool operator==(const MyPrimitive &o) const
3250 return x == o.x && y == o.y;
3255 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3261 MyData() : ptr(this) {}
3264 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3266 MyData(const MyData& o) : ptr(this)
3268 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3270 MyData &operator=(const MyData &o)
3272 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3273 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3276 bool operator==(const MyData &o) const
3278 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3279 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3288 MyMovable() { v = count++; }
3289 ~MyMovable() { count--; }
3290 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3292 bool operator==(const MyMovable &o) const
3298 int MyMovable::count = 0;
3304 MyNotMovable() : that(this) { count++; }
3305 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3306 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3307 MyNotMovable &operator=(const MyNotMovable &o) {
3308 bool ok = that == this && o.that == &o;
3309 if (!ok) qFatal("MyNotMovable has been moved");
3313 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3314 // so it would be fine just to always return true
3315 bool operator==(const MyNotMovable &o) const
3317 bool ok = that == this && o.that == &o;
3318 if (!ok) qFatal("MyNotMovable has been moved");
3323 int MyNotMovable::count = 0;
3325 struct MyShared : QSharedData {
3330 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3333 Q_DECLARE_METATYPE(QList<QSize>)
3334 Q_DECLARE_METATYPE(MyPrimitive)
3335 Q_DECLARE_METATYPE(MyData)
3336 Q_DECLARE_METATYPE(MyMovable)
3337 Q_DECLARE_METATYPE(MyNotMovable)
3338 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3339 Q_DECLARE_METATYPE(QList<MyData>)
3340 Q_DECLARE_METATYPE(QList<MyMovable>)
3341 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3342 Q_DECLARE_METATYPE(MyPrimitive *)
3343 Q_DECLARE_METATYPE(MyData *)
3344 Q_DECLARE_METATYPE(MyMovable *)
3345 Q_DECLARE_METATYPE(MyNotMovable *)
3346 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3349 void tst_QVariant::moreCustomTypes()
3352 QList<QSize> listSize;
3353 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3354 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3355 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3360 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3361 str = QString::fromLatin1("123456789.123");
3362 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3367 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3368 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3373 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3374 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3376 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3377 l << MyData() << MyData();
3378 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3382 MyPrimitive d = { 4, 5 };
3383 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3384 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3385 QList<MyPrimitive> l;
3386 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3388 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3393 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3394 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3396 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3397 l << MyMovable() << d;
3398 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3400 QCOMPARE(MyMovable::count, 0);
3402 QCOMPARE(MyNotMovable::count, 0);
3405 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3406 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3407 QList<MyNotMovable> l;
3408 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3409 l << MyNotMovable() << d;
3410 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3412 QCOMPARE(MyNotMovable::count, 0);
3415 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3416 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3417 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3418 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3419 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3420 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3421 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3422 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3423 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3424 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3425 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3426 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3427 PLAY_WITH_VARIANT( qint8(-12), false, QLatin1String("\xf4"), -12, true); // qint8 is char, so it gets converted via QChar
3428 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3429 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3430 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3431 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3432 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3433 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3435 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3440 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3441 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3445 QVariant v1 = QVariant::fromValue(5);
3446 QVariant v2 = QVariant::fromValue(5.0);
3447 QVariant v3 = QVariant::fromValue(quint16(5));
3449 QVariant v5 = QVariant::fromValue(MyPrimitive());
3450 QVariant v6 = QVariant::fromValue(MyMovable());
3451 QVariant v7 = QVariant::fromValue(MyData());
3452 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3453 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3454 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3455 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3457 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3460 QCOMPARE(MyMovable::count, 0);
3462 QSharedDataPointer<MyShared> d(new MyShared);
3463 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3465 QCOMPARE(MyMovable::count, 0);
3468 QList<QList<int> > data;
3469 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3470 data << (QList<int>() << 42);
3471 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3475 QList<QVector<int> > data;
3476 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3477 data << (QVector<int>() << 42);
3478 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3482 QList<QSet<int> > data;
3483 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3484 data << (QSet<int>() << 42);
3485 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3489 QList<QLinkedList<int> > data;
3490 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3491 data << (QLinkedList<int>() << 42);
3492 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3496 void tst_QVariant::movabilityTest()
3498 // This test checks if QVariant is movable even if an internal data is not movable.
3499 QVERIFY(!MyNotMovable::count);
3501 QVariant variant = QVariant::fromValue(MyNotMovable());
3502 QVERIFY(MyNotMovable::count);
3504 // prepare destination memory space to which variant will be moved
3506 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3507 buffer[0].~QVariant();
3509 memcpy(buffer, &variant, sizeof(QVariant));
3510 QCOMPARE(buffer[0].type(), QVariant::UserType);
3511 QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3512 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3514 new (&variant) QVariant();
3516 QVERIFY(!MyNotMovable::count);
3519 void tst_QVariant::variantInVariant()
3522 QCOMPARE(var1.type(), QVariant::Int);
3523 QVariant var2 = var1;
3524 QCOMPARE(var2, var1);
3525 QCOMPARE(var2.type(), QVariant::Int);
3526 QVariant var3 = QVariant::fromValue(var1);
3527 QCOMPARE(var3, var1);
3528 QCOMPARE(var3.type(), QVariant::Int);
3529 QVariant var4 = qvariant_cast<QVariant>(var1);
3530 QCOMPARE(var4, var1);
3531 QCOMPARE(var4.type(), QVariant::Int);
3534 QCOMPARE(var5, var1);
3535 QCOMPARE(var5.type(), QVariant::Int);
3537 var6.setValue(var1);
3538 QCOMPARE(var6, var1);
3539 QCOMPARE(var6.type(), QVariant::Int);
3541 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3542 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3543 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3545 QString str("hello");
3546 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3547 QCOMPARE((int)var8.type(), (int)QVariant::String);
3548 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3550 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3551 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3552 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3555 void tst_QVariant::colorInteger()
3557 QVariant v = QColor(Qt::red);
3558 QCOMPARE(v.type(), QVariant::Color);
3559 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3562 QCOMPARE(v.type(), QVariant::Int);
3563 QCOMPARE(v.toInt(), 1000);
3565 v.setValue(QColor(Qt::yellow));
3566 QCOMPARE(v.type(), QVariant::Color);
3567 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3571 Q_DECLARE_OPAQUE_POINTER(Forward*)
3572 Q_DECLARE_METATYPE(Forward*)
3574 void tst_QVariant::forwardDeclare()
3577 QVariant v = QVariant::fromValue(f);
3578 QCOMPARE(qvariant_cast<Forward*>(v), f);
3581 void tst_QVariant::loadQt5Stream_data()
3583 dataStream_data(QDataStream::Qt_5_0);
3586 void tst_QVariant::loadQt5Stream()
3588 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3591 void tst_QVariant::saveQt5Stream_data()
3593 dataStream_data(QDataStream::Qt_5_0);
3596 void tst_QVariant::saveQt5Stream()
3598 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3601 void tst_QVariant::loadQt4Stream_data()
3603 dataStream_data(QDataStream::Qt_4_9);
3606 void tst_QVariant::loadQt4Stream()
3608 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3611 void tst_QVariant::saveQt4Stream_data()
3613 dataStream_data(QDataStream::Qt_4_9);
3616 void tst_QVariant::saveQt4Stream()
3618 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3621 void tst_QVariant::dataStream_data(QDataStream::Version version)
3623 QTest::addColumn<QString>("fileName");
3627 case QDataStream::Qt_4_9:
3628 path = QString::fromLatin1("qt4.9");
3630 case QDataStream::Qt_5_0:
3631 path = QString::fromLatin1("qt5.0");
3637 path = path.prepend(":/stream/").append("/");
3640 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3641 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3647 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3649 QFETCH(QString, fileName);
3651 QFile file(fileName);
3652 QVERIFY(file.open(QIODevice::ReadOnly));
3654 QDataStream stream(&file);
3655 stream.setVersion(version);
3658 QVariant loadedVariant;
3659 stream >> typeName >> loadedVariant;
3661 const int id = QMetaType::type(typeName.toLatin1());
3662 if (id == QMetaType::Void) {
3663 // Void type is not supported by QVariant
3667 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3668 QCOMPARE(constructedVariant.userType(), id);
3669 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3670 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3673 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3675 QFETCH(QString, fileName);
3677 QFile file(fileName);
3678 QVERIFY(file.open(QIODevice::ReadOnly));
3679 QDataStream dataFileStream(&file);
3682 dataFileStream >> typeName;
3683 QByteArray data = file.readAll();
3684 const int id = QMetaType::type(typeName.toLatin1());
3685 if (id == QMetaType::Void) {
3686 // Void type is not supported by QVariant
3691 buffer.open(QIODevice::ReadWrite);
3692 QDataStream stream(&buffer);
3693 stream.setVersion(version);
3695 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3696 QCOMPARE(constructedVariant.userType(), id);
3697 stream << constructedVariant;
3699 // We are testing QVariant there is no point in testing full array.
3700 QCOMPARE(buffer.data().left(5), data.left(5));
3703 QVariant recunstructedVariant;
3704 stream >> recunstructedVariant;
3705 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3708 class MessageHandler {
3710 MessageHandler(const int typeId, QtMsgHandler msgHandler = handler)
3711 : oldMsgHandler(qInstallMsgHandler(msgHandler))
3718 qInstallMsgHandler(oldMsgHandler);
3721 bool testPassed() const
3726 static void handler(QtMsgType, const char *txt)
3728 QString msg = QString::fromLatin1(txt);
3729 // Format itself is not important, but basic data as a type name should be included in the output
3730 ok = msg.startsWith("QVariant(");
3731 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3732 ok &= (currentId == QMetaType::UnknownType
3733 ? msg.contains("Invalid")
3734 : msg.contains(QMetaType::typeName(currentId)));
3735 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3736 if (currentId == QMetaType::Char || currentId == QMetaType::QChar) {
3737 // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length
3740 if (QMetaType::typeFlags(currentId) & QMetaType::PointerToQObject) {
3741 QByteArray currentName = QMetaType::typeName(currentId);
3742 currentName.chop(1);
3743 ok &= (msg.contains(", " + currentName) || msg.contains(", 0x0"));
3745 ok &= msg.endsWith(") ");
3746 QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData());
3750 QtMsgHandler oldMsgHandler;
3751 static int currentId;
3754 bool MessageHandler::ok;
3755 int MessageHandler::currentId;
3757 void tst_QVariant::debugStream_data()
3759 QTest::addColumn<QVariant>("variant");
3760 QTest::addColumn<int>("typeId");
3761 for (int id = 0; id < QMetaType::User; ++id) {
3762 const char *tagName = QMetaType::typeName(id);
3765 if (id != QMetaType::Void) {
3766 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3769 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3770 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3771 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3772 QTest::newRow("InvalidVariant") << QVariant() << int(QMetaType::UnknownType);
3773 QTest::newRow("CustomQObject") << QVariant::fromValue(this) << qMetaTypeId<tst_QVariant*>();
3776 void tst_QVariant::debugStream()
3778 QFETCH(QVariant, variant);
3779 QFETCH(int, typeId);
3781 MessageHandler msgHandler(typeId);
3782 qDebug() << variant;
3783 QVERIFY(msgHandler.testPassed());
3786 struct MessageHandlerType : public MessageHandler
3788 MessageHandlerType(const int typeId)
3789 : MessageHandler(typeId, handler)
3791 static void handler(QtMsgType, const char *txt)
3793 QString msg = QString::fromLatin1(txt);
3794 // Format itself is not important, but basic data as a type name should be included in the output
3795 ok = msg.startsWith("QVariant::");
3796 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3797 ok &= (currentId == QMetaType::UnknownType
3798 ? msg.contains("Invalid")
3799 : msg.contains(QMetaType::typeName(currentId)));
3800 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3804 void tst_QVariant::debugStreamType_data()
3809 void tst_QVariant::debugStreamType()
3811 QFETCH(QVariant, variant);
3812 QFETCH(int, typeId);
3814 MessageHandlerType msgHandler(typeId);
3815 qDebug() << QVariant::Type(typeId);
3816 QVERIFY(msgHandler.testPassed());
3819 void tst_QVariant::guiVariantAtExit()
3821 // crash test, it should not crash at QGuiApplication exit
3822 static QVariant cursor = QCursor();
3823 static QVariant point = QPoint();
3824 static QVariant image = QImage();
3825 static QVariant pallete = QPalette();
3833 void tst_QVariant::widgetsVariantAtExit()
3835 // crash test, it should not crash at QGuiApplication exit
3836 static QVariant icon= QIcon();
3837 static QVariant sizePolicy = QSizePolicy();
3839 Q_UNUSED(sizePolicy);
3843 QTEST_MAIN(tst_QVariant)
3844 #include "tst_qvariant.moc"