tests/benchmarks/script/qscriptvalue/tst_qscriptvalue.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7: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_QScriptValue : public QObject
       
    48 {
       
    49     Q_OBJECT
       
    50 
       
    51 public:
       
    52     tst_QScriptValue();
       
    53     virtual ~tst_QScriptValue();
       
    54 
       
    55 public slots:
       
    56     void init();
       
    57     void cleanup();
       
    58 
       
    59 private slots:
       
    60     void numberConstructor();
       
    61     void stringConstructor();
       
    62     void call_data();
       
    63     void call();
       
    64     void construct_data();
       
    65     void construct();
       
    66     void toString_data();
       
    67     void toString();
       
    68     void toQObject();
       
    69     void property();
       
    70     void setProperty();
       
    71     void propertyFlags();
       
    72     void readMetaProperty();
       
    73     void writeMetaProperty();
       
    74 };
       
    75 
       
    76 tst_QScriptValue::tst_QScriptValue()
       
    77 {
       
    78 }
       
    79 
       
    80 tst_QScriptValue::~tst_QScriptValue()
       
    81 {
       
    82 }
       
    83 
       
    84 void tst_QScriptValue::init()
       
    85 {
       
    86 }
       
    87 
       
    88 void tst_QScriptValue::cleanup()
       
    89 {
       
    90 }
       
    91 
       
    92 void tst_QScriptValue::numberConstructor()
       
    93 {
       
    94     QBENCHMARK {
       
    95         (void)QScriptValue(123);
       
    96     }
       
    97 }
       
    98 
       
    99 void tst_QScriptValue::stringConstructor()
       
   100 {
       
   101     QString str = QString::fromLatin1("ciao");
       
   102     QBENCHMARK {
       
   103         (void)QScriptValue(str);
       
   104     }
       
   105 }
       
   106 
       
   107 void tst_QScriptValue::call_data()
       
   108 {
       
   109     QTest::addColumn<QString>("code");
       
   110     QTest::newRow("empty function") << QString::fromLatin1("(function(){})");
       
   111     QTest::newRow("function returning number") << QString::fromLatin1("(function(){ return 123; })");
       
   112     QTest::newRow("closure") << QString::fromLatin1("(function(a, b){ return function() { return a + b; }; })(1, 2)");
       
   113 }
       
   114 
       
   115 void tst_QScriptValue::call()
       
   116 {
       
   117     QFETCH(QString, code);
       
   118     QScriptEngine engine;
       
   119     QScriptValue fun = engine.evaluate(code);
       
   120     QVERIFY(fun.isFunction());
       
   121     QBENCHMARK {
       
   122         (void)fun.call();
       
   123     }
       
   124 }
       
   125 
       
   126 void tst_QScriptValue::construct_data()
       
   127 {
       
   128     QTest::addColumn<QString>("code");
       
   129     QTest::newRow("empty function") << QString::fromLatin1("(function(){})");
       
   130     QTest::newRow("simple constructor") << QString::fromLatin1("(function(){ this.x = 10; this.y = 20; })");
       
   131 }
       
   132 
       
   133 void tst_QScriptValue::construct()
       
   134 {
       
   135     QFETCH(QString, code);
       
   136     QScriptEngine engine;
       
   137     QScriptValue fun = engine.evaluate(code);
       
   138     QVERIFY(fun.isFunction());
       
   139     QBENCHMARK {
       
   140         (void)fun.construct();
       
   141     }
       
   142 }
       
   143 
       
   144 void tst_QScriptValue::toString_data()
       
   145 {
       
   146     QTest::addColumn<QString>("code");
       
   147     QTest::newRow("number") << QString::fromLatin1("123");
       
   148     QTest::newRow("string") << QString::fromLatin1("'ciao'");
       
   149     QTest::newRow("null") << QString::fromLatin1("null");
       
   150     QTest::newRow("undefined") << QString::fromLatin1("undefined");
       
   151     QTest::newRow("function") << QString::fromLatin1("(function foo(a, b, c) { return a + b + c; })");
       
   152 }
       
   153 
       
   154 void tst_QScriptValue::toString()
       
   155 {
       
   156     QFETCH(QString, code);
       
   157     QScriptEngine engine;
       
   158     QScriptValue val = engine.evaluate(code);
       
   159     QBENCHMARK {
       
   160         (void)val.toString();
       
   161     }
       
   162 }
       
   163 
       
   164 void tst_QScriptValue::toQObject()
       
   165 {
       
   166     QScriptEngine engine;
       
   167     QScriptValue obj = engine.newQObject(QCoreApplication::instance());
       
   168     QBENCHMARK {
       
   169         (void)obj.toQObject();
       
   170     }
       
   171 }
       
   172 
       
   173 void tst_QScriptValue::property()
       
   174 {
       
   175     QScriptEngine engine;
       
   176     QScriptValue obj = engine.newObject();
       
   177     QString propertyName = QString::fromLatin1("foo");
       
   178     obj.setProperty(propertyName, 123);
       
   179     QBENCHMARK {
       
   180         (void)obj.property(propertyName);
       
   181     }
       
   182 }
       
   183 
       
   184 void tst_QScriptValue::setProperty()
       
   185 {
       
   186     QScriptEngine engine;
       
   187     QScriptValue obj = engine.newObject();
       
   188     QString propertyName = QString::fromLatin1("foo");
       
   189     QScriptValue val(123);
       
   190     QBENCHMARK {
       
   191         obj.setProperty(propertyName, val);
       
   192     }
       
   193 }
       
   194 
       
   195 void tst_QScriptValue::propertyFlags()
       
   196 {
       
   197     QScriptEngine engine;
       
   198     QScriptValue obj = engine.newObject();
       
   199     QString propertyName = QString::fromLatin1("foo");
       
   200     obj.setProperty(propertyName, 123, QScriptValue::SkipInEnumeration | QScriptValue::ReadOnly);
       
   201     QBENCHMARK {
       
   202         (void)obj.propertyFlags(propertyName);
       
   203     }
       
   204 }
       
   205 
       
   206 void tst_QScriptValue::readMetaProperty()
       
   207 {
       
   208     QScriptEngine engine;
       
   209     QScriptValue object = engine.newQObject(QCoreApplication::instance());
       
   210     QScriptString propertyName = engine.toStringHandle("objectName");
       
   211     QBENCHMARK {
       
   212         for (int i = 0; i < 10000; ++i)
       
   213             object.property(propertyName);
       
   214     }
       
   215 }
       
   216 
       
   217 void tst_QScriptValue::writeMetaProperty()
       
   218 {
       
   219     QScriptEngine engine;
       
   220     QScriptValue object = engine.newQObject(QCoreApplication::instance());
       
   221     QScriptString propertyName = engine.toStringHandle("objectName");
       
   222     QScriptValue value(&engine, "foo");
       
   223     QBENCHMARK {
       
   224         for (int i = 0; i < 10000; ++i)
       
   225             object.setProperty(propertyName, value);
       
   226     }
       
   227 }
       
   228 
       
   229 QTEST_MAIN(tst_QScriptValue)
       
   230 #include "tst_qscriptvalue.moc"