tests/benchmarks/qscriptengine/tst_qscriptengine.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 connectAndDisconnect();
       
    64     void newObject();
       
    65     void newQObject();
       
    66     void newFunction();
       
    67     void newVariant();
       
    68     void collectGarbage();
       
    69     void pushAndPopContext();
       
    70     void toStringHandle();
       
    71     void castValueToQreal();
       
    72     void nativeCall();
       
    73 };
       
    74 
       
    75 tst_QScriptEngine::tst_QScriptEngine()
       
    76 {
       
    77 }
       
    78 
       
    79 tst_QScriptEngine::~tst_QScriptEngine()
       
    80 {
       
    81 }
       
    82 
       
    83 void tst_QScriptEngine::init()
       
    84 {
       
    85 }
       
    86 
       
    87 void tst_QScriptEngine::cleanup()
       
    88 {
       
    89 }
       
    90 
       
    91 void tst_QScriptEngine::constructor()
       
    92 {
       
    93     QBENCHMARK {
       
    94         QScriptEngine engine;
       
    95         (void)engine.parent();
       
    96     }
       
    97 }
       
    98 
       
    99 void tst_QScriptEngine::evaluate_data()
       
   100 {
       
   101     QTest::addColumn<QString>("code");
       
   102     QTest::newRow("empty script") << QString::fromLatin1("");
       
   103     QTest::newRow("number literal") << QString::fromLatin1("123");
       
   104     QTest::newRow("string literal") << QString::fromLatin1("'ciao'");
       
   105     QTest::newRow("regexp literal") << QString::fromLatin1("/foo/gim");
       
   106     QTest::newRow("null literal") << QString::fromLatin1("null");
       
   107     QTest::newRow("undefined literal") << QString::fromLatin1("undefined");
       
   108     QTest::newRow("null literal") << QString::fromLatin1("null");
       
   109     QTest::newRow("empty object literal") << QString::fromLatin1("{}");
       
   110     QTest::newRow("this") << QString::fromLatin1("this");
       
   111     QTest::newRow("object literal with one property") << QString::fromLatin1("{ foo: 123 }");
       
   112     QTest::newRow("object literal with two properties") << QString::fromLatin1("{ foo: 123, bar: 456 }");
       
   113     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 }");
       
   114     QTest::newRow("empty array literal") << QString::fromLatin1("[]");
       
   115     QTest::newRow("array literal with one element") << QString::fromLatin1("[1]");
       
   116     QTest::newRow("array literal with two elements") << QString::fromLatin1("[1,2]");
       
   117     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]");
       
   118     QTest::newRow("empty function definition") << QString::fromLatin1("function foo() { }");
       
   119     QTest::newRow("function definition") << QString::fromLatin1("function foo() { return 123; }");
       
   120     QTest::newRow("for loop with empty body (1000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000; ++i) {}");
       
   121     QTest::newRow("for loop with empty body (10000 iterations)") << QString::fromLatin1("for (i = 0; i < 10000; ++i) {}");
       
   122     QTest::newRow("for loop with empty body (100000 iterations)") << QString::fromLatin1("for (i = 0; i < 100000; ++i) {}");
       
   123     QTest::newRow("for loop with empty body (1000000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000000; ++i) {}");
       
   124     QTest::newRow("for loop (1000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000; ++i) { j += i; }; j");
       
   125     QTest::newRow("for loop (10000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 10000; ++i) { j += i; }; j");
       
   126     QTest::newRow("for loop (100000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 100000; ++i) { j += i; }; j");
       
   127     QTest::newRow("for loop (1000000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000000; ++i) { j += i; }; j");
       
   128     QTest::newRow("assignments") << QString::fromLatin1("a = 1; b = 2; c = 3; d = 4");
       
   129     QTest::newRow("while loop (1000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000) { ++i; }; i");
       
   130     QTest::newRow("while loop (10000 iterations)") << QString::fromLatin1("i = 0; while (i < 10000) { ++i; }; i");
       
   131     QTest::newRow("while loop (100000 iterations)") << QString::fromLatin1("i = 0; while (i < 100000) { ++i; }; i");
       
   132     QTest::newRow("while loop (1000000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000000) { ++i; }; i");
       
   133     QTest::newRow("function expression") << QString::fromLatin1("(function(a, b, c){ return a + b + c; })(1, 2, 3)");
       
   134 }
       
   135 
       
   136 void tst_QScriptEngine::evaluate()
       
   137 {
       
   138     QFETCH(QString, code);
       
   139     QScriptEngine engine;
       
   140 
       
   141     QBENCHMARK {
       
   142         (void)engine.evaluate(code);
       
   143     }
       
   144 }
       
   145 
       
   146 void tst_QScriptEngine::connectAndDisconnect()
       
   147 {
       
   148     QScriptEngine engine;
       
   149     QScriptValue fun = engine.evaluate("(function() { })");
       
   150     QBENCHMARK {
       
   151         qScriptConnect(&engine, SIGNAL(destroyed()), QScriptValue(), fun);
       
   152         qScriptDisconnect(&engine, SIGNAL(destroyed()), QScriptValue(), fun);
       
   153     }
       
   154 }
       
   155 
       
   156 void tst_QScriptEngine::newObject()
       
   157 {
       
   158     QScriptEngine engine;
       
   159     QBENCHMARK {
       
   160         (void)engine.newObject();
       
   161     }
       
   162 }
       
   163 
       
   164 void tst_QScriptEngine::newQObject()
       
   165 {
       
   166     QScriptEngine engine;
       
   167     QBENCHMARK {
       
   168         (void)engine.newQObject(QCoreApplication::instance());
       
   169     }
       
   170 }
       
   171 
       
   172 static QScriptValue testFunction(QScriptContext *, QScriptEngine *)
       
   173 {
       
   174     return 0;
       
   175 }
       
   176 
       
   177 void tst_QScriptEngine::newFunction()
       
   178 {
       
   179     QScriptEngine engine;
       
   180     QBENCHMARK {
       
   181         (void)engine.newFunction(testFunction);
       
   182     }
       
   183 }
       
   184 
       
   185 void tst_QScriptEngine::newVariant()
       
   186 {
       
   187     QScriptEngine engine;
       
   188     QVariant var(123);
       
   189     QBENCHMARK {
       
   190         (void)engine.newVariant(var);
       
   191     }
       
   192 }
       
   193 
       
   194 void tst_QScriptEngine::collectGarbage()
       
   195 {
       
   196     QScriptEngine engine;
       
   197     QBENCHMARK {
       
   198         engine.collectGarbage();
       
   199     }
       
   200 }
       
   201 
       
   202 void tst_QScriptEngine::pushAndPopContext()
       
   203 {
       
   204     QScriptEngine engine;
       
   205     QBENCHMARK {
       
   206         (void)engine.pushContext();
       
   207         engine.popContext();
       
   208     }
       
   209 }
       
   210 
       
   211 void tst_QScriptEngine::toStringHandle()
       
   212 {
       
   213     QScriptEngine engine;
       
   214     QString str = QString::fromLatin1("foobarbaz");
       
   215     QBENCHMARK {
       
   216         (void)engine.toStringHandle(str);
       
   217     }
       
   218 }
       
   219 
       
   220 void tst_QScriptEngine::castValueToQreal()
       
   221 {
       
   222     QScriptEngine engine;
       
   223     QScriptValue val(123);
       
   224     QBENCHMARK {
       
   225         (void)qscriptvalue_cast<qreal>(val);
       
   226     }
       
   227 }
       
   228 
       
   229 static QScriptValue native_function(QScriptContext *, QScriptEngine *)
       
   230 {
       
   231     return 42;
       
   232 }
       
   233 
       
   234 void tst_QScriptEngine::nativeCall()
       
   235 {
       
   236     QScriptEngine eng;
       
   237     eng.globalObject().setProperty("fun", eng.newFunction(native_function));
       
   238     QBENCHMARK{
       
   239         eng.evaluate("var w = 0; for (i = 0; i < 100000; ++i) {\n"
       
   240                      "  w += fun() + fun(); w -= fun(); fun(); w -= fun(); }");
       
   241     }
       
   242 }
       
   243 
       
   244 
       
   245 QTEST_MAIN(tst_QScriptEngine)
       
   246 #include "tst_qscriptengine.moc"