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>
61 #include <qtransform.h>
62 #include <qvector2d.h>
63 #include <qvector3d.h>
64 #include <qvector4d.h>
65 #include <qquaternion.h>
70 Q_DECLARE_METATYPE(qlonglong)
71 Q_DECLARE_METATYPE(qulonglong)
72 Q_DECLARE_METATYPE(QPointF)
73 Q_DECLARE_METATYPE(QRectF)
74 Q_DECLARE_METATYPE(QSize)
75 Q_DECLARE_METATYPE(QSizeF)
76 Q_DECLARE_METATYPE(QLine)
77 Q_DECLARE_METATYPE(QLineF)
78 Q_DECLARE_METATYPE(QPoint)
79 Q_DECLARE_METATYPE(QRect)
80 Q_DECLARE_METATYPE(QPixmap)
81 Q_DECLARE_METATYPE(QBrush)
82 Q_DECLARE_METATYPE(QFont)
83 Q_DECLARE_METATYPE(QColor)
84 Q_DECLARE_METATYPE(QKeySequence)
86 class CustomNonQObject;
88 class tst_QVariant : public QObject
93 tst_QVariant(QObject *parent = 0)
94 : QObject(parent), customNonQObjectPointer(0)
100 void cleanupTestCase();
103 void copy_constructor();
107 void canConvert_data();
140 void toLongLong_data();
143 void toULongLong_data();
146 void toByteArray_data();
149 void toString_data();
158 void toDateTime_data();
161 void toDouble_data();
164 void toPointF_data();
170 void toKeySequence_data();
171 void toKeySequence();
179 void toPixmap_data();
188 void qvariant_cast_QObject_data();
189 void qvariant_cast_QObject();
190 void qvariant_cast_QObject_derived();
195 void toRegularExpression();
210 void basicUserType();
214 void writeToReadFromDataStream_data();
215 void writeToReadFromDataStream();
216 void writeToReadFromOldDataStream();
217 void checkDataStream();
219 void operator_eq_eq_data();
220 void operator_eq_eq();
222 void operator_eq_eq_rhs();
224 void typeName_data();
228 void streamInvalidVariant();
235 void saveLoadCustomTypes();
242 void convertToQUint8() const;
243 void invalidQColor() const;
244 void comparePointers() const;
245 void voidStar() const;
246 void dataStar() const;
247 void canConvertQStringList() const;
248 void canConvertQStringList_data() const;
249 void canConvertMetaTypeToInt() const;
250 void variantToDateTimeWithoutWarnings() const;
251 void invalidDateTime() const;
253 void loadUnknownUserType();
254 void loadBrokenUserType();
256 void invalidDate() const;
257 void compareCustomTypes() const;
258 void timeToDateTime() const;
259 void copyingUserTypes() const;
260 void convertBoolToByteArray() const;
261 void convertBoolToByteArray_data() const;
262 void convertByteArrayToBool() const;
263 void convertByteArrayToBool_data() const;
264 void toIntFromQString() const;
265 void toIntFromDouble() const;
268 void numericalConvert();
269 void moreCustomTypes();
270 void movabilityTest();
271 void variantInVariant();
275 void forwardDeclare();
276 void debugStream_data();
279 void loadQt4Stream_data();
280 void loadQt4Stream();
281 void saveQt4Stream_data();
282 void saveQt4Stream();
283 void loadQt5Stream_data();
284 void loadQt5Stream();
285 void saveQt5Stream_data();
286 void saveQt5Stream();
288 void dataStream_data(QDataStream::Version version);
289 void loadQVariantFromDataStream(QDataStream::Version version);
290 void saveQVariantFromDataStream(QDataStream::Version version);
292 CustomNonQObject *customNonQObjectPointer;
293 QVector<QObject*> objectPointerTestData;
296 Q_DECLARE_METATYPE(QDate)
297 Q_DECLARE_METATYPE(QTime)
298 Q_DECLARE_METATYPE(QDateTime)
299 Q_DECLARE_METATYPE(QVariant)
301 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
302 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
304 void tst_QVariant::constructor()
307 QVERIFY( !variant.isValid() );
308 QVERIFY( variant.isNull() );
310 QVariant var2(variant);
311 QVERIFY( !var2.isValid() );
312 QVERIFY( variant.isNull() );
314 QVariant varll(intMax1);
315 QVariant varll2(varll);
316 QCOMPARE(varll2, varll);
318 QVariant var3(QVariant::String);
319 QCOMPARE(var3.typeName(), "QString");
320 QVERIFY(var3.isNull());
321 QVERIFY(var3.isValid());
323 QVariant var4(QVariant::Invalid);
324 QCOMPARE(var4.type(), QVariant::Invalid);
325 QVERIFY(var4.isNull());
326 QVERIFY(!var4.isValid());
328 QVariant var5(QLatin1String("hallo"));
329 QCOMPARE(var5.type(), QVariant::String);
330 QCOMPARE(var5.typeName(), "QString");
332 QVariant var6(qlonglong(0));
333 QCOMPARE(var6.type(), QVariant::LongLong);
334 QCOMPARE(var6.typeName(), "qlonglong");
337 QVERIFY(var7.isValid());
338 QVERIFY(!var7.isNull());
340 var8.setValue<int>(5);
341 QVERIFY(var8.isValid());
342 QVERIFY(!var8.isNull());
345 void tst_QVariant::copy_constructor()
347 QVariant var7(QVariant::Int);
349 QCOMPARE(var8.type(), QVariant::Int);
350 QVERIFY(var8.isNull());
353 void tst_QVariant::isNull()
356 QVERIFY( var.isNull() );
359 QVariant var1( str1 );
360 QVERIFY( var1.isNull() );
362 QVariant var2( QString::null );
363 QVERIFY( var2.isNull() );
365 QVariant var3( QString( "blah" ) );
366 QVERIFY( !var3.isNull() );
369 QVERIFY( !var4.isNull() );
371 QVariant var5 = QString();
372 QVERIFY( var5.isNull() );
374 QVariant var6( QString( "blah" ) );
375 QVERIFY( !var6.isNull() );
377 QVERIFY( var6.isNull() );
378 var6.convert( QVariant::String );
379 QVERIFY( var6.isNull() );
380 QVariant varLL( (qlonglong)0 );
381 QVERIFY( !varLL.isNull() );
382 QVariant var7(QString::null);
383 QVERIFY(var7.isNull());
386 void tst_QVariant::swap()
388 QVariant v1 = 1, v2 = 2.0;
390 QCOMPARE(v1.type(),QVariant::Double);
391 QCOMPARE(v1.toDouble(),2.0);
392 QCOMPARE(v2.type(),QVariant::Int);
393 QCOMPARE(v2.toInt(),1);
396 void tst_QVariant::canConvert_data()
398 QTest::addColumn<QVariant>("val");
399 QTest::addColumn<bool>("BitArrayCast");
400 QTest::addColumn<bool>("BitmapCast");
401 QTest::addColumn<bool>("BoolCast");
402 QTest::addColumn<bool>("BrushCast");
403 QTest::addColumn<bool>("ByteArrayCast");
404 QTest::addColumn<bool>("ColorCast");
405 QTest::addColumn<bool>("CursorCast");
406 QTest::addColumn<bool>("DateCast");
407 QTest::addColumn<bool>("DateTimeCast");
408 QTest::addColumn<bool>("DoubleCast");
409 QTest::addColumn<bool>("FontCast");
410 QTest::addColumn<bool>("ImageCast");
411 QTest::addColumn<bool>("IntCast");
412 QTest::addColumn<bool>("InvalidCast");
413 QTest::addColumn<bool>("KeySequenceCast");
414 QTest::addColumn<bool>("ListCast");
415 QTest::addColumn<bool>("LongLongCast");
416 QTest::addColumn<bool>("MapCast");
417 QTest::addColumn<bool>("PaletteCast");
418 QTest::addColumn<bool>("PenCast");
419 QTest::addColumn<bool>("PixmapCast");
420 QTest::addColumn<bool>("PointCast");
421 QTest::addColumn<bool>("RectCast");
422 QTest::addColumn<bool>("RegionCast");
423 QTest::addColumn<bool>("SizeCast");
424 QTest::addColumn<bool>("SizePolicyCast");
425 QTest::addColumn<bool>("StringCast");
426 QTest::addColumn<bool>("StringListCast");
427 QTest::addColumn<bool>("TimeCast");
428 QTest::addColumn<bool>("UIntCast");
429 QTest::addColumn<bool>("ULongLongCast");
440 // 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
443 QVariant var(QBitArray(0));
444 QTest::newRow("BitArray")
445 << 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;
446 var = qVariantFromValue(QBitmap());
447 QTest::newRow("Bitmap")
448 << 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;
449 var = qVariantFromValue(QBrush());
450 QTest::newRow("Brush")
451 << 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;
452 var = QVariant(QByteArray());
453 QTest::newRow("ByteArray")
454 << 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;
455 var = qVariantFromValue(QColor());
456 QTest::newRow("Color")
457 << 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;
459 var = qVariantFromValue(QCursor());
460 QTest::newRow("Cursor")
461 << 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;
463 var = QVariant(QDate());
464 QTest::newRow("Date")
465 << 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;
466 var = QVariant(QDateTime());
467 QTest::newRow("DateTime")
468 << 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;
469 var = QVariant((double)0.1);
470 QTest::newRow("Double")
471 << 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;
472 var = QVariant(0.1f);
473 QTest::newRow("Float")
474 << 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;
475 var = qVariantFromValue(QFont());
476 QTest::newRow("Font")
477 << 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;
478 var = qVariantFromValue(QIcon());
479 QTest::newRow("Icon")
480 << 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;
481 var = qVariantFromValue(QImage());
482 QTest::newRow("Image")
483 << 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;
484 var = QVariant((int)1);
486 << 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;
488 QTest::newRow("Invalid")
489 << 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;
490 var = qVariantFromValue(QKeySequence());
491 QTest::newRow("KeySequence")
492 << 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;
493 var = QVariant(QList<QVariant>());
494 QTest::newRow("List")
495 << 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;
496 var = QVariant((qlonglong)1);
497 QTest::newRow("LongLong")
498 << 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;
499 var = QVariant(QMap<QString,QVariant>());
501 << 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;
502 var = qVariantFromValue(QPalette());
503 QTest::newRow("Palette")
504 << 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;
505 var = qVariantFromValue(QPen());
507 << 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;
508 var = qVariantFromValue(QPixmap());
509 QTest::newRow("Pixmap")
510 << 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;
511 var = qVariantFromValue(QPolygon());
512 QTest::newRow("PointArray")
513 << 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;
514 var = QVariant(QPoint());
515 QTest::newRow("Point")
516 << 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;
517 var = QVariant(QRect());
518 QTest::newRow("Rect")
519 << 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;
520 var = qVariantFromValue(QRegion());
521 QTest::newRow("Region")
522 << 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;
523 var = QVariant(QSize());
524 QTest::newRow("Size")
525 << 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;
526 var = qVariantFromValue(QSizePolicy());
527 QTest::newRow("SizePolicy")
528 << 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;
529 var = QVariant(QString());
530 QTest::newRow("String")
531 << 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;
532 var = QVariant(QStringList("entry"));
533 QTest::newRow("StringList")
534 << 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;
535 var = QVariant(QTime());
536 QTest::newRow("Time")
537 << 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;
538 var = QVariant((uint)1);
539 QTest::newRow("UInt")
540 << 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;
541 var = QVariant((qulonglong)1);
542 QTest::newRow("ULongLong")
543 << 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;
549 void tst_QVariant::canConvert()
551 QFETCH(QVariant, val);
552 QFETCH(bool, BitArrayCast);
553 QFETCH(bool, BitmapCast);
554 QFETCH(bool, BoolCast);
555 QFETCH(bool, BrushCast);
556 QFETCH(bool, ByteArrayCast);
557 QFETCH(bool, ColorCast);
558 QFETCH(bool, CursorCast);
559 QFETCH(bool, DateCast);
560 QFETCH(bool, DateTimeCast);
561 QFETCH(bool, DoubleCast);
562 QFETCH(bool, FontCast);
563 QFETCH(bool, ImageCast);
564 QFETCH(bool, IntCast);
565 QFETCH(bool, InvalidCast);
566 QFETCH(bool, KeySequenceCast);
567 QFETCH(bool, ListCast);
568 QFETCH(bool, LongLongCast);
569 QFETCH(bool, MapCast);
570 QFETCH(bool, PaletteCast);
571 QFETCH(bool, PenCast);
572 QFETCH(bool, PixmapCast);
573 QFETCH(bool, PointCast);
574 QFETCH(bool, RectCast);
575 QFETCH(bool, RegionCast);
576 QFETCH(bool, SizeCast);
577 QFETCH(bool, SizePolicyCast);
578 QFETCH(bool, StringCast);
579 QFETCH(bool, StringListCast);
580 QFETCH(bool, TimeCast);
581 QFETCH(bool, UIntCast);
582 QFETCH(bool, ULongLongCast);
584 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
585 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
586 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
587 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
588 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
589 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
590 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
591 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
592 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
593 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
594 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
595 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
596 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
597 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
598 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
599 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
600 QCOMPARE(val.canConvert(QVariant::List), ListCast);
601 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
602 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
603 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
604 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
605 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
606 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
607 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
608 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
609 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
610 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
611 QCOMPARE(val.canConvert(QVariant::String), StringCast);
612 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
613 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
614 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
615 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
618 QCOMPARE(val.canConvert(-1), false);
619 QCOMPARE(val.canConvert(-23), false);
620 QCOMPARE(val.canConvert(-23876), false);
621 QCOMPARE(val.canConvert(23876), false);
624 void tst_QVariant::toInt_data()
626 QTest::addColumn<QVariant>("value");
627 QTest::addColumn<int>("result");
628 QTest::addColumn<bool>("valueOK");
630 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
631 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
632 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
633 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
634 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
635 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
636 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
637 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
638 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
639 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
640 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
641 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
642 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
643 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
644 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
645 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
646 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
647 QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
648 QByteArray bytearray(4, ' ');
653 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
658 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
661 void tst_QVariant::toInt()
663 QFETCH( QVariant, value );
664 QFETCH( int, result );
665 QFETCH( bool, valueOK );
666 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
668 int i = value.toInt( &ok );
669 QCOMPARE( i, result );
670 QVERIFY( ok == valueOK );
673 void tst_QVariant::toUInt_data()
675 QTest::addColumn<QVariant>("value");
676 QTest::addColumn<uint>("result");
677 QTest::addColumn<bool>("valueOK");
679 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
680 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
681 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
682 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
683 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
684 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
685 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
686 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
687 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
688 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
689 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
690 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
691 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
692 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
693 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
694 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
695 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
696 QByteArray bytearray(4, ' ');
701 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
704 void tst_QVariant::toUInt()
706 QFETCH( QVariant, value );
707 QFETCH( uint, result );
708 QFETCH( bool, valueOK );
709 QVERIFY( value.isValid() );
710 QVERIFY( value.canConvert( QVariant::UInt ) );
713 uint i = value.toUInt( &ok );
714 QVERIFY( ok == valueOK );
715 QCOMPARE( i, result );
719 void tst_QVariant::toSize_data()
721 QTest::addColumn<QVariant>("value");
722 QTest::addColumn<QSize>("result");
723 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
724 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
725 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
726 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
729 void tst_QVariant::toSize()
731 QFETCH( QVariant, value );
732 QFETCH( QSize, result );
733 QVERIFY( value.isValid() );
734 QVERIFY( value.canConvert( QVariant::Size ) );
736 QSize i = value.toSize();
737 QCOMPARE( i, result );
740 void tst_QVariant::toSizeF_data()
742 QTest::addColumn<QVariant>("value");
743 QTest::addColumn<QSizeF>("result");
744 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
745 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
746 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
747 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
750 void tst_QVariant::toSizeF()
752 QFETCH( QVariant, value );
753 QFETCH( QSizeF, result );
754 QVERIFY( value.isValid() );
755 QVERIFY( value.canConvert( QVariant::SizeF ) );
757 QSizeF i = value.toSizeF();
758 QCOMPARE( i, result );
761 void tst_QVariant::toLine_data()
763 QTest::addColumn<QVariant>("value");
764 QTest::addColumn<QLine>("result");
765 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
766 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
767 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
768 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
771 void tst_QVariant::toLine()
773 QFETCH( QVariant, value );
774 QFETCH( QLine, result );
775 QVERIFY( value.isValid() );
776 QVERIFY( value.canConvert( QVariant::Line ) );
778 QLine i = value.toLine();
779 QCOMPARE( i, result );
782 void tst_QVariant::toLineF_data()
784 QTest::addColumn<QVariant>("value");
785 QTest::addColumn<QLineF>("result");
786 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
787 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
788 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
789 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
792 void tst_QVariant::toLineF()
794 QFETCH( QVariant, value );
795 QFETCH( QLineF, result );
796 QVERIFY( value.isValid() );
797 QVERIFY( value.canConvert( QVariant::LineF ) );
799 QLineF i = value.toLineF();
800 QCOMPARE( i, result );
803 void tst_QVariant::toPoint_data()
805 QTest::addColumn<QVariant>("value");
806 QTest::addColumn<QPoint>("result");
807 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
808 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
809 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
810 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
811 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
814 void tst_QVariant::toPoint()
816 QFETCH( QVariant, value );
817 QFETCH( QPoint, result );
818 QVERIFY( value.isValid() );
819 QVERIFY( value.canConvert( QVariant::Point ) );
820 QPoint i = value.toPoint();
821 QCOMPARE( i, result );
824 void tst_QVariant::toRect_data()
826 QTest::addColumn<QVariant>("value");
827 QTest::addColumn<QRect>("result");
828 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
829 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
830 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
831 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
832 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
835 void tst_QVariant::toRect()
837 QFETCH( QVariant, value );
838 QFETCH( QRect, result );
839 QVERIFY( value.isValid() );
840 QVERIFY( value.canConvert( QVariant::Rect ) );
841 QRect i = value.toRect();
842 QCOMPARE( i, result );
845 void tst_QVariant::toChar_data()
847 QTest::addColumn<QVariant>("value");
848 QTest::addColumn<QChar>("result");
849 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
850 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
853 void tst_QVariant::toChar()
855 QFETCH( QVariant, value );
856 QFETCH( QChar, result );
857 QVERIFY( value.isValid() );
858 QVERIFY( value.canConvert( QVariant::Char ) );
860 QChar i = value.toChar();
861 QCOMPARE( i, result );
864 void tst_QVariant::toBool_data()
866 QTest::addColumn<QVariant>("value");
867 QTest::addColumn<bool>("result");
869 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
870 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
871 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
872 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
873 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
874 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
875 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
876 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
877 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
878 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
879 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
880 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
881 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
882 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
883 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
884 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
885 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
886 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
889 void tst_QVariant::toBool()
891 QFETCH( QVariant, value );
892 QFETCH( bool, result );
893 QVERIFY( value.isValid() );
894 QVERIFY( value.canConvert( QVariant::Bool ) );
896 bool i = value.toBool();
897 QCOMPARE( i, result );
900 void tst_QVariant::toPointF_data()
902 QTest::addColumn<QVariant>("value");
903 QTest::addColumn<QPointF>("result");
905 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
908 void tst_QVariant::toPointF()
910 QFETCH( QVariant, value );
911 QFETCH( QPointF, result );
912 QVERIFY( value.isValid() );
913 QVERIFY( value.canConvert( QVariant::PointF ) );
914 QPointF d = value.toPointF();
915 QCOMPARE( d, result );
918 void tst_QVariant::toRectF_data()
920 QTest::addColumn<QVariant>("value");
921 QTest::addColumn<QRectF>("result");
923 QRect r( 1, 9, 8, 4 );
924 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
925 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
928 void tst_QVariant::toRectF()
930 QFETCH( QVariant, value );
931 QFETCH( QRectF, result );
932 QVERIFY( value.isValid() );
933 QVERIFY( value.canConvert( QVariant::RectF ) );
934 QRectF d = value.toRectF();
935 QCOMPARE( d, result );
938 void tst_QVariant::toColor_data()
940 QTest::addColumn<QVariant>("value");
941 QTest::addColumn<QColor>("result");
944 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
945 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
948 void tst_QVariant::toColor()
950 QFETCH( QVariant, value );
951 QFETCH( QColor, result );
952 QVERIFY( value.isValid() );
953 QVERIFY( value.canConvert( QVariant::Color ) );
954 QColor d = qVariantValue<QColor>(value);
955 QCOMPARE( d, result );
958 void tst_QVariant::toPixmap_data()
960 QTest::addColumn<QVariant>("value");
961 QTest::addColumn<QPixmap>("result");
965 QTest::newRow( "image" ) << QVariant( pm ) << pm;
969 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
972 void tst_QVariant::toPixmap()
974 QFETCH( QVariant, value );
975 QFETCH( QPixmap, result );
976 QVERIFY( value.isValid() );
977 QVERIFY( value.canConvert( QVariant::Pixmap ) );
978 QPixmap d = qVariantValue<QPixmap>(value);
979 QCOMPARE( d, result );
982 void tst_QVariant::toImage_data()
984 QTest::addColumn<QVariant>("value");
985 QTest::addColumn<QImage>("result");
987 QImage im(30, 30, QImage::Format_ARGB32);
989 QTest::newRow( "image" ) << QVariant( im ) << im;
992 void tst_QVariant::toImage()
994 QFETCH( QVariant, value );
995 QFETCH( QImage, result );
996 QVERIFY( value.isValid() );
997 QVERIFY( value.canConvert( QVariant::Image ) );
998 QImage d = qVariantValue<QImage>(value);
999 QCOMPARE( d, result );
1002 void tst_QVariant::toBrush_data()
1004 QTest::addColumn<QVariant>("value");
1005 QTest::addColumn<QBrush>("result");
1008 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
1011 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
1014 void tst_QVariant::toBrush()
1016 QFETCH( QVariant, value );
1017 QFETCH( QBrush, result );
1018 QVERIFY( value.isValid() );
1019 QVERIFY( value.canConvert( QVariant::Brush ) );
1020 QBrush d = qVariantValue<QBrush>(value);
1021 QCOMPARE( d, result );
1024 void tst_QVariant::toFont_data()
1026 QTest::addColumn<QVariant>("value");
1027 QTest::addColumn<QFont>("result");
1029 QFont f("times",12,-1,false);
1030 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1033 void tst_QVariant::toFont()
1035 QFETCH( QVariant, value );
1036 QFETCH( QFont, result );
1037 QVERIFY( value.isValid() );
1038 QVERIFY( value.canConvert( QVariant::Font ) );
1039 QFont d = qVariantValue<QFont>(value);
1040 QCOMPARE( d, result );
1043 void tst_QVariant::toKeySequence_data()
1045 QTest::addColumn<QVariant>("value");
1046 QTest::addColumn<QKeySequence>("result");
1049 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1052 QTest::newRow( "qstring" )
1053 << QVariant( QString( "Ctrl+A" ) )
1054 << QKeySequence( Qt::CTRL + Qt::Key_A );
1057 void tst_QVariant::toKeySequence()
1059 QFETCH( QVariant, value );
1060 QFETCH( QKeySequence, result );
1061 QVERIFY( value.isValid() );
1062 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1063 QKeySequence d = qVariantValue<QKeySequence>(value);
1064 QCOMPARE( d, result );
1067 void tst_QVariant::toDouble_data()
1069 QTest::addColumn<QVariant>("value");
1070 QTest::addColumn<double>("result");
1071 QTest::addColumn<bool>("valueOK");
1073 QByteArray bytearray(4, ' ');
1078 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1081 void tst_QVariant::toDouble()
1083 QFETCH( QVariant, value );
1084 QFETCH( double, result );
1085 QFETCH( bool, valueOK );
1086 QVERIFY( value.isValid() );
1087 QVERIFY( value.canConvert( QVariant::Double ) );
1089 double d = value.toDouble( &ok );
1090 QCOMPARE( d, result );
1091 QVERIFY( ok == valueOK );
1094 void tst_QVariant::toLongLong_data()
1096 QTest::addColumn<QVariant>("value");
1097 QTest::addColumn<qlonglong>("result");
1098 QTest::addColumn<bool>("valueOK");
1100 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1101 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1102 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1103 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1104 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1105 << (qlonglong)123 << true;
1106 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1108 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1109 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1110 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1111 QByteArray bytearray(4, ' ');
1116 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1119 void tst_QVariant::toLongLong()
1121 QFETCH( QVariant, value );
1122 QFETCH( qlonglong, result );
1123 QFETCH( bool, valueOK );
1124 QVERIFY( value.isValid() );
1125 QVERIFY( value.canConvert( QVariant::LongLong ) );
1127 qlonglong ll = value.toLongLong( &ok );
1128 QCOMPARE( ll, result );
1129 QVERIFY( ok == valueOK );
1132 void tst_QVariant::toULongLong_data()
1134 QTest::addColumn<QVariant>("value");
1135 QTest::addColumn<qulonglong>("result");
1136 QTest::addColumn<bool>("valueOK");
1138 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1139 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1140 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1141 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1142 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1143 << (qulonglong)123 << true;
1144 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1146 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1147 << Q_UINT64_C(18446744073709551615)
1149 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1150 << Q_UINT64_C(18446744073709551615)
1152 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1153 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1154 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1155 QByteArray bytearray(4, ' ');
1160 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1163 void tst_QVariant::toULongLong()
1165 QFETCH( QVariant, value );
1166 QFETCH( qulonglong, result );
1167 QFETCH( bool, valueOK );
1168 QVERIFY( value.isValid() );
1169 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1171 qulonglong ll = value.toULongLong( &ok );
1172 QCOMPARE( ll, result );
1173 QVERIFY( ok == valueOK );
1176 void tst_QVariant::toByteArray_data()
1178 QTest::addColumn<QVariant>("value");
1179 QTest::addColumn<QByteArray>("result");
1181 QByteArray ba(5, ' ');
1188 QByteArray variantBa = ba;
1190 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1191 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1192 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1193 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1194 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1195 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1196 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1199 void tst_QVariant::toByteArray()
1201 QFETCH( QVariant, value );
1202 QFETCH( QByteArray, result );
1203 QVERIFY( value.isValid() );
1204 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1205 QByteArray ba = value.toByteArray();
1206 QCOMPARE( ba, result );
1209 void tst_QVariant::toString_data()
1211 QTest::addColumn<QVariant>("value");
1212 QTest::addColumn<QString>("result");
1214 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1215 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1216 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1217 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1218 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1219 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1220 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1221 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1222 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1223 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1224 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1225 QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1227 << QString( "Ctrl+A" );
1229 << QString(QChar(0x2318)) + "A";
1232 QFont font( "times", 12 );
1233 QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1234 QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1235 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1236 QString( "123456789012" );
1239 void tst_QVariant::toString()
1241 QFETCH( QVariant, value );
1242 QFETCH( QString, result );
1243 QVERIFY( value.isValid() );
1244 QVERIFY( value.canConvert( QVariant::String ) );
1245 QString str = value.toString();
1246 QCOMPARE( str, result );
1249 void tst_QVariant::toDate_data()
1251 QTest::addColumn<QVariant>("value");
1252 QTest::addColumn<QDate>("result");
1254 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1255 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1256 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1259 void tst_QVariant::toDate()
1261 QFETCH( QVariant, value );
1262 QFETCH( QDate, result );
1263 QVERIFY( value.isValid() );
1264 QVERIFY( value.canConvert( QVariant::Date ) );
1265 QCOMPARE( value.toDate(), result );
1268 void tst_QVariant::toTime_data()
1270 QTest::addColumn<QVariant>("value");
1271 QTest::addColumn<QTime>("result");
1273 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1274 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1275 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1278 void tst_QVariant::toTime()
1280 QFETCH( QVariant, value );
1281 QFETCH( QTime, result );
1282 QVERIFY( value.isValid() );
1283 QVERIFY( value.canConvert( QVariant::Time ) );
1284 QCOMPARE( value.toTime(), result );
1287 void tst_QVariant::toDateTime_data()
1289 QTest::addColumn<QVariant>("value");
1290 QTest::addColumn<QDateTime>("result");
1292 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1293 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1294 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1295 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1298 void tst_QVariant::toDateTime()
1300 QFETCH( QVariant, value );
1301 QFETCH( QDateTime, result );
1302 QVERIFY( value.isValid() );
1303 QVERIFY( value.canConvert( QVariant::DateTime ) );
1304 QCOMPARE( value.toDateTime(), result );
1307 void tst_QVariant::toLocale()
1310 QLocale loc = variant.toLocale();
1311 variant = QLocale::system();
1312 loc = variant.toLocale();
1315 void tst_QVariant::toRegExp()
1318 QRegExp rx = variant.toRegExp();
1319 variant = QRegExp("foo");
1320 rx = variant.toRegExp();
1323 void tst_QVariant::toRegularExpression()
1326 QRegularExpression re = variant.toRegularExpression();
1327 QCOMPARE(re, QRegularExpression());
1329 variant = QRegularExpression("abc.*def");
1330 re = variant.toRegularExpression();
1331 QCOMPARE(re, QRegularExpression("abc.*def"));
1333 variant = QVariant::fromValue(QRegularExpression("[ab]\\w+"));
1334 re = variant.value<QRegularExpression>();
1335 QCOMPARE(re, QRegularExpression("[ab]\\w+"));
1338 void tst_QVariant::matrix()
1341 QMatrix matrix = qVariantValue<QMatrix>(variant);
1342 QVERIFY(matrix.isIdentity());
1343 qVariantSetValue(variant, QMatrix().rotate(90));
1344 QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1346 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1348 QMetaType::destroy(QVariant::Matrix, mmatrix);
1351 void tst_QVariant::matrix4x4()
1354 QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1355 QVERIFY(matrix.isIdentity());
1358 qVariantSetValue(variant, m);
1359 QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1361 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1363 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1366 void tst_QVariant::transform()
1369 QTransform matrix = qVariantValue<QTransform>(variant);
1370 QVERIFY(matrix.isIdentity());
1371 qVariantSetValue(variant, QTransform().rotate(90));
1372 QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1374 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1376 QMetaType::destroy(QVariant::Transform, mmatrix);
1380 void tst_QVariant::vector2D()
1383 QVector2D vector = qVariantValue<QVector2D>(variant);
1384 QVERIFY(vector.isNull());
1385 qVariantSetValue(variant, QVector2D(0.1, 0.2));
1386 QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1388 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1390 QMetaType::destroy(QVariant::Vector2D, pvector);
1393 void tst_QVariant::vector3D()
1396 QVector3D vector = qVariantValue<QVector3D>(variant);
1397 QVERIFY(vector.isNull());
1398 qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1399 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1401 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1403 QMetaType::destroy(QVariant::Vector3D, pvector);
1406 void tst_QVariant::vector4D()
1409 QVector4D vector = qVariantValue<QVector4D>(variant);
1410 QVERIFY(vector.isNull());
1411 qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1412 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1414 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1416 QMetaType::destroy(QVariant::Vector4D, pvector);
1419 void tst_QVariant::quaternion()
1422 QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1423 QVERIFY(quaternion.isIdentity());
1424 qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1425 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1427 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1428 QVERIFY(pquaternion);
1429 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1432 struct CustomStreamableClass
1435 bool operator==(const CustomStreamableClass& other) const
1437 return i == other.i;
1440 Q_DECLARE_METATYPE(CustomStreamableClass);
1442 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1444 return out << myObj.i;
1447 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1449 return in >> myObj.i;
1452 void tst_QVariant::writeToReadFromDataStream_data()
1454 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1456 QTest::addColumn<QVariant>("writeVariant");
1457 QTest::addColumn<bool>("isNull");
1459 typedef QList<QVariant> variantsList;
1460 variantsList valuelist;
1461 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1462 QVariant var(valuelist);
1463 QTest::newRow( "list_valid" ) << var << false;
1466 QTest::newRow( "invalid" ) << QVariant() << true;
1467 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1468 QBitArray bitarray( 3 );
1472 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1473 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1474 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1475 QByteArray bytearray(5, ' ');
1480 bytearray[4] = '\0';
1481 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1482 QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1483 QBitmap bitmap( 10, 10 );
1484 bitmap.fill( Qt::red );
1485 QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1486 QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1487 QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1488 #ifndef QT_NO_CURSOR
1489 QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1491 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1492 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1493 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1494 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1495 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1496 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1497 QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1498 QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1499 QPixmap pixmap( 10, 10 );
1500 pixmap.fill( Qt::red );
1501 QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1502 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1503 QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1504 QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1505 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1506 typedef QMap<QString, QVariant> variantsMap;
1508 vMap.insert( "int", QVariant( 1 ) );
1509 vMap.insert( "string", QVariant( QString("Two") ) );
1510 vMap.insert( "double", QVariant( 3.45 ) );
1511 vMap.insert( "float", QVariant( 3.45f ) );
1512 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1513 QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1514 QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1515 QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1516 QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1517 QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1518 QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1519 QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1520 QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1521 QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1522 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1523 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1524 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1525 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1526 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1527 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1528 QStringList stringlist;
1529 stringlist << "One" << "Two" << "Three";
1530 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1531 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1532 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1533 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1534 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1535 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1536 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1537 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1538 QTest::newRow( "regularexpression" ) << QVariant(QRegularExpression("abc.*def")) << false;
1539 QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
1541 // types known to QMetaType, but not part of QVariant::Type
1542 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1544 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1545 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1547 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1548 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1550 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1551 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1552 ulong ulongInt = 1ul;
1553 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1554 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1555 ushort ushortInt = 1u;
1556 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1557 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1559 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1560 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1562 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1563 CustomStreamableClass custom = {123};
1564 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1567 void tst_QVariant::writeToReadFromDataStream()
1569 QFETCH( QVariant, writeVariant );
1570 QFETCH( bool, isNull );
1573 QDataStream writeStream( &data, QIODevice::WriteOnly );
1574 writeStream << writeVariant;
1576 QVariant readVariant;
1577 QDataStream readStream( &data, QIODevice::ReadOnly );
1578 readStream >> readVariant;
1579 QVERIFY( readVariant.isNull() == isNull );
1580 // Best way to confirm the readVariant contains the same data?
1581 // Since only a few won't match since the serial numbers are different
1582 // I won't bother adding another bool in the data test.
1583 const int writeType = writeVariant.userType();
1584 if (writeType == qMetaTypeId<CustomStreamableClass>())
1585 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1586 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1587 && writeType != QVariant::Image) {
1588 switch (writeType) {
1590 QCOMPARE( readVariant, writeVariant );
1593 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1594 case QMetaType::Long:
1595 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1597 case QMetaType::ULong:
1598 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1600 case QMetaType::Short:
1601 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1603 case QMetaType::UShort:
1604 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1606 case QMetaType::Char:
1607 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1609 case QMetaType::UChar:
1610 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1612 case QMetaType::Float:
1614 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1615 float readFloat = qvariant_cast<float>(readVariant);
1616 float writtenFloat = qvariant_cast<float>(writeVariant);
1617 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1618 if (!qIsNaN(readFloat))
1619 QVERIFY(readFloat == writtenFloat);
1626 void tst_QVariant::writeToReadFromOldDataStream()
1628 QVariant writeVariant = QString("hello");
1631 QDataStream writeStream(&data, QIODevice::WriteOnly);
1632 writeStream.setVersion(QDataStream::Qt_2_1);
1633 writeStream << writeVariant;
1635 QVariant readVariant;
1636 QDataStream readStream(&data, QIODevice::ReadOnly);
1637 readStream.setVersion(QDataStream::Qt_2_1);
1638 readStream >> readVariant;
1640 QCOMPARE(writeVariant.userType(), readVariant.userType());
1641 QCOMPARE(writeVariant, readVariant);
1644 void tst_QVariant::checkDataStream()
1646 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 46");
1647 const QByteArray settingsHex("0000002effffffffff");
1648 const QByteArray settings = QByteArray::fromHex(settingsHex);
1649 QDataStream in(settings);
1652 // the line below has been left out for now since the data stream
1653 // is not necessarily considered corrupt when an invalid QVariant is
1654 // constructed. However, it might be worth considering changing that behavior
1656 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1657 QCOMPARE(v.type(), QVariant::Invalid);
1660 void tst_QVariant::operator_eq_eq_data()
1662 QTest::addColumn<QVariant>("left");
1663 QTest::addColumn<QVariant>("right");
1664 QTest::addColumn<bool>("equal"); // left == right ?
1667 QVariant i0( int(0) );
1668 QVariant i1( int(1) );
1670 QTest::newRow( "invinv" ) << inv << inv << true;
1672 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1673 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1674 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1676 // LongLong and ULongLong
1677 QVariant ll1( (qlonglong)1 );
1678 QVariant lln2( (qlonglong)-2 );
1679 QVariant ull1( (qulonglong)1 );
1680 QVariant ull3( (qulonglong)3 );
1681 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1682 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1683 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1684 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1685 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1686 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1689 QVariant mIntString(QByteArray("-42"));
1690 QVariant mIntQString(QString("-42"));
1692 QVariant mUInt(42u);
1693 QVariant mUIntString(QByteArray("42"));
1694 QVariant mUIntQString(QString("42"));
1696 QVariant mDouble(42.11);
1697 QVariant mDoubleString(QByteArray("42.11"));
1698 QVariant mDoubleQString(QString("42.11"));
1700 QVariant mFloat(42.11f);
1701 QVariant mFloatString(QByteArray("42.11"));
1702 QVariant mFloatQString(QString("42.11"));
1704 QVariant mLongLong((qlonglong)-42);
1705 QVariant mLongLongString(QByteArray("-42"));
1706 QVariant mLongLongQString(QString("-42"));
1708 QVariant mULongLong((qulonglong)42);
1709 QVariant mULongLongString(QByteArray("42"));
1710 QVariant mULongLongQString(QString("42"));
1712 QVariant mBool(false);
1713 QVariant mBoolString(QByteArray("false"));
1714 QVariant mBoolQString(QString("false"));
1716 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1717 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1718 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1719 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1720 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1721 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1723 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1724 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1725 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1726 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1728 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1729 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1730 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1731 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1733 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1734 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1735 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1736 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1738 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1739 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1740 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1741 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1743 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1744 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1745 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1746 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1748 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1749 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1750 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1751 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1753 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1754 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1755 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1756 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1758 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1759 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1760 // ### many other combinations missing
1763 QMap<QString, QVariant> map1;
1764 map1.insert( "X", 1 );
1766 QMap<QString, QVariant> map2;
1767 map2.insert( "Y", 1 );
1769 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1774 QMap<QString, QVariant> map1;
1775 map1.insert( "X", 1 );
1777 QMap<QString, QVariant> map2;
1778 map2.insert( "X", 1 );
1780 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1784 QMap<QString, QVariant> map1;
1785 map1.insert( "X", 1 );
1787 QMap<QString, QVariant> map2;
1789 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1793 QMap<QString, QVariant> map1;
1795 QMap<QString, QVariant> map2;
1796 map2.insert( "X", 1 );
1798 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1802 QMap<QString, QVariant> map1;
1803 map1.insert( "X", 1 );
1804 map1.insert( "Y", 1 );
1806 QMap<QString, QVariant> map2;
1807 map2.insert( "X", 1 );
1809 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1813 QMap<QString, QVariant> map1;
1814 map1.insert( "X", 1 );
1816 QMap<QString, QVariant> map2;
1817 map2.insert( "X", 1 );
1818 map2.insert( "Y", 1 );
1820 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1823 // same thing with hash
1825 QHash<QString, QVariant> hash1;
1826 hash1.insert( "X", 1 );
1828 QHash<QString, QVariant> hash2;
1829 hash2.insert( "Y", 1 );
1831 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1836 QHash<QString, QVariant> hash1;
1837 hash1.insert( "X", 1 );
1839 QHash<QString, QVariant> hash2;
1840 hash2.insert( "X", 1 );
1842 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1846 QHash<QString, QVariant> hash1;
1847 hash1.insert( "X", 1 );
1849 QHash<QString, QVariant> hash2;
1851 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1855 QHash<QString, QVariant> hash1;
1857 QHash<QString, QVariant> hash2;
1858 hash2.insert( "X", 1 );
1860 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1864 QHash<QString, QVariant> hash1;
1865 hash1.insert( "X", 1 );
1866 hash1.insert( "Y", 1 );
1868 QHash<QString, QVariant> hash2;
1869 hash2.insert( "X", 1 );
1871 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1875 QHash<QString, QVariant> hash1;
1876 hash1.insert( "X", 1 );
1878 QHash<QString, QVariant> hash2;
1879 hash2.insert( "X", 1 );
1880 hash2.insert( "Y", 1 );
1882 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1886 void tst_QVariant::operator_eq_eq()
1888 QFETCH( QVariant, left );
1889 QFETCH( QVariant, right );
1890 QFETCH( bool, equal );
1891 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1892 QCOMPARE( left == right, equal );
1895 void tst_QVariant::operator_eq_eq_rhs()
1903 /* This should _not_ compile */
1907 QVERIFY(dt == list);
1911 void tst_QVariant::typeName_data()
1913 QTest::addColumn<int>("type");
1914 QTest::addColumn<QByteArray>("res");
1915 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1916 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1917 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1918 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1919 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1920 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1921 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1922 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1923 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1924 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1925 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1926 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1927 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1928 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1929 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1930 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1931 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1932 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1933 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1934 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1935 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1936 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1937 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1938 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1939 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1940 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1941 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1942 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1943 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1944 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1945 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1946 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1947 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1948 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1949 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1950 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1951 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1952 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1953 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1954 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1955 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1956 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1957 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1958 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1959 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1960 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1961 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1962 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1963 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1964 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1965 QTest::newRow("51") << int(QVariant::RegularExpression) << QByteArray("QRegularExpression");
1968 void tst_QVariant::typeName()
1970 QFETCH( int, type );
1971 QFETCH( QByteArray, res );
1972 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1973 QString::fromLatin1(res.constData()));
1976 // test nameToType as well
1977 void tst_QVariant::typeToName()
1980 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1981 // assumes that QVariant::Type contains consecutive values
1983 int max = QVariant::LastGuiType;
1984 for ( int t = 1; t <= max; t++ ) {
1985 const char *n = QVariant::typeToName( (QVariant::Type)t );
1987 QCOMPARE( int(QVariant::nameToType( n )), t );
1990 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1991 // not documented but we return 0 if the type is out of range
1992 // by testing this we catch cases where QVariant is extended
1993 // but type_map is not updated accordingly
1994 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1995 // invalid type names
1996 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
1997 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
1998 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
2000 QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
2002 // We don't support these old (Qt3) types anymore.
2003 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
2004 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
2005 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
2006 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
2009 void tst_QVariant::streamInvalidVariant()
2015 QVariant writeVariant;
2016 QVariant readVariant;
2018 QVERIFY( writeVariant.type() == QVariant::Invalid );
2021 QDataStream writeStream( &data, QIODevice::WriteOnly );
2022 writeStream << writeX << writeVariant << writeY;
2024 QDataStream readStream( &data, QIODevice::ReadOnly );
2025 readStream >> readX >> readVariant >> readY;
2027 QVERIFY( readX == writeX );
2028 // Two invalid QVariant's aren't necessarily the same, so == will
2029 // return false if one is invalid, so check the type() instead
2030 QVERIFY( readVariant.type() == QVariant::Invalid );
2031 QVERIFY( readY == writeY );
2034 static int instanceCount = 0;
2038 MyType(int n = 0, const char *t=0): number(n), text(t)
2042 MyType(const MyType &other)
2043 : number(other.number), text(other.text)
2055 Q_DECLARE_METATYPE(MyType)
2056 Q_DECLARE_METATYPE(MyType*)
2058 void tst_QVariant::userType()
2061 MyType data(1, "eins");
2062 MyType data2(2, "zwei");
2066 qVariantSetValue(userVar, data);
2068 QCOMPARE(userVar.type(), QVariant::UserType);
2069 QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2070 QCOMPARE(userVar.typeName(), "MyType");
2071 QVERIFY(!userVar.isNull());
2072 QVERIFY(!userVar.canConvert(QVariant::String));
2074 QVariant userVar2(userVar);
2075 QVERIFY(userVar == userVar2);
2077 qVariantSetValue(userVar2, data2);
2078 QVERIFY(userVar != userVar2);
2080 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2082 QCOMPARE(varData->number, data.number);
2083 QCOMPARE(varData->text, data.text);
2086 qVariantSetValue(userVar3, data2);
2088 userVar3 = userVar2;
2089 QVERIFY(userVar2 == userVar3);
2091 // At this point all QVariants got destroyed but we have 2 MyType instances.
2092 QCOMPARE(instanceCount, 2);
2095 qVariantSetValue(userVar, &data);
2097 QCOMPARE(userVar.type(), QVariant::UserType);
2098 QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2099 QCOMPARE(userVar.typeName(), "MyType*");
2100 QVERIFY(!userVar.isNull());
2101 QVERIFY(!userVar.canConvert(QVariant::String));
2103 QVariant userVar2(userVar);
2104 QVERIFY(userVar == userVar2);
2106 qVariantSetValue(userVar2, &data2);
2107 QVERIFY(userVar != userVar2);
2109 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2111 QCOMPARE(*varData, &data);
2114 qVariantSetValue(userVar3, &data2);
2116 /* This check is correct now. userVar2 contains a pointer to data2 and so
2118 QVERIFY(userVar2 == userVar3);
2120 userVar3 = userVar2;
2121 QVERIFY(userVar2 == userVar3);
2124 QCOMPARE(instanceCount, 2);
2126 qVariantSetValue(myCarrier, data);
2127 QCOMPARE(instanceCount, 3);
2129 QVariant second = myCarrier;
2130 QCOMPARE(instanceCount, 3);
2132 QCOMPARE(instanceCount, 4);
2134 QCOMPARE(instanceCount, 3);
2136 MyType data3(0, "null");
2137 data3 = qVariantValue<MyType>(myCarrier);
2138 QCOMPARE(data3.number, 1);
2139 QCOMPARE(data3.text, (const char *)"eins");
2141 QCOMPARE(instanceCount, 4);
2147 const MyType data(3, "drei");
2150 qVariantSetValue(myCarrier, data);
2151 QCOMPARE(myCarrier.typeName(), "MyType");
2153 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2154 QCOMPARE(data2.number, 3);
2155 QCOMPARE(data2.text, (const char *)"drei");
2162 qVariantSetValue(myCarrier, s);
2163 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2167 qlonglong ll = Q_INT64_C(42);
2170 qVariantSetValue(myCarrier, ll);
2171 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2174 // At this point all QVariants got destroyed and MyType objects too.
2175 QCOMPARE(instanceCount, 0);
2183 Q_DECLARE_METATYPE(MyTypePOD)
2185 void tst_QVariant::podUserType()
2191 QVariant pod_as_variant = qVariantFromValue(pod);
2192 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2194 QCOMPARE(pod.a, pod2.a);
2195 QCOMPARE(pod.b, pod2.b);
2197 qVariantSetValue(pod_as_variant, pod);
2198 pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2200 QCOMPARE(pod.a, pod2.a);
2201 QCOMPARE(pod.b, pod2.b);
2204 void tst_QVariant::basicUserType()
2209 v = QVariant(QMetaType::Int, &i);
2211 QCOMPARE(v.type(), QVariant::Int);
2212 QCOMPARE(v.toInt(), 7);
2216 v = QVariant(QMetaType::QString, &s);
2218 QCOMPARE(v.type(), QVariant::String);
2219 QCOMPARE(v.toString(), QString("foo"));
2223 v = QVariant(QMetaType::Double, &d);
2225 QCOMPARE(v.type(), QVariant::Double);
2226 QCOMPARE(v.toDouble(), 4.4);
2230 v = QVariant(QMetaType::Float, &f);
2232 QCOMPARE(v.userType(), int(QMetaType::Float));
2233 QCOMPARE(v.toDouble(), 4.5);
2236 QByteArray ba("bar");
2237 v = QVariant(QMetaType::QByteArray, &ba);
2239 QCOMPARE(v.type(), QVariant::ByteArray);
2240 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2243 void tst_QVariant::data()
2250 QVariant ll = (qlonglong)2;
2251 QVariant ull = (qulonglong)3;
2252 QVariant s(QString("hallo"));
2253 QVariant r(QRect(1,2,3,4));
2257 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2261 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2265 QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2269 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2273 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2277 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2281 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2284 void tst_QVariant::constData()
2297 QVERIFY(v.constData());
2298 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2301 QVERIFY(v.constData());
2302 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2305 QVERIFY(v.constData());
2306 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2309 QVERIFY(v.constData());
2310 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2313 QVERIFY(v.constData());
2314 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2317 QVERIFY(v.constData());
2318 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2321 QVERIFY(v.constData());
2322 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2331 Q_DECLARE_METATYPE(Foo)
2333 void tst_QVariant::variant_to()
2342 sl << QLatin1String("blah");
2344 qVariantSetValue(v3, sl);
2349 qVariantSetValue(v4, foo);
2351 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2352 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2353 QCOMPARE(qvariant_cast<int>(v2), 5);
2354 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2355 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2357 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2360 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2362 QCOMPARE(qvariant_cast<int>(v1), 4);
2364 QVariant n = qVariantFromValue<short>(42);
2365 QCOMPARE(qvariant_cast<int>(n), 42);
2366 QCOMPARE(qvariant_cast<uint>(n), 42u);
2367 QCOMPARE(qvariant_cast<double>(n), 42.0);
2368 QCOMPARE(qvariant_cast<float>(n), 42.f);
2369 QCOMPARE(qvariant_cast<short>(n), short(42));
2370 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2372 n = qVariantFromValue(43l);
2373 QCOMPARE(qvariant_cast<int>(n), 43);
2374 QCOMPARE(qvariant_cast<uint>(n), 43u);
2375 QCOMPARE(qvariant_cast<double>(n), 43.0);
2376 QCOMPARE(qvariant_cast<float>(n), 43.f);
2377 QCOMPARE(qvariant_cast<long>(n), 43l);
2379 n = QLatin1String("44");
2380 QCOMPARE(qvariant_cast<int>(n), 44);
2381 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2382 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2384 QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2387 struct Blah { int i; };
2389 QDataStream& operator>>(QDataStream& s, Blah& c)
2390 { return (s >> c.i); }
2392 QDataStream& operator<<(QDataStream& s, const Blah& c)
2393 { return (s << c.i); }
2395 void tst_QVariant::saveLoadCustomTypes()
2400 int tp = qRegisterMetaType<Blah>("Blah");
2401 QVariant v = QVariant(tp, &i);
2403 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2405 QCOMPARE(v.userType(), tp);
2406 QVERIFY(v.type() == QVariant::UserType);
2408 QDataStream stream(&data, QIODevice::WriteOnly);
2415 QDataStream stream(data);
2419 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2420 int value = *(int*)v.constData();
2421 QCOMPARE(value, 42);
2424 void tst_QVariant::url()
2426 QString str("http://qt.nokia.com");
2429 QVariant v(url); //built with a QUrl
2433 QVariant v3(str); //built with a QString
2435 QCOMPARE(v2.toUrl(), url);
2436 QVERIFY(qVariantCanConvert<QUrl>(v3));
2437 QCOMPARE(v2.toUrl(), v3.toUrl());
2439 QVERIFY(qVariantCanConvert<QString>(v2));
2440 QCOMPARE(v2.toString(), str);
2441 QCOMPARE(v3.toString(), str);
2444 void tst_QVariant::globalColor()
2446 QVariant variant(Qt::blue);
2447 QVERIFY(variant.type() == QVariant::Color);
2448 QVERIFY(qVariantValue<QColor>(variant) == QColor(Qt::blue));
2451 void tst_QVariant::variantMap()
2453 QMap<QString, QVariant> map;
2457 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2459 QCOMPARE(map2.value("test").toInt(), 42);
2461 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2462 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2464 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2465 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2468 void tst_QVariant::variantHash()
2470 QHash<QString, QVariant> hash;
2474 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2476 QCOMPARE(hash2.value("test").toInt(), 42);
2478 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2479 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2481 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2482 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2485 void tst_QVariant::invalidQColor() const
2487 QVariant va("An invalid QColor::name() value.");
2488 QVERIFY(va.canConvert(QVariant::Color));
2490 QVERIFY(!va.convert(QVariant::Color));
2492 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2495 class CustomQObject : public QObject {
2498 CustomQObject(QObject *parent = 0) : QObject(parent) {}
2500 class CustomQWidget : public QWidget {
2503 CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
2505 Q_DECLARE_METATYPE(CustomQObject*)
2506 Q_DECLARE_METATYPE(CustomQWidget*)
2508 class CustomNonQObject { };
2509 Q_DECLARE_METATYPE(CustomNonQObject)
2510 Q_DECLARE_METATYPE(CustomNonQObject*)
2512 void tst_QVariant::cleanupTestCase()
2514 delete customNonQObjectPointer;
2515 qDeleteAll(objectPointerTestData);
2518 void tst_QVariant::qvariant_cast_QObject_data()
2520 QTest::addColumn<QVariant>("data");
2521 QTest::addColumn<bool>("success");
2522 QObject *obj = new QObject;
2523 obj->setObjectName(QString::fromLatin1("Hello"));
2524 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2525 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2526 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2527 QTest::newRow("from int") << QVariant((int) 123) << false;
2528 QWidget *widget = new QWidget;
2529 widget->setObjectName(QString::fromLatin1("Hello"));
2530 QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true;
2531 CustomQObject *customObject = new CustomQObject(this);
2532 customObject->setObjectName(QString::fromLatin1("Hello"));
2533 QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2534 CustomQWidget *customWidget = new CustomQWidget;
2535 customWidget->setObjectName(QString::fromLatin1("Hello"));
2536 QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true;
2537 QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2539 // Deleted in cleanupTestCase.
2540 customNonQObjectPointer = new CustomNonQObject;
2541 QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2543 // Deleted in cleanupTestCase.
2544 objectPointerTestData.push_back(obj);
2545 objectPointerTestData.push_back(widget);
2546 objectPointerTestData.push_back(customObject);
2547 objectPointerTestData.push_back(customWidget);
2550 void tst_QVariant::qvariant_cast_QObject()
2552 QFETCH(QVariant, data);
2553 QFETCH(bool, success);
2555 QObject *o = qvariant_cast<QObject *>(data);
2556 QCOMPARE(o != 0, success);
2558 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2562 class CustomQObjectDerived : public CustomQObject {
2565 CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2567 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2569 class CustomQObjectDerivedNoMetaType : public CustomQObject {
2572 CustomQObjectDerivedNoMetaType(QObject *parent = 0) : CustomQObject(parent) {}
2575 void tst_QVariant::qvariant_cast_QObject_derived()
2578 CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this);
2579 QVariant data = QVariant::fromValue(object);
2580 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>());
2581 QCOMPARE(data.value<QObject *>(), object);
2582 QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object);
2583 QCOMPARE(data.value<CustomQObject *>(), object);
2584 QVERIFY(data.value<CustomQWidget*>() == 0);
2587 CustomQObjectDerived *object = new CustomQObjectDerived(this);
2588 QVariant data = QVariant::fromValue(object);
2590 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2592 QCOMPARE(data.value<QObject *>(), object);
2593 QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2594 QCOMPARE(data.value<CustomQObject *>(), object);
2595 QVERIFY(data.value<CustomQWidget*>() == 0);
2598 CustomQWidget customWidget;
2599 QWidget *widget = &customWidget;
2600 QVariant data = QVariant::fromValue(widget);
2601 QVERIFY(data.userType() == QMetaType::QWidgetStar);
2603 QCOMPARE(data.value<QObject*>(), widget);
2604 QCOMPARE(data.value<QWidget*>(), widget);
2605 QCOMPARE(data.value<CustomQWidget*>(), widget);
2609 Q_DECLARE_METATYPE(qint8);
2611 void tst_QVariant::convertToQUint8() const
2615 const qint8 anInt = 32;
2617 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2618 * It's of type QVariant::Int. */
2619 const QVariant v0 = anInt;
2621 QVERIFY(qVariantCanConvert<qint8>(v0));
2622 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2623 QCOMPARE(int(v0.toInt()), 32);
2624 QCOMPARE(v0.toString(), QString("32"));
2626 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2627 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2628 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2629 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2630 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2631 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2636 const quint8 anInt = 32;
2637 const QVariant v0 = anInt;
2639 QVERIFY(qVariantCanConvert<quint8>(v0));
2640 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2641 QCOMPARE(int(v0.toUInt()), 32);
2642 QCOMPARE(v0.toString(), QString("32"));
2647 const qint16 anInt = 32;
2648 const QVariant v0 = anInt;
2650 QVERIFY(qVariantCanConvert<qint16>(v0));
2651 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2652 QCOMPARE(int(v0.toInt()), 32);
2653 QCOMPARE(v0.toString(), QString("32"));
2658 const quint16 anInt = 32;
2659 const QVariant v0 = anInt;
2661 QVERIFY(qVariantCanConvert<quint16>(v0));
2662 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2663 QCOMPARE(int(v0.toUInt()), 32);
2664 QCOMPARE(v0.toString(), QString("32"));
2668 void tst_QVariant::comparePointers() const
2676 QVariant v = qVariantFromValue<void *>(&myClass);
2677 QVariant v2 = qVariantFromValue<void *>(&myClass);
2683 Q_DECLARE_METATYPE(Data*)
2685 void tst_QVariant::voidStar() const
2692 v1 = qVariantFromValue(p1);
2696 v2 = qVariantFromValue(p2);
2700 v2 = qVariantFromValue(p2);
2704 void tst_QVariant::dataStar() const
2706 qRegisterMetaType<Data*>();
2707 Data *p1 = new Data;
2709 QVariant v1 = qVariantFromValue(p1);
2710 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2711 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2716 v2 = qVariantFromValue(p1);
2721 void tst_QVariant::canConvertQStringList() const
2723 QFETCH(bool, canConvert);
2724 QFETCH(QStringList, input);
2725 QFETCH(QString, result);
2729 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2730 QCOMPARE(v.toString(), result);
2733 void tst_QVariant::canConvertQStringList_data() const
2735 QTest::addColumn<bool>("canConvert");
2736 QTest::addColumn<QStringList>("input");
2737 QTest::addColumn<QString>("result");
2739 QTest::newRow("An empty list") << false << QStringList() << QString();
2740 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2741 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2746 QTest::newRow("Two items") << false << l << QString();
2749 QTest::newRow("Three items") << false << l << QString();
2752 template<typename T> void convertMetaType()
2754 QVERIFY(qVariantFromValue<T>(10).isValid());
2755 QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2756 QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2757 QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2760 #define CONVERT_META_TYPE(Type) \
2761 convertMetaType<Type>(); \
2762 if (QTest::currentTestFailed()) \
2763 QFAIL("convertMetaType<" #Type "> failed");
2765 void tst_QVariant::canConvertMetaTypeToInt() const
2767 CONVERT_META_TYPE(long);
2768 CONVERT_META_TYPE(short);
2769 CONVERT_META_TYPE(short);
2770 CONVERT_META_TYPE(unsigned short);
2771 CONVERT_META_TYPE(ushort);
2772 CONVERT_META_TYPE(ulong);
2773 CONVERT_META_TYPE(unsigned long);
2774 CONVERT_META_TYPE(uchar);
2775 CONVERT_META_TYPE(unsigned char);
2776 CONVERT_META_TYPE(char);
2777 CONVERT_META_TYPE(uint);
2778 CONVERT_META_TYPE(unsigned int);
2781 #undef CONVERT_META_TYPE
2784 These calls should not produce any warnings.
2786 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2789 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2790 const QDateTime dateTime(variant.toDateTime());
2791 QVERIFY(!dateTime.isValid());
2795 QVariant v1(QLatin1String("xyz"));
2796 v1.convert(QVariant::DateTime);
2798 QVariant v2(QLatin1String("xyz"));
2799 QDateTime dt1(v2.toDateTime());
2801 const QVariant v3(QLatin1String("xyz"));
2802 const QDateTime dt2(v3.toDateTime());
2806 void tst_QVariant::invalidDateTime() const
2808 QVariant variant(QString::fromLatin1("Invalid date time string"));
2809 QVERIFY(!variant.toDateTime().isValid());
2810 QVERIFY(!variant.convert(QVariant::DateTime));
2815 MyClass() : myValue(0) {}
2819 Q_DECLARE_METATYPE( MyClass )
2821 void tst_QVariant::loadUnknownUserType()
2823 qRegisterMetaType<MyClass>("MyClass");
2824 char data[] = {0, 0, 1, 0, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0};
2826 QByteArray ba(data, sizeof(data));
2827 QDataStream ds(&ba, QIODevice::ReadOnly);
2830 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2833 void tst_QVariant::loadBrokenUserType()
2835 char data[] = {0, 0, 0, 127, 0, 112 };
2837 QByteArray ba(data, sizeof(data));
2838 QDataStream ds(&ba, QIODevice::ReadOnly);
2841 QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2844 void tst_QVariant::invalidDate() const
2846 QString foo("Hello");
2847 QVariant variant(foo);
2848 QVERIFY(!variant.convert(QVariant::Date));
2851 QVERIFY(!variant.convert(QVariant::DateTime));
2854 QVERIFY(!variant.convert(QVariant::Time));
2857 QVERIFY(!variant.convert(QVariant::Int));
2860 QVERIFY(!variant.convert(QVariant::Double));
2863 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2868 int x,y,z,q,w,e,r,t;
2870 Q_DECLARE_METATYPE(WontCompare);
2872 void tst_QVariant::compareCustomTypes() const
2874 qRegisterMetaType<WontCompare>("WontCompare");
2878 const QVariant variant1(qVariantFromValue(f1));
2882 const QVariant variant2(qVariantFromValue(f2));
2884 /* We compare pointers. */
2885 QVERIFY(variant1 != variant2);
2886 QVERIFY(variant1 == variant1);
2887 QVERIFY(variant2 == variant2);
2890 void tst_QVariant::timeToDateTime() const
2892 const QVariant val(QTime::currentTime());
2893 QVERIFY(!val.canConvert(QVariant::DateTime));
2894 QVERIFY(!val.toDateTime().isValid());
2897 Q_DECLARE_METATYPE(QHostAddress)
2899 void tst_QVariant::copyingUserTypes() const
2903 const QHostAddress ha("127.0.0.1");
2904 qVariantSetValue(var, ha);
2907 QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2910 void tst_QVariant::convertBoolToByteArray() const
2912 QFETCH(QByteArray, input);
2913 QFETCH(bool, canConvert);
2914 QFETCH(bool, value);
2916 const QVariant variant(input);
2918 QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2921 /* Just call this function so we run the code path. */
2922 QCOMPARE(variant.toBool(), value);
2926 void tst_QVariant::convertBoolToByteArray_data() const
2928 QTest::addColumn<QByteArray>("input");
2929 QTest::addColumn<bool>("canConvert");
2930 QTest::addColumn<bool>("value");
2932 QTest::newRow("false")
2933 << QByteArray("false")
2937 QTest::newRow("FALSE")
2938 << QByteArray("FALSE")
2942 QTest::newRow("falSE")
2943 << QByteArray("FALSE")
2952 QTest::newRow("null QByteArray")
2957 QTest::newRow("any-content")
2958 << QByteArray("any-content")
2962 QTest::newRow("true")
2963 << QByteArray("true")
2967 QTest::newRow("TRUE")
2968 << QByteArray("TRUE")
2972 QTest::newRow("trUE")
2973 << QByteArray("trUE")
2978 void tst_QVariant::convertByteArrayToBool() const
2980 QFETCH(bool, input);
2981 QFETCH(QByteArray, output);
2983 const QVariant variant(input);
2984 QCOMPARE(variant.type(), QVariant::Bool);
2985 QCOMPARE(variant.toBool(), input);
2986 QVERIFY(qVariantCanConvert<bool>(variant));
2988 QCOMPARE(variant.toByteArray(), output);
2991 void tst_QVariant::convertByteArrayToBool_data() const
2993 QTest::addColumn<bool>("input");
2994 QTest::addColumn<QByteArray>("output");
2996 QTest::newRow("false")
2998 << QByteArray("false");
3000 QTest::newRow("true")
3002 << QByteArray("true");
3007 1. Converting the string "9.9" to int fails. This is the behavior of
3008 toLongLong() and hence also QVariant, since it uses it.
3009 2. Converting the QVariant containing the double 9.9 to int works.
3011 Rationale: "9.9" is not a valid int. However, doubles are by definition not
3012 ints and therefore it makes more sense to perform conversion for those.
3014 void tst_QVariant::toIntFromQString() const
3016 QVariant first("9.9");
3018 QCOMPARE(first.toInt(&ok), 0);
3021 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
3025 QCOMPARE(v.toInt(&ok), 10);
3031 1. Conversion from (64 bit) double to int works (no overflow).
3032 2. Same conversion works for QVariant::convert.
3034 Rationale: if 2147483630 is set in float and then converted to int,
3035 there will be overflow and the result will be -2147483648.
3037 void tst_QVariant::toIntFromDouble() const
3039 double d = 2147483630; // max int 2147483647
3040 QVERIFY((int)d == 2147483630);
3043 QVERIFY( var.canConvert( QVariant::Int ) );
3046 int result = var.toInt(&ok);
3048 QVERIFY( ok == true );
3049 QCOMPARE(result, 2147483630);
3052 void tst_QVariant::setValue()
3054 QTransform t; //we just take a value so that we're sure that it will be shared
3056 QVERIFY( v1.isDetached() );
3058 QVERIFY( !v1.isDetached() );
3059 QVERIFY( !v2.isDetached() );
3061 qVariantSetValue(v2, 3); //set an integer value
3063 QVERIFY( v1.isDetached() );
3064 QVERIFY( v2.isDetached() );
3067 void tst_QVariant::numericalConvert()
3069 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3070 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3072 QVariant vfloat(float(5.3));
3073 QVariant vdouble(double(5.3));
3074 QVariant vreal(qreal(5.3));
3075 QVariant vint(int(5));
3076 QVariant vuint(uint(5));
3077 QVariant vshort(short(5));
3078 QVariant vlonglong(quint64(5));
3079 QVariant vstringint(QString::fromLatin1("5"));
3080 QVariant vstring(QString::fromLatin1("5.3"));
3082 QVector<QVariant *> vect;
3083 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3085 for(int i = 0; i < vect.size(); i++) {
3087 if (i >= 3 && i <= 7)
3089 QVariant *v = vect.at(i);
3090 QCOMPARE(v->toFloat() , float(num));
3091 QCOMPARE(float(v->toReal()) , float(num));
3092 QCOMPARE(float(v->toDouble()) , float(num));
3094 QCOMPARE(v->toInt() , int(num));
3095 QCOMPARE(v->toUInt() , uint(num));
3096 QCOMPARE(v->toULongLong() , quint64(num));
3098 QCOMPARE(v->toString() , QString::number(num));
3103 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3105 QVariant v = QVariant::fromValue(orig);
3106 QVERIFY(v.isValid());
3107 QCOMPARE(v.isNull(), isNull);
3108 QCOMPARE(v.toString(), toString);
3109 QCOMPARE(v.toDouble(), toDouble);
3110 QCOMPARE(v.toBool(), toBool);
3111 QCOMPARE(qvariant_cast<T>(v), orig);
3115 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3116 // Type is movable so standard comparison algorithm in QVariant should work
3117 // In a custom type QVariant is not aware of ==operator so it won't be called,
3118 // which may cause problems especially visible when using a not-movable type
3121 QVERIFY(v2.isValid());
3122 QCOMPARE(v2.isNull(), isNull);
3123 QCOMPARE(v2.toString(), toString);
3124 QCOMPARE(v2.toDouble(), toDouble);
3125 QCOMPARE(v2.toBool(), toBool);
3126 QCOMPARE(qvariant_cast<T>(v2), orig);
3132 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3133 // Type is movable so standard comparison algorithm in QVariant should work
3134 // In a custom type QVariant is not aware of ==operator so it won't be called,
3135 // which may cause problems especially visible when using a not-movable type
3138 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3139 QCOMPARE(v2.toString(), toString);
3140 v3 = qVariantFromValue(orig);
3142 QVERIFY(v3.isValid());
3143 QCOMPARE(v3.isNull(), isNull);
3144 QCOMPARE(v3.toString(), toString);
3145 QCOMPARE(v3.toDouble(), toDouble);
3146 QCOMPARE(v3.toBool(), toBool);
3147 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3150 QVERIFY(v.isValid());
3151 QCOMPARE(v.isNull(), isNull);
3152 QCOMPARE(v.toString(), toString);
3153 QCOMPARE(v.toDouble(), toDouble);
3154 QCOMPARE(v.toBool(), toBool);
3155 QCOMPARE(qvariant_cast<T>(v), orig);
3157 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3158 QCOMPARE(v.userType(), qMetaTypeId<T>());
3159 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3163 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3164 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3165 if (QTest::currentTestFailed())\
3166 QFAIL("playWithVariant failed");
3171 bool operator==(const MyPrimitive &o) const
3173 return x == o.x && y == o.y;
3178 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3184 MyData() : ptr(this) {}
3187 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3189 MyData(const MyData& o) : ptr(this)
3191 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3193 MyData &operator=(const MyData &o)
3195 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3196 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3199 bool operator==(const MyData &o) const
3201 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3202 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3211 MyMovable() { v = count++; }
3212 ~MyMovable() { count--; }
3213 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3215 bool operator==(const MyMovable &o) const
3221 int MyMovable::count = 0;
3227 MyNotMovable() : that(this) { count++; }
3228 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3229 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3230 MyNotMovable &operator=(const MyNotMovable &o) {
3231 bool ok = that == this && o.that == &o;
3232 if (!ok) qFatal("MyNotMovable has been moved");
3236 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3237 // so it would be fine just to always return true
3238 bool operator==(const MyNotMovable &o) const
3240 bool ok = that == this && o.that == &o;
3241 if (!ok) qFatal("MyNotMovable has been moved");
3246 int MyNotMovable::count = 0;
3248 struct MyShared : QSharedData {
3253 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3256 Q_DECLARE_METATYPE(QList<QSize>)
3257 Q_DECLARE_METATYPE(MyPrimitive)
3258 Q_DECLARE_METATYPE(MyData)
3259 Q_DECLARE_METATYPE(MyMovable)
3260 Q_DECLARE_METATYPE(MyNotMovable)
3261 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3262 Q_DECLARE_METATYPE(QList<MyData>)
3263 Q_DECLARE_METATYPE(QList<MyMovable>)
3264 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3265 Q_DECLARE_METATYPE(MyPrimitive *)
3266 Q_DECLARE_METATYPE(MyData *)
3267 Q_DECLARE_METATYPE(MyMovable *)
3268 Q_DECLARE_METATYPE(MyNotMovable *)
3269 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3272 void tst_QVariant::moreCustomTypes()
3275 QList<QSize> listSize;
3276 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3277 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3278 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3283 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3284 str = QString::fromLatin1("123456789.123");
3285 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3290 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3291 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3296 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3297 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3299 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3300 l << MyData() << MyData();
3301 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3305 MyPrimitive d = { 4, 5 };
3306 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3307 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3308 QList<MyPrimitive> l;
3309 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3311 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3316 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3317 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3319 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3320 l << MyMovable() << d;
3321 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3323 QCOMPARE(MyMovable::count, 0);
3325 QCOMPARE(MyNotMovable::count, 0);
3328 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3329 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3330 QList<MyNotMovable> l;
3331 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3332 l << MyNotMovable() << d;
3333 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3335 QCOMPARE(MyNotMovable::count, 0);
3338 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3339 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3340 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3341 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3342 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3343 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3344 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3345 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3346 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3347 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3348 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3349 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3350 PLAY_WITH_VARIANT( qint8(-12), false, "\xf4", -12, true);
3351 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3352 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3353 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3354 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3355 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3356 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3358 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3363 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3364 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3368 QVariant v1 = QVariant::fromValue(5);
3369 QVariant v2 = QVariant::fromValue(5.0);
3370 QVariant v3 = QVariant::fromValue(quint16(5));
3372 QVariant v5 = QVariant::fromValue(MyPrimitive());
3373 QVariant v6 = QVariant::fromValue(MyMovable());
3374 QVariant v7 = QVariant::fromValue(MyData());
3375 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3376 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3377 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3378 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3380 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3383 QCOMPARE(MyMovable::count, 0);
3385 QSharedDataPointer<MyShared> d(new MyShared);
3386 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3388 QCOMPARE(MyMovable::count, 0);
3391 QList<QList<int> > data;
3392 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3393 data << (QList<int>() << 42);
3394 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3398 QList<QVector<int> > data;
3399 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3400 data << (QVector<int>() << 42);
3401 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3405 QList<QSet<int> > data;
3406 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3407 data << (QSet<int>() << 42);
3408 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3412 QList<QLinkedList<int> > data;
3413 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3414 data << (QLinkedList<int>() << 42);
3415 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3419 void tst_QVariant::movabilityTest()
3421 // This test checks if QVariant is movable even if an internal data is not movable.
3422 QVERIFY(!MyNotMovable::count);
3424 QVariant variant = QVariant::fromValue(MyNotMovable());
3425 QVERIFY(MyNotMovable::count);
3427 // prepare destination memory space to which variant will be moved
3429 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3430 buffer[0].~QVariant();
3432 memcpy(buffer, &variant, sizeof(QVariant));
3433 QCOMPARE(buffer[0].type(), QVariant::UserType);
3434 QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3435 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3437 new (&variant) QVariant();
3439 QVERIFY(!MyNotMovable::count);
3442 void tst_QVariant::variantInVariant()
3445 QCOMPARE(var1.type(), QVariant::Int);
3446 QVariant var2 = var1;
3447 QCOMPARE(var2, var1);
3448 QCOMPARE(var2.type(), QVariant::Int);
3449 QVariant var3 = QVariant::fromValue(var1);
3450 QCOMPARE(var3, var1);
3451 QCOMPARE(var3.type(), QVariant::Int);
3452 QVariant var4 = qvariant_cast<QVariant>(var1);
3453 QCOMPARE(var4, var1);
3454 QCOMPARE(var4.type(), QVariant::Int);
3457 QCOMPARE(var5, var1);
3458 QCOMPARE(var5.type(), QVariant::Int);
3460 var6.setValue(var1);
3461 QCOMPARE(var6, var1);
3462 QCOMPARE(var6.type(), QVariant::Int);
3464 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3465 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3466 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3468 QString str("hello");
3469 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3470 QCOMPARE((int)var8.type(), (int)QVariant::String);
3471 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3473 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3474 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3475 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3478 void tst_QVariant::colorInteger()
3480 QVariant v = QColor(Qt::red);
3481 QCOMPARE(v.type(), QVariant::Color);
3482 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3485 QCOMPARE(v.type(), QVariant::Int);
3486 QCOMPARE(v.toInt(), 1000);
3488 v.setValue(QColor(Qt::yellow));
3489 QCOMPARE(v.type(), QVariant::Color);
3490 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3494 Q_DECLARE_OPAQUE_POINTER(Forward*)
3495 Q_DECLARE_METATYPE(Forward*)
3497 void tst_QVariant::forwardDeclare()
3500 QVariant v = QVariant::fromValue(f);
3501 QCOMPARE(qvariant_cast<Forward*>(v), f);
3504 void tst_QVariant::loadQt5Stream_data()
3506 dataStream_data(QDataStream::Qt_5_0);
3509 void tst_QVariant::loadQt5Stream()
3511 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3514 void tst_QVariant::saveQt5Stream_data()
3516 dataStream_data(QDataStream::Qt_5_0);
3519 void tst_QVariant::saveQt5Stream()
3521 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3524 void tst_QVariant::loadQt4Stream_data()
3526 dataStream_data(QDataStream::Qt_4_9);
3529 void tst_QVariant::loadQt4Stream()
3531 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3534 void tst_QVariant::saveQt4Stream_data()
3536 dataStream_data(QDataStream::Qt_4_9);
3539 void tst_QVariant::saveQt4Stream()
3541 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3544 void tst_QVariant::dataStream_data(QDataStream::Version version)
3546 QTest::addColumn<QString>("fileName");
3550 case QDataStream::Qt_4_9:
3551 path = QString::fromLatin1("qt4.9");
3553 case QDataStream::Qt_5_0:
3554 path = QString::fromLatin1("qt5.0");
3560 path = path.prepend(":/stream/").append("/");
3563 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3564 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3570 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3572 QFETCH(QString, fileName);
3574 QFile file(fileName);
3575 QVERIFY(file.open(QIODevice::ReadOnly));
3577 QDataStream stream(&file);
3578 stream.setVersion(version);
3581 QVariant loadedVariant;
3582 stream >> typeName >> loadedVariant;
3584 const int id = QMetaType::type(typeName.toLatin1());
3585 if (id == QMetaType::Void) {
3586 // Void type is not supported by QVariant
3590 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3591 QCOMPARE(constructedVariant.userType(), id);
3592 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3593 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3596 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3598 QFETCH(QString, fileName);
3600 QFile file(fileName);
3601 QVERIFY(file.open(QIODevice::ReadOnly));
3602 QDataStream dataFileStream(&file);
3605 dataFileStream >> typeName;
3606 QByteArray data = file.readAll();
3607 const int id = QMetaType::type(typeName.toLatin1());
3608 if (id == QMetaType::Void) {
3609 // Void type is not supported by QVariant
3614 buffer.open(QIODevice::ReadWrite);
3615 QDataStream stream(&buffer);
3616 stream.setVersion(version);
3618 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3619 QCOMPARE(constructedVariant.userType(), id);
3620 stream << constructedVariant;
3622 // We are testing QVariant there is no point in testing full array.
3623 QCOMPARE(buffer.data().left(5), data.left(5));
3626 QVariant recunstructedVariant;
3627 stream >> recunstructedVariant;
3628 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3631 class MessageHandler {
3633 MessageHandler(const int typeId)
3634 : oldMsgHandler(qInstallMsgHandler(handler))
3641 qInstallMsgHandler(oldMsgHandler);
3644 bool testPassed() const
3649 static void handler(QtMsgType, const char *txt)
3651 QString msg = QString::fromLatin1(txt);
3652 // Format itself is not important, but basic data as a type name should be included in the output
3653 ok = msg.startsWith("QVariant(") + QMetaType::typeName(currentId);
3654 QVERIFY2(ok, (QString::fromLatin1("Message is not valid: '") + msg + '\'').toLatin1().constData());
3657 QtMsgHandler oldMsgHandler;
3658 static int currentId;
3661 bool MessageHandler::ok;
3662 int MessageHandler::currentId;
3664 void tst_QVariant::debugStream_data()
3666 QTest::addColumn<QVariant>("variant");
3667 QTest::addColumn<int>("typeId");
3668 for (int id = 0; id < QMetaType::User; ++id) {
3669 const char *tagName = QMetaType::typeName(id);
3672 if (id != QMetaType::Void) {
3673 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3676 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3677 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3678 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3681 void tst_QVariant::debugStream()
3683 QFETCH(QVariant, variant);
3684 QFETCH(int, typeId);
3686 MessageHandler msgHandler(typeId);
3687 qDebug() << variant;
3688 QVERIFY(msgHandler.testPassed());
3691 QTEST_MAIN(tst_QVariant)
3692 #include "tst_qvariant.moc"