1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2016 Intel Corporation. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the test suite of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <qstringlist.h> |
32 | #include <qvariant.h> |
33 | |
34 | #include <qlocale.h> |
35 | #include <locale.h> |
36 | |
37 | |
38 | class tst_QStringRef : public QObject |
39 | { |
40 | Q_OBJECT |
41 | public slots: |
42 | void cleanup(); |
43 | private slots: |
44 | void at(); |
45 | void endsWith(); |
46 | void startsWith(); |
47 | void contains(); |
48 | void count(); |
49 | void lastIndexOf_data(); |
50 | void lastIndexOf(); |
51 | void indexOf_data(); |
52 | void indexOf(); |
53 | void indexOf2_data(); |
54 | void indexOf2(); |
55 | void iteration(); |
56 | void length_data(); |
57 | void length(); |
58 | void isEmpty(); |
59 | void compare_data(); |
60 | void compare(); |
61 | void compare2_data(); |
62 | void compare2(); |
63 | void operator_eqeq_nullstring(); |
64 | void toNum(); |
65 | void toDouble_data(); |
66 | void toDouble(); |
67 | void toFloat(); |
68 | void toLong_data(); |
69 | void toLong(); |
70 | void toULong_data(); |
71 | void toULong(); |
72 | void toLongLong(); |
73 | void toULongLong(); |
74 | void toUInt(); |
75 | void toInt(); |
76 | void toShort(); |
77 | void toUShort(); |
78 | void double_conversion_data(); |
79 | void double_conversion(); |
80 | void integer_conversion_data(); |
81 | void integer_conversion(); |
82 | void trimmed(); |
83 | void truncate(); |
84 | void chop(); |
85 | void left(); |
86 | void right(); |
87 | void mid(); |
88 | void split_data(); |
89 | void split(); |
90 | }; |
91 | |
92 | static QStringRef emptyRef() |
93 | { |
94 | static const QString empty("" ); |
95 | return empty.midRef(position: 0); |
96 | } |
97 | |
98 | #define CREATE_REF(string) \ |
99 | const QString padded = QLatin1Char(' ') + string + QLatin1Char(' '); \ |
100 | QStringRef ref = padded.midRef(1, padded.size() - 2); |
101 | |
102 | typedef QList<int> IntList; |
103 | |
104 | // This next bit is needed for the NAN and INF in string -> number conversion tests |
105 | #include <float.h> |
106 | #include <limits.h> |
107 | #include <math.h> |
108 | #if defined(Q_OS_WIN) |
109 | # include <windows.h> |
110 | // mingw defines NAN and INFINITY to 0/0 and x/0 |
111 | # if defined(Q_CC_GNU) |
112 | # undef NAN |
113 | # undef INFINITY |
114 | # else |
115 | # define isnan(d) _isnan(d) |
116 | # endif |
117 | #endif |
118 | #if defined(Q_OS_MAC) && !defined isnan |
119 | #define isnan(d) __isnand(d) |
120 | #endif |
121 | #if defined(Q_OS_SOLARIS) |
122 | # include <ieeefp.h> |
123 | #endif |
124 | |
125 | enum { |
126 | LittleEndian, |
127 | BigEndian |
128 | #ifdef Q_BYTE_ORDER |
129 | # if Q_BYTE_ORDER == Q_BIG_ENDIAN |
130 | , ByteOrder = BigEndian |
131 | # elif Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
132 | , ByteOrder = LittleEndian |
133 | # else |
134 | # error "undefined byte order" |
135 | # endif |
136 | }; |
137 | #else |
138 | }; |
139 | static const unsigned int one = 1; |
140 | static const bool ByteOrder = ((*((unsigned char *) &one) == 0) ? BigEndian : LittleEndian); |
141 | #endif |
142 | #if !defined(INFINITY) |
143 | static const unsigned char be_inf_bytes[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0,0 }; |
144 | static const unsigned char le_inf_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f }; |
145 | static inline double inf() |
146 | { |
147 | if (ByteOrder == BigEndian) |
148 | return *reinterpret_cast<const double *>(be_inf_bytes); |
149 | return *reinterpret_cast<const double *>(le_inf_bytes); |
150 | } |
151 | # define INFINITY (::inf()) |
152 | #endif |
153 | #if !defined(NAN) |
154 | static const unsigned char be_nan_bytes[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0,0 }; |
155 | static const unsigned char le_nan_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f }; |
156 | static inline double nan() |
157 | { |
158 | if (ByteOrder == BigEndian) |
159 | return *reinterpret_cast<const double *>(be_nan_bytes); |
160 | return *reinterpret_cast<const double *>(le_nan_bytes); |
161 | } |
162 | # define NAN (::nan()) |
163 | #endif |
164 | |
165 | void tst_QStringRef::cleanup() |
166 | { |
167 | QLocale::setDefault(QString(QLatin1Char('C'))); |
168 | } |
169 | |
170 | void tst_QStringRef::at() |
171 | { |
172 | const QString hw = QStringLiteral("Hello World" ); |
173 | const QStringRef ref = hw.midRef(position: 6); |
174 | QCOMPARE(ref.at(0), QChar('W')); |
175 | QCOMPARE(ref.at(4), QChar('d')); |
176 | QCOMPARE(ref[0], QChar('W')); |
177 | QCOMPARE(ref[4], QChar('d')); |
178 | } |
179 | |
180 | void tst_QStringRef::length_data() |
181 | { |
182 | QTest::addColumn<QString>(name: "s1" ); |
183 | QTest::addColumn<int>(name: "res" ); |
184 | |
185 | QTest::newRow(dataTag: "data0" ) << QString("Test" ) << 4; |
186 | QTest::newRow(dataTag: "data1" ) << QString("The quick brown fox jumps over the lazy dog" ) << 43; |
187 | QTest::newRow(dataTag: "data2" ) << QString() << 0; |
188 | QTest::newRow(dataTag: "data3" ) << QString("A" ) << 1; |
189 | QTest::newRow(dataTag: "data4" ) << QString("AB" ) << 2; |
190 | QTest::newRow(dataTag: "data5" ) << QString("AB\n" ) << 3; |
191 | QTest::newRow(dataTag: "data6" ) << QString("AB\nC" ) << 4; |
192 | QTest::newRow(dataTag: "data7" ) << QString("\n" ) << 1; |
193 | QTest::newRow(dataTag: "data8" ) << QString("\nA" ) << 2; |
194 | QTest::newRow(dataTag: "data9" ) << QString("\nAB" ) << 3; |
195 | QTest::newRow(dataTag: "data10" ) << QString("\nAB\nCDE" ) << 7; |
196 | QTest::newRow(dataTag: "data11" ) << QString("shdnftrheid fhgnt gjvnfmd chfugkh bnfhg thgjf vnghturkf chfnguh bjgnfhvygh hnbhgutjfv dhdnjds dcjs d" ) << 100; |
197 | QTest::newRow(dataTag: "data12" ) << QString("" ) << 0; |
198 | } |
199 | |
200 | |
201 | void tst_QStringRef::length() |
202 | { |
203 | QFETCH(QString, s1); |
204 | CREATE_REF(s1); |
205 | QTEST(ref.length(), "res" ); |
206 | } |
207 | |
208 | |
209 | void tst_QStringRef::isEmpty() |
210 | { |
211 | QStringRef a; |
212 | QVERIFY(a.isEmpty()); |
213 | QVERIFY(emptyRef().isEmpty()); |
214 | CREATE_REF("Not empty" ); |
215 | QVERIFY(!ref.isEmpty()); |
216 | } |
217 | |
218 | void tst_QStringRef::indexOf_data() |
219 | { |
220 | QTest::addColumn<QString>(name: "haystack" ); |
221 | QTest::addColumn<QString>(name: "needle" ); |
222 | QTest::addColumn<int>(name: "startpos" ); |
223 | QTest::addColumn<bool>(name: "bcs" ); |
224 | QTest::addColumn<int>(name: "resultpos" ); |
225 | |
226 | QTest::newRow(dataTag: "data0" ) << QString("abc" ) << QString("a" ) << 0 << true << 0; |
227 | QTest::newRow(dataTag: "data1" ) << QString("abc" ) << QString("a" ) << 0 << false << 0; |
228 | QTest::newRow(dataTag: "data2" ) << QString("abc" ) << QString("A" ) << 0 << true << -1; |
229 | QTest::newRow(dataTag: "data3" ) << QString("abc" ) << QString("A" ) << 0 << false << 0; |
230 | QTest::newRow(dataTag: "data4" ) << QString("abc" ) << QString("a" ) << 1 << true << -1; |
231 | QTest::newRow(dataTag: "data5" ) << QString("abc" ) << QString("a" ) << 1 << false << -1; |
232 | QTest::newRow(dataTag: "data6" ) << QString("abc" ) << QString("A" ) << 1 << true << -1; |
233 | QTest::newRow(dataTag: "data7" ) << QString("abc" ) << QString("A" ) << 1 << false << -1; |
234 | QTest::newRow(dataTag: "data8" ) << QString("abc" ) << QString("b" ) << 0 << true << 1; |
235 | QTest::newRow(dataTag: "data9" ) << QString("abc" ) << QString("b" ) << 0 << false << 1; |
236 | QTest::newRow(dataTag: "data10" ) << QString("abc" ) << QString("B" ) << 0 << true << -1; |
237 | QTest::newRow(dataTag: "data11" ) << QString("abc" ) << QString("B" ) << 0 << false << 1; |
238 | QTest::newRow(dataTag: "data12" ) << QString("abc" ) << QString("b" ) << 1 << true << 1; |
239 | QTest::newRow(dataTag: "data13" ) << QString("abc" ) << QString("b" ) << 1 << false << 1; |
240 | QTest::newRow(dataTag: "data14" ) << QString("abc" ) << QString("B" ) << 1 << true << -1; |
241 | QTest::newRow(dataTag: "data15" ) << QString("abc" ) << QString("B" ) << 1 << false << 1; |
242 | QTest::newRow(dataTag: "data16" ) << QString("abc" ) << QString("b" ) << 2 << true << -1; |
243 | QTest::newRow(dataTag: "data17" ) << QString("abc" ) << QString("b" ) << 2 << false << -1; |
244 | |
245 | QTest::newRow(dataTag: "data20" ) << QString("ABC" ) << QString("A" ) << 0 << true << 0; |
246 | QTest::newRow(dataTag: "data21" ) << QString("ABC" ) << QString("A" ) << 0 << false << 0; |
247 | QTest::newRow(dataTag: "data22" ) << QString("ABC" ) << QString("a" ) << 0 << true << -1; |
248 | QTest::newRow(dataTag: "data23" ) << QString("ABC" ) << QString("a" ) << 0 << false << 0; |
249 | QTest::newRow(dataTag: "data24" ) << QString("ABC" ) << QString("A" ) << 1 << true << -1; |
250 | QTest::newRow(dataTag: "data25" ) << QString("ABC" ) << QString("A" ) << 1 << false << -1; |
251 | QTest::newRow(dataTag: "data26" ) << QString("ABC" ) << QString("a" ) << 1 << true << -1; |
252 | QTest::newRow(dataTag: "data27" ) << QString("ABC" ) << QString("a" ) << 1 << false << -1; |
253 | QTest::newRow(dataTag: "data28" ) << QString("ABC" ) << QString("B" ) << 0 << true << 1; |
254 | QTest::newRow(dataTag: "data29" ) << QString("ABC" ) << QString("B" ) << 0 << false << 1; |
255 | QTest::newRow(dataTag: "data30" ) << QString("ABC" ) << QString("b" ) << 0 << true << -1; |
256 | QTest::newRow(dataTag: "data31" ) << QString("ABC" ) << QString("b" ) << 0 << false << 1; |
257 | QTest::newRow(dataTag: "data32" ) << QString("ABC" ) << QString("B" ) << 1 << true << 1; |
258 | QTest::newRow(dataTag: "data33" ) << QString("ABC" ) << QString("B" ) << 1 << false << 1; |
259 | QTest::newRow(dataTag: "data34" ) << QString("ABC" ) << QString("b" ) << 1 << true << -1; |
260 | QTest::newRow(dataTag: "data35" ) << QString("ABC" ) << QString("b" ) << 1 << false << 1; |
261 | QTest::newRow(dataTag: "data36" ) << QString("ABC" ) << QString("B" ) << 2 << true << -1; |
262 | QTest::newRow(dataTag: "data37" ) << QString("ABC" ) << QString("B" ) << 2 << false << -1; |
263 | |
264 | QTest::newRow(dataTag: "data40" ) << QString("aBc" ) << QString("bc" ) << 0 << true << -1; |
265 | QTest::newRow(dataTag: "data41" ) << QString("aBc" ) << QString("Bc" ) << 0 << true << 1; |
266 | QTest::newRow(dataTag: "data42" ) << QString("aBc" ) << QString("bC" ) << 0 << true << -1; |
267 | QTest::newRow(dataTag: "data43" ) << QString("aBc" ) << QString("BC" ) << 0 << true << -1; |
268 | QTest::newRow(dataTag: "data44" ) << QString("aBc" ) << QString("bc" ) << 0 << false << 1; |
269 | QTest::newRow(dataTag: "data45" ) << QString("aBc" ) << QString("Bc" ) << 0 << false << 1; |
270 | QTest::newRow(dataTag: "data46" ) << QString("aBc" ) << QString("bC" ) << 0 << false << 1; |
271 | QTest::newRow(dataTag: "data47" ) << QString("aBc" ) << QString("BC" ) << 0 << false << 1; |
272 | QTest::newRow(dataTag: "data48" ) << QString("AbC" ) << QString("bc" ) << 0 << true << -1; |
273 | QTest::newRow(dataTag: "data49" ) << QString("AbC" ) << QString("Bc" ) << 0 << true << -1; |
274 | QTest::newRow(dataTag: "data50" ) << QString("AbC" ) << QString("bC" ) << 0 << true << 1; |
275 | QTest::newRow(dataTag: "data51" ) << QString("AbC" ) << QString("BC" ) << 0 << true << -1; |
276 | QTest::newRow(dataTag: "data52" ) << QString("AbC" ) << QString("bc" ) << 0 << false << 1; |
277 | QTest::newRow(dataTag: "data53" ) << QString("AbC" ) << QString("Bc" ) << 0 << false << 1; |
278 | |
279 | QTest::newRow(dataTag: "data54" ) << QString("AbC" ) << QString("bC" ) << 0 << false << 1; |
280 | QTest::newRow(dataTag: "data55" ) << QString("AbC" ) << QString("BC" ) << 0 << false << 1; |
281 | QTest::newRow(dataTag: "data56" ) << QString("AbC" ) << QString("BC" ) << 1 << false << 1; |
282 | QTest::newRow(dataTag: "data57" ) << QString("AbC" ) << QString("BC" ) << 2 << false << -1; |
283 | #if 0 |
284 | QTest::newRow("null-in-null" ) << QString() << QString() << 0 << false << 0; |
285 | QTest::newRow("empty-in-null" ) << QString() << QString("" ) << 0 << false << 0; |
286 | QTest::newRow("null-in-empty" ) << QString("" ) << QString() << 0 << false << 0; |
287 | QTest::newRow("empty-in-empty" ) << QString("" ) << QString("" ) << 0 << false << 0; |
288 | #endif |
289 | |
290 | |
291 | QString s1 = "abc" ; |
292 | s1 += QChar(0xb5); |
293 | QString s2; |
294 | s2 += QChar(0x3bc); |
295 | QTest::newRow(dataTag: "data58" ) << QString(s1) << QString(s2) << 0 << false << 3; |
296 | s2.prepend(c: QLatin1Char('C')); |
297 | QTest::newRow(dataTag: "data59" ) << QString(s1) << QString(s2) << 0 << false << 2; |
298 | |
299 | QString veryBigHaystack(500, 'a'); |
300 | veryBigHaystack += 'B'; |
301 | QTest::newRow(dataTag: "BoyerMooreStressTest" ) << veryBigHaystack << veryBigHaystack << 0 << true << 0; |
302 | QTest::newRow(dataTag: "BoyerMooreStressTest2" ) << veryBigHaystack + 'c' << veryBigHaystack << 0 << true << 0; |
303 | QTest::newRow(dataTag: "BoyerMooreStressTest3" ) << 'c' + veryBigHaystack << veryBigHaystack << 0 << true << 1; |
304 | QTest::newRow(dataTag: "BoyerMooreStressTest4" ) << veryBigHaystack << veryBigHaystack + 'c' << 0 << true << -1; |
305 | QTest::newRow(dataTag: "BoyerMooreStressTest5" ) << veryBigHaystack << 'c' + veryBigHaystack << 0 << true << -1; |
306 | QTest::newRow(dataTag: "BoyerMooreStressTest6" ) << 'd' + veryBigHaystack << 'c' + veryBigHaystack << 0 << true << -1; |
307 | QTest::newRow(dataTag: "BoyerMooreStressTest7" ) << veryBigHaystack + 'c' << 'c' + veryBigHaystack << 0 << true << -1; |
308 | |
309 | QTest::newRow(dataTag: "BoyerMooreInsensitiveStressTest" ) << veryBigHaystack << veryBigHaystack << 0 << false << 0; |
310 | |
311 | } |
312 | |
313 | void tst_QStringRef::indexOf() |
314 | { |
315 | QFETCH(QString, haystack); |
316 | QFETCH(QString, needle); |
317 | QFETCH(int, startpos); |
318 | QFETCH(bool, bcs); |
319 | QFETCH(int, resultpos); |
320 | |
321 | const QString haystackPadded = QLatin1Char(' ') + haystack + QLatin1Char(' '); |
322 | const QString needlePadded = QLatin1Char(' ') + needle + QLatin1Char(' '); |
323 | const QStringRef haystackRef(&haystackPadded, 1, haystack.size()); |
324 | const QStringRef needleRef(&needlePadded, 1, needle.size()); |
325 | |
326 | Qt::CaseSensitivity cs = bcs ? Qt::CaseSensitive : Qt::CaseInsensitive; |
327 | |
328 | QCOMPARE(haystack.indexOf(needle, startpos, cs), resultpos); |
329 | QCOMPARE(haystackRef.indexOf(needle, startpos, cs), resultpos); |
330 | QCOMPARE(haystackRef.indexOf(needleRef, startpos, cs), resultpos); |
331 | QCOMPARE(haystack.indexOf(needleRef, startpos, cs), resultpos); |
332 | |
333 | if (cs == Qt::CaseSensitive) { |
334 | QCOMPARE(haystack.indexOf(needle, startpos), resultpos); |
335 | QCOMPARE(haystackRef.indexOf(needle, startpos), resultpos); |
336 | QCOMPARE(haystackRef.indexOf(needleRef, startpos), resultpos); |
337 | QCOMPARE(haystack.indexOf(needleRef, startpos), resultpos); |
338 | if (startpos == 0) { |
339 | QCOMPARE(haystack.indexOf(needle), resultpos); |
340 | QCOMPARE(haystackRef.indexOf(needle), resultpos); |
341 | QCOMPARE(haystackRef.indexOf(needleRef), resultpos); |
342 | QCOMPARE(haystack.indexOf(needleRef), resultpos); |
343 | } |
344 | } |
345 | if (needle.size() == 1) { |
346 | QCOMPARE(needle.at(0), needleRef.at(0)); |
347 | QCOMPARE(haystack.indexOf(needleRef.at(0), startpos, cs), resultpos); |
348 | QCOMPARE(haystackRef.indexOf(needle.at(0), startpos, cs), resultpos); |
349 | QCOMPARE(haystackRef.indexOf(needleRef.at(0), startpos, cs), resultpos); |
350 | QCOMPARE(haystack.indexOf(needleRef.at(0), startpos ,cs), resultpos); |
351 | } |
352 | } |
353 | |
354 | void tst_QStringRef::indexOf2_data() |
355 | { |
356 | QTest::addColumn<QString>(name: "haystack" ); |
357 | QTest::addColumn<QString>(name: "needle" ); |
358 | QTest::addColumn<int>(name: "resultpos" ); |
359 | |
360 | QTest::newRow(dataTag: "data0" ) << QString() << QString() << 0; |
361 | QTest::newRow(dataTag: "data1" ) << QString() << QString("" ) << 0; |
362 | QTest::newRow(dataTag: "data2" ) << QString("" ) << QString() << 0; |
363 | QTest::newRow(dataTag: "data3" ) << QString("" ) << QString("" ) << 0; |
364 | QTest::newRow(dataTag: "data4" ) << QString() << QString("a" ) << -1; |
365 | QTest::newRow(dataTag: "data5" ) << QString() << QString("abcdefg" ) << -1; |
366 | QTest::newRow(dataTag: "data6" ) << QString("" ) << QString("a" ) << -1; |
367 | QTest::newRow(dataTag: "data7" ) << QString("" ) << QString("abcdefg" ) << -1; |
368 | |
369 | QTest::newRow(dataTag: "data8" ) << QString("a" ) << QString() << 0; |
370 | QTest::newRow(dataTag: "data9" ) << QString("a" ) << QString("" ) << 0; |
371 | QTest::newRow(dataTag: "data10" ) << QString("a" ) << QString("a" ) << 0; |
372 | QTest::newRow(dataTag: "data11" ) << QString("a" ) << QString("b" ) << -1; |
373 | QTest::newRow(dataTag: "data12" ) << QString("a" ) << QString("abcdefg" ) << -1; |
374 | QTest::newRow(dataTag: "data13" ) << QString("ab" ) << QString() << 0; |
375 | QTest::newRow(dataTag: "data14" ) << QString("ab" ) << QString("" ) << 0; |
376 | QTest::newRow(dataTag: "data15" ) << QString("ab" ) << QString("a" ) << 0; |
377 | QTest::newRow(dataTag: "data16" ) << QString("ab" ) << QString("b" ) << 1; |
378 | QTest::newRow(dataTag: "data17" ) << QString("ab" ) << QString("ab" ) << 0; |
379 | QTest::newRow(dataTag: "data18" ) << QString("ab" ) << QString("bc" ) << -1; |
380 | QTest::newRow(dataTag: "data19" ) << QString("ab" ) << QString("abcdefg" ) << -1; |
381 | |
382 | QTest::newRow(dataTag: "data30" ) << QString("abc" ) << QString("a" ) << 0; |
383 | QTest::newRow(dataTag: "data31" ) << QString("abc" ) << QString("b" ) << 1; |
384 | QTest::newRow(dataTag: "data32" ) << QString("abc" ) << QString("c" ) << 2; |
385 | QTest::newRow(dataTag: "data33" ) << QString("abc" ) << QString("d" ) << -1; |
386 | QTest::newRow(dataTag: "data34" ) << QString("abc" ) << QString("ab" ) << 0; |
387 | QTest::newRow(dataTag: "data35" ) << QString("abc" ) << QString("bc" ) << 1; |
388 | QTest::newRow(dataTag: "data36" ) << QString("abc" ) << QString("cd" ) << -1; |
389 | QTest::newRow(dataTag: "data37" ) << QString("abc" ) << QString("ac" ) << -1; |
390 | |
391 | // sizeof(whale) > 32 |
392 | QString whale = "a5zby6cx7dw8evf9ug0th1si2rj3qkp4lomn" ; |
393 | QString minnow = "zby" ; |
394 | QTest::newRow(dataTag: "data40" ) << whale << minnow << 2; |
395 | QTest::newRow(dataTag: "data41" ) << (whale + whale) << minnow << 2; |
396 | QTest::newRow(dataTag: "data42" ) << (minnow + whale) << minnow << 0; |
397 | QTest::newRow(dataTag: "data43" ) << whale << whale << 0; |
398 | QTest::newRow(dataTag: "data44" ) << (whale + whale) << whale << 0; |
399 | QTest::newRow(dataTag: "data45" ) << whale << (whale + whale) << -1; |
400 | QTest::newRow(dataTag: "data46" ) << (whale + whale) << (whale + whale) << 0; |
401 | QTest::newRow(dataTag: "data47" ) << (whale + whale) << (whale + minnow) << -1; |
402 | QTest::newRow(dataTag: "data48" ) << (minnow + whale) << whale << (int)minnow.length(); |
403 | } |
404 | |
405 | void tst_QStringRef::indexOf2() |
406 | { |
407 | QFETCH(QString, haystack); |
408 | QFETCH(QString, needle); |
409 | QFETCH(int, resultpos); |
410 | |
411 | const QString haystackPadded = QLatin1Char(' ') + haystack + QLatin1Char(' '); |
412 | const QString needlePadded = QLatin1Char(' ') + needle + QLatin1Char(' '); |
413 | const QStringRef haystackRef(&haystackPadded, 1, haystack.size()); |
414 | const QStringRef needleRef(&needlePadded, 1, needle.size()); |
415 | |
416 | |
417 | int got; |
418 | |
419 | QCOMPARE(haystack.indexOf(needleRef, 0, Qt::CaseSensitive), resultpos); |
420 | QCOMPARE(haystackRef.indexOf(needle, 0, Qt::CaseSensitive), resultpos); |
421 | QCOMPARE(haystackRef.indexOf(needleRef, 0, Qt::CaseSensitive), resultpos); |
422 | QCOMPARE(haystack.indexOf(needleRef, 0, Qt::CaseInsensitive), resultpos); |
423 | QCOMPARE(haystackRef.indexOf(needle, 0, Qt::CaseInsensitive), resultpos); |
424 | QCOMPARE(haystackRef.indexOf(needleRef, 0, Qt::CaseInsensitive), resultpos); |
425 | if (needle.length() > 0) { |
426 | got = haystackRef.lastIndexOf(str: needle, from: -1, cs: Qt::CaseSensitive); |
427 | QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); |
428 | got = haystackRef.lastIndexOf(str: needle, from: -1, cs: Qt::CaseInsensitive); |
429 | QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); |
430 | |
431 | got = haystack.lastIndexOf(s: needleRef, from: -1, cs: Qt::CaseSensitive); |
432 | QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); |
433 | got = haystack.lastIndexOf(s: needleRef, from: -1, cs: Qt::CaseInsensitive); |
434 | QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); |
435 | |
436 | got = haystackRef.lastIndexOf(str: needleRef, from: -1, cs: Qt::CaseSensitive); |
437 | QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); |
438 | got = haystackRef.lastIndexOf(str: needleRef, from: -1, cs: Qt::CaseInsensitive); |
439 | QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); |
440 | } |
441 | } |
442 | |
443 | void tst_QStringRef::iteration() |
444 | { |
445 | QString hello = "Hello" ; |
446 | QString olleh = "olleH" ; |
447 | |
448 | QStringRef ref(&hello); |
449 | QStringRef rref(&olleh); |
450 | |
451 | const QStringRef &cref = ref; |
452 | const QStringRef &crref = rref; |
453 | |
454 | QVERIFY(std::equal( ref.begin(), ref.end(), hello.begin())); |
455 | QVERIFY(std::equal( rref.begin(), rref.end(), olleh.begin())); |
456 | QVERIFY(std::equal( cref.begin(), cref.end(), hello.begin())); |
457 | QVERIFY(std::equal(crref.begin(), crref.end(), olleh.begin())); |
458 | |
459 | QVERIFY(std::equal( ref.cbegin(), ref.cend(), hello.begin())); |
460 | QVERIFY(std::equal( rref.cbegin(), rref.cend(), olleh.begin())); |
461 | QVERIFY(std::equal( cref.cbegin(), cref.cend(), hello.begin())); |
462 | QVERIFY(std::equal(crref.cbegin(), crref.cend(), olleh.begin())); |
463 | |
464 | QVERIFY(std::equal( ref.rbegin(), ref.rend(), hello.rbegin())); |
465 | QVERIFY(std::equal( rref.rbegin(), rref.rend(), olleh.rbegin())); |
466 | QVERIFY(std::equal( cref.rbegin(), cref.rend(), hello.rbegin())); |
467 | QVERIFY(std::equal(crref.rbegin(), crref.rend(), olleh.rbegin())); |
468 | |
469 | QVERIFY(std::equal( ref.crbegin(), ref.crend(), hello.rbegin())); |
470 | QVERIFY(std::equal( rref.crbegin(), rref.crend(), olleh.rbegin())); |
471 | QVERIFY(std::equal( cref.crbegin(), cref.crend(), hello.rbegin())); |
472 | QVERIFY(std::equal(crref.crbegin(), crref.crend(), olleh.rbegin())); |
473 | } |
474 | |
475 | void tst_QStringRef::lastIndexOf_data() |
476 | { |
477 | QTest::addColumn<QString>(name: "haystack" ); |
478 | QTest::addColumn<QString>(name: "needle" ); |
479 | QTest::addColumn<int>(name: "from" ); |
480 | QTest::addColumn<int>(name: "expected" ); |
481 | QTest::addColumn<bool>(name: "caseSensitive" ); |
482 | |
483 | QString a = "ABCDEFGHIEfGEFG" ; |
484 | |
485 | QTest::newRow(dataTag: "-1" ) << a << "G" << a.size() - 1 << 14 << true; |
486 | QTest::newRow(dataTag: "1" ) << a << "G" << - 1 << 14 << true; |
487 | QTest::newRow(dataTag: "2" ) << a << "G" << -3 << 11 << true; |
488 | QTest::newRow(dataTag: "3" ) << a << "G" << -5 << 6 << true; |
489 | QTest::newRow(dataTag: "4" ) << a << "G" << 14 << 14 << true; |
490 | QTest::newRow(dataTag: "5" ) << a << "G" << 13 << 11 << true; |
491 | QTest::newRow(dataTag: "6" ) << a << "B" << a.size() - 1 << 1 << true; |
492 | QTest::newRow(dataTag: "7" ) << a << "B" << - 1 << 1 << true; |
493 | QTest::newRow(dataTag: "8" ) << a << "B" << 1 << 1 << true; |
494 | QTest::newRow(dataTag: "9" ) << a << "B" << 0 << -1 << true; |
495 | |
496 | QTest::newRow(dataTag: "10" ) << a << "G" << -1 << a.size()-1 << true; |
497 | QTest::newRow(dataTag: "11" ) << a << "G" << a.size()-1 << a.size()-1 << true; |
498 | QTest::newRow(dataTag: "12" ) << a << "G" << a.size() << -1 << true; |
499 | QTest::newRow(dataTag: "13" ) << a << "A" << 0 << 0 << true; |
500 | QTest::newRow(dataTag: "14" ) << a << "A" << -1*a.size() << 0 << true; |
501 | |
502 | QTest::newRow(dataTag: "15" ) << a << "efg" << 0 << -1 << false; |
503 | QTest::newRow(dataTag: "16" ) << a << "efg" << a.size() << -1 << false; |
504 | QTest::newRow(dataTag: "17" ) << a << "efg" << -1 * a.size() << -1 << false; |
505 | QTest::newRow(dataTag: "19" ) << a << "efg" << a.size() - 1 << 12 << false; |
506 | QTest::newRow(dataTag: "20" ) << a << "efg" << 12 << 12 << false; |
507 | QTest::newRow(dataTag: "21" ) << a << "efg" << -12 << -1 << false; |
508 | QTest::newRow(dataTag: "22" ) << a << "efg" << 11 << 9 << false; |
509 | |
510 | QTest::newRow(dataTag: "24" ) << "" << "asdf" << -1 << -1 << false; |
511 | QTest::newRow(dataTag: "25" ) << "asd" << "asdf" << -1 << -1 << false; |
512 | QTest::newRow(dataTag: "26" ) << "" << QString() << -1 << -1 << false; |
513 | |
514 | QTest::newRow(dataTag: "27" ) << a << "" << a.size() << a.size() << false; |
515 | QTest::newRow(dataTag: "28" ) << a << "" << a.size() + 10 << -1 << false; |
516 | } |
517 | |
518 | void tst_QStringRef::lastIndexOf() |
519 | { |
520 | QFETCH(QString, haystack); |
521 | QFETCH(QString, needle); |
522 | QFETCH(int, from); |
523 | QFETCH(int, expected); |
524 | QFETCH(bool, caseSensitive); |
525 | |
526 | const QString haystackPadded = QLatin1Char(' ') + haystack + QLatin1Char(' '); |
527 | const QString needlePadded = QLatin1Char(' ') + needle + QLatin1Char(' '); |
528 | const QStringRef haystackRef(&haystackPadded, 1, haystack.size()); |
529 | const QStringRef needleRef(&needlePadded, 1, needle.size()); |
530 | |
531 | Qt::CaseSensitivity cs = (caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); |
532 | |
533 | QCOMPARE(haystack.lastIndexOf(needleRef, from, cs), expected); |
534 | QCOMPARE(haystackRef.lastIndexOf(needle, from, cs), expected); |
535 | QCOMPARE(haystackRef.lastIndexOf(needleRef, from, cs), expected); |
536 | |
537 | |
538 | if (cs == Qt::CaseSensitive) { |
539 | QCOMPARE(haystack.lastIndexOf(needleRef, from), expected); |
540 | QCOMPARE(haystackRef.lastIndexOf(needle, from), expected); |
541 | QCOMPARE(haystackRef.lastIndexOf(needleRef, from), expected); |
542 | |
543 | if (from == -1) { |
544 | QCOMPARE(haystack.lastIndexOf(needleRef), expected); |
545 | QCOMPARE(haystackRef.lastIndexOf(needle), expected); |
546 | QCOMPARE(haystackRef.lastIndexOf(needleRef), expected); |
547 | |
548 | } |
549 | } |
550 | if (needle.size() == 1) { |
551 | QCOMPARE(haystack.lastIndexOf(needleRef.at(0), from), expected); |
552 | QCOMPARE(haystackRef.lastIndexOf(needle.at(0), from), expected); |
553 | QCOMPARE(haystackRef.lastIndexOf(needleRef.at(0), from), expected); |
554 | } |
555 | } |
556 | |
557 | void tst_QStringRef::count() |
558 | { |
559 | const QString a = QString::fromLatin1(str: "ABCDEFGHIEfGEFG" ); // 15 chars |
560 | CREATE_REF(a); |
561 | QCOMPARE(ref.count('A'),1); |
562 | QCOMPARE(ref.count('Z'),0); |
563 | QCOMPARE(ref.count('E'),3); |
564 | QCOMPARE(ref.count('F'),2); |
565 | QCOMPARE(ref.count('F',Qt::CaseInsensitive),3); |
566 | QCOMPARE(ref.count("FG" ),2); |
567 | QCOMPARE(ref.count("FG" ,Qt::CaseInsensitive),3); |
568 | QCOMPARE(ref.count(QString(), Qt::CaseInsensitive), 16); |
569 | QCOMPARE(ref.count("" , Qt::CaseInsensitive), 16); |
570 | } |
571 | |
572 | void tst_QStringRef::contains() |
573 | { |
574 | const QString a = QString::fromLatin1(str: "ABCDEFGHIEfGEFG" ); // 15 chars |
575 | CREATE_REF(a); |
576 | QVERIFY(ref.contains('A')); |
577 | QVERIFY(!ref.contains('Z')); |
578 | QVERIFY(ref.contains('E')); |
579 | QVERIFY(ref.contains('F')); |
580 | QVERIFY(ref.contains('F',Qt::CaseInsensitive)); |
581 | QVERIFY(ref.contains("FG" )); |
582 | QVERIFY(ref.contains(QString("FG" ).midRef(0))); |
583 | const QString ref2 = QString::fromLatin1(str: " FG " ); |
584 | QVERIFY(ref.contains(ref2.midRef(1, 2),Qt::CaseInsensitive)); |
585 | QVERIFY(ref.contains(QString(), Qt::CaseInsensitive)); |
586 | QVERIFY(ref.contains("" , Qt::CaseInsensitive)); // apparently |
587 | } |
588 | |
589 | void tst_QStringRef::startsWith() |
590 | { |
591 | { |
592 | const QString a = QString::fromLatin1(str: "AB" ); |
593 | CREATE_REF(a); |
594 | QVERIFY(ref.startsWith("A" )); |
595 | QVERIFY(ref.startsWith("AB" )); |
596 | QVERIFY(!ref.startsWith("C" )); |
597 | QVERIFY(!ref.startsWith("ABCDEF" )); |
598 | QVERIFY(ref.startsWith("" )); |
599 | QVERIFY(ref.startsWith(QString())); |
600 | #if QT_DEPRECATED_SINCE(5, 9) |
601 | QVERIFY(ref.startsWith(QString::null)); |
602 | #endif |
603 | QVERIFY(ref.startsWith('A')); |
604 | QVERIFY(ref.startsWith(QLatin1Char('A'))); |
605 | QVERIFY(ref.startsWith(QChar('A'))); |
606 | QVERIFY(!ref.startsWith('C')); |
607 | QVERIFY(!ref.startsWith(QChar())); |
608 | QVERIFY(!ref.startsWith(QLatin1Char(0))); |
609 | |
610 | QVERIFY(ref.startsWith(QLatin1String("A" ))); |
611 | QVERIFY(ref.startsWith(QLatin1String("AB" ))); |
612 | QVERIFY(!ref.startsWith(QLatin1String("C" ))); |
613 | QVERIFY(!ref.startsWith(QLatin1String("ABCDEF" ))); |
614 | QVERIFY(ref.startsWith(QLatin1String("" ))); |
615 | QVERIFY(ref.startsWith(QLatin1String(0))); |
616 | |
617 | QVERIFY(ref.startsWith("A" , Qt::CaseSensitive)); |
618 | QVERIFY(ref.startsWith("A" , Qt::CaseInsensitive)); |
619 | QVERIFY(!ref.startsWith("a" , Qt::CaseSensitive)); |
620 | QVERIFY(ref.startsWith("a" , Qt::CaseInsensitive)); |
621 | QVERIFY(!ref.startsWith("aB" , Qt::CaseSensitive)); |
622 | QVERIFY(ref.startsWith("aB" , Qt::CaseInsensitive)); |
623 | QVERIFY(!ref.startsWith("C" , Qt::CaseSensitive)); |
624 | QVERIFY(!ref.startsWith("C" , Qt::CaseInsensitive)); |
625 | QVERIFY(!ref.startsWith("c" , Qt::CaseSensitive)); |
626 | QVERIFY(!ref.startsWith("c" , Qt::CaseInsensitive)); |
627 | QVERIFY(!ref.startsWith("abcdef" , Qt::CaseInsensitive)); |
628 | QVERIFY(ref.startsWith("" , Qt::CaseInsensitive)); |
629 | QVERIFY(ref.startsWith(QString(), Qt::CaseInsensitive)); |
630 | #if QT_DEPRECATED_SINCE(5, 9) |
631 | QVERIFY(ref.startsWith(QString::null, Qt::CaseInsensitive)); |
632 | #endif |
633 | QVERIFY(ref.startsWith('a', Qt::CaseInsensitive)); |
634 | QVERIFY(ref.startsWith('A', Qt::CaseInsensitive)); |
635 | QVERIFY(ref.startsWith(QLatin1Char('a'), Qt::CaseInsensitive)); |
636 | QVERIFY(ref.startsWith(QChar('a'), Qt::CaseInsensitive)); |
637 | QVERIFY(!ref.startsWith('c', Qt::CaseInsensitive)); |
638 | QVERIFY(!ref.startsWith(QChar(), Qt::CaseInsensitive)); |
639 | QVERIFY(!ref.startsWith(QLatin1Char(0), Qt::CaseInsensitive)); |
640 | |
641 | QVERIFY(ref.startsWith(QLatin1String("A" ), Qt::CaseSensitive)); |
642 | QVERIFY(ref.startsWith(QLatin1String("A" ), Qt::CaseInsensitive)); |
643 | QVERIFY(!ref.startsWith(QLatin1String("a" ), Qt::CaseSensitive)); |
644 | QVERIFY(ref.startsWith(QLatin1String("a" ), Qt::CaseInsensitive)); |
645 | QVERIFY(!ref.startsWith(QLatin1String("aB" ), Qt::CaseSensitive)); |
646 | QVERIFY(ref.startsWith(QLatin1String("aB" ), Qt::CaseInsensitive)); |
647 | QVERIFY(!ref.startsWith(QLatin1String("C" ), Qt::CaseSensitive)); |
648 | QVERIFY(!ref.startsWith(QLatin1String("C" ), Qt::CaseInsensitive)); |
649 | QVERIFY(!ref.startsWith(QLatin1String("c" ), Qt::CaseSensitive)); |
650 | QVERIFY(!ref.startsWith(QLatin1String("c" ), Qt::CaseInsensitive)); |
651 | QVERIFY(!ref.startsWith(QLatin1String("abcdef" ), Qt::CaseInsensitive)); |
652 | QVERIFY(ref.startsWith(QLatin1String("" ), Qt::CaseInsensitive)); |
653 | QVERIFY(ref.startsWith(QLatin1String(0), Qt::CaseInsensitive)); |
654 | QVERIFY(ref.startsWith('A', Qt::CaseSensitive)); |
655 | QVERIFY(ref.startsWith(QLatin1Char('A'), Qt::CaseSensitive)); |
656 | QVERIFY(ref.startsWith(QChar('A'), Qt::CaseSensitive)); |
657 | QVERIFY(!ref.startsWith('a', Qt::CaseSensitive)); |
658 | QVERIFY(!ref.startsWith(QChar(), Qt::CaseSensitive)); |
659 | QVERIFY(!ref.startsWith(QLatin1Char(0), Qt::CaseSensitive)); |
660 | } |
661 | { |
662 | const QString a = QString::fromLatin1(str: "" ); |
663 | CREATE_REF(a); |
664 | QVERIFY(ref.startsWith("" )); |
665 | QVERIFY(ref.startsWith(QString())); |
666 | #if QT_DEPRECATED_SINCE(5, 9) |
667 | QVERIFY(ref.startsWith(QString::null)); |
668 | #endif |
669 | QVERIFY(!ref.startsWith("ABC" )); |
670 | |
671 | QVERIFY(ref.startsWith(QLatin1String("" ))); |
672 | QVERIFY(ref.startsWith(QLatin1String(0))); |
673 | QVERIFY(!ref.startsWith(QLatin1String("ABC" ))); |
674 | |
675 | QVERIFY(!ref.startsWith(QLatin1Char(0))); |
676 | QVERIFY(!ref.startsWith(QLatin1Char('x'))); |
677 | QVERIFY(!ref.startsWith(QChar())); |
678 | } |
679 | { |
680 | const QStringRef ref; |
681 | QVERIFY(!ref.startsWith("" )); |
682 | QVERIFY(ref.startsWith(QString())); |
683 | #if QT_DEPRECATED_SINCE(5, 9) |
684 | QVERIFY(ref.startsWith(QString::null)); |
685 | #endif |
686 | QVERIFY(!ref.startsWith("ABC" )); |
687 | |
688 | QVERIFY(!ref.startsWith(QLatin1String("" ))); |
689 | QVERIFY(ref.startsWith(QLatin1String(0))); |
690 | QVERIFY(!ref.startsWith(QLatin1String("ABC" ))); |
691 | |
692 | QVERIFY(!ref.startsWith(QLatin1Char(0))); |
693 | QVERIFY(!ref.startsWith(QLatin1Char('x'))); |
694 | QVERIFY(!ref.startsWith(QChar())); |
695 | } |
696 | } |
697 | |
698 | void tst_QStringRef::endsWith() |
699 | { |
700 | { |
701 | const QString a = QString::fromLatin1(str: "AB" ); |
702 | CREATE_REF(a); |
703 | QVERIFY(ref.endsWith("B" )); |
704 | QVERIFY(ref.endsWith("AB" )); |
705 | QVERIFY(!ref.endsWith("C" )); |
706 | QVERIFY(!ref.endsWith("ABCDEF" )); |
707 | QVERIFY(ref.endsWith("" )); |
708 | QVERIFY(ref.endsWith(QString())); |
709 | #if QT_DEPRECATED_SINCE(5, 9) |
710 | QVERIFY(ref.endsWith(QString::null)); |
711 | #endif |
712 | QVERIFY(ref.endsWith('B')); |
713 | QVERIFY(ref.endsWith(QLatin1Char('B'))); |
714 | QVERIFY(ref.endsWith(QChar('B'))); |
715 | QVERIFY(!ref.endsWith('C')); |
716 | QVERIFY(!ref.endsWith(QChar())); |
717 | QVERIFY(!ref.endsWith(QLatin1Char(0))); |
718 | |
719 | QVERIFY(ref.endsWith(QLatin1String("B" ))); |
720 | QVERIFY(ref.endsWith(QLatin1String("AB" ))); |
721 | QVERIFY(!ref.endsWith(QLatin1String("C" ))); |
722 | QVERIFY(!ref.endsWith(QLatin1String("ABCDEF" ))); |
723 | QVERIFY(ref.endsWith(QLatin1String("" ))); |
724 | QVERIFY(ref.endsWith(QLatin1String(0))); |
725 | |
726 | QVERIFY(ref.endsWith("B" , Qt::CaseSensitive)); |
727 | QVERIFY(ref.endsWith("B" , Qt::CaseInsensitive)); |
728 | QVERIFY(!ref.endsWith("b" , Qt::CaseSensitive)); |
729 | QVERIFY(ref.endsWith("b" , Qt::CaseInsensitive)); |
730 | QVERIFY(!ref.endsWith("aB" , Qt::CaseSensitive)); |
731 | QVERIFY(ref.endsWith("aB" , Qt::CaseInsensitive)); |
732 | QVERIFY(!ref.endsWith("C" , Qt::CaseSensitive)); |
733 | QVERIFY(!ref.endsWith("C" , Qt::CaseInsensitive)); |
734 | QVERIFY(!ref.endsWith("c" , Qt::CaseSensitive)); |
735 | QVERIFY(!ref.endsWith("c" , Qt::CaseInsensitive)); |
736 | QVERIFY(!ref.endsWith("abcdef" , Qt::CaseInsensitive)); |
737 | QVERIFY(ref.endsWith("" , Qt::CaseInsensitive)); |
738 | QVERIFY(ref.endsWith(QString(), Qt::CaseInsensitive)); |
739 | #if QT_DEPRECATED_SINCE(5, 9) |
740 | QVERIFY(ref.endsWith(QString::null, Qt::CaseInsensitive)); |
741 | #endif |
742 | QVERIFY(ref.endsWith('b', Qt::CaseInsensitive)); |
743 | QVERIFY(ref.endsWith('B', Qt::CaseInsensitive)); |
744 | QVERIFY(ref.endsWith(QLatin1Char('b'), Qt::CaseInsensitive)); |
745 | QVERIFY(ref.endsWith(QChar('b'), Qt::CaseInsensitive)); |
746 | QVERIFY(!ref.endsWith('c', Qt::CaseInsensitive)); |
747 | QVERIFY(!ref.endsWith(QChar(), Qt::CaseInsensitive)); |
748 | QVERIFY(!ref.endsWith(QLatin1Char(0), Qt::CaseInsensitive)); |
749 | |
750 | QVERIFY(ref.endsWith(QLatin1String("B" ), Qt::CaseSensitive)); |
751 | QVERIFY(ref.endsWith(QLatin1String("B" ), Qt::CaseInsensitive)); |
752 | QVERIFY(!ref.endsWith(QLatin1String("b" ), Qt::CaseSensitive)); |
753 | QVERIFY(ref.endsWith(QLatin1String("b" ), Qt::CaseInsensitive)); |
754 | QVERIFY(!ref.endsWith(QLatin1String("aB" ), Qt::CaseSensitive)); |
755 | QVERIFY(ref.endsWith(QLatin1String("aB" ), Qt::CaseInsensitive)); |
756 | QVERIFY(!ref.endsWith(QLatin1String("C" ), Qt::CaseSensitive)); |
757 | QVERIFY(!ref.endsWith(QLatin1String("C" ), Qt::CaseInsensitive)); |
758 | QVERIFY(!ref.endsWith(QLatin1String("c" ), Qt::CaseSensitive)); |
759 | QVERIFY(!ref.endsWith(QLatin1String("c" ), Qt::CaseInsensitive)); |
760 | QVERIFY(!ref.endsWith(QLatin1String("abcdef" ), Qt::CaseInsensitive)); |
761 | QVERIFY(ref.endsWith(QLatin1String("" ), Qt::CaseInsensitive)); |
762 | QVERIFY(ref.endsWith(QLatin1String(0), Qt::CaseInsensitive)); |
763 | QVERIFY(ref.endsWith('B', Qt::CaseSensitive)); |
764 | QVERIFY(ref.endsWith(QLatin1Char('B'), Qt::CaseSensitive)); |
765 | QVERIFY(ref.endsWith(QChar('B'), Qt::CaseSensitive)); |
766 | QVERIFY(!ref.endsWith('b', Qt::CaseSensitive)); |
767 | QVERIFY(!ref.endsWith(QChar(), Qt::CaseSensitive)); |
768 | QVERIFY(!ref.endsWith(QLatin1Char(0), Qt::CaseSensitive)); |
769 | |
770 | } |
771 | { |
772 | const QString a = QString::fromLatin1(str: "" ); |
773 | CREATE_REF(a); |
774 | QVERIFY(ref.endsWith("" )); |
775 | QVERIFY(ref.endsWith(QString())); |
776 | #if QT_DEPRECATED_SINCE(5, 9) |
777 | QVERIFY(ref.endsWith(QString::null)); |
778 | #endif |
779 | QVERIFY(!ref.endsWith("ABC" )); |
780 | QVERIFY(!ref.endsWith(QLatin1Char(0))); |
781 | QVERIFY(!ref.endsWith(QLatin1Char('x'))); |
782 | QVERIFY(!ref.endsWith(QChar())); |
783 | |
784 | QVERIFY(ref.endsWith(QLatin1String("" ))); |
785 | QVERIFY(ref.endsWith(QLatin1String(0))); |
786 | QVERIFY(!ref.endsWith(QLatin1String("ABC" ))); |
787 | } |
788 | |
789 | { |
790 | QStringRef ref; |
791 | QVERIFY(!ref.endsWith("" )); |
792 | QVERIFY(ref.endsWith(QString())); |
793 | #if QT_DEPRECATED_SINCE(5, 9) |
794 | QVERIFY(ref.endsWith(QString::null)); |
795 | #endif |
796 | QVERIFY(!ref.endsWith("ABC" )); |
797 | |
798 | QVERIFY(!ref.endsWith(QLatin1String("" ))); |
799 | QVERIFY(ref.endsWith(QLatin1String(0))); |
800 | QVERIFY(!ref.endsWith(QLatin1String("ABC" ))); |
801 | |
802 | QVERIFY(!ref.endsWith(QLatin1Char(0))); |
803 | QVERIFY(!ref.endsWith(QLatin1Char('x'))); |
804 | QVERIFY(!ref.endsWith(QChar())); |
805 | } |
806 | } |
807 | |
808 | void tst_QStringRef::operator_eqeq_nullstring() |
809 | { |
810 | /* Some of these might not be all that logical but it's the behaviour we've had since 3.0.0 |
811 | so we should probably stick with it. */ |
812 | |
813 | QVERIFY(QStringRef() == "" ); |
814 | QVERIFY("" == QStringRef()); |
815 | |
816 | QVERIFY(QString("" ) == "" ); |
817 | QVERIFY("" == QString("" )); |
818 | |
819 | QVERIFY(QStringRef().size() == 0); |
820 | |
821 | QVERIFY(QString("" ).size() == 0); |
822 | |
823 | QVERIFY(QStringRef() == QString("" )); |
824 | QVERIFY(QString("" ) == QString()); |
825 | } |
826 | |
827 | static inline int sign(int x) |
828 | { |
829 | return x == 0 ? 0 : (x < 0 ? -1 : 1); |
830 | } |
831 | |
832 | void tst_QStringRef::compare_data() |
833 | { |
834 | QTest::addColumn<QString>(name: "s1" ); |
835 | QTest::addColumn<QString>(name: "s2" ); |
836 | QTest::addColumn<int>(name: "csr" ); // case sensitive result |
837 | QTest::addColumn<int>(name: "cir" ); // case insensitive result |
838 | |
839 | |
840 | // null strings |
841 | QTest::newRow(dataTag: "data0" ) << QString("" ) << QString("" ) << 0 << 0; |
842 | QTest::newRow(dataTag: "data1" ) << QString("a" ) << QString("" ) << 1 << 1; |
843 | QTest::newRow(dataTag: "data2" ) << QString("" ) << QString("a" ) << -1 << -1; |
844 | |
845 | // equal length |
846 | QTest::newRow(dataTag: "data3" ) << QString("abc" ) << QString("abc" ) << 0 << 0; |
847 | QTest::newRow(dataTag: "data4" ) << QString("abC" ) << QString("abc" ) << -1 << 0; |
848 | QTest::newRow(dataTag: "data5" ) << QString("abc" ) << QString("abC" ) << 1 << 0; |
849 | QTest::newRow(dataTag: "data10" ) << QString("abcdefgh" ) << QString("abcdefgh" ) << 0 << 0; |
850 | QTest::newRow(dataTag: "data11" ) << QString("abcdefgh" ) << QString("abCdefgh" ) << 1 << 0; |
851 | QTest::newRow(dataTag: "data12" ) << QString("0123456789012345" ) << QString("0123456789012345" ) << 0 << 0; |
852 | QTest::newRow(dataTag: "data13" ) << QString("0123556789012345" ) << QString("0123456789012345" ) << 1 << 1; |
853 | |
854 | // different length |
855 | QTest::newRow(dataTag: "data6" ) << QString("abcdef" ) << QString("abc" ) << 1 << 1; |
856 | QTest::newRow(dataTag: "data7" ) << QString("abCdef" ) << QString("abc" ) << -1 << 1; |
857 | QTest::newRow(dataTag: "data8" ) << QString("abc" ) << QString("abcdef" ) << -1 << -1; |
858 | QTest::newRow(dataTag: "data14" ) << QString("abcdefgh" ) << QString("abcdefghi" ) << -1 << -1; |
859 | QTest::newRow(dataTag: "data15" ) << QString("01234567890123456" ) << QString("0123456789012345" ) << 1 << 1; |
860 | |
861 | QString upper; |
862 | upper += QChar(QChar::highSurrogate(ucs4: 0x10400)); |
863 | upper += QChar(QChar::lowSurrogate(ucs4: 0x10400)); |
864 | QString lower; |
865 | lower += QChar(QChar::highSurrogate(ucs4: 0x10428)); |
866 | lower += QChar(QChar::lowSurrogate(ucs4: 0x10428)); |
867 | QTest::newRow(dataTag: "data9" ) << upper << lower << -1 << 0; |
868 | } |
869 | |
870 | static bool isLatin(const QString &s) |
871 | { |
872 | for (int i = 0; i < s.length(); ++i) |
873 | if (s.at(i).unicode() > 0xff) |
874 | return false; |
875 | return true; |
876 | } |
877 | |
878 | void tst_QStringRef::compare() |
879 | { |
880 | QFETCH(QString, s1); |
881 | QFETCH(QString, s2); |
882 | QFETCH(int, csr); |
883 | QFETCH(int, cir); |
884 | |
885 | QStringRef r1(&s1, 0, s1.length()); |
886 | QStringRef r2(&s2, 0, s2.length()); |
887 | |
888 | QCOMPARE(sign(QString::compare(s1, s2)), csr); |
889 | QCOMPARE(sign(QStringRef::compare(r1, r2)), csr); |
890 | QCOMPARE(sign(s1.compare(s2)), csr); |
891 | QCOMPARE(sign(s1.compare(r2)), csr); |
892 | QCOMPARE(sign(r1.compare(r2)), csr); |
893 | |
894 | QCOMPARE(sign(s1.compare(s2, Qt::CaseSensitive)), csr); |
895 | QCOMPARE(sign(s1.compare(s2, Qt::CaseInsensitive)), cir); |
896 | QCOMPARE(sign(s1.compare(r2, Qt::CaseSensitive)), csr); |
897 | QCOMPARE(sign(s1.compare(r2, Qt::CaseInsensitive)), cir); |
898 | QCOMPARE(sign(r1.compare(r2, Qt::CaseSensitive)), csr); |
899 | QCOMPARE(sign(r1.compare(r2, Qt::CaseInsensitive)), cir); |
900 | |
901 | QCOMPARE(sign(QString::compare(s1, s2, Qt::CaseSensitive)), csr); |
902 | QCOMPARE(sign(QString::compare(s1, s2, Qt::CaseInsensitive)), cir); |
903 | QCOMPARE(sign(QString::compare(s1, r2, Qt::CaseSensitive)), csr); |
904 | QCOMPARE(sign(QString::compare(s1, r2, Qt::CaseInsensitive)), cir); |
905 | QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseSensitive)), csr); |
906 | QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseInsensitive)), cir); |
907 | |
908 | if (!cir) { |
909 | QCOMPARE(s1.toCaseFolded(), s2.toCaseFolded()); |
910 | } |
911 | |
912 | if (isLatin(s: s2)) { |
913 | QCOMPARE(sign(QString::compare(s1, QLatin1String(s2.toLatin1()))), csr); |
914 | QCOMPARE(sign(QString::compare(s1, QLatin1String(s2.toLatin1()), Qt::CaseInsensitive)), cir); |
915 | QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(s2.toLatin1()))), csr); |
916 | QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(s2.toLatin1()), Qt::CaseInsensitive)), cir); |
917 | } |
918 | |
919 | if (isLatin(s: s1)) { |
920 | QCOMPARE(sign(QString::compare(QLatin1String(s1.toLatin1()), s2)), csr); |
921 | QCOMPARE(sign(QString::compare(QLatin1String(s1.toLatin1()), s2, Qt::CaseInsensitive)), cir); |
922 | } |
923 | } |
924 | |
925 | void tst_QStringRef::compare2_data() |
926 | { |
927 | compare_data(); |
928 | } |
929 | |
930 | void tst_QStringRef::compare2() |
931 | { |
932 | QFETCH(QString, s1); |
933 | QFETCH(QString, s2); |
934 | QFETCH(int, csr); |
935 | QFETCH(int, cir); |
936 | |
937 | // prepend and append data |
938 | // we only use Latin1 here so isLatin1 still results true |
939 | s1.prepend(s: "xyz" ).append(s: "zyx" ); |
940 | s2.prepend(s: "foobar" ).append(s: "raboof" ); |
941 | |
942 | QStringRef r1(&s1, 3, s1.length() - 6); |
943 | QStringRef r2(&s2, 6, s2.length() - 12); |
944 | |
945 | QCOMPARE(sign(QStringRef::compare(r1, r2)), csr); |
946 | QCOMPARE(sign(r1.compare(r2)), csr); |
947 | |
948 | QCOMPARE(sign(r1.compare(r2, Qt::CaseSensitive)), csr); |
949 | QCOMPARE(sign(r1.compare(r2, Qt::CaseInsensitive)), cir); |
950 | |
951 | QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseSensitive)), csr); |
952 | QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseInsensitive)), cir); |
953 | |
954 | if (isLatin(s: s2)) { |
955 | QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(r2.toLatin1()))), csr); |
956 | QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(r2.toLatin1()), Qt::CaseInsensitive)), cir); |
957 | } |
958 | |
959 | if (isLatin(s: s1)) { |
960 | QCOMPARE(sign(QStringRef::compare(r2, QLatin1String(r1.toLatin1()))), -csr); |
961 | QCOMPARE(sign(QStringRef::compare(r2, QLatin1String(r1.toLatin1()), Qt::CaseInsensitive)), -cir); |
962 | } |
963 | } |
964 | |
965 | void tst_QStringRef::toNum() |
966 | { |
967 | #define TEST_TO_INT(num, func, type) \ |
968 | a = #num; \ |
969 | b = a.leftRef(-1); \ |
970 | QCOMPARE(b.func(&ok), type(Q_INT64_C(num))); \ |
971 | QVERIFY2(ok, "Failed: num=" #num); |
972 | |
973 | QString a; |
974 | QStringRef b; |
975 | bool ok = false; |
976 | |
977 | TEST_TO_INT(0, toInt, int) |
978 | TEST_TO_INT(-1, toInt, int) |
979 | TEST_TO_INT(1, toInt, int) |
980 | TEST_TO_INT(2147483647, toInt, int) |
981 | TEST_TO_INT(-2147483648, toInt, int) |
982 | |
983 | TEST_TO_INT(0, toShort, short) |
984 | TEST_TO_INT(-1, toShort, short) |
985 | TEST_TO_INT(1, toShort, short) |
986 | TEST_TO_INT(32767, toShort, short) |
987 | TEST_TO_INT(-32768, toShort, short) |
988 | |
989 | TEST_TO_INT(0, toLong, long) |
990 | TEST_TO_INT(-1, toLong, long) |
991 | TEST_TO_INT(1, toLong, long) |
992 | TEST_TO_INT(2147483647, toLong, long) |
993 | TEST_TO_INT(-2147483648, toLong, long) |
994 | TEST_TO_INT(0, toLongLong, (long long)) |
995 | TEST_TO_INT(-1, toLongLong, (long long)) |
996 | TEST_TO_INT(1, toLongLong, (long long)) |
997 | TEST_TO_INT(9223372036854775807, toLongLong, (long long)) |
998 | TEST_TO_INT(-9223372036854775807, toLongLong, (long long)) |
999 | |
1000 | #undef TEST_TO_INT |
1001 | |
1002 | #define TEST_TO_UINT(num, func, type) \ |
1003 | a = #num; \ |
1004 | b = a.leftRef(-1); \ |
1005 | QCOMPARE(b.func(&ok), type(Q_UINT64_C(num))); \ |
1006 | QVERIFY2(ok, "Failed: num=" #num); |
1007 | |
1008 | TEST_TO_UINT(0, toUInt, (unsigned int)) |
1009 | TEST_TO_UINT(1, toUInt, (unsigned int)) |
1010 | TEST_TO_UINT(4294967295, toUInt, (unsigned int)) |
1011 | |
1012 | TEST_TO_UINT(0, toUShort, (unsigned short)) |
1013 | TEST_TO_UINT(1, toUShort, (unsigned short)) |
1014 | TEST_TO_UINT(65535, toUShort, (unsigned short)) |
1015 | |
1016 | TEST_TO_UINT(0, toULong, (unsigned long)) |
1017 | TEST_TO_UINT(1, toULong, (unsigned long)) |
1018 | TEST_TO_UINT(4294967295, toULong, (unsigned long)) |
1019 | |
1020 | TEST_TO_UINT(0, toULongLong, (unsigned long long)) |
1021 | TEST_TO_UINT(1, toULongLong, (unsigned long long)) |
1022 | TEST_TO_UINT(18446744073709551615, toULongLong, (unsigned long long)) |
1023 | |
1024 | #undef TEST_TO_UINT |
1025 | |
1026 | #define TEST_BASE(str, base, num) \ |
1027 | a = str; \ |
1028 | b = a.leftRef(-1); \ |
1029 | QCOMPARE(b.toInt(&ok,base), int(num)); \ |
1030 | QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toInt"); \ |
1031 | QCOMPARE(b.toUInt(&ok, base), (unsigned int)(num)); \ |
1032 | QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toUInt"); \ |
1033 | QCOMPARE(b.toShort(&ok, base), short(num)); \ |
1034 | QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toShort"); \ |
1035 | QCOMPARE(b.toUShort(&ok, base), (unsigned short)(num)); \ |
1036 | QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toUShort"); \ |
1037 | QCOMPARE(b.toLong(&ok, base), long(num)); \ |
1038 | QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toLong"); \ |
1039 | QCOMPARE(b.toULong(&ok, base), (unsigned long)(num)); \ |
1040 | QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toULong"); \ |
1041 | QCOMPARE(b.toLongLong(&ok, base), (long long)(num)); \ |
1042 | QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toLongLong"); \ |
1043 | QCOMPARE(b.toULongLong(&ok, base), (unsigned long long)(num)); \ |
1044 | QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toULongLong"); |
1045 | |
1046 | TEST_BASE("FF" , 16, 255) |
1047 | TEST_BASE("0xFF" , 16, 255) |
1048 | TEST_BASE("77" , 8, 63) |
1049 | TEST_BASE("077" , 8, 63) |
1050 | |
1051 | TEST_BASE("0xFF" , 0, 255) |
1052 | TEST_BASE("077" , 0, 63) |
1053 | TEST_BASE("255" , 0, 255) |
1054 | |
1055 | TEST_BASE(" FF" , 16, 255) |
1056 | TEST_BASE(" 0xFF" , 16, 255) |
1057 | TEST_BASE(" 77" , 8, 63) |
1058 | TEST_BASE(" 077" , 8, 63) |
1059 | |
1060 | TEST_BASE(" 0xFF" , 0, 255) |
1061 | TEST_BASE(" 077" , 0, 63) |
1062 | TEST_BASE(" 255" , 0, 255) |
1063 | |
1064 | TEST_BASE("\tFF\t" , 16, 255) |
1065 | TEST_BASE("\t0xFF " , 16, 255) |
1066 | TEST_BASE(" 77 " , 8, 63) |
1067 | TEST_BASE("77 " , 8, 63) |
1068 | |
1069 | #undef TEST_BASE |
1070 | |
1071 | #define TEST_NEG_BASE(str, base, num) \ |
1072 | a = str; \ |
1073 | b = a.leftRef(-1); \ |
1074 | QCOMPARE(b.toInt(&ok, base), int(num)); \ |
1075 | QVERIFY2(ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toInt"); \ |
1076 | QCOMPARE(b.toShort(&ok,base), short(num)); \ |
1077 | QVERIFY2(ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toShort"); \ |
1078 | QCOMPARE(b.toLong(&ok, base), long(num)); \ |
1079 | QVERIFY2(ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toLong"); \ |
1080 | QCOMPARE(b.toLongLong(&ok, base), (long long)(num)); \ |
1081 | QVERIFY2(ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toLongLong"); |
1082 | |
1083 | TEST_NEG_BASE("-FE" , 16, -254) |
1084 | TEST_NEG_BASE("-0xFE" , 16, -254) |
1085 | TEST_NEG_BASE("-77" , 8, -63) |
1086 | TEST_NEG_BASE("-077" , 8, -63) |
1087 | |
1088 | TEST_NEG_BASE("-0xFE" , 0, -254) |
1089 | TEST_NEG_BASE("-077" , 0, -63) |
1090 | TEST_NEG_BASE("-254" , 0, -254) |
1091 | |
1092 | #undef TEST_NEG_BASE |
1093 | |
1094 | #define TEST_DOUBLE(num, str) \ |
1095 | a = str; \ |
1096 | b = a.leftRef(-1); \ |
1097 | QCOMPARE(b.toDouble(&ok), num); \ |
1098 | QVERIFY(ok); |
1099 | |
1100 | TEST_DOUBLE(1.2345, "1.2345" ) |
1101 | TEST_DOUBLE(12.345, "1.2345e+01" ) |
1102 | TEST_DOUBLE(12.345, "1.2345E+01" ) |
1103 | TEST_DOUBLE(12345.6, "12345.6" ) |
1104 | |
1105 | #undef TEST_DOUBLE |
1106 | |
1107 | #define TEST_BAD(str, func) \ |
1108 | a = str; \ |
1109 | b = a.leftRef(-1); \ |
1110 | b.func(&ok); \ |
1111 | QVERIFY2(!ok, "Failed: str=" #str " func=" #func); |
1112 | |
1113 | TEST_BAD("32768" , toShort) |
1114 | TEST_BAD("-32769" , toShort) |
1115 | TEST_BAD("65536" , toUShort) |
1116 | TEST_BAD("2147483648" , toInt) |
1117 | TEST_BAD("-2147483649" , toInt) |
1118 | TEST_BAD("4294967296" , toUInt) |
1119 | if (sizeof(long) == 4) { |
1120 | TEST_BAD("2147483648" , toLong) |
1121 | TEST_BAD("-2147483649" , toLong) |
1122 | TEST_BAD("4294967296" , toULong) |
1123 | } |
1124 | TEST_BAD("9223372036854775808" , toLongLong) |
1125 | TEST_BAD("-9223372036854775809" , toLongLong) |
1126 | TEST_BAD("18446744073709551616" , toULongLong) |
1127 | TEST_BAD("-1" , toUShort) |
1128 | TEST_BAD("-1" , toUInt) |
1129 | TEST_BAD("-1" , toULong) |
1130 | TEST_BAD("-1" , toULongLong) |
1131 | |
1132 | #undef TEST_BAD |
1133 | |
1134 | #define TEST_BAD_ALL(str) \ |
1135 | a = str; \ |
1136 | b = a.leftRef(-1); \ |
1137 | b.toShort(&ok); \ |
1138 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1139 | b.toUShort(&ok); \ |
1140 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1141 | b.toInt(&ok); \ |
1142 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1143 | b.toUInt(&ok); \ |
1144 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1145 | b.toLong(&ok); \ |
1146 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1147 | b.toULong(&ok); \ |
1148 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1149 | b.toLongLong(&ok); \ |
1150 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1151 | b.toULongLong(&ok); \ |
1152 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1153 | b.toFloat(&ok); \ |
1154 | QVERIFY2(!ok, "Failed: str=" #str); \ |
1155 | b.toDouble(&ok); \ |
1156 | QVERIFY2(!ok, "Failed: str=" #str); |
1157 | |
1158 | TEST_BAD_ALL((const char*)0); |
1159 | TEST_BAD_ALL("" ); |
1160 | TEST_BAD_ALL(" " ); |
1161 | TEST_BAD_ALL("." ); |
1162 | TEST_BAD_ALL("-" ); |
1163 | TEST_BAD_ALL("hello" ); |
1164 | TEST_BAD_ALL("1.2.3" ); |
1165 | TEST_BAD_ALL("0x0x0x" ); |
1166 | TEST_BAD_ALL("123-^~<" ); |
1167 | TEST_BAD_ALL("123ThisIsNotANumber" ); |
1168 | |
1169 | #undef TEST_BAD_ALL |
1170 | |
1171 | a = "FF" ; |
1172 | b = a.leftRef(n: -1); |
1173 | b.toULongLong(ok: &ok, base: 10); |
1174 | QVERIFY(!ok); |
1175 | |
1176 | a = "FF" ; |
1177 | b = a.leftRef(n: -1); |
1178 | b.toULongLong(ok: &ok, base: 0); |
1179 | QVERIFY(!ok); |
1180 | |
1181 | #ifdef QT_NO_FPU |
1182 | double d = 3.40282346638528e+38; // slightly off FLT_MAX when using hardfloats |
1183 | #else |
1184 | double d = 3.4028234663852886e+38; // FLT_MAX |
1185 | #endif |
1186 | QString::number(d, f: 'e', prec: 17).leftRef(n: -1).toFloat(ok: &ok); |
1187 | QVERIFY(ok); |
1188 | QString::number(d + 1e32, f: 'e', prec: 17).leftRef(n: -1).toFloat(ok: &ok); |
1189 | QVERIFY(!ok); |
1190 | QString::number(-d, f: 'e', prec: 17).leftRef(n: -1).toFloat(ok: &ok); |
1191 | QVERIFY(ok); |
1192 | QString::number(-d - 1e32, f: 'e', prec: 17).leftRef(n: -1).toFloat(ok: &ok); |
1193 | QVERIFY(!ok); |
1194 | QString::number(d + 1e32, f: 'e', prec: 17).leftRef(n: -1).toDouble(ok: &ok); |
1195 | QVERIFY(ok); |
1196 | QString::number(-d - 1e32, f: 'e', prec: 17).leftRef(n: -1).toDouble(ok: &ok); |
1197 | QVERIFY(ok); |
1198 | } |
1199 | |
1200 | void tst_QStringRef::toUShort() |
1201 | { |
1202 | QString a; |
1203 | QStringRef b; |
1204 | bool ok; |
1205 | QCOMPARE(b.toUShort(), ushort(0)); |
1206 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1207 | QVERIFY(!ok); |
1208 | |
1209 | a = "" ; |
1210 | b = a.leftRef(n: -1); |
1211 | QCOMPARE(b.toUShort(), ushort(0)); |
1212 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1213 | QVERIFY(!ok); |
1214 | |
1215 | a = "COMPARE" ; |
1216 | b = a.leftRef(n: -1); |
1217 | QCOMPARE(b.toUShort(), ushort(0)); |
1218 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1219 | QVERIFY(!ok); |
1220 | |
1221 | a = "123" ; |
1222 | b = a.leftRef(n: -1); |
1223 | QCOMPARE(b.toUShort(), ushort(123)); |
1224 | QCOMPARE(b.toUShort(&ok), ushort(123)); |
1225 | QVERIFY(ok); |
1226 | |
1227 | a = "123A" ; |
1228 | b = a.leftRef(n: -1); |
1229 | QCOMPARE(b.toUShort(), ushort(0)); |
1230 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1231 | QVERIFY(!ok); |
1232 | |
1233 | a = "1234567" ; |
1234 | b = a.leftRef(n: -1); |
1235 | QCOMPARE(b.toUShort(), ushort(0)); |
1236 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1237 | QVERIFY(!ok); |
1238 | |
1239 | a = "aaa123aaa" ; |
1240 | b = a.leftRef(n: -1); |
1241 | QCOMPARE(b.toUShort(), ushort(0)); |
1242 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1243 | QVERIFY(!ok); |
1244 | |
1245 | a = "aaa123" ; |
1246 | b = a.leftRef(n: -1); |
1247 | QCOMPARE(b.toUShort(), ushort(0)); |
1248 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1249 | QVERIFY(!ok); |
1250 | |
1251 | a = "123aaa" ; |
1252 | b = a.leftRef(n: -1); |
1253 | QCOMPARE(b.toUShort(), ushort(0)); |
1254 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1255 | QVERIFY(!ok); |
1256 | |
1257 | a = "32767" ; |
1258 | b = a.leftRef(n: -1); |
1259 | QCOMPARE(b.toUShort(), ushort(32767)); |
1260 | QCOMPARE(b.toUShort(&ok), ushort(32767)); |
1261 | QVERIFY(ok); |
1262 | |
1263 | a = "-32767" ; |
1264 | b = a.leftRef(n: -1); |
1265 | QCOMPARE(b.toUShort(), ushort(0)); |
1266 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1267 | QVERIFY(!ok); |
1268 | |
1269 | a = "65535" ; |
1270 | b = a.leftRef(n: -1); |
1271 | QCOMPARE(b.toUShort(), ushort(65535)); |
1272 | QCOMPARE(b.toUShort(&ok), ushort(65535)); |
1273 | QVERIFY(ok); |
1274 | |
1275 | if (sizeof(short) == 2) { |
1276 | a = "65536" ; |
1277 | b = a.leftRef(n: -1); |
1278 | QCOMPARE(b.toUShort(), ushort(0)); |
1279 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1280 | QVERIFY(!ok); |
1281 | |
1282 | a = "123456" ; |
1283 | b = a.leftRef(n: -1); |
1284 | QCOMPARE(b.toUShort(), ushort(0)); |
1285 | QCOMPARE(b.toUShort(&ok), ushort(0)); |
1286 | QVERIFY(!ok); |
1287 | } |
1288 | } |
1289 | |
1290 | void tst_QStringRef::toShort() |
1291 | { |
1292 | QString a; |
1293 | QStringRef b; |
1294 | bool ok; |
1295 | QCOMPARE(b.toShort(), short(0)); |
1296 | QCOMPARE(b.toShort(&ok), short(0)); |
1297 | QVERIFY(!ok); |
1298 | |
1299 | a = "" ; |
1300 | b = a.leftRef(n: -1); |
1301 | QCOMPARE(b.toShort(), short(0)); |
1302 | QCOMPARE(b.toShort(&ok), short(0)); |
1303 | QVERIFY(!ok); |
1304 | |
1305 | a = "COMPARE" ; |
1306 | b = a.leftRef(n: -1); |
1307 | QCOMPARE(b.toShort(), short(0)); |
1308 | QCOMPARE(b.toShort(&ok), short(0)); |
1309 | QVERIFY(!ok); |
1310 | |
1311 | a = "123" ; |
1312 | b = a.leftRef(n: -1); |
1313 | QCOMPARE(b.toShort(), short(123)); |
1314 | QCOMPARE(b.toShort(&ok), short(123)); |
1315 | QVERIFY(ok); |
1316 | |
1317 | a = "123A" ; |
1318 | b = a.leftRef(n: -1); |
1319 | QCOMPARE(b.toShort(), short(0)); |
1320 | QCOMPARE(b.toShort(&ok), short(0)); |
1321 | QVERIFY(!ok); |
1322 | |
1323 | a = "1234567" ; |
1324 | b = a.leftRef(n: -1); |
1325 | QCOMPARE(b.toShort(), short(0)); |
1326 | QCOMPARE(b.toShort(&ok), short(0)); |
1327 | QVERIFY(!ok); |
1328 | |
1329 | a = "aaa123aaa" ; |
1330 | b = a.leftRef(n: -1); |
1331 | QCOMPARE(b.toShort(), short(0)); |
1332 | QCOMPARE(b.toShort(&ok), short(0)); |
1333 | QVERIFY(!ok); |
1334 | |
1335 | a = "aaa123" ; |
1336 | b = a.leftRef(n: -1); |
1337 | QCOMPARE(b.toShort(), short(0)); |
1338 | QCOMPARE(b.toShort(&ok), short(0)); |
1339 | QVERIFY(!ok); |
1340 | |
1341 | a = "123aaa" ; |
1342 | b = a.leftRef(n: -1); |
1343 | QCOMPARE(b.toShort(), short(0)); |
1344 | QCOMPARE(b.toShort(&ok), short(0)); |
1345 | QVERIFY(!ok); |
1346 | |
1347 | a = "32767" ; |
1348 | b = a.leftRef(n: -1); |
1349 | QCOMPARE(b.toShort(), short(32767)); |
1350 | QCOMPARE(b.toShort(&ok), short(32767)); |
1351 | QVERIFY(ok); |
1352 | |
1353 | a = "-32767" ; |
1354 | b = a.leftRef(n: -1); |
1355 | QCOMPARE(b.toShort(), short(-32767)); |
1356 | QCOMPARE(b.toShort(&ok), short(-32767)); |
1357 | QVERIFY(ok); |
1358 | |
1359 | a = "-32768" ; |
1360 | b = a.leftRef(n: -1); |
1361 | QCOMPARE(b.toShort(), short(-32768)); |
1362 | QCOMPARE(b.toShort(&ok), short(-32768)); |
1363 | QVERIFY(ok); |
1364 | |
1365 | if (sizeof(short) == 2) { |
1366 | a = "32768" ; |
1367 | b = a.leftRef(n: -1); |
1368 | QCOMPARE(b.toShort(), short(0)); |
1369 | QCOMPARE(b.toShort(&ok), short(0)); |
1370 | QVERIFY(!ok); |
1371 | |
1372 | a = "-32769" ; |
1373 | b = a.leftRef(n: -1); |
1374 | QCOMPARE(b.toShort(), short(0)); |
1375 | QCOMPARE(b.toShort(&ok), short(0)); |
1376 | QVERIFY(!ok); |
1377 | } |
1378 | } |
1379 | |
1380 | void tst_QStringRef::toInt() |
1381 | { |
1382 | QString a; |
1383 | QStringRef b; |
1384 | bool ok; |
1385 | QCOMPARE(b.toInt(), 0); |
1386 | QCOMPARE(b.toInt(&ok), 0); |
1387 | QVERIFY(!ok); |
1388 | |
1389 | a = "" ; |
1390 | b = a.leftRef(n: -1); |
1391 | QCOMPARE(b.toInt(), 0); |
1392 | QCOMPARE(b.toInt(&ok), 0); |
1393 | QVERIFY(!ok); |
1394 | |
1395 | a = "COMPARE" ; |
1396 | b = a.leftRef(n: -1); |
1397 | QCOMPARE(b.toInt(), 0); |
1398 | QCOMPARE(b.toInt(&ok), 0); |
1399 | QVERIFY(!ok); |
1400 | |
1401 | a = "123" ; |
1402 | b = a.leftRef(n: -1); |
1403 | QCOMPARE(b.toInt(), 123); |
1404 | QCOMPARE(b.toInt(&ok), 123); |
1405 | QVERIFY(ok); |
1406 | |
1407 | a = "123A" ; |
1408 | b = a.leftRef(n: -1); |
1409 | QCOMPARE(b.toInt(), 0); |
1410 | QCOMPARE(b.toInt(&ok), 0); |
1411 | QVERIFY(!ok); |
1412 | |
1413 | a = "1234567" ; |
1414 | b = a.leftRef(n: -1); |
1415 | QCOMPARE(b.toInt(), 1234567); |
1416 | QCOMPARE(b.toInt(&ok), 1234567); |
1417 | QVERIFY(ok); |
1418 | |
1419 | a = "12345678901234" ; |
1420 | b = a.leftRef(n: -1); |
1421 | QCOMPARE(b.toInt(), 0); |
1422 | QCOMPARE(b.toInt(&ok), 0); |
1423 | QVERIFY(!ok); |
1424 | |
1425 | a = "3234567890" ; |
1426 | b = a.leftRef(n: -1); |
1427 | QCOMPARE(b.toInt(), 0); |
1428 | QCOMPARE(b.toInt(&ok), 0); |
1429 | QVERIFY(!ok); |
1430 | |
1431 | a = "aaa12345aaa" ; |
1432 | b = a.leftRef(n: -1); |
1433 | QCOMPARE(b.toInt(), 0); |
1434 | QCOMPARE(b.toInt(&ok), 0); |
1435 | QVERIFY(!ok); |
1436 | |
1437 | a = "aaa12345" ; |
1438 | b = a.leftRef(n: -1); |
1439 | QCOMPARE(b.toInt(), 0); |
1440 | QCOMPARE(b.toInt(&ok), 0); |
1441 | QVERIFY(!ok); |
1442 | |
1443 | a = "12345aaa" ; |
1444 | b = a.leftRef(n: -1); |
1445 | QCOMPARE(b.toInt(), 0); |
1446 | QCOMPARE(b.toInt(&ok), 0); |
1447 | QVERIFY(!ok); |
1448 | |
1449 | a = "2147483647" ; // 2**31 - 1 |
1450 | b = a.leftRef(n: -1); |
1451 | QCOMPARE(b.toInt(), 2147483647); |
1452 | QCOMPARE(b.toInt(&ok), 2147483647); |
1453 | QVERIFY(ok); |
1454 | |
1455 | if (sizeof(int) == 4) { |
1456 | a = "-2147483647" ; // -(2**31 - 1) |
1457 | b = a.leftRef(n: -1); |
1458 | QCOMPARE(b.toInt(), -2147483647); |
1459 | QCOMPARE(b.toInt(&ok), -2147483647); |
1460 | QVERIFY(ok); |
1461 | |
1462 | a = "2147483648" ; // 2**31 |
1463 | b = a.leftRef(n: -1); |
1464 | QCOMPARE(b.toInt(), 0); |
1465 | QCOMPARE(b.toInt(&ok), 0); |
1466 | QVERIFY(!ok); |
1467 | |
1468 | a = "-2147483648" ; // -2**31 |
1469 | b = a.leftRef(n: -1); |
1470 | QCOMPARE(b.toInt(), -2147483647 - 1); |
1471 | QCOMPARE(b.toInt(&ok), -2147483647 - 1); |
1472 | QVERIFY(ok); |
1473 | |
1474 | a = "2147483649" ; // 2**31 + 1 |
1475 | b = a.leftRef(n: -1); |
1476 | QCOMPARE(b.toInt(), 0); |
1477 | QCOMPARE(b.toInt(&ok), 0); |
1478 | QVERIFY(!ok); |
1479 | } |
1480 | } |
1481 | |
1482 | void tst_QStringRef::toUInt() |
1483 | { |
1484 | bool ok; |
1485 | QString a; |
1486 | QStringRef b; |
1487 | a = "3234567890" ; |
1488 | b = a.leftRef(n: -1); |
1489 | QCOMPARE(b.toUInt(&ok), 3234567890u); |
1490 | QVERIFY(ok); |
1491 | |
1492 | a = "-50" ; |
1493 | b = a.leftRef(n: -1); |
1494 | QCOMPARE(b.toUInt(), 0u); |
1495 | QCOMPARE(b.toUInt(&ok), 0u); |
1496 | QVERIFY(!ok); |
1497 | |
1498 | a = "4294967295" ; // 2**32 - 1 |
1499 | b = a.leftRef(n: -1); |
1500 | QCOMPARE(b.toUInt(), 4294967295u); |
1501 | QCOMPARE(b.toUInt(&ok), 4294967295u); |
1502 | QVERIFY(ok); |
1503 | |
1504 | if (sizeof(int) == 4) { |
1505 | a = "4294967296" ; // 2**32 |
1506 | b = a.leftRef(n: -1); |
1507 | QCOMPARE(b.toUInt(), 0u); |
1508 | QCOMPARE(b.toUInt(&ok), 0u); |
1509 | QVERIFY(!ok); |
1510 | } |
1511 | } |
1512 | |
1513 | ///////////////////////////// to*Long ////////////////////////////////////// |
1514 | |
1515 | void tst_QStringRef::toULong_data() |
1516 | { |
1517 | QTest::addColumn<QString>(name: "str" ); |
1518 | QTest::addColumn<int>(name: "base" ); |
1519 | QTest::addColumn<ulong>(name: "result" ); |
1520 | QTest::addColumn<bool>(name: "ok" ); |
1521 | |
1522 | QTest::newRow(dataTag: "default" ) << QString() << 10 << 0UL << false; |
1523 | QTest::newRow(dataTag: "empty" ) << QString("" ) << 10 << 0UL << false; |
1524 | QTest::newRow(dataTag: "ulong1" ) << QString("3234567890" ) << 10 << 3234567890UL << true; |
1525 | QTest::newRow(dataTag: "ulong2" ) << QString("fFFfFfFf" ) << 16 << 0xFFFFFFFFUL << true; |
1526 | } |
1527 | |
1528 | void tst_QStringRef::toULong() |
1529 | { |
1530 | QFETCH(QString, str); |
1531 | QFETCH(int, base); |
1532 | QFETCH(ulong, result); |
1533 | QFETCH(bool, ok); |
1534 | QStringRef strRef = str.leftRef(n: -1); |
1535 | |
1536 | bool b; |
1537 | QCOMPARE(strRef.toULong(0, base), result); |
1538 | QCOMPARE(strRef.toULong(&b, base), result); |
1539 | QCOMPARE(b, ok); |
1540 | } |
1541 | |
1542 | void tst_QStringRef::toLong_data() |
1543 | { |
1544 | QTest::addColumn<QString>(name: "str" ); |
1545 | QTest::addColumn<int>(name: "base" ); |
1546 | QTest::addColumn<long>(name: "result" ); |
1547 | QTest::addColumn<bool>(name: "ok" ); |
1548 | |
1549 | QTest::newRow(dataTag: "default" ) << QString() << 10 << 0L << false; |
1550 | QTest::newRow(dataTag: "empty" ) << QString("" ) << 10 << 0L << false; |
1551 | QTest::newRow(dataTag: "normal" ) << QString("7fFFfFFf" ) << 16 << 0x7fFFfFFfL << true; |
1552 | QTest::newRow(dataTag: "long_max" ) << QString("2147483647" ) << 10 << 2147483647L << true; |
1553 | if (sizeof(long) == 4) { |
1554 | QTest::newRow(dataTag: "long_max+1" ) << QString("2147483648" ) << 10 << 0L << false; |
1555 | QTest::newRow(dataTag: "long_min-1" ) << QString("-80000001" ) << 16 << 0L << false; |
1556 | } |
1557 | QTest::newRow(dataTag: "negative" ) << QString("-7fffffff" ) << 16 << -0x7fffffffL << true; |
1558 | // QTest::newRow("long_min") << QString("-80000000") << 16 << 0x80000000uL << true; |
1559 | } |
1560 | |
1561 | void tst_QStringRef::toLong() |
1562 | { |
1563 | QFETCH(QString, str); |
1564 | QFETCH(int, base); |
1565 | QFETCH(long, result); |
1566 | QFETCH(bool, ok); |
1567 | QStringRef strRef = str.leftRef(n: -1); |
1568 | |
1569 | bool b; |
1570 | QCOMPARE(strRef.toLong(0, base), result); |
1571 | QCOMPARE(strRef.toLong(&b, base), result); |
1572 | QCOMPARE(b, ok); |
1573 | } |
1574 | |
1575 | |
1576 | ////////////////////////// to*LongLong ////////////////////////////////////// |
1577 | |
1578 | void tst_QStringRef::toULongLong() |
1579 | { |
1580 | QString str; |
1581 | QStringRef strRef; |
1582 | bool ok; |
1583 | str = "18446744073709551615" ; // ULLONG_MAX |
1584 | strRef = str.leftRef(n: -1); |
1585 | QCOMPARE(strRef.toULongLong(0), Q_UINT64_C(18446744073709551615)); |
1586 | QCOMPARE(strRef.toULongLong(&ok), Q_UINT64_C(18446744073709551615)); |
1587 | QVERIFY(ok); |
1588 | |
1589 | str = "18446744073709551616" ; // ULLONG_MAX + 1 |
1590 | strRef = str.leftRef(n: -1); |
1591 | QCOMPARE(strRef.toULongLong(0), Q_UINT64_C(0)); |
1592 | QCOMPARE(strRef.toULongLong(&ok), Q_UINT64_C(0)); |
1593 | QVERIFY(!ok); |
1594 | |
1595 | str = "-150" ; |
1596 | strRef = str.leftRef(n: -1); |
1597 | QCOMPARE(strRef.toULongLong(0), Q_UINT64_C(0)); |
1598 | QCOMPARE(strRef.toULongLong(&ok), Q_UINT64_C(0)); |
1599 | QVERIFY(!ok); |
1600 | } |
1601 | |
1602 | void tst_QStringRef::toLongLong() |
1603 | { |
1604 | QString str; |
1605 | QStringRef strRef; |
1606 | bool ok; |
1607 | |
1608 | str = "9223372036854775807" ; // LLONG_MAX |
1609 | strRef = str.leftRef(n: -1); |
1610 | QCOMPARE(strRef.toLongLong(0), Q_INT64_C(9223372036854775807)); |
1611 | QCOMPARE(strRef.toLongLong(&ok), Q_INT64_C(9223372036854775807)); |
1612 | QVERIFY(ok); |
1613 | |
1614 | str = "-9223372036854775808" ; // LLONG_MIN |
1615 | strRef = str.leftRef(n: -1); |
1616 | QCOMPARE(strRef.toLongLong(0), |
1617 | -Q_INT64_C(9223372036854775807) - Q_INT64_C(1)); |
1618 | QCOMPARE(strRef.toLongLong(&ok), |
1619 | -Q_INT64_C(9223372036854775807) - Q_INT64_C(1)); |
1620 | QVERIFY(ok); |
1621 | |
1622 | str = "aaaa9223372036854775807aaaa" ; |
1623 | strRef = str.leftRef(n: -1); |
1624 | QCOMPARE(strRef.toLongLong(0), Q_INT64_C(0)); |
1625 | QCOMPARE(strRef.toLongLong(&ok), Q_INT64_C(0)); |
1626 | QVERIFY(!ok); |
1627 | |
1628 | str = "9223372036854775807aaaa" ; |
1629 | strRef = str.leftRef(n: -1); |
1630 | QCOMPARE(strRef.toLongLong(0), Q_INT64_C(0)); |
1631 | QCOMPARE(strRef.toLongLong(&ok), Q_INT64_C(0)); |
1632 | QVERIFY(!ok); |
1633 | |
1634 | str = "aaaa9223372036854775807" ; |
1635 | strRef = str.leftRef(n: -1); |
1636 | QCOMPARE(strRef.toLongLong(0), Q_INT64_C(0)); |
1637 | QCOMPARE(strRef.toLongLong(&ok), Q_INT64_C(0)); |
1638 | QVERIFY(!ok); |
1639 | |
1640 | static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; |
1641 | |
1642 | for (int i = 0; i < 36; ++i) { |
1643 | for (int j = 0; j < 36; ++j) { |
1644 | for (int k = 0; k < 36; ++k) { |
1645 | QString str; |
1646 | str += QChar(digits[i]); |
1647 | str += QChar(digits[j]); |
1648 | str += QChar(digits[k]); |
1649 | strRef = str.leftRef(n: -1); |
1650 | qlonglong value = (((i * 36) + j) * 36) + k; |
1651 | QVERIFY(strRef.toLongLong(0, 36) == value); |
1652 | } |
1653 | } |
1654 | } |
1655 | } |
1656 | |
1657 | //////////////////////////////////////////////////////////////////////////// |
1658 | |
1659 | void tst_QStringRef::toFloat() |
1660 | { |
1661 | QString a; |
1662 | QStringRef b; |
1663 | bool ok; |
1664 | a = "0.000000000931322574615478515625" ; |
1665 | b = a.leftRef(n: -1); |
1666 | QCOMPARE(b.toFloat(&ok), float(0.000000000931322574615478515625)); |
1667 | QVERIFY(ok); |
1668 | } |
1669 | |
1670 | void tst_QStringRef::toDouble_data() |
1671 | { |
1672 | QTest::addColumn<QString>(name: "str" ); |
1673 | QTest::addColumn<double>(name: "result" ); |
1674 | QTest::addColumn<bool>(name: "result_ok" ); |
1675 | |
1676 | QTest::newRow(dataTag: "ok00" ) << QString("0.000000000931322574615478515625" ) << 0.000000000931322574615478515625 << true; |
1677 | QTest::newRow(dataTag: "ok01" ) << QString(" 123.45" ) << 123.45 << true; |
1678 | |
1679 | QTest::newRow(dataTag: "ok02" ) << QString("0.1e10" ) << 0.1e10 << true; |
1680 | QTest::newRow(dataTag: "ok03" ) << QString("0.1e-10" ) << 0.1e-10 << true; |
1681 | |
1682 | QTest::newRow(dataTag: "ok04" ) << QString("1e10" ) << 1.0e10 << true; |
1683 | QTest::newRow(dataTag: "ok05" ) << QString("1e+10" ) << 1.0e10 << true; |
1684 | QTest::newRow(dataTag: "ok06" ) << QString("1e-10" ) << 1.0e-10 << true; |
1685 | |
1686 | QTest::newRow(dataTag: "ok07" ) << QString(" 1e10" ) << 1.0e10 << true; |
1687 | QTest::newRow(dataTag: "ok08" ) << QString(" 1e+10" ) << 1.0e10 << true; |
1688 | QTest::newRow(dataTag: "ok09" ) << QString(" 1e-10" ) << 1.0e-10 << true; |
1689 | |
1690 | QTest::newRow(dataTag: "ok10" ) << QString("1." ) << 1.0 << true; |
1691 | QTest::newRow(dataTag: "ok11" ) << QString(".1" ) << 0.1 << true; |
1692 | |
1693 | QTest::newRow(dataTag: "wrong00" ) << QString("123.45 " ) << 123.45 << true; |
1694 | QTest::newRow(dataTag: "wrong01" ) << QString(" 123.45 " ) << 123.45 << true; |
1695 | |
1696 | QTest::newRow(dataTag: "wrong02" ) << QString("aa123.45aa" ) << 0.0 << false; |
1697 | QTest::newRow(dataTag: "wrong03" ) << QString("123.45aa" ) << 0.0 << false; |
1698 | QTest::newRow(dataTag: "wrong04" ) << QString("123erf" ) << 0.0 << false; |
1699 | |
1700 | QTest::newRow(dataTag: "wrong05" ) << QString("abc" ) << 0.0 << false; |
1701 | QTest::newRow(dataTag: "wrong06" ) << QString() << 0.0 << false; |
1702 | QTest::newRow(dataTag: "wrong07" ) << QString("" ) << 0.0 << false; |
1703 | } |
1704 | |
1705 | void tst_QStringRef::toDouble() |
1706 | { |
1707 | QFETCH(QString, str); |
1708 | QFETCH(bool, result_ok); |
1709 | QStringRef strRef = str.leftRef(n: -1); |
1710 | bool ok; |
1711 | double d = strRef.toDouble(ok: &ok); |
1712 | if (result_ok) { |
1713 | QTEST(d, "result" ); |
1714 | QVERIFY(ok); |
1715 | } else { |
1716 | QVERIFY(!ok); |
1717 | } |
1718 | } |
1719 | |
1720 | void tst_QStringRef::integer_conversion_data() |
1721 | { |
1722 | QTest::addColumn<QString>(name: "num_str" ); |
1723 | QTest::addColumn<int>(name: "base" ); |
1724 | QTest::addColumn<bool>(name: "good" ); |
1725 | QTest::addColumn<qlonglong>(name: "num" ); |
1726 | |
1727 | QTest::newRow(dataTag: "C empty 0" ) << QString("" ) << 0 << false << (qlonglong)0; |
1728 | QTest::newRow(dataTag: "C empty 8" ) << QString("" ) << 8 << false << (qlonglong)0; |
1729 | QTest::newRow(dataTag: "C empty 10" ) << QString("" ) << 10 << false << (qlonglong)0; |
1730 | QTest::newRow(dataTag: "C empty 16" ) << QString("" ) << 16 << false << (qlonglong)0; |
1731 | |
1732 | QTest::newRow(dataTag: "C null 0" ) << QString() << 0 << false << (qlonglong)0; |
1733 | QTest::newRow(dataTag: "C null 8" ) << QString() << 8 << false << (qlonglong)0; |
1734 | QTest::newRow(dataTag: "C null 10" ) << QString() << 10 << false << (qlonglong)0; |
1735 | QTest::newRow(dataTag: "C null 16" ) << QString() << 16 << false << (qlonglong)0; |
1736 | |
1737 | QTest::newRow(dataTag: "C -0xf 0" ) << QString(" -0xf" ) << 0 << true << (qlonglong)-15; |
1738 | QTest::newRow(dataTag: "C -0xf 0" ) << QString("-0xf " ) << 0 << true << (qlonglong)-15; |
1739 | QTest::newRow(dataTag: "C \t0xf\t 0" ) << QString("\t0xf\t" ) << 0 << true << (qlonglong)15; |
1740 | QTest::newRow(dataTag: "C -010 0" ) << QString(" -010" ) << 0 << true << (qlonglong)-8; |
1741 | QTest::newRow(dataTag: "C 010 0" ) << QString("010 " ) << 0 << true << (qlonglong)8; |
1742 | QTest::newRow(dataTag: "C \t-010\t 0" ) << QString("\t-010\t" ) << 0 << true << (qlonglong)-8; |
1743 | QTest::newRow(dataTag: "C 123 10" ) << QString(" 123" ) << 10 << true << (qlonglong)123; |
1744 | QTest::newRow(dataTag: "C 123 10" ) << QString("123 " ) << 10 << true << (qlonglong)123; |
1745 | QTest::newRow(dataTag: "C \t123\t 10" ) << QString("\t123\t" ) << 10 << true << (qlonglong)123; |
1746 | QTest::newRow(dataTag: "C -0xf 16" ) << QString(" -0xf" ) << 16 << true << (qlonglong)-15; |
1747 | QTest::newRow(dataTag: "C -0xf 16" ) << QString("-0xf " ) << 16 << true << (qlonglong)-15; |
1748 | QTest::newRow(dataTag: "C \t0xf\t 16" ) << QString("\t0xf\t" ) << 16 << true << (qlonglong)15; |
1749 | |
1750 | QTest::newRow(dataTag: "C -0 0" ) << QString("-0" ) << 0 << true << (qlonglong)0; |
1751 | QTest::newRow(dataTag: "C -0 8" ) << QString("-0" ) << 8 << true << (qlonglong)0; |
1752 | QTest::newRow(dataTag: "C -0 10" ) << QString("-0" ) << 10 << true << (qlonglong)0; |
1753 | QTest::newRow(dataTag: "C -0 16" ) << QString("-0" ) << 16 << true << (qlonglong)0; |
1754 | |
1755 | QTest::newRow(dataTag: "C 1.234 10" ) << QString("1.234" ) << 10 << false << (qlonglong)0; |
1756 | QTest::newRow(dataTag: "C 1,234 10" ) << QString("1,234" ) << 10 << false << (qlonglong)0; |
1757 | |
1758 | QTest::newRow(dataTag: "C 0x 0" ) << QString("0x" ) << 0 << false << (qlonglong)0; |
1759 | QTest::newRow(dataTag: "C 0x 16" ) << QString("0x" ) << 16 << false << (qlonglong)0; |
1760 | |
1761 | QTest::newRow(dataTag: "C 10 0" ) << QString("10" ) << 0 << true << (qlonglong)10; |
1762 | QTest::newRow(dataTag: "C 010 0" ) << QString("010" ) << 0 << true << (qlonglong)8; |
1763 | QTest::newRow(dataTag: "C 0x10 0" ) << QString("0x10" ) << 0 << true << (qlonglong)16; |
1764 | QTest::newRow(dataTag: "C 10 8" ) << QString("10" ) << 8 << true << (qlonglong)8; |
1765 | QTest::newRow(dataTag: "C 010 8" ) << QString("010" ) << 8 << true << (qlonglong)8; |
1766 | QTest::newRow(dataTag: "C 0x10 8" ) << QString("0x10" ) << 8 << false << (qlonglong)0; |
1767 | QTest::newRow(dataTag: "C 10 10" ) << QString("10" ) << 10 << true << (qlonglong)10; |
1768 | QTest::newRow(dataTag: "C 010 10" ) << QString("010" ) << 10 << true << (qlonglong)10; |
1769 | QTest::newRow(dataTag: "C 0x10 10" ) << QString("0x10" ) << 10 << false << (qlonglong)0; |
1770 | QTest::newRow(dataTag: "C 10 16" ) << QString("10" ) << 16 << true << (qlonglong)16; |
1771 | QTest::newRow(dataTag: "C 010 16" ) << QString("010" ) << 16 << true << (qlonglong)16; |
1772 | QTest::newRow(dataTag: "C 0x10 16" ) << QString("0x10" ) << 16 << true << (qlonglong)16; |
1773 | |
1774 | QTest::newRow(dataTag: "C -10 0" ) << QString("-10" ) << 0 << true << (qlonglong)-10; |
1775 | QTest::newRow(dataTag: "C -010 0" ) << QString("-010" ) << 0 << true << (qlonglong)-8; |
1776 | QTest::newRow(dataTag: "C -0x10 0" ) << QString("-0x10" ) << 0 << true << (qlonglong)-16; |
1777 | QTest::newRow(dataTag: "C -10 8" ) << QString("-10" ) << 8 << true << (qlonglong)-8; |
1778 | QTest::newRow(dataTag: "C -010 8" ) << QString("-010" ) << 8 << true << (qlonglong)-8; |
1779 | QTest::newRow(dataTag: "C -0x10 8" ) << QString("-0x10" ) << 8 << false << (qlonglong)0; |
1780 | QTest::newRow(dataTag: "C -10 10" ) << QString("-10" ) << 10 << true << (qlonglong)-10; |
1781 | QTest::newRow(dataTag: "C -010 10" ) << QString("-010" ) << 10 << true << (qlonglong)-10; |
1782 | QTest::newRow(dataTag: "C -0x10 10" ) << QString("-0x10" ) << 10 << false << (qlonglong)0; |
1783 | QTest::newRow(dataTag: "C -10 16" ) << QString("-10" ) << 16 << true << (qlonglong)-16; |
1784 | QTest::newRow(dataTag: "C -010 16" ) << QString("-010" ) << 16 << true << (qlonglong)-16; |
1785 | QTest::newRow(dataTag: "C -0x10 16" ) << QString("-0x10" ) << 16 << true << (qlonglong)-16; |
1786 | |
1787 | // Let's try some Arabic |
1788 | const quint16 arabic_str[] = { 0x0661, 0x0662, 0x0663, 0x0664, 0x0000 }; // "1234" |
1789 | QTest::newRow(dataTag: "ar_SA 1234 0" ) << QString::fromUtf16(arabic_str) << 0 << false << (qlonglong)0; |
1790 | } |
1791 | |
1792 | void tst_QStringRef::integer_conversion() |
1793 | { |
1794 | QFETCH(QString, num_str); |
1795 | QFETCH(int, base); |
1796 | QFETCH(bool, good); |
1797 | QFETCH(qlonglong, num); |
1798 | QStringRef num_strRef = num_str.leftRef(n: -1); |
1799 | |
1800 | bool ok; |
1801 | qlonglong d = num_strRef.toLongLong(ok: &ok, base); |
1802 | QCOMPARE(ok, good); |
1803 | |
1804 | if (ok) { |
1805 | QCOMPARE(d, num); |
1806 | } |
1807 | } |
1808 | |
1809 | void tst_QStringRef::double_conversion_data() |
1810 | { |
1811 | QTest::addColumn<QString>(name: "num_str" ); |
1812 | QTest::addColumn<bool>(name: "good" ); |
1813 | QTest::addColumn<double>(name: "num" ); |
1814 | |
1815 | // The good... |
1816 | |
1817 | QTest::newRow(dataTag: "C 1" ) << QString("1" ) << true << 1.0; |
1818 | QTest::newRow(dataTag: "C 1.0" ) << QString("1.0" ) << true << 1.0; |
1819 | QTest::newRow(dataTag: "C 1.234" ) << QString("1.234" ) << true << 1.234; |
1820 | QTest::newRow(dataTag: "C 1.234e-10" ) << QString("1.234e-10" ) << true << 1.234e-10; |
1821 | QTest::newRow(dataTag: "C 1.234E10" ) << QString("1.234E10" ) << true << 1.234e10; |
1822 | QTest::newRow(dataTag: "C 1e10" ) << QString("1e10" ) << true << 1.0e10; |
1823 | |
1824 | // The bad... |
1825 | |
1826 | QTest::newRow(dataTag: "C empty" ) << QString("" ) << false << 0.0; |
1827 | QTest::newRow(dataTag: "C null" ) << QString() << false << 0.0; |
1828 | QTest::newRow(dataTag: "C ." ) << QString("." ) << false << 0.0; |
1829 | QTest::newRow(dataTag: "C 1e" ) << QString("1e" ) << false << 0.0; |
1830 | QTest::newRow(dataTag: "C 1," ) << QString("1," ) << false << 0.0; |
1831 | QTest::newRow(dataTag: "C 1,0" ) << QString("1,0" ) << false << 0.0; |
1832 | QTest::newRow(dataTag: "C 1,000" ) << QString("1,000" ) << false << 0.0; |
1833 | QTest::newRow(dataTag: "C 1e1.0" ) << QString("1e1.0" ) << false << 0.0; |
1834 | QTest::newRow(dataTag: "C 1e+" ) << QString("1e+" ) << false << 0.0; |
1835 | QTest::newRow(dataTag: "C 1e-" ) << QString("1e-" ) << false << 0.0; |
1836 | QTest::newRow(dataTag: "de_DE 1,0" ) << QString("1,0" ) << false << 0.0; |
1837 | QTest::newRow(dataTag: "de_DE 1,234" ) << QString("1,234" ) << false << 0.0; |
1838 | QTest::newRow(dataTag: "de_DE 1,234e-10" ) << QString("1,234e-10" ) << false << 0.0; |
1839 | QTest::newRow(dataTag: "de_DE 1,234E10" ) << QString("1,234E10" ) << false << 0.0; |
1840 | |
1841 | // And the ugly... |
1842 | |
1843 | QTest::newRow(dataTag: "C .1" ) << QString(".1" ) << true << 0.1; |
1844 | QTest::newRow(dataTag: "C -.1" ) << QString("-.1" ) << true << -0.1; |
1845 | QTest::newRow(dataTag: "C 1." ) << QString("1." ) << true << 1.0; |
1846 | QTest::newRow(dataTag: "C 1.E10" ) << QString("1.E10" ) << true << 1.0e10; |
1847 | QTest::newRow(dataTag: "C 1e+10" ) << QString("1e+10" ) << true << 1.0e+10; |
1848 | QTest::newRow(dataTag: "C 1" ) << QString(" 1" ) << true << 1.0; |
1849 | QTest::newRow(dataTag: "C 1 " ) << QString("1 " ) << true << 1.0; |
1850 | |
1851 | // Let's try some Arabic |
1852 | const quint16 arabic_str[] = { 0x0660, 0x066B, 0x0661, 0x0662, |
1853 | 0x0663, 0x0664, 0x0065, 0x0662, |
1854 | 0x0000 }; // "0.1234e2" |
1855 | QTest::newRow(dataTag: "ar_SA" ) << QString::fromUtf16(arabic_str) << false << 0.0; |
1856 | } |
1857 | |
1858 | void tst_QStringRef::double_conversion() |
1859 | { |
1860 | #define MY_DOUBLE_EPSILON (2.22045e-16) |
1861 | |
1862 | QFETCH(QString, num_str); |
1863 | QFETCH(bool, good); |
1864 | QFETCH(double, num); |
1865 | QStringRef num_strRef = num_str.leftRef(n: -1); |
1866 | |
1867 | bool ok; |
1868 | double d = num_strRef.toDouble(ok: &ok); |
1869 | QCOMPARE(ok, good); |
1870 | |
1871 | if (ok) { |
1872 | double diff = d - num; |
1873 | if (diff < 0) |
1874 | diff = -diff; |
1875 | QVERIFY(diff <= MY_DOUBLE_EPSILON); |
1876 | } |
1877 | } |
1878 | |
1879 | void tst_QStringRef::trimmed() |
1880 | { |
1881 | QVERIFY(QStringRef().trimmed().isNull()); |
1882 | QString a = "" ; |
1883 | QVERIFY(!QStringRef(&a).trimmed().isNull()); |
1884 | QStringRef b; |
1885 | a = "Text" ; |
1886 | b = a.leftRef(n: -1); |
1887 | QCOMPARE(b.compare(QStringLiteral("Text" )), 0); |
1888 | QCOMPARE(b.trimmed().compare(QStringLiteral("Text" )), 0); |
1889 | a = " " ; |
1890 | b = a.leftRef(n: -1); |
1891 | QCOMPARE(b.compare(QStringLiteral(" " )), 0); |
1892 | QCOMPARE(b.trimmed().compare(QStringLiteral("" )), 0); |
1893 | a = " a " ; |
1894 | b = a.leftRef(n: -1); |
1895 | QCOMPARE(b.trimmed().compare(QStringLiteral("a" )), 0); |
1896 | a = "Text a " ; |
1897 | b = a.midRef(position: 4); |
1898 | QCOMPARE(b.compare(QStringLiteral(" a " )), 0); |
1899 | QCOMPARE(b.trimmed().compare(QStringLiteral("a" )), 0); |
1900 | } |
1901 | |
1902 | void tst_QStringRef::truncate() |
1903 | { |
1904 | const QString str = "OriginalString~" ; |
1905 | const QStringRef cref = str.midRef(position: 0); |
1906 | { |
1907 | QStringRef ref = cref; |
1908 | ref.truncate(pos: 1000); |
1909 | QCOMPARE(ref, cref); |
1910 | for (int i = str.size(); i >= 0; --i) { |
1911 | ref.truncate(pos: i); |
1912 | QCOMPARE(ref.size(), i); |
1913 | QCOMPARE(ref, cref.left(i)); |
1914 | } |
1915 | QVERIFY(ref.isEmpty()); |
1916 | } |
1917 | |
1918 | { |
1919 | QStringRef ref = cref; |
1920 | QVERIFY(!ref.isEmpty()); |
1921 | ref.truncate(pos: -1); |
1922 | QVERIFY(ref.isEmpty()); |
1923 | } |
1924 | } |
1925 | |
1926 | void tst_QStringRef::chop() |
1927 | { |
1928 | const QString originalString = QStringLiteral("OriginalString~" ); |
1929 | const QStringRef cref(&originalString); |
1930 | { |
1931 | const int n = 1; |
1932 | QStringRef ref = cref; |
1933 | QString str = originalString; |
1934 | ref.chop(n); |
1935 | str.chop(n); |
1936 | QCOMPARE(ref.toString(), QLatin1String("OriginalString" )); |
1937 | QCOMPARE(ref.toString(), str); |
1938 | } |
1939 | { |
1940 | const int n = -1; |
1941 | QStringRef ref = cref; |
1942 | QString str = originalString; |
1943 | ref.chop(n); |
1944 | str.chop(n); |
1945 | QCOMPARE(ref.toString(), originalString); |
1946 | QCOMPARE(ref.toString(), str); |
1947 | } |
1948 | { |
1949 | const int n = 0; |
1950 | QStringRef ref = cref; |
1951 | QString str = originalString; |
1952 | ref.chop(n); |
1953 | str.chop(n); |
1954 | QCOMPARE(ref.toString(), originalString); |
1955 | QCOMPARE(ref.toString(), str); |
1956 | } |
1957 | { |
1958 | const int n = 1000; |
1959 | QStringRef ref = cref; |
1960 | QString str = originalString; |
1961 | ref.chop(n); |
1962 | str.chop(n); |
1963 | QCOMPARE(ref.toString(), str); |
1964 | QVERIFY(ref.isEmpty()); |
1965 | } |
1966 | } |
1967 | |
1968 | void tst_QStringRef::left() |
1969 | { |
1970 | QString originalString = "OrginalString~" ; |
1971 | QStringRef ref = originalString.leftRef(n: originalString.size() - 1); |
1972 | QCOMPARE(ref.toString(), QStringLiteral("OrginalString" )); |
1973 | |
1974 | QVERIFY(ref.left(0).toString().isEmpty()); |
1975 | QCOMPARE(ref.left(ref.size()).toString(), QStringLiteral("OrginalString" )); |
1976 | |
1977 | QStringRef nullRef; |
1978 | QVERIFY(nullRef.isNull()); |
1979 | QVERIFY(nullRef.left(3).toString().isEmpty()); |
1980 | QVERIFY(nullRef.left(0).toString().isEmpty()); |
1981 | QVERIFY(nullRef.left(-1).toString().isEmpty()); |
1982 | |
1983 | QStringRef emptyRef(&originalString, 0, 0); |
1984 | QVERIFY(emptyRef.isEmpty()); |
1985 | QVERIFY(emptyRef.left(3).toString().isEmpty()); |
1986 | QVERIFY(emptyRef.left(0).toString().isEmpty()); |
1987 | QVERIFY(emptyRef.left(-1).toString().isEmpty()); |
1988 | |
1989 | QCOMPARE(ref.left(-1), ref); |
1990 | QCOMPARE(ref.left(100), ref); |
1991 | } |
1992 | |
1993 | void tst_QStringRef::right() |
1994 | { |
1995 | QString originalString = "~OrginalString" ; |
1996 | QStringRef ref = originalString.rightRef(n: originalString.size() - 1); |
1997 | QCOMPARE(ref.toString(), QLatin1String("OrginalString" )); |
1998 | |
1999 | QCOMPARE(ref.right(6).toString(), QLatin1String("String" )); |
2000 | QCOMPARE(ref.right(ref.size()).toString(), QLatin1String("OrginalString" )); |
2001 | QCOMPARE(ref.right(0).toString(), QLatin1String("" )); |
2002 | |
2003 | QStringRef nullRef; |
2004 | QVERIFY(nullRef.isNull()); |
2005 | QVERIFY(nullRef.right(3).toString().isEmpty()); |
2006 | QVERIFY(nullRef.right(0).toString().isEmpty()); |
2007 | QVERIFY(nullRef.right(-1).toString().isEmpty()); |
2008 | |
2009 | QStringRef emptyRef(&originalString, 0, 0); |
2010 | QVERIFY(emptyRef.isEmpty()); |
2011 | QVERIFY(emptyRef.right(3).toString().isEmpty()); |
2012 | QVERIFY(emptyRef.right(0).toString().isEmpty()); |
2013 | QVERIFY(emptyRef.right(-1).toString().isEmpty()); |
2014 | |
2015 | QCOMPARE(ref.right(-1), ref); |
2016 | QCOMPARE(ref.right(100), ref); |
2017 | } |
2018 | |
2019 | void tst_QStringRef::mid() |
2020 | { |
2021 | QString orig = QStringLiteral("~ABCDEFGHIEfGEFG~" ); // 15 + 2 chars |
2022 | QStringRef a = orig.midRef(position: 1, n: 15); |
2023 | QCOMPARE(a.size(), orig.size() - 2); |
2024 | |
2025 | QCOMPARE(a.mid(3,3).toString(),(QString)"DEF" ); |
2026 | QCOMPARE(a.mid(0,0).toString(),(QString)"" ); |
2027 | QVERIFY(!a.mid(15,0).toString().isNull()); |
2028 | QVERIFY(a.mid(15,0).toString().isEmpty()); |
2029 | QVERIFY(!a.mid(15,1).toString().isNull()); |
2030 | QVERIFY(a.mid(15,1).toString().isEmpty()); |
2031 | QVERIFY(a.mid(9999).toString().isEmpty()); |
2032 | QVERIFY(a.mid(9999,1).toString().isEmpty()); |
2033 | |
2034 | QCOMPARE(a.mid(-1, 6), a.mid(0, 5)); |
2035 | QVERIFY(a.mid(-100, 6).isEmpty()); |
2036 | QVERIFY(a.mid(INT_MIN, 0).isEmpty()); |
2037 | QCOMPARE(a.mid(INT_MIN, -1), a); |
2038 | QVERIFY(a.mid(INT_MIN, INT_MAX).isNull()); |
2039 | QVERIFY(a.mid(INT_MIN + 1, INT_MAX).isEmpty()); |
2040 | QCOMPARE(a.mid(INT_MIN + 2, INT_MAX), a.left(1)); |
2041 | QCOMPARE(a.mid(INT_MIN + a.size() + 1, INT_MAX), a); |
2042 | QVERIFY(a.mid(INT_MAX).isNull()); |
2043 | QVERIFY(a.mid(INT_MAX, INT_MAX).isNull()); |
2044 | QCOMPARE(a.mid(-5, INT_MAX), a); |
2045 | QCOMPARE(a.mid(-1, INT_MAX), a); |
2046 | QCOMPARE(a.mid(0, INT_MAX), a); |
2047 | QCOMPARE(a.mid(1, INT_MAX).toString(), QString("BCDEFGHIEfGEFG" )); |
2048 | QCOMPARE(a.mid(5, INT_MAX).toString(), QString("FGHIEfGEFG" )); |
2049 | QVERIFY(a.mid(20, INT_MAX).isNull()); |
2050 | QCOMPARE(a.mid(-1, -1), a); |
2051 | |
2052 | QStringRef nullRef; |
2053 | QVERIFY(nullRef.mid(3,3).toString().isEmpty()); |
2054 | QVERIFY(nullRef.mid(0,0).toString().isEmpty()); |
2055 | QVERIFY(nullRef.mid(9999,0).toString().isEmpty()); |
2056 | QVERIFY(nullRef.mid(9999,1).toString().isEmpty()); |
2057 | |
2058 | QVERIFY(nullRef.mid(-1, 6).isNull()); |
2059 | QVERIFY(nullRef.mid(-100, 6).isNull()); |
2060 | QVERIFY(nullRef.mid(INT_MIN, 0).isNull()); |
2061 | QVERIFY(nullRef.mid(INT_MIN, -1).isNull()); |
2062 | QVERIFY(nullRef.mid(INT_MIN, INT_MAX).isNull()); |
2063 | QVERIFY(nullRef.mid(INT_MIN + 1, INT_MAX).isNull()); |
2064 | QVERIFY(nullRef.mid(INT_MIN + 2, INT_MAX).isNull()); |
2065 | QVERIFY(nullRef.mid(INT_MIN + nullRef.size() + 1, INT_MAX).isNull()); |
2066 | QVERIFY(nullRef.mid(INT_MAX).isNull()); |
2067 | QVERIFY(nullRef.mid(INT_MAX, INT_MAX).isNull()); |
2068 | QVERIFY(nullRef.mid(-5, INT_MAX).isNull()); |
2069 | QVERIFY(nullRef.mid(-1, INT_MAX).isNull()); |
2070 | QVERIFY(nullRef.mid(0, INT_MAX).isNull()); |
2071 | QVERIFY(nullRef.mid(1, INT_MAX).isNull()); |
2072 | QVERIFY(nullRef.mid(5, INT_MAX).isNull()); |
2073 | QVERIFY(nullRef.mid(20, INT_MAX).isNull()); |
2074 | QVERIFY(nullRef.mid(-1, -1).isNull()); |
2075 | |
2076 | QString ninePineapples = "~Nine pineapples~" ; |
2077 | QStringRef x = ninePineapples.midRef(position: 1, n: ninePineapples.size() - 1); |
2078 | QCOMPARE(x.mid(5, 4).toString(), QString("pine" )); |
2079 | QCOMPARE(x.mid(5).toString(), QString("pineapples~" )); |
2080 | |
2081 | QCOMPARE(x.mid(-1, 6), x.mid(0, 5)); |
2082 | QVERIFY(x.mid(-100, 6).isEmpty()); |
2083 | QVERIFY(x.mid(INT_MIN, 0).isEmpty()); |
2084 | QCOMPARE(x.mid(INT_MIN, -1).toString(), x.toString()); |
2085 | QVERIFY(x.mid(INT_MIN, INT_MAX).isNull()); |
2086 | QVERIFY(x.mid(INT_MIN + 1, INT_MAX).isEmpty()); |
2087 | QCOMPARE(x.mid(INT_MIN + 2, INT_MAX), x.left(1)); |
2088 | QCOMPARE(x.mid(INT_MIN + x.size() + 1, INT_MAX).toString(), x.toString()); |
2089 | QVERIFY(x.mid(INT_MAX).isNull()); |
2090 | QVERIFY(x.mid(INT_MAX, INT_MAX).isNull()); |
2091 | QCOMPARE(x.mid(-5, INT_MAX).toString(), x.toString()); |
2092 | QCOMPARE(x.mid(-1, INT_MAX).toString(), x.toString()); |
2093 | QCOMPARE(x.mid(0, INT_MAX), x); |
2094 | QCOMPARE(x.mid(1, INT_MAX).toString(), QString("ine pineapples~" )); |
2095 | QCOMPARE(x.mid(5, INT_MAX).toString(), QString("pineapples~" )); |
2096 | QVERIFY(x.mid(20, INT_MAX).isNull()); |
2097 | QCOMPARE(x.mid(-1, -1), x); |
2098 | |
2099 | QStringRef emptyRef(&ninePineapples, 0, 0); |
2100 | QVERIFY(emptyRef.mid(1).isEmpty()); |
2101 | QVERIFY(emptyRef.mid(-1).isEmpty()); |
2102 | QVERIFY(emptyRef.mid(0).isEmpty()); |
2103 | QVERIFY(emptyRef.mid(0, 3).isEmpty()); |
2104 | QVERIFY(emptyRef.mid(-10, 3).isEmpty()); |
2105 | } |
2106 | |
2107 | static bool operator ==(const QStringList &left, const QVector<QStringRef> &right) |
2108 | { |
2109 | if (left.size() != right.size()) |
2110 | return false; |
2111 | |
2112 | QStringList::const_iterator iLeft = left.constBegin(); |
2113 | QVector<QStringRef>::const_iterator iRight = right.constBegin(); |
2114 | for (; iLeft != left.end(); ++iLeft, ++iRight) { |
2115 | if (*iLeft != *iRight) |
2116 | return false; |
2117 | } |
2118 | return true; |
2119 | } |
2120 | static inline bool operator ==(const QVector<QStringRef> &left, const QStringList &right) { return right == left; } |
2121 | |
2122 | void tst_QStringRef::split_data() |
2123 | { |
2124 | QTest::addColumn<QString>(name: "str" ); |
2125 | QTest::addColumn<QString>(name: "sep" ); |
2126 | QTest::addColumn<QStringList>(name: "result" ); |
2127 | |
2128 | QTest::newRow(dataTag: "a,b,c" ) << "a,b,c" << "," << (QStringList() << "a" << "b" << "c" ); |
2129 | QTest::newRow(dataTag: "a,b,c,a,b,c" ) << "a,b,c,a,b,c" << "," << (QStringList() << "a" << "b" << "c" << "a" << "b" << "c" ); |
2130 | QTest::newRow(dataTag: "a,b,c,,a,b,c" ) << "a,b,c,,a,b,c" << "," << (QStringList() << "a" << "b" << "c" << "" << "a" << "b" << "c" ); |
2131 | QTest::newRow(dataTag: "2" ) << QString("-rw-r--r-- 1 0 0 519240 Jul 9 2002 bigfile" ) |
2132 | << " " |
2133 | << (QStringList() << "-rw-r--r--" << "" << "1" << "0" << "" << "0" << "" |
2134 | << "519240" << "Jul" << "" << "9" << "" << "2002" << "bigfile" ); |
2135 | QTest::newRow(dataTag: "one-empty" ) << "" << " " << (QStringList() << "" ); |
2136 | QTest::newRow(dataTag: "two-empty" ) << " " << " " << (QStringList() << "" << "" ); |
2137 | QTest::newRow(dataTag: "three-empty" ) << " " << " " << (QStringList() << "" << "" << "" ); |
2138 | |
2139 | QTest::newRow(dataTag: "all-empty" ) << "" << "" << (QStringList() << "" << "" ); |
2140 | QTest::newRow(dataTag: "all-null" ) << QString() << QString() << (QStringList() << QString() << QString()); |
2141 | QTest::newRow(dataTag: "sep-empty" ) << "abc" << "" << (QStringList() << "" << "a" << "b" << "c" << "" ); |
2142 | } |
2143 | |
2144 | void tst_QStringRef::split() |
2145 | { |
2146 | QFETCH(QString, str); |
2147 | QFETCH(QString, sep); |
2148 | QFETCH(QStringList, result); |
2149 | |
2150 | QVector<QStringRef> list; |
2151 | // we construct a bigger valid string to check |
2152 | // if ref.split is using the right size |
2153 | QString source = str + str + str; |
2154 | QStringRef ref = source.midRef(position: str.size(), n: str.size()); |
2155 | QCOMPARE(ref.size(), str.size()); |
2156 | |
2157 | list = ref.split(sep); |
2158 | QVERIFY(list == result); |
2159 | if (sep.size() == 1) { |
2160 | list = ref.split(sep: sep.at(i: 0)); |
2161 | QVERIFY(list == result); |
2162 | } |
2163 | |
2164 | list = ref.split(sep, behavior: Qt::KeepEmptyParts); |
2165 | QVERIFY(list == result); |
2166 | if (sep.size() == 1) { |
2167 | list = ref.split(sep: sep.at(i: 0), behavior: Qt::KeepEmptyParts); |
2168 | QVERIFY(list == result); |
2169 | } |
2170 | |
2171 | result.removeAll(t: "" ); |
2172 | list = ref.split(sep, behavior: Qt::SkipEmptyParts); |
2173 | QVERIFY(list == result); |
2174 | if (sep.size() == 1) { |
2175 | list = ref.split(sep: sep.at(i: 0), behavior: Qt::SkipEmptyParts); |
2176 | QVERIFY(list == result); |
2177 | } |
2178 | } |
2179 | |
2180 | QTEST_APPLESS_MAIN(tst_QStringRef) |
2181 | |
2182 | #include "tst_qstringref.moc" |
2183 | |