From aac3704b08a4043318a2a6e4df4aca24c7a4f452 Mon Sep 17 00:00:00 2001 From: Jedrzej Nowacki Date: Fri, 5 Aug 2011 17:21:20 +0200 Subject: Migrate part of QtScript benchmarks to QtDeclarative Benchmarks for QJSValue QJSEngine and for QJSValueIterator were added. Change-Id: Ie52a3e8f5a461add3269d41d6e9a1a9bfb0c7abd Reviewed-on: http://codereview.qt.nokia.com/2705 Reviewed-by: Qt Sanity Bot Reviewed-by: Simon Hausmann --- tests/benchmarks/declarative/declarative.pro | 3 +- tests/benchmarks/declarative/js/js.pro | 10 + .../declarative/js/qjsengine/qjsengine.pro | 12 + .../declarative/js/qjsengine/tst_qjsengine.cpp | 677 +++++++++++++ .../declarative/js/qjsvalue/qjsvalue.pro | 7 + .../declarative/js/qjsvalue/tst_qjsvalue.cpp | 1061 ++++++++++++++++++++ .../js/qjsvalueiterator/qjsvalueiterator.pro | 7 + .../js/qjsvalueiterator/tst_qjsvalueiterator.cpp | 311 ++++++ 8 files changed, 2087 insertions(+), 1 deletion(-) create mode 100644 tests/benchmarks/declarative/js/js.pro create mode 100644 tests/benchmarks/declarative/js/qjsengine/qjsengine.pro create mode 100644 tests/benchmarks/declarative/js/qjsengine/tst_qjsengine.cpp create mode 100644 tests/benchmarks/declarative/js/qjsvalue/qjsvalue.pro create mode 100644 tests/benchmarks/declarative/js/qjsvalue/tst_qjsvalue.cpp create mode 100644 tests/benchmarks/declarative/js/qjsvalueiterator/qjsvalueiterator.pro create mode 100644 tests/benchmarks/declarative/js/qjsvalueiterator/tst_qjsvalueiterator.cpp diff --git a/tests/benchmarks/declarative/declarative.pro b/tests/benchmarks/declarative/declarative.pro index f2dfdf971a..820d84b0a2 100644 --- a/tests/benchmarks/declarative/declarative.pro +++ b/tests/benchmarks/declarative/declarative.pro @@ -10,7 +10,8 @@ SUBDIRS += \ qdeclarativeimage \ qdeclarativemetaproperty \ script \ - qmltime + qmltime \ + js contains(QT_CONFIG, opengl): SUBDIRS += painting diff --git a/tests/benchmarks/declarative/js/js.pro b/tests/benchmarks/declarative/js/js.pro new file mode 100644 index 0000000000..b1448d8eb8 --- /dev/null +++ b/tests/benchmarks/declarative/js/js.pro @@ -0,0 +1,10 @@ +TEMPLATE = subdirs +SUBDIRS = \ + qjsengine \ + qjsvalue \ + qjsvalueiterator \ + +TRUSTED_BENCHMARKS += \ + qjsvalue \ + qjsengine \ + diff --git a/tests/benchmarks/declarative/js/qjsengine/qjsengine.pro b/tests/benchmarks/declarative/js/qjsengine/qjsengine.pro new file mode 100644 index 0000000000..cbd128bf97 --- /dev/null +++ b/tests/benchmarks/declarative/js/qjsengine/qjsengine.pro @@ -0,0 +1,12 @@ +load(qttest_p4) +TEMPLATE = app +TARGET = tst_bench_qjsengine + +SOURCES += tst_qjsengine.cpp + +QT += declarative + +symbian* { + TARGET.EPOCHEAPSIZE = 0x20000 0x2000000 // Min 128kB, Max 32MB + TARGET.EPOCSTACKSIZE = 0x14000 +} diff --git a/tests/benchmarks/declarative/js/qjsengine/tst_qjsengine.cpp b/tests/benchmarks/declarative/js/qjsengine/tst_qjsengine.cpp new file mode 100644 index 0000000000..ba486df2b1 --- /dev/null +++ b/tests/benchmarks/declarative/js/qjsengine/tst_qjsengine.cpp @@ -0,0 +1,677 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + + +Q_DECLARE_METATYPE(QJSValue) + +//TESTED_FILES= + +class tst_QJSEngine : public QObject +{ + Q_OBJECT + +public: + tst_QJSEngine(); + virtual ~tst_QJSEngine(); + +public slots: + void init(); + void cleanup(); + +private slots: + void constructor(); +#if 0 // No defaultPrototype for now + void defaultPrototype(); + void setDefaultPrototype(); +#endif + void evaluate_data(); + void evaluate(); +#if 0 // No program + void evaluateProgram_data(); + void evaluateProgram(); +#endif +#if 0 // no connections for now + void connectAndDisconnect(); +#endif + void globalObject(); + void hasUncaughtException(); +#if 0 // no is Evaluating for now + void isEvaluating(); +#endif + void newArray_data(); + void newArray(); + void newDate(); + void newDateFromMs(); + void newObject(); +#if 0 // No ScriptClass + void newObjectWithScriptClass(); +#endif +#if 0 // no qmetaobject + void newQMetaObject(); +#endif + void newQObject(); +#if 0 // no native functions for now + void newFunction(); +#endif + void newRegExp(); + void newRegExpFromString(); + void newVariant(); + void nullValue(); + void undefinedValue(); + void collectGarbage(); +#if 0 // No extensions + void availableExtensions(); + void importedExtensions(); +#endif +#if 0 // no context + void currentContext(); + void pushAndPopContext(); +#endif + void toObject_data(); + void toObject(); +#if 0 // no stringhandle + void toStringHandle(); +#endif + void castValueToQreal(); +#if 0 // no native functions for now + void nativeCall(); +#endif +#if 0 // no translations + void installTranslatorFunctions(); + void translation_data(); + void translation(); +#endif +#if 0 // no declarative class + void readScopeProperty_data(); + void readScopeProperty(); +#endif +#if 0 // no context + void evaluateInNewContext(); + void evaluateInNewContextWithScope(); +#endif +#if 0 // no pushScope + void evaluateBindingExpression(); +#endif + +private: + void defineStandardTestValues(); + void newEngine() + { + delete m_engine; + m_engine = new QJSEngine; + } + + QJSEngine *m_engine; +}; + +tst_QJSEngine::tst_QJSEngine() + : m_engine(0) +{ +} + +tst_QJSEngine::~tst_QJSEngine() +{ + delete m_engine; +} + +void tst_QJSEngine::init() +{ +} + +void tst_QJSEngine::cleanup() +{ +} + +void tst_QJSEngine::constructor() +{ + QBENCHMARK { + QJSEngine engine; + (void)engine.parent(); + } +} + +#if 0 // No defaultPrototype for now +void tst_QJSEngine::defaultPrototype() +{ + newEngine(); + int type = qMetaTypeId(); + m_engine->setDefaultPrototype(type, m_engine->newObject()); + QBENCHMARK { + m_engine->defaultPrototype(type); + } +} + +void tst_QJSEngine::setDefaultPrototype() +{ + newEngine(); + int type = qMetaTypeId(); + QJSValue proto = m_engine->newObject(); + QBENCHMARK { + m_engine->setDefaultPrototype(type, proto); + } +} + +#endif + +void tst_QJSEngine::evaluate_data() +{ + QTest::addColumn("code"); + QTest::newRow("empty script") << QString::fromLatin1(""); + QTest::newRow("number literal") << QString::fromLatin1("123"); + QTest::newRow("string literal") << QString::fromLatin1("'ciao'"); + QTest::newRow("regexp literal") << QString::fromLatin1("/foo/gim"); + QTest::newRow("null literal") << QString::fromLatin1("null"); + QTest::newRow("undefined literal") << QString::fromLatin1("undefined"); + QTest::newRow("null literal") << QString::fromLatin1("null"); + QTest::newRow("empty object literal") << QString::fromLatin1("{}"); + QTest::newRow("this") << QString::fromLatin1("this"); + QTest::newRow("object literal with one property") << QString::fromLatin1("{ foo: 123 }"); + QTest::newRow("object literal with two properties") << QString::fromLatin1("{ foo: 123, bar: 456 }"); + QTest::newRow("object literal with many properties") << QString::fromLatin1("{ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }"); + QTest::newRow("empty array literal") << QString::fromLatin1("[]"); + QTest::newRow("array literal with one element") << QString::fromLatin1("[1]"); + QTest::newRow("array literal with two elements") << QString::fromLatin1("[1,2]"); + QTest::newRow("array literal with many elements") << QString::fromLatin1("[1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1]"); + QTest::newRow("empty function definition") << QString::fromLatin1("function foo() { }"); + QTest::newRow("function definition") << QString::fromLatin1("function foo() { return 123; }"); + QTest::newRow("for loop with empty body (1000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000; ++i) {}"); + QTest::newRow("for loop with empty body (10000 iterations)") << QString::fromLatin1("for (i = 0; i < 10000; ++i) {}"); + QTest::newRow("for loop with empty body (100000 iterations)") << QString::fromLatin1("for (i = 0; i < 100000; ++i) {}"); + QTest::newRow("for loop with empty body (1000000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000000; ++i) {}"); + QTest::newRow("for loop (1000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000; ++i) { j += i; }; j"); + QTest::newRow("for loop (10000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 10000; ++i) { j += i; }; j"); + QTest::newRow("for loop (100000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 100000; ++i) { j += i; }; j"); + QTest::newRow("for loop (1000000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000000; ++i) { j += i; }; j"); + QTest::newRow("assignments") << QString::fromLatin1("a = 1; b = 2; c = 3; d = 4"); + QTest::newRow("while loop (1000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000) { ++i; }; i"); + QTest::newRow("while loop (10000 iterations)") << QString::fromLatin1("i = 0; while (i < 10000) { ++i; }; i"); + QTest::newRow("while loop (100000 iterations)") << QString::fromLatin1("i = 0; while (i < 100000) { ++i; }; i"); + QTest::newRow("while loop (1000000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000000) { ++i; }; i"); + QTest::newRow("function expression") << QString::fromLatin1("(function(a, b, c){ return a + b + c; })(1, 2, 3)"); +} + +void tst_QJSEngine::evaluate() +{ + QFETCH(QString, code); + newEngine(); + + QBENCHMARK { + (void)m_engine->evaluate(code); + } +} + +#if 0 +void tst_QJSEngine::connectAndDisconnect() +{ + newEngine(); + QJSValue fun = m_engine->evaluate("(function() { })"); + QBENCHMARK { + qScriptConnect(m_engine, SIGNAL(destroyed()), QJSValue(), fun); + qScriptDisconnect(m_engine, SIGNAL(destroyed()), QJSValue(), fun); + } +} + +void tst_QJSEngine::evaluateProgram_data() +{ + evaluate_data(); +} + +void tst_QJSEngine::evaluateProgram() +{ + QFETCH(QString, code); + QScriptProgram program(code); + newEngine(); + + QBENCHMARK { + (void)m_engine->evaluate(program); + } +} +#endif + +void tst_QJSEngine::globalObject() +{ + newEngine(); + QBENCHMARK { + m_engine->globalObject(); + } +} + +void tst_QJSEngine::hasUncaughtException() +{ + newEngine(); + QBENCHMARK { + m_engine->hasUncaughtException(); + } +} + +#if 0 +void tst_QJSEngine::isEvaluating() +{ + newEngine(); + QBENCHMARK { + m_engine->isEvaluating(); + } +} +#endif + +void tst_QJSEngine::newArray_data() +{ + QTest::addColumn("size"); + QTest::newRow("size=0") << 0; + QTest::newRow("size=10") << 10; + QTest::newRow("size=100") << 0; + QTest::newRow("size=1000") << 0; + QTest::newRow("size=10000") << 0; + QTest::newRow("size=50000") << 0; +} + +void tst_QJSEngine::newArray() +{ + QFETCH(int, size); + newEngine(); + QBENCHMARK { + m_engine->newArray(size); + } +} + +void tst_QJSEngine::newDate() +{ + newEngine(); + QDateTime dt = QDateTime::currentDateTime(); + QBENCHMARK { + m_engine->newDate(dt); + } +} + +void tst_QJSEngine::newDateFromMs() +{ + newEngine(); + QBENCHMARK { + m_engine->newDate(0); + } +} + +void tst_QJSEngine::newObject() +{ + newEngine(); + QBENCHMARK { + (void)m_engine->newObject(); + } +} + +#if 0 +void tst_QJSEngine::newObjectWithScriptClass() +{ + newEngine(); + QScriptClass cls(m_engine); + QBENCHMARK { + m_engine->newObject(&cls); + } +} + +void tst_QJSEngine::newQMetaObject() +{ + newEngine(); + QBENCHMARK { + m_engine->newQMetaObject(&QJSEngine::staticMetaObject); + } +} +#endif + +void tst_QJSEngine::newQObject() +{ + newEngine(); + QBENCHMARK { + (void)m_engine->newQObject(QCoreApplication::instance()); + } +} + +#if 0 +static QJSValue testFunction(QScriptContext *, QJSEngine *) +{ + return 0; +} + +void tst_QJSEngine::newFunction() +{ + newEngine(); + QBENCHMARK { + (void)m_engine->newFunction(testFunction); + } +} +#endif + +void tst_QJSEngine::newRegExp() +{ + newEngine(); + QRegExp re = QRegExp("foo"); + QBENCHMARK { + m_engine->newRegExp(re); + } +} + +void tst_QJSEngine::newRegExpFromString() +{ + newEngine(); + QString pattern("foo"); + QString flags("gim"); + QBENCHMARK { + m_engine->newRegExp(pattern, flags); + } +} + +void tst_QJSEngine::newVariant() +{ + newEngine(); + QVariant var(123); + QBENCHMARK { + (void)m_engine->newVariant(var); + } +} + +void tst_QJSEngine::nullValue() +{ + newEngine(); + QBENCHMARK { + m_engine->nullValue(); + } +} + +void tst_QJSEngine::undefinedValue() +{ + newEngine(); + QBENCHMARK { + m_engine->undefinedValue(); + } +} + +void tst_QJSEngine::collectGarbage() +{ + newEngine(); + QBENCHMARK { + m_engine->collectGarbage(); + } +} + +#if 0 +void tst_QJSEngine::availableExtensions() +{ + newEngine(); + QBENCHMARK { + m_engine->availableExtensions(); + } +} + +void tst_QJSEngine::importedExtensions() +{ + newEngine(); + QBENCHMARK { + m_engine->importedExtensions(); + } +} + +void tst_QJSEngine::currentContext() +{ + newEngine(); + QBENCHMARK { + m_engine->currentContext(); + } +} + +void tst_QJSEngine::pushAndPopContext() +{ + newEngine(); + QBENCHMARK { + (void)m_engine->pushContext(); + m_engine->popContext(); + } +} +#endif + +void tst_QJSEngine::toObject_data() +{ + newEngine(); + QTest::addColumn("val"); + QTest::newRow("bool") << m_engine->evaluate("true"); + QTest::newRow("number") << m_engine->evaluate("123"); + QTest::newRow("string") << m_engine->evaluate("'ciao'"); + QTest::newRow("null") << m_engine->evaluate("null"); + QTest::newRow("undefined") << m_engine->evaluate("undefined"); + QTest::newRow("object") << m_engine->evaluate("({foo:123})"); + QTest::newRow("array") << m_engine->evaluate("[10,20,30]"); + QTest::newRow("function") << m_engine->evaluate("(function foo(a, b, c) { return a + b + c; })"); + QTest::newRow("date") << m_engine->evaluate("new Date"); + QTest::newRow("regexp") << m_engine->evaluate("new RegExp('foo')"); + QTest::newRow("error") << m_engine->evaluate("new Error"); + + QTest::newRow("qobject") << m_engine->newQObject(this); +#if 0 // no QMetaObject + QTest::newRow("qmetaobject") << m_engine->newQMetaObject(&QJSEngine::staticMetaObject); +#endif + QTest::newRow("variant") << m_engine->newVariant(123); +#if 0 //no classes + QTest::newRow("qscriptclassobject") << m_engine->newObject(new QScriptClass(m_engine)); +#endif + QTest::newRow("invalid") << QJSValue(); + QTest::newRow("bool-no-engine") << QJSValue(true); + QTest::newRow("number-no-engine") << QJSValue(123.0); + QTest::newRow("string-no-engine") << QJSValue(QString::fromLatin1("hello")); + QTest::newRow("null-no-engine") << QJSValue(QJSValue::NullValue); + QTest::newRow("undefined-no-engine") << QJSValue(QJSValue::UndefinedValue); +} + +void tst_QJSEngine::toObject() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + m_engine->toObject(val); + } +} + +#if 0 +void tst_QJSEngine::toStringHandle() +{ + newEngine(); + QString str = QString::fromLatin1("foobarbaz"); + QBENCHMARK { + (void)m_engine->toStringHandle(str); + } +} +#endif + +void tst_QJSEngine::castValueToQreal() +{ + QJSValue val(123); + QBENCHMARK { + (void)qjsvalue_cast(val); + } +} + +#if 0 +static QJSValue native_function(QScriptContext *, QJSEngine *) +{ + return 42; +} + +void tst_QJSEngine::nativeCall() +{ + newEngine(); + m_engine->globalObject().setProperty("fun", m_engine->newFunction(native_function)); + QBENCHMARK{ +#if !defined(Q_OS_SYMBIAN) + m_engine->evaluate("var w = 0; for (i = 0; i < 100000; ++i) {\n" + " w += fun() + fun(); w -= fun(); fun(); w -= fun(); }"); +#else + m_engine->evaluate("var w = 0; for (i = 0; i < 25000; ++i) {\n" + " w += fun() + fun(); w -= fun(); fun(); w -= fun(); }"); +#endif + } +} + +void tst_QJSEngine::installTranslatorFunctions() +{ + newEngine(); + QBENCHMARK { + m_engine->installTranslatorFunctions(); + } +} + +void tst_QJSEngine::translation_data() +{ + QTest::addColumn("text"); + QTest::addColumn("fileName"); + QTest::newRow("no translation") << "\"hello world\"" << ""; + QTest::newRow("qsTr") << "qsTr(\"hello world\")" << ""; + QTest::newRow("qsTranslate") << "qsTranslate(\"\", \"hello world\")" << ""; + QTest::newRow("qsTr:script.js") << "qsTr(\"hello world\")" << "script.js"; +} + +void tst_QJSEngine::translation() +{ + QFETCH(QString, text); + QFETCH(QString, fileName); + newEngine(); + m_engine->installTranslatorFunctions(); + + QBENCHMARK { + (void)m_engine->evaluate(text, fileName); + } +} +#endif + +#if 0 +void tst_QJSEngine::readScopeProperty_data() +{ + QTest::addColumn("staticScope"); + QTest::addColumn("nestedScope"); + QTest::newRow("single dynamic scope") << false << false; + QTest::newRow("single static scope") << true << false; + QTest::newRow("double dynamic scope") << false << true; + QTest::newRow("double static scope") << true << true; +} + +void tst_QJSEngine::readScopeProperty() +{ + QFETCH(bool, staticScope); + QFETCH(bool, nestedScope); + + newEngine(); + QScriptContext *ctx = m_engine->pushContext(); + + QJSValue scope; + if (staticScope) + scope = QScriptDeclarativeClass::newStaticScopeObject(m_engine); + else + scope = m_engine->newObject(); + scope.setProperty("foo", 123); + ctx->pushScope(scope); + + if (nestedScope) { + QJSValue scope2; + if (staticScope) + scope2 = QScriptDeclarativeClass::newStaticScopeObject(m_engine); + else + scope2 = m_engine->newObject(); + scope2.setProperty("bar", 456); // ensure a miss in inner scope + ctx->pushScope(scope2); + } + + QJSValue fun = m_engine->evaluate("(function() {\n" + " for (var i = 0; i < 10000; ++i) {\n" + " foo; foo; foo; foo; foo; foo; foo; foo;\n" + " }\n" + "})"); + m_engine->popContext(); + QVERIFY(fun.isFunction()); + QBENCHMARK { + fun.call(); + } +} + +void tst_QJSEngine::evaluateInNewContext() +{ + QJSEngine engine; + QBENCHMARK { + engine.pushContext(); + engine.evaluate("var a = 10"); + engine.popContext(); + } +} + +void tst_QJSEngine::evaluateInNewContextWithScope() +{ + QJSEngine engine; + QJSValue scope = engine.newObject(); + scope.setProperty("foo", 123); + QBENCHMARK { + QScriptContext *ctx = engine.pushContext(); + ctx->pushScope(scope); + engine.evaluate("foo"); + engine.popContext(); + } +} + +// Binding expressions in QML are implemented as anonymous functions +// with custom scopes. +void tst_QJSEngine::evaluateBindingExpression() +{ + QJSEngine engine; + QScriptContext *ctx = engine.pushContext(); + QJSValue scope = engine.newObject(); + scope.setProperty("foo", 123); + ctx->pushScope(scope); + QJSValue fun = engine.evaluate("(function() { return foo; })"); + QVERIFY(fun.isFunction()); + engine.popContext(); + QVERIFY(fun.call().equals(scope.property("foo"))); + QJSValue receiver = engine.globalObject(); + QBENCHMARK { + fun.call(receiver); + } +} +#endif + +QTEST_MAIN(tst_QJSEngine) +#include "tst_qjsengine.moc" diff --git a/tests/benchmarks/declarative/js/qjsvalue/qjsvalue.pro b/tests/benchmarks/declarative/js/qjsvalue/qjsvalue.pro new file mode 100644 index 0000000000..48c40a2a52 --- /dev/null +++ b/tests/benchmarks/declarative/js/qjsvalue/qjsvalue.pro @@ -0,0 +1,7 @@ +load(qttest_p4) +TEMPLATE = app +TARGET = tst_bench_qjsvalue + +SOURCES += tst_qjsvalue.cpp + +QT += declarative diff --git a/tests/benchmarks/declarative/js/qjsvalue/tst_qjsvalue.cpp b/tests/benchmarks/declarative/js/qjsvalue/tst_qjsvalue.cpp new file mode 100644 index 0000000000..afe6d1c4db --- /dev/null +++ b/tests/benchmarks/declarative/js/qjsvalue/tst_qjsvalue.cpp @@ -0,0 +1,1061 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +Q_DECLARE_METATYPE(QJSValue) + +//TESTED_FILES= + +class tst_QJSValue : public QObject +{ + Q_OBJECT + +public: + tst_QJSValue(); + virtual ~tst_QJSValue(); + +public slots: + void init(); + void cleanup(); + +private slots: + void boolConstructor(); + void floatConstructor(); + void numberConstructor(); + void stringConstructor(); + void nullConstructor(); + void undefinedConstructor(); + void boolConstructorWithEngine(); + void floatConstructorWithEngine(); + void intConstructorWithEngine(); + void stringConstructorWithEngine(); + void nullConstructorWithEngine(); + void undefinedConstructorWithEngine(); + void copyConstructor_data(); + void copyConstructor(); + void call_data(); + void call(); + void construct_data(); + void construct(); +#if 0 // no data + void data(); + void setData(); + void data_noData_data(); + void data_noData(); +#endif + void engine_data(); + void engine(); + void equalsSelf_data(); + void equalsSelf(); +#if 0 // no less then + void lessThanSelf_data(); + void lessThanSelf(); +#endif + void strictlyEqualsSelf_data(); + void strictlyEqualsSelf(); + void instanceOf(); + void isArray_data(); + void isArray(); + void isBool_data(); + void isBool(); + void isDate_data(); + void isDate(); + void isError_data(); + void isError(); + void isFunction_data(); + void isFunction(); + void isNull_data(); + void isNull(); + void isNumber_data(); + void isNumber(); + void isObject_data(); + void isObject(); +#if 0 // no qmetaobject + void isQMetaObject_data(); + void isQMetaObject(); +#endif + void isQObject_data(); + void isQObject(); + void isRegExp_data(); + void isRegExp(); + void isString_data(); + void isString(); + void isUndefined_data(); + void isUndefined(); + void isValid_data(); + void isValid(); + void isVariant_data(); + void isVariant(); + void toBool_data(); + void toBool(); + void toDateTime_data(); + void toDateTime(); + void toInt32_data(); + void toInt32(); + void toInteger_data(); + void toInteger(); + void toNumber_data(); + void toNumber(); + void toRegExp_data(); + void toRegExp(); + void toString_data(); + void toString(); + void toUInt16_data(); + void toUInt16(); + void toUInt32_data(); + void toUInt32(); +#if 0 // no qmetaobject + void toQMetaObject_data(); + void toQMetaObject(); +#endif + void toQObject_data(); + void toQObject(); + void toVariant_data(); + void toVariant(); + void property_data(); + void property(); +#if 0 // no string handle + void propertyById_data(); + void propertyById(); +#endif + void propertyByIndex(); + void setProperty_data(); + void setProperty(); +#if 0 // no string handle + void setPropertyById_data(); + void setPropertyById(); +#endif + void setPropertyByIndex(); +#if 0 // no propertyFlags for now + void propertyFlags_data(); + void propertyFlags(); + void propertyFlagsById_data(); + void propertyFlagsById(); +#endif + void prototype_data(); + void prototype(); + void setPrototype(); +#if 0 // no script class + void scriptClass_data(); + void scriptClass(); + void setScriptClass(); +#endif +#if 0 // no string handle + void readMetaProperty(); + void writeMetaProperty(); +#endif + +private: + void defineStandardTestValues(); + void newEngine() + { + delete m_engine; + m_engine = new QJSEngine; + } + + QJSEngine *m_engine; +}; + +tst_QJSValue::tst_QJSValue() + : m_engine(0) +{ +} + +tst_QJSValue::~tst_QJSValue() +{ + delete m_engine; +} + +void tst_QJSValue::init() +{ +} + +void tst_QJSValue::cleanup() +{ +} + +void tst_QJSValue::boolConstructor() +{ + QBENCHMARK { + QJSValue val(true); + } +} + +void tst_QJSValue::floatConstructor() +{ + QBENCHMARK { + QJSValue val(123.0); + } +} + +void tst_QJSValue::numberConstructor() +{ + QBENCHMARK { + (void)QJSValue(123); + } +} + +void tst_QJSValue::stringConstructor() +{ + QString str = QString::fromLatin1("ciao"); + QBENCHMARK { + (void)QJSValue(str); + } +} + +void tst_QJSValue::nullConstructor() +{ + QBENCHMARK { + QJSValue val(QJSValue::NullValue); + } +} + +void tst_QJSValue::undefinedConstructor() +{ + QBENCHMARK { + QJSValue val(QJSValue::UndefinedValue); + } +} + +void tst_QJSValue::boolConstructorWithEngine() +{ + newEngine(); + QBENCHMARK { + QJSValue val(m_engine, true); + } +} + +void tst_QJSValue::floatConstructorWithEngine() +{ + newEngine(); + QBENCHMARK { + QJSValue val(m_engine, 123.0); + } +} + +void tst_QJSValue::intConstructorWithEngine() +{ + newEngine(); + QBENCHMARK { + (void)QJSValue(m_engine, 123); + } +} + +void tst_QJSValue::stringConstructorWithEngine() +{ + newEngine(); + QString str = QString::fromLatin1("ciao"); + QBENCHMARK { + (void)QJSValue(m_engine, str); + } +} + +void tst_QJSValue::nullConstructorWithEngine() +{ + newEngine(); + QBENCHMARK { + QJSValue val(m_engine, QJSValue::NullValue); + } +} + +void tst_QJSValue::undefinedConstructorWithEngine() +{ + newEngine(); + QBENCHMARK { + QJSValue val(m_engine, QJSValue::UndefinedValue); + } +} + +void tst_QJSValue::copyConstructor_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::copyConstructor() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + QJSValue copy(val); + } +} + +void tst_QJSValue::call_data() +{ + newEngine(); + QTest::addColumn("code"); + QTest::newRow("empty function") << QString::fromLatin1("(function(){})"); + QTest::newRow("function returning number") << QString::fromLatin1("(function(){ return 123; })"); + QTest::newRow("closure") << QString::fromLatin1("(function(a, b){ return function() { return a + b; }; })(1, 2)"); +} + +void tst_QJSValue::call() +{ + QFETCH(QString, code); + QJSValue fun = m_engine->evaluate(code); + QVERIFY(fun.isFunction()); + QBENCHMARK { + (void)fun.call(); + } +} + +void tst_QJSValue::construct_data() +{ + newEngine(); + QTest::addColumn("code"); + QTest::newRow("empty function") << QString::fromLatin1("(function(){})"); + QTest::newRow("simple constructor") << QString::fromLatin1("(function(){ this.x = 10; this.y = 20; })"); +} + +void tst_QJSValue::construct() +{ + QFETCH(QString, code); + QJSValue fun = m_engine->evaluate(code); + QVERIFY(fun.isFunction()); + QBENCHMARK { + (void)fun.construct(); + } +} + +#if 0 +void tst_QJSValue::data() +{ + newEngine(); + QJSValue obj = m_engine->newObject(); + obj.setData(QJSValue(m_engine, 123)); + QBENCHMARK { + obj.data(); + } +} + +void tst_QJSValue::setData() +{ + newEngine(); + QJSValue obj = m_engine->newObject(); + QJSValue val(m_engine, 123); + QBENCHMARK { + obj.setData(val); + } +} + +void tst_QJSValue::data_noData_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::data_noData() +{ + QFETCH(QJSValue, val); + QVERIFY(!val.data().isValid()); + QBENCHMARK { + val.data(); + } +} +#endif + +void tst_QJSValue::engine_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::engine() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.engine(); + } +} + +void tst_QJSValue::equalsSelf_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::equalsSelf() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.equals(val); + } +} + +#if 0 +void tst_QJSValue::lessThanSelf_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::lessThanSelf() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.lessThan(val); + } +} +#endif + +void tst_QJSValue::strictlyEqualsSelf_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::strictlyEqualsSelf() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.strictlyEquals(val); + } +} + +void tst_QJSValue::instanceOf() +{ + newEngine(); + QJSValue arrayCtor = m_engine->globalObject().property("Array"); + QJSValue array = arrayCtor.construct(); + QVERIFY(array.instanceOf(arrayCtor)); + QBENCHMARK { + array.instanceOf(arrayCtor); + } +} + +void tst_QJSValue::isArray_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isArray() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isArray(); + } +} + +void tst_QJSValue::isBool_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isBool() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isBool(); + } +} + +void tst_QJSValue::isDate_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isDate() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isDate(); + } +} + +void tst_QJSValue::isError_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isError() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isError(); + } +} + +void tst_QJSValue::isFunction_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isFunction() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isFunction(); + } +} + +void tst_QJSValue::isNull_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isNull() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isNull(); + } +} + +void tst_QJSValue::isNumber_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isNumber() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isNumber(); + } +} + +void tst_QJSValue::isObject_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isObject() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isObject(); + } +} + +#if 0 +void tst_QJSValue::isQMetaObject_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isQMetaObject() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isQMetaObject(); + } +} +#endif + +void tst_QJSValue::isQObject_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isQObject() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isQObject(); + } +} + +void tst_QJSValue::isRegExp_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isRegExp() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isRegExp(); + } +} + +void tst_QJSValue::isString_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isString() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isString(); + } +} + +void tst_QJSValue::isUndefined_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isUndefined() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isUndefined(); + } +} + +void tst_QJSValue::isValid_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isValid() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isValid(); + } +} + +void tst_QJSValue::isVariant_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::isVariant() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.isVariant(); + } +} + +void tst_QJSValue::toBool_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toBool() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toBool(); + } +} + +void tst_QJSValue::toDateTime_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toDateTime() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toDateTime(); + } +} + +void tst_QJSValue::toInt32_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toInt32() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toInt32(); + } +} + +void tst_QJSValue::toInteger_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toInteger() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toInteger(); + } +} + +void tst_QJSValue::toNumber_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toNumber() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toNumber(); + } +} + +void tst_QJSValue::toRegExp_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toRegExp() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toRegExp(); + } +} + +void tst_QJSValue::toString_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toString() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + (void)val.toString(); + } +} + +#if 0 +void tst_QJSValue::toQMetaObject_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toQMetaObject() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toQMetaObject(); + } +} +#endif + +void tst_QJSValue::toQObject_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toQObject() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + (void)val.toQObject(); + } +} + +void tst_QJSValue::toUInt16_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toUInt16() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toUInt16(); + } +} + +void tst_QJSValue::toUInt32_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toUInt32() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toUInt32(); + } +} + +void tst_QJSValue::toVariant_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::toVariant() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.toVariant(); + } +} +void tst_QJSValue::property_data() +{ + QTest::addColumn("propertyName"); + QTest::addColumn("create"); + QTest::newRow("foo") << QString::fromLatin1("foo") << true; + QTest::newRow("hasOwnProperty") << QString::fromLatin1("hasOwnProperty") << false; // From Object.prototype. + QTest::newRow("noSuchProperty") << QString::fromLatin1("noSuchProperty") << false; +} + +void tst_QJSValue::property() +{ + QFETCH(QString, propertyName); + QFETCH(bool, create); + newEngine(); + QJSValue obj = m_engine->newObject(); + if (create) + obj.setProperty(propertyName, 123); + QBENCHMARK { + (void)obj.property(propertyName); + } +} + +#if 0 +void tst_QJSValue::propertyById_data() +{ + property_data(); +} + +void tst_QJSValue::propertyById() +{ + QFETCH(QString, propertyName); + QFETCH(bool, create); + newEngine(); + QJSValue obj = m_engine->newObject(); + QJSString id = m_engine->toStringHandle(propertyName); + if (create) + obj.setProperty(id, 123); + QBENCHMARK { + obj.property(id); + } +} +#endif + +void tst_QJSValue::propertyByIndex() +{ + newEngine(); + QJSValue obj = m_engine->newObject(); + obj.setProperty(123, 456); + QBENCHMARK { + obj.property(123); + } +} + +void tst_QJSValue::setProperty_data() +{ + newEngine(); + QTest::addColumn("propertyName"); + QTest::addColumn("val"); + QTest::newRow("foo") << QString::fromLatin1("foo") << QJSValue(123); + QTest::newRow("bar") << QString::fromLatin1("bar") << QJSValue(m_engine, 123); + QTest::newRow("baz") << QString::fromLatin1("baz") << QJSValue(); + QTest::newRow("toString") << QString::fromLatin1("toString") << QJSValue(m_engine, true); +} + +void tst_QJSValue::setProperty() +{ + QFETCH(QString, propertyName); + QFETCH(QJSValue, val); + QJSValue obj = m_engine->newObject(); + QBENCHMARK { + obj.setProperty(propertyName, val); + } +} + +#if 0 +void tst_QJSValue::setPropertyById_data() +{ + setProperty_data(); +} + +void tst_QJSValue::setPropertyById() +{ + QFETCH(QString, propertyName); + QFETCH(QJSValue, val); + QJSValue obj = m_engine->newObject(); + QJSString id = m_engine->toStringHandle(propertyName); + QBENCHMARK { + obj.setProperty(id, val); + } +} +#endif + +void tst_QJSValue::setPropertyByIndex() +{ + newEngine(); + QJSValue obj = m_engine->newObject(); + QJSValue val(456); + QBENCHMARK { + obj.setProperty(123, 456); + } +} + +#if 0 +void tst_QJSValue::propertyFlags_data() +{ + property_data(); +} + +void tst_QJSValue::propertyFlags() +{ + QFETCH(QString, propertyName); + QFETCH(bool, create); + newEngine(); + QJSValue obj = m_engine->newObject(); + if (create) + obj.setProperty(propertyName, 123, QJSValue::SkipInEnumeration | QJSValue::ReadOnly); + QBENCHMARK { + (void)obj.propertyFlags(propertyName); + } +} + +void tst_QJSValue::propertyFlagsById_data() +{ + propertyFlags_data(); +} + +void tst_QJSValue::propertyFlagsById() +{ + QFETCH(QString, propertyName); + QFETCH(bool, create); + newEngine(); + QJSValue obj = m_engine->newObject(); + QJSString id = m_engine->toStringHandle(propertyName); + if (create) + obj.setProperty(id, 123, QJSValue::SkipInEnumeration | QJSValue::ReadOnly); + QBENCHMARK { + obj.propertyFlags(id); + } +} +#endif + +void tst_QJSValue::prototype_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::prototype() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.prototype(); + } +} + +void tst_QJSValue::setPrototype() +{ + newEngine(); + QJSValue obj = m_engine->newObject(); + QJSValue proto = m_engine->newObject(); + QBENCHMARK { + obj.setPrototype(proto); + } +} + +#if 0 +void tst_QJSValue::scriptClass_data() +{ + defineStandardTestValues(); +} + +void tst_QJSValue::scriptClass() +{ + QFETCH(QJSValue, val); + QBENCHMARK { + val.scriptClass(); + } +} + +void tst_QJSValue::setScriptClass() +{ + newEngine(); + QJSValue obj = m_engine->newObject(); + QJSClass cls(m_engine); + QBENCHMARK { + obj.setScriptClass(&cls); + } +} + +void tst_QJSValue::readMetaProperty() +{ + newEngine(); + QJSValue object = m_engine->newQObject(QCoreApplication::instance()); + QJSString propertyName = m_engine->toStringHandle("objectName"); + QBENCHMARK { + for (int i = 0; i < 10000; ++i) + object.property(propertyName); + } +} + +void tst_QJSValue::writeMetaProperty() +{ + newEngine(); + QJSValue object = m_engine->newQObject(QCoreApplication::instance()); + QJSString propertyName = m_engine->toStringHandle("objectName"); + QJSValue value(m_engine, "foo"); + QBENCHMARK { + for (int i = 0; i < 10000; ++i) + object.setProperty(propertyName, value); + } +} +#endif + +void tst_QJSValue::defineStandardTestValues() +{ + newEngine(); + QTest::addColumn("val"); + QTest::newRow("bool") << m_engine->evaluate("true"); + QTest::newRow("number") << m_engine->evaluate("123"); + QTest::newRow("string") << m_engine->evaluate("'ciao'"); + QTest::newRow("null") << m_engine->evaluate("null"); + QTest::newRow("undefined") << m_engine->evaluate("undefined"); + QTest::newRow("object") << m_engine->evaluate("({foo:123})"); + QTest::newRow("array") << m_engine->evaluate("[10,20,30]"); + QTest::newRow("function") << m_engine->evaluate("(function foo(a, b, c) { return a + b + c; })"); + QTest::newRow("date") << m_engine->evaluate("new Date"); + QTest::newRow("regexp") << m_engine->evaluate("new RegExp('foo')"); + QTest::newRow("error") << m_engine->evaluate("new Error"); + + QTest::newRow("qobject") << m_engine->newQObject(this); +#if 0 // no qmetaobject + QTest::newRow("qmetaobject") << m_engine->newQMetaObject(&QJSEngine::staticMetaObject); +#endif + QTest::newRow("variant") << m_engine->newVariant(123); +#if 0 // no classess + QTest::newRow("qscriptclassobject") << m_engine->newObject(new QJSClass(m_engine)); +#endif + + QTest::newRow("invalid") << QJSValue(); + QTest::newRow("bool-no-engine") << QJSValue(true); + QTest::newRow("number-no-engine") << QJSValue(123.0); + QTest::newRow("string-no-engine") << QJSValue(QString::fromLatin1("hello")); + QTest::newRow("null-no-engine") << QJSValue(QJSValue::NullValue); + QTest::newRow("undefined-no-engine") << QJSValue(QJSValue::UndefinedValue); +} + +QTEST_MAIN(tst_QJSValue) +#include "tst_qjsvalue.moc" diff --git a/tests/benchmarks/declarative/js/qjsvalueiterator/qjsvalueiterator.pro b/tests/benchmarks/declarative/js/qjsvalueiterator/qjsvalueiterator.pro new file mode 100644 index 0000000000..a114b863f6 --- /dev/null +++ b/tests/benchmarks/declarative/js/qjsvalueiterator/qjsvalueiterator.pro @@ -0,0 +1,7 @@ +load(qttest_p4) +TEMPLATE = app +TARGET = tst_bench_qjsvalueiterator + +SOURCES += tst_qjsvalueiterator.cpp + +QT = core declarative diff --git a/tests/benchmarks/declarative/js/qjsvalueiterator/tst_qjsvalueiterator.cpp b/tests/benchmarks/declarative/js/qjsvalueiterator/tst_qjsvalueiterator.cpp new file mode 100644 index 0000000000..8b4921cf8e --- /dev/null +++ b/tests/benchmarks/declarative/js/qjsvalueiterator/tst_qjsvalueiterator.cpp @@ -0,0 +1,311 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include + +//TESTED_FILES= + +class tst_QJSValueIterator : public QObject +{ + Q_OBJECT + +public: + tst_QJSValueIterator(); + virtual ~tst_QJSValueIterator(); + + void dataHelper(); + +private slots: + void init(); + void cleanup(); + + void hasNextAndNext(); + + void constructAndNext_data(); + void constructAndNext(); + + void name_data(); + void name(); +#if 0 // No string handle + void scriptName_data(); + void scriptName(); +#endif + + void value_data(); + void value(); +#if 0 // no setValue + void setValue_data(); + void setValue(); +#endif +#if 0 // no flags + void flags(); +#endif + +#if 0 // no array index + void iterateArrayAndConvertNameToIndex(); +#endif +#if 0 // no setValue + void iterateArrayAndDoubleElements(); +#endif +#if 0 // no remove + void iterateArrayAndRemoveAllElements(); +#endif +}; + +tst_QJSValueIterator::tst_QJSValueIterator() +{ +} + +tst_QJSValueIterator::~tst_QJSValueIterator() +{ +} + +void tst_QJSValueIterator::init() +{ +} + +void tst_QJSValueIterator::cleanup() +{ +} + +void tst_QJSValueIterator::dataHelper() +{ + QTest::addColumn("code"); + QTest::newRow("{ foo: 123 }") << QString::fromLatin1("({ foo: 123 })"); + QTest::newRow("Math") << QString::fromLatin1("Math"); + QTest::newRow("Array.prototype") << QString::fromLatin1("Array.prototype"); + QTest::newRow("Global Object") << QString::fromLatin1("this"); + QTest::newRow("['foo']") << QString::fromLatin1("['foo']"); + QTest::newRow("array with 1000 elements") + << QString::fromLatin1("(function() {" + " var a = new Array;" + " for (i = 0; i < 1000; ++i)" + " a[i] = i;" + " return a;" + "})()"); +} + +void tst_QJSValueIterator::hasNextAndNext() +{ + QJSEngine engine; + QJSValue object = engine.newObject(); + for (int i = 0; i < 2000; ++i) + object.setProperty(i, i); + QBENCHMARK { + for (int i = 0; i < 1000; ++i) { + QJSValueIterator it(object); + while (it.hasNext()) + it.next(); + } + } +} + +void tst_QJSValueIterator::constructAndNext_data() +{ + dataHelper(); +} + +void tst_QJSValueIterator::constructAndNext() +{ + QFETCH(QString, code); + QJSEngine engine; + QJSValue object = engine.evaluate(code); + Q_ASSERT(object.isObject()); + + QBENCHMARK { + for (int i = 0; i < 100; ++i) { + QJSValueIterator it(object); + it.next(); + } + } +} + +void tst_QJSValueIterator::name_data() +{ + dataHelper(); +} + +void tst_QJSValueIterator::name() +{ + QFETCH(QString, code); + QJSEngine engine; + QJSValue object = engine.evaluate(code); + Q_ASSERT(object.isObject()); + + QJSValueIterator it(object); + it.next(); + QBENCHMARK { + for (int i = 0; i < 200000; ++i) + it.name(); + } +} + +#if 0 +void tst_QJSValueIterator::scriptName_data() +{ + dataHelper(); +} + +void tst_QJSValueIterator::scriptName() +{ + QFETCH(QString, code); + QJSEngine engine; + QJSValue object = engine.evaluate(code); + Q_ASSERT(object.isObject()); + + QJSValueIterator it(object); + it.next(); + QBENCHMARK { + for (int i = 0; i < 50000; ++i) + it.scriptName(); + } +} +#endif + +void tst_QJSValueIterator::value_data() +{ + dataHelper(); +} + +void tst_QJSValueIterator::value() +{ + QFETCH(QString, code); + QJSEngine engine; + QJSValue object = engine.evaluate(code); + Q_ASSERT(object.isObject()); + + QJSValueIterator it(object); + it.next(); + QBENCHMARK { + for (int i = 0; i < 50000; ++i) + it.value(); + } +} + +#if 0 +void tst_QJSValueIterator::setValue_data() +{ + dataHelper(); +} + +void tst_QJSValueIterator::setValue() +{ + QFETCH(QString, code); + QJSEngine engine; + QJSValue object = engine.evaluate(code); + Q_ASSERT(object.isObject()); + + QJSValueIterator it(object); + it.next(); + QJSValue newValue(&engine, 456); + QBENCHMARK { + for (int i = 0; i < 50000; ++i) + it.setValue(newValue); + } +} + +void tst_QJSValueIterator::flags() +{ + QJSEngine engine; + QJSValue object = engine.newObject(); + QJSValue::PropertyFlags flags = flags; + object.setProperty("foo", 123, QJSValue::SkipInEnumeration | QJSValue::ReadOnly | QJSValue::Undeletable); + QJSValueIterator it(object); + it.next(); + QBENCHMARK { + for (int i = 0; i < 50000; ++i) + it.flags(); + } +} +#endif + +#if 0 +void tst_QJSValueIterator::iterateArrayAndConvertNameToIndex() +{ + QJSEngine engine; + QJSValue array = engine.newArray(); + for (int i = 0; i < 20000; ++i) + array.setProperty(i, i); + QBENCHMARK { + QJSValueIterator it(array); + while (it.hasNext()) { + it.next(); + it.scriptName().toArrayIndex(); + } + } +} + +void tst_QJSValueIterator::iterateArrayAndDoubleElements() +{ + QJSEngine engine; + QJSValue array = engine.newArray(); + for (int i = 0; i < 20000; ++i) + array.setProperty(i, i); + QBENCHMARK { + QJSValueIterator it(array); + while (it.hasNext()) { + it.next(); + it.setValue(QJSValue(&engine, it.value().toNumber() * 2)); + } + } +} + +void tst_QJSValueIterator::iterateArrayAndRemoveAllElements() +{ + QJSEngine engine; + QJSValue array = engine.newArray(); + for (int i = 0; i < 20000; ++i) + array.setProperty(i, i); + QBENCHMARK { + QJSValueIterator it(array); + while (it.hasNext()) { + it.next(); + it.remove(); + } + } +} +#endif + +QTEST_MAIN(tst_QJSValueIterator) +#include "tst_qjsvalueiterator.moc" -- cgit v1.2.3