tests/auto/qscriptstring/tst_qscriptstring.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 
       
    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 };
       
    63 
       
    64 tst_QScriptString::tst_QScriptString()
       
    65 {
       
    66 }
       
    67 
       
    68 tst_QScriptString::~tst_QScriptString()
       
    69 {
       
    70 }
       
    71 
       
    72 void tst_QScriptString::test()
       
    73 {
       
    74     QScriptEngine eng;
       
    75 
       
    76     {
       
    77         QScriptString str;
       
    78         QVERIFY(!str.isValid());
       
    79         QVERIFY(str == str);
       
    80         QVERIFY(!(str != str));
       
    81         QVERIFY(str.toString().isNull());
       
    82 
       
    83         QScriptString str1(str);
       
    84         QVERIFY(!str1.isValid());
       
    85 
       
    86         QScriptString str2 = str;
       
    87         QVERIFY(!str2.isValid());
       
    88     }
       
    89 
       
    90     for (int x = 0; x < 2; ++x) {
       
    91         QString ciao = QString::fromLatin1("ciao");
       
    92         QScriptString str = eng.toStringHandle(ciao);
       
    93         QVERIFY(str.isValid());
       
    94         QVERIFY(str == str);
       
    95         QVERIFY(!(str != str));
       
    96         QCOMPARE(str.toString(), ciao);
       
    97 
       
    98         QScriptString str1(str);
       
    99         QCOMPARE(str, str1);
       
   100 
       
   101         QScriptString str2 = str;
       
   102         QCOMPARE(str, str2);
       
   103 
       
   104         QScriptString str3 = eng.toStringHandle(ciao);
       
   105         QVERIFY(str3.isValid());
       
   106         QCOMPARE(str, str3);
       
   107 
       
   108         eng.collectGarbage();
       
   109 
       
   110         QVERIFY(str.isValid());
       
   111         QCOMPARE(str.toString(), ciao);
       
   112         QVERIFY(str1.isValid());
       
   113         QCOMPARE(str1.toString(), ciao);
       
   114         QVERIFY(str2.isValid());
       
   115         QCOMPARE(str2.toString(), ciao);
       
   116         QVERIFY(str3.isValid());
       
   117         QCOMPARE(str3.toString(), ciao);
       
   118     }
       
   119 
       
   120     {
       
   121         QScriptEngine *eng2 = new QScriptEngine;
       
   122         QString one = QString::fromLatin1("one");
       
   123         QString two = QString::fromLatin1("two");
       
   124         QScriptString oneInterned = eng2->toStringHandle(one);
       
   125         QCOMPARE(oneInterned.toString(), one);
       
   126         QScriptString twoInterned = eng2->toStringHandle(two);
       
   127         QCOMPARE(twoInterned.toString(), two);
       
   128         QVERIFY(oneInterned != twoInterned);
       
   129         QVERIFY(!(oneInterned == twoInterned));
       
   130         QScriptString copy1(oneInterned);
       
   131         QScriptString copy2 = oneInterned;
       
   132 
       
   133         delete eng2;
       
   134 
       
   135         QVERIFY(!oneInterned.isValid());
       
   136         QVERIFY(!twoInterned.isValid());
       
   137         QVERIFY(!copy1.isValid());
       
   138         QVERIFY(!copy2.isValid());
       
   139     }
       
   140 }
       
   141 
       
   142 void tst_QScriptString::hash()
       
   143 {
       
   144     QScriptEngine engine;
       
   145     QHash<QScriptString, int> stringToInt;
       
   146     QScriptString foo = engine.toStringHandle("foo");
       
   147     QScriptString bar = engine.toStringHandle("bar");
       
   148     QVERIFY(!stringToInt.contains(foo));
       
   149     for (int i = 0; i < 1000000; ++i)
       
   150         stringToInt.insert(foo, 123);
       
   151     QCOMPARE(stringToInt.value(foo), 123);
       
   152     QVERIFY(!stringToInt.contains(bar));
       
   153     stringToInt.insert(bar, 456);
       
   154     QCOMPARE(stringToInt.value(bar), 456);
       
   155     QCOMPARE(stringToInt.value(foo), 123);
       
   156 }
       
   157 
       
   158 QTEST_MAIN(tst_QScriptString)
       
   159 #include "tst_qscriptstring.moc"