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 <QtDeclarative/qjsengine.h>
44 #include <QtDeclarative/qjsvalue.h>
45 #include <QtDeclarative/qjsvalueiterator.h>
47 class tst_QJSValueIterator : public QObject
52 tst_QJSValueIterator();
53 virtual ~tst_QJSValueIterator();
61 void hasNextAndNext();
63 void constructAndNext_data();
64 void constructAndNext();
68 #if 0 // No string handle
69 void scriptName_data();
83 #if 0 // no array index
84 void iterateArrayAndConvertNameToIndex();
87 void iterateArrayAndDoubleElements();
90 void iterateArrayAndRemoveAllElements();
94 tst_QJSValueIterator::tst_QJSValueIterator()
98 tst_QJSValueIterator::~tst_QJSValueIterator()
102 void tst_QJSValueIterator::init()
106 void tst_QJSValueIterator::cleanup()
110 void tst_QJSValueIterator::dataHelper()
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)"
127 void tst_QJSValueIterator::hasNextAndNext()
130 QJSValue object = engine.newObject();
131 for (int i = 0; i < 2000; ++i)
132 object.setProperty(i, i);
134 for (int i = 0; i < 1000; ++i) {
135 QJSValueIterator it(object);
142 void tst_QJSValueIterator::constructAndNext_data()
147 void tst_QJSValueIterator::constructAndNext()
149 QFETCH(QString, code);
151 QJSValue object = engine.evaluate(code);
152 Q_ASSERT(object.isObject());
155 for (int i = 0; i < 100; ++i) {
156 QJSValueIterator it(object);
162 void tst_QJSValueIterator::name_data()
167 void tst_QJSValueIterator::name()
169 QFETCH(QString, code);
171 QJSValue object = engine.evaluate(code);
172 Q_ASSERT(object.isObject());
174 QJSValueIterator it(object);
177 for (int i = 0; i < 200000; ++i)
183 void tst_QJSValueIterator::scriptName_data()
188 void tst_QJSValueIterator::scriptName()
190 QFETCH(QString, code);
192 QJSValue object = engine.evaluate(code);
193 Q_ASSERT(object.isObject());
195 QJSValueIterator it(object);
198 for (int i = 0; i < 50000; ++i)
204 void tst_QJSValueIterator::value_data()
209 void tst_QJSValueIterator::value()
211 QFETCH(QString, code);
213 QJSValue object = engine.evaluate(code);
214 Q_ASSERT(object.isObject());
216 QJSValueIterator it(object);
219 for (int i = 0; i < 50000; ++i)
225 void tst_QJSValueIterator::setValue_data()
230 void tst_QJSValueIterator::setValue()
232 QFETCH(QString, code);
234 QJSValue object = engine.evaluate(code);
235 Q_ASSERT(object.isObject());
237 QJSValueIterator it(object);
239 QJSValue newValue(&engine, 456);
241 for (int i = 0; i < 50000; ++i)
242 it.setValue(newValue);
246 void tst_QJSValueIterator::flags()
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);
255 for (int i = 0; i < 50000; ++i)
262 void tst_QJSValueIterator::iterateArrayAndConvertNameToIndex()
265 QJSValue array = engine.newArray();
266 for (int i = 0; i < 20000; ++i)
267 array.setProperty(i, i);
269 QJSValueIterator it(array);
270 while (it.hasNext()) {
272 it.scriptName().toArrayIndex();
277 void tst_QJSValueIterator::iterateArrayAndDoubleElements()
280 QJSValue array = engine.newArray();
281 for (int i = 0; i < 20000; ++i)
282 array.setProperty(i, i);
284 QJSValueIterator it(array);
285 while (it.hasNext()) {
287 it.setValue(QJSValue(&engine, it.value().toNumber() * 2));
292 void tst_QJSValueIterator::iterateArrayAndRemoveAllElements()
295 QJSValue array = engine.newArray();
296 for (int i = 0; i < 20000; ++i)
297 array.setProperty(i, i);
299 QJSValueIterator it(array);
300 while (it.hasNext()) {
308 QTEST_MAIN(tst_QJSValueIterator)
309 #include "tst_qjsvalueiterator.moc"