Make tst_QVariant::canConvertMetaTypeToInt more verbose.
[profile/ivi/qtbase.git] / tests / auto / corelib / kernel / qvariant / tst_qvariant.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #include <qvariant.h>
45 #include <qbitarray.h>
46 #include <qhostaddress.h>
47 #include <qdatetime.h>
48 #include <qmap.h>
49 #include <qiodevice.h>
50 #include <qurl.h>
51 #include <qlocale.h>
52 #include <qkeysequence.h>
53 #include <qbitmap.h>
54 #include <qcursor.h>
55 #include <qimage.h>
56 #include <qicon.h>
57 #include <qmatrix.h>
58 #include <qmatrix4x4.h>
59 #include <qpen.h>
60 #include <qpolygon.h>
61 #include <qtransform.h>
62 #include <qvector2d.h>
63 #include <qvector3d.h>
64 #include <qvector4d.h>
65 #include <qquaternion.h>
66
67 #include <limits.h>
68
69 Q_DECLARE_METATYPE(qlonglong)
70 Q_DECLARE_METATYPE(qulonglong)
71 Q_DECLARE_METATYPE(QPointF)
72 Q_DECLARE_METATYPE(QRectF)
73 Q_DECLARE_METATYPE(QSize)
74 Q_DECLARE_METATYPE(QSizeF)
75 Q_DECLARE_METATYPE(QLine)
76 Q_DECLARE_METATYPE(QLineF)
77 Q_DECLARE_METATYPE(QPoint)
78 Q_DECLARE_METATYPE(QRect)
79 Q_DECLARE_METATYPE(QPixmap)
80 Q_DECLARE_METATYPE(QBrush)
81 Q_DECLARE_METATYPE(QFont)
82 Q_DECLARE_METATYPE(QColor)
83 Q_DECLARE_METATYPE(QKeySequence)
84
85 //TESTED_CLASS=
86 //TESTED_FILES=
87
88 class tst_QVariant : public QObject
89 {
90     Q_OBJECT
91
92 private slots:
93     void constructor();
94     void copy_constructor();
95     void isNull();
96     void swap();
97
98     void canConvert_data();
99     void canConvert();
100
101     void toSize_data();
102     void toSize();
103
104     void toSizeF_data();
105     void toSizeF();
106
107     void toPoint_data();
108     void toPoint();
109
110     void toRect_data();
111     void toRect();
112
113     void toChar_data();
114     void toChar();
115
116     void toLine_data();
117     void toLine();
118
119     void toLineF_data();
120     void toLineF();
121
122     void toInt_data();
123     void toInt();
124
125     void toUInt_data();
126     void toUInt();
127
128     void toBool_data();
129     void toBool();
130
131     void toLongLong_data();
132     void toLongLong();
133
134     void toULongLong_data();
135     void toULongLong();
136
137     void toByteArray_data();
138     void toByteArray();
139
140     void toString_data();
141     void toString();
142
143     void toDate_data();
144     void toDate();
145
146     void toTime_data();
147     void toTime();
148
149     void toDateTime_data();
150     void toDateTime();
151
152     void toDouble_data();
153     void toDouble();
154
155     void toPointF_data();
156     void toPointF();
157
158     void toFont_data();
159     void toFont();
160
161     void toKeySequence_data();
162     void toKeySequence();
163
164     void toRectF_data();
165     void toRectF();
166
167     void toColor_data();
168     void toColor();
169
170     void toPixmap_data();
171     void toPixmap();
172
173     void toImage_data();
174     void toImage();
175
176     void toBrush_data();
177     void toBrush();
178
179     void qvariant_cast_QObject_data();
180     void qvariant_cast_QObject();
181
182     void toLocale();
183
184     void toRegExp();
185
186     void matrix();
187
188     void transform();
189
190     void matrix4x4();
191     void vector2D();
192     void vector3D();
193     void vector4D();
194     void quaternion();
195
196     void url();
197
198     void userType();
199     void basicUserType();
200
201     void variant_to();
202
203     void writeToReadFromDataStream_data();
204     void writeToReadFromDataStream();
205     void writeToReadFromOldDataStream();
206     void checkDataStream();
207
208     void operator_eq_eq_data();
209     void operator_eq_eq();
210
211     void operator_eq_eq_rhs();
212
213     void typeName_data();
214     void typeName();
215     void typeToName();
216
217     void streamInvalidVariant();
218
219     void podUserType();
220
221     void data_(); // data is virtual function in QtTestCase
222     void constData();
223
224     void saveLoadCustomTypes();
225
226     void globalColor();
227
228     void variantMap();
229     void variantHash();
230
231     void convertToQUint8() const;
232     void invalidQColor() const;
233     void comparePointers() const;
234     void voidStar() const;
235     void dataStar() const;
236     void canConvertQStringList() const;
237     void canConvertQStringList_data() const;
238     void canConvertMetaTypeToInt() const;
239     void variantToDateTimeWithoutWarnings() const;
240     void invalidDateTime() const;
241
242     void loadUnknownUserType();
243     void loadBrokenUserType();
244
245     void invalidDate() const;
246     void compareCustomTypes() const;
247     void timeToDateTime() const;
248     void copyingUserTypes() const;
249     void convertBoolToByteArray() const;
250     void convertBoolToByteArray_data() const;
251     void convertByteArrayToBool() const;
252     void convertByteArrayToBool_data() const;
253     void toIntFromQString() const;
254     void toIntFromDouble() const;
255     void setValue();
256
257     void numericalConvert();
258     void moreCustomTypes();
259     void variantInVariant();
260
261     void colorInteger();
262
263     void forwardDeclare();
264 };
265
266 Q_DECLARE_METATYPE(QDate)
267 Q_DECLARE_METATYPE(QTime)
268 Q_DECLARE_METATYPE(QDateTime)
269 Q_DECLARE_METATYPE(QVariant)
270
271 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
272 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
273
274 void tst_QVariant::constructor()
275 {
276     QVariant variant;
277     QVERIFY( !variant.isValid() );
278     QVERIFY( variant.isNull() );
279
280     QVariant var2(variant);
281     QVERIFY( !var2.isValid() );
282     QVERIFY( variant.isNull() );
283
284     QVariant varll(intMax1);
285     QVariant varll2(varll);
286     QCOMPARE(varll2, varll);
287
288     QVariant var3(QVariant::String);
289     QCOMPARE(var3.typeName(), "QString");
290     QVERIFY(var3.isNull());
291     QVERIFY(var3.isValid());
292
293     QVariant var4(QVariant::Invalid);
294     QCOMPARE(var4.type(), QVariant::Invalid);
295     QVERIFY(var4.isNull());
296     QVERIFY(!var4.isValid());
297
298     QVariant var5(QLatin1String("hallo"));
299     QCOMPARE(var5.type(), QVariant::String);
300     QCOMPARE(var5.typeName(), "QString");
301
302     QVariant var6(qlonglong(0));
303     QCOMPARE(var6.type(), QVariant::LongLong);
304     QCOMPARE(var6.typeName(), "qlonglong");
305
306     QVariant var7 = 5;
307     QVERIFY(var7.isValid());
308     QVERIFY(!var7.isNull());
309     QVariant var8;
310     var8.setValue<int>(5);
311     QVERIFY(var8.isValid());
312     QVERIFY(!var8.isNull());
313 }
314
315 void tst_QVariant::copy_constructor()
316 {
317     QVariant var7(QVariant::Int);
318     QVariant var8(var7);
319     QCOMPARE(var8.type(), QVariant::Int);
320     QVERIFY(var8.isNull());
321 }
322
323 void tst_QVariant::isNull()
324 {
325     QVariant var;
326     QVERIFY( var.isNull() );
327
328     QString str1;
329     QVariant var1( str1 );
330     QVERIFY( var1.isNull() );
331
332     QVariant var2( QString::null );
333     QVERIFY( var2.isNull() );
334
335     QVariant var3( QString( "blah" ) );
336     QVERIFY( !var3.isNull() );
337
338     QVariant var4( 0 );
339     QVERIFY( !var4.isNull() );
340
341     QVariant var5 = QString();
342     QVERIFY( var5.isNull() );
343
344     QVariant var6( QString( "blah" ) );
345     QVERIFY( !var6.isNull() );
346     var6 = QVariant();
347     QVERIFY( var6.isNull() );
348     var6.convert( QVariant::String );
349     QVERIFY( var6.isNull() );
350     QVariant varLL( (qlonglong)0 );
351     QVERIFY( !varLL.isNull() );
352     QVariant var7(QString::null);
353     QVERIFY(var7.isNull());
354 }
355
356 void tst_QVariant::swap()
357 {
358     QVariant v1 = 1, v2 = 2.0;
359     v1.swap(v2);
360     QCOMPARE(v1.type(),QVariant::Double);
361     QCOMPARE(v1.toDouble(),2.0);
362     QCOMPARE(v2.type(),QVariant::Int);
363     QCOMPARE(v2.toInt(),1);
364 }
365
366 void tst_QVariant::canConvert_data()
367 {
368     QTest::addColumn<QVariant>("val");
369     QTest::addColumn<bool>("BitArrayCast");
370     QTest::addColumn<bool>("BitmapCast");
371     QTest::addColumn<bool>("BoolCast");
372     QTest::addColumn<bool>("BrushCast");
373     QTest::addColumn<bool>("ByteArrayCast");
374     QTest::addColumn<bool>("ColorCast");
375     QTest::addColumn<bool>("CursorCast");
376     QTest::addColumn<bool>("DateCast");
377     QTest::addColumn<bool>("DateTimeCast");
378     QTest::addColumn<bool>("DoubleCast");
379     QTest::addColumn<bool>("FontCast");
380     QTest::addColumn<bool>("ImageCast");
381     QTest::addColumn<bool>("IntCast");
382     QTest::addColumn<bool>("InvalidCast");
383     QTest::addColumn<bool>("KeySequenceCast");
384     QTest::addColumn<bool>("ListCast");
385     QTest::addColumn<bool>("LongLongCast");
386     QTest::addColumn<bool>("MapCast");
387     QTest::addColumn<bool>("PaletteCast");
388     QTest::addColumn<bool>("PenCast");
389     QTest::addColumn<bool>("PixmapCast");
390     QTest::addColumn<bool>("PointCast");
391     QTest::addColumn<bool>("RectCast");
392     QTest::addColumn<bool>("RegionCast");
393     QTest::addColumn<bool>("SizeCast");
394     QTest::addColumn<bool>("SizePolicyCast");
395     QTest::addColumn<bool>("StringCast");
396     QTest::addColumn<bool>("StringListCast");
397     QTest::addColumn<bool>("TimeCast");
398     QTest::addColumn<bool>("UIntCast");
399     QTest::addColumn<bool>("ULongLongCast");
400
401
402 #ifdef Y
403 #undef Y
404 #endif
405 #ifdef N
406 #undef N
407 #endif
408 #define Y true
409 #define N false
410     //            bita bitm bool brsh byta col  curs date dt   dbl  font img  int  inv  kseq list ll   map  pal  pen  pix  pnt  rect reg  size sp   str  strl time uint ull
411
412
413     QVariant var(QBitArray(0));
414     QTest::newRow("BitArray")
415         << var << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
416     var = qVariantFromValue(QBitmap());
417     QTest::newRow("Bitmap")
418         << var << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
419     var = qVariantFromValue(QBrush());
420     QTest::newRow("Brush")
421         << var << N << N << N << Y << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
422     var = QVariant(QByteArray());
423     QTest::newRow("ByteArray")
424         << var << N << N << Y << N << Y << Y << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
425     var = qVariantFromValue(QColor());
426     QTest::newRow("Color")
427         << var << N << N << N << Y << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
428 #ifndef QT_NO_CURSOR
429     var = qVariantFromValue(QCursor());
430     QTest::newRow("Cursor")
431         << var << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
432 #endif
433     var = QVariant(QDate());
434     QTest::newRow("Date")
435         << var << N << N << N << N << N << N << N << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
436     var = QVariant(QDateTime());
437     QTest::newRow("DateTime")
438         << var << N << N << N << N << N << N << N << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N;
439     var = QVariant((double)0.1);
440     QTest::newRow("Double")
441         << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
442     var = QVariant(0.1f);
443     QTest::newRow("Float")
444         << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
445     var = qVariantFromValue(QFont());
446     QTest::newRow("Font")
447         << var << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
448     var = qVariantFromValue(QIcon());
449     QTest::newRow("Icon")
450         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
451     var = qVariantFromValue(QImage());
452     QTest::newRow("Image")
453         << var << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
454     var = QVariant((int)1);
455     QTest::newRow("Int")
456         << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
457     var = QVariant();
458     QTest::newRow("Invalid")
459         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
460     var = qVariantFromValue(QKeySequence());
461     QTest::newRow("KeySequence")
462         << var << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
463     var = QVariant(QList<QVariant>());
464     QTest::newRow("List")
465         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N;
466     var = QVariant((qlonglong)1);
467     QTest::newRow("LongLong")
468         << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
469     var = QVariant(QMap<QString,QVariant>());
470     QTest::newRow("Map")
471         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N;
472     var = qVariantFromValue(QPalette());
473     QTest::newRow("Palette")
474         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N;
475     var = qVariantFromValue(QPen());
476     QTest::newRow("Pen")
477         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N;
478     var = qVariantFromValue(QPixmap());
479     QTest::newRow("Pixmap")
480         << var << N << Y << N << Y << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
481     var = qVariantFromValue(QPolygon());
482     QTest::newRow("PointArray")
483         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
484     var = QVariant(QPoint());
485     QTest::newRow("Point")
486         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N;
487     var = QVariant(QRect());
488     QTest::newRow("Rect")
489         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N;
490     var = qVariantFromValue(QRegion());
491     QTest::newRow("Region")
492         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N;
493     var = QVariant(QSize());
494     QTest::newRow("Size")
495         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N;
496     var = qVariantFromValue(QSizePolicy());
497     QTest::newRow("SizePolicy")
498         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N;
499     var = QVariant(QString());
500     QTest::newRow("String")
501         << var << N << N << Y << N << Y << Y << N << Y << Y << Y << Y << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << Y << Y << Y << Y;
502    var = QVariant(QStringList("entry"));
503     QTest::newRow("StringList")
504         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << Y << Y << N << N << N;
505     var = QVariant(QTime());
506     QTest::newRow("Time")
507         << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N;
508     var = QVariant((uint)1);
509     QTest::newRow("UInt")
510         << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
511     var = QVariant((qulonglong)1);
512     QTest::newRow("ULongLong")
513         << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
514
515 #undef N
516 #undef Y
517 }
518
519 void tst_QVariant::canConvert()
520 {
521     QFETCH(QVariant, val);
522     QFETCH(bool, BitArrayCast);
523     QFETCH(bool, BitmapCast);
524     QFETCH(bool, BoolCast);
525     QFETCH(bool, BrushCast);
526     QFETCH(bool, ByteArrayCast);
527     QFETCH(bool, ColorCast);
528     QFETCH(bool, CursorCast);
529     QFETCH(bool, DateCast);
530     QFETCH(bool, DateTimeCast);
531     QFETCH(bool, DoubleCast);
532     QFETCH(bool, FontCast);
533     QFETCH(bool, ImageCast);
534     QFETCH(bool, IntCast);
535     QFETCH(bool, InvalidCast);
536     QFETCH(bool, KeySequenceCast);
537     QFETCH(bool, ListCast);
538     QFETCH(bool, LongLongCast);
539     QFETCH(bool, MapCast);
540     QFETCH(bool, PaletteCast);
541     QFETCH(bool, PenCast);
542     QFETCH(bool, PixmapCast);
543     QFETCH(bool, PointCast);
544     QFETCH(bool, RectCast);
545     QFETCH(bool, RegionCast);
546     QFETCH(bool, SizeCast);
547     QFETCH(bool, SizePolicyCast);
548     QFETCH(bool, StringCast);
549     QFETCH(bool, StringListCast);
550     QFETCH(bool, TimeCast);
551     QFETCH(bool, UIntCast);
552     QFETCH(bool, ULongLongCast);
553
554     QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
555     QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
556     QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
557     QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
558     QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
559     QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
560     QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
561     QCOMPARE(val.canConvert(QVariant::Date), DateCast);
562     QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
563     QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
564     QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
565     QCOMPARE(val.canConvert(QVariant::Font), FontCast);
566     QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
567     QCOMPARE(val.canConvert(QVariant::Int), IntCast);
568     QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
569     QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
570     QCOMPARE(val.canConvert(QVariant::List), ListCast);
571     QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
572     QCOMPARE(val.canConvert(QVariant::Map), MapCast);
573     QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
574     QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
575     QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
576     QCOMPARE(val.canConvert(QVariant::Point), PointCast);
577     QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
578     QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
579     QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
580     QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
581     QCOMPARE(val.canConvert(QVariant::String), StringCast);
582     QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
583     QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
584     QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
585     QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
586 }
587
588 void tst_QVariant::toInt_data()
589 {
590     QTest::addColumn<QVariant>("value");
591     QTest::addColumn<int>("result");
592     QTest::addColumn<bool>("valueOK");
593
594     QTest::newRow( "invalid" ) << QVariant()  << 0 << false;
595     QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
596     QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
597     QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
598     QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
599     QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
600     QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
601     QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
602     QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
603     QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
604     QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
605     QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
606     QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
607     QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
608     QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
609     QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
610     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
611     QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
612     QByteArray bytearray(4, ' ');
613     bytearray[0] = 'T';
614     bytearray[1] = 'e';
615     bytearray[2] = 's';
616     bytearray[3] = 't';
617     QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
618     bytearray[0] = '4';
619     bytearray[1] = '5';
620     bytearray[2] = '0';
621     bytearray[3] = '0';
622     QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
623 }
624
625 void tst_QVariant::toInt()
626 {
627     QFETCH( QVariant, value );
628     QFETCH( int, result );
629     QFETCH( bool, valueOK );
630     QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
631     bool ok;
632     int i = value.toInt( &ok );
633     QCOMPARE( i, result );
634     QVERIFY( ok == valueOK );
635 }
636
637 void tst_QVariant::toUInt_data()
638 {
639     QTest::addColumn<QVariant>("value");
640     QTest::addColumn<uint>("result");
641     QTest::addColumn<bool>("valueOK");
642
643     QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
644     QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
645     QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
646     QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
647     QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
648     QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
649     QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
650     QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
651     QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
652     QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
653     QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
654     QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
655     QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
656     QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
657     QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
658     QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
659     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
660     QByteArray bytearray(4, ' ');
661     bytearray[0] = '4';
662     bytearray[1] = '3';
663     bytearray[2] = '2';
664     bytearray[3] = '1';
665     QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
666 }
667
668 void tst_QVariant::toUInt()
669 {
670     QFETCH( QVariant, value );
671     QFETCH( uint, result );
672     QFETCH( bool, valueOK );
673     QVERIFY( value.isValid() );
674     QVERIFY( value.canConvert( QVariant::UInt ) );
675
676     bool ok;
677     uint i = value.toUInt( &ok );
678     QVERIFY( ok == valueOK );
679     QCOMPARE( i, result );
680 }
681
682
683 void tst_QVariant::toSize_data()
684 {
685     QTest::addColumn<QVariant>("value");
686     QTest::addColumn<QSize>("result");
687     QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
688     QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
689     QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
690     QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
691 }
692
693 void tst_QVariant::toSize()
694 {
695     QFETCH( QVariant, value );
696     QFETCH( QSize, result );
697     QVERIFY( value.isValid() );
698     QVERIFY( value.canConvert( QVariant::Size ) );
699
700     QSize i = value.toSize();
701     QCOMPARE( i, result );
702 }
703
704 void tst_QVariant::toSizeF_data()
705 {
706     QTest::addColumn<QVariant>("value");
707     QTest::addColumn<QSizeF>("result");
708     QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
709     QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
710      QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
711     QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
712 }
713
714 void tst_QVariant::toSizeF()
715 {
716     QFETCH( QVariant, value );
717     QFETCH( QSizeF, result );
718     QVERIFY( value.isValid() );
719     QVERIFY( value.canConvert( QVariant::SizeF ) );
720
721     QSizeF i = value.toSizeF();
722     QCOMPARE( i, result );
723 }
724
725 void tst_QVariant::toLine_data()
726 {
727     QTest::addColumn<QVariant>("value");
728     QTest::addColumn<QLine>("result");
729     QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
730     QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
731     QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
732     QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
733 }
734
735 void tst_QVariant::toLine()
736 {
737     QFETCH( QVariant, value );
738     QFETCH( QLine, result );
739     QVERIFY( value.isValid() );
740     QVERIFY( value.canConvert( QVariant::Line ) );
741
742     QLine i = value.toLine();
743     QCOMPARE( i, result );
744 }
745
746 void tst_QVariant::toLineF_data()
747 {
748     QTest::addColumn<QVariant>("value");
749     QTest::addColumn<QLineF>("result");
750     QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
751     QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
752     QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
753     QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
754 }
755
756 void tst_QVariant::toLineF()
757 {
758     QFETCH( QVariant, value );
759     QFETCH( QLineF, result );
760     QVERIFY( value.isValid() );
761     QVERIFY( value.canConvert( QVariant::LineF ) );
762
763     QLineF i = value.toLineF();
764     QCOMPARE( i, result );
765 }
766
767 void tst_QVariant::toPoint_data()
768 {
769     QTest::addColumn<QVariant>("value");
770     QTest::addColumn<QPoint>("result");
771     QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
772     QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
773     QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
774     QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
775     QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
776 }
777
778 void tst_QVariant::toPoint()
779 {
780     QFETCH( QVariant, value );
781     QFETCH( QPoint, result );
782     QVERIFY( value.isValid() );
783     QVERIFY( value.canConvert( QVariant::Point ) );
784     QPoint i = value.toPoint();
785     QCOMPARE( i, result );
786 }
787
788 void tst_QVariant::toRect_data()
789 {
790     QTest::addColumn<QVariant>("value");
791     QTest::addColumn<QRect>("result");
792     QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
793     QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
794     QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
795     QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
796     QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
797 }
798
799 void tst_QVariant::toRect()
800 {
801     QFETCH( QVariant, value );
802     QFETCH( QRect, result );
803     QVERIFY( value.isValid() );
804     QVERIFY( value.canConvert( QVariant::Rect ) );
805     QRect i = value.toRect();
806     QCOMPARE( i, result );
807 }
808
809 void tst_QVariant::toChar_data()
810 {
811     QTest::addColumn<QVariant>("value");
812     QTest::addColumn<QChar>("result");
813     QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
814     QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
815 }
816
817 void tst_QVariant::toChar()
818 {
819     QFETCH( QVariant, value );
820     QFETCH( QChar, result );
821     QVERIFY( value.isValid() );
822     QVERIFY( value.canConvert( QVariant::Char ) );
823
824     QChar i = value.toChar();
825     QCOMPARE( i, result );
826 }
827
828 void tst_QVariant::toBool_data()
829 {
830     QTest::addColumn<QVariant>("value");
831     QTest::addColumn<bool>("result");
832
833     QTest::newRow( "int0" ) << QVariant( 0 ) << false;
834     QTest::newRow( "int1" ) << QVariant( 123 ) << true;
835     QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
836     QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
837     QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
838     QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
839     QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
840     QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
841     QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
842     QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
843     QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
844     QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
845     QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
846     QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
847     QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
848     QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
849     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
850     QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
851 }
852
853 void tst_QVariant::toBool()
854 {
855     QFETCH( QVariant, value );
856     QFETCH( bool, result );
857     QVERIFY( value.isValid() );
858     QVERIFY( value.canConvert( QVariant::Bool ) );
859
860     bool i = value.toBool();
861     QCOMPARE( i, result );
862 }
863
864 void tst_QVariant::toPointF_data()
865 {
866     QTest::addColumn<QVariant>("value");
867     QTest::addColumn<QPointF>("result");
868
869     QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
870 }
871
872 void tst_QVariant::toPointF()
873 {
874     QFETCH( QVariant, value );
875     QFETCH( QPointF, result );
876     QVERIFY( value.isValid() );
877     QVERIFY( value.canConvert( QVariant::PointF ) );
878     QPointF d = value.toPointF();
879     QCOMPARE( d, result );
880 }
881
882 void tst_QVariant::toRectF_data()
883 {
884     QTest::addColumn<QVariant>("value");
885     QTest::addColumn<QRectF>("result");
886
887     QRect r( 1, 9, 8, 4 );
888     QRectF rf( 1.0, 9.0, 8.0, 4.0 );
889     QTest::newRow( "QRect" ) << QVariant( r ) << rf;
890 }
891
892 void tst_QVariant::toRectF()
893 {
894     QFETCH( QVariant, value );
895     QFETCH( QRectF, result );
896     QVERIFY( value.isValid() );
897     QVERIFY( value.canConvert( QVariant::RectF ) );
898     QRectF d = value.toRectF();
899     QCOMPARE( d, result );
900 }
901
902 void tst_QVariant::toColor_data()
903 {
904     QTest::addColumn<QVariant>("value");
905     QTest::addColumn<QColor>("result");
906
907     QColor c("red");
908     QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
909     QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
910 }
911
912 void tst_QVariant::toColor()
913 {
914     QFETCH( QVariant, value );
915     QFETCH( QColor, result );
916     QVERIFY( value.isValid() );
917     QVERIFY( value.canConvert( QVariant::Color ) );
918     QColor d = qVariantValue<QColor>(value);
919     QCOMPARE( d, result );
920 }
921
922 void tst_QVariant::toPixmap_data()
923 {
924     QTest::addColumn<QVariant>("value");
925     QTest::addColumn<QPixmap>("result");
926
927     QPixmap pm(30, 30);
928     pm.fill(Qt::red);
929     QTest::newRow( "image" ) << QVariant( pm ) << pm;
930
931     QBitmap bm(30, 30);
932     bm.fill(Qt::color1);
933     QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
934 }
935
936 void tst_QVariant::toPixmap()
937 {
938     QFETCH( QVariant, value );
939     QFETCH( QPixmap, result );
940     QVERIFY( value.isValid() );
941     QVERIFY( value.canConvert( QVariant::Pixmap ) );
942     QPixmap d = qVariantValue<QPixmap>(value);
943     QCOMPARE( d, result );
944 }
945
946 void tst_QVariant::toImage_data()
947 {
948     QTest::addColumn<QVariant>("value");
949     QTest::addColumn<QImage>("result");
950
951     QImage im(30, 30, QImage::Format_ARGB32);
952     im.fill(0x7fff0000);
953     QTest::newRow( "image" ) << QVariant( im ) << im;
954 }
955
956 void tst_QVariant::toImage()
957 {
958     QFETCH( QVariant, value );
959     QFETCH( QImage, result );
960     QVERIFY( value.isValid() );
961     QVERIFY( value.canConvert( QVariant::Image ) );
962     QImage d = qVariantValue<QImage>(value);
963     QCOMPARE( d, result );
964 }
965
966 void tst_QVariant::toBrush_data()
967 {
968     QTest::addColumn<QVariant>("value");
969     QTest::addColumn<QBrush>("result");
970
971     QColor c(Qt::red);
972     QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
973     QPixmap pm(30, 30);
974     pm.fill(c);
975     QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
976 }
977
978 void tst_QVariant::toBrush()
979 {
980     QFETCH( QVariant, value );
981     QFETCH( QBrush, result );
982     QVERIFY( value.isValid() );
983     QVERIFY( value.canConvert( QVariant::Brush ) );
984     QBrush d = qVariantValue<QBrush>(value);
985     QCOMPARE( d, result );
986 }
987
988 void tst_QVariant::toFont_data()
989 {
990     QTest::addColumn<QVariant>("value");
991     QTest::addColumn<QFont>("result");
992
993     QFont f("times",12,-1,false);
994     QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
995 }
996
997 void tst_QVariant::toFont()
998 {
999     QFETCH( QVariant, value );
1000     QFETCH( QFont, result );
1001     QVERIFY( value.isValid() );
1002     QVERIFY( value.canConvert( QVariant::Font ) );
1003     QFont d = qVariantValue<QFont>(value);
1004     QCOMPARE( d, result );
1005 }
1006
1007 void tst_QVariant::toKeySequence_data()
1008 {
1009     QTest::addColumn<QVariant>("value");
1010     QTest::addColumn<QKeySequence>("result");
1011
1012
1013     QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1014
1015
1016     QTest::newRow( "qstring" )
1017         << QVariant( QString( "Ctrl+A" ) )
1018         << QKeySequence( Qt::CTRL + Qt::Key_A );
1019 }
1020
1021 void tst_QVariant::toKeySequence()
1022 {
1023     QFETCH( QVariant, value );
1024     QFETCH( QKeySequence, result );
1025     QVERIFY( value.isValid() );
1026     QVERIFY( value.canConvert( QVariant::KeySequence ) );
1027     QKeySequence d = qVariantValue<QKeySequence>(value);
1028     QCOMPARE( d, result );
1029 }
1030
1031 void tst_QVariant::toDouble_data()
1032 {
1033     QTest::addColumn<QVariant>("value");
1034     QTest::addColumn<double>("result");
1035     QTest::addColumn<bool>("valueOK");
1036
1037     QByteArray bytearray(4, ' ');
1038     bytearray[0] = '3';
1039     bytearray[1] = '2';
1040     bytearray[2] = '.';
1041     bytearray[3] = '1';
1042     QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1043 }
1044
1045 void tst_QVariant::toDouble()
1046 {
1047     QFETCH( QVariant, value );
1048     QFETCH( double, result );
1049     QFETCH( bool, valueOK );
1050     QVERIFY( value.isValid() );
1051     QVERIFY( value.canConvert( QVariant::Double ) );
1052     bool ok;
1053     double d = value.toDouble( &ok );
1054     QCOMPARE( d, result );
1055     QVERIFY( ok == valueOK );
1056 }
1057
1058 void tst_QVariant::toLongLong_data()
1059 {
1060     QTest::addColumn<QVariant>("value");
1061     QTest::addColumn<qlonglong>("result");
1062     QTest::addColumn<bool>("valueOK");
1063
1064     QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1065     QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1066     QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1067     QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1068     QTest::newRow( "int-string" ) << QVariant( QString("123") )
1069                                << (qlonglong)123 << true;
1070     QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1071                            << false;
1072     QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1073     QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1074     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1075     QByteArray bytearray(4, ' ');
1076     bytearray[0] = '3';
1077     bytearray[1] = '2';
1078     bytearray[2] = '0';
1079     bytearray[3] = '0';
1080     QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1081 }
1082
1083 void tst_QVariant::toLongLong()
1084 {
1085     QFETCH( QVariant, value );
1086     QFETCH( qlonglong, result );
1087     QFETCH( bool, valueOK );
1088     QVERIFY( value.isValid() );
1089     QVERIFY( value.canConvert( QVariant::LongLong ) );
1090     bool ok;
1091     qlonglong ll = value.toLongLong( &ok );
1092     QCOMPARE( ll, result );
1093     QVERIFY( ok == valueOK );
1094 }
1095
1096 void tst_QVariant::toULongLong_data()
1097 {
1098     QTest::addColumn<QVariant>("value");
1099     QTest::addColumn<qulonglong>("result");
1100     QTest::addColumn<bool>("valueOK");
1101
1102     QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1103     QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1104     QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1105     QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1106     QTest::newRow( "int-string" ) << QVariant( QString("123") )
1107                                << (qulonglong)123 << true;
1108     QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1109                            << false;
1110     QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1111                                      << Q_UINT64_C(18446744073709551615)
1112                                      << true;
1113     QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1114                                      << Q_UINT64_C(18446744073709551615)
1115                                      << true;
1116     QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1117     QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1118     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1119     QByteArray bytearray(4, ' ');
1120     bytearray[0] = '3';
1121     bytearray[1] = '2';
1122     bytearray[2] = '0';
1123     bytearray[3] = '1';
1124     QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1125 }
1126
1127 void tst_QVariant::toULongLong()
1128 {
1129     QFETCH( QVariant, value );
1130     QFETCH( qulonglong, result );
1131     QFETCH( bool, valueOK );
1132     QVERIFY( value.isValid() );
1133     QVERIFY( value.canConvert( QVariant::ULongLong ) );
1134     bool ok;
1135     qulonglong ll = value.toULongLong( &ok );
1136     QCOMPARE( ll, result );
1137     QVERIFY( ok == valueOK );
1138 }
1139
1140 void tst_QVariant::toByteArray_data()
1141 {
1142     QTest::addColumn<QVariant>("value");
1143     QTest::addColumn<QByteArray>("result");
1144
1145     QByteArray ba(5, ' ');
1146     ba[0] = 'T';
1147     ba[1] = 'e';
1148     ba[2] = 's';
1149     ba[3] = 't';
1150     ba[4] = '\0';
1151
1152     QByteArray variantBa = ba;
1153
1154     QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1155     QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1156     QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1157     QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1158     QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1159     QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1160     QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1161 }
1162
1163 void tst_QVariant::toByteArray()
1164 {
1165     QFETCH( QVariant, value );
1166     QFETCH( QByteArray, result );
1167     QVERIFY( value.isValid() );
1168     QVERIFY( value.canConvert( QVariant::ByteArray ) );
1169     QByteArray ba = value.toByteArray();
1170     QCOMPARE( ba, result );
1171 }
1172
1173 void tst_QVariant::toString_data()
1174 {
1175     QTest::addColumn<QVariant>("value");
1176     QTest::addColumn<QString>("result");
1177
1178     QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1179     QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1180     QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1181     QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1182     QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1183     QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1184     QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1185     QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1186     QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1187     QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1188     QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1189     QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1190 #ifndef Q_OS_MAC
1191         << QString( "Ctrl+A" );
1192 #else
1193         << QString(QChar(0x2318)) + "A";
1194 #endif
1195
1196     QFont font( "times", 12 );
1197     QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1198     QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1199     QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1200         QString( "123456789012" );
1201 }
1202
1203 void tst_QVariant::toString()
1204 {
1205     QFETCH( QVariant, value );
1206     QFETCH( QString, result );
1207     QVERIFY( value.isValid() );
1208     QVERIFY( value.canConvert( QVariant::String ) );
1209     QString str = value.toString();
1210     QCOMPARE( str, result );
1211 }
1212
1213 void tst_QVariant::toDate_data()
1214 {
1215     QTest::addColumn<QVariant>("value");
1216     QTest::addColumn<QDate>("result");
1217
1218     QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1219     QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1220     QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1221 }
1222
1223 void tst_QVariant::toDate()
1224 {
1225     QFETCH( QVariant, value );
1226     QFETCH( QDate, result );
1227     QVERIFY( value.isValid() );
1228     QVERIFY( value.canConvert( QVariant::Date ) );
1229     QCOMPARE( value.toDate(), result );
1230 }
1231
1232 void tst_QVariant::toTime_data()
1233 {
1234     QTest::addColumn<QVariant>("value");
1235     QTest::addColumn<QTime>("result");
1236
1237     QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1238     QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1239     QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1240 }
1241
1242 void tst_QVariant::toTime()
1243 {
1244     QFETCH( QVariant, value );
1245     QFETCH( QTime, result );
1246     QVERIFY( value.isValid() );
1247     QVERIFY( value.canConvert( QVariant::Time ) );
1248     QCOMPARE( value.toTime(), result );
1249 }
1250
1251 void tst_QVariant::toDateTime_data()
1252 {
1253     QTest::addColumn<QVariant>("value");
1254     QTest::addColumn<QDateTime>("result");
1255
1256     QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1257         << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1258     QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1259     QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1260 }
1261
1262 void tst_QVariant::toDateTime()
1263 {
1264     QFETCH( QVariant, value );
1265     QFETCH( QDateTime, result );
1266     QVERIFY( value.isValid() );
1267     QVERIFY( value.canConvert( QVariant::DateTime ) );
1268     QCOMPARE( value.toDateTime(), result );
1269 }
1270
1271 void tst_QVariant::toLocale()
1272 {
1273     QVariant variant;
1274     QLocale loc = variant.toLocale();
1275     variant = QLocale::system();
1276     loc = variant.toLocale();
1277 }
1278
1279 void tst_QVariant::toRegExp()
1280 {
1281     QVariant variant;
1282     QRegExp rx = variant.toRegExp();
1283     variant = QRegExp("foo");
1284     rx = variant.toRegExp();
1285 }
1286
1287 void tst_QVariant::matrix()
1288 {
1289     QVariant variant;
1290     QMatrix matrix = qVariantValue<QMatrix>(variant);
1291     QVERIFY(matrix.isIdentity());
1292     qVariantSetValue(variant, QMatrix().rotate(90));
1293     QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1294
1295     void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1296     QVERIFY(mmatrix);
1297     QMetaType::destroy(QVariant::Matrix, mmatrix);
1298 }
1299
1300 void tst_QVariant::matrix4x4()
1301 {
1302     QVariant variant;
1303     QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1304     QVERIFY(matrix.isIdentity());
1305     QMatrix4x4 m;
1306     m.scale(2.0f);
1307     qVariantSetValue(variant, m);
1308     QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1309
1310     void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1311     QVERIFY(mmatrix);
1312     QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1313 }
1314
1315 void tst_QVariant::transform()
1316 {
1317     QVariant variant;
1318     QTransform matrix = qVariantValue<QTransform>(variant);
1319     QVERIFY(matrix.isIdentity());
1320     qVariantSetValue(variant, QTransform().rotate(90));
1321     QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1322
1323     void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1324     QVERIFY(mmatrix);
1325     QMetaType::destroy(QVariant::Transform, mmatrix);
1326 }
1327
1328
1329 void tst_QVariant::vector2D()
1330 {
1331     QVariant variant;
1332     QVector2D vector = qVariantValue<QVector2D>(variant);
1333     QVERIFY(vector.isNull());
1334     qVariantSetValue(variant, QVector2D(0.1, 0.2));
1335     QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1336
1337     void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1338     QVERIFY(pvector);
1339     QMetaType::destroy(QVariant::Vector2D, pvector);
1340 }
1341
1342 void tst_QVariant::vector3D()
1343 {
1344     QVariant variant;
1345     QVector3D vector = qVariantValue<QVector3D>(variant);
1346     QVERIFY(vector.isNull());
1347     qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1348     QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1349
1350     void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1351     QVERIFY(pvector);
1352     QMetaType::destroy(QVariant::Vector3D, pvector);
1353 }
1354
1355 void tst_QVariant::vector4D()
1356 {
1357     QVariant variant;
1358     QVector4D vector = qVariantValue<QVector4D>(variant);
1359     QVERIFY(vector.isNull());
1360     qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1361     QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1362
1363     void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1364     QVERIFY(pvector);
1365     QMetaType::destroy(QVariant::Vector4D, pvector);
1366 }
1367
1368 void tst_QVariant::quaternion()
1369 {
1370     QVariant variant;
1371     QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1372     QVERIFY(quaternion.isIdentity());
1373     qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1374     QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1375
1376     void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1377     QVERIFY(pquaternion);
1378     QMetaType::destroy(QVariant::Quaternion, pquaternion);
1379 }
1380
1381 void tst_QVariant::writeToReadFromDataStream_data()
1382 {
1383
1384     QTest::addColumn<QVariant>("writeVariant");
1385     QTest::addColumn<bool>("isNull");
1386     {
1387         typedef QList<QVariant> variantsList;
1388         variantsList valuelist;
1389         valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1390         QVariant var(valuelist);
1391         QTest::newRow( "list_valid" ) << var << false;
1392     }
1393
1394     QTest::newRow( "invalid" ) << QVariant() << true;
1395     QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1396     QBitArray bitarray( 3 );
1397     bitarray[0] = 0;
1398     bitarray[1] = 1;
1399     bitarray[2] = 0;
1400     QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1401     QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1402     QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1403     QByteArray bytearray(5, ' ');
1404     bytearray[0] = 'T';
1405     bytearray[1] = 'e';
1406     bytearray[2] = 's';
1407     bytearray[3] = 't';
1408     bytearray[4] = '\0';
1409     QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1410     QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1411     QBitmap bitmap( 10, 10 );
1412     bitmap.fill( Qt::red );
1413     QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1414     QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1415     QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1416 #ifndef QT_NO_CURSOR
1417     QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1418 #endif
1419     QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1420     QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1421     QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1422     QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1423     QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1424     QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1425     QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1426     QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1427     QPixmap pixmap( 10, 10 );
1428     pixmap.fill( Qt::red );
1429     QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1430 //    QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1431     QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1432     QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1433     QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1434     typedef QMap<QString, QVariant> variantsMap;
1435     variantsMap vMap;
1436     vMap.insert( "int", QVariant( 1 ) );
1437     vMap.insert( "string", QVariant( QString("Two") ) );
1438     vMap.insert( "double", QVariant( 3.45 ) );
1439     vMap.insert( "float", QVariant( 3.45f ) );
1440     QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1441     QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1442     QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1443     QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1444     QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1445     QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1446     QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1447     QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1448     QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1449     QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1450     QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1451     QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1452     QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1453     QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1454     QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1455     QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1456     QStringList stringlist;
1457     stringlist << "One" << "Two" << "Three";
1458     QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1459     QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1460     QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1461     QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1462     QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1463     QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1464     QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1465     QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1466
1467     // types known to QMetaType, but not part of QVariant::Type
1468     QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1469     long longInt = -1l;
1470     QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1471     QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1472     short shortInt = 1;
1473     QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1474     QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1475     char ch = 'c';
1476     QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1477     QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1478     ulong ulongInt = 1ul;
1479     QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1480     QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1481     ushort ushortInt = 1u;
1482     QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1483     QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1484     uchar uch = 0xf0;
1485     QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1486     QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1487     float f = 1.234f;
1488     QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1489 }
1490
1491 void tst_QVariant::writeToReadFromDataStream()
1492 {
1493     QFETCH( QVariant, writeVariant );
1494     QFETCH( bool, isNull );
1495     QByteArray data;
1496
1497     QDataStream writeStream( &data, QIODevice::WriteOnly );
1498     writeStream << writeVariant;
1499
1500     QVariant readVariant;
1501     QDataStream readStream( &data, QIODevice::ReadOnly );
1502     readStream >> readVariant;
1503     QVERIFY( readVariant.isNull() == isNull );
1504     // Best way to confirm the readVariant contains the same data?
1505     // Since only a few won't match since the serial numbers are different
1506     // I won't bother adding another bool in the data test.
1507     QVariant::Type writeType = writeVariant.type();
1508     if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1509         && writeType != QVariant::Image) {
1510         switch (writeType) {
1511         default:
1512             QCOMPARE( readVariant, writeVariant );
1513             break;
1514
1515         // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1516         case QMetaType::Long:
1517             QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1518             break;
1519         case QMetaType::ULong:
1520             QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1521             break;
1522         case QMetaType::Short:
1523             QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1524             break;
1525         case QMetaType::UShort:
1526             QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1527             break;
1528         case QMetaType::Char:
1529             QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1530             break;
1531         case QMetaType::UChar:
1532             QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1533             break;
1534         case QMetaType::Float:
1535             {
1536                 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1537                 float readFloat = qvariant_cast<float>(readVariant);
1538                 float writtenFloat = qvariant_cast<float>(writeVariant);
1539                 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1540                 if (!qIsNaN(readFloat))
1541                     QVERIFY(readFloat == writtenFloat);
1542             }
1543             break;
1544         }
1545     }
1546 }
1547
1548 void tst_QVariant::writeToReadFromOldDataStream()
1549 {
1550     QVariant writeVariant = QString("hello");
1551     QByteArray data;
1552
1553     QDataStream writeStream(&data, QIODevice::WriteOnly);
1554     writeStream.setVersion(QDataStream::Qt_2_1);
1555     writeStream << writeVariant;
1556
1557     QVariant readVariant;
1558     QDataStream readStream(&data, QIODevice::ReadOnly);
1559     readStream.setVersion(QDataStream::Qt_2_1);
1560     readStream >> readVariant;
1561
1562     QCOMPARE(writeVariant.userType(), readVariant.userType());
1563     QCOMPARE(writeVariant, readVariant);
1564 }
1565
1566 void tst_QVariant::checkDataStream()
1567 {
1568     const QByteArray settingsHex("0000002effffffffff");
1569     const QByteArray settings = QByteArray::fromHex(settingsHex);
1570     QDataStream in(settings);
1571     QVariant v;
1572     in >> v;
1573     // the line below has been left out for now since the data stream
1574     // is not necessarily considered corrupt when an invalid QVariant is
1575     // constructed. However, it might be worth considering changing that behavior
1576     // in the future.
1577 //    QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1578     QCOMPARE(v.type(), QVariant::Invalid);
1579 }
1580
1581 void tst_QVariant::operator_eq_eq_data()
1582 {
1583     QTest::addColumn<QVariant>("left");
1584     QTest::addColumn<QVariant>("right");
1585     QTest::addColumn<bool>("equal"); // left == right ?
1586
1587     QVariant inv;
1588     QVariant i0( int(0) );
1589     QVariant i1( int(1) );
1590     // Invalid
1591     QTest::newRow( "invinv" ) << inv << inv << true;
1592     // Int
1593     QTest::newRow( "int1int1" ) << i1 << i1 << true;
1594     QTest::newRow( "int1int0" ) << i1 << i0 << false;
1595     QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1596
1597     // LongLong and ULongLong
1598     QVariant ll1( (qlonglong)1 );
1599     QVariant lln2( (qlonglong)-2 );
1600     QVariant ull1( (qulonglong)1 );
1601     QVariant ull3( (qulonglong)3 );
1602     QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1603     QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1604     QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1605     QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1606     QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1607     QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1608
1609     QVariant mInt(-42);
1610     QVariant mIntString(QByteArray("-42"));
1611     QVariant mIntQString(QString("-42"));
1612
1613     QVariant mUInt(42u);
1614     QVariant mUIntString(QByteArray("42"));
1615     QVariant mUIntQString(QString("42"));
1616
1617     QVariant mDouble(42.11);
1618     QVariant mDoubleString(QByteArray("42.11"));
1619     QVariant mDoubleQString(QString("42.11"));
1620
1621     QVariant mFloat(42.11f);
1622     QVariant mFloatString(QByteArray("42.11"));
1623     QVariant mFloatQString(QString("42.11"));
1624
1625     QVariant mLongLong((qlonglong)-42);
1626     QVariant mLongLongString(QByteArray("-42"));
1627     QVariant mLongLongQString(QString("-42"));
1628
1629     QVariant mULongLong((qulonglong)42);
1630     QVariant mULongLongString(QByteArray("42"));
1631     QVariant mULongLongQString(QString("42"));
1632
1633     QVariant mBool(false);
1634     QVariant mBoolString(QByteArray("false"));
1635     QVariant mBoolQString(QString("false"));
1636
1637     QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1638     QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1639     QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1640     QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1641     QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1642     QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1643
1644     QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1645     QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1646     QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1647     QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1648
1649     QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1650     QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1651     QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1652     QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1653
1654     QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1655     QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1656     QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1657     QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1658
1659     QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1660     QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1661     QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1662     QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1663
1664     QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1665     QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1666     QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1667     QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1668
1669     QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1670     QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1671     QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1672     QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1673
1674     QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1675     QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1676     QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1677     QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1678
1679     QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1680     QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1681     // ### many other combinations missing
1682
1683     {
1684         QMap<QString, QVariant> map1;
1685         map1.insert( "X", 1 );
1686
1687         QMap<QString, QVariant> map2;
1688         map2.insert( "Y", 1 );
1689
1690         QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1691
1692     }
1693
1694     {
1695         QMap<QString, QVariant> map1;
1696         map1.insert( "X", 1 );
1697
1698         QMap<QString, QVariant> map2;
1699         map2.insert( "X", 1 );
1700
1701         QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1702     }
1703
1704     {
1705         QMap<QString, QVariant> map1;
1706         map1.insert( "X", 1 );
1707
1708         QMap<QString, QVariant> map2;
1709
1710         QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1711     }
1712
1713     {
1714         QMap<QString, QVariant> map1;
1715
1716         QMap<QString, QVariant> map2;
1717         map2.insert( "X", 1 );
1718
1719         QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1720     }
1721
1722     {
1723         QMap<QString, QVariant> map1;
1724         map1.insert( "X", 1 );
1725         map1.insert( "Y", 1 );
1726
1727         QMap<QString, QVariant> map2;
1728         map2.insert( "X", 1 );
1729
1730         QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1731     }
1732
1733     {
1734         QMap<QString, QVariant> map1;
1735         map1.insert( "X", 1 );
1736
1737         QMap<QString, QVariant> map2;
1738         map2.insert( "X", 1 );
1739         map2.insert( "Y", 1 );
1740
1741         QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1742     }
1743
1744     // same thing with hash
1745     {
1746         QHash<QString, QVariant> hash1;
1747         hash1.insert( "X", 1 );
1748
1749         QHash<QString, QVariant> hash2;
1750         hash2.insert( "Y", 1 );
1751
1752         QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1753
1754     }
1755
1756     {
1757         QHash<QString, QVariant> hash1;
1758         hash1.insert( "X", 1 );
1759
1760         QHash<QString, QVariant> hash2;
1761         hash2.insert( "X", 1 );
1762
1763         QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1764     }
1765
1766     {
1767         QHash<QString, QVariant> hash1;
1768         hash1.insert( "X", 1 );
1769
1770         QHash<QString, QVariant> hash2;
1771
1772         QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1773     }
1774
1775     {
1776         QHash<QString, QVariant> hash1;
1777
1778         QHash<QString, QVariant> hash2;
1779         hash2.insert( "X", 1 );
1780
1781         QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1782     }
1783
1784     {
1785         QHash<QString, QVariant> hash1;
1786         hash1.insert( "X", 1 );
1787         hash1.insert( "Y", 1 );
1788
1789         QHash<QString, QVariant> hash2;
1790         hash2.insert( "X", 1 );
1791
1792         QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1793     }
1794
1795     {
1796         QHash<QString, QVariant> hash1;
1797         hash1.insert( "X", 1 );
1798
1799         QHash<QString, QVariant> hash2;
1800         hash2.insert( "X", 1 );
1801         hash2.insert( "Y", 1 );
1802
1803         QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1804     }
1805
1806     QTest::newRow( "UserType" ) << QVariant(QVariant::UserType) << QVariant(QVariant::UserType) << true;
1807     QVariant mUserType(QVariant::UserType);
1808     QTest::newRow( "Shared UserType" ) << mUserType << mUserType << true;
1809 }
1810
1811 void tst_QVariant::operator_eq_eq()
1812 {
1813     QFETCH( QVariant, left );
1814     QFETCH( QVariant, right );
1815     QFETCH( bool, equal );
1816     QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1817     QCOMPARE( left == right, equal );
1818 }
1819
1820 void tst_QVariant::operator_eq_eq_rhs()
1821 {
1822     QVariant v = 42;
1823
1824     QVERIFY(v == 42);
1825     QVERIFY(42 == v);
1826
1827 #if 0
1828     /* This should _not_ compile */
1829     QStringList list;
1830     QDateTime dt;
1831
1832     QVERIFY(dt == list);
1833 #endif
1834 }
1835
1836 void tst_QVariant::typeName_data()
1837 {
1838     QTest::addColumn<int>("type");
1839     QTest::addColumn<QByteArray>("res");
1840     QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1841     QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1842     QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1843     QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1844     QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1845     QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1846     QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1847     QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1848     QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1849     QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1850     QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1851     QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1852     QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1853     QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1854     QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1855     QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1856     QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1857     QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1858     QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1859     QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1860     QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1861     QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1862     QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1863     QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1864     QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1865     QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1866     QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1867     QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1868     QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1869     QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1870     QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1871     QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1872     QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1873     QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1874     QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1875     QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1876     QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1877     QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1878     QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1879     QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1880     QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1881     QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1882     QTest::newRow("42") << int(QVariant::UserType) << QByteArray("UserType");
1883     QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1884     QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1885     QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1886     QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1887     QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1888     QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1889     QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1890     QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1891 }
1892
1893 void tst_QVariant::typeName()
1894 {
1895     QFETCH( int, type );
1896     QFETCH( QByteArray, res );
1897     QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1898             QString::fromLatin1(res.constData()));
1899 }
1900
1901 // test nameToType as well
1902 void tst_QVariant::typeToName()
1903 {
1904     QVariant v;
1905     QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1906     // assumes that QVariant::Type contains consecutive values
1907
1908     int max = QVariant::LastGuiType;
1909     for ( int t = 1; t <= max; t++ ) {
1910         const char *n = QVariant::typeToName( (QVariant::Type)t );
1911         if (n)
1912             QCOMPARE( int(QVariant::nameToType( n )), t );
1913
1914     }
1915     QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1916     // not documented but we return 0 if the type is out of range
1917     // by testing this we catch cases where QVariant is extended
1918     // but type_map is not updated accordingly
1919     QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1920     // invalid type names
1921     QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
1922     QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
1923     QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
1924     QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Icon);
1925 }
1926
1927 void tst_QVariant::streamInvalidVariant()
1928 {
1929     int writeX = 1;
1930     int writeY = 2;
1931     int readX;
1932     int readY;
1933     QVariant writeVariant;
1934     QVariant readVariant;
1935
1936     QVERIFY( writeVariant.type() == QVariant::Invalid );
1937
1938     QByteArray data;
1939     QDataStream writeStream( &data, QIODevice::WriteOnly );
1940     writeStream << writeX << writeVariant << writeY;
1941
1942     QDataStream readStream( &data, QIODevice::ReadOnly );
1943     readStream >> readX >> readVariant >> readY;
1944
1945     QVERIFY( readX == writeX );
1946     // Two invalid QVariant's aren't necessarily the same, so == will
1947     // return false if one is invalid, so check the type() instead
1948     QVERIFY( readVariant.type() == QVariant::Invalid );
1949     QVERIFY( readY == writeY );
1950 }
1951
1952 static int instanceCount = 0;
1953
1954 struct MyType
1955 {
1956     MyType(int n = 0, const char *t=0): number(n), text(t)
1957     {
1958         ++instanceCount;
1959     }
1960     MyType(const MyType &other)
1961         : number(other.number), text(other.text)
1962     {
1963         ++instanceCount;
1964     }
1965     ~MyType()
1966     {
1967         --instanceCount;
1968     }
1969     int number;
1970     const char *text;
1971 };
1972
1973 Q_DECLARE_METATYPE(MyType)
1974 Q_DECLARE_METATYPE(MyType*)
1975
1976 void tst_QVariant::userType()
1977 {
1978     {
1979         QVariant userVariant(QVariant::UserType);
1980
1981         QVERIFY(userVariant.isValid());
1982         QVERIFY(userVariant.isNull());
1983     }
1984
1985     {
1986         MyType data(1, "eins");
1987         MyType data2(2, "zwei");
1988
1989         {
1990             QVariant userVar;
1991             qVariantSetValue(userVar, data);
1992
1993             QCOMPARE(userVar.type(), QVariant::UserType);
1994             QCOMPARE(userVar.typeName(), "MyType");
1995             QVERIFY(!userVar.isNull());
1996             QVERIFY(!userVar.canConvert(QVariant::String));
1997             QVERIFY(!userVar.canConvert(QVariant::UserType));
1998
1999             QVariant userVar2(userVar);
2000             QVERIFY(userVar == userVar2);
2001
2002             qVariantSetValue(userVar2, data2);
2003             QVERIFY(userVar != userVar2);
2004
2005             const MyType *varData = static_cast<const MyType *>(userVar.constData());
2006             QVERIFY(varData);
2007             QCOMPARE(varData->number, data.number);
2008             QCOMPARE(varData->text, data.text);
2009
2010             QVariant userVar3;
2011             qVariantSetValue(userVar3, data2);
2012             QVERIFY(userVar2 == userVar3);
2013             userVar3 = userVar2;
2014             QVERIFY(userVar2 == userVar3);
2015         }
2016         // At this point all QVariants got destroyed but we have 2 MyType instances.
2017         QCOMPARE(instanceCount, 2);
2018         {
2019             QVariant userVar;
2020             qVariantSetValue(userVar, &data);
2021
2022             QCOMPARE(userVar.type(), QVariant::UserType);
2023             QCOMPARE(userVar.typeName(), "MyType*");
2024             QVERIFY(!userVar.isNull());
2025             QVERIFY(!userVar.canConvert(QVariant::String));
2026             QVERIFY(!userVar.canConvert(QVariant::UserType));
2027
2028             QVariant userVar2(userVar);
2029             QVERIFY(userVar == userVar2);
2030
2031             qVariantSetValue(userVar2, &data2);
2032             QVERIFY(userVar != userVar2);
2033
2034             MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2035             QVERIFY(varData);
2036             QCOMPARE(*varData, &data);
2037
2038             QVariant userVar3;
2039             qVariantSetValue(userVar3, &data2);
2040
2041             /* This check is correct now. userVar2 contains a pointer to data2 and so
2042              * does userVar3. */
2043             QVERIFY(userVar2 == userVar3);
2044
2045             userVar3 = userVar2;
2046             QVERIFY(userVar2 == userVar3);
2047         }
2048
2049         QCOMPARE(instanceCount, 2);
2050         QVariant myCarrier;
2051         qVariantSetValue(myCarrier, data);
2052         QCOMPARE(instanceCount, 3);
2053         {
2054             QVariant second = myCarrier;
2055             QCOMPARE(instanceCount, 4);
2056             second.detach();
2057             QCOMPARE(instanceCount, 4);
2058         }
2059         QCOMPARE(instanceCount, 3);
2060
2061         MyType data3(0, "null");
2062         data3 = qVariantValue<MyType>(myCarrier);
2063         QCOMPARE(data3.number, 1);
2064         QCOMPARE(data3.text, (const char *)"eins");
2065 #ifndef Q_CC_SUN
2066         QCOMPARE(instanceCount, 4);
2067 #endif
2068
2069     }
2070
2071     {
2072         const MyType data(3, "drei");
2073         QVariant myCarrier;
2074
2075         qVariantSetValue(myCarrier, data);
2076         QCOMPARE(myCarrier.typeName(), "MyType");
2077
2078         const MyType data2 = qvariant_cast<MyType>(myCarrier);
2079         QCOMPARE(data2.number, 3);
2080         QCOMPARE(data2.text, (const char *)"drei");
2081     }
2082
2083     {
2084         short s = 42;
2085         QVariant myCarrier;
2086
2087         qVariantSetValue(myCarrier, s);
2088         QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2089     }
2090
2091     {
2092         qlonglong ll = Q_INT64_C(42);
2093         QVariant myCarrier;
2094
2095         qVariantSetValue(myCarrier, ll);
2096         QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2097     }
2098
2099     // At this point all QVariants got destroyed and MyType objects too.
2100     QCOMPARE(instanceCount, 0);
2101 }
2102
2103 struct MyTypePOD
2104 {
2105     int a;
2106     int b;
2107 };
2108 Q_DECLARE_METATYPE(MyTypePOD)
2109
2110 void tst_QVariant::podUserType()
2111 {
2112     MyTypePOD pod;
2113     pod.a = 10;
2114     pod.b = 20;
2115
2116     QVariant pod_as_variant = qVariantFromValue(pod);
2117     MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2118
2119     QCOMPARE(pod.a, pod2.a);
2120     QCOMPARE(pod.b, pod2.b);
2121
2122     qVariantSetValue(pod_as_variant, pod);
2123     pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2124
2125     QCOMPARE(pod.a, pod2.a);
2126     QCOMPARE(pod.b, pod2.b);
2127 }
2128
2129 void tst_QVariant::basicUserType()
2130 {
2131     QVariant v;
2132     {
2133         int i = 7;
2134         v = QVariant(QMetaType::Int, &i);
2135     }
2136     QCOMPARE(v.type(), QVariant::Int);
2137     QCOMPARE(v.toInt(), 7);
2138
2139     {
2140         QString s("foo");
2141         v = QVariant(QMetaType::QString, &s);
2142     }
2143     QCOMPARE(v.type(), QVariant::String);
2144     QCOMPARE(v.toString(), QString("foo"));
2145
2146     {
2147         double d = 4.4;
2148         v = QVariant(QMetaType::Double, &d);
2149     }
2150     QCOMPARE(v.type(), QVariant::Double);
2151     QCOMPARE(v.toDouble(), 4.4);
2152
2153     {
2154         float f = 4.5f;
2155         v = QVariant(QMetaType::Float, &f);
2156     }
2157     QCOMPARE(v.userType(), int(QMetaType::Float));
2158     QCOMPARE(v.toDouble(), 4.5);
2159
2160     {
2161         QByteArray ba("bar");
2162         v = QVariant(QMetaType::QByteArray, &ba);
2163     }
2164     QCOMPARE(v.type(), QVariant::ByteArray);
2165     QCOMPARE(v.toByteArray(), QByteArray("bar"));
2166 }
2167
2168 void tst_QVariant::data_()
2169 {
2170     QVariant v;
2171
2172     QVariant i = 1;
2173     QVariant d = 1.12;
2174     QVariant f = 1.12f;
2175     QVariant ll = (qlonglong)2;
2176     QVariant ull = (qulonglong)3;
2177     QVariant s(QString("hallo"));
2178     QVariant r(QRect(1,2,3,4));
2179
2180     v = i;
2181     QVERIFY(v.data());
2182     QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2183
2184     v = d;
2185     QVERIFY(v.data());
2186     QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2187
2188     v = f;
2189     QVERIFY(v.data());
2190     QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2191
2192     v = ll;
2193     QVERIFY(v.data());
2194     QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2195
2196     v = ull;
2197     QVERIFY(v.data());
2198     QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2199
2200     v = s;
2201     QVERIFY(v.data());
2202     QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2203
2204     v = r;
2205     QVERIFY(v.data());
2206     QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2207 }
2208
2209 void tst_QVariant::constData()
2210 {
2211     QVariant v;
2212
2213     int i = 1;
2214     double d = 1.12;
2215     float f = 1.12f;
2216     qlonglong ll = 2;
2217     qulonglong ull = 3;
2218     QString s("hallo");
2219     QRect r(1,2,3,4);
2220
2221     v = QVariant(i);
2222     QVERIFY(v.constData());
2223     QCOMPARE(*static_cast<const int *>(v.constData()), i);
2224
2225     v = QVariant(d);
2226     QVERIFY(v.constData());
2227     QCOMPARE(*static_cast<const double *>(v.constData()), d);
2228
2229     v = QVariant(f);
2230     QVERIFY(v.constData());
2231     QCOMPARE(*static_cast<const float *>(v.constData()), f);
2232
2233     v = QVariant(ll);
2234     QVERIFY(v.constData());
2235     QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2236
2237     v = QVariant(ull);
2238     QVERIFY(v.constData());
2239     QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2240
2241     v = QVariant(s);
2242     QVERIFY(v.constData());
2243     QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2244
2245     v = QVariant(r);
2246     QVERIFY(v.constData());
2247     QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2248 }
2249
2250 struct Foo
2251 {
2252     Foo(): i(0) {}
2253     int i;
2254 };
2255
2256 Q_DECLARE_METATYPE(Foo)
2257
2258 void tst_QVariant::variant_to()
2259 {
2260     QVariant v1(4.2);
2261     QVariant v2(5);
2262
2263     QVariant v3;
2264     QVariant v4;
2265
2266     QStringList sl;
2267     sl << QLatin1String("blah");
2268
2269     qVariantSetValue(v3, sl);
2270
2271     Foo foo;
2272     foo.i = 42;
2273
2274     qVariantSetValue(v4, foo);
2275
2276     QCOMPARE(qvariant_cast<double>(v1), 4.2);
2277     QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2278     QCOMPARE(qvariant_cast<int>(v2), 5);
2279     QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2280     QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2281
2282     QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2283
2284     QVariant v5;
2285     QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2286
2287     QCOMPARE(qvariant_cast<int>(v1), 4);
2288
2289     QVariant n = qVariantFromValue<short>(42);
2290     QCOMPARE(qvariant_cast<int>(n), 42);
2291     QCOMPARE(qvariant_cast<uint>(n), 42u);
2292     QCOMPARE(qvariant_cast<double>(n), 42.0);
2293     QCOMPARE(qvariant_cast<float>(n), 42.f);
2294     QCOMPARE(qvariant_cast<short>(n), short(42));
2295     QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2296
2297     n = qVariantFromValue(43l);
2298     QCOMPARE(qvariant_cast<int>(n), 43);
2299     QCOMPARE(qvariant_cast<uint>(n), 43u);
2300     QCOMPARE(qvariant_cast<double>(n), 43.0);
2301     QCOMPARE(qvariant_cast<float>(n), 43.f);
2302     QCOMPARE(qvariant_cast<long>(n), 43l);
2303
2304     n = QLatin1String("44");
2305     QCOMPARE(qvariant_cast<int>(n), 44);
2306     QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2307     QCOMPARE(qvariant_cast<float>(n), 44.0f);
2308
2309     QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2310 }
2311
2312 struct Blah { int i; };
2313
2314 QDataStream& operator>>(QDataStream& s, Blah& c)
2315 { return (s >> c.i); }
2316
2317 QDataStream& operator<<(QDataStream& s, const Blah& c)
2318 { return (s << c.i); }
2319
2320 void tst_QVariant::saveLoadCustomTypes()
2321 {
2322     QByteArray data;
2323
2324     Blah i = { 42 };
2325     int tp = qRegisterMetaType<Blah>("Blah");
2326     QVariant v = QVariant(tp, &i);
2327
2328     qRegisterMetaTypeStreamOperators<Blah>("Blah");
2329
2330     QCOMPARE(v.userType(), tp);
2331     QVERIFY(v.type() == QVariant::UserType);
2332     {
2333         QDataStream stream(&data, QIODevice::WriteOnly);
2334         stream << v;
2335     }
2336
2337     v = QVariant();
2338
2339     {
2340         QDataStream stream(data);
2341         stream >> v;
2342     }
2343
2344     QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2345     int value = *(int*)v.constData();
2346     QCOMPARE(value, 42);
2347 }
2348
2349 void tst_QVariant::url()
2350 {
2351     QString str("http://qt.nokia.com");
2352     QUrl url(str);
2353
2354     QVariant v(url); //built with a QUrl
2355
2356     QVariant v2 = v;
2357
2358     QVariant v3(str); //built with a QString
2359
2360     QCOMPARE(v2.toUrl(), url);
2361     QVERIFY(qVariantCanConvert<QUrl>(v3));
2362     QCOMPARE(v2.toUrl(), v3.toUrl());
2363
2364     QVERIFY(qVariantCanConvert<QString>(v2));
2365     QCOMPARE(v2.toString(), str);
2366     QCOMPARE(v3.toString(), str);
2367 }
2368
2369 void tst_QVariant::globalColor()
2370 {
2371     QVariant variant(Qt::blue);
2372     QVERIFY(variant.type() == QVariant::Color);
2373     QVERIFY(qVariantValue<QColor>(variant) == QColor(Qt::blue));
2374 }
2375
2376 void tst_QVariant::variantMap()
2377 {
2378     QMap<QString, QVariant> map;
2379     map["test"] = 42;
2380
2381     QVariant v = map;
2382     QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2383
2384     QCOMPARE(map2.value("test").toInt(), 42);
2385
2386     QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2387     QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2388
2389     QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2390     QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2391 }
2392
2393 void tst_QVariant::variantHash()
2394 {
2395     QHash<QString, QVariant> hash;
2396     hash["test"] = 42;
2397
2398     QVariant v = hash;
2399     QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2400
2401     QCOMPARE(hash2.value("test").toInt(), 42);
2402
2403     QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2404     QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2405
2406     QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2407     QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2408 }
2409
2410 void tst_QVariant::invalidQColor() const
2411 {
2412     QVariant va("An invalid QColor::name() value.");
2413     QVERIFY(va.canConvert(QVariant::Color));
2414
2415     QVERIFY(!va.convert(QVariant::Color));
2416
2417     QVERIFY(!qvariant_cast<QColor>(va).isValid());
2418 }
2419
2420 void tst_QVariant::qvariant_cast_QObject_data() {
2421
2422     QTest::addColumn<QVariant>("data");
2423     QTest::addColumn<bool>("success");
2424     QObject *obj = new QObject(this);
2425     obj->setObjectName(QString::fromLatin1("Hello"));
2426     QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2427     QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2428     QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2429     QTest::newRow("from int") << QVariant((int) 123) << false;
2430 }
2431
2432
2433 void tst_QVariant::qvariant_cast_QObject() {
2434     QFETCH(QVariant, data);
2435     QFETCH(bool, success);
2436
2437     QObject *o = qvariant_cast<QObject *>(data);
2438     QCOMPARE(o != 0, success);
2439     if (success) {
2440         QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2441     }
2442 }
2443
2444 Q_DECLARE_METATYPE(qint8);
2445
2446 void tst_QVariant::convertToQUint8() const
2447 {
2448     /* qint8. */
2449     {
2450         const qint8 anInt = 32;
2451
2452         /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2453          * It's of type QVariant::Int. */
2454         const QVariant v0 = anInt;
2455
2456         QVERIFY(qVariantCanConvert<qint8>(v0));
2457         QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2458         QCOMPARE(int(v0.toInt()), 32);
2459         QCOMPARE(v0.toString(), QString("32"));
2460
2461         QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2462         QCOMPARE(int(qvariant_cast<char>(v0)),      32);
2463         QCOMPARE(int(qvariant_cast<short>(v0)),     32);
2464         QCOMPARE(int(qvariant_cast<long>(v0)),      32);
2465         QCOMPARE(int(qvariant_cast<float>(v0)),     32);
2466         QCOMPARE(int(qvariant_cast<double>(v0)),    32);
2467     }
2468
2469     /* quint8. */
2470     {
2471         const quint8 anInt = 32;
2472         const QVariant v0 = anInt;
2473
2474         QVERIFY(qVariantCanConvert<quint8>(v0));
2475         QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2476         QCOMPARE(int(v0.toUInt()), 32);
2477         QCOMPARE(v0.toString(), QString("32"));
2478     }
2479
2480     /* qint16. */
2481     {
2482         const qint16 anInt = 32;
2483         const QVariant v0 = anInt;
2484
2485         QVERIFY(qVariantCanConvert<qint16>(v0));
2486         QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2487         QCOMPARE(int(v0.toInt()), 32);
2488         QCOMPARE(v0.toString(), QString("32"));
2489     }
2490
2491     /* quint16. */
2492     {
2493         const quint16 anInt = 32;
2494         const QVariant v0 = anInt;
2495
2496         QVERIFY(qVariantCanConvert<quint16>(v0));
2497         QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2498         QCOMPARE(int(v0.toUInt()), 32);
2499         QCOMPARE(v0.toString(), QString("32"));
2500     }
2501 }
2502
2503 void tst_QVariant::comparePointers() const
2504 {
2505     class MyClass
2506     {
2507     };
2508
2509     MyClass myClass;
2510
2511     QVariant v  = qVariantFromValue<void *>(&myClass);
2512     QVariant v2 = qVariantFromValue<void *>(&myClass);
2513
2514     QCOMPARE(v, v2);
2515 }
2516
2517 struct Data {};
2518 Q_DECLARE_METATYPE(Data*)
2519
2520 void tst_QVariant::voidStar() const
2521 {
2522     char c;
2523     void *p1 = &c;
2524     void *p2 = p1;
2525
2526     QVariant v1, v2;
2527     v1 = qVariantFromValue(p1);
2528     v2 = v1;
2529     QVERIFY(v1 == v2);
2530
2531     v2 = qVariantFromValue(p2);
2532     QVERIFY(v1 == v2);
2533
2534     p2 = 0;
2535     v2 = qVariantFromValue(p2);
2536     QVERIFY(v1 != v2);
2537 }
2538
2539 void tst_QVariant::dataStar() const
2540 {
2541     qRegisterMetaType<Data*>();
2542     Data *p1 = new Data;
2543
2544     QVariant v1 = qVariantFromValue(p1);
2545     QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2546     QCOMPARE(qvariant_cast<Data*>(v1), p1);
2547
2548     QVariant v2 = v1;
2549     QVERIFY(v1 == v2);
2550
2551     v2 = qVariantFromValue(p1);
2552     QVERIFY(v1 == v2);
2553     delete p1;
2554 }
2555
2556 void tst_QVariant::canConvertQStringList() const
2557 {
2558     QFETCH(bool, canConvert);
2559     QFETCH(QStringList, input);
2560     QFETCH(QString, result);
2561
2562     QVariant v(input);
2563
2564     QCOMPARE(v.canConvert(QVariant::String), canConvert);
2565     QCOMPARE(v.toString(), result);
2566 }
2567
2568 void tst_QVariant::canConvertQStringList_data() const
2569 {
2570     QTest::addColumn<bool>("canConvert");
2571     QTest::addColumn<QStringList>("input");
2572     QTest::addColumn<QString>("result");
2573
2574     QTest::newRow("An empty list") << false << QStringList() << QString();
2575     QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2576     QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2577
2578     QStringList l;
2579     l << "a" << "b";
2580
2581     QTest::newRow("Two items") << false << l << QString();
2582
2583     l << "c";
2584     QTest::newRow("Three items") << false << l << QString();
2585 }
2586
2587 template<typename T> void convertMetaType()
2588 {
2589     QVERIFY(qVariantFromValue<T>(10).isValid());
2590     QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2591     QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2592     QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2593 }
2594
2595 #define CONVERT_META_TYPE(Type) \
2596     convertMetaType<Type>(); \
2597     if (QTest::currentTestFailed()) \
2598         QFAIL("convertMetaType<" #Type "> failed");
2599
2600 void tst_QVariant::canConvertMetaTypeToInt() const
2601 {
2602     CONVERT_META_TYPE(long);
2603     CONVERT_META_TYPE(short);
2604     CONVERT_META_TYPE(short);
2605     CONVERT_META_TYPE(unsigned short);
2606     CONVERT_META_TYPE(ushort);
2607     CONVERT_META_TYPE(ulong);
2608     CONVERT_META_TYPE(unsigned long);
2609     CONVERT_META_TYPE(uchar);
2610     CONVERT_META_TYPE(unsigned char);
2611     CONVERT_META_TYPE(char);
2612     CONVERT_META_TYPE(uint);
2613     CONVERT_META_TYPE(unsigned int);
2614 }
2615
2616 #undef CONVERT_META_TYPE
2617
2618 /*!
2619  These calls should not produce any warnings.
2620  */
2621 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2622 {
2623     {
2624         const QVariant variant(QLatin1String("An invalid QDateTime string"));
2625         const QDateTime dateTime(variant.toDateTime());
2626         QVERIFY(!dateTime.isValid());
2627     }
2628
2629     {
2630         QVariant v1(QLatin1String("xyz"));
2631         v1.convert(QVariant::DateTime);
2632
2633         QVariant v2(QLatin1String("xyz"));
2634         QDateTime dt1(v2.toDateTime());
2635
2636         const QVariant v3(QLatin1String("xyz"));
2637         const QDateTime dt2(v3.toDateTime());
2638     }
2639 }
2640
2641 void tst_QVariant::invalidDateTime() const
2642 {
2643     QVariant variant(QString::fromLatin1("Invalid date time string"));
2644     QVERIFY(!variant.toDateTime().isValid());
2645     QVERIFY(!variant.convert(QVariant::DateTime));
2646 }
2647
2648 struct MyClass
2649 {
2650     MyClass() : myValue(0) {}
2651     int myValue;
2652 };
2653
2654 Q_DECLARE_METATYPE( MyClass )
2655
2656 void tst_QVariant::loadUnknownUserType()
2657 {
2658     qRegisterMetaType<MyClass>("MyClass");
2659     char data[] = {0, 0, 0, 127, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0};
2660
2661     QByteArray ba(data, sizeof(data));
2662     QDataStream ds(&ba, QIODevice::ReadOnly);
2663     QVariant var;
2664     var.load(ds);
2665     QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2666 }
2667
2668 void tst_QVariant::loadBrokenUserType()
2669 {
2670     char data[] = {0, 0, 0, 127, 0, 112 };
2671
2672     QByteArray ba(data, sizeof(data));
2673     QDataStream ds(&ba, QIODevice::ReadOnly);
2674     QVariant var;
2675     var.load(ds);
2676     QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2677 }
2678
2679 void tst_QVariant::invalidDate() const
2680 {
2681     QString foo("Hello");
2682     QVariant variant(foo);
2683     QVERIFY(!variant.convert(QVariant::Date));
2684
2685     variant = foo;
2686     QVERIFY(!variant.convert(QVariant::DateTime));
2687
2688     variant = foo;
2689     QVERIFY(!variant.convert(QVariant::Time));
2690
2691     variant = foo;
2692     QVERIFY(!variant.convert(QVariant::Int));
2693
2694     variant = foo;
2695     QVERIFY(!variant.convert(QVariant::Double));
2696
2697     variant = foo;
2698     QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2699 }
2700
2701 struct WontCompare
2702 {
2703     int x,y,z,q,w,e,r,t;
2704 };
2705 Q_DECLARE_METATYPE(WontCompare);
2706
2707 void tst_QVariant::compareCustomTypes() const
2708 {
2709     qRegisterMetaType<WontCompare>("WontCompare");
2710
2711     WontCompare f1;
2712     f1.x = 0;
2713     const QVariant variant1(qVariantFromValue(f1));
2714
2715     WontCompare f2;
2716     f2.x = 0;
2717     const QVariant variant2(qVariantFromValue(f2));
2718
2719     /* We compare pointers. */
2720     QVERIFY(variant1 != variant2);
2721     QVERIFY(variant1 == variant1);
2722     QVERIFY(variant2 == variant2);
2723 }
2724
2725 void tst_QVariant::timeToDateTime() const
2726 {
2727     const QVariant val(QTime::currentTime());
2728     QVERIFY(!val.canConvert(QVariant::DateTime));
2729     QVERIFY(!val.toDateTime().isValid());
2730 }
2731
2732 Q_DECLARE_METATYPE(QHostAddress)
2733
2734 void tst_QVariant::copyingUserTypes() const
2735 {
2736     QVariant var;
2737     QVariant var3;
2738     const QHostAddress ha("127.0.0.1");
2739     qVariantSetValue(var, ha);
2740     var3 = var;
2741
2742     QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2743 }
2744
2745 void tst_QVariant::convertBoolToByteArray() const
2746 {
2747     QFETCH(QByteArray, input);
2748     QFETCH(bool, canConvert);
2749     QFETCH(bool, value);
2750
2751     const QVariant variant(input);
2752
2753     QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2754
2755     if(canConvert) {
2756         /* Just call this function so we run the code path. */
2757         QCOMPARE(variant.toBool(), value);
2758     }
2759 }
2760
2761 void tst_QVariant::convertBoolToByteArray_data() const
2762 {
2763     QTest::addColumn<QByteArray>("input");
2764     QTest::addColumn<bool>("canConvert");
2765     QTest::addColumn<bool>("value");
2766
2767     QTest::newRow("false")
2768         << QByteArray("false")
2769         << true
2770         << false;
2771
2772     QTest::newRow("FALSE")
2773         << QByteArray("FALSE")
2774         << true
2775         << false;
2776
2777     QTest::newRow("falSE")
2778         << QByteArray("FALSE")
2779         << true
2780         << false;
2781
2782     QTest::newRow("")
2783         << QByteArray("")
2784         << true
2785         << false;
2786
2787     QTest::newRow("null QByteArray")
2788         << QByteArray()
2789         << true
2790         << false;
2791
2792     QTest::newRow("any-content")
2793         << QByteArray("any-content")
2794         << true
2795         << true;
2796
2797     QTest::newRow("true")
2798         << QByteArray("true")
2799         << true
2800         << true;
2801
2802     QTest::newRow("TRUE")
2803         << QByteArray("TRUE")
2804         << true
2805         << true;
2806
2807     QTest::newRow("trUE")
2808         << QByteArray("trUE")
2809         << true
2810         << true;
2811 }
2812
2813 void tst_QVariant::convertByteArrayToBool() const
2814 {
2815     QFETCH(bool, input);
2816     QFETCH(QByteArray, output);
2817
2818     const QVariant variant(input);
2819     QCOMPARE(variant.type(), QVariant::Bool);
2820     QCOMPARE(variant.toBool(), input);
2821     QVERIFY(qVariantCanConvert<bool>(variant));
2822
2823     QCOMPARE(variant.toByteArray(), output);
2824 }
2825
2826 void tst_QVariant::convertByteArrayToBool_data() const
2827 {
2828     QTest::addColumn<bool>("input");
2829     QTest::addColumn<QByteArray>("output");
2830
2831     QTest::newRow("false")
2832         << false
2833         << QByteArray("false");
2834
2835     QTest::newRow("true")
2836         << true
2837         << QByteArray("true");
2838 }
2839
2840 /*!
2841   We verify that:
2842     1. Converting the string "9.9" to int fails. This is the behavior of
2843        toLongLong() and hence also QVariant, since it uses it.
2844     2. Converting the QVariant containing the double 9.9 to int works.
2845
2846   Rationale: "9.9" is not a valid int. However, doubles are by definition not
2847   ints and therefore it makes more sense to perform conversion for those.
2848 */
2849 void tst_QVariant::toIntFromQString() const
2850 {
2851     QVariant first("9.9");
2852     bool ok;
2853     QCOMPARE(first.toInt(&ok), 0);
2854     QVERIFY(!ok);
2855
2856     QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
2857     QVERIFY(!ok);
2858
2859     QVariant v(9.9);
2860     QCOMPARE(v.toInt(&ok), 10);
2861     QVERIFY(ok);
2862 }
2863
2864 /*!
2865   We verify that:
2866     1. Conversion from (64 bit) double to int works (no overflow).
2867     2. Same conversion works for QVariant::convert.
2868
2869   Rationale: if 2147483630 is set in float and then converted to int,
2870   there will be overflow and the result will be -2147483648.
2871 */
2872 void tst_QVariant::toIntFromDouble() const
2873 {
2874     double d = 2147483630;  // max int 2147483647
2875     QVERIFY((int)d == 2147483630);
2876
2877     QVariant var(d);
2878     QVERIFY( var.canConvert( QVariant::Int ) );
2879
2880     bool ok;
2881     int result = var.toInt(&ok);
2882
2883     QVERIFY( ok == true );
2884     QCOMPARE(result, 2147483630);
2885 }
2886
2887 void tst_QVariant::setValue()
2888 {
2889     QTransform t; //we just take a value so that we're sure that it will be shared
2890     QVariant v1 = t;
2891     QVERIFY( v1.isDetached() );
2892     QVariant v2 = v1;
2893     QVERIFY( !v1.isDetached() );
2894     QVERIFY( !v2.isDetached() );
2895
2896     qVariantSetValue(v2, 3); //set an integer value
2897
2898     QVERIFY( v1.isDetached() );
2899     QVERIFY( v2.isDetached() );
2900 }
2901
2902 void tst_QVariant::numericalConvert()
2903 {
2904 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
2905     QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
2906 #endif
2907     QVariant vfloat(float(5.3));
2908     QVariant vdouble(double(5.3));
2909     QVariant vreal(qreal(5.3));
2910     QVariant vint(int(5));
2911     QVariant vuint(uint(5));
2912     QVariant vshort(short(5));
2913     QVariant vlonglong(quint64(5));
2914     QVariant vstringint(QString::fromLatin1("5"));
2915     QVariant vstring(QString::fromLatin1("5.3"));
2916
2917     QVector<QVariant *> vect;
2918     vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
2919
2920     for(int i = 0; i < vect.size(); i++) {
2921         double num = 5.3;
2922         if (i >= 3 && i <= 7)
2923             num = 5;
2924         QVariant *v = vect.at(i);
2925         QCOMPARE(v->toFloat() , float(num));
2926         QCOMPARE(float(v->toReal()) , float(num));
2927         QCOMPARE(float(v->toDouble()) , float(num));
2928         if(i != 8) {
2929             QCOMPARE(v->toInt() , int(num));
2930             QCOMPARE(v->toUInt() , uint(num));
2931             QCOMPARE(v->toULongLong() , quint64(num));
2932         }
2933         QCOMPARE(v->toString() , QString::number(num));
2934     }
2935 }
2936
2937
2938 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
2939 {
2940     QVariant v = QVariant::fromValue(orig);
2941     QVERIFY(v.isValid());
2942     QCOMPARE(v.isNull(), isNull);
2943     QCOMPARE(v.toString(), toString);
2944     QCOMPARE(v.toDouble(), toDouble);
2945     QCOMPARE(v.toBool(), toBool);
2946     QCOMPARE(qvariant_cast<T>(v), orig);
2947
2948     {
2949         QVariant v2 = v;
2950         if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
2951             // Type is movable so standard comparison algorithm in QVariant should work
2952             // In a custom type QVariant is not aware of ==operator so it won't be called,
2953             // which may cause problems especially visible when using a not-movable type
2954             QCOMPARE(v2, v);
2955         }
2956         QVERIFY(v2.isValid());
2957         QCOMPARE(v2.isNull(), isNull);
2958         QCOMPARE(v2.toString(), toString);
2959         QCOMPARE(v2.toDouble(), toDouble);
2960         QCOMPARE(v2.toBool(), toBool);
2961         QCOMPARE(qvariant_cast<T>(v2), orig);
2962
2963         QVariant v3;
2964         v = QVariant();
2965         QCOMPARE(v3, v);
2966         v = v2;
2967         if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
2968             // Type is movable so standard comparison algorithm in QVariant should work
2969             // In a custom type QVariant is not aware of ==operator so it won't be called,
2970             // which may cause problems especially visible when using a not-movable type
2971             QCOMPARE(v2, v);
2972         }
2973         QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
2974         QCOMPARE(v2.toString(), toString);
2975         v3 = qVariantFromValue(orig);
2976
2977         QVERIFY(v3.isValid());
2978         QCOMPARE(v3.isNull(), isNull);
2979         QCOMPARE(v3.toString(), toString);
2980         QCOMPARE(v3.toDouble(), toDouble);
2981         QCOMPARE(v3.toBool(), toBool);
2982         QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
2983     }
2984
2985     QVERIFY(v.isValid());
2986     QCOMPARE(v.isNull(), isNull);
2987     QCOMPARE(v.toString(), toString);
2988     QCOMPARE(v.toDouble(), toDouble);
2989     QCOMPARE(v.toBool(), toBool);
2990     QCOMPARE(qvariant_cast<T>(v), orig);
2991
2992     if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
2993         QCOMPARE(v.userType(), qMetaTypeId<T>());
2994         QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
2995     }
2996 }
2997
2998 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
2999     playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3000     if (QTest::currentTestFailed())\
3001         QFAIL("playWithVariant failed");
3002
3003 struct MyPrimitive
3004 {
3005     char x, y;
3006     bool operator==(const MyPrimitive &o) const
3007     {
3008         return x == o.x && y == o.y;
3009     }
3010 };
3011
3012 QT_BEGIN_NAMESPACE
3013 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3014 QT_END_NAMESPACE
3015
3016 struct MyData
3017 {
3018     void *ptr;
3019     MyData() : ptr(this) {}
3020     ~MyData()
3021     {
3022         if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3023     }
3024     MyData(const MyData& o) : ptr(this)
3025     {
3026         if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3027     }
3028     MyData &operator=(const MyData &o)
3029     {
3030         if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3031         if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3032         return *this;
3033     }
3034     bool operator==(const MyData &o) const
3035     {
3036         if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3037         if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3038         return true;
3039     }
3040 };
3041
3042 struct MyMovable
3043 {
3044     static int count;
3045     int v;
3046     MyMovable() { v = count++; }
3047     ~MyMovable() { count--; }
3048     MyMovable(const MyMovable &o) : v(o.v) { count++; }
3049
3050     bool operator==(const MyMovable &o) const
3051     {
3052         return v == o.v;
3053     }
3054 };
3055
3056 int MyMovable::count  = 0;
3057
3058 struct MyNotMovable
3059 {
3060     static int count;
3061     MyNotMovable *that;
3062     MyNotMovable() : that(this) { count++; }
3063     ~MyNotMovable() { QCOMPARE(that, this);  count--; }
3064     MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3065     MyNotMovable &operator=(const MyNotMovable &o) {
3066         bool ok = that == this && o.that == &o;
3067         if (!ok) qFatal("MyNotMovable has been moved");
3068         return *this;
3069     }
3070
3071     //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3072     // so it would be fine just to always return true
3073     bool operator==(const MyNotMovable &o) const
3074     {
3075         bool ok = that == this && o.that == &o;
3076         if (!ok) qFatal("MyNotMovable has been moved");
3077         return ok;
3078     }
3079 };
3080
3081 int MyNotMovable::count  = 0;
3082
3083 struct MyShared : QSharedData {
3084     MyMovable movable;
3085 };
3086
3087 QT_BEGIN_NAMESPACE
3088 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3089 QT_END_NAMESPACE
3090
3091 Q_DECLARE_METATYPE(QList<QSize>)
3092 Q_DECLARE_METATYPE(MyPrimitive)
3093 Q_DECLARE_METATYPE(MyData)
3094 Q_DECLARE_METATYPE(MyMovable)
3095 Q_DECLARE_METATYPE(MyNotMovable)
3096 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3097 Q_DECLARE_METATYPE(QList<MyData>)
3098 Q_DECLARE_METATYPE(QList<MyMovable>)
3099 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3100 Q_DECLARE_METATYPE(MyPrimitive *)
3101 Q_DECLARE_METATYPE(MyData *)
3102 Q_DECLARE_METATYPE(MyMovable *)
3103 Q_DECLARE_METATYPE(MyNotMovable *)
3104 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3105
3106
3107 void tst_QVariant::moreCustomTypes()
3108 {
3109     {
3110         QList<QSize> listSize;
3111         PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3112         listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3113         PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3114     }
3115
3116     {
3117         QString str;
3118         PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3119         str = QString::fromLatin1("123456789.123");
3120         PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3121     }
3122
3123     {
3124         QSize size;
3125         PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3126         PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3127     }
3128
3129     {
3130         MyData d;
3131         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3132         PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3133         QList<MyData> l;
3134         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3135         l << MyData() << MyData();
3136         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3137     }
3138
3139     {
3140         MyPrimitive d = { 4, 5 };
3141         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3142         PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3143         QList<MyPrimitive> l;
3144         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3145         l << d;
3146         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3147     }
3148
3149     {
3150         MyMovable d;
3151         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3152         PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3153         QList<MyMovable> l;
3154         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3155         l << MyMovable() << d;
3156         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3157     }
3158     QCOMPARE(MyMovable::count, 0);
3159
3160     QCOMPARE(MyNotMovable::count, 0);
3161     {
3162         MyNotMovable d;
3163         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3164         PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3165         QList<MyNotMovable> l;
3166         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3167         l << MyNotMovable() << d;
3168         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3169     }
3170     QCOMPARE(MyNotMovable::count, 0);
3171
3172     {
3173         PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3174         PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3175         PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3176         PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3177         PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3178         PLAY_WITH_VARIANT(  qint8(13), false, "\xd", 13, true);
3179         PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3180         PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3181         PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3182         PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3183         PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3184         PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3185         PLAY_WITH_VARIANT(  qint8(-12), false, "\xf4", -12, true);
3186         PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3187         PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3188         PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3189         PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3190         PLAY_WITH_VARIANT( true, false, "true", 1, true);
3191         PLAY_WITH_VARIANT( false, false, "false", 0, false);
3192
3193         PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3194     }
3195
3196     {
3197         int i = 5;
3198         PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3199         PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3200     }
3201
3202     {
3203         QVariant v1 = QVariant::fromValue(5);
3204         QVariant v2 = QVariant::fromValue(5.0);
3205         QVariant v3 = QVariant::fromValue(quint16(5));
3206         QVariant v4 = 5;
3207         QVariant v5 = QVariant::fromValue(MyPrimitive());
3208         QVariant v6 = QVariant::fromValue(MyMovable());
3209         QVariant v7 = QVariant::fromValue(MyData());
3210         PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3211         PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3212         PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3213         PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3214
3215         PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3216     }
3217
3218     QCOMPARE(MyMovable::count, 0);
3219     {
3220         QSharedDataPointer<MyShared> d(new MyShared);
3221         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3222     }
3223     QCOMPARE(MyMovable::count, 0);
3224 }
3225
3226
3227 void tst_QVariant::variantInVariant()
3228 {
3229     QVariant var1 = 5;
3230     QCOMPARE(var1.type(), QVariant::Int);
3231     QVariant var2 = var1;
3232     QCOMPARE(var2, var1);
3233     QCOMPARE(var2.type(), QVariant::Int);
3234     QVariant var3 = QVariant::fromValue(var1);
3235     QCOMPARE(var3, var1);
3236     QCOMPARE(var3.type(), QVariant::Int);
3237     QVariant var4 = qvariant_cast<QVariant>(var1);
3238     QCOMPARE(var4, var1);
3239     QCOMPARE(var4.type(), QVariant::Int);
3240     QVariant var5;
3241     var5 = var1;
3242     QCOMPARE(var5, var1);
3243     QCOMPARE(var5.type(), QVariant::Int);
3244     QVariant var6;
3245     var6.setValue(var1);
3246     QCOMPARE(var6, var1);
3247     QCOMPARE(var6.type(), QVariant::Int);
3248
3249     QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3250     QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3251     QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3252
3253     QString str("hello");
3254     QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3255     QCOMPARE((int)var8.type(), (int)QVariant::String);
3256     QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3257
3258     QVariant var9(qMetaTypeId<QVariant>(), &var1);
3259     QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3260     QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3261 }
3262
3263 void tst_QVariant::colorInteger()
3264 {
3265     QVariant v = QColor(Qt::red);
3266     QCOMPARE(v.type(), QVariant::Color);
3267     QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3268
3269     v.setValue(1000);
3270     QCOMPARE(v.type(), QVariant::Int);
3271     QCOMPARE(v.toInt(), 1000);
3272
3273     v.setValue(QColor(Qt::yellow));
3274     QCOMPARE(v.type(), QVariant::Color);
3275     QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3276 }
3277
3278 class Forward;
3279 Q_DECLARE_METATYPE(Forward*);
3280
3281 void tst_QVariant::forwardDeclare()
3282 {
3283     Forward *f = 0;
3284     QVariant v = QVariant::fromValue(f);
3285     QCOMPARE(qvariant_cast<Forward*>(v), f);
3286 }
3287
3288
3289 QTEST_MAIN(tst_QVariant)
3290 #include "tst_qvariant.moc"