1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
45 #include <qbitarray.h>
46 #include <qhostaddress.h>
47 #include <qdatetime.h>
49 #include <qiodevice.h>
52 #include <qkeysequence.h>
58 #include <qmatrix4x4.h>
61 #include <qtransform.h>
62 #include <qvector2d.h>
63 #include <qvector3d.h>
64 #include <qvector4d.h>
65 #include <qquaternion.h>
70 Q_DECLARE_METATYPE(qlonglong)
71 Q_DECLARE_METATYPE(qulonglong)
72 Q_DECLARE_METATYPE(QPointF)
73 Q_DECLARE_METATYPE(QRectF)
74 Q_DECLARE_METATYPE(QSize)
75 Q_DECLARE_METATYPE(QSizeF)
76 Q_DECLARE_METATYPE(QLine)
77 Q_DECLARE_METATYPE(QLineF)
78 Q_DECLARE_METATYPE(QPoint)
79 Q_DECLARE_METATYPE(QRect)
80 Q_DECLARE_METATYPE(QPixmap)
81 Q_DECLARE_METATYPE(QBrush)
82 Q_DECLARE_METATYPE(QFont)
83 Q_DECLARE_METATYPE(QColor)
84 Q_DECLARE_METATYPE(QKeySequence)
86 class tst_QVariant : public QObject
92 void copy_constructor();
96 void canConvert_data();
129 void toLongLong_data();
132 void toULongLong_data();
135 void toByteArray_data();
138 void toString_data();
147 void toDateTime_data();
150 void toDouble_data();
153 void toPointF_data();
159 void toKeySequence_data();
160 void toKeySequence();
168 void toPixmap_data();
177 void qvariant_cast_QObject_data();
178 void qvariant_cast_QObject();
197 void basicUserType();
201 void writeToReadFromDataStream_data();
202 void writeToReadFromDataStream();
203 void writeToReadFromOldDataStream();
204 void checkDataStream();
206 void operator_eq_eq_data();
207 void operator_eq_eq();
209 void operator_eq_eq_rhs();
211 void typeName_data();
215 void streamInvalidVariant();
219 void data_(); // data is virtual function in QtTestCase
222 void saveLoadCustomTypes();
229 void convertToQUint8() const;
230 void invalidQColor() const;
231 void comparePointers() const;
232 void voidStar() const;
233 void dataStar() const;
234 void canConvertQStringList() const;
235 void canConvertQStringList_data() const;
236 void canConvertMetaTypeToInt() const;
237 void variantToDateTimeWithoutWarnings() const;
238 void invalidDateTime() const;
240 void loadUnknownUserType();
241 void loadBrokenUserType();
243 void invalidDate() const;
244 void compareCustomTypes() const;
245 void timeToDateTime() const;
246 void copyingUserTypes() const;
247 void convertBoolToByteArray() const;
248 void convertBoolToByteArray_data() const;
249 void convertByteArrayToBool() const;
250 void convertByteArrayToBool_data() const;
251 void toIntFromQString() const;
252 void toIntFromDouble() const;
255 void numericalConvert();
256 void moreCustomTypes();
257 void movabilityTest();
258 void variantInVariant();
262 void forwardDeclare();
263 void debugStream_data();
267 Q_DECLARE_METATYPE(QDate)
268 Q_DECLARE_METATYPE(QTime)
269 Q_DECLARE_METATYPE(QDateTime)
270 Q_DECLARE_METATYPE(QVariant)
272 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
273 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
275 void tst_QVariant::constructor()
278 QVERIFY( !variant.isValid() );
279 QVERIFY( variant.isNull() );
281 QVariant var2(variant);
282 QVERIFY( !var2.isValid() );
283 QVERIFY( variant.isNull() );
285 QVariant varll(intMax1);
286 QVariant varll2(varll);
287 QCOMPARE(varll2, varll);
289 QVariant var3(QVariant::String);
290 QCOMPARE(var3.typeName(), "QString");
291 QVERIFY(var3.isNull());
292 QVERIFY(var3.isValid());
294 QVariant var4(QVariant::Invalid);
295 QCOMPARE(var4.type(), QVariant::Invalid);
296 QVERIFY(var4.isNull());
297 QVERIFY(!var4.isValid());
299 QVariant var5(QLatin1String("hallo"));
300 QCOMPARE(var5.type(), QVariant::String);
301 QCOMPARE(var5.typeName(), "QString");
303 QVariant var6(qlonglong(0));
304 QCOMPARE(var6.type(), QVariant::LongLong);
305 QCOMPARE(var6.typeName(), "qlonglong");
308 QVERIFY(var7.isValid());
309 QVERIFY(!var7.isNull());
311 var8.setValue<int>(5);
312 QVERIFY(var8.isValid());
313 QVERIFY(!var8.isNull());
316 void tst_QVariant::copy_constructor()
318 QVariant var7(QVariant::Int);
320 QCOMPARE(var8.type(), QVariant::Int);
321 QVERIFY(var8.isNull());
324 void tst_QVariant::isNull()
327 QVERIFY( var.isNull() );
330 QVariant var1( str1 );
331 QVERIFY( var1.isNull() );
333 QVariant var2( QString::null );
334 QVERIFY( var2.isNull() );
336 QVariant var3( QString( "blah" ) );
337 QVERIFY( !var3.isNull() );
340 QVERIFY( !var4.isNull() );
342 QVariant var5 = QString();
343 QVERIFY( var5.isNull() );
345 QVariant var6( QString( "blah" ) );
346 QVERIFY( !var6.isNull() );
348 QVERIFY( var6.isNull() );
349 var6.convert( QVariant::String );
350 QVERIFY( var6.isNull() );
351 QVariant varLL( (qlonglong)0 );
352 QVERIFY( !varLL.isNull() );
353 QVariant var7(QString::null);
354 QVERIFY(var7.isNull());
357 void tst_QVariant::swap()
359 QVariant v1 = 1, v2 = 2.0;
361 QCOMPARE(v1.type(),QVariant::Double);
362 QCOMPARE(v1.toDouble(),2.0);
363 QCOMPARE(v2.type(),QVariant::Int);
364 QCOMPARE(v2.toInt(),1);
367 void tst_QVariant::canConvert_data()
369 QTest::addColumn<QVariant>("val");
370 QTest::addColumn<bool>("BitArrayCast");
371 QTest::addColumn<bool>("BitmapCast");
372 QTest::addColumn<bool>("BoolCast");
373 QTest::addColumn<bool>("BrushCast");
374 QTest::addColumn<bool>("ByteArrayCast");
375 QTest::addColumn<bool>("ColorCast");
376 QTest::addColumn<bool>("CursorCast");
377 QTest::addColumn<bool>("DateCast");
378 QTest::addColumn<bool>("DateTimeCast");
379 QTest::addColumn<bool>("DoubleCast");
380 QTest::addColumn<bool>("FontCast");
381 QTest::addColumn<bool>("ImageCast");
382 QTest::addColumn<bool>("IntCast");
383 QTest::addColumn<bool>("InvalidCast");
384 QTest::addColumn<bool>("KeySequenceCast");
385 QTest::addColumn<bool>("ListCast");
386 QTest::addColumn<bool>("LongLongCast");
387 QTest::addColumn<bool>("MapCast");
388 QTest::addColumn<bool>("PaletteCast");
389 QTest::addColumn<bool>("PenCast");
390 QTest::addColumn<bool>("PixmapCast");
391 QTest::addColumn<bool>("PointCast");
392 QTest::addColumn<bool>("RectCast");
393 QTest::addColumn<bool>("RegionCast");
394 QTest::addColumn<bool>("SizeCast");
395 QTest::addColumn<bool>("SizePolicyCast");
396 QTest::addColumn<bool>("StringCast");
397 QTest::addColumn<bool>("StringListCast");
398 QTest::addColumn<bool>("TimeCast");
399 QTest::addColumn<bool>("UIntCast");
400 QTest::addColumn<bool>("ULongLongCast");
411 // 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
414 QVariant var(QBitArray(0));
415 QTest::newRow("BitArray")
416 << 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;
417 var = qVariantFromValue(QBitmap());
418 QTest::newRow("Bitmap")
419 << 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;
420 var = qVariantFromValue(QBrush());
421 QTest::newRow("Brush")
422 << 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;
423 var = QVariant(QByteArray());
424 QTest::newRow("ByteArray")
425 << 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;
426 var = qVariantFromValue(QColor());
427 QTest::newRow("Color")
428 << 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;
430 var = qVariantFromValue(QCursor());
431 QTest::newRow("Cursor")
432 << 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;
434 var = QVariant(QDate());
435 QTest::newRow("Date")
436 << 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;
437 var = QVariant(QDateTime());
438 QTest::newRow("DateTime")
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 << Y << N << N;
440 var = QVariant((double)0.1);
441 QTest::newRow("Double")
442 << 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;
443 var = QVariant(0.1f);
444 QTest::newRow("Float")
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 = qVariantFromValue(QFont());
447 QTest::newRow("Font")
448 << 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;
449 var = qVariantFromValue(QIcon());
450 QTest::newRow("Icon")
451 << 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;
452 var = qVariantFromValue(QImage());
453 QTest::newRow("Image")
454 << 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;
455 var = QVariant((int)1);
457 << 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;
459 QTest::newRow("Invalid")
460 << 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;
461 var = qVariantFromValue(QKeySequence());
462 QTest::newRow("KeySequence")
463 << 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;
464 var = QVariant(QList<QVariant>());
465 QTest::newRow("List")
466 << 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;
467 var = QVariant((qlonglong)1);
468 QTest::newRow("LongLong")
469 << 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;
470 var = QVariant(QMap<QString,QVariant>());
472 << 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;
473 var = qVariantFromValue(QPalette());
474 QTest::newRow("Palette")
475 << 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;
476 var = qVariantFromValue(QPen());
478 << 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;
479 var = qVariantFromValue(QPixmap());
480 QTest::newRow("Pixmap")
481 << 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;
482 var = qVariantFromValue(QPolygon());
483 QTest::newRow("PointArray")
484 << 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;
485 var = QVariant(QPoint());
486 QTest::newRow("Point")
487 << 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;
488 var = QVariant(QRect());
489 QTest::newRow("Rect")
490 << 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;
491 var = qVariantFromValue(QRegion());
492 QTest::newRow("Region")
493 << 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;
494 var = QVariant(QSize());
495 QTest::newRow("Size")
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 << N << Y << N << N << N << N << N << N;
497 var = qVariantFromValue(QSizePolicy());
498 QTest::newRow("SizePolicy")
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 << N << Y << N << N << N << N << N;
500 var = QVariant(QString());
501 QTest::newRow("String")
502 << 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;
503 var = QVariant(QStringList("entry"));
504 QTest::newRow("StringList")
505 << 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;
506 var = QVariant(QTime());
507 QTest::newRow("Time")
508 << 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;
509 var = QVariant((uint)1);
510 QTest::newRow("UInt")
511 << 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;
512 var = QVariant((qulonglong)1);
513 QTest::newRow("ULongLong")
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;
520 void tst_QVariant::canConvert()
522 QFETCH(QVariant, val);
523 QFETCH(bool, BitArrayCast);
524 QFETCH(bool, BitmapCast);
525 QFETCH(bool, BoolCast);
526 QFETCH(bool, BrushCast);
527 QFETCH(bool, ByteArrayCast);
528 QFETCH(bool, ColorCast);
529 QFETCH(bool, CursorCast);
530 QFETCH(bool, DateCast);
531 QFETCH(bool, DateTimeCast);
532 QFETCH(bool, DoubleCast);
533 QFETCH(bool, FontCast);
534 QFETCH(bool, ImageCast);
535 QFETCH(bool, IntCast);
536 QFETCH(bool, InvalidCast);
537 QFETCH(bool, KeySequenceCast);
538 QFETCH(bool, ListCast);
539 QFETCH(bool, LongLongCast);
540 QFETCH(bool, MapCast);
541 QFETCH(bool, PaletteCast);
542 QFETCH(bool, PenCast);
543 QFETCH(bool, PixmapCast);
544 QFETCH(bool, PointCast);
545 QFETCH(bool, RectCast);
546 QFETCH(bool, RegionCast);
547 QFETCH(bool, SizeCast);
548 QFETCH(bool, SizePolicyCast);
549 QFETCH(bool, StringCast);
550 QFETCH(bool, StringListCast);
551 QFETCH(bool, TimeCast);
552 QFETCH(bool, UIntCast);
553 QFETCH(bool, ULongLongCast);
555 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
556 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
557 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
558 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
559 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
560 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
561 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
562 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
563 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
564 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
565 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
566 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
567 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
568 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
569 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
570 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
571 QCOMPARE(val.canConvert(QVariant::List), ListCast);
572 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
573 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
574 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
575 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
576 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
577 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
578 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
579 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
580 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
581 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
582 QCOMPARE(val.canConvert(QVariant::String), StringCast);
583 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
584 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
585 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
586 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
589 void tst_QVariant::toInt_data()
591 QTest::addColumn<QVariant>("value");
592 QTest::addColumn<int>("result");
593 QTest::addColumn<bool>("valueOK");
595 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
596 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
597 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
598 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
599 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
600 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
601 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
602 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
603 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
604 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
605 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
606 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
607 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
608 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
609 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
610 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
611 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
612 QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
613 QByteArray bytearray(4, ' ');
618 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
623 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
626 void tst_QVariant::toInt()
628 QFETCH( QVariant, value );
629 QFETCH( int, result );
630 QFETCH( bool, valueOK );
631 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
633 int i = value.toInt( &ok );
634 QCOMPARE( i, result );
635 QVERIFY( ok == valueOK );
638 void tst_QVariant::toUInt_data()
640 QTest::addColumn<QVariant>("value");
641 QTest::addColumn<uint>("result");
642 QTest::addColumn<bool>("valueOK");
644 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
645 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
646 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
647 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
648 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
649 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
650 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
651 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
652 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
653 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
654 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
655 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
656 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
657 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
658 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
659 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
660 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
661 QByteArray bytearray(4, ' ');
666 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
669 void tst_QVariant::toUInt()
671 QFETCH( QVariant, value );
672 QFETCH( uint, result );
673 QFETCH( bool, valueOK );
674 QVERIFY( value.isValid() );
675 QVERIFY( value.canConvert( QVariant::UInt ) );
678 uint i = value.toUInt( &ok );
679 QVERIFY( ok == valueOK );
680 QCOMPARE( i, result );
684 void tst_QVariant::toSize_data()
686 QTest::addColumn<QVariant>("value");
687 QTest::addColumn<QSize>("result");
688 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
689 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
690 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
691 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
694 void tst_QVariant::toSize()
696 QFETCH( QVariant, value );
697 QFETCH( QSize, result );
698 QVERIFY( value.isValid() );
699 QVERIFY( value.canConvert( QVariant::Size ) );
701 QSize i = value.toSize();
702 QCOMPARE( i, result );
705 void tst_QVariant::toSizeF_data()
707 QTest::addColumn<QVariant>("value");
708 QTest::addColumn<QSizeF>("result");
709 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
710 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
711 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
712 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
715 void tst_QVariant::toSizeF()
717 QFETCH( QVariant, value );
718 QFETCH( QSizeF, result );
719 QVERIFY( value.isValid() );
720 QVERIFY( value.canConvert( QVariant::SizeF ) );
722 QSizeF i = value.toSizeF();
723 QCOMPARE( i, result );
726 void tst_QVariant::toLine_data()
728 QTest::addColumn<QVariant>("value");
729 QTest::addColumn<QLine>("result");
730 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
731 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
732 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
733 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
736 void tst_QVariant::toLine()
738 QFETCH( QVariant, value );
739 QFETCH( QLine, result );
740 QVERIFY( value.isValid() );
741 QVERIFY( value.canConvert( QVariant::Line ) );
743 QLine i = value.toLine();
744 QCOMPARE( i, result );
747 void tst_QVariant::toLineF_data()
749 QTest::addColumn<QVariant>("value");
750 QTest::addColumn<QLineF>("result");
751 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
752 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
753 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
754 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
757 void tst_QVariant::toLineF()
759 QFETCH( QVariant, value );
760 QFETCH( QLineF, result );
761 QVERIFY( value.isValid() );
762 QVERIFY( value.canConvert( QVariant::LineF ) );
764 QLineF i = value.toLineF();
765 QCOMPARE( i, result );
768 void tst_QVariant::toPoint_data()
770 QTest::addColumn<QVariant>("value");
771 QTest::addColumn<QPoint>("result");
772 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
773 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
774 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
775 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
776 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
779 void tst_QVariant::toPoint()
781 QFETCH( QVariant, value );
782 QFETCH( QPoint, result );
783 QVERIFY( value.isValid() );
784 QVERIFY( value.canConvert( QVariant::Point ) );
785 QPoint i = value.toPoint();
786 QCOMPARE( i, result );
789 void tst_QVariant::toRect_data()
791 QTest::addColumn<QVariant>("value");
792 QTest::addColumn<QRect>("result");
793 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
794 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
795 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
796 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
797 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
800 void tst_QVariant::toRect()
802 QFETCH( QVariant, value );
803 QFETCH( QRect, result );
804 QVERIFY( value.isValid() );
805 QVERIFY( value.canConvert( QVariant::Rect ) );
806 QRect i = value.toRect();
807 QCOMPARE( i, result );
810 void tst_QVariant::toChar_data()
812 QTest::addColumn<QVariant>("value");
813 QTest::addColumn<QChar>("result");
814 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
815 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
818 void tst_QVariant::toChar()
820 QFETCH( QVariant, value );
821 QFETCH( QChar, result );
822 QVERIFY( value.isValid() );
823 QVERIFY( value.canConvert( QVariant::Char ) );
825 QChar i = value.toChar();
826 QCOMPARE( i, result );
829 void tst_QVariant::toBool_data()
831 QTest::addColumn<QVariant>("value");
832 QTest::addColumn<bool>("result");
834 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
835 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
836 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
837 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
838 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
839 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
840 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
841 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
842 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
843 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
844 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
845 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
846 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
847 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
848 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
849 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
850 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
851 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
854 void tst_QVariant::toBool()
856 QFETCH( QVariant, value );
857 QFETCH( bool, result );
858 QVERIFY( value.isValid() );
859 QVERIFY( value.canConvert( QVariant::Bool ) );
861 bool i = value.toBool();
862 QCOMPARE( i, result );
865 void tst_QVariant::toPointF_data()
867 QTest::addColumn<QVariant>("value");
868 QTest::addColumn<QPointF>("result");
870 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
873 void tst_QVariant::toPointF()
875 QFETCH( QVariant, value );
876 QFETCH( QPointF, result );
877 QVERIFY( value.isValid() );
878 QVERIFY( value.canConvert( QVariant::PointF ) );
879 QPointF d = value.toPointF();
880 QCOMPARE( d, result );
883 void tst_QVariant::toRectF_data()
885 QTest::addColumn<QVariant>("value");
886 QTest::addColumn<QRectF>("result");
888 QRect r( 1, 9, 8, 4 );
889 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
890 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
893 void tst_QVariant::toRectF()
895 QFETCH( QVariant, value );
896 QFETCH( QRectF, result );
897 QVERIFY( value.isValid() );
898 QVERIFY( value.canConvert( QVariant::RectF ) );
899 QRectF d = value.toRectF();
900 QCOMPARE( d, result );
903 void tst_QVariant::toColor_data()
905 QTest::addColumn<QVariant>("value");
906 QTest::addColumn<QColor>("result");
909 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
910 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
913 void tst_QVariant::toColor()
915 QFETCH( QVariant, value );
916 QFETCH( QColor, result );
917 QVERIFY( value.isValid() );
918 QVERIFY( value.canConvert( QVariant::Color ) );
919 QColor d = qVariantValue<QColor>(value);
920 QCOMPARE( d, result );
923 void tst_QVariant::toPixmap_data()
925 QTest::addColumn<QVariant>("value");
926 QTest::addColumn<QPixmap>("result");
930 QTest::newRow( "image" ) << QVariant( pm ) << pm;
934 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
937 void tst_QVariant::toPixmap()
939 QFETCH( QVariant, value );
940 QFETCH( QPixmap, result );
941 QVERIFY( value.isValid() );
942 QVERIFY( value.canConvert( QVariant::Pixmap ) );
943 QPixmap d = qVariantValue<QPixmap>(value);
944 QCOMPARE( d, result );
947 void tst_QVariant::toImage_data()
949 QTest::addColumn<QVariant>("value");
950 QTest::addColumn<QImage>("result");
952 QImage im(30, 30, QImage::Format_ARGB32);
954 QTest::newRow( "image" ) << QVariant( im ) << im;
957 void tst_QVariant::toImage()
959 QFETCH( QVariant, value );
960 QFETCH( QImage, result );
961 QVERIFY( value.isValid() );
962 QVERIFY( value.canConvert( QVariant::Image ) );
963 QImage d = qVariantValue<QImage>(value);
964 QCOMPARE( d, result );
967 void tst_QVariant::toBrush_data()
969 QTest::addColumn<QVariant>("value");
970 QTest::addColumn<QBrush>("result");
973 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
976 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
979 void tst_QVariant::toBrush()
981 QFETCH( QVariant, value );
982 QFETCH( QBrush, result );
983 QVERIFY( value.isValid() );
984 QVERIFY( value.canConvert( QVariant::Brush ) );
985 QBrush d = qVariantValue<QBrush>(value);
986 QCOMPARE( d, result );
989 void tst_QVariant::toFont_data()
991 QTest::addColumn<QVariant>("value");
992 QTest::addColumn<QFont>("result");
994 QFont f("times",12,-1,false);
995 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
998 void tst_QVariant::toFont()
1000 QFETCH( QVariant, value );
1001 QFETCH( QFont, result );
1002 QVERIFY( value.isValid() );
1003 QVERIFY( value.canConvert( QVariant::Font ) );
1004 QFont d = qVariantValue<QFont>(value);
1005 QCOMPARE( d, result );
1008 void tst_QVariant::toKeySequence_data()
1010 QTest::addColumn<QVariant>("value");
1011 QTest::addColumn<QKeySequence>("result");
1014 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1017 QTest::newRow( "qstring" )
1018 << QVariant( QString( "Ctrl+A" ) )
1019 << QKeySequence( Qt::CTRL + Qt::Key_A );
1022 void tst_QVariant::toKeySequence()
1024 QFETCH( QVariant, value );
1025 QFETCH( QKeySequence, result );
1026 QVERIFY( value.isValid() );
1027 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1028 QKeySequence d = qVariantValue<QKeySequence>(value);
1029 QCOMPARE( d, result );
1032 void tst_QVariant::toDouble_data()
1034 QTest::addColumn<QVariant>("value");
1035 QTest::addColumn<double>("result");
1036 QTest::addColumn<bool>("valueOK");
1038 QByteArray bytearray(4, ' ');
1043 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1046 void tst_QVariant::toDouble()
1048 QFETCH( QVariant, value );
1049 QFETCH( double, result );
1050 QFETCH( bool, valueOK );
1051 QVERIFY( value.isValid() );
1052 QVERIFY( value.canConvert( QVariant::Double ) );
1054 double d = value.toDouble( &ok );
1055 QCOMPARE( d, result );
1056 QVERIFY( ok == valueOK );
1059 void tst_QVariant::toLongLong_data()
1061 QTest::addColumn<QVariant>("value");
1062 QTest::addColumn<qlonglong>("result");
1063 QTest::addColumn<bool>("valueOK");
1065 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1066 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1067 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1068 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1069 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1070 << (qlonglong)123 << true;
1071 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1073 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1074 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1075 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1076 QByteArray bytearray(4, ' ');
1081 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1084 void tst_QVariant::toLongLong()
1086 QFETCH( QVariant, value );
1087 QFETCH( qlonglong, result );
1088 QFETCH( bool, valueOK );
1089 QVERIFY( value.isValid() );
1090 QVERIFY( value.canConvert( QVariant::LongLong ) );
1092 qlonglong ll = value.toLongLong( &ok );
1093 QCOMPARE( ll, result );
1094 QVERIFY( ok == valueOK );
1097 void tst_QVariant::toULongLong_data()
1099 QTest::addColumn<QVariant>("value");
1100 QTest::addColumn<qulonglong>("result");
1101 QTest::addColumn<bool>("valueOK");
1103 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1104 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1105 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1106 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1107 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1108 << (qulonglong)123 << true;
1109 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1111 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1112 << Q_UINT64_C(18446744073709551615)
1114 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1115 << Q_UINT64_C(18446744073709551615)
1117 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1118 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1119 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1120 QByteArray bytearray(4, ' ');
1125 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1128 void tst_QVariant::toULongLong()
1130 QFETCH( QVariant, value );
1131 QFETCH( qulonglong, result );
1132 QFETCH( bool, valueOK );
1133 QVERIFY( value.isValid() );
1134 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1136 qulonglong ll = value.toULongLong( &ok );
1137 QCOMPARE( ll, result );
1138 QVERIFY( ok == valueOK );
1141 void tst_QVariant::toByteArray_data()
1143 QTest::addColumn<QVariant>("value");
1144 QTest::addColumn<QByteArray>("result");
1146 QByteArray ba(5, ' ');
1153 QByteArray variantBa = ba;
1155 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1156 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1157 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1158 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1159 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1160 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1161 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1164 void tst_QVariant::toByteArray()
1166 QFETCH( QVariant, value );
1167 QFETCH( QByteArray, result );
1168 QVERIFY( value.isValid() );
1169 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1170 QByteArray ba = value.toByteArray();
1171 QCOMPARE( ba, result );
1174 void tst_QVariant::toString_data()
1176 QTest::addColumn<QVariant>("value");
1177 QTest::addColumn<QString>("result");
1179 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1180 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1181 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1182 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1183 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1184 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1185 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1186 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1187 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1188 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1189 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1190 QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1192 << QString( "Ctrl+A" );
1194 << QString(QChar(0x2318)) + "A";
1197 QFont font( "times", 12 );
1198 QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1199 QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1200 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1201 QString( "123456789012" );
1204 void tst_QVariant::toString()
1206 QFETCH( QVariant, value );
1207 QFETCH( QString, result );
1208 QVERIFY( value.isValid() );
1209 QVERIFY( value.canConvert( QVariant::String ) );
1210 QString str = value.toString();
1211 QCOMPARE( str, result );
1214 void tst_QVariant::toDate_data()
1216 QTest::addColumn<QVariant>("value");
1217 QTest::addColumn<QDate>("result");
1219 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1220 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1221 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1224 void tst_QVariant::toDate()
1226 QFETCH( QVariant, value );
1227 QFETCH( QDate, result );
1228 QVERIFY( value.isValid() );
1229 QVERIFY( value.canConvert( QVariant::Date ) );
1230 QCOMPARE( value.toDate(), result );
1233 void tst_QVariant::toTime_data()
1235 QTest::addColumn<QVariant>("value");
1236 QTest::addColumn<QTime>("result");
1238 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1239 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1240 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1243 void tst_QVariant::toTime()
1245 QFETCH( QVariant, value );
1246 QFETCH( QTime, result );
1247 QVERIFY( value.isValid() );
1248 QVERIFY( value.canConvert( QVariant::Time ) );
1249 QCOMPARE( value.toTime(), result );
1252 void tst_QVariant::toDateTime_data()
1254 QTest::addColumn<QVariant>("value");
1255 QTest::addColumn<QDateTime>("result");
1257 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1258 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1259 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1260 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1263 void tst_QVariant::toDateTime()
1265 QFETCH( QVariant, value );
1266 QFETCH( QDateTime, result );
1267 QVERIFY( value.isValid() );
1268 QVERIFY( value.canConvert( QVariant::DateTime ) );
1269 QCOMPARE( value.toDateTime(), result );
1272 void tst_QVariant::toLocale()
1275 QLocale loc = variant.toLocale();
1276 variant = QLocale::system();
1277 loc = variant.toLocale();
1280 void tst_QVariant::toRegExp()
1283 QRegExp rx = variant.toRegExp();
1284 variant = QRegExp("foo");
1285 rx = variant.toRegExp();
1288 void tst_QVariant::matrix()
1291 QMatrix matrix = qVariantValue<QMatrix>(variant);
1292 QVERIFY(matrix.isIdentity());
1293 qVariantSetValue(variant, QMatrix().rotate(90));
1294 QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1296 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1298 QMetaType::destroy(QVariant::Matrix, mmatrix);
1301 void tst_QVariant::matrix4x4()
1304 QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1305 QVERIFY(matrix.isIdentity());
1308 qVariantSetValue(variant, m);
1309 QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1311 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1313 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1316 void tst_QVariant::transform()
1319 QTransform matrix = qVariantValue<QTransform>(variant);
1320 QVERIFY(matrix.isIdentity());
1321 qVariantSetValue(variant, QTransform().rotate(90));
1322 QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1324 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1326 QMetaType::destroy(QVariant::Transform, mmatrix);
1330 void tst_QVariant::vector2D()
1333 QVector2D vector = qVariantValue<QVector2D>(variant);
1334 QVERIFY(vector.isNull());
1335 qVariantSetValue(variant, QVector2D(0.1, 0.2));
1336 QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1338 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1340 QMetaType::destroy(QVariant::Vector2D, pvector);
1343 void tst_QVariant::vector3D()
1346 QVector3D vector = qVariantValue<QVector3D>(variant);
1347 QVERIFY(vector.isNull());
1348 qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1349 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1351 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1353 QMetaType::destroy(QVariant::Vector3D, pvector);
1356 void tst_QVariant::vector4D()
1359 QVector4D vector = qVariantValue<QVector4D>(variant);
1360 QVERIFY(vector.isNull());
1361 qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1362 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1364 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1366 QMetaType::destroy(QVariant::Vector4D, pvector);
1369 void tst_QVariant::quaternion()
1372 QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1373 QVERIFY(quaternion.isIdentity());
1374 qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1375 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1377 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1378 QVERIFY(pquaternion);
1379 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1382 struct CustomStreamableClass
1385 bool operator==(const CustomStreamableClass& other) const
1387 return i == other.i;
1390 Q_DECLARE_METATYPE(CustomStreamableClass);
1392 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1394 return out << myObj.i;
1397 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1399 return in >> myObj.i;
1402 void tst_QVariant::writeToReadFromDataStream_data()
1404 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1406 QTest::addColumn<QVariant>("writeVariant");
1407 QTest::addColumn<bool>("isNull");
1409 typedef QList<QVariant> variantsList;
1410 variantsList valuelist;
1411 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1412 QVariant var(valuelist);
1413 QTest::newRow( "list_valid" ) << var << false;
1416 QTest::newRow( "invalid" ) << QVariant() << true;
1417 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1418 QBitArray bitarray( 3 );
1422 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1423 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1424 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1425 QByteArray bytearray(5, ' ');
1430 bytearray[4] = '\0';
1431 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1432 QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1433 QBitmap bitmap( 10, 10 );
1434 bitmap.fill( Qt::red );
1435 QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1436 QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1437 QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1438 #ifndef QT_NO_CURSOR
1439 QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1441 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1442 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1443 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1444 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1445 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1446 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1447 QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1448 QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1449 QPixmap pixmap( 10, 10 );
1450 pixmap.fill( Qt::red );
1451 QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1452 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1453 QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1454 QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1455 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1456 typedef QMap<QString, QVariant> variantsMap;
1458 vMap.insert( "int", QVariant( 1 ) );
1459 vMap.insert( "string", QVariant( QString("Two") ) );
1460 vMap.insert( "double", QVariant( 3.45 ) );
1461 vMap.insert( "float", QVariant( 3.45f ) );
1462 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1463 QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1464 QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1465 QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1466 QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1467 QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1468 QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1469 QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1470 QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1471 QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1472 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1473 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1474 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1475 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1476 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1477 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1478 QStringList stringlist;
1479 stringlist << "One" << "Two" << "Three";
1480 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1481 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1482 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1483 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1484 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1485 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1486 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1487 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1489 // types known to QMetaType, but not part of QVariant::Type
1490 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1492 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1493 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1495 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1496 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1498 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1499 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1500 ulong ulongInt = 1ul;
1501 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1502 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1503 ushort ushortInt = 1u;
1504 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1505 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1507 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1508 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1510 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1511 CustomStreamableClass custom = {123};
1512 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1515 void tst_QVariant::writeToReadFromDataStream()
1517 QFETCH( QVariant, writeVariant );
1518 QFETCH( bool, isNull );
1521 QDataStream writeStream( &data, QIODevice::WriteOnly );
1522 writeStream << writeVariant;
1524 QVariant readVariant;
1525 QDataStream readStream( &data, QIODevice::ReadOnly );
1526 readStream >> readVariant;
1527 QVERIFY( readVariant.isNull() == isNull );
1528 // Best way to confirm the readVariant contains the same data?
1529 // Since only a few won't match since the serial numbers are different
1530 // I won't bother adding another bool in the data test.
1531 const int writeType = writeVariant.userType();
1532 if (writeType == qMetaTypeId<CustomStreamableClass>())
1533 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1534 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1535 && writeType != QVariant::Image) {
1536 switch (writeType) {
1538 QCOMPARE( readVariant, writeVariant );
1541 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1542 case QMetaType::Long:
1543 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1545 case QMetaType::ULong:
1546 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1548 case QMetaType::Short:
1549 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1551 case QMetaType::UShort:
1552 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1554 case QMetaType::Char:
1555 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1557 case QMetaType::UChar:
1558 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1560 case QMetaType::Float:
1562 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1563 float readFloat = qvariant_cast<float>(readVariant);
1564 float writtenFloat = qvariant_cast<float>(writeVariant);
1565 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1566 if (!qIsNaN(readFloat))
1567 QVERIFY(readFloat == writtenFloat);
1574 void tst_QVariant::writeToReadFromOldDataStream()
1576 QVariant writeVariant = QString("hello");
1579 QDataStream writeStream(&data, QIODevice::WriteOnly);
1580 writeStream.setVersion(QDataStream::Qt_2_1);
1581 writeStream << writeVariant;
1583 QVariant readVariant;
1584 QDataStream readStream(&data, QIODevice::ReadOnly);
1585 readStream.setVersion(QDataStream::Qt_2_1);
1586 readStream >> readVariant;
1588 QCOMPARE(writeVariant.userType(), readVariant.userType());
1589 QCOMPARE(writeVariant, readVariant);
1592 void tst_QVariant::checkDataStream()
1594 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 46");
1595 const QByteArray settingsHex("0000002effffffffff");
1596 const QByteArray settings = QByteArray::fromHex(settingsHex);
1597 QDataStream in(settings);
1600 // the line below has been left out for now since the data stream
1601 // is not necessarily considered corrupt when an invalid QVariant is
1602 // constructed. However, it might be worth considering changing that behavior
1604 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1605 QCOMPARE(v.type(), QVariant::Invalid);
1608 void tst_QVariant::operator_eq_eq_data()
1610 QTest::addColumn<QVariant>("left");
1611 QTest::addColumn<QVariant>("right");
1612 QTest::addColumn<bool>("equal"); // left == right ?
1615 QVariant i0( int(0) );
1616 QVariant i1( int(1) );
1618 QTest::newRow( "invinv" ) << inv << inv << true;
1620 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1621 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1622 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1624 // LongLong and ULongLong
1625 QVariant ll1( (qlonglong)1 );
1626 QVariant lln2( (qlonglong)-2 );
1627 QVariant ull1( (qulonglong)1 );
1628 QVariant ull3( (qulonglong)3 );
1629 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1630 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1631 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1632 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1633 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1634 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1637 QVariant mIntString(QByteArray("-42"));
1638 QVariant mIntQString(QString("-42"));
1640 QVariant mUInt(42u);
1641 QVariant mUIntString(QByteArray("42"));
1642 QVariant mUIntQString(QString("42"));
1644 QVariant mDouble(42.11);
1645 QVariant mDoubleString(QByteArray("42.11"));
1646 QVariant mDoubleQString(QString("42.11"));
1648 QVariant mFloat(42.11f);
1649 QVariant mFloatString(QByteArray("42.11"));
1650 QVariant mFloatQString(QString("42.11"));
1652 QVariant mLongLong((qlonglong)-42);
1653 QVariant mLongLongString(QByteArray("-42"));
1654 QVariant mLongLongQString(QString("-42"));
1656 QVariant mULongLong((qulonglong)42);
1657 QVariant mULongLongString(QByteArray("42"));
1658 QVariant mULongLongQString(QString("42"));
1660 QVariant mBool(false);
1661 QVariant mBoolString(QByteArray("false"));
1662 QVariant mBoolQString(QString("false"));
1664 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1665 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1666 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1667 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1668 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1669 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1671 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1672 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1673 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1674 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1676 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1677 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1678 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1679 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1681 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1682 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1683 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1684 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1686 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1687 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1688 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1689 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1691 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1692 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1693 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1694 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1696 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1697 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1698 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1699 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1701 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1702 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1703 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1704 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1706 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1707 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1708 // ### many other combinations missing
1711 QMap<QString, QVariant> map1;
1712 map1.insert( "X", 1 );
1714 QMap<QString, QVariant> map2;
1715 map2.insert( "Y", 1 );
1717 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1722 QMap<QString, QVariant> map1;
1723 map1.insert( "X", 1 );
1725 QMap<QString, QVariant> map2;
1726 map2.insert( "X", 1 );
1728 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1732 QMap<QString, QVariant> map1;
1733 map1.insert( "X", 1 );
1735 QMap<QString, QVariant> map2;
1737 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1741 QMap<QString, QVariant> map1;
1743 QMap<QString, QVariant> map2;
1744 map2.insert( "X", 1 );
1746 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1750 QMap<QString, QVariant> map1;
1751 map1.insert( "X", 1 );
1752 map1.insert( "Y", 1 );
1754 QMap<QString, QVariant> map2;
1755 map2.insert( "X", 1 );
1757 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1761 QMap<QString, QVariant> map1;
1762 map1.insert( "X", 1 );
1764 QMap<QString, QVariant> map2;
1765 map2.insert( "X", 1 );
1766 map2.insert( "Y", 1 );
1768 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1771 // same thing with hash
1773 QHash<QString, QVariant> hash1;
1774 hash1.insert( "X", 1 );
1776 QHash<QString, QVariant> hash2;
1777 hash2.insert( "Y", 1 );
1779 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1784 QHash<QString, QVariant> hash1;
1785 hash1.insert( "X", 1 );
1787 QHash<QString, QVariant> hash2;
1788 hash2.insert( "X", 1 );
1790 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1794 QHash<QString, QVariant> hash1;
1795 hash1.insert( "X", 1 );
1797 QHash<QString, QVariant> hash2;
1799 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1803 QHash<QString, QVariant> hash1;
1805 QHash<QString, QVariant> hash2;
1806 hash2.insert( "X", 1 );
1808 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1812 QHash<QString, QVariant> hash1;
1813 hash1.insert( "X", 1 );
1814 hash1.insert( "Y", 1 );
1816 QHash<QString, QVariant> hash2;
1817 hash2.insert( "X", 1 );
1819 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1823 QHash<QString, QVariant> hash1;
1824 hash1.insert( "X", 1 );
1826 QHash<QString, QVariant> hash2;
1827 hash2.insert( "X", 1 );
1828 hash2.insert( "Y", 1 );
1830 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1833 QTest::newRow( "UserType" ) << QVariant(QVariant::UserType) << QVariant(QVariant::UserType) << true;
1834 QVariant mUserType(QVariant::UserType);
1835 QTest::newRow( "Shared UserType" ) << mUserType << mUserType << true;
1838 void tst_QVariant::operator_eq_eq()
1840 QFETCH( QVariant, left );
1841 QFETCH( QVariant, right );
1842 QFETCH( bool, equal );
1843 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1844 QCOMPARE( left == right, equal );
1847 void tst_QVariant::operator_eq_eq_rhs()
1855 /* This should _not_ compile */
1859 QVERIFY(dt == list);
1863 void tst_QVariant::typeName_data()
1865 QTest::addColumn<int>("type");
1866 QTest::addColumn<QByteArray>("res");
1867 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1868 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1869 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1870 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1871 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1872 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1873 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1874 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1875 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1876 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1877 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1878 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1879 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1880 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1881 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1882 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1883 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1884 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1885 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1886 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1887 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1888 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1889 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1890 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1891 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1892 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1893 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1894 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1895 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1896 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1897 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1898 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1899 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1900 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1901 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1902 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1903 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1904 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1905 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1906 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1907 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1908 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1909 QTest::newRow("42") << int(QVariant::UserType) << QByteArray("UserType");
1910 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1911 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1912 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1913 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1914 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1915 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1916 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1917 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1920 void tst_QVariant::typeName()
1922 QFETCH( int, type );
1923 QFETCH( QByteArray, res );
1924 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1925 QString::fromLatin1(res.constData()));
1928 // test nameToType as well
1929 void tst_QVariant::typeToName()
1932 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1933 // assumes that QVariant::Type contains consecutive values
1935 int max = QVariant::LastGuiType;
1936 for ( int t = 1; t <= max; t++ ) {
1937 const char *n = QVariant::typeToName( (QVariant::Type)t );
1939 QCOMPARE( int(QVariant::nameToType( n )), t );
1942 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1943 // not documented but we return 0 if the type is out of range
1944 // by testing this we catch cases where QVariant is extended
1945 // but type_map is not updated accordingly
1946 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1947 // invalid type names
1948 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
1949 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
1950 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
1951 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Icon);
1954 void tst_QVariant::streamInvalidVariant()
1960 QVariant writeVariant;
1961 QVariant readVariant;
1963 QVERIFY( writeVariant.type() == QVariant::Invalid );
1966 QDataStream writeStream( &data, QIODevice::WriteOnly );
1967 writeStream << writeX << writeVariant << writeY;
1969 QDataStream readStream( &data, QIODevice::ReadOnly );
1970 readStream >> readX >> readVariant >> readY;
1972 QVERIFY( readX == writeX );
1973 // Two invalid QVariant's aren't necessarily the same, so == will
1974 // return false if one is invalid, so check the type() instead
1975 QVERIFY( readVariant.type() == QVariant::Invalid );
1976 QVERIFY( readY == writeY );
1979 static int instanceCount = 0;
1983 MyType(int n = 0, const char *t=0): number(n), text(t)
1987 MyType(const MyType &other)
1988 : number(other.number), text(other.text)
2000 Q_DECLARE_METATYPE(MyType)
2001 Q_DECLARE_METATYPE(MyType*)
2003 void tst_QVariant::userType()
2006 QVariant userVariant(QVariant::UserType);
2008 QVERIFY(userVariant.isValid());
2009 QVERIFY(userVariant.isNull());
2013 MyType data(1, "eins");
2014 MyType data2(2, "zwei");
2018 qVariantSetValue(userVar, data);
2020 QCOMPARE(userVar.type(), QVariant::UserType);
2021 QCOMPARE(userVar.typeName(), "MyType");
2022 QVERIFY(!userVar.isNull());
2023 QVERIFY(!userVar.canConvert(QVariant::String));
2024 QVERIFY(!userVar.canConvert(QVariant::UserType));
2026 QVariant userVar2(userVar);
2027 QVERIFY(userVar == userVar2);
2029 qVariantSetValue(userVar2, data2);
2030 QVERIFY(userVar != userVar2);
2032 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2034 QCOMPARE(varData->number, data.number);
2035 QCOMPARE(varData->text, data.text);
2038 qVariantSetValue(userVar3, data2);
2040 userVar3 = userVar2;
2041 QVERIFY(userVar2 == userVar3);
2043 // At this point all QVariants got destroyed but we have 2 MyType instances.
2044 QCOMPARE(instanceCount, 2);
2047 qVariantSetValue(userVar, &data);
2049 QCOMPARE(userVar.type(), QVariant::UserType);
2050 QCOMPARE(userVar.typeName(), "MyType*");
2051 QVERIFY(!userVar.isNull());
2052 QVERIFY(!userVar.canConvert(QVariant::String));
2053 QVERIFY(!userVar.canConvert(QVariant::UserType));
2055 QVariant userVar2(userVar);
2056 QVERIFY(userVar == userVar2);
2058 qVariantSetValue(userVar2, &data2);
2059 QVERIFY(userVar != userVar2);
2061 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2063 QCOMPARE(*varData, &data);
2066 qVariantSetValue(userVar3, &data2);
2068 /* This check is correct now. userVar2 contains a pointer to data2 and so
2070 QVERIFY(userVar2 == userVar3);
2072 userVar3 = userVar2;
2073 QVERIFY(userVar2 == userVar3);
2076 QCOMPARE(instanceCount, 2);
2078 qVariantSetValue(myCarrier, data);
2079 QCOMPARE(instanceCount, 3);
2081 QVariant second = myCarrier;
2082 QCOMPARE(instanceCount, 3);
2084 QCOMPARE(instanceCount, 4);
2086 QCOMPARE(instanceCount, 3);
2088 MyType data3(0, "null");
2089 data3 = qVariantValue<MyType>(myCarrier);
2090 QCOMPARE(data3.number, 1);
2091 QCOMPARE(data3.text, (const char *)"eins");
2093 QCOMPARE(instanceCount, 4);
2099 const MyType data(3, "drei");
2102 qVariantSetValue(myCarrier, data);
2103 QCOMPARE(myCarrier.typeName(), "MyType");
2105 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2106 QCOMPARE(data2.number, 3);
2107 QCOMPARE(data2.text, (const char *)"drei");
2114 qVariantSetValue(myCarrier, s);
2115 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2119 qlonglong ll = Q_INT64_C(42);
2122 qVariantSetValue(myCarrier, ll);
2123 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2126 // At this point all QVariants got destroyed and MyType objects too.
2127 QCOMPARE(instanceCount, 0);
2135 Q_DECLARE_METATYPE(MyTypePOD)
2137 void tst_QVariant::podUserType()
2143 QVariant pod_as_variant = qVariantFromValue(pod);
2144 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2146 QCOMPARE(pod.a, pod2.a);
2147 QCOMPARE(pod.b, pod2.b);
2149 qVariantSetValue(pod_as_variant, pod);
2150 pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2152 QCOMPARE(pod.a, pod2.a);
2153 QCOMPARE(pod.b, pod2.b);
2156 void tst_QVariant::basicUserType()
2161 v = QVariant(QMetaType::Int, &i);
2163 QCOMPARE(v.type(), QVariant::Int);
2164 QCOMPARE(v.toInt(), 7);
2168 v = QVariant(QMetaType::QString, &s);
2170 QCOMPARE(v.type(), QVariant::String);
2171 QCOMPARE(v.toString(), QString("foo"));
2175 v = QVariant(QMetaType::Double, &d);
2177 QCOMPARE(v.type(), QVariant::Double);
2178 QCOMPARE(v.toDouble(), 4.4);
2182 v = QVariant(QMetaType::Float, &f);
2184 QCOMPARE(v.userType(), int(QMetaType::Float));
2185 QCOMPARE(v.toDouble(), 4.5);
2188 QByteArray ba("bar");
2189 v = QVariant(QMetaType::QByteArray, &ba);
2191 QCOMPARE(v.type(), QVariant::ByteArray);
2192 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2195 void tst_QVariant::data_()
2202 QVariant ll = (qlonglong)2;
2203 QVariant ull = (qulonglong)3;
2204 QVariant s(QString("hallo"));
2205 QVariant r(QRect(1,2,3,4));
2209 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2213 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2217 QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2221 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2225 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2229 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2233 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2236 void tst_QVariant::constData()
2249 QVERIFY(v.constData());
2250 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2253 QVERIFY(v.constData());
2254 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2257 QVERIFY(v.constData());
2258 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2261 QVERIFY(v.constData());
2262 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2265 QVERIFY(v.constData());
2266 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2269 QVERIFY(v.constData());
2270 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2273 QVERIFY(v.constData());
2274 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2283 Q_DECLARE_METATYPE(Foo)
2285 void tst_QVariant::variant_to()
2294 sl << QLatin1String("blah");
2296 qVariantSetValue(v3, sl);
2301 qVariantSetValue(v4, foo);
2303 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2304 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2305 QCOMPARE(qvariant_cast<int>(v2), 5);
2306 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2307 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2309 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2312 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2314 QCOMPARE(qvariant_cast<int>(v1), 4);
2316 QVariant n = qVariantFromValue<short>(42);
2317 QCOMPARE(qvariant_cast<int>(n), 42);
2318 QCOMPARE(qvariant_cast<uint>(n), 42u);
2319 QCOMPARE(qvariant_cast<double>(n), 42.0);
2320 QCOMPARE(qvariant_cast<float>(n), 42.f);
2321 QCOMPARE(qvariant_cast<short>(n), short(42));
2322 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2324 n = qVariantFromValue(43l);
2325 QCOMPARE(qvariant_cast<int>(n), 43);
2326 QCOMPARE(qvariant_cast<uint>(n), 43u);
2327 QCOMPARE(qvariant_cast<double>(n), 43.0);
2328 QCOMPARE(qvariant_cast<float>(n), 43.f);
2329 QCOMPARE(qvariant_cast<long>(n), 43l);
2331 n = QLatin1String("44");
2332 QCOMPARE(qvariant_cast<int>(n), 44);
2333 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2334 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2336 QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2339 struct Blah { int i; };
2341 QDataStream& operator>>(QDataStream& s, Blah& c)
2342 { return (s >> c.i); }
2344 QDataStream& operator<<(QDataStream& s, const Blah& c)
2345 { return (s << c.i); }
2347 void tst_QVariant::saveLoadCustomTypes()
2352 int tp = qRegisterMetaType<Blah>("Blah");
2353 QVariant v = QVariant(tp, &i);
2355 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2357 QCOMPARE(v.userType(), tp);
2358 QVERIFY(v.type() == QVariant::UserType);
2360 QDataStream stream(&data, QIODevice::WriteOnly);
2367 QDataStream stream(data);
2371 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2372 int value = *(int*)v.constData();
2373 QCOMPARE(value, 42);
2376 void tst_QVariant::url()
2378 QString str("http://qt.nokia.com");
2381 QVariant v(url); //built with a QUrl
2385 QVariant v3(str); //built with a QString
2387 QCOMPARE(v2.toUrl(), url);
2388 QVERIFY(qVariantCanConvert<QUrl>(v3));
2389 QCOMPARE(v2.toUrl(), v3.toUrl());
2391 QVERIFY(qVariantCanConvert<QString>(v2));
2392 QCOMPARE(v2.toString(), str);
2393 QCOMPARE(v3.toString(), str);
2396 void tst_QVariant::globalColor()
2398 QVariant variant(Qt::blue);
2399 QVERIFY(variant.type() == QVariant::Color);
2400 QVERIFY(qVariantValue<QColor>(variant) == QColor(Qt::blue));
2403 void tst_QVariant::variantMap()
2405 QMap<QString, QVariant> map;
2409 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2411 QCOMPARE(map2.value("test").toInt(), 42);
2413 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2414 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2416 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2417 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2420 void tst_QVariant::variantHash()
2422 QHash<QString, QVariant> hash;
2426 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2428 QCOMPARE(hash2.value("test").toInt(), 42);
2430 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2431 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2433 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2434 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2437 void tst_QVariant::invalidQColor() const
2439 QVariant va("An invalid QColor::name() value.");
2440 QVERIFY(va.canConvert(QVariant::Color));
2442 QVERIFY(!va.convert(QVariant::Color));
2444 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2447 void tst_QVariant::qvariant_cast_QObject_data() {
2449 QTest::addColumn<QVariant>("data");
2450 QTest::addColumn<bool>("success");
2451 QObject *obj = new QObject(this);
2452 obj->setObjectName(QString::fromLatin1("Hello"));
2453 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2454 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2455 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2456 QTest::newRow("from int") << QVariant((int) 123) << false;
2460 void tst_QVariant::qvariant_cast_QObject() {
2461 QFETCH(QVariant, data);
2462 QFETCH(bool, success);
2464 QObject *o = qvariant_cast<QObject *>(data);
2465 QCOMPARE(o != 0, success);
2467 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2471 Q_DECLARE_METATYPE(qint8);
2473 void tst_QVariant::convertToQUint8() const
2477 const qint8 anInt = 32;
2479 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2480 * It's of type QVariant::Int. */
2481 const QVariant v0 = anInt;
2483 QVERIFY(qVariantCanConvert<qint8>(v0));
2484 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2485 QCOMPARE(int(v0.toInt()), 32);
2486 QCOMPARE(v0.toString(), QString("32"));
2488 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2489 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2490 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2491 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2492 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2493 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2498 const quint8 anInt = 32;
2499 const QVariant v0 = anInt;
2501 QVERIFY(qVariantCanConvert<quint8>(v0));
2502 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2503 QCOMPARE(int(v0.toUInt()), 32);
2504 QCOMPARE(v0.toString(), QString("32"));
2509 const qint16 anInt = 32;
2510 const QVariant v0 = anInt;
2512 QVERIFY(qVariantCanConvert<qint16>(v0));
2513 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2514 QCOMPARE(int(v0.toInt()), 32);
2515 QCOMPARE(v0.toString(), QString("32"));
2520 const quint16 anInt = 32;
2521 const QVariant v0 = anInt;
2523 QVERIFY(qVariantCanConvert<quint16>(v0));
2524 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2525 QCOMPARE(int(v0.toUInt()), 32);
2526 QCOMPARE(v0.toString(), QString("32"));
2530 void tst_QVariant::comparePointers() const
2538 QVariant v = qVariantFromValue<void *>(&myClass);
2539 QVariant v2 = qVariantFromValue<void *>(&myClass);
2545 Q_DECLARE_METATYPE(Data*)
2547 void tst_QVariant::voidStar() const
2554 v1 = qVariantFromValue(p1);
2558 v2 = qVariantFromValue(p2);
2562 v2 = qVariantFromValue(p2);
2566 void tst_QVariant::dataStar() const
2568 qRegisterMetaType<Data*>();
2569 Data *p1 = new Data;
2571 QVariant v1 = qVariantFromValue(p1);
2572 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2573 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2578 v2 = qVariantFromValue(p1);
2583 void tst_QVariant::canConvertQStringList() const
2585 QFETCH(bool, canConvert);
2586 QFETCH(QStringList, input);
2587 QFETCH(QString, result);
2591 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2592 QCOMPARE(v.toString(), result);
2595 void tst_QVariant::canConvertQStringList_data() const
2597 QTest::addColumn<bool>("canConvert");
2598 QTest::addColumn<QStringList>("input");
2599 QTest::addColumn<QString>("result");
2601 QTest::newRow("An empty list") << false << QStringList() << QString();
2602 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2603 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2608 QTest::newRow("Two items") << false << l << QString();
2611 QTest::newRow("Three items") << false << l << QString();
2614 template<typename T> void convertMetaType()
2616 QVERIFY(qVariantFromValue<T>(10).isValid());
2617 QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2618 QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2619 QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2622 #define CONVERT_META_TYPE(Type) \
2623 convertMetaType<Type>(); \
2624 if (QTest::currentTestFailed()) \
2625 QFAIL("convertMetaType<" #Type "> failed");
2627 void tst_QVariant::canConvertMetaTypeToInt() const
2629 CONVERT_META_TYPE(long);
2630 CONVERT_META_TYPE(short);
2631 CONVERT_META_TYPE(short);
2632 CONVERT_META_TYPE(unsigned short);
2633 CONVERT_META_TYPE(ushort);
2634 CONVERT_META_TYPE(ulong);
2635 CONVERT_META_TYPE(unsigned long);
2636 CONVERT_META_TYPE(uchar);
2637 CONVERT_META_TYPE(unsigned char);
2638 CONVERT_META_TYPE(char);
2639 CONVERT_META_TYPE(uint);
2640 CONVERT_META_TYPE(unsigned int);
2643 #undef CONVERT_META_TYPE
2646 These calls should not produce any warnings.
2648 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2651 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2652 const QDateTime dateTime(variant.toDateTime());
2653 QVERIFY(!dateTime.isValid());
2657 QVariant v1(QLatin1String("xyz"));
2658 v1.convert(QVariant::DateTime);
2660 QVariant v2(QLatin1String("xyz"));
2661 QDateTime dt1(v2.toDateTime());
2663 const QVariant v3(QLatin1String("xyz"));
2664 const QDateTime dt2(v3.toDateTime());
2668 void tst_QVariant::invalidDateTime() const
2670 QVariant variant(QString::fromLatin1("Invalid date time string"));
2671 QVERIFY(!variant.toDateTime().isValid());
2672 QVERIFY(!variant.convert(QVariant::DateTime));
2677 MyClass() : myValue(0) {}
2681 Q_DECLARE_METATYPE( MyClass )
2683 void tst_QVariant::loadUnknownUserType()
2685 qRegisterMetaType<MyClass>("MyClass");
2686 char data[] = {0, 0, 0, 127, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0};
2688 QByteArray ba(data, sizeof(data));
2689 QDataStream ds(&ba, QIODevice::ReadOnly);
2692 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2695 void tst_QVariant::loadBrokenUserType()
2697 char data[] = {0, 0, 0, 127, 0, 112 };
2699 QByteArray ba(data, sizeof(data));
2700 QDataStream ds(&ba, QIODevice::ReadOnly);
2703 QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2706 void tst_QVariant::invalidDate() const
2708 QString foo("Hello");
2709 QVariant variant(foo);
2710 QVERIFY(!variant.convert(QVariant::Date));
2713 QVERIFY(!variant.convert(QVariant::DateTime));
2716 QVERIFY(!variant.convert(QVariant::Time));
2719 QVERIFY(!variant.convert(QVariant::Int));
2722 QVERIFY(!variant.convert(QVariant::Double));
2725 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2730 int x,y,z,q,w,e,r,t;
2732 Q_DECLARE_METATYPE(WontCompare);
2734 void tst_QVariant::compareCustomTypes() const
2736 qRegisterMetaType<WontCompare>("WontCompare");
2740 const QVariant variant1(qVariantFromValue(f1));
2744 const QVariant variant2(qVariantFromValue(f2));
2746 /* We compare pointers. */
2747 QVERIFY(variant1 != variant2);
2748 QVERIFY(variant1 == variant1);
2749 QVERIFY(variant2 == variant2);
2752 void tst_QVariant::timeToDateTime() const
2754 const QVariant val(QTime::currentTime());
2755 QVERIFY(!val.canConvert(QVariant::DateTime));
2756 QVERIFY(!val.toDateTime().isValid());
2759 Q_DECLARE_METATYPE(QHostAddress)
2761 void tst_QVariant::copyingUserTypes() const
2765 const QHostAddress ha("127.0.0.1");
2766 qVariantSetValue(var, ha);
2769 QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2772 void tst_QVariant::convertBoolToByteArray() const
2774 QFETCH(QByteArray, input);
2775 QFETCH(bool, canConvert);
2776 QFETCH(bool, value);
2778 const QVariant variant(input);
2780 QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2783 /* Just call this function so we run the code path. */
2784 QCOMPARE(variant.toBool(), value);
2788 void tst_QVariant::convertBoolToByteArray_data() const
2790 QTest::addColumn<QByteArray>("input");
2791 QTest::addColumn<bool>("canConvert");
2792 QTest::addColumn<bool>("value");
2794 QTest::newRow("false")
2795 << QByteArray("false")
2799 QTest::newRow("FALSE")
2800 << QByteArray("FALSE")
2804 QTest::newRow("falSE")
2805 << QByteArray("FALSE")
2814 QTest::newRow("null QByteArray")
2819 QTest::newRow("any-content")
2820 << QByteArray("any-content")
2824 QTest::newRow("true")
2825 << QByteArray("true")
2829 QTest::newRow("TRUE")
2830 << QByteArray("TRUE")
2834 QTest::newRow("trUE")
2835 << QByteArray("trUE")
2840 void tst_QVariant::convertByteArrayToBool() const
2842 QFETCH(bool, input);
2843 QFETCH(QByteArray, output);
2845 const QVariant variant(input);
2846 QCOMPARE(variant.type(), QVariant::Bool);
2847 QCOMPARE(variant.toBool(), input);
2848 QVERIFY(qVariantCanConvert<bool>(variant));
2850 QCOMPARE(variant.toByteArray(), output);
2853 void tst_QVariant::convertByteArrayToBool_data() const
2855 QTest::addColumn<bool>("input");
2856 QTest::addColumn<QByteArray>("output");
2858 QTest::newRow("false")
2860 << QByteArray("false");
2862 QTest::newRow("true")
2864 << QByteArray("true");
2869 1. Converting the string "9.9" to int fails. This is the behavior of
2870 toLongLong() and hence also QVariant, since it uses it.
2871 2. Converting the QVariant containing the double 9.9 to int works.
2873 Rationale: "9.9" is not a valid int. However, doubles are by definition not
2874 ints and therefore it makes more sense to perform conversion for those.
2876 void tst_QVariant::toIntFromQString() const
2878 QVariant first("9.9");
2880 QCOMPARE(first.toInt(&ok), 0);
2883 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
2887 QCOMPARE(v.toInt(&ok), 10);
2893 1. Conversion from (64 bit) double to int works (no overflow).
2894 2. Same conversion works for QVariant::convert.
2896 Rationale: if 2147483630 is set in float and then converted to int,
2897 there will be overflow and the result will be -2147483648.
2899 void tst_QVariant::toIntFromDouble() const
2901 double d = 2147483630; // max int 2147483647
2902 QVERIFY((int)d == 2147483630);
2905 QVERIFY( var.canConvert( QVariant::Int ) );
2908 int result = var.toInt(&ok);
2910 QVERIFY( ok == true );
2911 QCOMPARE(result, 2147483630);
2914 void tst_QVariant::setValue()
2916 QTransform t; //we just take a value so that we're sure that it will be shared
2918 QVERIFY( v1.isDetached() );
2920 QVERIFY( !v1.isDetached() );
2921 QVERIFY( !v2.isDetached() );
2923 qVariantSetValue(v2, 3); //set an integer value
2925 QVERIFY( v1.isDetached() );
2926 QVERIFY( v2.isDetached() );
2929 void tst_QVariant::numericalConvert()
2931 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
2932 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
2934 QVariant vfloat(float(5.3));
2935 QVariant vdouble(double(5.3));
2936 QVariant vreal(qreal(5.3));
2937 QVariant vint(int(5));
2938 QVariant vuint(uint(5));
2939 QVariant vshort(short(5));
2940 QVariant vlonglong(quint64(5));
2941 QVariant vstringint(QString::fromLatin1("5"));
2942 QVariant vstring(QString::fromLatin1("5.3"));
2944 QVector<QVariant *> vect;
2945 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
2947 for(int i = 0; i < vect.size(); i++) {
2949 if (i >= 3 && i <= 7)
2951 QVariant *v = vect.at(i);
2952 QCOMPARE(v->toFloat() , float(num));
2953 QCOMPARE(float(v->toReal()) , float(num));
2954 QCOMPARE(float(v->toDouble()) , float(num));
2956 QCOMPARE(v->toInt() , int(num));
2957 QCOMPARE(v->toUInt() , uint(num));
2958 QCOMPARE(v->toULongLong() , quint64(num));
2960 QCOMPARE(v->toString() , QString::number(num));
2965 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
2967 QVariant v = QVariant::fromValue(orig);
2968 QVERIFY(v.isValid());
2969 QCOMPARE(v.isNull(), isNull);
2970 QCOMPARE(v.toString(), toString);
2971 QCOMPARE(v.toDouble(), toDouble);
2972 QCOMPARE(v.toBool(), toBool);
2973 QCOMPARE(qvariant_cast<T>(v), orig);
2977 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
2978 // Type is movable so standard comparison algorithm in QVariant should work
2979 // In a custom type QVariant is not aware of ==operator so it won't be called,
2980 // which may cause problems especially visible when using a not-movable type
2983 QVERIFY(v2.isValid());
2984 QCOMPARE(v2.isNull(), isNull);
2985 QCOMPARE(v2.toString(), toString);
2986 QCOMPARE(v2.toDouble(), toDouble);
2987 QCOMPARE(v2.toBool(), toBool);
2988 QCOMPARE(qvariant_cast<T>(v2), orig);
2994 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
2995 // Type is movable so standard comparison algorithm in QVariant should work
2996 // In a custom type QVariant is not aware of ==operator so it won't be called,
2997 // which may cause problems especially visible when using a not-movable type
3000 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3001 QCOMPARE(v2.toString(), toString);
3002 v3 = qVariantFromValue(orig);
3004 QVERIFY(v3.isValid());
3005 QCOMPARE(v3.isNull(), isNull);
3006 QCOMPARE(v3.toString(), toString);
3007 QCOMPARE(v3.toDouble(), toDouble);
3008 QCOMPARE(v3.toBool(), toBool);
3009 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3012 QVERIFY(v.isValid());
3013 QCOMPARE(v.isNull(), isNull);
3014 QCOMPARE(v.toString(), toString);
3015 QCOMPARE(v.toDouble(), toDouble);
3016 QCOMPARE(v.toBool(), toBool);
3017 QCOMPARE(qvariant_cast<T>(v), orig);
3019 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3020 QCOMPARE(v.userType(), qMetaTypeId<T>());
3021 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3025 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3026 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3027 if (QTest::currentTestFailed())\
3028 QFAIL("playWithVariant failed");
3033 bool operator==(const MyPrimitive &o) const
3035 return x == o.x && y == o.y;
3040 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3046 MyData() : ptr(this) {}
3049 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3051 MyData(const MyData& o) : ptr(this)
3053 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3055 MyData &operator=(const MyData &o)
3057 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3058 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3061 bool operator==(const MyData &o) const
3063 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3064 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3073 MyMovable() { v = count++; }
3074 ~MyMovable() { count--; }
3075 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3077 bool operator==(const MyMovable &o) const
3083 int MyMovable::count = 0;
3089 MyNotMovable() : that(this) { count++; }
3090 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3091 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3092 MyNotMovable &operator=(const MyNotMovable &o) {
3093 bool ok = that == this && o.that == &o;
3094 if (!ok) qFatal("MyNotMovable has been moved");
3098 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3099 // so it would be fine just to always return true
3100 bool operator==(const MyNotMovable &o) const
3102 bool ok = that == this && o.that == &o;
3103 if (!ok) qFatal("MyNotMovable has been moved");
3108 int MyNotMovable::count = 0;
3110 struct MyShared : QSharedData {
3115 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3118 Q_DECLARE_METATYPE(QList<QSize>)
3119 Q_DECLARE_METATYPE(MyPrimitive)
3120 Q_DECLARE_METATYPE(MyData)
3121 Q_DECLARE_METATYPE(MyMovable)
3122 Q_DECLARE_METATYPE(MyNotMovable)
3123 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3124 Q_DECLARE_METATYPE(QList<MyData>)
3125 Q_DECLARE_METATYPE(QList<MyMovable>)
3126 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3127 Q_DECLARE_METATYPE(MyPrimitive *)
3128 Q_DECLARE_METATYPE(MyData *)
3129 Q_DECLARE_METATYPE(MyMovable *)
3130 Q_DECLARE_METATYPE(MyNotMovable *)
3131 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3134 void tst_QVariant::moreCustomTypes()
3137 QList<QSize> listSize;
3138 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3139 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3140 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3145 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3146 str = QString::fromLatin1("123456789.123");
3147 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3152 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3153 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3158 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3159 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3161 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3162 l << MyData() << MyData();
3163 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3167 MyPrimitive d = { 4, 5 };
3168 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3169 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3170 QList<MyPrimitive> l;
3171 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3173 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3178 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3179 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3181 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3182 l << MyMovable() << d;
3183 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3185 QCOMPARE(MyMovable::count, 0);
3187 QCOMPARE(MyNotMovable::count, 0);
3190 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3191 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3192 QList<MyNotMovable> l;
3193 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3194 l << MyNotMovable() << d;
3195 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3197 QCOMPARE(MyNotMovable::count, 0);
3200 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3201 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3202 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3203 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3204 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3205 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3206 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3207 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3208 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3209 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3210 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3211 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3212 PLAY_WITH_VARIANT( qint8(-12), false, "\xf4", -12, true);
3213 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3214 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3215 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3216 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3217 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3218 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3220 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3225 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3226 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3230 QVariant v1 = QVariant::fromValue(5);
3231 QVariant v2 = QVariant::fromValue(5.0);
3232 QVariant v3 = QVariant::fromValue(quint16(5));
3234 QVariant v5 = QVariant::fromValue(MyPrimitive());
3235 QVariant v6 = QVariant::fromValue(MyMovable());
3236 QVariant v7 = QVariant::fromValue(MyData());
3237 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3238 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3239 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3240 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3242 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3245 QCOMPARE(MyMovable::count, 0);
3247 QSharedDataPointer<MyShared> d(new MyShared);
3248 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3250 QCOMPARE(MyMovable::count, 0);
3253 void tst_QVariant::movabilityTest()
3255 // This test checks if QVariant is movable even if an internal data is not movable.
3256 QVERIFY(!MyNotMovable::count);
3258 QVariant variant = QVariant::fromValue(MyNotMovable());
3259 QVERIFY(MyNotMovable::count);
3261 // prepare destination memory space to which variant will be moved
3263 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3264 buffer[0].~QVariant();
3266 memcpy(buffer, &variant, sizeof(QVariant));
3267 QCOMPARE(buffer[0].type(), QVariant::UserType);
3268 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3270 new (&variant) QVariant();
3272 QVERIFY(!MyNotMovable::count);
3275 void tst_QVariant::variantInVariant()
3278 QCOMPARE(var1.type(), QVariant::Int);
3279 QVariant var2 = var1;
3280 QCOMPARE(var2, var1);
3281 QCOMPARE(var2.type(), QVariant::Int);
3282 QVariant var3 = QVariant::fromValue(var1);
3283 QCOMPARE(var3, var1);
3284 QCOMPARE(var3.type(), QVariant::Int);
3285 QVariant var4 = qvariant_cast<QVariant>(var1);
3286 QCOMPARE(var4, var1);
3287 QCOMPARE(var4.type(), QVariant::Int);
3290 QCOMPARE(var5, var1);
3291 QCOMPARE(var5.type(), QVariant::Int);
3293 var6.setValue(var1);
3294 QCOMPARE(var6, var1);
3295 QCOMPARE(var6.type(), QVariant::Int);
3297 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3298 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3299 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3301 QString str("hello");
3302 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3303 QCOMPARE((int)var8.type(), (int)QVariant::String);
3304 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3306 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3307 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3308 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3311 void tst_QVariant::colorInteger()
3313 QVariant v = QColor(Qt::red);
3314 QCOMPARE(v.type(), QVariant::Color);
3315 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3318 QCOMPARE(v.type(), QVariant::Int);
3319 QCOMPARE(v.toInt(), 1000);
3321 v.setValue(QColor(Qt::yellow));
3322 QCOMPARE(v.type(), QVariant::Color);
3323 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3327 Q_DECLARE_METATYPE(Forward*);
3329 void tst_QVariant::forwardDeclare()
3332 QVariant v = QVariant::fromValue(f);
3333 QCOMPARE(qvariant_cast<Forward*>(v), f);
3337 class MessageHandler {
3339 MessageHandler(const int typeId)
3340 : oldMsgHandler(qInstallMsgHandler(handler))
3347 qInstallMsgHandler(oldMsgHandler);
3350 bool testPassed() const
3355 static void handler(QtMsgType, const char *txt)
3357 QString msg = QString::fromLatin1(txt);
3358 // Format itself is not important, but basic data as a type name should be included in the output
3359 ok = msg.startsWith("QVariant(") + QMetaType::typeName(currentId);
3360 QVERIFY2(ok, (QString::fromLatin1("Message is not valid: '") + msg + '\'').toLatin1().constData());
3363 QtMsgHandler oldMsgHandler;
3364 static int currentId;
3367 bool MessageHandler::ok;
3368 int MessageHandler::currentId;
3370 void tst_QVariant::debugStream_data()
3372 QTest::addColumn<QVariant>("variant");
3373 QTest::addColumn<int>("typeId");
3374 for (int id = QMetaType::Void; id < QMetaType::User; ++id) {
3375 const char *tagName = QMetaType::typeName(id);
3378 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3380 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3381 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3382 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3385 void tst_QVariant::debugStream()
3387 QFETCH(QVariant, variant);
3388 QFETCH(int, typeId);
3390 MessageHandler msgHandler(typeId);
3391 qDebug() << variant;
3392 QVERIFY(msgHandler.testPassed());
3396 QTEST_MAIN(tst_QVariant)
3397 #include "tst_qvariant.moc"