tests/benchmarks/script/qscriptengine/tst_qscriptengine.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
child 14 c0432d11811c
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 <QtScript>
       
    44 
       
    45 //TESTED_FILES=
       
    46 
       
    47 class tst_QScriptEngine : public QObject
       
    48 {
       
    49     Q_OBJECT
       
    50 
       
    51 public:
       
    52     tst_QScriptEngine();
       
    53     virtual ~tst_QScriptEngine();
       
    54 
       
    55 public slots:
       
    56     void init();
       
    57     void cleanup();
       
    58 
       
    59 private slots:
       
    60     void constructor();
       
    61     void evaluate_data();
       
    62     void evaluate();
       
    63     void evaluateProgram_data();
       
    64     void evaluateProgram();
       
    65     void connectAndDisconnect();
       
    66     void newObject();
       
    67     void newQObject();
       
    68     void newFunction();
       
    69     void newVariant();
       
    70     void collectGarbage();
       
    71     void pushAndPopContext();
       
    72     void toStringHandle();
       
    73     void castValueToQreal();
       
    74     void nativeCall();
       
    75     void translation_data();
       
    76     void translation();
       
    77 };
       
    78 
       
    79 tst_QScriptEngine::tst_QScriptEngine()
       
    80 {
       
    81 }
       
    82 
       
    83 tst_QScriptEngine::~tst_QScriptEngine()
       
    84 {
       
    85 }
       
    86 
       
    87 void tst_QScriptEngine::init()
       
    88 {
       
    89 }
       
    90 
       
    91 void tst_QScriptEngine::cleanup()
       
    92 {
       
    93 }
       
    94 
       
    95 void tst_QScriptEngine::constructor()
       
    96 {
       
    97     QBENCHMARK {
       
    98         QScriptEngine engine;
       
    99         (void)engine.parent();
       
   100     }
       
   101 }
       
   102 
       
   103 void tst_QScriptEngine::evaluate_data()
       
   104 {
       
   105     QTest::addColumn<QString>("code");
       
   106     QTest::newRow("empty script") << QString::fromLatin1("");
       
   107     QTest::newRow("number literal") << QString::fromLatin1("123");
       
   108     QTest::newRow("string literal") << QString::fromLatin1("'ciao'");
       
   109     QTest::newRow("regexp literal") << QString::fromLatin1("/foo/gim");
       
   110     QTest::newRow("null literal") << QString::fromLatin1("null");
       
   111     QTest::newRow("undefined literal") << QString::fromLatin1("undefined");
       
   112     QTest::newRow("null literal") << QString::fromLatin1("null");
       
   113     QTest::newRow("empty object literal") << QString::fromLatin1("{}");
       
   114     QTest::newRow("this") << QString::fromLatin1("this");
       
   115     QTest::newRow("object literal with one property") << QString::fromLatin1("{ foo: 123 }");
       
   116     QTest::newRow("object literal with two properties") << QString::fromLatin1("{ foo: 123, bar: 456 }");
       
   117     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 }");
       
   118     QTest::newRow("empty array literal") << QString::fromLatin1("[]");
       
   119     QTest::newRow("array literal with one element") << QString::fromLatin1("[1]");
       
   120     QTest::newRow("array literal with two elements") << QString::fromLatin1("[1,2]");
       
   121     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]");
       
   122     QTest::newRow("empty function definition") << QString::fromLatin1("function foo() { }");
       
   123     QTest::newRow("function definition") << QString::fromLatin1("function foo() { return 123; }");
       
   124     QTest::newRow("for loop with empty body (1000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000; ++i) {}");
       
   125     QTest::newRow("for loop with empty body (10000 iterations)") << QString::fromLatin1("for (i = 0; i < 10000; ++i) {}");
       
   126     QTest::newRow("for loop with empty body (100000 iterations)") << QString::fromLatin1("for (i = 0; i < 100000; ++i) {}");
       
   127     QTest::newRow("for loop with empty body (1000000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000000; ++i) {}");
       
   128     QTest::newRow("for loop (1000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000; ++i) { j += i; }; j");
       
   129     QTest::newRow("for loop (10000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 10000; ++i) { j += i; }; j");
       
   130     QTest::newRow("for loop (100000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 100000; ++i) { j += i; }; j");
       
   131     QTest::newRow("for loop (1000000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000000; ++i) { j += i; }; j");
       
   132     QTest::newRow("assignments") << QString::fromLatin1("a = 1; b = 2; c = 3; d = 4");
       
   133     QTest::newRow("while loop (1000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000) { ++i; }; i");
       
   134     QTest::newRow("while loop (10000 iterations)") << QString::fromLatin1("i = 0; while (i < 10000) { ++i; }; i");
       
   135     QTest::newRow("while loop (100000 iterations)") << QString::fromLatin1("i = 0; while (i < 100000) { ++i; }; i");
       
   136     QTest::newRow("while loop (1000000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000000) { ++i; }; i");
       
   137     QTest::newRow("function expression") << QString::fromLatin1("(function(a, b, c){ return a + b + c; })(1, 2, 3)");
       
   138 }
       
   139 
       
   140 void tst_QScriptEngine::evaluate()
       
   141 {
       
   142     QFETCH(QString, code);
       
   143     QScriptEngine engine;
       
   144 
       
   145     QBENCHMARK {
       
   146         (void)engine.evaluate(code);
       
   147     }
       
   148 }
       
   149 
       
   150 void tst_QScriptEngine::connectAndDisconnect()
       
   151 {
       
   152     QScriptEngine engine;
       
   153     QScriptValue fun = engine.evaluate("(function() { })");
       
   154     QBENCHMARK {
       
   155         qScriptConnect(&engine, SIGNAL(destroyed()), QScriptValue(), fun);
       
   156         qScriptDisconnect(&engine, SIGNAL(destroyed()), QScriptValue(), fun);
       
   157     }
       
   158 }
       
   159 
       
   160 void tst_QScriptEngine::evaluateProgram_data()
       
   161 {
       
   162     evaluate_data();
       
   163 }
       
   164 
       
   165 void tst_QScriptEngine::evaluateProgram()
       
   166 {
       
   167     QFETCH(QString, code);
       
   168     QScriptEngine engine;
       
   169     QScriptProgram program(code);
       
   170 
       
   171     QBENCHMARK {
       
   172         (void)engine.evaluate(program);
       
   173     }
       
   174 }
       
   175 
       
   176 void tst_QScriptEngine::newObject()
       
   177 {
       
   178     QScriptEngine engine;
       
   179     QBENCHMARK {
       
   180         (void)engine.newObject();
       
   181     }
       
   182 }
       
   183 
       
   184 void tst_QScriptEngine::newQObject()
       
   185 {
       
   186     QScriptEngine engine;
       
   187     QBENCHMARK {
       
   188         (void)engine.newQObject(QCoreApplication::instance());
       
   189     }
       
   190 }
       
   191 
       
   192 static QScriptValue testFunction(QScriptContext *, QScriptEngine *)
       
   193 {
       
   194     return 0;
       
   195 }
       
   196 
       
   197 void tst_QScriptEngine::newFunction()
       
   198 {
       
   199     QScriptEngine engine;
       
   200     QBENCHMARK {
       
   201         (void)engine.newFunction(testFunction);
       
   202     }
       
   203 }
       
   204 
       
   205 void tst_QScriptEngine::newVariant()
       
   206 {
       
   207     QScriptEngine engine;
       
   208     QVariant var(123);
       
   209     QBENCHMARK {
       
   210         (void)engine.newVariant(var);
       
   211     }
       
   212 }
       
   213 
       
   214 void tst_QScriptEngine::collectGarbage()
       
   215 {
       
   216     QScriptEngine engine;
       
   217     QBENCHMARK {
       
   218         engine.collectGarbage();
       
   219     }
       
   220 }
       
   221 
       
   222 void tst_QScriptEngine::pushAndPopContext()
       
   223 {
       
   224     QScriptEngine engine;
       
   225     QBENCHMARK {
       
   226         (void)engine.pushContext();
       
   227         engine.popContext();
       
   228     }
       
   229 }
       
   230 
       
   231 void tst_QScriptEngine::toStringHandle()
       
   232 {
       
   233     QScriptEngine engine;
       
   234     QString str = QString::fromLatin1("foobarbaz");
       
   235     QBENCHMARK {
       
   236         (void)engine.toStringHandle(str);
       
   237     }
       
   238 }
       
   239 
       
   240 void tst_QScriptEngine::castValueToQreal()
       
   241 {
       
   242     QScriptEngine engine;
       
   243     QScriptValue val(123);
       
   244     QBENCHMARK {
       
   245         (void)qscriptvalue_cast<qreal>(val);
       
   246     }
       
   247 }
       
   248 
       
   249 static QScriptValue native_function(QScriptContext *, QScriptEngine *)
       
   250 {
       
   251     return 42;
       
   252 }
       
   253 
       
   254 void tst_QScriptEngine::nativeCall()
       
   255 {
       
   256     QScriptEngine eng;
       
   257     eng.globalObject().setProperty("fun", eng.newFunction(native_function));
       
   258     QBENCHMARK{
       
   259 #if !defined(Q_OS_SYMBIAN)
       
   260         eng.evaluate("var w = 0; for (i = 0; i < 100000; ++i) {\n"
       
   261                      "  w += fun() + fun(); w -= fun(); fun(); w -= fun(); }");
       
   262 #else
       
   263         eng.evaluate("var w = 0; for (i = 0; i < 25000; ++i) {\n"
       
   264                      "  w += fun() + fun(); w -= fun(); fun(); w -= fun(); }");
       
   265 #endif
       
   266     }
       
   267 }
       
   268 
       
   269 void tst_QScriptEngine::translation_data()
       
   270 {
       
   271     QTest::addColumn<QString>("text");
       
   272     QTest::newRow("no translation") << "\"hello world\"";
       
   273     QTest::newRow("qsTr") << "qsTr(\"hello world\")";
       
   274     QTest::newRow("qsTranslate") << "qsTranslate(\"\", \"hello world\")";
       
   275 }
       
   276 
       
   277 void tst_QScriptEngine::translation()
       
   278 {
       
   279     QFETCH(QString, text);
       
   280     QScriptEngine engine;
       
   281     engine.installTranslatorFunctions();
       
   282 
       
   283     QBENCHMARK {
       
   284         (void)engine.evaluate(text);
       
   285     }
       
   286 }
       
   287 
       
   288 QTEST_MAIN(tst_QScriptEngine)
       
   289 #include "tst_qscriptengine.moc"