2 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
20 #ifndef tst_qscriptstring_h
21 #define tst_qscriptstring_h
23 #include "qscriptengine.h"
24 #include "qscriptstring.h"
25 #include <QtCore/qhash.h>
26 #include <QtTest/QtTest>
28 class tst_QScriptString
: public QObject
{
33 virtual ~tst_QScriptString();
38 void toArrayIndex_data();
42 tst_QScriptString::tst_QScriptString()
46 tst_QScriptString::~tst_QScriptString()
50 void tst_QScriptString::test()
55 QVERIFY(!str
.isValid());
57 QVERIFY(!(str
!= str
));
58 QVERIFY(str
.toString().isNull());
60 QScriptString
str1(str
);
61 QVERIFY(!str1
.isValid());
63 QScriptString str2
= str
;
64 QVERIFY(!str2
.isValid());
66 QCOMPARE(str
.toArrayIndex(), quint32(0xffffffff));
68 for (int x
= 0; x
< 2; ++x
) {
69 QString ciao
= QString::fromLatin1("ciao");
70 QScriptString str
= eng
.toStringHandle(ciao
);
71 QVERIFY(str
.isValid());
73 QVERIFY(!(str
!= str
));
74 QCOMPARE(str
.toString(), ciao
);
76 QScriptString
str1(str
);
79 QScriptString str2
= str
;
82 QScriptString str3
= eng
.toStringHandle(ciao
);
83 QVERIFY(str3
.isValid());
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
);
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
));
112 void tst_QScriptString::hash()
114 QScriptEngine engine
;
115 QHash
<QScriptString
, int> stringToInt
;
116 QScriptString foo
= engine
.toStringHandle("foo");
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);
129 void tst_QScriptString::toArrayIndex_data()
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);
156 void tst_QScriptString::toArrayIndex()
158 QFETCH(QString
, input
);
159 QFETCH(bool, expectSuccess
);
160 QFETCH(quint32
, expectedIndex
);
161 QScriptEngine engine
;
162 for (int x
= 0; x
< 2; ++x
) {
164 bool* ptr
= (!x
) ? &isArrayIndex
: (bool*)0;
165 quint32 result
= engine
.toStringHandle(input
).toArrayIndex(ptr
);
167 QCOMPARE(isArrayIndex
, expectSuccess
);
168 QCOMPARE(result
, expectedIndex
);
172 QTEST_MAIN(tst_QScriptString
)
173 #include "tst_qscriptstring.moc"
175 #endif // tst_qscriptstring_h