tests/benchmarks/script/qscriptengine/tst_qscriptengine.cpp
changeset 33 3e2da88830cd
parent 22 79de32ba3296
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include <qtest.h>
    42 #include <qtest.h>
    43 #include <QtScript>
    43 #include <QtScript>
       
    44 
       
    45 #include <QtScript/private/qscriptdeclarativeclass_p.h>
    44 
    46 
    45 //TESTED_FILES=
    47 //TESTED_FILES=
    46 
    48 
    47 class tst_QScriptEngine : public QObject
    49 class tst_QScriptEngine : public QObject
    48 {
    50 {
    72     void toStringHandle();
    74     void toStringHandle();
    73     void castValueToQreal();
    75     void castValueToQreal();
    74     void nativeCall();
    76     void nativeCall();
    75     void translation_data();
    77     void translation_data();
    76     void translation();
    78     void translation();
       
    79     void readScopeProperty_data();
       
    80     void readScopeProperty();
    77 };
    81 };
    78 
    82 
    79 tst_QScriptEngine::tst_QScriptEngine()
    83 tst_QScriptEngine::tst_QScriptEngine()
    80 {
    84 {
    81 }
    85 }
   286     QBENCHMARK {
   290     QBENCHMARK {
   287         (void)engine.evaluate(text, fileName);
   291         (void)engine.evaluate(text, fileName);
   288     }
   292     }
   289 }
   293 }
   290 
   294 
       
   295 void tst_QScriptEngine::readScopeProperty_data()
       
   296 {
       
   297     QTest::addColumn<bool>("staticScope");
       
   298     QTest::addColumn<bool>("nestedScope");
       
   299     QTest::newRow("single dynamic scope") << false << false;
       
   300     QTest::newRow("single static scope") << true << false;
       
   301     QTest::newRow("double dynamic scope") << false << true;
       
   302     QTest::newRow("double static scope") << true << true;
       
   303 }
       
   304 
       
   305 void tst_QScriptEngine::readScopeProperty()
       
   306 {
       
   307     QFETCH(bool, staticScope);
       
   308     QFETCH(bool, nestedScope);
       
   309 
       
   310     QScriptEngine engine;
       
   311     QScriptContext *ctx = engine.pushContext();
       
   312 
       
   313     QScriptValue scope;
       
   314     if (staticScope)
       
   315         scope = QScriptDeclarativeClass::newStaticScopeObject(&engine);
       
   316     else
       
   317         scope = engine.newObject();
       
   318     scope.setProperty("foo", 123);
       
   319     ctx->pushScope(scope);
       
   320 
       
   321     if (nestedScope) {
       
   322         QScriptValue scope2;
       
   323         if (staticScope)
       
   324             scope2 = QScriptDeclarativeClass::newStaticScopeObject(&engine);
       
   325         else
       
   326             scope2 = engine.newObject();
       
   327         scope2.setProperty("bar", 456); // ensure a miss in inner scope
       
   328         ctx->pushScope(scope2);
       
   329     }
       
   330 
       
   331     QScriptValue fun = engine.evaluate("(function() {\n"
       
   332                                        "  for (var i = 0; i < 10000; ++i) {\n"
       
   333                                        "    foo; foo; foo; foo; foo; foo; foo; foo;\n"
       
   334                                        "  }\n"
       
   335                                        "})");
       
   336     engine.popContext();
       
   337     QVERIFY(fun.isFunction());
       
   338     QBENCHMARK {
       
   339         fun.call();
       
   340     }
       
   341 }
       
   342 
   291 QTEST_MAIN(tst_QScriptEngine)
   343 QTEST_MAIN(tst_QScriptEngine)
   292 #include "tst_qscriptengine.moc"
   344 #include "tst_qscriptengine.moc"