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