1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include <QtDeclarative/qjsengine.h>
44 #include <QtDeclarative/qjsvalue.h>
45 #include <QtDeclarative/qjsvalueiterator.h>
49 class tst_QJSValueIterator : public QObject
54 tst_QJSValueIterator();
55 virtual ~tst_QJSValueIterator();
63 void hasNextAndNext();
65 void constructAndNext_data();
66 void constructAndNext();
70 #if 0 // No string handle
71 void scriptName_data();
85 #if 0 // no array index
86 void iterateArrayAndConvertNameToIndex();
89 void iterateArrayAndDoubleElements();
92 void iterateArrayAndRemoveAllElements();
96 tst_QJSValueIterator::tst_QJSValueIterator()
100 tst_QJSValueIterator::~tst_QJSValueIterator()
104 void tst_QJSValueIterator::init()
108 void tst_QJSValueIterator::cleanup()
112 void tst_QJSValueIterator::dataHelper()
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)"
129 void tst_QJSValueIterator::hasNextAndNext()
132 QJSValue object = engine.newObject();
133 for (int i = 0; i < 2000; ++i)
134 object.setProperty(i, i);
136 for (int i = 0; i < 1000; ++i) {
137 QJSValueIterator it(object);
144 void tst_QJSValueIterator::constructAndNext_data()
149 void tst_QJSValueIterator::constructAndNext()
151 QFETCH(QString, code);
153 QJSValue object = engine.evaluate(code);
154 Q_ASSERT(object.isObject());
157 for (int i = 0; i < 100; ++i) {
158 QJSValueIterator it(object);
164 void tst_QJSValueIterator::name_data()
169 void tst_QJSValueIterator::name()
171 QFETCH(QString, code);
173 QJSValue object = engine.evaluate(code);
174 Q_ASSERT(object.isObject());
176 QJSValueIterator it(object);
179 for (int i = 0; i < 200000; ++i)
185 void tst_QJSValueIterator::scriptName_data()
190 void tst_QJSValueIterator::scriptName()
192 QFETCH(QString, code);
194 QJSValue object = engine.evaluate(code);
195 Q_ASSERT(object.isObject());
197 QJSValueIterator it(object);
200 for (int i = 0; i < 50000; ++i)
206 void tst_QJSValueIterator::value_data()
211 void tst_QJSValueIterator::value()
213 QFETCH(QString, code);
215 QJSValue object = engine.evaluate(code);
216 Q_ASSERT(object.isObject());
218 QJSValueIterator it(object);
221 for (int i = 0; i < 50000; ++i)
227 void tst_QJSValueIterator::setValue_data()
232 void tst_QJSValueIterator::setValue()
234 QFETCH(QString, code);
236 QJSValue object = engine.evaluate(code);
237 Q_ASSERT(object.isObject());
239 QJSValueIterator it(object);
241 QJSValue newValue(&engine, 456);
243 for (int i = 0; i < 50000; ++i)
244 it.setValue(newValue);
248 void tst_QJSValueIterator::flags()
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);
257 for (int i = 0; i < 50000; ++i)
264 void tst_QJSValueIterator::iterateArrayAndConvertNameToIndex()
267 QJSValue array = engine.newArray();
268 for (int i = 0; i < 20000; ++i)
269 array.setProperty(i, i);
271 QJSValueIterator it(array);
272 while (it.hasNext()) {
274 it.scriptName().toArrayIndex();
279 void tst_QJSValueIterator::iterateArrayAndDoubleElements()
282 QJSValue array = engine.newArray();
283 for (int i = 0; i < 20000; ++i)
284 array.setProperty(i, i);
286 QJSValueIterator it(array);
287 while (it.hasNext()) {
289 it.setValue(QJSValue(&engine, it.value().toNumber() * 2));
294 void tst_QJSValueIterator::iterateArrayAndRemoveAllElements()
297 QJSValue array = engine.newArray();
298 for (int i = 0; i < 20000; ++i)
299 array.setProperty(i, i);
301 QJSValueIterator it(array);
302 while (it.hasNext()) {
310 QTEST_MAIN(tst_QJSValueIterator)
311 #include "tst_qjsvalueiterator.moc"