1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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>
69 Q_DECLARE_METATYPE(qlonglong)
70 Q_DECLARE_METATYPE(qulonglong)
71 Q_DECLARE_METATYPE(QPointF)
72 Q_DECLARE_METATYPE(QRectF)
73 Q_DECLARE_METATYPE(QSize)
74 Q_DECLARE_METATYPE(QSizeF)
75 Q_DECLARE_METATYPE(QLine)
76 Q_DECLARE_METATYPE(QLineF)
77 Q_DECLARE_METATYPE(QPoint)
78 Q_DECLARE_METATYPE(QRect)
79 Q_DECLARE_METATYPE(QPixmap)
80 Q_DECLARE_METATYPE(QBrush)
81 Q_DECLARE_METATYPE(QFont)
82 Q_DECLARE_METATYPE(QColor)
83 Q_DECLARE_METATYPE(QKeySequence)
88 class tst_QVariant : public QObject
94 void copy_constructor();
98 void canConvert_data();
131 void toLongLong_data();
134 void toULongLong_data();
137 void toByteArray_data();
140 void toString_data();
149 void toDateTime_data();
152 void toDouble_data();
155 void toPointF_data();
161 void toKeySequence_data();
162 void toKeySequence();
170 void toPixmap_data();
179 void qvariant_cast_QObject_data();
180 void qvariant_cast_QObject();
199 void basicUserType();
203 void writeToReadFromDataStream_data();
204 void writeToReadFromDataStream();
205 void writeToReadFromOldDataStream();
206 void checkDataStream();
208 void operator_eq_eq_data();
209 void operator_eq_eq();
211 void operator_eq_eq_rhs();
213 void typeName_data();
217 void streamInvalidVariant();
221 void data_(); // data is virtual function in QtTestCase
224 void saveLoadCustomTypes();
231 void convertToQUint8() const;
232 void invalidQColor() const;
233 void comparePointers() const;
234 void voidStar() const;
235 void dataStar() const;
236 void canConvertQStringList() const;
237 void canConvertQStringList_data() const;
238 void canConvertMetaTypeToInt() const;
239 void variantToDateTimeWithoutWarnings() const;
240 void invalidDateTime() const;
242 void loadUnknownUserType();
243 void loadBrokenUserType();
245 void invalidDate() const;
246 void compareCustomTypes() const;
247 void timeToDateTime() const;
248 void copyingUserTypes() const;
249 void convertBoolToByteArray() const;
250 void convertBoolToByteArray_data() const;
251 void convertByteArrayToBool() const;
252 void convertByteArrayToBool_data() const;
253 void toIntFromQString() const;
254 void toIntFromDouble() const;
257 void numericalConvert();
258 void moreCustomTypes();
259 void variantInVariant();
263 void forwardDeclare();
266 Q_DECLARE_METATYPE(QDate)
267 Q_DECLARE_METATYPE(QTime)
268 Q_DECLARE_METATYPE(QDateTime)
269 Q_DECLARE_METATYPE(QVariant)
271 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
272 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
274 void tst_QVariant::constructor()
277 QVERIFY( !variant.isValid() );
278 QVERIFY( variant.isNull() );
280 QVariant var2(variant);
281 QVERIFY( !var2.isValid() );
282 QVERIFY( variant.isNull() );
284 QVariant varll(intMax1);
285 QVariant varll2(varll);
286 QCOMPARE(varll2, varll);
288 QVariant var3(QVariant::String);
289 QCOMPARE(var3.typeName(), "QString");
290 QVERIFY(var3.isNull());
291 QVERIFY(var3.isValid());
293 QVariant var4(QVariant::Invalid);
294 QCOMPARE(var4.type(), QVariant::Invalid);
295 QVERIFY(var4.isNull());
296 QVERIFY(!var4.isValid());
298 QVariant var5(QLatin1String("hallo"));
299 QCOMPARE(var5.type(), QVariant::String);
300 QCOMPARE(var5.typeName(), "QString");
302 QVariant var6(qlonglong(0));
303 QCOMPARE(var6.type(), QVariant::LongLong);
304 QCOMPARE(var6.typeName(), "qlonglong");
307 QVERIFY(var7.isValid());
308 QVERIFY(!var7.isNull());
310 var8.setValue<int>(5);
311 QVERIFY(var8.isValid());
312 QVERIFY(!var8.isNull());
315 void tst_QVariant::copy_constructor()
317 QVariant var7(QVariant::Int);
319 QCOMPARE(var8.type(), QVariant::Int);
320 QVERIFY(var8.isNull());
323 void tst_QVariant::isNull()
326 QVERIFY( var.isNull() );
329 QVariant var1( str1 );
330 QVERIFY( var1.isNull() );
332 QVariant var2( QString::null );
333 QVERIFY( var2.isNull() );
335 QVariant var3( QString( "blah" ) );
336 QVERIFY( !var3.isNull() );
339 QVERIFY( !var4.isNull() );
341 QVariant var5 = QString();
342 QVERIFY( var5.isNull() );
344 QVariant var6( QString( "blah" ) );
345 QVERIFY( !var6.isNull() );
347 QVERIFY( var6.isNull() );
348 var6.convert( QVariant::String );
349 QVERIFY( var6.isNull() );
350 QVariant varLL( (qlonglong)0 );
351 QVERIFY( !varLL.isNull() );
352 QVariant var7(QString::null);
353 QVERIFY(var7.isNull());
356 void tst_QVariant::swap()
358 QVariant v1 = 1, v2 = 2.0;
360 QCOMPARE(v1.type(),QVariant::Double);
361 QCOMPARE(v1.toDouble(),2.0);
362 QCOMPARE(v2.type(),QVariant::Int);
363 QCOMPARE(v2.toInt(),1);
366 void tst_QVariant::canConvert_data()
368 QTest::addColumn<QVariant>("val");
369 QTest::addColumn<bool>("BitArrayCast");
370 QTest::addColumn<bool>("BitmapCast");
371 QTest::addColumn<bool>("BoolCast");
372 QTest::addColumn<bool>("BrushCast");
373 QTest::addColumn<bool>("ByteArrayCast");
374 QTest::addColumn<bool>("ColorCast");
375 QTest::addColumn<bool>("CursorCast");
376 QTest::addColumn<bool>("DateCast");
377 QTest::addColumn<bool>("DateTimeCast");
378 QTest::addColumn<bool>("DoubleCast");
379 QTest::addColumn<bool>("FontCast");
380 QTest::addColumn<bool>("ImageCast");
381 QTest::addColumn<bool>("IntCast");
382 QTest::addColumn<bool>("InvalidCast");
383 QTest::addColumn<bool>("KeySequenceCast");
384 QTest::addColumn<bool>("ListCast");
385 QTest::addColumn<bool>("LongLongCast");
386 QTest::addColumn<bool>("MapCast");
387 QTest::addColumn<bool>("PaletteCast");
388 QTest::addColumn<bool>("PenCast");
389 QTest::addColumn<bool>("PixmapCast");
390 QTest::addColumn<bool>("PointCast");
391 QTest::addColumn<bool>("RectCast");
392 QTest::addColumn<bool>("RegionCast");
393 QTest::addColumn<bool>("SizeCast");
394 QTest::addColumn<bool>("SizePolicyCast");
395 QTest::addColumn<bool>("StringCast");
396 QTest::addColumn<bool>("StringListCast");
397 QTest::addColumn<bool>("TimeCast");
398 QTest::addColumn<bool>("UIntCast");
399 QTest::addColumn<bool>("ULongLongCast");
410 // 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
413 QVariant var(QBitArray(0));
414 QTest::newRow("BitArray")
415 << 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;
416 var = qVariantFromValue(QBitmap());
417 QTest::newRow("Bitmap")
418 << 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;
419 var = qVariantFromValue(QBrush());
420 QTest::newRow("Brush")
421 << 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;
422 var = QVariant(QByteArray());
423 QTest::newRow("ByteArray")
424 << 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;
425 var = qVariantFromValue(QColor());
426 QTest::newRow("Color")
427 << 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;
429 var = qVariantFromValue(QCursor());
430 QTest::newRow("Cursor")
431 << 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;
433 var = QVariant(QDate());
434 QTest::newRow("Date")
435 << 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;
436 var = QVariant(QDateTime());
437 QTest::newRow("DateTime")
438 << 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;
439 var = QVariant((double)0.1);
440 QTest::newRow("Double")
441 << 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;
442 var = QVariant(0.1f);
443 QTest::newRow("Float")
444 << 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;
445 var = qVariantFromValue(QFont());
446 QTest::newRow("Font")
447 << 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;
448 var = qVariantFromValue(QIcon());
449 QTest::newRow("Icon")
450 << 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;
451 var = qVariantFromValue(QImage());
452 QTest::newRow("Image")
453 << 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;
454 var = QVariant((int)1);
456 << 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;
458 QTest::newRow("Invalid")
459 << 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;
460 var = qVariantFromValue(QKeySequence());
461 QTest::newRow("KeySequence")
462 << 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;
463 var = QVariant(QList<QVariant>());
464 QTest::newRow("List")
465 << 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;
466 var = QVariant((qlonglong)1);
467 QTest::newRow("LongLong")
468 << 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;
469 var = QVariant(QMap<QString,QVariant>());
471 << 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;
472 var = qVariantFromValue(QPalette());
473 QTest::newRow("Palette")
474 << 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;
475 var = qVariantFromValue(QPen());
477 << 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;
478 var = qVariantFromValue(QPixmap());
479 QTest::newRow("Pixmap")
480 << 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;
481 var = qVariantFromValue(QPolygon());
482 QTest::newRow("PointArray")
483 << 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;
484 var = QVariant(QPoint());
485 QTest::newRow("Point")
486 << 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;
487 var = QVariant(QRect());
488 QTest::newRow("Rect")
489 << 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;
490 var = qVariantFromValue(QRegion());
491 QTest::newRow("Region")
492 << 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;
493 var = QVariant(QSize());
494 QTest::newRow("Size")
495 << 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;
496 var = qVariantFromValue(QSizePolicy());
497 QTest::newRow("SizePolicy")
498 << 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;
499 var = QVariant(QString());
500 QTest::newRow("String")
501 << 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;
502 var = QVariant(QStringList("entry"));
503 QTest::newRow("StringList")
504 << 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;
505 var = QVariant(QTime());
506 QTest::newRow("Time")
507 << 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;
508 var = QVariant((uint)1);
509 QTest::newRow("UInt")
510 << 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;
511 var = QVariant((qulonglong)1);
512 QTest::newRow("ULongLong")
513 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
519 void tst_QVariant::canConvert()
521 QFETCH(QVariant, val);
522 QFETCH(bool, BitArrayCast);
523 QFETCH(bool, BitmapCast);
524 QFETCH(bool, BoolCast);
525 QFETCH(bool, BrushCast);
526 QFETCH(bool, ByteArrayCast);
527 QFETCH(bool, ColorCast);
528 QFETCH(bool, CursorCast);
529 QFETCH(bool, DateCast);
530 QFETCH(bool, DateTimeCast);
531 QFETCH(bool, DoubleCast);
532 QFETCH(bool, FontCast);
533 QFETCH(bool, ImageCast);
534 QFETCH(bool, IntCast);
535 QFETCH(bool, InvalidCast);
536 QFETCH(bool, KeySequenceCast);
537 QFETCH(bool, ListCast);
538 QFETCH(bool, LongLongCast);
539 QFETCH(bool, MapCast);
540 QFETCH(bool, PaletteCast);
541 QFETCH(bool, PenCast);
542 QFETCH(bool, PixmapCast);
543 QFETCH(bool, PointCast);
544 QFETCH(bool, RectCast);
545 QFETCH(bool, RegionCast);
546 QFETCH(bool, SizeCast);
547 QFETCH(bool, SizePolicyCast);
548 QFETCH(bool, StringCast);
549 QFETCH(bool, StringListCast);
550 QFETCH(bool, TimeCast);
551 QFETCH(bool, UIntCast);
552 QFETCH(bool, ULongLongCast);
554 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
555 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
556 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
557 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
558 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
559 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
560 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
561 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
562 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
563 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
564 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
565 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
566 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
567 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
568 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
569 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
570 QCOMPARE(val.canConvert(QVariant::List), ListCast);
571 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
572 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
573 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
574 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
575 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
576 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
577 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
578 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
579 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
580 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
581 QCOMPARE(val.canConvert(QVariant::String), StringCast);
582 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
583 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
584 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
585 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
588 void tst_QVariant::toInt_data()
590 QTest::addColumn<QVariant>("value");
591 QTest::addColumn<int>("result");
592 QTest::addColumn<bool>("valueOK");
594 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
595 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
596 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
597 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
598 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
599 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
600 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
601 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
602 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
603 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
604 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
605 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
606 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
607 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
608 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
609 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
610 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
611 QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
612 QByteArray bytearray(4, ' ');
617 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
622 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
625 void tst_QVariant::toInt()
627 QFETCH( QVariant, value );
628 QFETCH( int, result );
629 QFETCH( bool, valueOK );
630 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
632 int i = value.toInt( &ok );
633 QCOMPARE( i, result );
634 QVERIFY( ok == valueOK );
637 void tst_QVariant::toUInt_data()
639 QTest::addColumn<QVariant>("value");
640 QTest::addColumn<uint>("result");
641 QTest::addColumn<bool>("valueOK");
643 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
644 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
645 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
646 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
647 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
648 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
649 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
650 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
651 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
652 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
653 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
654 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
655 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
656 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
657 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
658 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
659 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
660 QByteArray bytearray(4, ' ');
665 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
668 void tst_QVariant::toUInt()
670 QFETCH( QVariant, value );
671 QFETCH( uint, result );
672 QFETCH( bool, valueOK );
673 QVERIFY( value.isValid() );
674 QVERIFY( value.canConvert( QVariant::UInt ) );
677 uint i = value.toUInt( &ok );
678 QVERIFY( ok == valueOK );
679 QCOMPARE( i, result );
683 void tst_QVariant::toSize_data()
685 QTest::addColumn<QVariant>("value");
686 QTest::addColumn<QSize>("result");
687 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
688 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
689 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
690 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
693 void tst_QVariant::toSize()
695 QFETCH( QVariant, value );
696 QFETCH( QSize, result );
697 QVERIFY( value.isValid() );
698 QVERIFY( value.canConvert( QVariant::Size ) );
700 QSize i = value.toSize();
701 QCOMPARE( i, result );
704 void tst_QVariant::toSizeF_data()
706 QTest::addColumn<QVariant>("value");
707 QTest::addColumn<QSizeF>("result");
708 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
709 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
710 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
711 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
714 void tst_QVariant::toSizeF()
716 QFETCH( QVariant, value );
717 QFETCH( QSizeF, result );
718 QVERIFY( value.isValid() );
719 QVERIFY( value.canConvert( QVariant::SizeF ) );
721 QSizeF i = value.toSizeF();
722 QCOMPARE( i, result );
725 void tst_QVariant::toLine_data()
727 QTest::addColumn<QVariant>("value");
728 QTest::addColumn<QLine>("result");
729 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
730 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
731 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
732 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
735 void tst_QVariant::toLine()
737 QFETCH( QVariant, value );
738 QFETCH( QLine, result );
739 QVERIFY( value.isValid() );
740 QVERIFY( value.canConvert( QVariant::Line ) );
742 QLine i = value.toLine();
743 QCOMPARE( i, result );
746 void tst_QVariant::toLineF_data()
748 QTest::addColumn<QVariant>("value");
749 QTest::addColumn<QLineF>("result");
750 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
751 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
752 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
753 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
756 void tst_QVariant::toLineF()
758 QFETCH( QVariant, value );
759 QFETCH( QLineF, result );
760 QVERIFY( value.isValid() );
761 QVERIFY( value.canConvert( QVariant::LineF ) );
763 QLineF i = value.toLineF();
764 QCOMPARE( i, result );
767 void tst_QVariant::toPoint_data()
769 QTest::addColumn<QVariant>("value");
770 QTest::addColumn<QPoint>("result");
771 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
772 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
773 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
774 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
775 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
778 void tst_QVariant::toPoint()
780 QFETCH( QVariant, value );
781 QFETCH( QPoint, result );
782 QVERIFY( value.isValid() );
783 QVERIFY( value.canConvert( QVariant::Point ) );
784 QPoint i = value.toPoint();
785 QCOMPARE( i, result );
788 void tst_QVariant::toRect_data()
790 QTest::addColumn<QVariant>("value");
791 QTest::addColumn<QRect>("result");
792 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
793 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
794 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
795 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
796 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
799 void tst_QVariant::toRect()
801 QFETCH( QVariant, value );
802 QFETCH( QRect, result );
803 QVERIFY( value.isValid() );
804 QVERIFY( value.canConvert( QVariant::Rect ) );
805 QRect i = value.toRect();
806 QCOMPARE( i, result );
809 void tst_QVariant::toChar_data()
811 QTest::addColumn<QVariant>("value");
812 QTest::addColumn<QChar>("result");
813 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
814 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
817 void tst_QVariant::toChar()
819 QFETCH( QVariant, value );
820 QFETCH( QChar, result );
821 QVERIFY( value.isValid() );
822 QVERIFY( value.canConvert( QVariant::Char ) );
824 QChar i = value.toChar();
825 QCOMPARE( i, result );
828 void tst_QVariant::toBool_data()
830 QTest::addColumn<QVariant>("value");
831 QTest::addColumn<bool>("result");
833 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
834 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
835 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
836 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
837 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
838 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
839 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
840 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
841 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
842 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
843 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
844 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
845 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
846 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
847 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
848 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
849 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
850 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
853 void tst_QVariant::toBool()
855 QFETCH( QVariant, value );
856 QFETCH( bool, result );
857 QVERIFY( value.isValid() );
858 QVERIFY( value.canConvert( QVariant::Bool ) );
860 bool i = value.toBool();
861 QCOMPARE( i, result );
864 void tst_QVariant::toPointF_data()
866 QTest::addColumn<QVariant>("value");
867 QTest::addColumn<QPointF>("result");
869 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
872 void tst_QVariant::toPointF()
874 QFETCH( QVariant, value );
875 QFETCH( QPointF, result );
876 QVERIFY( value.isValid() );
877 QVERIFY( value.canConvert( QVariant::PointF ) );
878 QPointF d = value.toPointF();
879 QCOMPARE( d, result );
882 void tst_QVariant::toRectF_data()
884 QTest::addColumn<QVariant>("value");
885 QTest::addColumn<QRectF>("result");
887 QRect r( 1, 9, 8, 4 );
888 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
889 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
892 void tst_QVariant::toRectF()
894 QFETCH( QVariant, value );
895 QFETCH( QRectF, result );
896 QVERIFY( value.isValid() );
897 QVERIFY( value.canConvert( QVariant::RectF ) );
898 QRectF d = value.toRectF();
899 QCOMPARE( d, result );
902 void tst_QVariant::toColor_data()
904 QTest::addColumn<QVariant>("value");
905 QTest::addColumn<QColor>("result");
908 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
909 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
912 void tst_QVariant::toColor()
914 QFETCH( QVariant, value );
915 QFETCH( QColor, result );
916 QVERIFY( value.isValid() );
917 QVERIFY( value.canConvert( QVariant::Color ) );
918 QColor d = qVariantValue<QColor>(value);
919 QCOMPARE( d, result );
922 void tst_QVariant::toPixmap_data()
924 QTest::addColumn<QVariant>("value");
925 QTest::addColumn<QPixmap>("result");
929 QTest::newRow( "image" ) << QVariant( pm ) << pm;
933 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
936 void tst_QVariant::toPixmap()
938 QFETCH( QVariant, value );
939 QFETCH( QPixmap, result );
940 QVERIFY( value.isValid() );
941 QVERIFY( value.canConvert( QVariant::Pixmap ) );
942 QPixmap d = qVariantValue<QPixmap>(value);
943 QCOMPARE( d, result );
946 void tst_QVariant::toImage_data()
948 QTest::addColumn<QVariant>("value");
949 QTest::addColumn<QImage>("result");
951 QImage im(30, 30, QImage::Format_ARGB32);
953 QTest::newRow( "image" ) << QVariant( im ) << im;
956 void tst_QVariant::toImage()
958 QFETCH( QVariant, value );
959 QFETCH( QImage, result );
960 QVERIFY( value.isValid() );
961 QVERIFY( value.canConvert( QVariant::Image ) );
962 QImage d = qVariantValue<QImage>(value);
963 QCOMPARE( d, result );
966 void tst_QVariant::toBrush_data()
968 QTest::addColumn<QVariant>("value");
969 QTest::addColumn<QBrush>("result");
972 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
975 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
978 void tst_QVariant::toBrush()
980 QFETCH( QVariant, value );
981 QFETCH( QBrush, result );
982 QVERIFY( value.isValid() );
983 QVERIFY( value.canConvert( QVariant::Brush ) );
984 QBrush d = qVariantValue<QBrush>(value);
985 QCOMPARE( d, result );
988 void tst_QVariant::toFont_data()
990 QTest::addColumn<QVariant>("value");
991 QTest::addColumn<QFont>("result");
993 QFont f("times",12,-1,false);
994 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
997 void tst_QVariant::toFont()
999 QFETCH( QVariant, value );
1000 QFETCH( QFont, result );
1001 QVERIFY( value.isValid() );
1002 QVERIFY( value.canConvert( QVariant::Font ) );
1003 QFont d = qVariantValue<QFont>(value);
1004 QCOMPARE( d, result );
1007 void tst_QVariant::toKeySequence_data()
1009 QTest::addColumn<QVariant>("value");
1010 QTest::addColumn<QKeySequence>("result");
1013 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1016 QTest::newRow( "qstring" )
1017 << QVariant( QString( "Ctrl+A" ) )
1018 << QKeySequence( Qt::CTRL + Qt::Key_A );
1021 void tst_QVariant::toKeySequence()
1023 QFETCH( QVariant, value );
1024 QFETCH( QKeySequence, result );
1025 QVERIFY( value.isValid() );
1026 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1027 QKeySequence d = qVariantValue<QKeySequence>(value);
1028 QCOMPARE( d, result );
1031 void tst_QVariant::toDouble_data()
1033 QTest::addColumn<QVariant>("value");
1034 QTest::addColumn<double>("result");
1035 QTest::addColumn<bool>("valueOK");
1037 QByteArray bytearray(4, ' ');
1042 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1045 void tst_QVariant::toDouble()
1047 QFETCH( QVariant, value );
1048 QFETCH( double, result );
1049 QFETCH( bool, valueOK );
1050 QVERIFY( value.isValid() );
1051 QVERIFY( value.canConvert( QVariant::Double ) );
1053 double d = value.toDouble( &ok );
1054 QCOMPARE( d, result );
1055 QVERIFY( ok == valueOK );
1058 void tst_QVariant::toLongLong_data()
1060 QTest::addColumn<QVariant>("value");
1061 QTest::addColumn<qlonglong>("result");
1062 QTest::addColumn<bool>("valueOK");
1064 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1065 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1066 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1067 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1068 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1069 << (qlonglong)123 << true;
1070 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1072 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1073 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1074 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1075 QByteArray bytearray(4, ' ');
1080 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1083 void tst_QVariant::toLongLong()
1085 QFETCH( QVariant, value );
1086 QFETCH( qlonglong, result );
1087 QFETCH( bool, valueOK );
1088 QVERIFY( value.isValid() );
1089 QVERIFY( value.canConvert( QVariant::LongLong ) );
1091 qlonglong ll = value.toLongLong( &ok );
1092 QCOMPARE( ll, result );
1093 QVERIFY( ok == valueOK );
1096 void tst_QVariant::toULongLong_data()
1098 QTest::addColumn<QVariant>("value");
1099 QTest::addColumn<qulonglong>("result");
1100 QTest::addColumn<bool>("valueOK");
1102 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1103 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1104 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1105 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1106 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1107 << (qulonglong)123 << true;
1108 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1110 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1111 << Q_UINT64_C(18446744073709551615)
1113 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1114 << Q_UINT64_C(18446744073709551615)
1116 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1117 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1118 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1119 QByteArray bytearray(4, ' ');
1124 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1127 void tst_QVariant::toULongLong()
1129 QFETCH( QVariant, value );
1130 QFETCH( qulonglong, result );
1131 QFETCH( bool, valueOK );
1132 QVERIFY( value.isValid() );
1133 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1135 qulonglong ll = value.toULongLong( &ok );
1136 QCOMPARE( ll, result );
1137 QVERIFY( ok == valueOK );
1140 void tst_QVariant::toByteArray_data()
1142 QTest::addColumn<QVariant>("value");
1143 QTest::addColumn<QByteArray>("result");
1145 QByteArray ba(5, ' ');
1152 QByteArray variantBa = ba;
1154 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1155 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1156 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1157 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1158 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1159 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1160 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1163 void tst_QVariant::toByteArray()
1165 QFETCH( QVariant, value );
1166 QFETCH( QByteArray, result );
1167 QVERIFY( value.isValid() );
1168 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1169 QByteArray ba = value.toByteArray();
1170 QCOMPARE( ba, result );
1173 void tst_QVariant::toString_data()
1175 QTest::addColumn<QVariant>("value");
1176 QTest::addColumn<QString>("result");
1178 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1179 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1180 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1181 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1182 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1183 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1184 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1185 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1186 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1187 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1188 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1189 QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1191 << QString( "Ctrl+A" );
1193 << QString(QChar(0x2318)) + "A";
1196 QFont font( "times", 12 );
1197 QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1198 QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1199 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1200 QString( "123456789012" );
1203 void tst_QVariant::toString()
1205 QFETCH( QVariant, value );
1206 QFETCH( QString, result );
1207 QVERIFY( value.isValid() );
1208 QVERIFY( value.canConvert( QVariant::String ) );
1209 QString str = value.toString();
1210 QCOMPARE( str, result );
1213 void tst_QVariant::toDate_data()
1215 QTest::addColumn<QVariant>("value");
1216 QTest::addColumn<QDate>("result");
1218 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1219 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1220 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1223 void tst_QVariant::toDate()
1225 QFETCH( QVariant, value );
1226 QFETCH( QDate, result );
1227 QVERIFY( value.isValid() );
1228 QVERIFY( value.canConvert( QVariant::Date ) );
1229 QCOMPARE( value.toDate(), result );
1232 void tst_QVariant::toTime_data()
1234 QTest::addColumn<QVariant>("value");
1235 QTest::addColumn<QTime>("result");
1237 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1238 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1239 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1242 void tst_QVariant::toTime()
1244 QFETCH( QVariant, value );
1245 QFETCH( QTime, result );
1246 QVERIFY( value.isValid() );
1247 QVERIFY( value.canConvert( QVariant::Time ) );
1248 QCOMPARE( value.toTime(), result );
1251 void tst_QVariant::toDateTime_data()
1253 QTest::addColumn<QVariant>("value");
1254 QTest::addColumn<QDateTime>("result");
1256 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1257 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1258 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1259 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1262 void tst_QVariant::toDateTime()
1264 QFETCH( QVariant, value );
1265 QFETCH( QDateTime, result );
1266 QVERIFY( value.isValid() );
1267 QVERIFY( value.canConvert( QVariant::DateTime ) );
1268 QCOMPARE( value.toDateTime(), result );
1271 void tst_QVariant::toLocale()
1274 QLocale loc = variant.toLocale();
1275 variant = QLocale::system();
1276 loc = variant.toLocale();
1279 void tst_QVariant::toRegExp()
1282 QRegExp rx = variant.toRegExp();
1283 variant = QRegExp("foo");
1284 rx = variant.toRegExp();
1287 void tst_QVariant::matrix()
1290 QMatrix matrix = qVariantValue<QMatrix>(variant);
1291 QVERIFY(matrix.isIdentity());
1292 qVariantSetValue(variant, QMatrix().rotate(90));
1293 QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1295 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1297 QMetaType::destroy(QVariant::Matrix, mmatrix);
1300 void tst_QVariant::matrix4x4()
1303 QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1304 QVERIFY(matrix.isIdentity());
1307 qVariantSetValue(variant, m);
1308 QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1310 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1312 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1315 void tst_QVariant::transform()
1318 QTransform matrix = qVariantValue<QTransform>(variant);
1319 QVERIFY(matrix.isIdentity());
1320 qVariantSetValue(variant, QTransform().rotate(90));
1321 QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1323 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1325 QMetaType::destroy(QVariant::Transform, mmatrix);
1329 void tst_QVariant::vector2D()
1332 QVector2D vector = qVariantValue<QVector2D>(variant);
1333 QVERIFY(vector.isNull());
1334 qVariantSetValue(variant, QVector2D(0.1, 0.2));
1335 QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1337 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1339 QMetaType::destroy(QVariant::Vector2D, pvector);
1342 void tst_QVariant::vector3D()
1345 QVector3D vector = qVariantValue<QVector3D>(variant);
1346 QVERIFY(vector.isNull());
1347 qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1348 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1350 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1352 QMetaType::destroy(QVariant::Vector3D, pvector);
1355 void tst_QVariant::vector4D()
1358 QVector4D vector = qVariantValue<QVector4D>(variant);
1359 QVERIFY(vector.isNull());
1360 qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1361 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1363 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1365 QMetaType::destroy(QVariant::Vector4D, pvector);
1368 void tst_QVariant::quaternion()
1371 QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1372 QVERIFY(quaternion.isIdentity());
1373 qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1374 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1376 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1377 QVERIFY(pquaternion);
1378 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1381 void tst_QVariant::writeToReadFromDataStream_data()
1384 QTest::addColumn<QVariant>("writeVariant");
1385 QTest::addColumn<bool>("isNull");
1387 typedef QList<QVariant> variantsList;
1388 variantsList valuelist;
1389 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1390 QVariant var(valuelist);
1391 QTest::newRow( "list_valid" ) << var << false;
1394 QTest::newRow( "invalid" ) << QVariant() << true;
1395 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1396 QBitArray bitarray( 3 );
1400 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1401 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1402 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1403 QByteArray bytearray(5, ' ');
1408 bytearray[4] = '\0';
1409 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1410 QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1411 QBitmap bitmap( 10, 10 );
1412 bitmap.fill( Qt::red );
1413 QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1414 QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1415 QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1416 #ifndef QT_NO_CURSOR
1417 QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1419 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1420 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1421 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1422 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1423 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1424 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1425 QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1426 QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1427 QPixmap pixmap( 10, 10 );
1428 pixmap.fill( Qt::red );
1429 QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1430 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1431 QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1432 QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1433 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1434 typedef QMap<QString, QVariant> variantsMap;
1436 vMap.insert( "int", QVariant( 1 ) );
1437 vMap.insert( "string", QVariant( QString("Two") ) );
1438 vMap.insert( "double", QVariant( 3.45 ) );
1439 vMap.insert( "float", QVariant( 3.45f ) );
1440 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1441 QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1442 QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1443 QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1444 QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1445 QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1446 QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1447 QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1448 QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1449 QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1450 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1451 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1452 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1453 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1454 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1455 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1456 QStringList stringlist;
1457 stringlist << "One" << "Two" << "Three";
1458 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1459 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1460 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1461 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1462 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1463 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1464 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1465 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1467 // types known to QMetaType, but not part of QVariant::Type
1468 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1470 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1471 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1473 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1474 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1476 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1477 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1478 ulong ulongInt = 1ul;
1479 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1480 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1481 ushort ushortInt = 1u;
1482 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1483 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1485 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1486 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1488 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1491 void tst_QVariant::writeToReadFromDataStream()
1493 QFETCH( QVariant, writeVariant );
1494 QFETCH( bool, isNull );
1497 QDataStream writeStream( &data, QIODevice::WriteOnly );
1498 writeStream << writeVariant;
1500 QVariant readVariant;
1501 QDataStream readStream( &data, QIODevice::ReadOnly );
1502 readStream >> readVariant;
1503 QVERIFY( readVariant.isNull() == isNull );
1504 // Best way to confirm the readVariant contains the same data?
1505 // Since only a few won't match since the serial numbers are different
1506 // I won't bother adding another bool in the data test.
1507 QVariant::Type writeType = writeVariant.type();
1508 if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1509 && writeType != QVariant::Image) {
1510 switch (writeType) {
1512 QCOMPARE( readVariant, writeVariant );
1515 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1516 case QMetaType::Long:
1517 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1519 case QMetaType::ULong:
1520 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1522 case QMetaType::Short:
1523 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1525 case QMetaType::UShort:
1526 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1528 case QMetaType::Char:
1529 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1531 case QMetaType::UChar:
1532 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1534 case QMetaType::Float:
1536 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1537 float readFloat = qvariant_cast<float>(readVariant);
1538 float writtenFloat = qvariant_cast<float>(writeVariant);
1539 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1540 if (!qIsNaN(readFloat))
1541 QVERIFY(readFloat == writtenFloat);
1548 void tst_QVariant::writeToReadFromOldDataStream()
1550 QVariant writeVariant = QString("hello");
1553 QDataStream writeStream(&data, QIODevice::WriteOnly);
1554 writeStream.setVersion(QDataStream::Qt_2_1);
1555 writeStream << writeVariant;
1557 QVariant readVariant;
1558 QDataStream readStream(&data, QIODevice::ReadOnly);
1559 readStream.setVersion(QDataStream::Qt_2_1);
1560 readStream >> readVariant;
1562 QCOMPARE(writeVariant.userType(), readVariant.userType());
1563 QCOMPARE(writeVariant, readVariant);
1566 void tst_QVariant::checkDataStream()
1568 const QByteArray settingsHex("0000002effffffffff");
1569 const QByteArray settings = QByteArray::fromHex(settingsHex);
1570 QDataStream in(settings);
1573 // the line below has been left out for now since the data stream
1574 // is not necessarily considered corrupt when an invalid QVariant is
1575 // constructed. However, it might be worth considering changing that behavior
1577 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1578 QCOMPARE(v.type(), QVariant::Invalid);
1581 void tst_QVariant::operator_eq_eq_data()
1583 QTest::addColumn<QVariant>("left");
1584 QTest::addColumn<QVariant>("right");
1585 QTest::addColumn<bool>("equal"); // left == right ?
1588 QVariant i0( int(0) );
1589 QVariant i1( int(1) );
1591 QTest::newRow( "invinv" ) << inv << inv << true;
1593 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1594 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1595 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1597 // LongLong and ULongLong
1598 QVariant ll1( (qlonglong)1 );
1599 QVariant lln2( (qlonglong)-2 );
1600 QVariant ull1( (qulonglong)1 );
1601 QVariant ull3( (qulonglong)3 );
1602 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1603 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1604 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1605 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1606 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1607 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1610 QVariant mIntString(QByteArray("-42"));
1611 QVariant mIntQString(QString("-42"));
1613 QVariant mUInt(42u);
1614 QVariant mUIntString(QByteArray("42"));
1615 QVariant mUIntQString(QString("42"));
1617 QVariant mDouble(42.11);
1618 QVariant mDoubleString(QByteArray("42.11"));
1619 QVariant mDoubleQString(QString("42.11"));
1621 QVariant mFloat(42.11f);
1622 QVariant mFloatString(QByteArray("42.11"));
1623 QVariant mFloatQString(QString("42.11"));
1625 QVariant mLongLong((qlonglong)-42);
1626 QVariant mLongLongString(QByteArray("-42"));
1627 QVariant mLongLongQString(QString("-42"));
1629 QVariant mULongLong((qulonglong)42);
1630 QVariant mULongLongString(QByteArray("42"));
1631 QVariant mULongLongQString(QString("42"));
1633 QVariant mBool(false);
1634 QVariant mBoolString(QByteArray("false"));
1635 QVariant mBoolQString(QString("false"));
1637 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1638 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1639 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1640 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1641 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1642 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1644 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1645 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1646 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1647 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1649 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1650 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1651 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1652 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1654 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1655 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1656 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1657 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1659 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1660 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1661 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1662 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1664 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1665 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1666 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1667 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1669 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1670 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1671 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1672 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1674 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1675 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1676 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1677 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1679 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1680 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1681 // ### many other combinations missing
1684 QMap<QString, QVariant> map1;
1685 map1.insert( "X", 1 );
1687 QMap<QString, QVariant> map2;
1688 map2.insert( "Y", 1 );
1690 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1695 QMap<QString, QVariant> map1;
1696 map1.insert( "X", 1 );
1698 QMap<QString, QVariant> map2;
1699 map2.insert( "X", 1 );
1701 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1705 QMap<QString, QVariant> map1;
1706 map1.insert( "X", 1 );
1708 QMap<QString, QVariant> map2;
1710 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1714 QMap<QString, QVariant> map1;
1716 QMap<QString, QVariant> map2;
1717 map2.insert( "X", 1 );
1719 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1723 QMap<QString, QVariant> map1;
1724 map1.insert( "X", 1 );
1725 map1.insert( "Y", 1 );
1727 QMap<QString, QVariant> map2;
1728 map2.insert( "X", 1 );
1730 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1734 QMap<QString, QVariant> map1;
1735 map1.insert( "X", 1 );
1737 QMap<QString, QVariant> map2;
1738 map2.insert( "X", 1 );
1739 map2.insert( "Y", 1 );
1741 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1744 // same thing with hash
1746 QHash<QString, QVariant> hash1;
1747 hash1.insert( "X", 1 );
1749 QHash<QString, QVariant> hash2;
1750 hash2.insert( "Y", 1 );
1752 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1757 QHash<QString, QVariant> hash1;
1758 hash1.insert( "X", 1 );
1760 QHash<QString, QVariant> hash2;
1761 hash2.insert( "X", 1 );
1763 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1767 QHash<QString, QVariant> hash1;
1768 hash1.insert( "X", 1 );
1770 QHash<QString, QVariant> hash2;
1772 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1776 QHash<QString, QVariant> hash1;
1778 QHash<QString, QVariant> hash2;
1779 hash2.insert( "X", 1 );
1781 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1785 QHash<QString, QVariant> hash1;
1786 hash1.insert( "X", 1 );
1787 hash1.insert( "Y", 1 );
1789 QHash<QString, QVariant> hash2;
1790 hash2.insert( "X", 1 );
1792 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1796 QHash<QString, QVariant> hash1;
1797 hash1.insert( "X", 1 );
1799 QHash<QString, QVariant> hash2;
1800 hash2.insert( "X", 1 );
1801 hash2.insert( "Y", 1 );
1803 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1806 QTest::newRow( "UserType" ) << QVariant(QVariant::UserType) << QVariant(QVariant::UserType) << true;
1807 QVariant mUserType(QVariant::UserType);
1808 QTest::newRow( "Shared UserType" ) << mUserType << mUserType << true;
1811 void tst_QVariant::operator_eq_eq()
1813 QFETCH( QVariant, left );
1814 QFETCH( QVariant, right );
1815 QFETCH( bool, equal );
1816 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1817 QCOMPARE( left == right, equal );
1820 void tst_QVariant::operator_eq_eq_rhs()
1828 /* This should _not_ compile */
1832 QVERIFY(dt == list);
1836 void tst_QVariant::typeName_data()
1838 QTest::addColumn<int>("type");
1839 QTest::addColumn<QByteArray>("res");
1840 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1841 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1842 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1843 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1844 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1845 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1846 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1847 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1848 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1849 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1850 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1851 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1852 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1853 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1854 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1855 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1856 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1857 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1858 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1859 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1860 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1861 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1862 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1863 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1864 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1865 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1866 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1867 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1868 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1869 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1870 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1871 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1872 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1873 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1874 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1875 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1876 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1877 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1878 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1879 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1880 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1881 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1882 QTest::newRow("42") << int(QVariant::UserType) << QByteArray("UserType");
1883 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1884 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1885 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1886 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1887 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1888 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1889 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1890 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1893 void tst_QVariant::typeName()
1895 QFETCH( int, type );
1896 QFETCH( QByteArray, res );
1897 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1898 QString::fromLatin1(res.constData()));
1901 // test nameToType as well
1902 void tst_QVariant::typeToName()
1905 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1906 // assumes that QVariant::Type contains consecutive values
1908 int max = QVariant::LastGuiType;
1909 for ( int t = 1; t <= max; t++ ) {
1910 const char *n = QVariant::typeToName( (QVariant::Type)t );
1912 QCOMPARE( int(QVariant::nameToType( n )), t );
1915 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1916 // not documented but we return 0 if the type is out of range
1917 // by testing this we catch cases where QVariant is extended
1918 // but type_map is not updated accordingly
1919 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1920 // invalid type names
1921 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
1922 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
1923 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
1924 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Icon);
1927 void tst_QVariant::streamInvalidVariant()
1933 QVariant writeVariant;
1934 QVariant readVariant;
1936 QVERIFY( writeVariant.type() == QVariant::Invalid );
1939 QDataStream writeStream( &data, QIODevice::WriteOnly );
1940 writeStream << writeX << writeVariant << writeY;
1942 QDataStream readStream( &data, QIODevice::ReadOnly );
1943 readStream >> readX >> readVariant >> readY;
1945 QVERIFY( readX == writeX );
1946 // Two invalid QVariant's aren't necessarily the same, so == will
1947 // return false if one is invalid, so check the type() instead
1948 QVERIFY( readVariant.type() == QVariant::Invalid );
1949 QVERIFY( readY == writeY );
1952 static int instanceCount = 0;
1956 MyType(int n = 0, const char *t=0): number(n), text(t)
1960 MyType(const MyType &other)
1961 : number(other.number), text(other.text)
1973 Q_DECLARE_METATYPE(MyType)
1974 Q_DECLARE_METATYPE(MyType*)
1976 void tst_QVariant::userType()
1979 QVariant userVariant(QVariant::UserType);
1981 QVERIFY(userVariant.isValid());
1982 QVERIFY(userVariant.isNull());
1986 MyType data(1, "eins");
1987 MyType data2(2, "zwei");
1991 qVariantSetValue(userVar, data);
1993 QCOMPARE(userVar.type(), QVariant::UserType);
1994 QCOMPARE(userVar.typeName(), "MyType");
1995 QVERIFY(!userVar.isNull());
1996 QVERIFY(!userVar.canConvert(QVariant::String));
1997 QVERIFY(!userVar.canConvert(QVariant::UserType));
1999 QVariant userVar2(userVar);
2000 QVERIFY(userVar == userVar2);
2002 qVariantSetValue(userVar2, data2);
2003 QVERIFY(userVar != userVar2);
2005 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2007 QCOMPARE(varData->number, data.number);
2008 QCOMPARE(varData->text, data.text);
2011 qVariantSetValue(userVar3, data2);
2012 QVERIFY(userVar2 == userVar3);
2013 userVar3 = userVar2;
2014 QVERIFY(userVar2 == userVar3);
2016 // At this point all QVariants got destroyed but we have 2 MyType instances.
2017 QCOMPARE(instanceCount, 2);
2020 qVariantSetValue(userVar, &data);
2022 QCOMPARE(userVar.type(), QVariant::UserType);
2023 QCOMPARE(userVar.typeName(), "MyType*");
2024 QVERIFY(!userVar.isNull());
2025 QVERIFY(!userVar.canConvert(QVariant::String));
2026 QVERIFY(!userVar.canConvert(QVariant::UserType));
2028 QVariant userVar2(userVar);
2029 QVERIFY(userVar == userVar2);
2031 qVariantSetValue(userVar2, &data2);
2032 QVERIFY(userVar != userVar2);
2034 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2036 QCOMPARE(*varData, &data);
2039 qVariantSetValue(userVar3, &data2);
2041 /* This check is correct now. userVar2 contains a pointer to data2 and so
2043 QVERIFY(userVar2 == userVar3);
2045 userVar3 = userVar2;
2046 QVERIFY(userVar2 == userVar3);
2049 QCOMPARE(instanceCount, 2);
2051 qVariantSetValue(myCarrier, data);
2052 QCOMPARE(instanceCount, 3);
2054 QVariant second = myCarrier;
2055 QCOMPARE(instanceCount, 4);
2057 QCOMPARE(instanceCount, 4);
2059 QCOMPARE(instanceCount, 3);
2061 MyType data3(0, "null");
2062 data3 = qVariantValue<MyType>(myCarrier);
2063 QCOMPARE(data3.number, 1);
2064 QCOMPARE(data3.text, (const char *)"eins");
2066 QCOMPARE(instanceCount, 4);
2072 const MyType data(3, "drei");
2075 qVariantSetValue(myCarrier, data);
2076 QCOMPARE(myCarrier.typeName(), "MyType");
2078 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2079 QCOMPARE(data2.number, 3);
2080 QCOMPARE(data2.text, (const char *)"drei");
2087 qVariantSetValue(myCarrier, s);
2088 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2092 qlonglong ll = Q_INT64_C(42);
2095 qVariantSetValue(myCarrier, ll);
2096 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2099 // At this point all QVariants got destroyed and MyType objects too.
2100 QCOMPARE(instanceCount, 0);
2108 Q_DECLARE_METATYPE(MyTypePOD)
2110 void tst_QVariant::podUserType()
2116 QVariant pod_as_variant = qVariantFromValue(pod);
2117 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2119 QCOMPARE(pod.a, pod2.a);
2120 QCOMPARE(pod.b, pod2.b);
2122 qVariantSetValue(pod_as_variant, pod);
2123 pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2125 QCOMPARE(pod.a, pod2.a);
2126 QCOMPARE(pod.b, pod2.b);
2129 void tst_QVariant::basicUserType()
2134 v = QVariant(QMetaType::Int, &i);
2136 QCOMPARE(v.type(), QVariant::Int);
2137 QCOMPARE(v.toInt(), 7);
2141 v = QVariant(QMetaType::QString, &s);
2143 QCOMPARE(v.type(), QVariant::String);
2144 QCOMPARE(v.toString(), QString("foo"));
2148 v = QVariant(QMetaType::Double, &d);
2150 QCOMPARE(v.type(), QVariant::Double);
2151 QCOMPARE(v.toDouble(), 4.4);
2155 v = QVariant(QMetaType::Float, &f);
2157 QCOMPARE(v.userType(), int(QMetaType::Float));
2158 QCOMPARE(v.toDouble(), 4.5);
2161 QByteArray ba("bar");
2162 v = QVariant(QMetaType::QByteArray, &ba);
2164 QCOMPARE(v.type(), QVariant::ByteArray);
2165 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2168 void tst_QVariant::data_()
2175 QVariant ll = (qlonglong)2;
2176 QVariant ull = (qulonglong)3;
2177 QVariant s(QString("hallo"));
2178 QVariant r(QRect(1,2,3,4));
2182 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2186 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2190 QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2194 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2198 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2202 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2206 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2209 void tst_QVariant::constData()
2222 QVERIFY(v.constData());
2223 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2226 QVERIFY(v.constData());
2227 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2230 QVERIFY(v.constData());
2231 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2234 QVERIFY(v.constData());
2235 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2238 QVERIFY(v.constData());
2239 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2242 QVERIFY(v.constData());
2243 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2246 QVERIFY(v.constData());
2247 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2256 Q_DECLARE_METATYPE(Foo)
2258 void tst_QVariant::variant_to()
2267 sl << QLatin1String("blah");
2269 qVariantSetValue(v3, sl);
2274 qVariantSetValue(v4, foo);
2276 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2277 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2278 QCOMPARE(qvariant_cast<int>(v2), 5);
2279 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2280 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2282 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2285 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2287 QCOMPARE(qvariant_cast<int>(v1), 4);
2289 QVariant n = qVariantFromValue<short>(42);
2290 QCOMPARE(qvariant_cast<int>(n), 42);
2291 QCOMPARE(qvariant_cast<uint>(n), 42u);
2292 QCOMPARE(qvariant_cast<double>(n), 42.0);
2293 QCOMPARE(qvariant_cast<float>(n), 42.f);
2294 QCOMPARE(qvariant_cast<short>(n), short(42));
2295 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2297 n = qVariantFromValue(43l);
2298 QCOMPARE(qvariant_cast<int>(n), 43);
2299 QCOMPARE(qvariant_cast<uint>(n), 43u);
2300 QCOMPARE(qvariant_cast<double>(n), 43.0);
2301 QCOMPARE(qvariant_cast<float>(n), 43.f);
2302 QCOMPARE(qvariant_cast<long>(n), 43l);
2304 n = QLatin1String("44");
2305 QCOMPARE(qvariant_cast<int>(n), 44);
2306 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2307 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2309 QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2312 struct Blah { int i; };
2314 QDataStream& operator>>(QDataStream& s, Blah& c)
2315 { return (s >> c.i); }
2317 QDataStream& operator<<(QDataStream& s, const Blah& c)
2318 { return (s << c.i); }
2320 void tst_QVariant::saveLoadCustomTypes()
2325 int tp = qRegisterMetaType<Blah>("Blah");
2326 QVariant v = QVariant(tp, &i);
2328 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2330 QCOMPARE(v.userType(), tp);
2331 QVERIFY(v.type() == QVariant::UserType);
2333 QDataStream stream(&data, QIODevice::WriteOnly);
2340 QDataStream stream(data);
2344 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2345 int value = *(int*)v.constData();
2346 QCOMPARE(value, 42);
2349 void tst_QVariant::url()
2351 QString str("http://qt.nokia.com");
2354 QVariant v(url); //built with a QUrl
2358 QVariant v3(str); //built with a QString
2360 QCOMPARE(v2.toUrl(), url);
2361 QVERIFY(qVariantCanConvert<QUrl>(v3));
2362 QCOMPARE(v2.toUrl(), v3.toUrl());
2364 QVERIFY(qVariantCanConvert<QString>(v2));
2365 QCOMPARE(v2.toString(), str);
2366 QCOMPARE(v3.toString(), str);
2369 void tst_QVariant::globalColor()
2371 QVariant variant(Qt::blue);
2372 QVERIFY(variant.type() == QVariant::Color);
2373 QVERIFY(qVariantValue<QColor>(variant) == QColor(Qt::blue));
2376 void tst_QVariant::variantMap()
2378 QMap<QString, QVariant> map;
2382 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2384 QCOMPARE(map2.value("test").toInt(), 42);
2386 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2387 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2389 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2390 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2393 void tst_QVariant::variantHash()
2395 QHash<QString, QVariant> hash;
2399 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2401 QCOMPARE(hash2.value("test").toInt(), 42);
2403 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2404 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2406 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2407 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2410 void tst_QVariant::invalidQColor() const
2412 QVariant va("An invalid QColor::name() value.");
2413 QVERIFY(va.canConvert(QVariant::Color));
2415 QVERIFY(!va.convert(QVariant::Color));
2417 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2420 void tst_QVariant::qvariant_cast_QObject_data() {
2422 QTest::addColumn<QVariant>("data");
2423 QTest::addColumn<bool>("success");
2424 QObject *obj = new QObject(this);
2425 obj->setObjectName(QString::fromLatin1("Hello"));
2426 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2427 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2428 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2429 QTest::newRow("from int") << QVariant((int) 123) << false;
2433 void tst_QVariant::qvariant_cast_QObject() {
2434 QFETCH(QVariant, data);
2435 QFETCH(bool, success);
2437 QObject *o = qvariant_cast<QObject *>(data);
2438 QCOMPARE(o != 0, success);
2440 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2444 Q_DECLARE_METATYPE(qint8);
2446 void tst_QVariant::convertToQUint8() const
2450 const qint8 anInt = 32;
2452 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2453 * It's of type QVariant::Int. */
2454 const QVariant v0 = anInt;
2456 QVERIFY(qVariantCanConvert<qint8>(v0));
2457 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2458 QCOMPARE(int(v0.toInt()), 32);
2459 QCOMPARE(v0.toString(), QString("32"));
2461 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2462 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2463 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2464 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2465 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2466 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2471 const quint8 anInt = 32;
2472 const QVariant v0 = anInt;
2474 QVERIFY(qVariantCanConvert<quint8>(v0));
2475 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2476 QCOMPARE(int(v0.toUInt()), 32);
2477 QCOMPARE(v0.toString(), QString("32"));
2482 const qint16 anInt = 32;
2483 const QVariant v0 = anInt;
2485 QVERIFY(qVariantCanConvert<qint16>(v0));
2486 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2487 QCOMPARE(int(v0.toInt()), 32);
2488 QCOMPARE(v0.toString(), QString("32"));
2493 const quint16 anInt = 32;
2494 const QVariant v0 = anInt;
2496 QVERIFY(qVariantCanConvert<quint16>(v0));
2497 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2498 QCOMPARE(int(v0.toUInt()), 32);
2499 QCOMPARE(v0.toString(), QString("32"));
2503 void tst_QVariant::comparePointers() const
2511 QVariant v = qVariantFromValue<void *>(&myClass);
2512 QVariant v2 = qVariantFromValue<void *>(&myClass);
2518 Q_DECLARE_METATYPE(Data*)
2520 void tst_QVariant::voidStar() const
2527 v1 = qVariantFromValue(p1);
2531 v2 = qVariantFromValue(p2);
2535 v2 = qVariantFromValue(p2);
2539 void tst_QVariant::dataStar() const
2541 qRegisterMetaType<Data*>();
2542 Data *p1 = new Data;
2544 QVariant v1 = qVariantFromValue(p1);
2545 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2546 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2551 v2 = qVariantFromValue(p1);
2556 void tst_QVariant::canConvertQStringList() const
2558 QFETCH(bool, canConvert);
2559 QFETCH(QStringList, input);
2560 QFETCH(QString, result);
2564 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2565 QCOMPARE(v.toString(), result);
2568 void tst_QVariant::canConvertQStringList_data() const
2570 QTest::addColumn<bool>("canConvert");
2571 QTest::addColumn<QStringList>("input");
2572 QTest::addColumn<QString>("result");
2574 QTest::newRow("An empty list") << false << QStringList() << QString();
2575 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2576 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2581 QTest::newRow("Two items") << false << l << QString();
2584 QTest::newRow("Three items") << false << l << QString();
2587 template<typename T> void convertMetaType()
2589 QVERIFY(qVariantFromValue<T>(10).isValid());
2590 QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2591 QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2592 QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2595 #define CONVERT_META_TYPE(Type) \
2596 convertMetaType<Type>(); \
2597 if (QTest::currentTestFailed()) \
2598 QFAIL("convertMetaType<" #Type "> failed");
2600 void tst_QVariant::canConvertMetaTypeToInt() const
2602 CONVERT_META_TYPE(long);
2603 CONVERT_META_TYPE(short);
2604 CONVERT_META_TYPE(short);
2605 CONVERT_META_TYPE(unsigned short);
2606 CONVERT_META_TYPE(ushort);
2607 CONVERT_META_TYPE(ulong);
2608 CONVERT_META_TYPE(unsigned long);
2609 CONVERT_META_TYPE(uchar);
2610 CONVERT_META_TYPE(unsigned char);
2611 CONVERT_META_TYPE(char);
2612 CONVERT_META_TYPE(uint);
2613 CONVERT_META_TYPE(unsigned int);
2616 #undef CONVERT_META_TYPE
2619 These calls should not produce any warnings.
2621 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2624 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2625 const QDateTime dateTime(variant.toDateTime());
2626 QVERIFY(!dateTime.isValid());
2630 QVariant v1(QLatin1String("xyz"));
2631 v1.convert(QVariant::DateTime);
2633 QVariant v2(QLatin1String("xyz"));
2634 QDateTime dt1(v2.toDateTime());
2636 const QVariant v3(QLatin1String("xyz"));
2637 const QDateTime dt2(v3.toDateTime());
2641 void tst_QVariant::invalidDateTime() const
2643 QVariant variant(QString::fromLatin1("Invalid date time string"));
2644 QVERIFY(!variant.toDateTime().isValid());
2645 QVERIFY(!variant.convert(QVariant::DateTime));
2650 MyClass() : myValue(0) {}
2654 Q_DECLARE_METATYPE( MyClass )
2656 void tst_QVariant::loadUnknownUserType()
2658 qRegisterMetaType<MyClass>("MyClass");
2659 char data[] = {0, 0, 0, 127, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0};
2661 QByteArray ba(data, sizeof(data));
2662 QDataStream ds(&ba, QIODevice::ReadOnly);
2665 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2668 void tst_QVariant::loadBrokenUserType()
2670 char data[] = {0, 0, 0, 127, 0, 112 };
2672 QByteArray ba(data, sizeof(data));
2673 QDataStream ds(&ba, QIODevice::ReadOnly);
2676 QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2679 void tst_QVariant::invalidDate() const
2681 QString foo("Hello");
2682 QVariant variant(foo);
2683 QVERIFY(!variant.convert(QVariant::Date));
2686 QVERIFY(!variant.convert(QVariant::DateTime));
2689 QVERIFY(!variant.convert(QVariant::Time));
2692 QVERIFY(!variant.convert(QVariant::Int));
2695 QVERIFY(!variant.convert(QVariant::Double));
2698 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2703 int x,y,z,q,w,e,r,t;
2705 Q_DECLARE_METATYPE(WontCompare);
2707 void tst_QVariant::compareCustomTypes() const
2709 qRegisterMetaType<WontCompare>("WontCompare");
2713 const QVariant variant1(qVariantFromValue(f1));
2717 const QVariant variant2(qVariantFromValue(f2));
2719 /* We compare pointers. */
2720 QVERIFY(variant1 != variant2);
2721 QVERIFY(variant1 == variant1);
2722 QVERIFY(variant2 == variant2);
2725 void tst_QVariant::timeToDateTime() const
2727 const QVariant val(QTime::currentTime());
2728 QVERIFY(!val.canConvert(QVariant::DateTime));
2729 QVERIFY(!val.toDateTime().isValid());
2732 Q_DECLARE_METATYPE(QHostAddress)
2734 void tst_QVariant::copyingUserTypes() const
2738 const QHostAddress ha("127.0.0.1");
2739 qVariantSetValue(var, ha);
2742 QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2745 void tst_QVariant::convertBoolToByteArray() const
2747 QFETCH(QByteArray, input);
2748 QFETCH(bool, canConvert);
2749 QFETCH(bool, value);
2751 const QVariant variant(input);
2753 QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2756 /* Just call this function so we run the code path. */
2757 QCOMPARE(variant.toBool(), value);
2761 void tst_QVariant::convertBoolToByteArray_data() const
2763 QTest::addColumn<QByteArray>("input");
2764 QTest::addColumn<bool>("canConvert");
2765 QTest::addColumn<bool>("value");
2767 QTest::newRow("false")
2768 << QByteArray("false")
2772 QTest::newRow("FALSE")
2773 << QByteArray("FALSE")
2777 QTest::newRow("falSE")
2778 << QByteArray("FALSE")
2787 QTest::newRow("null QByteArray")
2792 QTest::newRow("any-content")
2793 << QByteArray("any-content")
2797 QTest::newRow("true")
2798 << QByteArray("true")
2802 QTest::newRow("TRUE")
2803 << QByteArray("TRUE")
2807 QTest::newRow("trUE")
2808 << QByteArray("trUE")
2813 void tst_QVariant::convertByteArrayToBool() const
2815 QFETCH(bool, input);
2816 QFETCH(QByteArray, output);
2818 const QVariant variant(input);
2819 QCOMPARE(variant.type(), QVariant::Bool);
2820 QCOMPARE(variant.toBool(), input);
2821 QVERIFY(qVariantCanConvert<bool>(variant));
2823 QCOMPARE(variant.toByteArray(), output);
2826 void tst_QVariant::convertByteArrayToBool_data() const
2828 QTest::addColumn<bool>("input");
2829 QTest::addColumn<QByteArray>("output");
2831 QTest::newRow("false")
2833 << QByteArray("false");
2835 QTest::newRow("true")
2837 << QByteArray("true");
2842 1. Converting the string "9.9" to int fails. This is the behavior of
2843 toLongLong() and hence also QVariant, since it uses it.
2844 2. Converting the QVariant containing the double 9.9 to int works.
2846 Rationale: "9.9" is not a valid int. However, doubles are by definition not
2847 ints and therefore it makes more sense to perform conversion for those.
2849 void tst_QVariant::toIntFromQString() const
2851 QVariant first("9.9");
2853 QCOMPARE(first.toInt(&ok), 0);
2856 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
2860 QCOMPARE(v.toInt(&ok), 10);
2866 1. Conversion from (64 bit) double to int works (no overflow).
2867 2. Same conversion works for QVariant::convert.
2869 Rationale: if 2147483630 is set in float and then converted to int,
2870 there will be overflow and the result will be -2147483648.
2872 void tst_QVariant::toIntFromDouble() const
2874 double d = 2147483630; // max int 2147483647
2875 QVERIFY((int)d == 2147483630);
2878 QVERIFY( var.canConvert( QVariant::Int ) );
2881 int result = var.toInt(&ok);
2883 QVERIFY( ok == true );
2884 QCOMPARE(result, 2147483630);
2887 void tst_QVariant::setValue()
2889 QTransform t; //we just take a value so that we're sure that it will be shared
2891 QVERIFY( v1.isDetached() );
2893 QVERIFY( !v1.isDetached() );
2894 QVERIFY( !v2.isDetached() );
2896 qVariantSetValue(v2, 3); //set an integer value
2898 QVERIFY( v1.isDetached() );
2899 QVERIFY( v2.isDetached() );
2902 void tst_QVariant::numericalConvert()
2904 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
2905 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
2907 QVariant vfloat(float(5.3));
2908 QVariant vdouble(double(5.3));
2909 QVariant vreal(qreal(5.3));
2910 QVariant vint(int(5));
2911 QVariant vuint(uint(5));
2912 QVariant vshort(short(5));
2913 QVariant vlonglong(quint64(5));
2914 QVariant vstringint(QString::fromLatin1("5"));
2915 QVariant vstring(QString::fromLatin1("5.3"));
2917 QVector<QVariant *> vect;
2918 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
2920 for(int i = 0; i < vect.size(); i++) {
2922 if (i >= 3 && i <= 7)
2924 QVariant *v = vect.at(i);
2925 QCOMPARE(v->toFloat() , float(num));
2926 QCOMPARE(float(v->toReal()) , float(num));
2927 QCOMPARE(float(v->toDouble()) , float(num));
2929 QCOMPARE(v->toInt() , int(num));
2930 QCOMPARE(v->toUInt() , uint(num));
2931 QCOMPARE(v->toULongLong() , quint64(num));
2933 QCOMPARE(v->toString() , QString::number(num));
2938 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
2940 QVariant v = QVariant::fromValue(orig);
2941 QVERIFY(v.isValid());
2942 QCOMPARE(v.isNull(), isNull);
2943 QCOMPARE(v.toString(), toString);
2944 QCOMPARE(v.toDouble(), toDouble);
2945 QCOMPARE(v.toBool(), toBool);
2946 QCOMPARE(qvariant_cast<T>(v), orig);
2950 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
2951 // Type is movable so standard comparison algorithm in QVariant should work
2952 // In a custom type QVariant is not aware of ==operator so it won't be called,
2953 // which may cause problems especially visible when using a not-movable type
2956 QVERIFY(v2.isValid());
2957 QCOMPARE(v2.isNull(), isNull);
2958 QCOMPARE(v2.toString(), toString);
2959 QCOMPARE(v2.toDouble(), toDouble);
2960 QCOMPARE(v2.toBool(), toBool);
2961 QCOMPARE(qvariant_cast<T>(v2), orig);
2967 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
2968 // Type is movable so standard comparison algorithm in QVariant should work
2969 // In a custom type QVariant is not aware of ==operator so it won't be called,
2970 // which may cause problems especially visible when using a not-movable type
2973 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
2974 QCOMPARE(v2.toString(), toString);
2975 v3 = qVariantFromValue(orig);
2977 QVERIFY(v3.isValid());
2978 QCOMPARE(v3.isNull(), isNull);
2979 QCOMPARE(v3.toString(), toString);
2980 QCOMPARE(v3.toDouble(), toDouble);
2981 QCOMPARE(v3.toBool(), toBool);
2982 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
2985 QVERIFY(v.isValid());
2986 QCOMPARE(v.isNull(), isNull);
2987 QCOMPARE(v.toString(), toString);
2988 QCOMPARE(v.toDouble(), toDouble);
2989 QCOMPARE(v.toBool(), toBool);
2990 QCOMPARE(qvariant_cast<T>(v), orig);
2992 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
2993 QCOMPARE(v.userType(), qMetaTypeId<T>());
2994 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
2998 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
2999 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3000 if (QTest::currentTestFailed())\
3001 QFAIL("playWithVariant failed");
3006 bool operator==(const MyPrimitive &o) const
3008 return x == o.x && y == o.y;
3013 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3019 MyData() : ptr(this) {}
3022 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3024 MyData(const MyData& o) : ptr(this)
3026 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3028 MyData &operator=(const MyData &o)
3030 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3031 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3034 bool operator==(const MyData &o) const
3036 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3037 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3046 MyMovable() { v = count++; }
3047 ~MyMovable() { count--; }
3048 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3050 bool operator==(const MyMovable &o) const
3056 int MyMovable::count = 0;
3062 MyNotMovable() : that(this) { count++; }
3063 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3064 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3065 MyNotMovable &operator=(const MyNotMovable &o) {
3066 bool ok = that == this && o.that == &o;
3067 if (!ok) qFatal("MyNotMovable has been moved");
3071 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3072 // so it would be fine just to always return true
3073 bool operator==(const MyNotMovable &o) const
3075 bool ok = that == this && o.that == &o;
3076 if (!ok) qFatal("MyNotMovable has been moved");
3081 int MyNotMovable::count = 0;
3083 struct MyShared : QSharedData {
3088 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3091 Q_DECLARE_METATYPE(QList<QSize>)
3092 Q_DECLARE_METATYPE(MyPrimitive)
3093 Q_DECLARE_METATYPE(MyData)
3094 Q_DECLARE_METATYPE(MyMovable)
3095 Q_DECLARE_METATYPE(MyNotMovable)
3096 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3097 Q_DECLARE_METATYPE(QList<MyData>)
3098 Q_DECLARE_METATYPE(QList<MyMovable>)
3099 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3100 Q_DECLARE_METATYPE(MyPrimitive *)
3101 Q_DECLARE_METATYPE(MyData *)
3102 Q_DECLARE_METATYPE(MyMovable *)
3103 Q_DECLARE_METATYPE(MyNotMovable *)
3104 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3107 void tst_QVariant::moreCustomTypes()
3110 QList<QSize> listSize;
3111 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3112 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3113 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3118 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3119 str = QString::fromLatin1("123456789.123");
3120 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3125 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3126 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3131 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3132 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3134 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3135 l << MyData() << MyData();
3136 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3140 MyPrimitive d = { 4, 5 };
3141 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3142 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3143 QList<MyPrimitive> l;
3144 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3146 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3151 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3152 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3154 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3155 l << MyMovable() << d;
3156 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3158 QCOMPARE(MyMovable::count, 0);
3160 QCOMPARE(MyNotMovable::count, 0);
3163 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3164 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3165 QList<MyNotMovable> l;
3166 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3167 l << MyNotMovable() << d;
3168 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3170 QCOMPARE(MyNotMovable::count, 0);
3173 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3174 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3175 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3176 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3177 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3178 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3179 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3180 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3181 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3182 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3183 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3184 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3185 PLAY_WITH_VARIANT( qint8(-12), false, "\xf4", -12, true);
3186 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3187 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3188 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3189 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3190 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3191 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3193 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3198 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3199 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3203 QVariant v1 = QVariant::fromValue(5);
3204 QVariant v2 = QVariant::fromValue(5.0);
3205 QVariant v3 = QVariant::fromValue(quint16(5));
3207 QVariant v5 = QVariant::fromValue(MyPrimitive());
3208 QVariant v6 = QVariant::fromValue(MyMovable());
3209 QVariant v7 = QVariant::fromValue(MyData());
3210 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3211 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3212 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3213 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3215 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3218 QCOMPARE(MyMovable::count, 0);
3220 QSharedDataPointer<MyShared> d(new MyShared);
3221 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3223 QCOMPARE(MyMovable::count, 0);
3227 void tst_QVariant::variantInVariant()
3230 QCOMPARE(var1.type(), QVariant::Int);
3231 QVariant var2 = var1;
3232 QCOMPARE(var2, var1);
3233 QCOMPARE(var2.type(), QVariant::Int);
3234 QVariant var3 = QVariant::fromValue(var1);
3235 QCOMPARE(var3, var1);
3236 QCOMPARE(var3.type(), QVariant::Int);
3237 QVariant var4 = qvariant_cast<QVariant>(var1);
3238 QCOMPARE(var4, var1);
3239 QCOMPARE(var4.type(), QVariant::Int);
3242 QCOMPARE(var5, var1);
3243 QCOMPARE(var5.type(), QVariant::Int);
3245 var6.setValue(var1);
3246 QCOMPARE(var6, var1);
3247 QCOMPARE(var6.type(), QVariant::Int);
3249 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3250 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3251 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3253 QString str("hello");
3254 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3255 QCOMPARE((int)var8.type(), (int)QVariant::String);
3256 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3258 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3259 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3260 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3263 void tst_QVariant::colorInteger()
3265 QVariant v = QColor(Qt::red);
3266 QCOMPARE(v.type(), QVariant::Color);
3267 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3270 QCOMPARE(v.type(), QVariant::Int);
3271 QCOMPARE(v.toInt(), 1000);
3273 v.setValue(QColor(Qt::yellow));
3274 QCOMPARE(v.type(), QVariant::Color);
3275 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3279 Q_DECLARE_METATYPE(Forward*);
3281 void tst_QVariant::forwardDeclare()
3284 QVariant v = QVariant::fromValue(f);
3285 QCOMPARE(qvariant_cast<Forward*>(v), f);
3289 QTEST_MAIN(tst_QVariant)
3290 #include "tst_qvariant.moc"