util/tests/auto/qscriptstring/tst_qscriptstring.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     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 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <QtScript/qscriptengine.h>
       
    46 #include <QtScript/qscriptstring.h>
       
    47 
       
    48 //TESTED_CLASS=
       
    49 //TESTED_FILES=
       
    50 
       
    51 class tst_QScriptString : public QObject
       
    52 {
       
    53     Q_OBJECT
       
    54 
       
    55 public:
       
    56     tst_QScriptString();
       
    57     virtual ~tst_QScriptString();
       
    58 
       
    59 private slots:
       
    60     void test();
       
    61     void hash();
       
    62     void toArrayIndex_data();
       
    63     void toArrayIndex();
       
    64 };
       
    65 
       
    66 tst_QScriptString::tst_QScriptString()
       
    67 {
       
    68 }
       
    69 
       
    70 tst_QScriptString::~tst_QScriptString()
       
    71 {
       
    72 }
       
    73 
       
    74 void tst_QScriptString::test()
       
    75 {
       
    76     QScriptEngine eng;
       
    77 
       
    78     {
       
    79         QScriptString str;
       
    80         QVERIFY(!str.isValid());
       
    81         QVERIFY(str == str);
       
    82         QVERIFY(!(str != str));
       
    83         QVERIFY(str.toString().isNull());
       
    84 
       
    85         QScriptString str1(str);
       
    86         QVERIFY(!str1.isValid());
       
    87 
       
    88         QScriptString str2 = str;
       
    89         QVERIFY(!str2.isValid());
       
    90 
       
    91         QCOMPARE(str.toArrayIndex(), quint32(0xffffffff));
       
    92     }
       
    93 
       
    94     for (int x = 0; x < 2; ++x) {
       
    95         QString ciao = QString::fromLatin1("ciao");
       
    96         QScriptString str = eng.toStringHandle(ciao);
       
    97         QVERIFY(str.isValid());
       
    98         QVERIFY(str == str);
       
    99         QVERIFY(!(str != str));
       
   100         QCOMPARE(str.toString(), ciao);
       
   101 
       
   102         QScriptString str1(str);
       
   103         QCOMPARE(str, str1);
       
   104 
       
   105         QScriptString str2 = str;
       
   106         QCOMPARE(str, str2);
       
   107 
       
   108         QScriptString str3 = eng.toStringHandle(ciao);
       
   109         QVERIFY(str3.isValid());
       
   110         QCOMPARE(str, str3);
       
   111 
       
   112         eng.collectGarbage();
       
   113 
       
   114         QVERIFY(str.isValid());
       
   115         QCOMPARE(str.toString(), ciao);
       
   116         QVERIFY(str1.isValid());
       
   117         QCOMPARE(str1.toString(), ciao);
       
   118         QVERIFY(str2.isValid());
       
   119         QCOMPARE(str2.toString(), ciao);
       
   120         QVERIFY(str3.isValid());
       
   121         QCOMPARE(str3.toString(), ciao);
       
   122     }
       
   123 
       
   124     {
       
   125         QScriptEngine *eng2 = new QScriptEngine;
       
   126         QString one = QString::fromLatin1("one");
       
   127         QString two = QString::fromLatin1("two");
       
   128         QScriptString oneInterned = eng2->toStringHandle(one);
       
   129         QCOMPARE(oneInterned.toString(), one);
       
   130         QScriptString twoInterned = eng2->toStringHandle(two);
       
   131         QCOMPARE(twoInterned.toString(), two);
       
   132         QVERIFY(oneInterned != twoInterned);
       
   133         QVERIFY(!(oneInterned == twoInterned));
       
   134         QScriptString copy1(oneInterned);
       
   135         QScriptString copy2 = oneInterned;
       
   136 
       
   137         delete eng2;
       
   138 
       
   139         QVERIFY(!oneInterned.isValid());
       
   140         QVERIFY(!twoInterned.isValid());
       
   141         QVERIFY(!copy1.isValid());
       
   142         QVERIFY(!copy2.isValid());
       
   143     }
       
   144 }
       
   145 
       
   146 void tst_QScriptString::hash()
       
   147 {
       
   148     QScriptEngine engine;
       
   149     QHash<QScriptString, int> stringToInt;
       
   150     QScriptString foo = engine.toStringHandle("foo");
       
   151     QScriptString bar = engine.toStringHandle("bar");
       
   152     QVERIFY(!stringToInt.contains(foo));
       
   153     for (int i = 0; i < 1000000; ++i)
       
   154         stringToInt.insert(foo, 123);
       
   155     QCOMPARE(stringToInt.value(foo), 123);
       
   156     QVERIFY(!stringToInt.contains(bar));
       
   157     stringToInt.insert(bar, 456);
       
   158     QCOMPARE(stringToInt.value(bar), 456);
       
   159     QCOMPARE(stringToInt.value(foo), 123);
       
   160 }
       
   161 
       
   162 void tst_QScriptString::toArrayIndex_data()
       
   163 {
       
   164     QTest::addColumn<QString>("input");
       
   165     QTest::addColumn<bool>("expectSuccess");
       
   166     QTest::addColumn<quint32>("expectedIndex");
       
   167     QTest::newRow("foo") << QString::fromLatin1("foo") << false << quint32(0xffffffff);
       
   168     QTest::newRow("empty") << QString::fromLatin1("") << false << quint32(0xffffffff);
       
   169     QTest::newRow("0") << QString::fromLatin1("0") << true << quint32(0);
       
   170     QTest::newRow("00") << QString::fromLatin1("00") << false << quint32(0xffffffff);
       
   171     QTest::newRow("1") << QString::fromLatin1("1") << true << quint32(1);
       
   172     QTest::newRow("123") << QString::fromLatin1("123") << true << quint32(123);
       
   173     QTest::newRow("-1") << QString::fromLatin1("-1") << false << quint32(0xffffffff);
       
   174     QTest::newRow("0a") << QString::fromLatin1("0a") << false << quint32(0xffffffff);
       
   175     QTest::newRow("0x1") << QString::fromLatin1("0x1") << false << quint32(0xffffffff);
       
   176     QTest::newRow("01") << QString::fromLatin1("01") << false << quint32(0xffffffff);
       
   177     QTest::newRow("101a") << QString::fromLatin1("101a") << false << quint32(0xffffffff);
       
   178     QTest::newRow("4294967294") << QString::fromLatin1("4294967294") << true << quint32(0xfffffffe);
       
   179     QTest::newRow("4294967295") << QString::fromLatin1("4294967295") << false << quint32(0xffffffff);
       
   180     QTest::newRow("0.0") << QString::fromLatin1("0.0") << false << quint32(0xffffffff);
       
   181     QTest::newRow("1.0") << QString::fromLatin1("1.0") << false << quint32(0xffffffff);
       
   182     QTest::newRow("1.5") << QString::fromLatin1("1.5") << false << quint32(0xffffffff);
       
   183     QTest::newRow("1.") << QString::fromLatin1("1.") << false << quint32(0xffffffff);
       
   184     QTest::newRow(".1") << QString::fromLatin1(".1") << false << quint32(0xffffffff);
       
   185     QTest::newRow("1e0") << QString::fromLatin1("1e0") << false << quint32(0xffffffff);
       
   186 }
       
   187 
       
   188 void tst_QScriptString::toArrayIndex()
       
   189 {
       
   190     QFETCH(QString, input);
       
   191     QFETCH(bool, expectSuccess);
       
   192     QFETCH(quint32, expectedIndex);
       
   193     QScriptEngine engine;
       
   194     for (int x = 0; x < 2; ++x) {
       
   195         bool isArrayIndex;
       
   196         bool *ptr = (x == 0) ? &isArrayIndex : (bool*)0;
       
   197         quint32 result = engine.toStringHandle(input).toArrayIndex(ptr);
       
   198         if (x == 0)
       
   199             QCOMPARE(isArrayIndex, expectSuccess);
       
   200         QCOMPARE(result, expectedIndex);
       
   201     }
       
   202 }
       
   203 
       
   204 QTEST_MAIN(tst_QScriptString)
       
   205 #include "tst_qscriptstring.moc"