author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 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(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
void toArrayIndex_data(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
63 |
void toArrayIndex(); |
0 | 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 |
||
92 |
for (int x = 0; x < 2; ++x) { |
|
93 |
QString ciao = QString::fromLatin1("ciao"); |
|
94 |
QScriptString str = eng.toStringHandle(ciao); |
|
95 |
QVERIFY(str.isValid()); |
|
96 |
QVERIFY(str == str); |
|
97 |
QVERIFY(!(str != str)); |
|
98 |
QCOMPARE(str.toString(), ciao); |
|
99 |
||
100 |
QScriptString str1(str); |
|
101 |
QCOMPARE(str, str1); |
|
102 |
||
103 |
QScriptString str2 = str; |
|
104 |
QCOMPARE(str, str2); |
|
105 |
||
106 |
QScriptString str3 = eng.toStringHandle(ciao); |
|
107 |
QVERIFY(str3.isValid()); |
|
108 |
QCOMPARE(str, str3); |
|
109 |
||
110 |
eng.collectGarbage(); |
|
111 |
||
112 |
QVERIFY(str.isValid()); |
|
113 |
QCOMPARE(str.toString(), ciao); |
|
114 |
QVERIFY(str1.isValid()); |
|
115 |
QCOMPARE(str1.toString(), ciao); |
|
116 |
QVERIFY(str2.isValid()); |
|
117 |
QCOMPARE(str2.toString(), ciao); |
|
118 |
QVERIFY(str3.isValid()); |
|
119 |
QCOMPARE(str3.toString(), ciao); |
|
120 |
} |
|
121 |
||
122 |
{ |
|
123 |
QScriptEngine *eng2 = new QScriptEngine; |
|
124 |
QString one = QString::fromLatin1("one"); |
|
125 |
QString two = QString::fromLatin1("two"); |
|
126 |
QScriptString oneInterned = eng2->toStringHandle(one); |
|
127 |
QCOMPARE(oneInterned.toString(), one); |
|
128 |
QScriptString twoInterned = eng2->toStringHandle(two); |
|
129 |
QCOMPARE(twoInterned.toString(), two); |
|
130 |
QVERIFY(oneInterned != twoInterned); |
|
131 |
QVERIFY(!(oneInterned == twoInterned)); |
|
132 |
QScriptString copy1(oneInterned); |
|
133 |
QScriptString copy2 = oneInterned; |
|
134 |
||
135 |
delete eng2; |
|
136 |
||
137 |
QVERIFY(!oneInterned.isValid()); |
|
138 |
QVERIFY(!twoInterned.isValid()); |
|
139 |
QVERIFY(!copy1.isValid()); |
|
140 |
QVERIFY(!copy2.isValid()); |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
void tst_QScriptString::hash() |
|
145 |
{ |
|
146 |
QScriptEngine engine; |
|
147 |
QHash<QScriptString, int> stringToInt; |
|
148 |
QScriptString foo = engine.toStringHandle("foo"); |
|
149 |
QScriptString bar = engine.toStringHandle("bar"); |
|
150 |
QVERIFY(!stringToInt.contains(foo)); |
|
151 |
for (int i = 0; i < 1000000; ++i) |
|
152 |
stringToInt.insert(foo, 123); |
|
153 |
QCOMPARE(stringToInt.value(foo), 123); |
|
154 |
QVERIFY(!stringToInt.contains(bar)); |
|
155 |
stringToInt.insert(bar, 456); |
|
156 |
QCOMPARE(stringToInt.value(bar), 456); |
|
157 |
QCOMPARE(stringToInt.value(foo), 123); |
|
158 |
} |
|
159 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
void tst_QScriptString::toArrayIndex_data() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
QTest::addColumn<QString>("input"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
QTest::addColumn<bool>("expectSuccess"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
QTest::addColumn<quint32>("expectedIndex"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
QTest::newRow("foo") << QString::fromLatin1("foo") << false << quint32(0xffffffff); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
QTest::newRow("empty") << QString::fromLatin1("") << false << quint32(0xffffffff); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
QTest::newRow("0") << QString::fromLatin1("0") << true << quint32(0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
QTest::newRow("00") << QString::fromLatin1("00") << false << quint32(0xffffffff); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
QTest::newRow("1") << QString::fromLatin1("1") << true << quint32(1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
QTest::newRow("123") << QString::fromLatin1("123") << true << quint32(123); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
QTest::newRow("-1") << QString::fromLatin1("-1") << false << quint32(0xffffffff); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
QTest::newRow("0a") << QString::fromLatin1("0a") << false << quint32(0xffffffff); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
QTest::newRow("0x1") << QString::fromLatin1("0x1") << false << quint32(0xffffffff); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
QTest::newRow("01") << QString::fromLatin1("01") << false << quint32(0xffffffff); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
QTest::newRow("4294967294") << QString::fromLatin1("4294967294") << true << quint32(0xfffffffe); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
QTest::newRow("4294967295") << QString::fromLatin1("4294967295") << false << quint32(0xffffffff); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
void tst_QScriptString::toArrayIndex() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
QFETCH(QString, input); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
QFETCH(bool, expectSuccess); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
QFETCH(quint32, expectedIndex); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
184 |
QScriptEngine engine; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
185 |
for (int x = 0; x < 2; ++x) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
186 |
bool isArrayIndex; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
187 |
bool *ptr = (x == 0) ? &isArrayIndex : (bool*)0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
188 |
quint32 result = engine.toStringHandle(input).toArrayIndex(ptr); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
189 |
if (x == 0) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
190 |
QCOMPARE(isArrayIndex, expectSuccess); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
191 |
QCOMPARE(result, expectedIndex); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
192 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
193 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
194 |
|
0 | 195 |
QTEST_MAIN(tst_QScriptString) |
196 |
#include "tst_qscriptstring.moc" |