Migrate part of QtScript benchmarks to QtDeclarative
[profile/ivi/qtdeclarative.git] / tests / benchmarks / declarative / js / qjsvalueiterator / tst_qjsvalueiterator.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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QtDeclarative/qjsengine.h>
44 #include <QtDeclarative/qjsvalue.h>
45 #include <QtDeclarative/qjsvalueiterator.h>
46
47 //TESTED_FILES=
48
49 class tst_QJSValueIterator : public QObject
50 {
51     Q_OBJECT
52
53 public:
54     tst_QJSValueIterator();
55     virtual ~tst_QJSValueIterator();
56
57     void dataHelper();
58
59 private slots:
60     void init();
61     void cleanup();
62
63     void hasNextAndNext();
64
65     void constructAndNext_data();
66     void constructAndNext();
67
68     void name_data();
69     void name();
70 #if 0 // No string handle
71     void scriptName_data();
72     void scriptName();
73 #endif
74
75     void value_data();
76     void value();
77 #if 0 // no setValue
78     void setValue_data();
79     void setValue();
80 #endif
81 #if 0 // no flags
82     void flags();
83 #endif
84
85 #if 0 // no array index
86     void iterateArrayAndConvertNameToIndex();
87 #endif
88 #if 0 // no setValue
89     void iterateArrayAndDoubleElements();
90 #endif
91 #if 0 // no remove
92     void iterateArrayAndRemoveAllElements();
93 #endif
94 };
95
96 tst_QJSValueIterator::tst_QJSValueIterator()
97 {
98 }
99
100 tst_QJSValueIterator::~tst_QJSValueIterator()
101 {
102 }
103
104 void tst_QJSValueIterator::init()
105 {
106 }
107
108 void tst_QJSValueIterator::cleanup()
109 {
110 }
111
112 void tst_QJSValueIterator::dataHelper()
113 {
114     QTest::addColumn<QString>("code");
115     QTest::newRow("{ foo: 123 }") << QString::fromLatin1("({ foo: 123 })");
116     QTest::newRow("Math") << QString::fromLatin1("Math");
117     QTest::newRow("Array.prototype") << QString::fromLatin1("Array.prototype");
118     QTest::newRow("Global Object") << QString::fromLatin1("this");
119     QTest::newRow("['foo']") << QString::fromLatin1("['foo']");
120     QTest::newRow("array with 1000 elements")
121         << QString::fromLatin1("(function() {"
122                                "  var a = new Array;"
123                                "  for (i = 0; i < 1000; ++i)"
124                                "    a[i] = i;"
125                                "  return a;"
126                                "})()");
127 }
128
129 void tst_QJSValueIterator::hasNextAndNext()
130 {
131     QJSEngine engine;
132     QJSValue object = engine.newObject();
133     for (int i = 0; i < 2000; ++i)
134         object.setProperty(i, i);
135     QBENCHMARK {
136         for (int i = 0; i < 1000; ++i) {
137             QJSValueIterator it(object);
138             while (it.hasNext())
139                 it.next();
140         }
141     }
142 }
143
144 void tst_QJSValueIterator::constructAndNext_data()
145 {
146     dataHelper();
147 }
148
149 void tst_QJSValueIterator::constructAndNext()
150 {
151     QFETCH(QString, code);
152     QJSEngine engine;
153     QJSValue object = engine.evaluate(code);
154     Q_ASSERT(object.isObject());
155
156     QBENCHMARK {
157         for (int i = 0; i < 100; ++i) {
158             QJSValueIterator it(object);
159             it.next();
160         }
161     }
162 }
163
164 void tst_QJSValueIterator::name_data()
165 {
166     dataHelper();
167 }
168
169 void tst_QJSValueIterator::name()
170 {
171     QFETCH(QString, code);
172     QJSEngine engine;
173     QJSValue object = engine.evaluate(code);
174     Q_ASSERT(object.isObject());
175
176     QJSValueIterator it(object);
177     it.next();
178     QBENCHMARK {
179         for (int i = 0; i < 200000; ++i)
180             it.name();
181     }
182 }
183
184 #if 0
185 void tst_QJSValueIterator::scriptName_data()
186 {
187     dataHelper();
188 }
189
190 void tst_QJSValueIterator::scriptName()
191 {
192     QFETCH(QString, code);
193     QJSEngine engine;
194     QJSValue object = engine.evaluate(code);
195     Q_ASSERT(object.isObject());
196
197     QJSValueIterator it(object);
198     it.next();
199     QBENCHMARK {
200         for (int i = 0; i < 50000; ++i)
201             it.scriptName();
202     }
203 }
204 #endif
205
206 void tst_QJSValueIterator::value_data()
207 {
208     dataHelper();
209 }
210
211 void tst_QJSValueIterator::value()
212 {
213     QFETCH(QString, code);
214     QJSEngine engine;
215     QJSValue object = engine.evaluate(code);
216     Q_ASSERT(object.isObject());
217
218     QJSValueIterator it(object);
219     it.next();
220     QBENCHMARK {
221         for (int i = 0; i < 50000; ++i)
222             it.value();
223     }
224 }
225
226 #if 0
227 void tst_QJSValueIterator::setValue_data()
228 {
229     dataHelper();
230 }
231
232 void tst_QJSValueIterator::setValue()
233 {
234     QFETCH(QString, code);
235     QJSEngine engine;
236     QJSValue object = engine.evaluate(code);
237     Q_ASSERT(object.isObject());
238
239     QJSValueIterator it(object);
240     it.next();
241     QJSValue newValue(&engine, 456);
242     QBENCHMARK {
243         for (int i = 0; i < 50000; ++i)
244             it.setValue(newValue);
245     }
246 }
247
248 void tst_QJSValueIterator::flags()
249 {
250     QJSEngine engine;
251     QJSValue object = engine.newObject();
252     QJSValue::PropertyFlags flags = flags;
253     object.setProperty("foo", 123, QJSValue::SkipInEnumeration | QJSValue::ReadOnly | QJSValue::Undeletable);
254     QJSValueIterator it(object);
255     it.next();
256     QBENCHMARK {
257         for (int i = 0; i < 50000; ++i)
258             it.flags();
259     }
260 }
261 #endif
262
263 #if 0
264 void tst_QJSValueIterator::iterateArrayAndConvertNameToIndex()
265 {
266     QJSEngine engine;
267     QJSValue array = engine.newArray();
268     for (int i = 0; i < 20000; ++i)
269         array.setProperty(i, i);
270     QBENCHMARK {
271         QJSValueIterator it(array);
272         while (it.hasNext()) {
273             it.next();
274             it.scriptName().toArrayIndex();
275         }
276     }
277 }
278
279 void tst_QJSValueIterator::iterateArrayAndDoubleElements()
280 {
281     QJSEngine engine;
282     QJSValue array = engine.newArray();
283     for (int i = 0; i < 20000; ++i)
284         array.setProperty(i, i);
285     QBENCHMARK {
286         QJSValueIterator it(array);
287         while (it.hasNext()) {
288             it.next();
289             it.setValue(QJSValue(&engine, it.value().toNumber() * 2));
290         }
291     }
292 }
293
294 void tst_QJSValueIterator::iterateArrayAndRemoveAllElements()
295 {
296     QJSEngine engine;
297     QJSValue array = engine.newArray();
298     for (int i = 0; i < 20000; ++i)
299         array.setProperty(i, i);
300     QBENCHMARK {
301         QJSValueIterator it(array);
302         while (it.hasNext()) {
303             it.next();
304             it.remove();
305         }
306     }
307 }
308 #endif
309
310 QTEST_MAIN(tst_QJSValueIterator)
311 #include "tst_qjsvalueiterator.moc"