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 ****************************************************************************/
43 #include <QtTest/QtTest>
46 #include <qbitarray.h>
47 #include <qhostaddress.h>
48 #include <qdatetime.h>
50 #include <qiodevice.h>
53 #include <qkeysequence.h>
59 #include <qmatrix4x4.h>
62 #include <qtransform.h>
63 #include <qvector2d.h>
64 #include <qvector3d.h>
65 #include <qvector4d.h>
66 #include <qquaternion.h>
70 Q_DECLARE_METATYPE(qlonglong)
71 Q_DECLARE_METATYPE(qulonglong)
72 Q_DECLARE_METATYPE(QPointF)
73 Q_DECLARE_METATYPE(QRectF)
74 Q_DECLARE_METATYPE(QSize)
75 Q_DECLARE_METATYPE(QSizeF)
76 Q_DECLARE_METATYPE(QLine)
77 Q_DECLARE_METATYPE(QLineF)
78 Q_DECLARE_METATYPE(QPoint)
79 Q_DECLARE_METATYPE(QRect)
80 Q_DECLARE_METATYPE(QPixmap)
81 Q_DECLARE_METATYPE(QBrush)
82 Q_DECLARE_METATYPE(QFont)
83 Q_DECLARE_METATYPE(QColor)
84 Q_DECLARE_METATYPE(QKeySequence)
89 class tst_QVariant : public QObject
98 void copy_constructor();
102 void canConvert_data();
135 void toLongLong_data();
138 void toULongLong_data();
141 void toByteArray_data();
144 void toString_data();
153 void toDateTime_data();
156 void toDouble_data();
159 void toPointF_data();
165 void toKeySequence_data();
166 void toKeySequence();
174 void toPixmap_data();
183 void qvariant_cast_QObject_data();
184 void qvariant_cast_QObject();
203 void basicUserType();
207 void writeToReadFromDataStream_data();
208 void writeToReadFromDataStream();
209 void writeToReadFromOldDataStream();
210 void checkDataStream();
212 void operator_eq_eq_data();
213 void operator_eq_eq();
215 void operator_eq_eq_rhs();
217 void typeName_data();
221 void streamInvalidVariant();
225 void data_(); // data is virtual function in QtTestCase
228 void saveLoadCustomTypes();
235 void convertToQUint8() const;
236 void invalidQColor() const;
237 void comparePointers() const;
238 void voidStar() const;
239 void dataStar() const;
240 void canConvertQStringList() const;
241 void canConvertQStringList_data() const;
242 void canConvertMetaTypeToInt() const;
243 void variantToDateTimeWithoutWarnings() const;
244 void invalidDateTime() const;
246 void loadUnknownUserType();
247 void loadBrokenUserType();
249 void task172061_invalidDate() const;
250 void compareCustomTypes() const;
251 void timeToDateTime() const;
252 void copyingUserTypes() const;
253 void convertBoolToByteArray() const;
254 void convertBoolToByteArray_data() const;
255 void convertByteArrayToBool() const;
256 void convertByteArrayToBool_data() const;
257 void toIntFromQString() const;
258 void toIntFromDouble() const;
259 void task256984_setValue();
261 void numericalConvert();
262 void moreCustomTypes();
263 void variantInVariant();
267 void forwardDeclare();
270 Q_DECLARE_METATYPE(QDate)
271 Q_DECLARE_METATYPE(QTime)
272 Q_DECLARE_METATYPE(QDateTime)
273 Q_DECLARE_METATYPE(QVariant)
275 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
276 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
278 tst_QVariant::tst_QVariant()
282 void tst_QVariant::constructor()
285 QVERIFY( !variant.isValid() );
286 QVERIFY( variant.isNull() );
288 QVariant var2(variant);
289 QVERIFY( !var2.isValid() );
290 QVERIFY( variant.isNull() );
292 QVariant varll(intMax1);
293 QVariant varll2(varll);
294 QCOMPARE(varll2, varll);
296 QVariant var3(QVariant::String);
297 QCOMPARE(var3.typeName(), "QString");
298 QVERIFY(var3.isNull());
299 QVERIFY(var3.isValid());
301 QVariant var4(QVariant::Invalid);
302 QCOMPARE(var4.type(), QVariant::Invalid);
303 QVERIFY(var4.isNull());
304 QVERIFY(!var4.isValid());
306 QVariant var5(QLatin1String("hallo"));
307 QCOMPARE(var5.type(), QVariant::String);
308 QCOMPARE(var5.typeName(), "QString");
310 QVariant var6(qlonglong(0));
311 QCOMPARE(var6.type(), QVariant::LongLong);
312 QCOMPARE(var6.typeName(), "qlonglong");
315 QVERIFY(var7.isValid());
316 QVERIFY(!var7.isNull());
318 var8.setValue<int>(5);
319 QVERIFY(var8.isValid());
320 QVERIFY(!var8.isNull());
323 void tst_QVariant::copy_constructor()
325 QVariant var7(QVariant::Int);
327 QCOMPARE(var8.type(), QVariant::Int);
328 QVERIFY(var8.isNull());
331 void tst_QVariant::isNull()
334 QVERIFY( var.isNull() );
336 QVariant var2( QString::null );
337 QVERIFY( var2.isNull() );
339 QVariant var3( QString( "blah" ) );
340 QVERIFY( !var3.isNull() );
343 QVERIFY( !var4.isNull() );
345 QVariant var5 = QString();
346 QVERIFY( var5.isNull() );
348 QVariant var6( QString( "blah" ) );
349 QVERIFY( !var6.isNull() );
351 QVERIFY( var6.isNull() );
352 var6.convert( QVariant::String );
353 QVERIFY( var6.isNull() );
354 QVariant varLL( (qlonglong)0 );
355 QVERIFY( !varLL.isNull() );
356 QVariant var7(QString::null);
357 QVERIFY(var7.isNull());
360 void tst_QVariant::swap()
362 QVariant v1 = 1, v2 = 2.0;
364 QCOMPARE(v1.type(),QVariant::Double);
365 QCOMPARE(v1.toDouble(),2.0);
366 QCOMPARE(v2.type(),QVariant::Int);
367 QCOMPARE(v2.toInt(),1);
370 void tst_QVariant::canConvert_data()
372 QTest::addColumn<QVariant>("val");
373 QTest::addColumn<bool>("BitArrayCast");
374 QTest::addColumn<bool>("BitmapCast");
375 QTest::addColumn<bool>("BoolCast");
376 QTest::addColumn<bool>("BrushCast");
377 QTest::addColumn<bool>("ByteArrayCast");
378 QTest::addColumn<bool>("ColorCast");
379 QTest::addColumn<bool>("CursorCast");
380 QTest::addColumn<bool>("DateCast");
381 QTest::addColumn<bool>("DateTimeCast");
382 QTest::addColumn<bool>("DoubleCast");
383 QTest::addColumn<bool>("FontCast");
384 QTest::addColumn<bool>("ImageCast");
385 QTest::addColumn<bool>("IntCast");
386 QTest::addColumn<bool>("InvalidCast");
387 QTest::addColumn<bool>("KeySequenceCast");
388 QTest::addColumn<bool>("ListCast");
389 QTest::addColumn<bool>("LongLongCast");
390 QTest::addColumn<bool>("MapCast");
391 QTest::addColumn<bool>("PaletteCast");
392 QTest::addColumn<bool>("PenCast");
393 QTest::addColumn<bool>("PixmapCast");
394 QTest::addColumn<bool>("PointCast");
395 QTest::addColumn<bool>("RectCast");
396 QTest::addColumn<bool>("RegionCast");
397 QTest::addColumn<bool>("SizeCast");
398 QTest::addColumn<bool>("SizePolicyCast");
399 QTest::addColumn<bool>("StringCast");
400 QTest::addColumn<bool>("StringListCast");
401 QTest::addColumn<bool>("TimeCast");
402 QTest::addColumn<bool>("UIntCast");
403 QTest::addColumn<bool>("ULongLongCast");
414 // 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
417 QVariant var(QBitArray(0));
418 QTest::newRow("BitArray")
419 << 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;
420 var = qVariantFromValue(QBitmap());
421 QTest::newRow("Bitmap")
422 << 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;
423 var = qVariantFromValue(QBrush());
424 QTest::newRow("Brush")
425 << 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;
426 var = QVariant(QByteArray());
427 QTest::newRow("ByteArray")
428 << 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;
429 var = qVariantFromValue(QColor());
430 QTest::newRow("Color")
431 << 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;
433 var = qVariantFromValue(QCursor());
434 QTest::newRow("Cursor")
435 << 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;
437 var = QVariant(QDate());
438 QTest::newRow("Date")
439 << 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;
440 var = QVariant(QDateTime());
441 QTest::newRow("DateTime")
442 << 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;
443 var = QVariant((double)0.1);
444 QTest::newRow("Double")
445 << 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;
446 var = QVariant(0.1f);
447 QTest::newRow("Float")
448 << 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;
449 var = qVariantFromValue(QFont());
450 QTest::newRow("Font")
451 << 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;
452 var = qVariantFromValue(QIcon());
453 QTest::newRow("Icon")
454 << 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;
455 var = qVariantFromValue(QImage());
456 QTest::newRow("Image")
457 << 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;
458 var = QVariant((int)1);
460 << 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;
462 QTest::newRow("Invalid")
463 << 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;
464 var = qVariantFromValue(QKeySequence());
465 QTest::newRow("KeySequence")
466 << 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;
467 var = QVariant(QList<QVariant>());
468 QTest::newRow("List")
469 << 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;
470 var = QVariant((qlonglong)1);
471 QTest::newRow("LongLong")
472 << 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;
473 var = QVariant(QMap<QString,QVariant>());
475 << 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;
476 var = qVariantFromValue(QPalette());
477 QTest::newRow("Palette")
478 << 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;
479 var = qVariantFromValue(QPen());
481 << 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;
482 var = qVariantFromValue(QPixmap());
483 QTest::newRow("Pixmap")
484 << 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;
485 var = qVariantFromValue(QPolygon());
486 QTest::newRow("PointArray")
487 << 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;
488 var = QVariant(QPoint());
489 QTest::newRow("Point")
490 << 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;
491 var = QVariant(QRect());
492 QTest::newRow("Rect")
493 << 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;
494 var = qVariantFromValue(QRegion());
495 QTest::newRow("Region")
496 << 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;
497 var = QVariant(QSize());
498 QTest::newRow("Size")
499 << 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;
500 var = qVariantFromValue(QSizePolicy());
501 QTest::newRow("SizePolicy")
502 << 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;
503 var = QVariant(QString());
504 QTest::newRow("String")
505 << 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;
506 var = QVariant(QStringList("entry"));
507 QTest::newRow("StringList")
508 << 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;
509 var = QVariant(QTime());
510 QTest::newRow("Time")
511 << 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;
512 var = QVariant((uint)1);
513 QTest::newRow("UInt")
514 << 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;
515 var = QVariant((qulonglong)1);
516 QTest::newRow("ULongLong")
517 << 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;
523 void tst_QVariant::canConvert()
525 QFETCH(QVariant, val);
526 QFETCH(bool, BitArrayCast);
527 QFETCH(bool, BitmapCast);
528 QFETCH(bool, BoolCast);
529 QFETCH(bool, BrushCast);
530 QFETCH(bool, ByteArrayCast);
531 QFETCH(bool, ColorCast);
532 QFETCH(bool, CursorCast);
533 QFETCH(bool, DateCast);
534 QFETCH(bool, DateTimeCast);
535 QFETCH(bool, DoubleCast);
536 QFETCH(bool, FontCast);
537 QFETCH(bool, ImageCast);
538 QFETCH(bool, IntCast);
539 QFETCH(bool, InvalidCast);
540 QFETCH(bool, KeySequenceCast);
541 QFETCH(bool, ListCast);
542 QFETCH(bool, LongLongCast);
543 QFETCH(bool, MapCast);
544 QFETCH(bool, PaletteCast);
545 QFETCH(bool, PenCast);
546 QFETCH(bool, PixmapCast);
547 QFETCH(bool, PointCast);
548 QFETCH(bool, RectCast);
549 QFETCH(bool, RegionCast);
550 QFETCH(bool, SizeCast);
551 QFETCH(bool, SizePolicyCast);
552 QFETCH(bool, StringCast);
553 QFETCH(bool, StringListCast);
554 QFETCH(bool, TimeCast);
555 QFETCH(bool, UIntCast);
556 QFETCH(bool, ULongLongCast);
558 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
559 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
560 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
561 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
562 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
563 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
564 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
565 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
566 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
567 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
568 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
569 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
570 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
571 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
572 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
573 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
574 QCOMPARE(val.canConvert(QVariant::List), ListCast);
575 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
576 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
577 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
578 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
579 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
580 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
581 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
582 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
583 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
584 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
585 QCOMPARE(val.canConvert(QVariant::String), StringCast);
586 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
587 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
588 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
589 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
592 void tst_QVariant::toInt_data()
594 QTest::addColumn<QVariant>("value");
595 QTest::addColumn<int>("result");
596 QTest::addColumn<bool>("valueOK");
598 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
599 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
600 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
601 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
602 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
603 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
604 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
605 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
606 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
607 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
608 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
609 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
610 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
611 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
612 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
613 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
614 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
615 QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
616 QByteArray bytearray(4, ' ');
621 // bytearray[4] = '\0';
622 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
627 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
630 void tst_QVariant::toInt()
632 QFETCH( QVariant, value );
633 QFETCH( int, result );
634 QFETCH( bool, valueOK );
635 // QEXPECT_FAIL("QByteArray", "Expected to not yet be able to convert QByteArray to int", Abort);
636 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
638 int i = value.toInt( &ok );
639 QCOMPARE( i, result );
640 QVERIFY( ok == valueOK );
643 void tst_QVariant::toUInt_data()
645 QTest::addColumn<QVariant>("value");
646 QTest::addColumn<uint>("result");
647 QTest::addColumn<bool>("valueOK");
649 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
650 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
651 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
652 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
653 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
654 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
655 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
656 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
657 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
658 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
659 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
660 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
661 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
662 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
663 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
664 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
665 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
666 QByteArray bytearray(4, ' ');
671 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
674 void tst_QVariant::toUInt()
676 QFETCH( QVariant, value );
677 QFETCH( uint, result );
678 QFETCH( bool, valueOK );
679 QVERIFY( value.isValid() );
680 QVERIFY( value.canConvert( QVariant::UInt ) );
683 uint i = value.toUInt( &ok );
684 QVERIFY( ok == valueOK );
685 QCOMPARE( i, result );
689 void tst_QVariant::toSize_data()
691 QTest::addColumn<QVariant>("value");
692 QTest::addColumn<QSize>("result");
693 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
694 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
695 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
696 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
699 void tst_QVariant::toSize()
701 QFETCH( QVariant, value );
702 QFETCH( QSize, result );
703 QVERIFY( value.isValid() );
704 QVERIFY( value.canConvert( QVariant::Size ) );
706 QSize i = value.toSize();
707 QCOMPARE( i, result );
710 void tst_QVariant::toSizeF_data()
712 QTest::addColumn<QVariant>("value");
713 QTest::addColumn<QSizeF>("result");
714 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
715 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
716 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
717 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
720 void tst_QVariant::toSizeF()
722 QFETCH( QVariant, value );
723 QFETCH( QSizeF, result );
724 QVERIFY( value.isValid() );
725 QVERIFY( value.canConvert( QVariant::SizeF ) );
727 QSizeF i = value.toSizeF();
728 QCOMPARE( i, result );
731 void tst_QVariant::toLine_data()
733 QTest::addColumn<QVariant>("value");
734 QTest::addColumn<QLine>("result");
735 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
736 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
737 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
738 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
741 void tst_QVariant::toLine()
743 QFETCH( QVariant, value );
744 QFETCH( QLine, result );
745 QVERIFY( value.isValid() );
746 QVERIFY( value.canConvert( QVariant::Line ) );
748 QLine i = value.toLine();
749 QCOMPARE( i, result );
752 void tst_QVariant::toLineF_data()
754 QTest::addColumn<QVariant>("value");
755 QTest::addColumn<QLineF>("result");
756 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
757 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
758 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
759 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
762 void tst_QVariant::toLineF()
764 QFETCH( QVariant, value );
765 QFETCH( QLineF, result );
766 QVERIFY( value.isValid() );
767 QVERIFY( value.canConvert( QVariant::LineF ) );
769 QLineF i = value.toLineF();
770 QCOMPARE( i, result );
773 void tst_QVariant::toPoint_data()
775 QTest::addColumn<QVariant>("value");
776 QTest::addColumn<QPoint>("result");
777 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
778 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
779 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
780 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
781 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
784 void tst_QVariant::toPoint()
786 QFETCH( QVariant, value );
787 QFETCH( QPoint, result );
788 QVERIFY( value.isValid() );
789 QVERIFY( value.canConvert( QVariant::Point ) );
790 QPoint i = value.toPoint();
791 QCOMPARE( i, result );
794 void tst_QVariant::toRect_data()
796 QTest::addColumn<QVariant>("value");
797 QTest::addColumn<QRect>("result");
798 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
799 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
800 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
801 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
802 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
805 void tst_QVariant::toRect()
807 QFETCH( QVariant, value );
808 QFETCH( QRect, result );
809 QVERIFY( value.isValid() );
810 QVERIFY( value.canConvert( QVariant::Rect ) );
811 QRect i = value.toRect();
812 QCOMPARE( i, result );
815 void tst_QVariant::toChar_data()
817 QTest::addColumn<QVariant>("value");
818 QTest::addColumn<QChar>("result");
819 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
820 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
823 void tst_QVariant::toChar()
825 QFETCH( QVariant, value );
826 QFETCH( QChar, result );
827 QVERIFY( value.isValid() );
828 QVERIFY( value.canConvert( QVariant::Char ) );
830 QChar i = value.toChar();
831 QCOMPARE( i, result );
834 void tst_QVariant::toBool_data()
836 QTest::addColumn<QVariant>("value");
837 QTest::addColumn<bool>("result");
839 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
840 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
841 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
842 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
843 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
844 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
845 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
846 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
847 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
848 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
849 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
850 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
851 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
852 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
853 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
854 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
855 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
856 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
859 void tst_QVariant::toBool()
861 QFETCH( QVariant, value );
862 QFETCH( bool, result );
863 QVERIFY( value.isValid() );
864 QVERIFY( value.canConvert( QVariant::Bool ) );
866 bool i = value.toBool();
867 QCOMPARE( i, result );
870 void tst_QVariant::toPointF_data()
872 QTest::addColumn<QVariant>("value");
873 QTest::addColumn<QPointF>("result");
875 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
878 void tst_QVariant::toPointF()
880 QFETCH( QVariant, value );
881 QFETCH( QPointF, result );
882 QVERIFY( value.isValid() );
883 QVERIFY( value.canConvert( QVariant::PointF ) );
884 QPointF d = value.toPointF();
885 QCOMPARE( d, result );
888 void tst_QVariant::toRectF_data()
890 QTest::addColumn<QVariant>("value");
891 QTest::addColumn<QRectF>("result");
893 QRect r( 1, 9, 8, 4 );
894 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
895 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
898 void tst_QVariant::toRectF()
900 QFETCH( QVariant, value );
901 QFETCH( QRectF, result );
902 QVERIFY( value.isValid() );
903 QVERIFY( value.canConvert( QVariant::RectF ) );
904 QRectF d = value.toRectF();
905 QCOMPARE( d, result );
908 void tst_QVariant::toColor_data()
910 QTest::addColumn<QVariant>("value");
911 QTest::addColumn<QColor>("result");
914 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
915 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
918 void tst_QVariant::toColor()
920 QFETCH( QVariant, value );
921 QFETCH( QColor, result );
922 QVERIFY( value.isValid() );
923 QVERIFY( value.canConvert( QVariant::Color ) );
924 QColor d = qVariantValue<QColor>(value);
925 QCOMPARE( d, result );
928 void tst_QVariant::toPixmap_data()
930 QTest::addColumn<QVariant>("value");
931 QTest::addColumn<QPixmap>("result");
935 QTest::newRow( "image" ) << QVariant( pm ) << pm;
939 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
942 void tst_QVariant::toPixmap()
944 QFETCH( QVariant, value );
945 QFETCH( QPixmap, result );
946 QVERIFY( value.isValid() );
947 QVERIFY( value.canConvert( QVariant::Pixmap ) );
948 QPixmap d = qVariantValue<QPixmap>(value);
949 QCOMPARE( d, result );
952 void tst_QVariant::toImage_data()
954 QTest::addColumn<QVariant>("value");
955 QTest::addColumn<QImage>("result");
957 QImage im(30, 30, QImage::Format_ARGB32);
959 QTest::newRow( "image" ) << QVariant( im ) << im;
962 void tst_QVariant::toImage()
964 QFETCH( QVariant, value );
965 QFETCH( QImage, result );
966 QVERIFY( value.isValid() );
967 QVERIFY( value.canConvert( QVariant::Image ) );
968 QImage d = qVariantValue<QImage>(value);
969 QCOMPARE( d, result );
972 void tst_QVariant::toBrush_data()
974 QTest::addColumn<QVariant>("value");
975 QTest::addColumn<QBrush>("result");
978 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
981 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
984 void tst_QVariant::toBrush()
986 QFETCH( QVariant, value );
987 QFETCH( QBrush, result );
988 QVERIFY( value.isValid() );
989 QVERIFY( value.canConvert( QVariant::Brush ) );
990 QBrush d = qVariantValue<QBrush>(value);
991 QCOMPARE( d, result );
994 void tst_QVariant::toFont_data()
996 QTest::addColumn<QVariant>("value");
997 QTest::addColumn<QFont>("result");
999 QFont f("times",12,-1,false);
1000 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1003 void tst_QVariant::toFont()
1005 QFETCH( QVariant, value );
1006 QFETCH( QFont, result );
1007 QVERIFY( value.isValid() );
1008 QVERIFY( value.canConvert( QVariant::Font ) );
1009 QFont d = qVariantValue<QFont>(value);
1010 QCOMPARE( d, result );
1013 void tst_QVariant::toKeySequence_data()
1015 QTest::addColumn<QVariant>("value");
1016 QTest::addColumn<QKeySequence>("result");
1019 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1022 QTest::newRow( "qstring" )
1023 << QVariant( QString( "Ctrl+A" ) )
1024 << QKeySequence( Qt::CTRL + Qt::Key_A );
1027 void tst_QVariant::toKeySequence()
1029 QFETCH( QVariant, value );
1030 QFETCH( QKeySequence, result );
1031 QVERIFY( value.isValid() );
1032 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1033 QKeySequence d = qVariantValue<QKeySequence>(value);
1034 QCOMPARE( d, result );
1037 void tst_QVariant::toDouble_data()
1039 QTest::addColumn<QVariant>("value");
1040 QTest::addColumn<double>("result");
1041 QTest::addColumn<bool>("valueOK");
1043 QByteArray bytearray(4, ' ');
1048 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1051 void tst_QVariant::toDouble()
1053 QFETCH( QVariant, value );
1054 QFETCH( double, result );
1055 QFETCH( bool, valueOK );
1056 QVERIFY( value.isValid() );
1057 QVERIFY( value.canConvert( QVariant::Double ) );
1059 double d = value.toDouble( &ok );
1060 QCOMPARE( d, result );
1061 QVERIFY( ok == valueOK );
1064 void tst_QVariant::toLongLong_data()
1066 QTest::addColumn<QVariant>("value");
1067 QTest::addColumn<qlonglong>("result");
1068 QTest::addColumn<bool>("valueOK");
1070 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1071 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1072 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1073 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1074 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1075 << (qlonglong)123 << true;
1076 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1078 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1079 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1080 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1081 QByteArray bytearray(4, ' ');
1086 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1089 void tst_QVariant::toLongLong()
1091 QFETCH( QVariant, value );
1092 QFETCH( qlonglong, result );
1093 QFETCH( bool, valueOK );
1094 QVERIFY( value.isValid() );
1095 QVERIFY( value.canConvert( QVariant::LongLong ) );
1097 qlonglong ll = value.toLongLong( &ok );
1098 QCOMPARE( ll, result );
1099 QVERIFY( ok == valueOK );
1102 void tst_QVariant::toULongLong_data()
1104 QTest::addColumn<QVariant>("value");
1105 QTest::addColumn<qulonglong>("result");
1106 QTest::addColumn<bool>("valueOK");
1108 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1109 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1110 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1111 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1112 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1113 << (qulonglong)123 << true;
1114 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1116 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1117 << Q_UINT64_C(18446744073709551615)
1119 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1120 << Q_UINT64_C(18446744073709551615)
1122 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1123 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1124 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1125 QByteArray bytearray(4, ' ');
1130 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1133 void tst_QVariant::toULongLong()
1135 QFETCH( QVariant, value );
1136 QFETCH( qulonglong, result );
1137 QFETCH( bool, valueOK );
1138 QVERIFY( value.isValid() );
1139 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1141 qulonglong ll = value.toULongLong( &ok );
1142 QCOMPARE( ll, result );
1143 QVERIFY( ok == valueOK );
1146 void tst_QVariant::toByteArray_data()
1148 QTest::addColumn<QVariant>("value");
1149 QTest::addColumn<QByteArray>("result");
1151 QByteArray ba(5, ' ');
1158 QByteArray variantBa = ba;
1160 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1161 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1162 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1163 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1164 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1165 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1166 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1169 void tst_QVariant::toByteArray()
1171 QFETCH( QVariant, value );
1172 QFETCH( QByteArray, result );
1173 QVERIFY( value.isValid() );
1174 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1175 QByteArray ba = value.toByteArray();
1176 QCOMPARE( ba, result );
1179 void tst_QVariant::toString_data()
1181 QTest::addColumn<QVariant>("value");
1182 QTest::addColumn<QString>("result");
1184 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1185 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1186 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1187 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1188 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1189 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1190 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1191 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1192 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1193 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1194 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1195 QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1197 << QString( "Ctrl+A" );
1199 << QString(QChar(0x2318)) + "A";
1202 QFont font( "times", 12 );
1203 QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1204 QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1205 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1206 QString( "123456789012" );
1209 void tst_QVariant::toString()
1211 QFETCH( QVariant, value );
1212 QFETCH( QString, result );
1213 QVERIFY( value.isValid() );
1214 QVERIFY( value.canConvert( QVariant::String ) );
1215 QString str = value.toString();
1216 QCOMPARE( str, result );
1219 void tst_QVariant::toDate_data()
1221 QTest::addColumn<QVariant>("value");
1222 QTest::addColumn<QDate>("result");
1224 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1225 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1226 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1229 void tst_QVariant::toDate()
1231 QFETCH( QVariant, value );
1232 QFETCH( QDate, result );
1233 QVERIFY( value.isValid() );
1234 QVERIFY( value.canConvert( QVariant::Date ) );
1235 QCOMPARE( value.toDate(), result );
1238 void tst_QVariant::toTime_data()
1240 QTest::addColumn<QVariant>("value");
1241 QTest::addColumn<QTime>("result");
1243 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1244 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1245 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1248 void tst_QVariant::toTime()
1250 QFETCH( QVariant, value );
1251 QFETCH( QTime, result );
1252 QVERIFY( value.isValid() );
1253 QVERIFY( value.canConvert( QVariant::Time ) );
1254 QCOMPARE( value.toTime(), result );
1257 void tst_QVariant::toDateTime_data()
1259 QTest::addColumn<QVariant>("value");
1260 QTest::addColumn<QDateTime>("result");
1262 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1263 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1264 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1265 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1268 void tst_QVariant::toDateTime()
1270 QFETCH( QVariant, value );
1271 QFETCH( QDateTime, result );
1272 QVERIFY( value.isValid() );
1273 QVERIFY( value.canConvert( QVariant::DateTime ) );
1274 QCOMPARE( value.toDateTime(), result );
1277 void tst_QVariant::toLocale()
1280 QLocale loc = variant.toLocale();
1281 variant = QLocale::system();
1282 loc = variant.toLocale();
1285 void tst_QVariant::toRegExp()
1288 QRegExp rx = variant.toRegExp();
1289 variant = QRegExp("foo");
1290 rx = variant.toRegExp();
1293 void tst_QVariant::matrix()
1296 QMatrix matrix = qVariantValue<QMatrix>(variant);
1297 QVERIFY(matrix.isIdentity());
1298 qVariantSetValue(variant, QMatrix().rotate(90));
1299 QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1301 void *mmatrix = QMetaType::construct(QVariant::Matrix, 0);
1303 QMetaType::destroy(QVariant::Matrix, mmatrix);
1306 void tst_QVariant::matrix4x4()
1309 QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1310 QVERIFY(matrix.isIdentity());
1313 qVariantSetValue(variant, m);
1314 QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1316 void *mmatrix = QMetaType::construct(QVariant::Matrix4x4, 0);
1318 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1321 void tst_QVariant::transform()
1324 QTransform matrix = qVariantValue<QTransform>(variant);
1325 QVERIFY(matrix.isIdentity());
1326 qVariantSetValue(variant, QTransform().rotate(90));
1327 QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1329 void *mmatrix = QMetaType::construct(QVariant::Transform, 0);
1331 QMetaType::destroy(QVariant::Transform, mmatrix);
1335 void tst_QVariant::vector2D()
1338 QVector2D vector = qVariantValue<QVector2D>(variant);
1339 QVERIFY(vector.isNull());
1340 qVariantSetValue(variant, QVector2D(0.1, 0.2));
1341 QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1343 void *pvector = QMetaType::construct(QVariant::Vector2D, 0);
1345 QMetaType::destroy(QVariant::Vector2D, pvector);
1348 void tst_QVariant::vector3D()
1351 QVector3D vector = qVariantValue<QVector3D>(variant);
1352 QVERIFY(vector.isNull());
1353 qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1354 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1356 void *pvector = QMetaType::construct(QVariant::Vector3D, 0);
1358 QMetaType::destroy(QVariant::Vector3D, pvector);
1361 void tst_QVariant::vector4D()
1364 QVector4D vector = qVariantValue<QVector4D>(variant);
1365 QVERIFY(vector.isNull());
1366 qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1367 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1369 void *pvector = QMetaType::construct(QVariant::Vector4D, 0);
1371 QMetaType::destroy(QVariant::Vector4D, pvector);
1374 void tst_QVariant::quaternion()
1377 QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1378 QVERIFY(quaternion.isIdentity());
1379 qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1380 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1382 void *pquaternion = QMetaType::construct(QVariant::Quaternion, 0);
1383 QVERIFY(pquaternion);
1384 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1387 void tst_QVariant::writeToReadFromDataStream_data()
1390 QTest::addColumn<QVariant>("writeVariant");
1391 QTest::addColumn<bool>("isNull");
1393 typedef QList<QVariant> variantsList;
1394 variantsList valuelist;
1395 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1396 QVariant var(valuelist);
1397 QTest::newRow( "list_valid" ) << var << false;
1400 QTest::newRow( "invalid" ) << QVariant() << true;
1401 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1402 QBitArray bitarray( 3 );
1406 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1407 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1408 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1409 QByteArray bytearray(5, ' ');
1414 bytearray[4] = '\0';
1415 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1416 QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1417 QBitmap bitmap( 10, 10 );
1418 bitmap.fill( Qt::red );
1419 QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1420 QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1421 QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1422 #ifndef QT_NO_CURSOR
1423 QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1425 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1426 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1427 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1428 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1429 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1430 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1431 QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1432 QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1433 QPixmap pixmap( 10, 10 );
1434 pixmap.fill( Qt::red );
1435 QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1436 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1437 QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1438 QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1439 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1440 typedef QList<QVariant> variantsList;
1441 // variantsList valuelist;
1442 // valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1443 // QTest::newRow( "list_valid" ) << QVariant( valuelist ) << false;
1444 typedef QMap<QString, QVariant> variantsMap;
1446 vMap.insert( "int", QVariant( 1 ) );
1447 vMap.insert( "string", QVariant( QString("Two") ) );
1448 vMap.insert( "double", QVariant( 3.45 ) );
1449 vMap.insert( "float", QVariant( 3.45f ) );
1450 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1451 QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1452 QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1453 QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1454 QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1455 QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1456 QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1457 QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1458 QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1459 QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1460 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1461 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1462 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1463 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1464 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1465 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1466 QStringList stringlist;
1467 stringlist << "One" << "Two" << "Three";
1468 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1469 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1470 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1471 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1472 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1473 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1474 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1475 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1477 // types known to QMetaType, but not part of QVariant::Type
1478 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1480 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1481 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1483 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1484 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1486 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1487 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1488 ulong ulongInt = 1ul;
1489 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1490 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1491 ushort ushortInt = 1u;
1492 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1493 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1495 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1496 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1498 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1501 void tst_QVariant::writeToReadFromDataStream()
1503 // See #15831 for more information on the bug
1505 QFETCH( QVariant, writeVariant );
1506 QFETCH( bool, isNull );
1509 QDataStream writeStream( &data, QIODevice::WriteOnly );
1510 writeStream << writeVariant;
1512 QVariant readVariant;
1513 QDataStream readStream( &data, QIODevice::ReadOnly );
1514 readStream >> readVariant;
1515 QVERIFY( readVariant.isNull() == isNull );
1516 // Best way to confirm the readVariant contains the same data?
1517 // Since only a few won't match since the serial numbers are different
1518 // I won't bother adding another bool in the data test.
1519 QVariant::Type writeType = writeVariant.type();
1520 if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1521 && writeType != QVariant::Image) {
1522 switch (writeType) {
1524 QCOMPARE( readVariant, writeVariant );
1527 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1528 case QMetaType::Long:
1529 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1531 case QMetaType::ULong:
1532 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1534 case QMetaType::Short:
1535 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1537 case QMetaType::UShort:
1538 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1540 case QMetaType::Char:
1541 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1543 case QMetaType::UChar:
1544 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1546 case QMetaType::Float:
1548 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1549 float readFloat = qvariant_cast<float>(readVariant);
1550 float writtenFloat = qvariant_cast<float>(writeVariant);
1551 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1552 if (!qIsNaN(readFloat))
1553 QVERIFY(readFloat == writtenFloat);
1560 void tst_QVariant::writeToReadFromOldDataStream()
1562 QVariant writeVariant = QString("hello");
1565 QDataStream writeStream(&data, QIODevice::WriteOnly);
1566 writeStream.setVersion(QDataStream::Qt_2_1);
1567 writeStream << writeVariant;
1569 QVariant readVariant;
1570 QDataStream readStream(&data, QIODevice::ReadOnly);
1571 readStream.setVersion(QDataStream::Qt_2_1);
1572 readStream >> readVariant;
1574 QCOMPARE(writeVariant.userType(), readVariant.userType());
1575 QCOMPARE(writeVariant, readVariant);
1578 void tst_QVariant::checkDataStream()
1580 // test fix for task 215610
1581 const QByteArray settingsHex("0000002effffffffff");
1582 const QByteArray settings = QByteArray::fromHex(settingsHex);
1583 QDataStream in(settings);
1586 // the line below has been left out for now since the data stream
1587 // is not necessarily considered corrupt when an invalid QVariant is
1588 // constructed. However, it might be worth considering changing that behavior
1590 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1591 QCOMPARE(v.type(), QVariant::Invalid);
1594 void tst_QVariant::operator_eq_eq_data()
1596 QTest::addColumn<QVariant>("left");
1597 QTest::addColumn<QVariant>("right");
1598 QTest::addColumn<bool>("equal"); // left == right ?
1601 QVariant i0( int(0) );
1602 QVariant i1( int(1) );
1604 QTest::newRow( "invinv" ) << inv << inv << true;
1606 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1607 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1608 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1610 // LongLong and ULongLong
1611 QVariant ll1( (qlonglong)1 );
1612 QVariant lln2( (qlonglong)-2 );
1613 QVariant ull1( (qulonglong)1 );
1614 QVariant ull3( (qulonglong)3 );
1615 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1616 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1617 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1618 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1619 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1620 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1623 QVariant mIntString(QByteArray("-42"));
1624 QVariant mIntQString(QString("-42"));
1626 QVariant mUInt(42u);
1627 QVariant mUIntString(QByteArray("42"));
1628 QVariant mUIntQString(QString("42"));
1630 QVariant mDouble(42.11);
1631 QVariant mDoubleString(QByteArray("42.11"));
1632 QVariant mDoubleQString(QString("42.11"));
1634 QVariant mFloat(42.11f);
1635 QVariant mFloatString(QByteArray("42.11"));
1636 QVariant mFloatQString(QString("42.11"));
1638 QVariant mLongLong((qlonglong)-42);
1639 QVariant mLongLongString(QByteArray("-42"));
1640 QVariant mLongLongQString(QString("-42"));
1642 QVariant mULongLong((qulonglong)42);
1643 QVariant mULongLongString(QByteArray("42"));
1644 QVariant mULongLongQString(QString("42"));
1646 QVariant mBool(false);
1647 QVariant mBoolString(QByteArray("false"));
1648 QVariant mBoolQString(QString("false"));
1650 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1651 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1652 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1653 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1654 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1655 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1657 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1658 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1659 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1660 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1662 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1663 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1664 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1665 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1667 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1668 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1669 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1670 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1672 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1673 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1674 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1675 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1677 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1678 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1679 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1680 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1682 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1683 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1684 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1685 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1687 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1688 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1689 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1690 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1692 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1693 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1694 // ### many other combinations missing
1697 QMap<QString, QVariant> map1;
1698 map1.insert( "X", 1 );
1700 QMap<QString, QVariant> map2;
1701 map2.insert( "Y", 1 );
1703 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1708 QMap<QString, QVariant> map1;
1709 map1.insert( "X", 1 );
1711 QMap<QString, QVariant> map2;
1712 map2.insert( "X", 1 );
1714 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1718 QMap<QString, QVariant> map1;
1719 map1.insert( "X", 1 );
1721 QMap<QString, QVariant> map2;
1723 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1727 QMap<QString, QVariant> map1;
1729 QMap<QString, QVariant> map2;
1730 map2.insert( "X", 1 );
1732 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1736 QMap<QString, QVariant> map1;
1737 map1.insert( "X", 1 );
1738 map1.insert( "Y", 1 );
1740 QMap<QString, QVariant> map2;
1741 map2.insert( "X", 1 );
1743 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1747 QMap<QString, QVariant> map1;
1748 map1.insert( "X", 1 );
1750 QMap<QString, QVariant> map2;
1751 map2.insert( "X", 1 );
1752 map2.insert( "Y", 1 );
1754 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1757 // same thing with hash
1759 QHash<QString, QVariant> hash1;
1760 hash1.insert( "X", 1 );
1762 QHash<QString, QVariant> hash2;
1763 hash2.insert( "Y", 1 );
1765 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1770 QHash<QString, QVariant> hash1;
1771 hash1.insert( "X", 1 );
1773 QHash<QString, QVariant> hash2;
1774 hash2.insert( "X", 1 );
1776 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1780 QHash<QString, QVariant> hash1;
1781 hash1.insert( "X", 1 );
1783 QHash<QString, QVariant> hash2;
1785 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1789 QHash<QString, QVariant> hash1;
1791 QHash<QString, QVariant> hash2;
1792 hash2.insert( "X", 1 );
1794 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1798 QHash<QString, QVariant> hash1;
1799 hash1.insert( "X", 1 );
1800 hash1.insert( "Y", 1 );
1802 QHash<QString, QVariant> hash2;
1803 hash2.insert( "X", 1 );
1805 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1809 QHash<QString, QVariant> hash1;
1810 hash1.insert( "X", 1 );
1812 QHash<QString, QVariant> hash2;
1813 hash2.insert( "X", 1 );
1814 hash2.insert( "Y", 1 );
1816 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1819 QTest::newRow( "UserType" ) << QVariant(QVariant::UserType) << QVariant(QVariant::UserType) << true;
1820 QVariant mUserType(QVariant::UserType);
1821 QTest::newRow( "Shared UserType" ) << mUserType << mUserType << true;
1824 void tst_QVariant::operator_eq_eq()
1826 QFETCH( QVariant, left );
1827 QFETCH( QVariant, right );
1828 QFETCH( bool, equal );
1829 QEXPECT_FAIL("nullint", "See task 118496", Continue);
1830 QCOMPARE( left == right, equal );
1833 void tst_QVariant::operator_eq_eq_rhs()
1841 /* This should _not_ compile */
1845 QVERIFY(dt == list);
1849 void tst_QVariant::typeName_data()
1851 QTest::addColumn<int>("type");
1852 QTest::addColumn<QByteArray>("res");
1853 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1854 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1855 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1856 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1857 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1858 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1859 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1860 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1861 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1862 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1863 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1864 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1865 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1866 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1867 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1868 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1869 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1870 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1871 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1872 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1873 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1874 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1875 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1876 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1877 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1878 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1879 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1880 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1881 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1882 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1883 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1884 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1885 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1886 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1887 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1888 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1889 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1890 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1891 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1892 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1893 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1894 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1895 QTest::newRow("42") << int(QVariant::UserType) << QByteArray("UserType");
1896 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1897 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1898 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1899 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1900 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1901 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1902 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1903 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1906 void tst_QVariant::typeName()
1908 QFETCH( int, type );
1909 QFETCH( QByteArray, res );
1910 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1911 QString::fromLatin1(res.constData()));
1914 // test nameToType as well
1915 void tst_QVariant::typeToName()
1918 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1919 // assumes that QVariant::Type contains consecutive values
1921 int max = QVariant::Quaternion;
1922 for ( int t = 1; t <= max; t++ ) {
1923 const char *n = QVariant::typeToName( (QVariant::Type)t );
1925 QCOMPARE( int(QVariant::nameToType( n )), t );
1928 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1929 // not documented but we return 0 if the type is out of range
1930 // by testing this we catch cases where QVariant is extended
1931 // but type_map is not updated accordingly
1932 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1933 // invalid type names
1934 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
1935 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
1936 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
1937 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Icon);
1940 void tst_QVariant::streamInvalidVariant()
1942 // I wasn't sure where this test belonged, so it's here
1943 // See #17423 for more details
1949 QVariant writeVariant;
1950 QVariant readVariant;
1952 QVERIFY( writeVariant.type() == QVariant::Invalid );
1955 QDataStream writeStream( &data, QIODevice::WriteOnly );
1956 writeStream << writeX << writeVariant << writeY;
1958 QDataStream readStream( &data, QIODevice::ReadOnly );
1959 readStream >> readX >> readVariant >> readY;
1961 QVERIFY( readX == writeX );
1962 // Two invalid QVariant's aren't necessarily the same, so == will
1963 // return false if one is invalid, so check the type() instead
1964 QVERIFY( readVariant.type() == QVariant::Invalid );
1965 QVERIFY( readY == writeY );
1968 static int instanceCount = 0;
1972 MyType(int n = 0, const char *t=0): number(n), text(t)
1976 MyType(const MyType &other)
1977 : number(other.number), text(other.text)
1989 Q_DECLARE_METATYPE(MyType)
1990 Q_DECLARE_METATYPE(MyType*)
1992 void tst_QVariant::userType()
1995 QVariant userVariant(QVariant::UserType);
1997 QVERIFY(userVariant.isValid());
1998 QVERIFY(userVariant.isNull());
2002 MyType data(1, "eins");
2003 MyType data2(2, "zwei");
2007 qVariantSetValue(userVar, data);
2009 QCOMPARE(userVar.type(), QVariant::UserType);
2010 QCOMPARE(userVar.typeName(), "MyType");
2011 QVERIFY(!userVar.isNull());
2012 QVERIFY(!userVar.canConvert(QVariant::String));
2013 QVERIFY(!userVar.canConvert(QVariant::UserType));
2015 QVariant userVar2(userVar);
2016 QVERIFY(userVar == userVar2);
2018 qVariantSetValue(userVar2, data2);
2019 QVERIFY(userVar != userVar2);
2021 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2023 QCOMPARE(varData->number, data.number);
2024 QCOMPARE(varData->text, data.text);
2027 qVariantSetValue(userVar3, data2);
2028 QVERIFY(userVar2 != userVar3);
2029 userVar3 = userVar2;
2030 QVERIFY(userVar2 == userVar3);
2034 qVariantSetValue(userVar, &data);
2036 QCOMPARE(userVar.type(), QVariant::UserType);
2037 QCOMPARE(userVar.typeName(), "MyType*");
2038 QVERIFY(!userVar.isNull());
2039 QVERIFY(!userVar.canConvert(QVariant::String));
2040 QVERIFY(!userVar.canConvert(QVariant::UserType));
2042 QVariant userVar2(userVar);
2043 QVERIFY(userVar == userVar2);
2045 qVariantSetValue(userVar2, &data2);
2046 QVERIFY(userVar != userVar2);
2048 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2050 QCOMPARE(*varData, &data);
2053 qVariantSetValue(userVar3, &data2);
2055 /* This check is correct now. userVar2 contains a pointer to data2 and so
2057 QVERIFY(userVar2 == userVar3);
2059 userVar3 = userVar2;
2060 QVERIFY(userVar2 == userVar3);
2063 QCOMPARE(instanceCount, 2);
2065 qVariantSetValue(myCarrier, data);
2066 QCOMPARE(instanceCount, 3);
2069 QVariant second = myCarrier;
2070 QCOMPARE(instanceCount, 3);
2072 QCOMPARE(instanceCount, 4);
2074 QCOMPARE(instanceCount, 3);
2076 MyType data3(0, "null");
2077 data3 = qVariantValue<MyType>(myCarrier);
2078 QCOMPARE(data3.number, 1);
2079 QCOMPARE(data3.text, (const char *)"eins");
2081 QCOMPARE(instanceCount, 4);
2087 const MyType data(3, "drei");
2090 qVariantSetValue(myCarrier, data);
2091 QCOMPARE(myCarrier.typeName(), "MyType");
2093 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2094 QCOMPARE(data2.number, 3);
2095 QCOMPARE(data2.text, (const char *)"drei");
2102 qVariantSetValue(myCarrier, s);
2103 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2107 qlonglong ll = Q_INT64_C(42);
2110 qVariantSetValue(myCarrier, ll);
2111 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2114 QCOMPARE(instanceCount, 0);
2122 Q_DECLARE_METATYPE(MyTypePOD)
2124 void tst_QVariant::podUserType()
2130 QVariant pod_as_variant = qVariantFromValue(pod);
2131 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2133 QCOMPARE(pod.a, pod2.a);
2134 QCOMPARE(pod.b, pod2.b);
2136 qVariantSetValue(pod_as_variant, pod);
2137 pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2139 QCOMPARE(pod.a, pod2.a);
2140 QCOMPARE(pod.b, pod2.b);
2143 void tst_QVariant::basicUserType()
2148 v = QVariant(QMetaType::Int, &i);
2150 QCOMPARE(v.type(), QVariant::Int);
2151 QCOMPARE(v.toInt(), 7);
2155 v = QVariant(QMetaType::QString, &s);
2157 QCOMPARE(v.type(), QVariant::String);
2158 QCOMPARE(v.toString(), QString("foo"));
2162 v = QVariant(QMetaType::Double, &d);
2164 QCOMPARE(v.type(), QVariant::Double);
2165 QCOMPARE(v.toDouble(), 4.4);
2169 v = QVariant(QMetaType::Float, &f);
2171 QCOMPARE(v.userType(), int(QMetaType::Float));
2172 QCOMPARE(v.toDouble(), 4.5);
2175 QByteArray ba("bar");
2176 v = QVariant(QMetaType::QByteArray, &ba);
2178 QCOMPARE(v.type(), QVariant::ByteArray);
2179 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2182 void tst_QVariant::data_()
2189 QVariant ll = (qlonglong)2;
2190 QVariant ull = (qulonglong)3;
2191 QVariant s(QString("hallo"));
2192 QVariant r(QRect(1,2,3,4));
2196 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2200 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2204 QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2208 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2212 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2216 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2220 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2223 void tst_QVariant::constData()
2236 QVERIFY(v.constData());
2237 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2240 QVERIFY(v.constData());
2241 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2244 QVERIFY(v.constData());
2245 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2248 QVERIFY(v.constData());
2249 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2252 QVERIFY(v.constData());
2253 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2256 QVERIFY(v.constData());
2257 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2260 QVERIFY(v.constData());
2261 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2270 Q_DECLARE_METATYPE(Foo)
2272 void tst_QVariant::variant_to()
2281 sl << QLatin1String("blah");
2283 qVariantSetValue(v3, sl);
2288 qVariantSetValue(v4, foo);
2290 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2291 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2292 QCOMPARE(qvariant_cast<int>(v2), 5);
2293 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2294 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2296 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2299 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2301 QCOMPARE(qvariant_cast<int>(v1), 4);
2303 QVariant n = qVariantFromValue<short>(42);
2304 QCOMPARE(qvariant_cast<int>(n), 42);
2305 QCOMPARE(qvariant_cast<uint>(n), 42u);
2306 QCOMPARE(qvariant_cast<double>(n), 42.0);
2307 QCOMPARE(qvariant_cast<float>(n), 42.f);
2308 QCOMPARE(qvariant_cast<short>(n), short(42));
2309 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2311 n = qVariantFromValue(43l);
2312 QCOMPARE(qvariant_cast<int>(n), 43);
2313 QCOMPARE(qvariant_cast<uint>(n), 43u);
2314 QCOMPARE(qvariant_cast<double>(n), 43.0);
2315 QCOMPARE(qvariant_cast<float>(n), 43.f);
2316 QCOMPARE(qvariant_cast<long>(n), 43l);
2318 n = QLatin1String("44");
2319 QCOMPARE(qvariant_cast<int>(n), 44);
2320 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2321 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2323 QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2326 struct Blah { int i; };
2328 QDataStream& operator>>(QDataStream& s, Blah& c)
2329 { return (s >> c.i); }
2331 QDataStream& operator<<(QDataStream& s, const Blah& c)
2332 { return (s << c.i); }
2334 void tst_QVariant::saveLoadCustomTypes()
2339 int tp = qRegisterMetaType<Blah>("Blah");
2340 QVariant v = QVariant(tp, &i);
2342 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2344 QCOMPARE(v.userType(), tp);
2345 QVERIFY(v.type() == QVariant::UserType);
2347 QDataStream stream(&data, QIODevice::WriteOnly);
2354 QDataStream stream(data);
2358 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2359 int value = *(int*)v.constData();
2360 QCOMPARE(value, 42);
2363 void tst_QVariant::url()
2365 QString str("http://qt.nokia.com");
2368 QVariant v(url); //built with a QUrl
2372 QVariant v3(str); //built with a QString
2374 QCOMPARE(v2.toUrl(), url);
2375 QVERIFY(qVariantCanConvert<QUrl>(v3));
2376 QCOMPARE(v2.toUrl(), v3.toUrl());
2378 QVERIFY(qVariantCanConvert<QString>(v2));
2379 QCOMPARE(v2.toString(), str);
2380 QCOMPARE(v3.toString(), str);
2383 void tst_QVariant::globalColor()
2385 QVariant variant(Qt::blue);
2386 QVERIFY(variant.type() == QVariant::Color);
2387 QVERIFY(qVariantValue<QColor>(variant) == QColor(Qt::blue));
2390 void tst_QVariant::variantMap()
2392 QMap<QString, QVariant> map;
2396 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2398 QCOMPARE(map2.value("test").toInt(), 42);
2400 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2401 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2403 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2404 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2407 void tst_QVariant::variantHash()
2409 QHash<QString, QVariant> hash;
2413 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2415 QCOMPARE(hash2.value("test").toInt(), 42);
2417 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2418 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2420 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2421 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2424 void tst_QVariant::invalidQColor() const
2426 QVariant va("An invalid QColor::name() value.");
2427 QVERIFY(va.canConvert(QVariant::Color));
2429 QVERIFY(!va.convert(QVariant::Color));
2431 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2434 void tst_QVariant::qvariant_cast_QObject_data() {
2436 QTest::addColumn<QVariant>("data");
2437 QTest::addColumn<bool>("success");
2438 QObject *obj = new QObject(this);
2439 obj->setObjectName(QString::fromLatin1("Hello"));
2440 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2441 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2442 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2443 QTest::newRow("from int") << QVariant((int) 123) << false;
2447 void tst_QVariant::qvariant_cast_QObject() {
2448 QFETCH(QVariant, data);
2449 QFETCH(bool, success);
2451 QObject *o = qvariant_cast<QObject *>(data);
2452 QCOMPARE(o != 0, success);
2454 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2458 Q_DECLARE_METATYPE(qint8);
2460 void tst_QVariant::convertToQUint8() const
2466 const qint8 anInt = 32;
2468 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2469 * It's of type QVariant::Int. */
2470 const QVariant v0 = anInt;
2472 QVERIFY(qVariantCanConvert<qint8>(v0));
2473 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2474 QCOMPARE(int(v0.toInt()), 32);
2475 QCOMPARE(v0.toString(), QString("32"));
2477 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2478 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2479 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2480 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2481 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2482 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2487 const quint8 anInt = 32;
2488 const QVariant v0 = anInt;
2490 QVERIFY(qVariantCanConvert<quint8>(v0));
2491 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2492 QCOMPARE(int(v0.toUInt()), 32);
2493 QCOMPARE(v0.toString(), QString("32"));
2498 const qint16 anInt = 32;
2499 const QVariant v0 = anInt;
2501 QVERIFY(qVariantCanConvert<qint16>(v0));
2502 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2503 QCOMPARE(int(v0.toInt()), 32);
2504 QCOMPARE(v0.toString(), QString("32"));
2509 const quint16 anInt = 32;
2510 const QVariant v0 = anInt;
2512 QVERIFY(qVariantCanConvert<quint16>(v0));
2513 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2514 QCOMPARE(int(v0.toUInt()), 32);
2515 QCOMPARE(v0.toString(), QString("32"));
2519 void tst_QVariant::comparePointers() const
2527 QVariant v = qVariantFromValue<void *>(&myClass);
2528 QVariant v2 = qVariantFromValue<void *>(&myClass);
2534 Q_DECLARE_METATYPE(Data*)
2536 void tst_QVariant::voidStar() const
2543 v1 = qVariantFromValue(p1);
2547 v2 = qVariantFromValue(p2);
2551 v2 = qVariantFromValue(p2);
2555 void tst_QVariant::dataStar() const
2557 qRegisterMetaType<Data*>();
2558 Data *p1 = new Data;
2560 QVariant v1 = qVariantFromValue(p1);
2561 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2562 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2567 v2 = qVariantFromValue(p1);
2572 void tst_QVariant::canConvertQStringList() const
2574 QFETCH(bool, canConvert);
2575 QFETCH(QStringList, input);
2576 QFETCH(QString, result);
2580 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2581 QCOMPARE(v.toString(), result);
2584 void tst_QVariant::canConvertQStringList_data() const
2586 QTest::addColumn<bool>("canConvert");
2587 QTest::addColumn<QStringList>("input");
2588 QTest::addColumn<QString>("result");
2590 QTest::newRow("An empty list") << false << QStringList() << QString();
2591 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2592 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2597 QTest::newRow("Two items") << false << l << QString();
2600 QTest::newRow("Three items") << false << l << QString();
2603 template<typename T> void convertMetaType()
2605 QVERIFY(qVariantFromValue<T>(10).isValid());
2606 QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2607 QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2608 QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2611 void tst_QVariant::canConvertMetaTypeToInt() const
2613 convertMetaType<long>();
2614 convertMetaType<short>();
2615 convertMetaType<short>();
2616 convertMetaType<unsigned short>();
2617 convertMetaType<ushort>();
2618 convertMetaType<ulong>();
2619 convertMetaType<unsigned long>();
2620 convertMetaType<uchar>();
2621 convertMetaType<unsigned char>();
2622 convertMetaType<char>();
2623 convertMetaType<uint>();
2624 convertMetaType<unsigned int>();
2628 These calls should not produce any warnings.
2630 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2633 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2634 const QDateTime dateTime(variant.toDateTime());
2635 QVERIFY(!dateTime.isValid());
2639 QVariant v1(QLatin1String("xyz"));
2640 v1.convert(QVariant::DateTime);
2642 QVariant v2(QLatin1String("xyz"));
2643 QDateTime dt1(v2.toDateTime());
2645 const QVariant v3(QLatin1String("xyz"));
2646 const QDateTime dt2(v3.toDateTime());
2650 void tst_QVariant::invalidDateTime() const
2652 QVariant variant(QString::fromLatin1("Invalid date time string"));
2653 QVERIFY(!variant.toDateTime().isValid());
2654 QVERIFY(!variant.convert(QVariant::DateTime));
2659 MyClass() : myValue(0) {}
2663 Q_DECLARE_METATYPE( MyClass )
2665 void tst_QVariant::loadUnknownUserType()
2667 qRegisterMetaType<MyClass>("MyClass");
2668 char data[] = {0, 0, 0, 127, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0};
2670 QByteArray ba(data, sizeof(data));
2671 QDataStream ds(&ba, QIODevice::ReadOnly);
2674 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2677 void tst_QVariant::loadBrokenUserType()
2679 char data[] = {0, 0, 0, 127, 0, 112 };
2681 QByteArray ba(data, sizeof(data));
2682 QDataStream ds(&ba, QIODevice::ReadOnly);
2685 QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2688 void tst_QVariant::task172061_invalidDate() const
2690 QString foo("Hello");
2691 QVariant variant(foo);
2692 QVERIFY(!variant.convert(QVariant::Date));
2695 QVERIFY(!variant.convert(QVariant::DateTime));
2698 QVERIFY(!variant.convert(QVariant::Time));
2701 QVERIFY(!variant.convert(QVariant::Int));
2704 QVERIFY(!variant.convert(QVariant::Double));
2707 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2714 Q_DECLARE_METATYPE(WontCompare);
2716 void tst_QVariant::compareCustomTypes() const
2718 qRegisterMetaType<WontCompare>("WontCompare");
2722 const QVariant variant1(qVariantFromValue(f1));
2726 const QVariant variant2(qVariantFromValue(f2));
2728 /* We compare pointers. */
2729 QVERIFY(variant1 != variant2);
2730 QVERIFY(variant1 == variant1);
2731 QVERIFY(variant2 == variant2);
2734 void tst_QVariant::timeToDateTime() const
2736 const QVariant val(QTime::currentTime());
2737 QVERIFY(!val.canConvert(QVariant::DateTime));
2738 QVERIFY(!val.toDateTime().isValid());
2741 Q_DECLARE_METATYPE(QHostAddress)
2743 void tst_QVariant::copyingUserTypes() const
2747 const QHostAddress ha("127.0.0.1");
2748 qVariantSetValue(var, ha);
2751 QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2754 void tst_QVariant::convertBoolToByteArray() const
2756 QFETCH(QByteArray, input);
2757 QFETCH(bool, canConvert);
2758 QFETCH(bool, value);
2760 const QVariant variant(input);
2762 QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2765 /* Just call this function so we run the code path. */
2766 QCOMPARE(variant.toBool(), value);
2770 void tst_QVariant::convertBoolToByteArray_data() const
2772 QTest::addColumn<QByteArray>("input");
2773 QTest::addColumn<bool>("canConvert");
2774 QTest::addColumn<bool>("value");
2776 QTest::newRow("false")
2777 << QByteArray("false")
2781 QTest::newRow("FALSE")
2782 << QByteArray("FALSE")
2786 QTest::newRow("falSE")
2787 << QByteArray("FALSE")
2796 QTest::newRow("null QByteArray")
2801 QTest::newRow("any-content")
2802 << QByteArray("any-content")
2806 QTest::newRow("true")
2807 << QByteArray("true")
2811 QTest::newRow("TRUE")
2812 << QByteArray("TRUE")
2816 QTest::newRow("trUE")
2817 << QByteArray("trUE")
2822 void tst_QVariant::convertByteArrayToBool() const
2824 QFETCH(bool, input);
2825 QFETCH(QByteArray, output);
2827 const QVariant variant(input);
2828 QCOMPARE(variant.type(), QVariant::Bool);
2829 QCOMPARE(variant.toBool(), input);
2830 QVERIFY(qVariantCanConvert<bool>(variant));
2832 QCOMPARE(variant.toByteArray(), output);
2835 void tst_QVariant::convertByteArrayToBool_data() const
2837 QTest::addColumn<bool>("input");
2838 QTest::addColumn<QByteArray>("output");
2840 QTest::newRow("false")
2842 << QByteArray("false");
2844 QTest::newRow("true")
2846 << QByteArray("true");
2851 1. Converting the string "9.9" to int fails. This is the behavior of
2852 toLongLong() and hence also QVariant, since it uses it.
2853 2. Converting the QVariant containing the double 9.9 to int works.
2855 Rationale: "9.9" is not a valid int. However, doubles are by definition not
2856 ints and therefore it makes more sense to perform conversion for those.
2860 void tst_QVariant::toIntFromQString() const
2862 QVariant first("9.9");
2864 QCOMPARE(first.toInt(&ok), 0);
2867 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
2871 QCOMPARE(v.toInt(&ok), 10);
2877 1. Conversion from (64 bit) double to int works (no overflow).
2878 2. Same conversion works for QVariant::convert.
2880 Rationale: if 2147483630 is set in float and then converted to int,
2881 there will be overflow and the result will be -2147483648.
2885 void tst_QVariant::toIntFromDouble() const
2887 double d = 2147483630; // max int 2147483647
2888 QVERIFY((int)d == 2147483630);
2891 QVERIFY( var.canConvert( QVariant::Int ) );
2894 int result = var.toInt(&ok);
2896 QVERIFY( ok == true );
2897 QCOMPARE(result, 2147483630);
2900 void tst_QVariant::task256984_setValue()
2902 QTransform t; //we just take a value so that we're sure that it will be shared
2904 QVERIFY( v1.isDetached() );
2906 QVERIFY( !v1.isDetached() );
2907 QVERIFY( !v2.isDetached() );
2909 qVariantSetValue(v2, 3); //set an integer value
2911 QVERIFY( v1.isDetached() );
2912 QVERIFY( v2.isDetached() );
2915 void tst_QVariant::numericalConvert()
2917 QVariant vfloat(float(5.3));
2918 QVariant vdouble(double(5.3));
2919 QVariant vreal(qreal(5.3));
2920 QVariant vint(int(5));
2921 QVariant vuint(uint(5));
2922 QVariant vshort(short(5));
2923 QVariant vlonglong(quint64(5));
2924 QVariant vstringint(QString::fromLatin1("5"));
2925 QVariant vstring(QString::fromLatin1("5.3"));
2927 QVector<QVariant *> vect;
2928 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
2930 for(int i = 0; i < vect.size(); i++) {
2932 if (i >= 3 && i <= 7)
2934 QVariant *v = vect.at(i);
2935 QCOMPARE(v->toFloat() , float(num));
2936 QCOMPARE(float(v->toReal()) , float(num));
2937 QCOMPARE(float(v->toDouble()) , float(num));
2939 QCOMPARE(v->toInt() , int(num));
2940 QCOMPARE(v->toUInt() , uint(num));
2941 QCOMPARE(v->toULongLong() , quint64(num));
2943 QCOMPARE(v->toString() , QString::number(num));
2948 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
2950 QVariant v = QVariant::fromValue(orig);
2951 QVERIFY(v.isValid());
2952 QCOMPARE(v.isNull(), isNull);
2953 QCOMPARE(v.toString(), toString);
2954 QCOMPARE(v.toDouble(), toDouble);
2955 QCOMPARE(v.toBool(), toBool);
2956 QCOMPARE(qvariant_cast<T>(v), orig);
2961 QVERIFY(v2.isValid());
2962 QCOMPARE(v2.isNull(), isNull);
2963 QCOMPARE(v2.toString(), toString);
2964 QCOMPARE(v2.toDouble(), toDouble);
2965 QCOMPARE(v2.toBool(), toBool);
2966 QCOMPARE(qvariant_cast<T>(v2), orig);
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>()));
3002 bool operator==(const MyPrimitive &o) const
3004 return x == o.x && y == o.y;
3009 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3015 MyData() : ptr(this) {}
3018 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3020 MyData(const MyData& o) : ptr(this)
3022 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3024 MyData &operator=(const MyData &o)
3026 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3027 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3030 bool operator==(const MyData &o) const
3032 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3033 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3042 MyMovable() { v = count++; }
3043 ~MyMovable() { count--; }
3044 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3046 bool operator==(const MyMovable &o) const
3052 int MyMovable::count = 0;
3056 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3059 Q_DECLARE_METATYPE(QList<QSize>)
3060 Q_DECLARE_METATYPE(MyPrimitive)
3061 Q_DECLARE_METATYPE(MyData)
3062 Q_DECLARE_METATYPE(MyMovable)
3063 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3064 Q_DECLARE_METATYPE(QList<MyData>)
3065 Q_DECLARE_METATYPE(QList<MyMovable>)
3066 Q_DECLARE_METATYPE(MyPrimitive *)
3067 Q_DECLARE_METATYPE(MyData *)
3068 Q_DECLARE_METATYPE(MyMovable *)
3071 void tst_QVariant::moreCustomTypes()
3074 QList<QSize> listSize;
3075 playWithVariant(listSize, false, QString(), 0, false);
3076 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3077 playWithVariant(listSize, false, QString(), 0, false);
3082 playWithVariant(str, true, QString(), 0, false);
3083 str = QString::fromLatin1("123456789.123");
3084 playWithVariant(str, false, str, 123456789.123, true);
3089 playWithVariant(size, false, QString(), 0, false);
3090 playWithVariant(QSize(45,78), false, QString(), 0, false);
3095 playWithVariant(d, false, QString(), 0, false);
3096 playWithVariant(&d, false, QString(), 0, false);
3098 playWithVariant(l, false, QString(), 0, false);
3099 l << MyData() << MyData();
3100 playWithVariant(l, false, QString(), 0, false);
3104 MyPrimitive d = { 4, 5 };
3105 playWithVariant(d, false, QString(), 0, false);
3106 playWithVariant(&d, false, QString(), 0, false);
3107 QList<MyPrimitive> l;
3108 playWithVariant(l, false, QString(), 0, false);
3110 playWithVariant(l, false, QString(), 0, false);
3115 playWithVariant(d, false, QString(), 0, false);
3116 playWithVariant(&d, false, QString(), 0, false);
3118 playWithVariant(l, false, QString(), 0, false);
3119 l << MyMovable() << d;
3120 playWithVariant(l, false, QString(), 0, false);
3122 QCOMPARE(MyMovable::count, 0);
3125 playWithVariant(12.12, false, "12.12", 12.12, true);
3126 playWithVariant(12.12f, false, "12.12", 12.12f, true);
3127 playWithVariant('a', false, "a", 'a', true);
3128 playWithVariant((unsigned char)('a'), false, "a", 'a', true);
3129 playWithVariant( quint8(12), false, "\xc", 12, true);
3130 playWithVariant( qint8(13), false, "\xd", 13, true);
3131 playWithVariant(quint16(14), false, "14", 14, true);
3132 playWithVariant( qint16(15), false, "15", 15, true);
3133 playWithVariant(quint32(16), false, "16", 16, true);
3134 playWithVariant( qint32(17), false, "17", 17, true);
3135 playWithVariant(quint64(18), false, "18", 18, true);
3136 playWithVariant( qint64(19), false, "19", 19, true);
3137 playWithVariant( qint8(-12), false, "\xf4", -12, true);
3138 playWithVariant( qint16(-13), false, "-13", -13, true);
3139 playWithVariant( qint32(-14), false, "-14", -14, true);
3140 playWithVariant( qint64(-15), false, "-15", -15, true);
3141 playWithVariant(quint64(0), false, "0", 0, false);
3142 playWithVariant( true, false, "true", 1, true);
3143 playWithVariant( false, false, "false", 0, false);
3145 playWithVariant(QString("hello\n"), false, "hello\n", 0, true);
3150 playWithVariant((void *)(&i), false, QString(), 0, false);
3151 playWithVariant((void *)(0), false, QString(), 0, false);
3155 QVariant v1 = QVariant::fromValue(5);
3156 QVariant v2 = QVariant::fromValue(5.0);
3157 QVariant v3 = QVariant::fromValue(quint16(5));
3159 QVariant v5 = QVariant::fromValue(MyPrimitive());
3160 QVariant v6 = QVariant::fromValue(MyMovable());
3161 QVariant v7 = QVariant::fromValue(MyData());
3162 playWithVariant(v1, false, "5", 5, true);
3163 playWithVariant(v2, false, "5", 5, true);
3164 playWithVariant(v3, false, "5", 5, true);
3165 playWithVariant(v4, false, "5", 5, true);
3167 playWithVariant(v5, false, QString(), 0, false);
3172 void tst_QVariant::variantInVariant()
3175 QCOMPARE(var1.type(), QVariant::Int);
3176 QVariant var2 = var1;
3177 QCOMPARE(var2, var1);
3178 QCOMPARE(var2.type(), QVariant::Int);
3179 QVariant var3 = QVariant::fromValue(var1);
3180 QCOMPARE(var3, var1);
3181 QCOMPARE(var3.type(), QVariant::Int);
3182 QVariant var4 = qvariant_cast<QVariant>(var1);
3183 QCOMPARE(var4, var1);
3184 QCOMPARE(var4.type(), QVariant::Int);
3187 QCOMPARE(var5, var1);
3188 QCOMPARE(var5.type(), QVariant::Int);
3190 var6.setValue(var1);
3191 QCOMPARE(var6, var1);
3192 QCOMPARE(var6.type(), QVariant::Int);
3194 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3195 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3196 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3198 QString str("hello");
3199 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3200 QCOMPARE((int)var8.type(), (int)QVariant::String);
3201 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3203 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3204 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3205 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3208 void tst_QVariant::colorInteger()
3210 QVariant v = QColor(Qt::red);
3211 QCOMPARE(v.type(), QVariant::Color);
3212 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3215 QCOMPARE(v.type(), QVariant::Int);
3216 QCOMPARE(v.toInt(), 1000);
3218 v.setValue(QColor(Qt::yellow));
3219 QCOMPARE(v.type(), QVariant::Color);
3220 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3224 Q_DECLARE_METATYPE(Forward*);
3226 void tst_QVariant::forwardDeclare()
3229 QVariant v = QVariant::fromValue(f);
3230 QCOMPARE(qvariant_cast<Forward*>(v), f);
3234 QTEST_MAIN(tst_QVariant)
3235 #include "tst_qvariant.moc"