Debian Codegraph @

tests/auto/qscriptstring/tst_qscriptstring.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29
30 #include <QtTest/QtTest>
31
32 #include <QtScript/qscriptengine.h>
33 #include <QtScript/qscriptstring.h>
34
35 class tst_QScriptString : public QObject
36 {
37 Q_OBJECT
38
39 public:
40 tst_QScriptString();
41 virtual ~tst_QScriptString();
42
43 private slots:
44 void test();
45 void hash();
46 void toArrayIndex_data();
47 void toArrayIndex();
48 };
49
50 tst_QScriptString::tst_QScriptString()
51 {
52 }
53
54 tst_QScriptString::~tst_QScriptString()
55 {
56 }
57
58 void tst_QScriptString::test()
59 {
60 QScriptEngine eng;
61
62 {
63 QScriptString str;
64 QVERIFY(!str.isValid());
65 QVERIFY(str == str);
66 QVERIFY(!(str != str));
67 QVERIFY(str.toString().isNull());
68
69 QScriptString str1(str);
70 QVERIFY(!str1.isValid());
71
72 QScriptString str2 = str;
73 QVERIFY(!str2.isValid());
74
75 QCOMPARE(str.toArrayIndex(), quint32(0xffffffff));
76 }
77
78 for (int x = 0; x < 2; ++x) {
79 QString ciao = QString::fromLatin1("ciao");
80 QScriptString str = eng.toStringHandle(ciao);
81 QVERIFY(str.isValid());
82 QVERIFY(str == str);
83 QVERIFY(!(str != str));
84 QCOMPARE(str.toString(), ciao);
85
86 QScriptString str1(str);
87 QCOMPARE(str, str1);
88
89 QScriptString str2 = str;
90 QCOMPARE(str, str2);
91
92 QScriptString str3 = eng.toStringHandle(ciao);
93 QVERIFY(str3.isValid());
94 QCOMPARE(str, str3);
95
96 eng.collectGarbage();
97
98 QVERIFY(str.isValid());
99 QCOMPARE(str.toString(), ciao);
100 QVERIFY(str1.isValid());
101 QCOMPARE(str1.toString(), ciao);
102 QVERIFY(str2.isValid());
103 QCOMPARE(str2.toString(), ciao);
104 QVERIFY(str3.isValid());
105 QCOMPARE(str3.toString(), ciao);
106 }
107
108 {
109 QScriptEngine *eng2 = new QScriptEngine;
110 QString one = QString::fromLatin1("one");
111 QString two = QString::fromLatin1("two");
112 QScriptString oneInterned = eng2->toStringHandle(one);
113 QCOMPARE(oneInterned.toString(), one);
114 QScriptString twoInterned = eng2->toStringHandle(two);
115 QCOMPARE(twoInterned.toString(), two);
116 QVERIFY(oneInterned != twoInterned);
117 QVERIFY(!(oneInterned == twoInterned));
118 QScriptString copy1(oneInterned);
119 QScriptString copy2 = oneInterned;
120
121 delete eng2;
122
123 QVERIFY(!oneInterned.isValid());
124 QVERIFY(!twoInterned.isValid());
125 QVERIFY(!copy1.isValid());
126 QVERIFY(!copy2.isValid());
127 }
128 }
129
130 void tst_QScriptString::hash()
131 {
132 QScriptEngine engine;
133 QHash<QScriptString, int> stringToInt;
134 QScriptString foo = engine.toStringHandle("foo");
135 QScriptString bar = engine.toStringHandle("bar");
136 QVERIFY(!stringToInt.contains(foo));
137 for (int i = 0; i < 1000000; ++i)
138 stringToInt.insert(foo, 123);
139 QCOMPARE(stringToInt.value(foo), 123);
140 QVERIFY(!stringToInt.contains(bar));
141 stringToInt.insert(bar, 456);
142 QCOMPARE(stringToInt.value(bar), 456);
143 QCOMPARE(stringToInt.value(foo), 123);
144 }
145
146 void tst_QScriptString::toArrayIndex_data()
147 {
148 QTest::addColumn<QString>("input");
149 QTest::addColumn<bool>("expectSuccess");
150 QTest::addColumn<quint32>("expectedIndex");
151 QTest::newRow("foo") << QString::fromLatin1("foo") << false << quint32(0xffffffff);
152 QTest::newRow("empty") << QString::fromLatin1("") << false << quint32(0xffffffff);
153 QTest::newRow("0") << QString::fromLatin1("0") << true << quint32(0);
154 QTest::newRow("00") << QString::fromLatin1("00") << false << quint32(0xffffffff);
155 QTest::newRow("1") << QString::fromLatin1("1") << true << quint32(1);
156 QTest::newRow("123") << QString::fromLatin1("123") << true << quint32(123);
157 QTest::newRow("-1") << QString::fromLatin1("-1") << false << quint32(0xffffffff);
158 QTest::newRow("0a") << QString::fromLatin1("0a") << false << quint32(0xffffffff);
159 QTest::newRow("0x1") << QString::fromLatin1("0x1") << false << quint32(0xffffffff);
160 QTest::newRow("01") << QString::fromLatin1("01") << false << quint32(0xffffffff);
161 QTest::newRow("101a") << QString::fromLatin1("101a") << false << quint32(0xffffffff);
162 QTest::newRow("4294967294") << QString::fromLatin1("4294967294") << true << quint32(0xfffffffe);
163 QTest::newRow("4294967295") << QString::fromLatin1("4294967295") << false << quint32(0xffffffff);
164 QTest::newRow("0.0") << QString::fromLatin1("0.0") << false << quint32(0xffffffff);
165 QTest::newRow("1.0") << QString::fromLatin1("1.0") << false << quint32(0xffffffff);
166 QTest::newRow("1.5") << QString::fromLatin1("1.5") << false << quint32(0xffffffff);
167 QTest::newRow("1.") << QString::fromLatin1("1.") << false << quint32(0xffffffff);
168 QTest::newRow(".1") << QString::fromLatin1(".1") << false << quint32(0xffffffff);
169 QTest::newRow("1e0") << QString::fromLatin1("1e0") << false << quint32(0xffffffff);
170 }
171
172 void tst_QScriptString::toArrayIndex()
173 {
174 QFETCH(QString, input);
175 QFETCH(bool, expectSuccess);
176 QFETCH(quint32, expectedIndex);
177 QScriptEngine engine;
178 for (int x = 0; x < 2; ++x) {
179 bool isArrayIndex;
180 bool *ptr = (x == 0) ? &isArrayIndex : (bool*)0;
181 quint32 result = engine.toStringHandle(input).toArrayIndex(ptr);
182 if (x == 0)
183 QCOMPARE(isArrayIndex, expectSuccess);
184 QCOMPARE(result, expectedIndex);
185 }
186 }
187
188 QTEST_MAIN(tst_QScriptString)
189 #include "tst_qscriptstring.moc"