tests/benchmarks/qscriptvalue/tst_qscriptvalue.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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_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 };
       
    73 
       
    74 tst_QScriptValue::tst_QScriptValue()
       
    75 {
       
    76 }
       
    77 
       
    78 tst_QScriptValue::~tst_QScriptValue()
       
    79 {
       
    80 }
       
    81 
       
    82 void tst_QScriptValue::init()
       
    83 {
       
    84 }
       
    85 
       
    86 void tst_QScriptValue::cleanup()
       
    87 {
       
    88 }
       
    89 
       
    90 void tst_QScriptValue::numberConstructor()
       
    91 {
       
    92     QBENCHMARK {
       
    93         (void)QScriptValue(123);
       
    94     }
       
    95 }
       
    96 
       
    97 void tst_QScriptValue::stringConstructor()
       
    98 {
       
    99     QString str = QString::fromLatin1("ciao");
       
   100     QBENCHMARK {
       
   101         (void)QScriptValue(str);
       
   102     }
       
   103 }
       
   104 
       
   105 void tst_QScriptValue::call_data()
       
   106 {
       
   107     QTest::addColumn<QString>("code");
       
   108     QTest::newRow("empty function") << QString::fromLatin1("(function(){})");
       
   109     QTest::newRow("function returning number") << QString::fromLatin1("(function(){ return 123; })");
       
   110     QTest::newRow("closure") << QString::fromLatin1("(function(a, b){ return function() { return a + b; }; })(1, 2)");
       
   111 }
       
   112 
       
   113 void tst_QScriptValue::call()
       
   114 {
       
   115     QFETCH(QString, code);
       
   116     QScriptEngine engine;
       
   117     QScriptValue fun = engine.evaluate(code);
       
   118     QVERIFY(fun.isFunction());
       
   119     QBENCHMARK {
       
   120         (void)fun.call();
       
   121     }
       
   122 }
       
   123 
       
   124 void tst_QScriptValue::construct_data()
       
   125 {
       
   126     QTest::addColumn<QString>("code");
       
   127     QTest::newRow("empty function") << QString::fromLatin1("(function(){})");
       
   128     QTest::newRow("simple constructor") << QString::fromLatin1("(function(){ this.x = 10; this.y = 20; })");
       
   129 }
       
   130 
       
   131 void tst_QScriptValue::construct()
       
   132 {
       
   133     QFETCH(QString, code);
       
   134     QScriptEngine engine;
       
   135     QScriptValue fun = engine.evaluate(code);
       
   136     QVERIFY(fun.isFunction());
       
   137     QBENCHMARK {
       
   138         (void)fun.construct();
       
   139     }
       
   140 }
       
   141 
       
   142 void tst_QScriptValue::toString_data()
       
   143 {
       
   144     QTest::addColumn<QString>("code");
       
   145     QTest::newRow("number") << QString::fromLatin1("123");
       
   146     QTest::newRow("string") << QString::fromLatin1("'ciao'");
       
   147     QTest::newRow("null") << QString::fromLatin1("null");
       
   148     QTest::newRow("undefined") << QString::fromLatin1("undefined");
       
   149     QTest::newRow("function") << QString::fromLatin1("(function foo(a, b, c) { return a + b + c; })");
       
   150 }
       
   151 
       
   152 void tst_QScriptValue::toString()
       
   153 {
       
   154     QFETCH(QString, code);
       
   155     QScriptEngine engine;
       
   156     QScriptValue val = engine.evaluate(code);
       
   157     QBENCHMARK {
       
   158         (void)val.toString();
       
   159     }
       
   160 }
       
   161 
       
   162 void tst_QScriptValue::toQObject()
       
   163 {
       
   164     QScriptEngine engine;
       
   165     QScriptValue obj = engine.newQObject(QCoreApplication::instance());
       
   166     QBENCHMARK {
       
   167         (void)obj.toQObject();
       
   168     }
       
   169 }
       
   170 
       
   171 void tst_QScriptValue::property()
       
   172 {
       
   173     QScriptEngine engine;
       
   174     QScriptValue obj = engine.newObject();
       
   175     QString propertyName = QString::fromLatin1("foo");
       
   176     obj.setProperty(propertyName, 123);
       
   177     QBENCHMARK {
       
   178         (void)obj.property(propertyName);
       
   179     }
       
   180 }
       
   181 
       
   182 void tst_QScriptValue::setProperty()
       
   183 {
       
   184     QScriptEngine engine;
       
   185     QScriptValue obj = engine.newObject();
       
   186     QString propertyName = QString::fromLatin1("foo");
       
   187     QScriptValue val(123);
       
   188     QBENCHMARK {
       
   189         obj.setProperty(propertyName, val);
       
   190     }
       
   191 }
       
   192 
       
   193 void tst_QScriptValue::propertyFlags()
       
   194 {
       
   195     QScriptEngine engine;
       
   196     QScriptValue obj = engine.newObject();
       
   197     QString propertyName = QString::fromLatin1("foo");
       
   198     obj.setProperty(propertyName, 123, QScriptValue::SkipInEnumeration | QScriptValue::ReadOnly);
       
   199     QBENCHMARK {
       
   200         (void)obj.propertyFlags(propertyName);
       
   201     }
       
   202 }
       
   203 
       
   204 QTEST_MAIN(tst_QScriptValue)
       
   205 #include "tst_qscriptvalue.moc"