7529d2c66de8fc67aa05dfb8bd8e8e1988fa76c8
[profile/ivi/qtdeclarative.git] / tests / benchmarks / declarative / compilation / tst_compilation.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 <qtest.h>
43
44 #include <QtDeclarative/qdeclarativeengine.h>
45 #include <QtDeclarative/qdeclarativecomponent.h>
46 #include <QtDeclarative/private/qdeclarativejsengine_p.h>
47 #include <QtDeclarative/private/qdeclarativejsmemorypool_p.h>
48 #include <QtDeclarative/private/qdeclarativejsparser_p.h>
49 #include <QtDeclarative/private/qdeclarativejslexer_p.h>
50 #include <QtDeclarative/private/qdeclarativescript_p.h>
51
52 #include <QFile>
53 #include <QDebug>
54 #include <QTextStream>
55
56 class tst_compilation : public QObject
57 {
58     Q_OBJECT
59 public:
60     tst_compilation();
61
62 private slots:
63     void boomblock();
64
65     void jsparser_data();
66     void jsparser();
67
68     void scriptparser_data();
69     void scriptparser();
70
71 private:
72     QDeclarativeEngine engine;
73 };
74
75 tst_compilation::tst_compilation()
76 {
77 }
78
79 inline QUrl TEST_FILE(const QString &filename)
80 {
81     return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
82 }
83
84 void tst_compilation::boomblock()
85 {
86     QFile f(SRCDIR + QLatin1String("/data/BoomBlock.qml"));
87     QVERIFY(f.open(QIODevice::ReadOnly));
88     QByteArray data = f.readAll();
89
90     //get rid of initialization effects
91     {
92         QDeclarativeComponent c(&engine);
93         c.setData(data, QUrl());
94     }
95
96     QBENCHMARK {
97         QDeclarativeComponent c(&engine);
98         c.setData(data, QUrl());
99 //        QVERIFY(c.isReady());
100     }
101 }
102
103 void tst_compilation::jsparser_data()
104 {
105     QTest::addColumn<QString>("file");
106
107     QTest::newRow("boomblock") << QString(SRCDIR + QLatin1String("/data/BoomBlock.qml"));
108 }
109
110 void tst_compilation::jsparser()
111 {
112     QFETCH(QString, file);
113
114     QFile f(file);
115     QVERIFY(f.open(QIODevice::ReadOnly));
116     QByteArray data = f.readAll();
117
118     QTextStream stream(data, QIODevice::ReadOnly);
119     const QString code = stream.readAll();
120
121     QBENCHMARK {
122         QDeclarativeJS::Engine engine;
123         QDeclarativeJS::NodePool nodePool(file, &engine);
124
125         QDeclarativeJS::Lexer lexer(&engine);
126         lexer.setCode(code, -1);
127
128         QDeclarativeJS::Parser parser(&engine);
129         parser.parse();
130         parser.ast();
131     }
132 }
133
134 void tst_compilation::scriptparser_data()
135 {
136     QTest::addColumn<QString>("file");
137
138     QTest::newRow("boomblock") << QString(SRCDIR + QLatin1String("/data/BoomBlock.qml"));
139 }
140
141 void tst_compilation::scriptparser()
142 {
143     QFETCH(QString, file);
144
145     QFile f(file);
146     QVERIFY(f.open(QIODevice::ReadOnly));
147     QByteArray data = f.readAll();
148
149     QUrl url = QUrl::fromLocalFile(file);
150
151     QBENCHMARK {
152         QDeclarativeScript::Parser parser;
153         parser.parse(data, url);
154         parser.tree();
155     }
156 }
157
158 QTEST_MAIN(tst_compilation)
159
160 #include "tst_compilation.moc"