1 /* 2 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Library General Public 6 License as published by the Free Software Foundation; either 7 version 2 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Library General Public License for more details. 13 14 You should have received a copy of the GNU Library General Public License 15 along with this library; see the file COPYING.LIB. If not, write to 16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 Boston, MA 02110-1301, USA. 18 */ 19 20 #ifndef tst_qscriptstring_h 21 #define tst_qscriptstring_h 22 23 #include "qscriptengine.h" 24 #include "qscriptstring.h" 25 #include <QtCore/qhash.h> 26 #include <QtTest/QtTest> 27 28 class tst_QScriptString : public QObject { 29 Q_OBJECT 30 31 public: 32 tst_QScriptString(); 33 virtual ~tst_QScriptString(); 34 35 private slots: 36 void test(); 37 void hash(); 38 void toArrayIndex_data(); 39 void toArrayIndex(); 40 }; 41 42 tst_QScriptString::tst_QScriptString() 43 { 44 } 45 46 tst_QScriptString::~tst_QScriptString() 47 { 48 } 49 50 void tst_QScriptString::test() 51 { 52 QScriptEngine eng; 53 { 54 QScriptString str; 55 QVERIFY(!str.isValid()); 56 QVERIFY(str == str); 57 QVERIFY(!(str != str)); 58 QVERIFY(str.toString().isNull()); 59 60 QScriptString str1(str); 61 QVERIFY(!str1.isValid()); 62 63 QScriptString str2 = str; 64 QVERIFY(!str2.isValid()); 65 66 QCOMPARE(str.toArrayIndex(), quint32(0xffffffff)); 67 } 68 for (int x = 0; x < 2; ++x) { 69 QString ciao = QString::fromLatin1("ciao"); 70 QScriptString str = eng.toStringHandle(ciao); 71 QVERIFY(str.isValid()); 72 QVERIFY(str == str); 73 QVERIFY(!(str != str)); 74 QCOMPARE(str.toString(), ciao); 75 76 QScriptString str1(str); 77 QCOMPARE(str, str1); 78 79 QScriptString str2 = str; 80 QCOMPARE(str, str2); 81 82 QScriptString str3 = eng.toStringHandle(ciao); 83 QVERIFY(str3.isValid()); 84 QCOMPARE(str, str3); 85 86 eng.collectGarbage(); 87 88 QVERIFY(str.isValid()); 89 QCOMPARE(str.toString(), ciao); 90 QVERIFY(str1.isValid()); 91 QCOMPARE(str1.toString(), ciao); 92 QVERIFY(str2.isValid()); 93 QCOMPARE(str2.toString(), ciao); 94 QVERIFY(str3.isValid()); 95 QCOMPARE(str3.toString(), ciao); 96 } 97 { 98 QScriptEngine* eng2 = new QScriptEngine; 99 QString one = QString::fromLatin1("one"); 100 QString two = QString::fromLatin1("two"); 101 QScriptString oneInterned = eng2->toStringHandle(one); 102 QCOMPARE(oneInterned.toString(), one); 103 QScriptString twoInterned = eng2->toStringHandle(two); 104 QCOMPARE(twoInterned.toString(), two); 105 QVERIFY(oneInterned != twoInterned); 106 QVERIFY(!(oneInterned == twoInterned)); 107 108 delete eng2; 109 } 110 } 111 112 void tst_QScriptString::hash() 113 { 114 QScriptEngine engine; 115 QHash<QScriptString, int> stringToInt; 116 QScriptString foo = engine.toStringHandle("foo"); 117 118 QScriptString bar = engine.toStringHandle("bar"); 119 QVERIFY(!stringToInt.contains(foo)); 120 for (int i = 0; i < 1000000; ++i) 121 stringToInt.insert(foo, 123); 122 QCOMPARE(stringToInt.value(foo), 123); 123 QVERIFY(!stringToInt.contains(bar)); 124 stringToInt.insert(bar, 456); 125 QCOMPARE(stringToInt.value(bar), 456); 126 QCOMPARE(stringToInt.value(foo), 123); 127 } 128 129 void tst_QScriptString::toArrayIndex_data() 130 { 131 QTest::addColumn<QString>("input"); 132 QTest::addColumn<bool>("expectSuccess"); 133 QTest::addColumn<quint32>("expectedIndex"); 134 QTest::newRow("foo") << QString::fromLatin1("foo") << false << quint32(0xffffffff); 135 QTest::newRow("empty") << QString::fromLatin1("") << false << quint32(0xffffffff); 136 QTest::newRow("0") << QString::fromLatin1("0") << true << quint32(0); 137 QTest::newRow("00") << QString::fromLatin1("00") << false << quint32(0xffffffff); 138 QTest::newRow("1") << QString::fromLatin1("1") << true << quint32(1); 139 QTest::newRow("123") << QString::fromLatin1("123") << true << quint32(123); 140 QTest::newRow("-1") << QString::fromLatin1("-1") << false << quint32(0xffffffff); 141 QTest::newRow("0a") << QString::fromLatin1("0a") << false << quint32(0xffffffff); 142 QTest::newRow("0x1") << QString::fromLatin1("0x1") << false << quint32(0xffffffff); 143 QTest::newRow("01") << QString::fromLatin1("01") << false << quint32(0xffffffff); 144 QTest::newRow("101a") << QString::fromLatin1("101a") << false << quint32(0xffffffff); 145 QTest::newRow("4294967294") << QString::fromLatin1("4294967294") << true << quint32(0xfffffffe); 146 QTest::newRow("4294967295") << QString::fromLatin1("4294967295") << false << quint32(0xffffffff); 147 QTest::newRow("11111111111") << QString::fromLatin1("11111111111") << false << quint32(0xffffffff); 148 QTest::newRow("0.0") << QString::fromLatin1("0.0") << false << quint32(0xffffffff); 149 QTest::newRow("1.0") << QString::fromLatin1("1.0") << false << quint32(0xffffffff); 150 QTest::newRow("1.5") << QString::fromLatin1("1.5") << false << quint32(0xffffffff); 151 QTest::newRow("1.") << QString::fromLatin1("1.") << false << quint32(0xffffffff); 152 QTest::newRow(".1") << QString::fromLatin1(".1") << false << quint32(0xffffffff); 153 QTest::newRow("1e0") << QString::fromLatin1("1e0") << false << quint32(0xffffffff); 154 } 155 156 void tst_QScriptString::toArrayIndex() 157 { 158 QFETCH(QString, input); 159 QFETCH(bool, expectSuccess); 160 QFETCH(quint32, expectedIndex); 161 QScriptEngine engine; 162 for (int x = 0; x < 2; ++x) { 163 bool isArrayIndex; 164 bool* ptr = (!x) ? &isArrayIndex : (bool*)0; 165 quint32 result = engine.toStringHandle(input).toArrayIndex(ptr); 166 if (!x) 167 QCOMPARE(isArrayIndex, expectSuccess); 168 QCOMPARE(result, expectedIndex); 169 } 170 } 171 172 QTEST_MAIN(tst_QScriptString) 173 #include "tst_qscriptstring.moc" 174 175 #endif // tst_qscriptstring_h 176