1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <private/qdeclarativecompiler_p.h>
45 class tst_qdeclarativeinstruction : public QObject
49 tst_qdeclarativeinstruction() {}
65 static QStringList messages;
66 static void msgHandler(QtMsgType, const char *msg)
68 messages << QLatin1String(msg);
71 void tst_qdeclarativeinstruction::dump()
73 QDeclarativeEngine engine;
74 QDeclarativeCompiledData *data = new QDeclarativeCompiledData(&engine);
77 QDeclarativeCompiledData::Instruction::Init i;
79 i.parserStatusSize = 3;
81 i.compiledBinding = -1;
82 data->addInstruction(i);
86 QDeclarativeCompiledData::TypeReference ref;
87 ref.className = "Test";
90 QDeclarativeCompiledData::Instruction::CreateCppObject i;
94 data->addInstruction(i);
98 data->primitives << "testId";
100 QDeclarativeCompiledData::Instruction::SetId i;
101 i.value = data->primitives.count() - 1;
103 data->addInstruction(i);
107 QDeclarativeCompiledData::Instruction::SetDefault i;
108 data->addInstruction(i);
112 QDeclarativeCompiledData::Instruction::CreateComponent i;
118 data->addInstruction(i);
122 QDeclarativeCompiledData::Instruction::StoreMetaObject i;
127 data->addInstruction(i);
131 QDeclarativeCompiledData::Instruction::StoreFloat i;
134 data->addInstruction(i);
138 QDeclarativeCompiledData::Instruction::StoreDouble i;
141 data->addInstruction(i);
145 QDeclarativeCompiledData::Instruction::StoreInteger i;
148 data->addInstruction(i);
152 QDeclarativeCompiledData::Instruction::StoreBool i;
156 data->addInstruction(i);
160 data->primitives << "Test String";
161 QDeclarativeCompiledData::Instruction::StoreString i;
163 i.value = data->primitives.count() - 1;
164 data->addInstruction(i);
168 data->urls << QUrl("http://www.nokia.com");
169 QDeclarativeCompiledData::Instruction::StoreUrl i;
171 i.value = data->urls.count() - 1;
172 data->addInstruction(i);
176 QDeclarativeCompiledData::Instruction::StoreColor i;
178 i.value = 0xFF00FF00;
179 data->addInstruction(i);
183 QDeclarativeCompiledData::Instruction::StoreDate i;
184 i.propertyIndex = 10;
186 data->addInstruction(i);
190 QDeclarativeCompiledData::Instruction::StoreTime i;
191 i.propertyIndex = 11;
192 data->addInstruction(i);
196 QDeclarativeCompiledData::Instruction::StoreDateTime i;
197 i.propertyIndex = 12;
198 data->addInstruction(i);
202 QDeclarativeCompiledData::Instruction::StorePoint i;
203 i.propertyIndex = 13;
206 data->addInstruction(i);
210 QDeclarativeCompiledData::Instruction::StorePointF i;
211 i.propertyIndex = 13;
214 data->addInstruction(i);
218 QDeclarativeCompiledData::Instruction::StoreSize i;
219 i.propertyIndex = 15;
222 data->addInstruction(i);
226 QDeclarativeCompiledData::Instruction::StoreSizeF i;
227 i.propertyIndex = 15;
230 data->addInstruction(i);
234 QDeclarativeCompiledData::Instruction::StoreRect i;
235 i.propertyIndex = 17;
240 data->addInstruction(i);
244 QDeclarativeCompiledData::Instruction::StoreRectF i;
245 i.propertyIndex = 18;
250 data->addInstruction(i);
254 QDeclarativeCompiledData::Instruction::StoreVector3D i;
255 i.propertyIndex = 19;
259 data->addInstruction(i);
263 data->primitives << "color(1, 1, 1, 1)";
264 QDeclarativeCompiledData::Instruction::StoreVariant i;
265 i.propertyIndex = 20;
266 i.value = data->primitives.count() - 1;
268 data->addInstruction(i);
272 QDeclarativeCompiledData::Instruction::StoreObject i;
273 i.propertyIndex = 21;
274 data->addInstruction(i);
278 QDeclarativeCompiledData::Instruction::StoreVariantObject i;
279 i.propertyIndex = 22;
280 data->addInstruction(i);
284 QDeclarativeCompiledData::Instruction::StoreInterface i;
285 i.propertyIndex = 23;
286 data->addInstruction(i);
290 data->primitives << "console.log(1921)";
292 QDeclarativeCompiledData::Instruction::StoreSignal i;
294 i.value = data->primitives.count() - 1;
295 data->addInstruction(i);
299 QDeclarativeCompiledData::Instruction::StoreScriptString i;
300 i.propertyIndex = 24;
304 data->addInstruction(i);
308 data->primitives << "mySignal";
310 QDeclarativeCompiledData::Instruction::AssignSignalObject i;
311 i.signal = data->primitives.count() - 1;
312 data->addInstruction(i);
316 QDeclarativeCompiledData::Instruction::AssignCustomType i;
317 i.propertyIndex = 25;
320 data->addInstruction(i);
324 QDeclarativeCompiledData::Instruction::StoreBinding i;
325 i.property.coreIndex = 26;
329 data->addInstruction(i);
333 QDeclarativeCompiledData::Instruction::StoreV4Binding i;
338 data->addInstruction(i);
342 QDeclarativeCompiledData::Instruction::StoreValueSource i;
343 i.property.coreIndex = 29;
346 data->addInstruction(i);
350 QDeclarativeCompiledData::Instruction::StoreValueInterceptor i;
351 i.property.coreIndex = 30;
354 data->addInstruction(i);
358 QDeclarativeCompiledData::Instruction::BeginObject i;
360 data->addInstruction(i);
364 QDeclarativeCompiledData::Instruction::StoreObjectQList i;
365 data->addInstruction(i);
369 QDeclarativeCompiledData::Instruction::AssignObjectList i;
370 data->addInstruction(i);
374 QDeclarativeCompiledData::Instruction::FetchAttached i;
376 data->addInstruction(i);
380 QDeclarativeCompiledData::Instruction::FetchQList i;
382 data->addInstruction(i);
386 QDeclarativeCompiledData::Instruction::FetchObject i;
388 data->addInstruction(i);
392 QDeclarativeCompiledData::Instruction::FetchValueType i;
395 i.bindingSkipList = 7;
396 data->addInstruction(i);
400 QDeclarativeCompiledData::Instruction::PopFetchedObject i;
401 data->addInstruction(i);
405 QDeclarativeCompiledData::Instruction::PopQList i;
406 data->addInstruction(i);
410 QDeclarativeCompiledData::Instruction::PopValueType i;
413 data->addInstruction(i);
417 QDeclarativeCompiledData::Instruction::Defer i;
419 data->addInstruction(i);
423 QDeclarativeCompiledData::Instruction::Defer i;
425 data->addInstruction(i);
429 QDeclarativeCompiledData::Instruction::StoreImportedScript i;
431 data->addInstruction(i);
435 QDeclarativeCompiledData::Instruction::StoreVariantInteger i;
437 i.propertyIndex = 32;
438 data->addInstruction(i);
442 QDeclarativeCompiledData::Instruction::StoreVariantDouble i;
444 i.propertyIndex = 19;
445 data->addInstruction(i);
449 QDeclarativeCompiledData::Instruction::Done i;
450 data->addInstruction(i);
454 QDeclarativeCompiledData::Instruction::StoreTrString i;
455 i.propertyIndex = 99;
460 data->addInstruction(i);
464 QDeclarativeCompiledData::Instruction::StoreTrIdString i;
465 i.propertyIndex = 78;
468 data->addInstruction(i);
473 << "Index\tOperation\t\tData1\tData2\tData3\tComments"
474 << "-------------------------------------------------------------------------------"
475 << "0\t\tINIT\t\t\t0\t3\t-1\t-1"
476 << "1\t\tCREATECPP\t\t\t0\t\t\t\"Test\""
477 << "2\t\tSETID\t\t\t0\t\t\t\"testId\""
478 << "3\t\tSET_DEFAULT"
479 << "4\t\tCREATE_COMPONENT\t3"
480 << "5\t\tSTORE_META\t\t3"
481 << "6\t\tSTORE_FLOAT\t\t3\t11.3"
482 << "7\t\tSTORE_DOUBLE\t\t4\t14.8"
483 << "8\t\tSTORE_INTEGER\t\t5\t9"
484 << "9\t\tSTORE_BOOL\t\t6\ttrue"
485 << "10\t\tSTORE_STRING\t\t7\t1\t\t\"Test String\""
486 << "11\t\tSTORE_URL\t\t8\t0\t\tQUrl(\"http://www.nokia.com\") "
487 << "12\t\tSTORE_COLOR\t\t9\t\t\t\"ff00ff00\""
488 << "13\t\tSTORE_DATE\t\t10\t9"
489 << "14\t\tSTORE_TIME\t\t11"
490 << "15\t\tSTORE_DATETIME\t\t12"
491 << "16\t\tSTORE_POINT\t\t13\t3\t7"
492 << "17\t\tSTORE_POINTF\t\t13\t3\t7"
493 << "18\t\tSTORE_SIZE\t\t15\t8\t11"
494 << "19\t\tSTORE_SIZEF\t\t15\t8\t11"
495 << "20\t\tSTORE_RECT\t\t17\t7\t9\t11\t13"
496 << "21\t\tSTORE_RECTF\t\t18\t11.3\t9.8\t3\t2.1"
497 << "22\t\tSTORE_VECTOR3D\t\t19\t9\t3\t92"
498 << "23\t\tSTORE_VARIANT\t\t20\t2\t\t\"color(1, 1, 1, 1)\""
499 << "24\t\tSTORE_OBJECT\t\t21"
500 << "25\t\tSTORE_VARIANT_OBJECT\t22"
501 << "26\t\tSTORE_INTERFACE\t\t23"
502 << "27\t\tSTORE_SIGNAL\t\t2\t3\t\t\"console.log(1921)\""
503 << "28\t\tSTORE_SCRIPT_STRING\t24\t3\t1\t4"
504 << "29\t\tASSIGN_SIGNAL_OBJECT\t4\t\t\t\"mySignal\""
505 << "30\t\tASSIGN_CUSTOMTYPE\t25\t6\t9"
506 << "31\t\tSTORE_BINDING\t26\t3\t2"
507 << "32\t\tSTORE_COMPILED_BINDING\t27\t2\t4"
508 << "33\t\tSTORE_VALUE_SOURCE\t29\t4"
509 << "34\t\tSTORE_VALUE_INTERCEPTOR\t30\t-4"
510 << "35\t\tBEGIN\t\t\t4"
511 << "36\t\tSTORE_OBJECT_QLIST"
512 << "37\t\tASSIGN_OBJECT_LIST"
513 << "38\t\tFETCH_ATTACHED\t\t23"
514 << "39\t\tFETCH_QLIST\t\t32"
515 << "40\t\tFETCH\t\t\t33"
516 << "41\t\tFETCH_VALUE\t\t34\t6\t7"
519 << "44\t\tPOP_VALUE\t\t35\t8"
520 << "45\t\tDEFER\t\t\t7"
521 << "46\t\tDEFER\t\t\t7"
522 << "47\t\tSTORE_IMPORTED_SCRIPT\t2"
523 << "48\t\tSTORE_VARIANT_INTEGER\t\t32\t11"
524 << "49\t\tSTORE_VARIANT_DOUBLE\t\t19\t33.7"
526 << "51\t\tSTORE_TR_STRING\t99\t3\t14\t14\t2"
527 << "52\t\tSTORE_TRID_STRING\t78\t7\t-1"
528 << "-------------------------------------------------------------------------------";
530 messages = QStringList();
531 QtMsgHandler old = qInstallMsgHandler(msgHandler);
533 data->dumpInstructions();
534 qInstallMsgHandler(old);
536 QCOMPARE(messages.count(), expect.count());
537 for (int ii = 0; ii < messages.count(); ++ii) {
538 QCOMPARE(messages.at(ii), expect.at(ii));
544 void tst_qdeclarativeinstruction::point()
546 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storePoint::QPoint), sizeof(QPoint));
547 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storePoint::QPoint), Q_ALIGNOF(QPoint));
549 QDeclarativeInstruction i;
550 i.storePoint.point.xp = 8;
551 i.storePoint.point.yp = 11;
553 const QPoint &point = (const QPoint &)(i.storePoint.point);
554 QCOMPARE(point.x(), 8);
555 QCOMPARE(point.y(), 11);
558 void tst_qdeclarativeinstruction::pointf()
560 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storePointF::QPointF), sizeof(QPointF));
561 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storePointF::QPointF), Q_ALIGNOF(QPointF));
563 QDeclarativeInstruction i;
564 i.storePointF.point.xp = 8.7;
565 i.storePointF.point.yp = 11.3;
567 const QPointF &point = (const QPointF &)(i.storePointF.point);
568 QCOMPARE(point.x(), 8.7);
569 QCOMPARE(point.y(), 11.3);
572 void tst_qdeclarativeinstruction::size()
574 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storeSize::QSize), sizeof(QSize));
575 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storeSize::QSize), Q_ALIGNOF(QSize));
577 QDeclarativeInstruction i;
578 i.storeSize.size.wd = 8;
579 i.storeSize.size.ht = 11;
581 const QSize &size = (const QSize &)(i.storeSize.size);
582 QCOMPARE(size.width(), 8);
583 QCOMPARE(size.height(), 11);
586 void tst_qdeclarativeinstruction::sizef()
588 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storeSizeF::QSizeF), sizeof(QSizeF));
589 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storeSizeF::QSizeF), Q_ALIGNOF(QSizeF));
591 QDeclarativeInstruction i;
592 i.storeSizeF.size.wd = 8;
593 i.storeSizeF.size.ht = 11;
595 const QSizeF &size = (const QSizeF &)(i.storeSizeF.size);
596 QCOMPARE(size.width(), (qreal)8);
597 QCOMPARE(size.height(), (qreal)11);
600 void tst_qdeclarativeinstruction::rect()
602 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storeRect::QRect), sizeof(QRect));
603 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storeRect::QRect), Q_ALIGNOF(QRect));
605 QDeclarativeInstruction i;
606 i.storeRect.rect.x1 = 8;
607 i.storeRect.rect.y1 = 11;
608 i.storeRect.rect.x2 = 13;
609 i.storeRect.rect.y2 = 19;
611 const QRect &rect = (const QRect &)(i.storeRect.rect);
612 QCOMPARE(rect.left(), 8);
613 QCOMPARE(rect.top(), 11);
614 QCOMPARE(rect.right(), 13);
615 QCOMPARE(rect.bottom(), 19);
618 void tst_qdeclarativeinstruction::rectf()
620 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storeRectF::QRectF), sizeof(QRectF));
621 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storeRectF::QRectF), Q_ALIGNOF(QRectF));
623 QDeclarativeInstruction i;
624 i.storeRectF.rect.xp = 8;
625 i.storeRectF.rect.yp = 11;
626 i.storeRectF.rect.w = 13;
627 i.storeRectF.rect.h = 19;
629 const QRectF &rect = (const QRectF &)(i.storeRectF.rect);
630 QCOMPARE(rect.left(), (qreal)8);
631 QCOMPARE(rect.top(), (qreal)11);
632 QCOMPARE(rect.width(), (qreal)13);
633 QCOMPARE(rect.height(), (qreal)19);
636 void tst_qdeclarativeinstruction::vector3d()
638 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storeVector3D::QVector3D), sizeof(QVector3D));
639 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storeVector3D::QVector3D), Q_ALIGNOF(QVector3D));
641 QDeclarativeInstruction i;
642 i.storeVector3D.vector.xp = 8.2;
643 i.storeVector3D.vector.yp = 99.3;
644 i.storeVector3D.vector.zp = 12.0;
646 const QVector3D &vector = (const QVector3D &)(i.storeVector3D.vector);
647 QCOMPARE(vector.x(), (qreal)(float)8.2);
648 QCOMPARE(vector.y(), (qreal)(float)99.3);
649 QCOMPARE(vector.z(), (qreal)(float)12.0);
652 void tst_qdeclarativeinstruction::vector4d()
654 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storeVector4D::QVector4D), sizeof(QVector4D));
655 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storeVector4D::QVector4D), Q_ALIGNOF(QVector4D));
657 QDeclarativeInstruction i;
658 i.storeVector4D.vector.xp = 8.2;
659 i.storeVector4D.vector.yp = 99.3;
660 i.storeVector4D.vector.zp = 12.0;
661 i.storeVector4D.vector.wp = 121.1;
663 const QVector4D &vector = (const QVector4D &)(i.storeVector4D.vector);
664 QCOMPARE(vector.x(), (qreal)(float)8.2);
665 QCOMPARE(vector.y(), (qreal)(float)99.3);
666 QCOMPARE(vector.z(), (qreal)(float)12.0);
667 QCOMPARE(vector.w(), (qreal)(float)121.1);
670 void tst_qdeclarativeinstruction::time()
672 QCOMPARE(sizeof(QDeclarativeInstruction::instr_storeTime::QTime), sizeof(QTime));
673 QCOMPARE(Q_ALIGNOF(QDeclarativeInstruction::instr_storeTime::QTime), Q_ALIGNOF(QTime));
676 QTEST_MAIN(tst_qdeclarativeinstruction)
678 #include "tst_qdeclarativeinstruction.moc"