fix QString::isRightToLeft() for SMP code points
[profile/ivi/qtbase.git] / tests / auto / corelib / tools / qstring / tst_qstring.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifdef QT_NO_CAST_FROM_ASCII
43 # undef QT_NO_CAST_FROM_ASCII
44 #endif
45 #ifdef QT_NO_CAST_TO_ASCII
46 # undef QT_NO_CAST_TO_ASCII
47 #endif
48 #ifdef QT_ASCII_CAST_WARNINGS
49 # undef QT_ASCII_CAST_WARNINGS
50 #endif
51
52 #include <QtTest/QtTest>
53 #include <qregexp.h>
54 #include <qregularexpression.h>
55 #include <qtextcodec.h>
56 #include <qtextstream.h>
57 #include <qstringlist.h>
58 #include <qstringmatcher.h>
59 #include <qbytearraymatcher.h>
60 #include <qvariant.h>
61
62 #include <qlocale.h>
63 #include <locale.h>
64 #include <qhash.h>
65
66 Q_DECLARE_METATYPE(qlonglong)
67
68 #define CREATE_REF(string)                                              \
69     const QString padded = QString::fromLatin1(" %1 ").arg(string);     \
70     QStringRef ref = padded.midRef(1, padded.size() - 2);
71
72 class tst_QString : public QObject
73 {
74     Q_OBJECT
75
76 public:
77     tst_QString();
78 public slots:
79     void cleanup();
80 private slots:
81     void fromStdString();
82     void toStdString();
83     void check_QTextIOStream();
84     void check_QTextStream();
85     void check_QDataStream();
86     void fromRawData();
87     void setRawData();
88     void endsWith();
89     void startsWith();
90     void setNum();
91     void toDouble_data();
92     void toDouble();
93     void toFloat();
94     void toLong_data();
95     void toLong();
96     void toULong_data();
97     void toULong();
98     void toLongLong();
99     void toULongLong();
100     void toUInt();
101     void toInt();
102     void toShort();
103     void toUShort();
104     void replace_qchar_qchar_data();
105     void replace_qchar_qchar();
106     void replace_qchar_qstring_data();
107     void replace_qchar_qstring();
108     void replace_uint_uint_data();
109     void replace_uint_uint();
110     void replace_uint_uint_extra();
111     void replace_string_data();
112     void replace_string();
113     void replace_regexp_data();
114     void replace_regexp();
115     void remove_uint_uint_data();
116     void remove_uint_uint();
117     void remove_string_data();
118     void remove_string();
119     void remove_regexp_data();
120     void remove_regexp();
121     void swap();
122     void prepend();
123     void prepend_bytearray_data();
124     void prepend_bytearray();
125     void append();
126     void append_bytearray_data();
127     void append_bytearray();
128     void operator_pluseq_bytearray_data();
129     void operator_pluseq_bytearray();
130     void operator_eqeq_bytearray_data();
131     void operator_eqeq_bytearray();
132     void operator_eqeq_nullstring();
133     void operator_smaller();
134     void insert();
135     void simplified_data();
136     void simplified();
137     void trimmed();
138     void toUpper();
139     void toLower();
140     void toCaseFolded();
141     void rightJustified();
142     void leftJustified();
143     void mid();
144     void right();
145     void left();
146     void midRef();
147     void rightRef();
148     void leftRef();
149     void stringRef();
150     void contains();
151     void count();
152     void lastIndexOf_data();
153     void lastIndexOf();
154     void indexOf_data();
155     void indexOf();
156     void indexOf2_data();
157     void indexOf2();
158     void indexOf3_data();
159 //  void indexOf3();
160     void sprintf();
161     void fill();
162     void truncate();
163     void constructor();
164     void constructorQByteArray_data();
165     void constructorQByteArray();
166     void STL();
167     void isEmpty();
168     void isNull();
169     void acc_01();
170     void length_data();
171     void length();
172     void utf8_data();
173     void utf8();
174     void fromUtf8_data();
175     void fromUtf8();
176     void nullFromUtf8();
177     void fromLocal8Bit_data();
178     void fromLocal8Bit();
179     void local8Bit_data();
180     void local8Bit();
181     void fromLatin1Roundtrip_data();
182     void fromLatin1Roundtrip();
183     void toLatin1Roundtrip_data();
184     void toLatin1Roundtrip();
185     void stringRef_toLatin1Roundtrip_data();
186     void stringRef_toLatin1Roundtrip();
187     void stringRef_utf8_data();
188     void stringRef_utf8();
189     void stringRef_local8Bit_data();
190     void stringRef_local8Bit();
191     void fromLatin1();
192     void fromAscii();
193     void arg();
194     void number();
195     void arg_fillChar_data();
196     void arg_fillChar();
197     void capacity_data();
198     void capacity();
199     void section_data();
200     void section();
201     void double_conversion_data();
202     void double_conversion();
203     void integer_conversion_data();
204     void integer_conversion();
205     void tortureSprintfDouble();
206     void toNum();
207     void localeAwareCompare_data();
208     void localeAwareCompare();
209     void split_data();
210     void split();
211     void split_regexp_data();
212     void split_regexp();
213     void fromUtf16_data();
214     void fromUtf16();
215     void latin1String();
216     void nanAndInf();
217     void compare_data();
218     void compare();
219     void resizeAfterFromRawData();
220     void resizeAfterReserve();
221     void resizeWithNegative() const;
222     void truncateWithNegative() const;
223     void QCharRefMutableUnicode() const;
224     void QCharRefDetaching() const;
225     void sprintfZU() const;
226     void repeatedSignature() const;
227     void repeated() const;
228     void repeated_data() const;
229     void compareRef();
230     void arg_locale();
231     void toUpperLower_icu();
232     void literals();
233     void eightBitLiterals_data();
234     void eightBitLiterals();
235     void reserve();
236     void toHtmlEscaped_data();
237     void toHtmlEscaped();
238     void operatorGreaterWithQLatin1String();
239     void compareQLatin1Strings();
240     void fromQLatin1StringWithLength();
241     void assignQLatin1String();
242     void isRightToLeft_data();
243     void isRightToLeft();
244 };
245
246 template <class T> const T &verifyZeroTermination(const T &t) { return t; }
247
248 QString verifyZeroTermination(const QString &str)
249 {
250     // This test does some evil stuff, it's all supposed to work.
251
252     QString::DataPtr strDataPtr = const_cast<QString &>(str).data_ptr();
253
254     // Skip if isStatic() or fromRawData(), as those offer no guarantees
255     if (strDataPtr->ref.isStatic()
256             || strDataPtr->offset != QString().data_ptr()->offset)
257         return str;
258
259     int strSize = str.size();
260     QChar strTerminator = str.constData()[strSize];
261     if (QChar('\0') != strTerminator)
262         return QString::fromAscii(
263             "*** Result ('%1') not null-terminated: 0x%2 ***").arg(str)
264                 .arg(strTerminator.unicode(), 4, 16, QChar('0'));
265
266     // Skip mutating checks on shared strings
267     if (strDataPtr->ref.isShared())
268         return str;
269
270     const QChar *strData = str.constData();
271     const QString strCopy(strData, strSize); // Deep copy
272
273     const_cast<QChar *>(strData)[strSize] = QChar('x');
274     if (QChar('x') != str.constData()[strSize]) {
275         return QString::fromAscii("*** Failed to replace null-terminator in "
276                 "result ('%1') ***").arg(str);
277     }
278     if (str != strCopy) {
279         return QString::fromAscii( "*** Result ('%1') differs from its copy "
280                 "after null-terminator was replaced ***").arg(str);
281     }
282     const_cast<QChar *>(strData)[strSize] = QChar('\0'); // Restore sanity
283
284     return str;
285 }
286
287 // Overriding QTest's QCOMPARE, to check QString for null termination
288 #undef QCOMPARE
289 #define QCOMPARE(actual, expected)                                      \
290     do {                                                                \
291         if (!QTest::qCompare(verifyZeroTermination(actual), expected,   \
292                 #actual, #expected, __FILE__, __LINE__))                \
293             return;                                                     \
294     } while (0)                                                         \
295     /**/
296 #undef QTEST
297 #define QTEST(actual, testElement)                                      \
298     do {                                                                \
299         if (!QTest::qTest(verifyZeroTermination(actual), testElement,   \
300                 #actual, #testElement, __FILE__, __LINE__))             \
301             return;                                                     \
302     } while (0)                                                         \
303     /**/
304
305 typedef QList<int> IntList;
306
307 Q_DECLARE_METATYPE(QList<QVariant>)
308 Q_DECLARE_METATYPE(IntList)
309
310 // This next bit is needed for the NAN and INF in string -> number conversion tests
311 #include <float.h>
312 #include <limits.h>
313 #include <math.h>
314 #if defined (Q_OS_WIN)
315 #   include <windows.h>
316 // mingw defines NAN and INFINITY to 0/0 and x/0
317 #   if defined(Q_CC_GNU)
318 #      undef NAN
319 #      undef INFINITY
320 #   else
321 #      define isnan(d) _isnan(d)
322 #   endif
323 #endif
324 #if defined (Q_OS_MAC) && !defined isnan
325 #define isnan(d) __isnand(d)
326 #endif
327 #if defined (Q_OS_SOLARIS)
328 #   include <ieeefp.h>
329 #endif
330 #if defined (Q_OS_OSF) && (defined(__DECC) || defined(__DECCXX))
331 #   define INFINITY DBL_INFINITY
332 #   define NAN DBL_QNAN
333 #endif
334 #if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
335 #   include <ieeefp.h>
336 #   define isnan(d) isnand(d)
337 #endif
338
339 enum {
340     LittleEndian,
341     BigEndian
342 #ifdef Q_BYTE_ORDER
343 #  if Q_BYTE_ORDER == Q_BIG_ENDIAN
344     , ByteOrder = BigEndian
345 #  elif Q_BYTE_ORDER == Q_LITTLE_ENDIAN
346     , ByteOrder = LittleEndian
347 #  else
348 #    error "undefined byte order"
349 #  endif
350 };
351 #else
352 };
353 static const unsigned int one = 1;
354 static const bool ByteOrder = ((*((unsigned char *) &one) == 0) ? BigEndian : LittleEndian);
355 #endif
356 #if !defined(INFINITY)
357 static const unsigned char be_inf_bytes[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0,0 };
358 static const unsigned char le_inf_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f };
359 static inline double inf()
360 {
361     if (ByteOrder == BigEndian)
362         return *reinterpret_cast<const double *>(be_inf_bytes);
363     return *reinterpret_cast<const double *>(le_inf_bytes);
364 }
365 #   define INFINITY (::inf())
366 #endif
367 #if !defined(NAN)
368 static const unsigned char be_nan_bytes[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0,0 };
369 static const unsigned char le_nan_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f };
370 static inline double nan()
371 {
372     if (ByteOrder == BigEndian)
373         return *reinterpret_cast<const double *>(be_nan_bytes);
374     return *reinterpret_cast<const double *>(le_nan_bytes);
375 }
376 #   define NAN (::nan())
377 #endif
378
379 tst_QString::tst_QString()
380 {
381     QTextCodec::setCodecForLocale(QTextCodec::codecForName("ISO 8859-1"));
382 }
383
384 void tst_QString::cleanup()
385 {
386     QLocale::setDefault(QString("C"));
387 }
388
389 void tst_QString::remove_uint_uint_data()
390 {
391     replace_uint_uint_data();
392 }
393
394 void tst_QString::remove_string_data()
395 {
396     replace_string_data();
397 }
398
399 void tst_QString::remove_regexp_data()
400 {
401     replace_regexp_data();
402 }
403
404 void tst_QString::indexOf3_data()
405 {
406     indexOf2_data();
407 }
408
409 void tst_QString::length_data()
410 {
411     QTest::addColumn<QString>("s1" );
412     QTest::addColumn<int>("res" );
413
414     QTest::newRow( "data0" )  << QString("Test") << 4;
415     QTest::newRow( "data1" )  << QString("The quick brown fox jumps over the lazy dog") << 43;
416     QTest::newRow( "data2" )  << QString() << 0;
417     QTest::newRow( "data3" )  << QString("A") << 1;
418     QTest::newRow( "data4" )  << QString("AB") << 2;
419     QTest::newRow( "data5" )  << QString("AB\n") << 3;
420     QTest::newRow( "data6" )  << QString("AB\nC") << 4;
421     QTest::newRow( "data7" )  << QString("\n") << 1;
422     QTest::newRow( "data8" )  << QString("\nA") << 2;
423     QTest::newRow( "data9" )  << QString("\nAB") << 3;
424     QTest::newRow( "data10" )  << QString("\nAB\nCDE") << 7;
425     QTest::newRow( "data11" )  << QString("shdnftrheid fhgnt gjvnfmd chfugkh bnfhg thgjf vnghturkf chfnguh bjgnfhvygh hnbhgutjfv dhdnjds dcjs d") << 100;
426 }
427
428 void tst_QString::replace_qchar_qchar_data()
429 {
430     QTest::addColumn<QString>("src" );
431     QTest::addColumn<QChar>("before" );
432     QTest::addColumn<QChar>("after" );
433     QTest::addColumn<int>("cs" );
434     QTest::addColumn<QString>("expected" );
435
436     QTest::newRow( "1" ) << QString("foo") << QChar('o') << QChar('a')
437                       << int(Qt::CaseSensitive) << QString("faa");
438     QTest::newRow( "2" ) << QString("foo") << QChar('o') << QChar('a')
439                       << int(Qt::CaseInsensitive) << QString("faa");
440     QTest::newRow( "3" ) << QString("foo") << QChar('O') << QChar('a')
441                       << int(Qt::CaseSensitive) << QString("foo");
442     QTest::newRow( "4" ) << QString("foo") << QChar('O') << QChar('a')
443                       << int(Qt::CaseInsensitive) << QString("faa");
444     QTest::newRow( "5" ) << QString("ababABAB") << QChar('a') << QChar(' ')
445                       << int(Qt::CaseSensitive) << QString(" b bABAB");
446     QTest::newRow( "6" ) << QString("ababABAB") << QChar('a') << QChar(' ')
447                       << int(Qt::CaseInsensitive) << QString(" b b B B");
448     QTest::newRow( "7" ) << QString("ababABAB") << QChar() << QChar(' ')
449                       << int(Qt::CaseInsensitive) << QString("ababABAB");
450 }
451
452 void tst_QString::replace_qchar_qchar()
453 {
454     QFETCH(QString, src);
455     QFETCH(QChar, before);
456     QFETCH(QChar, after);
457     QFETCH(int, cs);
458     QFETCH(QString, expected);
459
460     QCOMPARE(src.replace(before, after, Qt::CaseSensitivity(cs)), expected);
461 }
462
463 void tst_QString::replace_qchar_qstring_data()
464 {
465     QTest::addColumn<QString>("src" );
466     QTest::addColumn<QChar>("before" );
467     QTest::addColumn<QString>("after" );
468     QTest::addColumn<int>("cs" );
469     QTest::addColumn<QString>("expected" );
470
471     QTest::newRow( "1" ) << QString("foo") << QChar('o') << QString("aA")
472                       << int(Qt::CaseSensitive) << QString("faAaA");
473     QTest::newRow( "2" ) << QString("foo") << QChar('o') << QString("aA")
474                       << int(Qt::CaseInsensitive) << QString("faAaA");
475     QTest::newRow( "3" ) << QString("foo") << QChar('O') << QString("aA")
476                       << int(Qt::CaseSensitive) << QString("foo");
477     QTest::newRow( "4" ) << QString("foo") << QChar('O') << QString("aA")
478                       << int(Qt::CaseInsensitive) << QString("faAaA");
479     QTest::newRow( "5" ) << QString("ababABAB") << QChar('a') << QString("  ")
480                       << int(Qt::CaseSensitive) << QString("  b  bABAB");
481     QTest::newRow( "6" ) << QString("ababABAB") << QChar('a') << QString("  ")
482                       << int(Qt::CaseInsensitive) << QString("  b  b  B  B");
483     QTest::newRow( "7" ) << QString("ababABAB") << QChar() << QString("  ")
484                       << int(Qt::CaseInsensitive) << QString("ababABAB");
485     QTest::newRow( "8" ) << QString("ababABAB") << QChar() << QString()
486                       << int(Qt::CaseInsensitive) << QString("ababABAB");
487 }
488
489 void tst_QString::replace_qchar_qstring()
490 {
491     QFETCH(QString, src);
492     QFETCH(QChar, before);
493     QFETCH(QString, after);
494     QFETCH(int, cs);
495     QFETCH(QString, expected);
496
497     QCOMPARE(src.replace(before, after, Qt::CaseSensitivity(cs)), expected);
498 }
499
500 void tst_QString::replace_uint_uint_data()
501 {
502     QTest::addColumn<QString>("string" );
503     QTest::addColumn<int>("index" );
504     QTest::addColumn<int>("len" );
505     QTest::addColumn<QString>("after" );
506     QTest::addColumn<QString>("result" );
507
508     QTest::newRow( "rem00" ) << QString("-<>ABCABCABCABC>") << 0 << 3 << QString("") << QString("ABCABCABCABC>");
509     QTest::newRow( "rem01" ) << QString("ABCABCABCABC>") << 1 << 4 << QString("") << QString("ACABCABC>");
510     QTest::newRow( "rem04" ) << QString("ACABCABC>") << 8 << 4 << QString("") << QString("ACABCABC");
511     QTest::newRow( "rem05" ) << QString("ACABCABC") << 7 << 1 << QString("") << QString("ACABCAB");
512     QTest::newRow( "rem06" ) << QString("ACABCAB") << 4 << 0 << QString("") << QString("ACABCAB");
513
514     QTest::newRow( "rep00" ) << QString("ACABCAB") << 4 << 0 << QString("X") << QString("ACABXCAB");
515     QTest::newRow( "rep01" ) << QString("ACABXCAB") << 4 << 1 << QString("Y") << QString("ACABYCAB");
516     QTest::newRow( "rep02" ) << QString("ACABYCAB") << 4 << 1 << QString("") << QString("ACABCAB");
517     QTest::newRow( "rep03" ) << QString("ACABCAB") << 0 << 9999 << QString("XX") << QString("XX");
518     QTest::newRow( "rep04" ) << QString("XX") << 0 << 9999 << QString("") << QString("");
519     QTest::newRow( "rep05" ) << QString("ACABCAB") << 0 << 2 << QString("XX") << QString("XXABCAB");
520     QTest::newRow( "rep06" ) << QString("ACABCAB") << 1 << 2 << QString("XX") << QString("AXXBCAB");
521     QTest::newRow( "rep07" ) << QString("ACABCAB") << 2 << 2 << QString("XX") << QString("ACXXCAB");
522     QTest::newRow( "rep08" ) << QString("ACABCAB") << 3 << 2 << QString("XX") << QString("ACAXXAB");
523     QTest::newRow( "rep09" ) << QString("ACABCAB") << 4 << 2 << QString("XX") << QString("ACABXXB");
524     QTest::newRow( "rep10" ) << QString("ACABCAB") << 5 << 2 << QString("XX") << QString("ACABCXX");
525     QTest::newRow( "rep11" ) << QString("ACABCAB") << 6 << 2 << QString("XX") << QString("ACABCAXX");
526     QTest::newRow( "rep12" ) << QString() << 0 << 10 << QString("X") << QString("X");
527     QTest::newRow( "rep13" ) << QString("short") << 0 << 10 << QString("X") << QString("X");
528     QTest::newRow( "rep14" ) << QString() << 0 << 10 << QString("XX") << QString("XX");
529     QTest::newRow( "rep15" ) << QString("short") << 0 << 10 << QString("XX") << QString("XX");
530
531     // This is a regression test for an old bug where QString would add index and len parameters,
532     // potentially causing integer overflow.
533     QTest::newRow( "no overflow" ) << QString("ACABCAB") << 1 << INT_MAX - 1 << QString("") << QString("A");
534     QTest::newRow( "overflow" ) << QString("ACABCAB") << 1 << INT_MAX << QString("") << QString("A");
535 }
536
537 void tst_QString::replace_string_data()
538 {
539     QTest::addColumn<QString>("string" );
540     QTest::addColumn<QString>("before" );
541     QTest::addColumn<QString>("after" );
542     QTest::addColumn<QString>("result" );
543     QTest::addColumn<bool>("bcs" );
544
545     QTest::newRow( "rem00" ) << QString("") << QString("") << QString("") << QString("") << true;
546     QTest::newRow( "rem01" ) << QString("A") << QString("") << QString("") << QString("A") << true;
547     QTest::newRow( "rem02" ) << QString("A") << QString("A") << QString("") << QString("") << true;
548     QTest::newRow( "rem03" ) << QString("A") << QString("B") << QString("") << QString("A") << true;
549     QTest::newRow( "rem04" ) << QString("AA") << QString("A") << QString("") << QString("") << true;
550     QTest::newRow( "rem05" ) << QString("AB") << QString("A") << QString("") << QString("B") << true;
551     QTest::newRow( "rem06" ) << QString("AB") << QString("B") << QString("") << QString("A") << true;
552     QTest::newRow( "rem07" ) << QString("AB") << QString("C") << QString("") << QString("AB") << true;
553     QTest::newRow( "rem08" ) << QString("ABA") << QString("A") << QString("") << QString("B") << true;
554     QTest::newRow( "rem09" ) << QString("ABA") << QString("B") << QString("") << QString("AA") << true;
555     QTest::newRow( "rem10" ) << QString("ABA") << QString("C") << QString("") << QString("ABA") << true;
556     QTest::newRow( "rem11" ) << QString("banana") << QString("an") << QString("") << QString("ba") << true;
557     QTest::newRow( "rem12" ) << QString("") << QString("A") << QString("") << QString("") << true;
558     QTest::newRow( "rem13" ) << QString("") << QString("A") << QString() << QString("") << true;
559     QTest::newRow( "rem14" ) << QString() << QString("A") << QString("") << QString() << true;
560     QTest::newRow( "rem15" ) << QString() << QString("A") << QString() << QString() << true;
561     QTest::newRow( "rem16" ) << QString() << QString("") << QString("") << QString("") << true;
562     QTest::newRow( "rem17" ) << QString("") << QString() << QString("") << QString("") << true;
563     QTest::newRow( "rem18" ) << QString("a") << QString("a") << QString("") << QString("") << false;
564     QTest::newRow( "rem19" ) << QString("A") << QString("A") << QString("") << QString("") << false;
565     QTest::newRow( "rem20" ) << QString("a") << QString("A") << QString("") << QString("") << false;
566     QTest::newRow( "rem21" ) << QString("A") << QString("a") << QString("") << QString("") << false;
567     QTest::newRow( "rem22" ) << QString("Alpha beta") << QString("a") << QString("") << QString("lph bet") << false;
568
569     QTest::newRow( "rep00" ) << QString("ABC") << QString("B") << QString("-") << QString("A-C") << true;
570     QTest::newRow( "rep01" ) << QString("$()*+.?[\\]^{|}") << QString("$()*+.?[\\]^{|}") << QString("X") << QString("X") << true;
571     QTest::newRow( "rep02" ) << QString("ABCDEF") << QString("") << QString("X") << QString("XAXBXCXDXEXFX") << true;
572     QTest::newRow( "rep03" ) << QString("") << QString("") << QString("X") << QString("X") << true;
573     QTest::newRow( "rep04" ) << QString("a") << QString("a") << QString("b") << QString("b") << false;
574     QTest::newRow( "rep05" ) << QString("A") << QString("A") << QString("b") << QString("b") << false;
575     QTest::newRow( "rep06" ) << QString("a") << QString("A") << QString("b") << QString("b") << false;
576     QTest::newRow( "rep07" ) << QString("A") << QString("a") << QString("b") << QString("b") << false;
577     QTest::newRow( "rep08" ) << QString("a") << QString("a") << QString("a") << QString("a") << false;
578     QTest::newRow( "rep09" ) << QString("A") << QString("A") << QString("a") << QString("a") << false;
579     QTest::newRow( "rep10" ) << QString("a") << QString("A") << QString("a") << QString("a") << false;
580     QTest::newRow( "rep11" ) << QString("A") << QString("a") << QString("a") << QString("a") << false;
581     QTest::newRow( "rep12" ) << QString("Alpha beta") << QString("a") << QString("o") << QString("olpho beto") << false;
582     QTest::newRow( "rep13" ) << QString() << QString("") << QString("A") << QString("A") << true;
583     QTest::newRow( "rep14" ) << QString("") << QString() << QString("A") << QString("A") << true;
584     QTest::newRow( "rep15" ) << QString("fooxbarxbazxblub") << QString("x") << QString("yz") << QString("fooyzbaryzbazyzblub") << true;
585     QTest::newRow( "rep16" ) << QString("fooxbarxbazxblub") << QString("x") << QString("z") << QString("foozbarzbazzblub") << true;
586     QTest::newRow( "rep17" ) << QString("fooxybarxybazxyblub") << QString("xy") << QString("z") << QString("foozbarzbazzblub") << true;
587 }
588
589 void tst_QString::replace_regexp_data()
590 {
591     QTest::addColumn<QString>("string" );
592     QTest::addColumn<QString>("regexp" );
593     QTest::addColumn<QString>("after" );
594     QTest::addColumn<QString>("result" );
595
596     QTest::newRow( "rem00" ) << QString("alpha") << QString("a+") << QString("") << QString("lph");
597     QTest::newRow( "rem01" ) << QString("banana") << QString("^.a") << QString("") << QString("nana");
598     QTest::newRow( "rem02" ) << QString("") << QString("^.a") << QString("") << QString("");
599     QTest::newRow( "rem03" ) << QString("") << QString("^.a") << QString() << QString("");
600     QTest::newRow( "rem04" ) << QString() << QString("^.a") << QString("") << QString();
601     QTest::newRow( "rem05" ) << QString() << QString("^.a") << QString() << QString();
602
603     QTest::newRow( "rep00" ) << QString("A <i>bon mot</i>.") << QString("<i>([^<]*)</i>") << QString("\\emph{\\1}") << QString("A \\emph{bon mot}.");
604     QTest::newRow( "rep01" ) << QString("banana") << QString("^.a()") << QString("\\1") << QString("nana");
605     QTest::newRow( "rep02" ) << QString("banana") << QString("(ba)") << QString("\\1X\\1") << QString("baXbanana");
606     QTest::newRow( "rep03" ) << QString("banana") << QString("(ba)(na)na") << QString("\\2X\\1") << QString("naXba");
607
608     QTest::newRow("backref00") << QString("\\1\\2\\3\\4\\5\\6\\7\\8\\9\\A\\10\\11") << QString("\\\\[34]")
609                                << QString("X") << QString("\\1\\2XX\\5\\6\\7\\8\\9\\A\\10\\11");
610     QTest::newRow("backref01") << QString("foo") << QString("[fo]") << QString("\\1") << QString("\\1\\1\\1");
611     QTest::newRow("backref02") << QString("foo") << QString("([fo])") << QString("(\\1)") << QString("(f)(o)(o)");
612     QTest::newRow("backref03") << QString("foo") << QString("([fo])") << QString("\\2") << QString("\\2\\2\\2");
613     QTest::newRow("backref04") << QString("foo") << QString("([fo])") << QString("\\10") << QString("f0o0o0");
614     QTest::newRow("backref05") << QString("foo") << QString("([fo])") << QString("\\11") << QString("f1o1o1");
615     QTest::newRow("backref06") << QString("foo") << QString("([fo])") << QString("\\19") << QString("f9o9o9");
616     QTest::newRow("backref07") << QString("foo") << QString("(f)(o+)")
617                                << QString("\\2\\1\\10\\20\\11\\22\\19\\29\\3")
618                                << QString("ooff0oo0f1oo2f9oo9\\3");
619     QTest::newRow("backref08") << QString("abc") << QString("(((((((((((((([abc]))))))))))))))")
620                                << QString("{\\14}") << QString("{a}{b}{c}");
621     QTest::newRow("backref09") << QString("abcdefghijklmn")
622                                << QString("(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)")
623                                << QString("\\19\\18\\17\\16\\15\\14\\13\\12\\11\\10"
624                                           "\\9\\90\\8\\80\\7\\70\\6\\60\\5\\50\\4\\40\\3\\30\\2\\20\\1")
625                                << QString("a9a8a7a6a5nmlkjii0hh0gg0ff0ee0dd0cc0bb0a");
626     QTest::newRow("backref10") << QString("abc") << QString("((((((((((((((abc))))))))))))))")
627                                << QString("\\0\\01\\011") << QString("\\0\\01\\011");
628 }
629
630 void tst_QString::utf8_data()
631 {
632     QString str;
633     QTest::addColumn<QByteArray>("utf8" );
634     QTest::addColumn<QString>("res" );
635
636     QTest::newRow( "str0" ) << QByteArray("abcdefgh")
637                           << QString("abcdefgh");
638     QTest::newRow( "str1" ) << QByteArray("\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205")
639                           << QString::fromLatin1("\366\344\374\326\304\334\370\346\345\330\306\305") ;
640     str += QChar( 0x05e9 );
641     str += QChar( 0x05d3 );
642     str += QChar( 0x05d2 );
643     QTest::newRow( "str2" ) << QByteArray("\327\251\327\223\327\222")
644                           << str;
645
646     str = QChar( 0x20ac );
647     str += " some text";
648     QTest::newRow( "str3" ) << QByteArray("\342\202\254 some text")
649                           << str;
650
651     str = "Old Italic: ";
652     str += QChar(0xd800);
653     str += QChar(0xdf00);
654     str += QChar(0xd800);
655     str += QChar(0xdf01);
656     str += QChar(0xd800);
657     str += QChar(0xdf02);
658     str += QChar(0xd800);
659     str += QChar(0xdf03);
660     str += QChar(0xd800);
661     str += QChar(0xdf04);
662     QTest::newRow("surrogate") << QByteArray("Old Italic: \360\220\214\200\360\220\214\201\360\220\214\202\360\220\214\203\360\220\214\204") << str;
663 }
664
665 void tst_QString::length()
666 {
667     QFETCH( QString, s1 );
668     QTEST( (int)s1.length(), "res" );
669 }
670
671 #include <qfile.h>
672
673 void tst_QString::acc_01()
674 {
675     QString a;
676     QString b; //b(10);
677     QString bb; //bb((int)0);
678     QString c("String C");
679     QChar tmp[10];
680     tmp[0] = 'S';
681     tmp[1] = 't';
682     tmp[2] = 'r';
683     tmp[3] = 'i';
684     tmp[4] = 'n';
685     tmp[5] = 'g';
686     tmp[6] = ' ';
687     tmp[7] = 'D';
688     tmp[8] = 'X';
689     tmp[9] = '\0';
690     QString d(tmp,8);
691     QString ca(a);
692     QString cb(b);
693     QString cc(c);
694     QString n;
695     QString e("String E");
696     QString f;
697     f = e;
698     f[7]='F';
699     QCOMPARE(e,(QString)"String E");
700     char text[]="String f";
701     f = text;
702     text[7]='!';
703     QCOMPARE(f,(QString)"String f");
704     f[7]='F';
705     QCOMPARE(text[7],'!');
706
707     a="";
708     a[0]='A';
709     QCOMPARE(a,(QString)"A");
710     QCOMPARE(a.length(),1);
711     a[1]='B';
712     QCOMPARE(a,(QString)"AB");
713     QCOMPARE(a.length(),2);
714     a[2]='C';
715     QCOMPARE(a,(QString)"ABC");
716     QCOMPARE(a.length(),3);
717     a = QString();
718     QVERIFY(a.isNull());
719     a[0]='A';
720     QCOMPARE(a,(QString)"A");
721     QCOMPARE(a.length(),1);
722     a[1]='B';
723     QCOMPARE(a,(QString)"AB");
724     QCOMPARE(a.length(),2);
725     a[2]='C';
726     QCOMPARE(a,(QString)"ABC");
727     QCOMPARE(a.length(),3);
728
729     a="123";
730     b="456";
731     a[0]=a[1];
732     QCOMPARE(a,(QString)"223");
733     a[1]=b[1];
734     QCOMPARE(b,(QString)"456");
735     QCOMPARE(a,(QString)"253");
736
737     char t[]="TEXT";
738     a="A";
739     a=t;
740     QCOMPARE(a,(QString)"TEXT");
741     QCOMPARE(a,(QString)t);
742     a[0]='X';
743     QCOMPARE(a,(QString)"XEXT");
744     QCOMPARE(t[0],'T');
745     t[0]='Z';
746     QCOMPARE(a,(QString)"XEXT");
747
748     a="ABC";
749     QCOMPARE(char(a.toLatin1()[1]),'B');
750     QCOMPARE(strcmp(a.toLatin1(),((QString)"ABC").toLatin1()),0);
751     QCOMPARE(a+="DEF",(QString)"ABCDEF");
752     QCOMPARE(a+='G',(QString)"ABCDEFG");
753     QCOMPARE(a+=((const char*)(0)),(QString)"ABCDEFG");
754
755     // non-member operators
756
757     a="ABC";
758     b="ABC";
759     c="ACB";
760     d="ABCD";
761     QVERIFY(a==b);
762     QVERIFY(!(a==d));
763     QVERIFY(!(a!=b));
764     QVERIFY(a!=d);
765     QVERIFY(!(a<b));
766     QVERIFY(a<c);
767     QVERIFY(a<d);
768     QVERIFY(!(d<a));
769     QVERIFY(!(c<a));
770     QVERIFY(a<=b);
771     QVERIFY(a<=d);
772     QVERIFY(a<=c);
773     QVERIFY(!(c<=a));
774     QVERIFY(!(d<=a));
775     QCOMPARE(QString(a+b),(QString)"ABCABC");
776     QCOMPARE(QString(a+"XXXX"),(QString)"ABCXXXX");
777     QCOMPARE(QString(a+'X'),(QString)"ABCX");
778     QCOMPARE(QString("XXXX"+a),(QString)"XXXXABC");
779     QCOMPARE(QString('X'+a),(QString)"XABC");
780     a = (const char*)0;
781     QVERIFY(a.isNull());
782     QVERIFY(*a.toLatin1().constData() == '\0');
783     {
784 #if defined(Q_OS_WINCE)
785         int argc = 0;
786         QCoreApplication app(argc, 0);
787 #endif
788         QFile f("COMPARE.txt");
789         f.open(QIODevice::ReadOnly);
790         QTextStream ts( &f );
791         ts.setCodec(QTextCodec::codecForName("UTF-16"));
792         ts << "Abc";
793     }
794 }
795
796 #ifdef Q_CC_GNU
797 #    pragma GCC diagnostic push
798 #    pragma GCC diagnostic ignored "-Wformat-security"
799 #endif
800
801 void tst_QString::isNull()
802 {
803     QString a;
804     QVERIFY(a.isNull());
805
806     const char *zero = 0;
807     a.sprintf( zero );
808     QVERIFY(!a.isNull());
809 }
810
811 #ifdef Q_CC_GNU
812 #    pragma GCC diagnostic pop
813 #endif
814
815 void tst_QString::isEmpty()
816 {
817     QString a;
818     QVERIFY(a.isEmpty());
819     QString c("Not empty");
820     QVERIFY(!c.isEmpty());
821 }
822
823 void tst_QString::constructor()
824 {
825     QString a;
826     QString b; //b(10);
827     QString c("String C");
828     QChar tmp[10];
829     tmp[0] = 'S';
830     tmp[1] = 't';
831     tmp[2] = 'r';
832     tmp[3] = 'i';
833     tmp[4] = 'n';
834     tmp[5] = 'g';
835     tmp[6] = ' ';
836     tmp[7] = 'D';
837     tmp[8] = 'X';
838     tmp[9] = '\0';
839     QString d(tmp,8);
840     QString ca(a);
841     QString cb(b);
842     QString cc(c);
843
844     QCOMPARE(a,ca);
845     QVERIFY(a.isNull());
846     QVERIFY(a == (QString)"");
847     QCOMPARE(b,cb);
848     QCOMPARE(c,cc);
849     QCOMPARE(d,(QString)"String D");
850
851     QString nullStr;
852     QVERIFY( nullStr.isNull() );
853     QVERIFY( nullStr.isEmpty() );
854     QString empty("");
855     QVERIFY( !empty.isNull() );
856     QVERIFY( empty.isEmpty() );
857 }
858
859 void tst_QString::constructorQByteArray_data()
860 {
861     QTest::addColumn<QByteArray>("src" );
862     QTest::addColumn<QString>("expected" );
863
864     QByteArray ba( 4, 0 );
865     ba[0] = 'C';
866     ba[1] = 'O';
867     ba[2] = 'M';
868     ba[3] = 'P';
869
870     QTest::newRow( "1" ) << ba << QString("COMP");
871
872     QByteArray ba1( 7, 0 );
873     ba1[0] = 'a';
874     ba1[1] = 'b';
875     ba1[2] = 'c';
876     ba1[3] = '\0';
877     ba1[4] = 'd';
878     ba1[5] = 'e';
879     ba1[6] = 'f';
880
881     QTest::newRow( "2" ) << ba1 << QString("abc");
882
883     QTest::newRow( "3" ) << QByteArray::fromRawData("abcd", 3) << QString("abc");
884     QTest::newRow( "4" ) << QByteArray("\xc3\xa9") << QString("\xc3\xa9");
885     QTest::newRow( "4-bis" ) << QByteArray("\xc3\xa9") << QString::fromUtf8("\xc3\xa9");
886     QTest::newRow( "4-tre" ) << QByteArray("\xc3\xa9") << QString::fromLatin1("\xe9");
887 }
888
889 void tst_QString::constructorQByteArray()
890 {
891     QFETCH(QByteArray, src);
892     QFETCH(QString, expected);
893
894     QString str1(src);
895     QCOMPARE(str1.length(), expected.length());
896     QCOMPARE( str1, expected );
897
898     QString strBA(src);
899     QCOMPARE( strBA, expected );
900
901     // test operator= too
902     if (src.constData()[src.length()] == '\0') {
903         str1.clear();
904         str1 = src.constData();
905         QCOMPARE( str1, expected );
906     }
907
908     strBA.clear();
909     strBA = src;
910     QCOMPARE( strBA, expected );
911 }
912
913 void tst_QString::STL()
914 {
915     std::string stdstr( "QString" );
916
917     QString stlqt = QString::fromStdString(stdstr);
918     QCOMPARE(stlqt, QString::fromLatin1(stdstr.c_str()));
919     QCOMPARE(stlqt.toStdString(), stdstr);
920
921     const wchar_t arr[] = {'h', 'e', 'l', 'l', 'o', 0};
922     std::wstring stlStr = arr;
923
924     QString s = QString::fromStdWString(stlStr);
925
926     QCOMPARE(s, QString::fromLatin1("hello"));
927     QCOMPARE(stlStr, s.toStdWString());
928 }
929
930 void tst_QString::truncate()
931 {
932     QString e("String E");
933     e.truncate(4);
934     QCOMPARE(e,(QString)"Stri");
935
936     e = "String E";
937     e.truncate(0);
938     QCOMPARE(e,(QString)"");
939     QVERIFY(e.isEmpty());
940     QVERIFY(!e.isNull());
941
942 }
943
944 void tst_QString::fill()
945 {
946     QString e;
947     e.fill('e',1);
948     QCOMPARE(e,(QString)"e");
949     QString f;
950     f.fill('f',3);
951     QCOMPARE(f,(QString)"fff");
952     f.fill('F');
953     QCOMPARE(f,(QString)"FFF");
954 }
955
956 void tst_QString::sprintf()
957 {
958     QString a;
959     a.sprintf("COMPARE");
960     QCOMPARE(a,(QString)"COMPARE");
961     a.sprintf("%%%d",1);
962     QCOMPARE(a,(QString)"%1");
963     QCOMPARE(a.sprintf("X%dY",2),(QString)"X2Y");
964     QCOMPARE(a.sprintf("X%9iY", 50000 ),(QString)"X    50000Y");
965     QCOMPARE(a.sprintf("X%-9sY","hello"),(QString)"Xhello    Y");
966     QCOMPARE(a.sprintf("X%-9iY", 50000 ),(QString)"X50000    Y");
967     QCOMPARE(a.sprintf("%lf", 1.23), QString("1.230000"));
968     QCOMPARE(a.sprintf("%lf", 1.23456789), QString("1.234568"));
969     QCOMPARE(a.sprintf("%p", (void *)0xbfffd350), QString("0xbfffd350"));
970     QCOMPARE(a.sprintf("%p", (void *)0), QString("0x0"));
971
972     int i = 6;
973     long l = -2;
974     float f = 4.023f;
975     QString S1;
976     S1.sprintf("%d %ld %f",i,l,f);
977     QCOMPARE(S1,QString("6 -2 4.023000"));
978
979     double d = -514.25683;
980     S1.sprintf("%f",d);
981     QCOMPARE(S1, QString("-514.256830"));
982
983     QCOMPARE(a.sprintf("%.3s", "Hello" ),(QString)"Hel");
984     QCOMPARE(a.sprintf("%10.3s", "Hello" ),(QString)"       Hel");
985     QCOMPARE(a.sprintf("%.10s", "Hello" ),(QString)"Hello");
986     QCOMPARE(a.sprintf("%10.10s", "Hello" ),(QString)"     Hello");
987     QCOMPARE(a.sprintf("%-10.10s", "Hello" ),(QString)"Hello     ");
988     QCOMPARE(a.sprintf("%-10.3s", "Hello" ),(QString)"Hel       ");
989     QCOMPARE(a.sprintf("%-5.5s", "Hello" ),(QString)"Hello");
990
991     // Check utf8 conversion for %s
992     QCOMPARE(a.sprintf("%s", "\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205"), QString::fromLatin1("\366\344\374\326\304\334\370\346\345\330\306\305"));
993
994     int n1;
995     a.sprintf("%s%n%s", "hello", &n1, "goodbye");
996     QCOMPARE(n1, 5);
997     QCOMPARE(a, QString("hellogoodbye"));
998     qlonglong n2;
999     a.sprintf("%s%s%lln%s", "foo", "bar", &n2, "whiz");
1000     QCOMPARE((int)n2, 6);
1001     QCOMPARE(a, QString("foobarwhiz"));
1002 }
1003
1004 /*
1005     indexOf() and indexOf02() test QString::indexOf(),
1006     QString::lastIndexOf(), and their QByteArray equivalents.
1007
1008     lastIndexOf() tests QString::lastIndexOf() more in depth, but it
1009     should probably be rewritten to use a data table.
1010 */
1011
1012 void tst_QString::indexOf_data()
1013 {
1014     QTest::addColumn<QString>("haystack" );
1015     QTest::addColumn<QString>("needle" );
1016     QTest::addColumn<int>("startpos" );
1017     QTest::addColumn<bool>("bcs" );
1018     QTest::addColumn<int>("resultpos" );
1019
1020     QTest::newRow( "data0" ) << QString("abc") << QString("a") << 0 << true << 0;
1021     QTest::newRow( "data1" ) << QString("abc") << QString("a") << 0 << false << 0;
1022     QTest::newRow( "data2" ) << QString("abc") << QString("A") << 0 << true << -1;
1023     QTest::newRow( "data3" ) << QString("abc") << QString("A") << 0 << false << 0;
1024     QTest::newRow( "data4" ) << QString("abc") << QString("a") << 1 << true << -1;
1025     QTest::newRow( "data5" ) << QString("abc") << QString("a") << 1 << false << -1;
1026     QTest::newRow( "data6" ) << QString("abc") << QString("A") << 1 << true << -1;
1027     QTest::newRow( "data7" ) << QString("abc") << QString("A") << 1 << false << -1;
1028     QTest::newRow( "data8" ) << QString("abc") << QString("b") << 0 << true << 1;
1029     QTest::newRow( "data9" ) << QString("abc") << QString("b") << 0 << false << 1;
1030     QTest::newRow( "data10" ) << QString("abc") << QString("B") << 0 << true << -1;
1031     QTest::newRow( "data11" ) << QString("abc") << QString("B") << 0 << false << 1;
1032     QTest::newRow( "data12" ) << QString("abc") << QString("b") << 1 << true << 1;
1033     QTest::newRow( "data13" ) << QString("abc") << QString("b") << 1 << false << 1;
1034     QTest::newRow( "data14" ) << QString("abc") << QString("B") << 1 << true << -1;
1035     QTest::newRow( "data15" ) << QString("abc") << QString("B") << 1 << false << 1;
1036     QTest::newRow( "data16" ) << QString("abc") << QString("b") << 2 << true << -1;
1037     QTest::newRow( "data17" ) << QString("abc") << QString("b") << 2 << false << -1;
1038
1039     QTest::newRow( "data20" ) << QString("ABC") << QString("A") << 0 << true << 0;
1040     QTest::newRow( "data21" ) << QString("ABC") << QString("A") << 0 << false << 0;
1041     QTest::newRow( "data22" ) << QString("ABC") << QString("a") << 0 << true << -1;
1042     QTest::newRow( "data23" ) << QString("ABC") << QString("a") << 0 << false << 0;
1043     QTest::newRow( "data24" ) << QString("ABC") << QString("A") << 1 << true << -1;
1044     QTest::newRow( "data25" ) << QString("ABC") << QString("A") << 1 << false << -1;
1045     QTest::newRow( "data26" ) << QString("ABC") << QString("a") << 1 << true << -1;
1046     QTest::newRow( "data27" ) << QString("ABC") << QString("a") << 1 << false << -1;
1047     QTest::newRow( "data28" ) << QString("ABC") << QString("B") << 0 << true << 1;
1048     QTest::newRow( "data29" ) << QString("ABC") << QString("B") << 0 << false << 1;
1049     QTest::newRow( "data30" ) << QString("ABC") << QString("b") << 0 << true << -1;
1050     QTest::newRow( "data31" ) << QString("ABC") << QString("b") << 0 << false << 1;
1051     QTest::newRow( "data32" ) << QString("ABC") << QString("B") << 1 << true << 1;
1052     QTest::newRow( "data33" ) << QString("ABC") << QString("B") << 1 << false << 1;
1053     QTest::newRow( "data34" ) << QString("ABC") << QString("b") << 1 << true << -1;
1054     QTest::newRow( "data35" ) << QString("ABC") << QString("b") << 1 << false << 1;
1055     QTest::newRow( "data36" ) << QString("ABC") << QString("B") << 2 << true << -1;
1056     QTest::newRow( "data37" ) << QString("ABC") << QString("B") << 2 << false << -1;
1057
1058     QTest::newRow( "data40" ) << QString("aBc") << QString("bc") << 0 << true << -1;
1059     QTest::newRow( "data41" ) << QString("aBc") << QString("Bc") << 0 << true << 1;
1060     QTest::newRow( "data42" ) << QString("aBc") << QString("bC") << 0 << true << -1;
1061     QTest::newRow( "data43" ) << QString("aBc") << QString("BC") << 0 << true << -1;
1062     QTest::newRow( "data44" ) << QString("aBc") << QString("bc") << 0 << false << 1;
1063     QTest::newRow( "data45" ) << QString("aBc") << QString("Bc") << 0 << false << 1;
1064     QTest::newRow( "data46" ) << QString("aBc") << QString("bC") << 0 << false << 1;
1065     QTest::newRow( "data47" ) << QString("aBc") << QString("BC") << 0 << false << 1;
1066     QTest::newRow( "data48" ) << QString("AbC") << QString("bc") << 0 << true << -1;
1067     QTest::newRow( "data49" ) << QString("AbC") << QString("Bc") << 0 << true << -1;
1068     QTest::newRow( "data50" ) << QString("AbC") << QString("bC") << 0 << true << 1;
1069     QTest::newRow( "data51" ) << QString("AbC") << QString("BC") << 0 << true << -1;
1070     QTest::newRow( "data52" ) << QString("AbC") << QString("bc") << 0 << false << 1;
1071     QTest::newRow( "data53" ) << QString("AbC") << QString("Bc") << 0 << false << 1;
1072
1073     QTest::newRow( "data54" ) << QString("AbC") << QString("bC") << 0 << false << 1;
1074     QTest::newRow( "data55" ) << QString("AbC") << QString("BC") << 0 << false << 1;
1075     QTest::newRow( "data56" ) << QString("AbC") << QString("BC") << 1 << false << 1;
1076     QTest::newRow( "data57" ) << QString("AbC") << QString("BC") << 2 << false << -1;
1077 #if 0
1078     QTest::newRow( "null-in-null") << QString() << QString() << 0 << false << 0;
1079     QTest::newRow( "empty-in-null") << QString() << QString("") << 0 << false << 0;
1080     QTest::newRow( "null-in-empty") << QString("") << QString() << 0 << false << 0;
1081     QTest::newRow( "empty-in-empty") << QString("") << QString("") << 0 << false << 0;
1082 #endif
1083
1084
1085     QString s1 = "abc";
1086     s1 += QChar(0xb5);
1087     QString s2;
1088     s2 += QChar(0x3bc);
1089     QTest::newRow( "data58" ) << s1 << s2 << 0 << false << 3;
1090     s2.prepend("C");
1091     QTest::newRow( "data59" ) << s1 << s2 << 0 << false << 2;
1092
1093     QString veryBigHaystack(500, 'a');
1094     veryBigHaystack += 'B';
1095     QTest::newRow("BoyerMooreStressTest") << veryBigHaystack << veryBigHaystack << 0 << true << 0;
1096     QTest::newRow("BoyerMooreStressTest2") << QString(veryBigHaystack + 'c') << veryBigHaystack << 0 << true << 0;
1097     QTest::newRow("BoyerMooreStressTest3") << QString('c' + veryBigHaystack) << veryBigHaystack << 0 << true << 1;
1098     QTest::newRow("BoyerMooreStressTest4") << veryBigHaystack << QString(veryBigHaystack + 'c') << 0 << true << -1;
1099     QTest::newRow("BoyerMooreStressTest5") << veryBigHaystack << QString('c' + veryBigHaystack) << 0 << true << -1;
1100     QTest::newRow("BoyerMooreStressTest6") << QString('d' + veryBigHaystack) << QString('c' + veryBigHaystack) << 0 << true << -1;
1101     QTest::newRow("BoyerMooreStressTest7") << QString(veryBigHaystack + 'c') << QString('c' + veryBigHaystack) << 0 << true << -1;
1102
1103     QTest::newRow("BoyerMooreInsensitiveStressTest") << veryBigHaystack << veryBigHaystack << 0 << false << 0;
1104
1105 }
1106
1107 void tst_QString::indexOf()
1108 {
1109     QFETCH( QString, haystack );
1110     QFETCH( QString, needle );
1111     QFETCH( int, startpos );
1112     QFETCH( bool, bcs );
1113     QFETCH( int, resultpos );
1114     CREATE_REF(needle);
1115
1116     Qt::CaseSensitivity cs = bcs ? Qt::CaseSensitive : Qt::CaseInsensitive;
1117
1118     bool needleIsLatin = (QString::fromLatin1(needle.toLatin1()) == needle);
1119
1120     QCOMPARE( haystack.indexOf(needle, startpos, cs), resultpos );
1121     QCOMPARE( haystack.indexOf(ref, startpos, cs), resultpos );
1122     if (needleIsLatin) {
1123         QCOMPARE( haystack.indexOf(needle.toLatin1(), startpos, cs), resultpos );
1124         QCOMPARE( haystack.indexOf(needle.toLatin1().data(), startpos, cs), resultpos );
1125     }
1126
1127     {
1128         QRegExp rx1 = QRegExp(QRegExp::escape(needle), cs);
1129         QRegExp rx2 = QRegExp(needle, cs, QRegExp::FixedString);
1130         QCOMPARE( haystack.indexOf(rx1, startpos), resultpos );
1131         QCOMPARE( haystack.indexOf(rx2, startpos), resultpos );
1132         // these QRegExp must have been modified
1133         QVERIFY( resultpos == -1 || rx1.matchedLength() > 0);
1134         QVERIFY( resultpos == -1 || rx2.matchedLength() > 0);
1135     }
1136
1137     {
1138         const QRegExp rx1 = QRegExp(QRegExp::escape(needle), cs);
1139         const QRegExp rx2 = QRegExp(needle, cs, QRegExp::FixedString);
1140         QCOMPARE( haystack.indexOf(rx1, startpos), resultpos );
1141         QCOMPARE( haystack.indexOf(rx2, startpos), resultpos );
1142         // our QRegExp mustn't have been modified
1143         QCOMPARE( rx1.matchedLength(), -1 );
1144         QCOMPARE( rx2.matchedLength(), -1 );
1145     }
1146
1147     {
1148         QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption;
1149         if (!bcs)
1150             options |= QRegularExpression::CaseInsensitiveOption;
1151
1152         QRegularExpression re(QRegularExpression::escape(needle), options);
1153         QEXPECT_FAIL("data58", "QRegularExpression does not support case folding", Continue);
1154         QEXPECT_FAIL("data59", "QRegularExpression does not support case folding", Continue);
1155         QCOMPARE( haystack.indexOf(re, startpos), resultpos );
1156     }
1157
1158     if (cs == Qt::CaseSensitive) {
1159         QCOMPARE( haystack.indexOf(needle, startpos), resultpos );
1160         QCOMPARE( haystack.indexOf(ref, startpos), resultpos );
1161         if (needleIsLatin) {
1162             QCOMPARE( haystack.indexOf(needle.toLatin1(), startpos), resultpos );
1163             QCOMPARE( haystack.indexOf(needle.toLatin1().data(), startpos), resultpos );
1164         }
1165         if (startpos == 0) {
1166             QCOMPARE( haystack.indexOf(needle), resultpos );
1167             QCOMPARE( haystack.indexOf(ref), resultpos );
1168             if (needleIsLatin) {
1169                 QCOMPARE( haystack.indexOf(needle.toLatin1()), resultpos );
1170                 QCOMPARE( haystack.indexOf(needle.toLatin1().data()), resultpos );
1171             }
1172         }
1173     }
1174     if (needle.size() == 1) {
1175         QCOMPARE(haystack.indexOf(needle.at(0), startpos, cs), resultpos);
1176         QCOMPARE(haystack.indexOf(ref.at(0), startpos, cs), resultpos);
1177     }
1178
1179 }
1180
1181 void tst_QString::indexOf2_data()
1182 {
1183     QTest::addColumn<QString>("haystack" );
1184     QTest::addColumn<QString>("needle" );
1185     QTest::addColumn<int>("resultpos" );
1186
1187     QTest::newRow( "data0" ) << QString() << QString() << 0;
1188     QTest::newRow( "data1" ) << QString() << QString("") << 0;
1189     QTest::newRow( "data2" ) << QString("") << QString() << 0;
1190     QTest::newRow( "data3" ) << QString("") << QString("") << 0;
1191     QTest::newRow( "data4" ) << QString() << QString("a") << -1;
1192     QTest::newRow( "data5" ) << QString() << QString("abcdefg") << -1;
1193     QTest::newRow( "data6" ) << QString("") << QString("a") << -1;
1194     QTest::newRow( "data7" ) << QString("") << QString("abcdefg") << -1;
1195
1196     QTest::newRow( "data8" ) << QString("a") << QString() << 0;
1197     QTest::newRow( "data9" ) << QString("a") << QString("") << 0;
1198     QTest::newRow( "data10" ) << QString("a") << QString("a") << 0;
1199     QTest::newRow( "data11" ) << QString("a") << QString("b") << -1;
1200     QTest::newRow( "data12" ) << QString("a") << QString("abcdefg") << -1;
1201     QTest::newRow( "data13" ) << QString("ab") << QString() << 0;
1202     QTest::newRow( "data14" ) << QString("ab") << QString("") << 0;
1203     QTest::newRow( "data15" ) << QString("ab") << QString("a") << 0;
1204     QTest::newRow( "data16" ) << QString("ab") << QString("b") << 1;
1205     QTest::newRow( "data17" ) << QString("ab") << QString("ab") << 0;
1206     QTest::newRow( "data18" ) << QString("ab") << QString("bc") << -1;
1207     QTest::newRow( "data19" ) << QString("ab") << QString("abcdefg") << -1;
1208
1209     QTest::newRow( "data30" ) << QString("abc") << QString("a") << 0;
1210     QTest::newRow( "data31" ) << QString("abc") << QString("b") << 1;
1211     QTest::newRow( "data32" ) << QString("abc") << QString("c") << 2;
1212     QTest::newRow( "data33" ) << QString("abc") << QString("d") << -1;
1213     QTest::newRow( "data34" ) << QString("abc") << QString("ab") << 0;
1214     QTest::newRow( "data35" ) << QString("abc") << QString("bc") << 1;
1215     QTest::newRow( "data36" ) << QString("abc") << QString("cd") << -1;
1216     QTest::newRow( "data37" ) << QString("abc") << QString("ac") << -1;
1217
1218     // sizeof(whale) > 32
1219     QString whale = "a5zby6cx7dw8evf9ug0th1si2rj3qkp4lomn";
1220     QString minnow = "zby";
1221     QTest::newRow( "data40" ) << whale << minnow << 2;
1222     QTest::newRow( "data41" ) << QString(whale + whale) << minnow << 2;
1223     QTest::newRow( "data42" ) << QString(minnow + whale) << minnow << 0;
1224     QTest::newRow( "data43" ) << whale << whale << 0;
1225     QTest::newRow( "data44" ) << QString(whale + whale) << whale << 0;
1226     QTest::newRow( "data45" ) << whale << QString(whale + whale) << -1;
1227     QTest::newRow( "data46" ) << QString(whale + whale) << QString(whale + whale) << 0;
1228     QTest::newRow( "data47" ) << QString(whale + whale) << QString(whale + minnow) << -1;
1229     QTest::newRow( "data48" ) << QString(minnow + whale) << whale << (int)minnow.length();
1230 }
1231
1232 void tst_QString::indexOf2()
1233 {
1234     QFETCH( QString, haystack );
1235     QFETCH( QString, needle );
1236     QFETCH( int, resultpos );
1237     CREATE_REF(needle);
1238
1239     QByteArray chaystack = haystack.toLatin1();
1240     QByteArray cneedle = needle.toLatin1();
1241     int got;
1242
1243     QCOMPARE( haystack.indexOf(needle, 0, Qt::CaseSensitive), resultpos );
1244     QCOMPARE( haystack.indexOf(ref, 0, Qt::CaseSensitive), resultpos );
1245     QCOMPARE( QStringMatcher(needle, Qt::CaseSensitive).indexIn(haystack, 0), resultpos );
1246     QCOMPARE( haystack.indexOf(needle, 0, Qt::CaseInsensitive), resultpos );
1247     QCOMPARE( haystack.indexOf(ref, 0, Qt::CaseInsensitive), resultpos );
1248     QCOMPARE( QStringMatcher(needle, Qt::CaseInsensitive).indexIn(haystack, 0), resultpos );
1249     if ( needle.length() > 0 ) {
1250         got = haystack.lastIndexOf( needle, -1, Qt::CaseSensitive );
1251         QVERIFY( got == resultpos || (resultpos >= 0 && got >= resultpos) );
1252         got = haystack.lastIndexOf( needle, -1, Qt::CaseInsensitive );
1253         QVERIFY( got == resultpos || (resultpos >= 0 && got >= resultpos) );
1254     }
1255
1256     QCOMPARE( chaystack.indexOf(cneedle, 0), resultpos );
1257     QCOMPARE( QByteArrayMatcher(cneedle).indexIn(chaystack, 0), resultpos );
1258     if ( cneedle.length() > 0 ) {
1259         got = chaystack.lastIndexOf(cneedle, -1);
1260         QVERIFY( got == resultpos || (resultpos >= 0 && got >= resultpos) );
1261     }
1262 }
1263
1264 void tst_QString::lastIndexOf_data()
1265 {
1266     QTest::addColumn<QString>("haystack" );
1267     QTest::addColumn<QString>("needle" );
1268     QTest::addColumn<int>("from" );
1269     QTest::addColumn<int>("expected" );
1270     QTest::addColumn<bool>("caseSensitive" );
1271
1272     QString a = "ABCDEFGHIEfGEFG";
1273
1274     QTest::newRow("-1") << a << "G" << a.size() - 1 << 14 << true;
1275     QTest::newRow("1") << a << "G" << - 1 << 14 << true;
1276     QTest::newRow("2") << a << "G" << -3 << 11 << true;
1277     QTest::newRow("3") << a << "G" << -5 << 6 << true;
1278     QTest::newRow("4") << a << "G" << 14 << 14 << true;
1279     QTest::newRow("5") << a << "G" << 13 << 11 << true;
1280     QTest::newRow("6") << a << "B" << a.size() - 1 << 1 << true;
1281     QTest::newRow("7") << a << "B" << - 1 << 1 << true;
1282     QTest::newRow("8") << a << "B" << 1 << 1 << true;
1283     QTest::newRow("9") << a << "B" << 0 << -1 << true;
1284
1285     QTest::newRow("10") << a << "G" <<  -1 <<  a.size()-1 << true;
1286     QTest::newRow("11") << a << "G" <<  a.size()-1 <<  a.size()-1 << true;
1287     QTest::newRow("12") << a << "G" <<  a.size() <<  -1 << true;
1288     QTest::newRow("13") << a << "A" <<  0 <<  0 << true;
1289     QTest::newRow("14") << a << "A" <<  -1*a.size() <<  0 << true;
1290
1291     QTest::newRow("15") << a << "efg" << 0 << -1 << false;
1292     QTest::newRow("16") << a << "efg" << a.size() << -1 << false;
1293     QTest::newRow("17") << a << "efg" << -1 * a.size() << -1 << false;
1294     QTest::newRow("19") << a << "efg" << a.size() - 1 << 12 << false;
1295     QTest::newRow("20") << a << "efg" << 12 << 12 << false;
1296     QTest::newRow("21") << a << "efg" << -12 << -1 << false;
1297     QTest::newRow("22") << a << "efg" << 11 << 9 << false;
1298
1299     QTest::newRow("24") << "" << "asdf" << -1 << -1 << false;
1300     QTest::newRow("25") << "asd" << "asdf" << -1 << -1 << false;
1301     QTest::newRow("26") << "" << QString() << -1 << -1 << false;
1302
1303     QTest::newRow("27") << a << "" << a.size() << a.size() << false;
1304     QTest::newRow("28") << a << "" << a.size() + 10 << -1 << false;
1305 }
1306
1307 void tst_QString::lastIndexOf()
1308 {
1309     QFETCH(QString, haystack);
1310     QFETCH(QString, needle);
1311     QFETCH(int, from);
1312     QFETCH(int, expected);
1313     QFETCH(bool, caseSensitive);
1314     CREATE_REF(needle);
1315
1316     Qt::CaseSensitivity cs = (caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
1317
1318     QCOMPARE(haystack.lastIndexOf(needle, from, cs), expected);
1319     QCOMPARE(haystack.lastIndexOf(ref, from, cs), expected);
1320     QCOMPARE(haystack.lastIndexOf(needle.toLatin1(), from, cs), expected);
1321     QCOMPARE(haystack.lastIndexOf(needle.toLatin1().data(), from, cs), expected);
1322
1323     if (from >= -1 && from < haystack.size()) {
1324         // unfortunately, QString and QRegExp don't have the same out of bound semantics
1325         // I think QString is wrong -- See file log for contact information.
1326         {
1327             QRegExp rx1 = QRegExp(QRegExp::escape(needle), cs);
1328             QRegExp rx2 = QRegExp(needle, cs, QRegExp::FixedString);
1329             QCOMPARE(haystack.lastIndexOf(rx1, from), expected);
1330             QCOMPARE(haystack.lastIndexOf(rx2, from), expected);
1331             // our QRegExp mustn't have been modified
1332             QVERIFY(expected == -1 || rx1.matchedLength() > 0);
1333             QVERIFY(expected == -1 || rx2.matchedLength() > 0);
1334         }
1335
1336         {
1337             const QRegExp rx1 = QRegExp(QRegExp::escape(needle), cs);
1338             const QRegExp rx2 = QRegExp(needle, cs, QRegExp::FixedString);
1339             QCOMPARE(haystack.lastIndexOf(rx1, from), expected);
1340             QCOMPARE(haystack.lastIndexOf(rx2, from), expected);
1341             // our QRegExp mustn't have been modified
1342             QCOMPARE(rx1.matchedLength(), -1);
1343             QCOMPARE(rx2.matchedLength(), -1);
1344         }
1345
1346         {
1347             QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption;
1348             if (!caseSensitive)
1349                 options |= QRegularExpression::CaseInsensitiveOption;
1350
1351             QRegularExpression re(QRegularExpression::escape(needle), options);
1352             QCOMPARE(haystack.lastIndexOf(re, from), expected);
1353         }
1354     }
1355
1356     if (cs == Qt::CaseSensitive) {
1357         QCOMPARE(haystack.lastIndexOf(needle, from), expected);
1358         QCOMPARE(haystack.lastIndexOf(ref, from), expected);
1359         QCOMPARE(haystack.lastIndexOf(needle.toLatin1(), from), expected);
1360         QCOMPARE(haystack.lastIndexOf(needle.toLatin1().data(), from), expected);
1361         if (from == -1) {
1362             QCOMPARE(haystack.lastIndexOf(needle), expected);
1363             QCOMPARE(haystack.lastIndexOf(ref), expected);
1364             QCOMPARE(haystack.lastIndexOf(needle.toLatin1()), expected);
1365             QCOMPARE(haystack.lastIndexOf(needle.toLatin1().data()), expected);
1366         }
1367     }
1368     if (needle.size() == 1) {
1369         QCOMPARE(haystack.lastIndexOf(needle.at(0), from), expected);
1370         QCOMPARE(haystack.lastIndexOf(ref.at(0), from), expected);
1371     }
1372 }
1373
1374 void tst_QString::count()
1375 {
1376     QString a;
1377     a="ABCDEFGHIEfGEFG"; // 15 chars
1378     QCOMPARE(a.count('A'),1);
1379     QCOMPARE(a.count('Z'),0);
1380     QCOMPARE(a.count('E'),3);
1381     QCOMPARE(a.count('F'),2);
1382     QCOMPARE(a.count('F',Qt::CaseInsensitive),3);
1383     QCOMPARE(a.count("FG"),2);
1384     QCOMPARE(a.count("FG",Qt::CaseInsensitive),3);
1385     QCOMPARE(a.count( QString(), Qt::CaseInsensitive), 16);
1386     QCOMPARE(a.count( "", Qt::CaseInsensitive), 16);
1387     QCOMPARE(a.count(QRegExp("[FG][HI]")),1);
1388     QCOMPARE(a.count(QRegExp("[G][HE]")),2);
1389     QCOMPARE(a.count(QRegularExpression("[FG][HI]")), 1);
1390     QCOMPARE(a.count(QRegularExpression("[G][HE]")), 2);
1391
1392
1393     CREATE_REF(QLatin1String("FG"));
1394     QCOMPARE(a.count(ref),2);
1395     QCOMPARE(a.count(ref,Qt::CaseInsensitive),3);
1396     QCOMPARE(a.count( QStringRef(), Qt::CaseInsensitive), 16);
1397     QStringRef emptyRef(&a, 0, 0);
1398     QCOMPARE(a.count( emptyRef, Qt::CaseInsensitive), 16);
1399
1400 }
1401
1402 void tst_QString::contains()
1403 {
1404     QString a;
1405     a="ABCDEFGHIEfGEFG"; // 15 chars
1406     QVERIFY(a.contains('A'));
1407     QVERIFY(!a.contains('Z'));
1408     QVERIFY(a.contains('E'));
1409     QVERIFY(a.contains('F'));
1410     QVERIFY(a.contains('F',Qt::CaseInsensitive));
1411     QVERIFY(a.contains("FG"));
1412     QVERIFY(a.contains("FG",Qt::CaseInsensitive));
1413     QVERIFY(a.contains( QString(), Qt::CaseInsensitive));
1414     QVERIFY(a.contains( "", Qt::CaseInsensitive));
1415     QVERIFY(a.contains(QRegExp("[FG][HI]")));
1416     QVERIFY(a.contains(QRegExp("[G][HE]")));
1417     QVERIFY(a.contains(QRegularExpression("[FG][HI]")));
1418     QVERIFY(a.contains(QRegularExpression("[G][HE]")));
1419
1420     CREATE_REF(QLatin1String("FG"));
1421     QVERIFY(a.contains(ref));
1422     QVERIFY(a.contains(ref, Qt::CaseInsensitive));
1423     QVERIFY(a.contains( QStringRef(), Qt::CaseInsensitive));
1424     QStringRef emptyRef(&a, 0, 0);
1425     QVERIFY(a.contains(emptyRef, Qt::CaseInsensitive));
1426
1427 }
1428
1429
1430 void tst_QString::left()
1431 {
1432     QString a;
1433     a="ABCDEFGHIEfGEFG"; // 15 chars
1434     QCOMPARE(a.left(3),(QString)"ABC");
1435     QVERIFY(!a.left(0).isNull());
1436     QCOMPARE(a.left(0),(QString)"");
1437
1438     QString n;
1439     QVERIFY(n.left(3).isNull());
1440     QVERIFY(n.left(0).isNull());
1441     QVERIFY(n.left(0).isNull());
1442
1443     QString l = "Left";
1444     QCOMPARE(l.left(-1), l);
1445     QCOMPARE(l.left(100), l);
1446 }
1447
1448 void tst_QString::leftRef()
1449 {
1450     QString a;
1451     a="ABCDEFGHIEfGEFG"; // 15 chars
1452     QCOMPARE(a.leftRef(3).toString(),(QString)"ABC");
1453
1454     QVERIFY(a.leftRef(0).toString().isEmpty());
1455     QCOMPARE(a.leftRef(0).toString(),(QString)"");
1456
1457     QString n;
1458     QVERIFY(n.leftRef(3).toString().isEmpty());
1459     QVERIFY(n.leftRef(0).toString().isEmpty());
1460     QVERIFY(n.leftRef(0).toString().isEmpty());
1461
1462     QString l = "Left";
1463     QCOMPARE(l.leftRef(-1).toString(), l);
1464     QCOMPARE(l.leftRef(100).toString(), l);
1465 }
1466
1467 void tst_QString::right()
1468 {
1469     QString a;
1470     a="ABCDEFGHIEfGEFG"; // 15 chars
1471     QCOMPARE(a.right(3),(QString)"EFG");
1472     QCOMPARE(a.right(0),(QString)"");
1473
1474     QString n;
1475     QVERIFY(n.right(3).isNull());
1476     QVERIFY(n.right(0).isNull());
1477
1478     QString r = "Right";
1479     QCOMPARE(r.right(-1), r);
1480     QCOMPARE(r.right(100), r);
1481 }
1482
1483 void tst_QString::rightRef()
1484 {
1485     QString a;
1486     a="ABCDEFGHIEfGEFG"; // 15 chars
1487     QCOMPARE(a.rightRef(3).toString(),(QString)"EFG");
1488     QCOMPARE(a.rightRef(0).toString(),(QString)"");
1489
1490     QString n;
1491     QVERIFY(n.rightRef(3).toString().isEmpty());
1492     QVERIFY(n.rightRef(0).toString().isEmpty());
1493
1494     QString r = "Right";
1495     QCOMPARE(r.rightRef(-1).toString(), r);
1496     QCOMPARE(r.rightRef(100).toString(), r);
1497 }
1498
1499 void tst_QString::mid()
1500 {
1501     QString a;
1502     a="ABCDEFGHIEfGEFG"; // 15 chars
1503
1504     QCOMPARE(a.mid(3,3),(QString)"DEF");
1505     QCOMPARE(a.mid(0,0),(QString)"");
1506     QVERIFY(!a.mid(15,0).isNull());
1507     QVERIFY(a.mid(15,0).isEmpty());
1508     QVERIFY(!a.mid(15,1).isNull());
1509     QVERIFY(a.mid(15,1).isEmpty());
1510     QVERIFY(a.mid(9999).isNull());
1511     QVERIFY(a.mid(9999,1).isNull());
1512
1513     QCOMPARE(a.mid(-1, 6), a.mid(0, 5));
1514     QVERIFY(a.mid(-100, 6).isEmpty());
1515     QVERIFY(a.mid(INT_MIN, 0).isEmpty());
1516     QCOMPARE(a.mid(INT_MIN, -1), a);
1517     QVERIFY(a.mid(INT_MIN, INT_MAX).isNull());
1518     QVERIFY(a.mid(INT_MIN + 1, INT_MAX).isEmpty());
1519     QCOMPARE(a.mid(INT_MIN + 2, INT_MAX), a.left(1));
1520     QCOMPARE(a.mid(INT_MIN + a.size() + 1, INT_MAX), a);
1521     QVERIFY(a.mid(INT_MAX).isNull());
1522     QVERIFY(a.mid(INT_MAX, INT_MAX).isNull());
1523     QCOMPARE(a.mid(-5, INT_MAX), a);
1524     QCOMPARE(a.mid(-1, INT_MAX), a);
1525     QCOMPARE(a.mid(0, INT_MAX), a);
1526     QCOMPARE(a.mid(1, INT_MAX), QString("BCDEFGHIEfGEFG"));
1527     QCOMPARE(a.mid(5, INT_MAX), QString("FGHIEfGEFG"));
1528     QVERIFY(a.mid(20, INT_MAX).isNull());
1529     QCOMPARE(a.mid(-1, -1), a);
1530
1531     QString n;
1532     QVERIFY(n.mid(3,3).isNull());
1533     QVERIFY(n.mid(0,0).isNull());
1534     QVERIFY(n.mid(9999,0).isNull());
1535     QVERIFY(n.mid(9999,1).isNull());
1536
1537     QVERIFY(n.mid(-1, 6).isNull());
1538     QVERIFY(n.mid(-100, 6).isNull());
1539     QVERIFY(n.mid(INT_MIN, 0).isNull());
1540     QVERIFY(n.mid(INT_MIN, -1).isNull());
1541     QVERIFY(n.mid(INT_MIN, INT_MAX).isNull());
1542     QVERIFY(n.mid(INT_MIN + 1, INT_MAX).isNull());
1543     QVERIFY(n.mid(INT_MIN + 2, INT_MAX).isNull());
1544     QVERIFY(n.mid(INT_MIN + n.size() + 1, INT_MAX).isNull());
1545     QVERIFY(n.mid(INT_MAX).isNull());
1546     QVERIFY(n.mid(INT_MAX, INT_MAX).isNull());
1547     QVERIFY(n.mid(-5, INT_MAX).isNull());
1548     QVERIFY(n.mid(-1, INT_MAX).isNull());
1549     QVERIFY(n.mid(0, INT_MAX).isNull());
1550     QVERIFY(n.mid(1, INT_MAX).isNull());
1551     QVERIFY(n.mid(5, INT_MAX).isNull());
1552     QVERIFY(n.mid(20, INT_MAX).isNull());
1553     QVERIFY(n.mid(-1, -1).isNull());
1554
1555     QString x = "Nine pineapples";
1556     QCOMPARE(x.mid(5, 4), QString("pine"));
1557     QCOMPARE(x.mid(5), QString("pineapples"));
1558
1559     QCOMPARE(x.mid(-1, 6), x.mid(0, 5));
1560     QVERIFY(x.mid(-100, 6).isEmpty());
1561     QVERIFY(x.mid(INT_MIN, 0).isEmpty());
1562     QCOMPARE(x.mid(INT_MIN, -1), x);
1563     QVERIFY(x.mid(INT_MIN, INT_MAX).isNull());
1564     QVERIFY(x.mid(INT_MIN + 1, INT_MAX).isEmpty());
1565     QCOMPARE(x.mid(INT_MIN + 2, INT_MAX), x.left(1));
1566     QCOMPARE(x.mid(INT_MIN + x.size() + 1, INT_MAX), x);
1567     QVERIFY(x.mid(INT_MAX).isNull());
1568     QVERIFY(x.mid(INT_MAX, INT_MAX).isNull());
1569     QCOMPARE(x.mid(-5, INT_MAX), x);
1570     QCOMPARE(x.mid(-1, INT_MAX), x);
1571     QCOMPARE(x.mid(0, INT_MAX), x);
1572     QCOMPARE(x.mid(1, INT_MAX), QString("ine pineapples"));
1573     QCOMPARE(x.mid(5, INT_MAX), QString("pineapples"));
1574     QVERIFY(x.mid(20, INT_MAX).isNull());
1575     QCOMPARE(x.mid(-1, -1), x);
1576 }
1577
1578 void tst_QString::midRef()
1579 {
1580     QString a;
1581     a="ABCDEFGHIEfGEFG"; // 15 chars
1582
1583     QCOMPARE(a.midRef(3,3).toString(),(QString)"DEF");
1584     QCOMPARE(a.midRef(0,0).toString(),(QString)"");
1585     QVERIFY(!a.midRef(15,0).toString().isNull());
1586     QVERIFY(a.midRef(15,0).toString().isEmpty());
1587     QVERIFY(!a.midRef(15,1).toString().isNull());
1588     QVERIFY(a.midRef(15,1).toString().isEmpty());
1589     QVERIFY(a.midRef(9999).toString().isEmpty());
1590     QVERIFY(a.midRef(9999,1).toString().isEmpty());
1591
1592     QCOMPARE(a.midRef(-1, 6), a.midRef(0, 5));
1593     QVERIFY(a.midRef(-100, 6).isEmpty());
1594     QVERIFY(a.midRef(INT_MIN, 0).isEmpty());
1595     QCOMPARE(a.midRef(INT_MIN, -1).toString(), a);
1596     QVERIFY(a.midRef(INT_MIN, INT_MAX).isNull());
1597     QVERIFY(a.midRef(INT_MIN + 1, INT_MAX).isEmpty());
1598     QCOMPARE(a.midRef(INT_MIN + 2, INT_MAX), a.leftRef(1));
1599     QCOMPARE(a.midRef(INT_MIN + a.size() + 1, INT_MAX).toString(), a);
1600     QVERIFY(a.midRef(INT_MAX).isNull());
1601     QVERIFY(a.midRef(INT_MAX, INT_MAX).isNull());
1602     QCOMPARE(a.midRef(-5, INT_MAX).toString(), a);
1603     QCOMPARE(a.midRef(-1, INT_MAX).toString(), a);
1604     QCOMPARE(a.midRef(0, INT_MAX).toString(), a);
1605     QCOMPARE(a.midRef(1, INT_MAX).toString(), QString("BCDEFGHIEfGEFG"));
1606     QCOMPARE(a.midRef(5, INT_MAX).toString(), QString("FGHIEfGEFG"));
1607     QVERIFY(a.midRef(20, INT_MAX).isNull());
1608     QCOMPARE(a.midRef(-1, -1).toString(), a);
1609
1610     QString n;
1611     QVERIFY(n.midRef(3,3).toString().isEmpty());
1612     QVERIFY(n.midRef(0,0).toString().isEmpty());
1613     QVERIFY(n.midRef(9999,0).toString().isEmpty());
1614     QVERIFY(n.midRef(9999,1).toString().isEmpty());
1615
1616     QVERIFY(n.midRef(-1, 6).isNull());
1617     QVERIFY(n.midRef(-100, 6).isNull());
1618     QVERIFY(n.midRef(INT_MIN, 0).isNull());
1619     QVERIFY(n.midRef(INT_MIN, -1).isNull());
1620     QVERIFY(n.midRef(INT_MIN, INT_MAX).isNull());
1621     QVERIFY(n.midRef(INT_MIN + 1, INT_MAX).isNull());
1622     QVERIFY(n.midRef(INT_MIN + 2, INT_MAX).isNull());
1623     QVERIFY(n.midRef(INT_MIN + n.size() + 1, INT_MAX).isNull());
1624     QVERIFY(n.midRef(INT_MAX).isNull());
1625     QVERIFY(n.midRef(INT_MAX, INT_MAX).isNull());
1626     QVERIFY(n.midRef(-5, INT_MAX).isNull());
1627     QVERIFY(n.midRef(-1, INT_MAX).isNull());
1628     QVERIFY(n.midRef(0, INT_MAX).isNull());
1629     QVERIFY(n.midRef(1, INT_MAX).isNull());
1630     QVERIFY(n.midRef(5, INT_MAX).isNull());
1631     QVERIFY(n.midRef(20, INT_MAX).isNull());
1632     QVERIFY(n.midRef(-1, -1).isNull());
1633
1634     QString x = "Nine pineapples";
1635     QCOMPARE(x.midRef(5, 4).toString(), QString("pine"));
1636     QCOMPARE(x.midRef(5).toString(), QString("pineapples"));
1637
1638     QCOMPARE(x.midRef(-1, 6), x.midRef(0, 5));
1639     QVERIFY(x.midRef(-100, 6).isEmpty());
1640     QVERIFY(x.midRef(INT_MIN, 0).isEmpty());
1641     QCOMPARE(x.midRef(INT_MIN, -1).toString(), x);
1642     QVERIFY(x.midRef(INT_MIN, INT_MAX).isNull());
1643     QVERIFY(x.midRef(INT_MIN + 1, INT_MAX).isEmpty());
1644     QCOMPARE(x.midRef(INT_MIN + 2, INT_MAX), x.leftRef(1));
1645     QCOMPARE(x.midRef(INT_MIN + x.size() + 1, INT_MAX).toString(), x);
1646     QVERIFY(x.midRef(INT_MAX).isNull());
1647     QVERIFY(x.midRef(INT_MAX, INT_MAX).isNull());
1648     QCOMPARE(x.midRef(-5, INT_MAX).toString(), x);
1649     QCOMPARE(x.midRef(-1, INT_MAX).toString(), x);
1650     QCOMPARE(x.midRef(0, INT_MAX).toString(), x);
1651     QCOMPARE(x.midRef(1, INT_MAX).toString(), QString("ine pineapples"));
1652     QCOMPARE(x.midRef(5, INT_MAX).toString(), QString("pineapples"));
1653     QVERIFY(x.midRef(20, INT_MAX).isNull());
1654     QCOMPARE(x.midRef(-1, -1).toString(), x);
1655 }
1656
1657 void tst_QString::stringRef()
1658 {
1659     QString a;
1660     a="ABCDEFGHIEfGEFG"; // 15 chars
1661
1662     QVERIFY(QStringRef(&a, 0, 0) == (QString)"");
1663
1664     QVERIFY(QStringRef(&a, 3, 3) == (QString)"DEF");
1665     QVERIFY(QStringRef(&a, 3, 3) == QLatin1String("DEF"));
1666     QVERIFY(QStringRef(&a, 3, 3) == "DEF");
1667     QVERIFY((QString)"DEF" == QStringRef(&a, 3, 3));
1668     QVERIFY(QLatin1String("DEF") == QStringRef(&a, 3, 3));
1669     QVERIFY("DEF" == QStringRef(&a, 3, 3));
1670
1671     QVERIFY(QStringRef(&a, 3, 3) != (QString)"DE");
1672     QVERIFY(QStringRef(&a, 3, 3) != QLatin1String("DE"));
1673     QVERIFY(QStringRef(&a, 3, 3) != "DE");
1674     QVERIFY((QString)"DE" != QStringRef(&a, 3, 3));
1675     QVERIFY(QLatin1String("DE") != QStringRef(&a, 3, 3));
1676     QVERIFY("DE" != QStringRef(&a, 3, 3));
1677
1678     QString s_alpha("alpha");
1679     QString s_beta("beta");
1680     QStringRef alpha(&s_alpha);
1681     QStringRef beta(&s_beta);
1682
1683     QVERIFY(alpha < beta);
1684     QVERIFY(alpha <= beta);
1685     QVERIFY(alpha <= alpha);
1686     QVERIFY(beta > alpha);
1687     QVERIFY(beta >= alpha);
1688     QVERIFY(beta >= beta);
1689
1690     QString s_alpha2("alpha");
1691
1692     QMap<QStringRef, QString> map;
1693     map.insert(alpha, "alpha");
1694     map.insert(beta, "beta");
1695     QVERIFY(alpha == map.value(QStringRef(&s_alpha2)));
1696
1697     QHash<QStringRef, QString> hash;
1698     hash.insert(alpha, "alpha");
1699     hash.insert(beta, "beta");
1700
1701     QVERIFY(alpha == hash.value(QStringRef(&s_alpha2)));
1702 }
1703
1704 void tst_QString::leftJustified()
1705 {
1706     QString a;
1707     a="ABC";
1708     QCOMPARE(a.leftJustified(5,'-'),(QString)"ABC--");
1709     QCOMPARE(a.leftJustified(4,'-'),(QString)"ABC-");
1710     QCOMPARE(a.leftJustified(4),(QString)"ABC ");
1711     QCOMPARE(a.leftJustified(3),(QString)"ABC");
1712     QCOMPARE(a.leftJustified(2),(QString)"ABC");
1713     QCOMPARE(a.leftJustified(1),(QString)"ABC");
1714     QCOMPARE(a.leftJustified(0),(QString)"ABC");
1715
1716     QString n;
1717     QVERIFY(!n.leftJustified(3).isNull());
1718     QCOMPARE(a.leftJustified(4,' ',true),(QString)"ABC ");
1719     QCOMPARE(a.leftJustified(3,' ',true),(QString)"ABC");
1720     QCOMPARE(a.leftJustified(2,' ',true),(QString)"AB");
1721     QCOMPARE(a.leftJustified(1,' ',true),(QString)"A");
1722     QCOMPARE(a.leftJustified(0,' ',true),(QString)"");
1723 }
1724
1725 void tst_QString::rightJustified()
1726 {
1727     QString a;
1728     a="ABC";
1729     QCOMPARE(a.rightJustified(5,'-'),(QString)"--ABC");
1730     QCOMPARE(a.rightJustified(4,'-'),(QString)"-ABC");
1731     QCOMPARE(a.rightJustified(4),(QString)" ABC");
1732     QCOMPARE(a.rightJustified(3),(QString)"ABC");
1733     QCOMPARE(a.rightJustified(2),(QString)"ABC");
1734     QCOMPARE(a.rightJustified(1),(QString)"ABC");
1735     QCOMPARE(a.rightJustified(0),(QString)"ABC");
1736
1737     QString n;
1738     QVERIFY(!n.rightJustified(3).isNull());
1739     QCOMPARE(a.rightJustified(4,'-',true),(QString)"-ABC");
1740     QCOMPARE(a.rightJustified(4,' ',true),(QString)" ABC");
1741     QCOMPARE(a.rightJustified(3,' ',true),(QString)"ABC");
1742     QCOMPARE(a.rightJustified(2,' ',true),(QString)"AB");
1743     QCOMPARE(a.rightJustified(1,' ',true),(QString)"A");
1744     QCOMPARE(a.rightJustified(0,' ',true),(QString)"");
1745     QCOMPARE(a,(QString)"ABC");
1746 }
1747
1748 void tst_QString::toUpper()
1749 {
1750     QCOMPARE( QString().toUpper(), QString() );
1751     QCOMPARE( QString("").toUpper(), QString("") );
1752     QCOMPARE( QString("text").toUpper(), QString("TEXT") );
1753     QCOMPARE( QString("Text").toUpper(), QString("TEXT") );
1754     QCOMPARE( QString("tExt").toUpper(), QString("TEXT") );
1755     QCOMPARE( QString("teXt").toUpper(), QString("TEXT") );
1756     QCOMPARE( QString("texT").toUpper(), QString("TEXT") );
1757     QCOMPARE( QString("TExt").toUpper(), QString("TEXT") );
1758     QCOMPARE( QString("teXT").toUpper(), QString("TEXT") );
1759     QCOMPARE( QString("tEXt").toUpper(), QString("TEXT") );
1760     QCOMPARE( QString("tExT").toUpper(), QString("TEXT") );
1761     QCOMPARE( QString("TEXT").toUpper(), QString("TEXT") );
1762     QCOMPARE( QString("@ABYZ[").toUpper(), QString("@ABYZ["));
1763     QCOMPARE( QString("@abyz[").toUpper(), QString("@ABYZ["));
1764     QCOMPARE( QString("`ABYZ{").toUpper(), QString("`ABYZ{"));
1765     QCOMPARE( QString("`abyz{").toUpper(), QString("`ABYZ{"));
1766
1767     QCOMPARE( QString(1, QChar(0xdf)).toUpper(), QString("SS"));
1768
1769     QString lower, upper;
1770     lower += QChar(QChar::highSurrogate(0x10428));
1771     lower += QChar(QChar::lowSurrogate(0x10428));
1772     upper += QChar(QChar::highSurrogate(0x10400));
1773     upper += QChar(QChar::lowSurrogate(0x10400));
1774     QCOMPARE( lower.toUpper(), upper);
1775     lower += lower;
1776     upper += upper;
1777     QCOMPARE( lower.toUpper(), upper);
1778
1779     // test for broken surrogate pair handling (low low hi low hi low)
1780     lower.prepend(QChar(QChar::lowSurrogate(0x10428)));
1781     lower.prepend(QChar(QChar::lowSurrogate(0x10428)));
1782     upper.prepend(QChar(QChar::lowSurrogate(0x10428)));
1783     upper.prepend(QChar(QChar::lowSurrogate(0x10428)));
1784     QCOMPARE(lower.toUpper(), upper);
1785     // test for broken surrogate pair handling (low low hi low hi low hi hi)
1786     lower += QChar(QChar::highSurrogate(0x10428));
1787     lower += QChar(QChar::highSurrogate(0x10428));
1788     upper += QChar(QChar::highSurrogate(0x10428));
1789     upper += QChar(QChar::highSurrogate(0x10428));
1790     QCOMPARE(lower.toUpper(), upper);
1791
1792 #ifdef QT_USE_ICU
1793     // test doesn't work with ICU support, since QChar is unaware of any locale
1794     QEXPECT_FAIL("", "test doesn't work with ICU support, since QChar is unaware of any locale", Continue);
1795     QVERIFY(false);
1796 #else
1797     for (int i = 0; i < 65536; ++i) {
1798         QString str(1, QChar(i));
1799         QString upper = str.toUpper();
1800         QVERIFY(upper.length() >= 1);
1801         if (upper.length() == 1)
1802             QVERIFY(upper == QString(1, QChar(i).toUpper()));
1803     }
1804 #endif
1805 }
1806
1807 void tst_QString::toLower()
1808 {
1809     QCOMPARE( QString().toLower(), QString() );
1810     QCOMPARE( QString("").toLower(), QString("") );
1811     QCOMPARE( QString("text").toLower(), QString("text") );
1812     QCOMPARE( QString("Text").toLower(), QString("text") );
1813     QCOMPARE( QString("tExt").toLower(), QString("text") );
1814     QCOMPARE( QString("teXt").toLower(), QString("text") );
1815     QCOMPARE( QString("texT").toLower(), QString("text") );
1816     QCOMPARE( QString("TExt").toLower(), QString("text") );
1817     QCOMPARE( QString("teXT").toLower(), QString("text") );
1818     QCOMPARE( QString("tEXt").toLower(), QString("text") );
1819     QCOMPARE( QString("tExT").toLower(), QString("text") );
1820     QCOMPARE( QString("TEXT").toLower(), QString("text") );
1821     QCOMPARE( QString("@ABYZ[").toLower(), QString("@abyz["));
1822     QCOMPARE( QString("@abyz[").toLower(), QString("@abyz["));
1823     QCOMPARE( QString("`ABYZ{").toLower(), QString("`abyz{"));
1824     QCOMPARE( QString("`abyz{").toLower(), QString("`abyz{"));
1825
1826     QCOMPARE( QString(1, QChar(0x130)).toLower(), QString(QString(1, QChar(0x69)) + QChar(0x307)));
1827
1828     QString lower, upper;
1829     lower += QChar(QChar::highSurrogate(0x10428));
1830     lower += QChar(QChar::lowSurrogate(0x10428));
1831     upper += QChar(QChar::highSurrogate(0x10400));
1832     upper += QChar(QChar::lowSurrogate(0x10400));
1833     QCOMPARE( upper.toLower(), lower);
1834     lower += lower;
1835     upper += upper;
1836     QCOMPARE( upper.toLower(), lower);
1837
1838     // test for broken surrogate pair handling (low low hi low hi low)
1839     lower.prepend(QChar(QChar::lowSurrogate(0x10400)));
1840     lower.prepend(QChar(QChar::lowSurrogate(0x10400)));
1841     upper.prepend(QChar(QChar::lowSurrogate(0x10400)));
1842     upper.prepend(QChar(QChar::lowSurrogate(0x10400)));
1843     QCOMPARE( upper.toLower(), lower);
1844     // test for broken surrogate pair handling (low low hi low hi low hi hi)
1845     lower += QChar(QChar::highSurrogate(0x10400));
1846     lower += QChar(QChar::highSurrogate(0x10400));
1847     upper += QChar(QChar::highSurrogate(0x10400));
1848     upper += QChar(QChar::highSurrogate(0x10400));
1849     QCOMPARE( upper.toLower(), lower);
1850
1851 #ifdef QT_USE_ICU
1852     // test doesn't work with ICU support, since QChar is unaware of any locale
1853     QEXPECT_FAIL("", "test doesn't work with ICU support, since QChar is unaware of any locale", Continue);
1854     QVERIFY(false);
1855 #else
1856     for (int i = 0; i < 65536; ++i) {
1857         QString str(1, QChar(i));
1858         QString lower = str.toLower();
1859         QVERIFY(lower.length() >= 1);
1860         if (lower.length() == 1)
1861             QVERIFY(str.toLower() == QString(1, QChar(i).toLower()));
1862     }
1863 #endif
1864 }
1865
1866 void tst_QString::toCaseFolded()
1867 {
1868     QCOMPARE( QString().toCaseFolded(), QString() );
1869     QCOMPARE( QString("").toCaseFolded(), QString("") );
1870     QCOMPARE( QString("text").toCaseFolded(), QString("text") );
1871     QCOMPARE( QString("Text").toCaseFolded(), QString("text") );
1872     QCOMPARE( QString("tExt").toCaseFolded(), QString("text") );
1873     QCOMPARE( QString("teXt").toCaseFolded(), QString("text") );
1874     QCOMPARE( QString("texT").toCaseFolded(), QString("text") );
1875     QCOMPARE( QString("TExt").toCaseFolded(), QString("text") );
1876     QCOMPARE( QString("teXT").toCaseFolded(), QString("text") );
1877     QCOMPARE( QString("tEXt").toCaseFolded(), QString("text") );
1878     QCOMPARE( QString("tExT").toCaseFolded(), QString("text") );
1879     QCOMPARE( QString("TEXT").toCaseFolded(), QString("text") );
1880     QCOMPARE( QString("@ABYZ[").toCaseFolded(), QString("@abyz["));
1881     QCOMPARE( QString("@abyz[").toCaseFolded(), QString("@abyz["));
1882     QCOMPARE( QString("`ABYZ{").toCaseFolded(), QString("`abyz{"));
1883     QCOMPARE( QString("`abyz{").toCaseFolded(), QString("`abyz{"));
1884
1885     QString lower, upper;
1886     upper += QChar(QChar::highSurrogate(0x10400));
1887     upper += QChar(QChar::lowSurrogate(0x10400));
1888     lower += QChar(QChar::highSurrogate(0x10428));
1889     lower += QChar(QChar::lowSurrogate(0x10428));
1890     QCOMPARE( upper.toCaseFolded(), lower);
1891     lower += lower;
1892     upper += upper;
1893     QCOMPARE( upper.toCaseFolded(), lower);
1894
1895     // test for broken surrogate pair handling (low low hi low hi low)
1896     lower.prepend(QChar(QChar::lowSurrogate(0x10400)));
1897     lower.prepend(QChar(QChar::lowSurrogate(0x10400)));
1898     upper.prepend(QChar(QChar::lowSurrogate(0x10400)));
1899     upper.prepend(QChar(QChar::lowSurrogate(0x10400)));
1900     QCOMPARE(upper.toCaseFolded(), lower);
1901     // test for broken surrogate pair handling (low low hi low hi low hi hi)
1902     lower += QChar(QChar::highSurrogate(0x10400));
1903     lower += QChar(QChar::highSurrogate(0x10400));
1904     upper += QChar(QChar::highSurrogate(0x10400));
1905     upper += QChar(QChar::highSurrogate(0x10400));
1906     QCOMPARE(upper.toCaseFolded(), lower);
1907
1908     //### we currently don't support full case foldings
1909     for (int i = 0; i < 65536; ++i) {
1910         QString str(1, QChar(i));
1911         QString lower = str.toCaseFolded();
1912         QVERIFY(lower.length() >= 1);
1913         if (lower.length() == 1)
1914             QVERIFY(str.toCaseFolded() == QString(1, QChar(i).toCaseFolded()));
1915     }
1916 }
1917
1918 void tst_QString::trimmed()
1919 {
1920     QString a;
1921     a="Text";
1922     QCOMPARE(a,(QString)"Text");
1923     QCOMPARE(a.trimmed(),(QString)"Text");
1924     QCOMPARE(a,(QString)"Text");
1925     a=" ";
1926     QCOMPARE(a.trimmed(),(QString)"");
1927     QCOMPARE(a,(QString)" ");
1928     a=" a   ";
1929     QCOMPARE(a.trimmed(),(QString)"a");
1930 }
1931
1932 void tst_QString::simplified_data()
1933 {
1934     QTest::addColumn<QString>("full" );
1935     QTest::addColumn<QString>("simple" );
1936
1937     QTest::newRow("null") << QString() << QString();
1938     QTest::newRow("empty") << "" << "";
1939     QTest::newRow("one char") << "a" << "a";
1940     QTest::newRow("one word") << "foo" << "foo";
1941     QTest::newRow("chars trivial") << "a b" << "a b";
1942     QTest::newRow("words trivial") << "foo bar" << "foo bar";
1943     QTest::newRow("allspace") << "  \t\v " << "";
1944     QTest::newRow("char trailing") << "a " << "a";
1945     QTest::newRow("char trailing tab") << "a\t" << "a";
1946     QTest::newRow("char multitrailing") << "a   " << "a";
1947     QTest::newRow("char multitrailing tab") << "a   \t" << "a";
1948     QTest::newRow("char leading") << " a" << "a";
1949     QTest::newRow("char leading tab") << "\ta" << "a";
1950     QTest::newRow("char multileading") << "   a" << "a";
1951     QTest::newRow("char multileading tab") << "\t   a" << "a";
1952     QTest::newRow("chars apart") << "a  b" << "a b";
1953     QTest::newRow("words apart") << "foo  bar" << "foo bar";
1954     QTest::newRow("enclosed word") << "   foo \t " << "foo";
1955     QTest::newRow("enclosed chars apart") << " a   b " << "a b";
1956     QTest::newRow("enclosed words apart") << " foo   bar " << "foo bar";
1957     QTest::newRow("chars apart posttab") << "a \tb" << "a b";
1958     QTest::newRow("chars apart pretab") << "a\t b" << "a b";
1959     QTest::newRow("many words") << "  just some    random\ttext here" << "just some random text here";
1960 }
1961
1962 void tst_QString::simplified()
1963 {
1964     QFETCH(QString, full);
1965     QFETCH(QString, simple);
1966
1967     QString result = full.simplified();
1968     if (simple.isNull()) {
1969         QVERIFY2(result.isNull(), qPrintable("'" + full + "' did not yield null: " + result));
1970     } else if (simple.isEmpty()) {
1971         QVERIFY2(result.isEmpty() && !result.isNull(), qPrintable("'" + full + "' did not yield empty: " + result));
1972     } else {
1973         QCOMPARE(result, simple);
1974         if (full == simple)
1975             QVERIFY(result.isSharedWith(full));
1976     }
1977 }
1978
1979 void tst_QString::insert()
1980 {
1981     QString a;
1982
1983     a = "Ys";
1984     QCOMPARE(a.insert(1,'e'), QString("Yes"));
1985     QCOMPARE(a.insert(3,'!'), QString("Yes!"));
1986     QCOMPARE(a.insert(5,'?'), QString("Yes! ?"));
1987
1988     a = "ABC";
1989     QCOMPARE(a.insert(5,"DEF"), QString("ABC  DEF"));
1990
1991     a = "ABC";
1992     QCOMPARE(a.insert(2, QString()), QString("ABC"));
1993     QCOMPARE(a.insert(0,"ABC"), QString("ABCABC"));
1994     QCOMPARE(a, QString("ABCABC"));
1995     QCOMPARE(a.insert(0,a), QString("ABCABCABCABC"));
1996
1997     QCOMPARE(a, QString("ABCABCABCABC"));
1998     QCOMPARE(a.insert(0,'<'), QString("<ABCABCABCABC"));
1999     QCOMPARE(a.insert(1,'>'), QString("<>ABCABCABCABC"));
2000
2001     a = "Meal";
2002     QCOMPARE(a.insert(1, QLatin1String("ontr")), QString("Montreal"));
2003     QCOMPARE(a.insert(4, ""), QString("Montreal"));
2004     QCOMPARE(a.insert(3, QLatin1String("")), QString("Montreal"));
2005     QCOMPARE(a.insert(3, QLatin1String(0)), QString("Montreal"));
2006     QCOMPARE(a.insert(3, static_cast<const char *>(0)), QString("Montreal"));
2007     QCOMPARE(a.insert(0, QLatin1String("a")), QString("aMontreal"));
2008 }
2009
2010 void tst_QString::append()
2011 {
2012     QString a;
2013     a = "<>ABCABCABCABC";
2014     QCOMPARE(a.append(">"),(QString)"<>ABCABCABCABC>");
2015 }
2016
2017 void tst_QString::append_bytearray_data()
2018 {
2019     QTest::addColumn<QString>("str" );
2020     QTest::addColumn<QByteArray>("ba" );
2021     QTest::addColumn<QString>("res" );
2022
2023     QByteArray ba( 5, 0 );
2024     ba[0] = 'a';
2025     ba[1] = 'b';
2026     ba[2] = 'c';
2027     ba[3] = 'd';
2028
2029     // no 0 termination
2030     ba.resize( 4 );
2031     QTest::newRow( "notTerminated_0" ) << QString() << ba << QString("abcd");
2032     QTest::newRow( "notTerminated_1" ) << QString("") << ba << QString("abcd");
2033     QTest::newRow( "notTerminated_2" ) << QString("foobar ") << ba << QString("foobar abcd");
2034
2035     // byte array with only a 0
2036     ba.resize( 1 );
2037     ba[0] = 0;
2038     QByteArray ba2("foobar ");
2039     ba2.append('\0');
2040     QTest::newRow( "emptyString" ) << QString("foobar ") << ba << QString(ba2);
2041
2042     // empty byte array
2043     ba.resize( 0 );
2044     QTest::newRow( "emptyByteArray" ) << QString("foobar ") << ba << QString("foobar ");
2045
2046     // non-ascii byte array
2047     QTest::newRow( "nonAsciiByteArray") << QString() << QByteArray("\xc3\xa9") << QString("\xc3\xa9");
2048     QTest::newRow( "nonAsciiByteArray2") << QString() << QByteArray("\xc3\xa9") << QString::fromUtf8("\xc3\xa9");
2049 }
2050
2051 void tst_QString::append_bytearray()
2052 {
2053     {
2054         QFETCH( QString, str );
2055         QFETCH( QByteArray, ba );
2056
2057         str.append( ba );
2058
2059         QTEST( str, "res" );
2060     }
2061     {
2062         QFETCH( QString, str );
2063         QFETCH( QByteArray, ba );
2064
2065         str.append( ba );
2066
2067         QTEST( str, "res" );
2068     }
2069
2070     QFETCH( QByteArray, ba );
2071     if (ba.constData()[ba.length()] == '\0') {
2072         QFETCH( QString, str );
2073
2074         str.append(ba.constData());
2075         QTEST( str, "res" );
2076     }
2077 }
2078
2079 void tst_QString::operator_pluseq_bytearray_data()
2080 {
2081     append_bytearray_data();
2082 }
2083
2084 void tst_QString::operator_pluseq_bytearray()
2085 {
2086     {
2087         QFETCH( QString, str );
2088         QFETCH( QByteArray, ba );
2089
2090         str += ba;
2091
2092         QTEST( str, "res" );
2093     }
2094     {
2095         QFETCH( QString, str );
2096         QFETCH( QByteArray, ba );
2097
2098         str += ba;
2099
2100         QTEST( str, "res" );
2101     }
2102
2103     QFETCH( QByteArray, ba );
2104     if (ba.constData()[ba.length()] == '\0') {
2105         QFETCH( QString, str );
2106
2107         str += ba.constData();
2108         QTEST( str, "res" );
2109     }
2110 }
2111
2112 void tst_QString::operator_eqeq_bytearray_data()
2113 {
2114     constructorQByteArray_data();
2115 }
2116
2117 void tst_QString::operator_eqeq_bytearray()
2118 {
2119     QFETCH(QByteArray, src);
2120     QFETCH(QString, expected);
2121
2122     QVERIFY(expected == src);
2123     QVERIFY(!(expected != src));
2124
2125     if (src.constData()[src.length()] == '\0') {
2126         QVERIFY(expected == src.constData());
2127         QVERIFY(!(expected != src.constData()));
2128     }
2129 }
2130
2131 void tst_QString::swap()
2132 {
2133     QString s1, s2;
2134     s1 = "s1";
2135     s2 = "s2";
2136     s1.swap(s2);
2137     QCOMPARE(s1,QLatin1String("s2"));
2138     QCOMPARE(s2,QLatin1String("s1"));
2139 }
2140
2141 void tst_QString::prepend()
2142 {
2143     QString a;
2144     a = "<>ABCABCABCABC>";
2145     QCOMPARE(a.prepend("-"),(QString)"-<>ABCABCABCABC>");
2146 }
2147
2148 void tst_QString::prepend_bytearray_data()
2149 {
2150     QTest::addColumn<QString>("str" );
2151     QTest::addColumn<QByteArray>("ba" );
2152     QTest::addColumn<QString>("res" );
2153
2154     QByteArray ba( 5, 0 );
2155     ba[0] = 'a';
2156     ba[1] = 'b';
2157     ba[2] = 'c';
2158     ba[3] = 'd';
2159
2160     // byte array with only a 0
2161     ba.resize( 1 );
2162     ba[0] = 0;
2163     QTest::newRow( "emptyString" ) << QString("foobar ") << ba << QString("foobar ");
2164
2165     // empty byte array
2166     ba.resize( 0 );
2167     QTest::newRow( "emptyByteArray" ) << QString(" foobar") << ba << QString(" foobar");
2168
2169     // non-ascii byte array
2170     QTest::newRow( "nonAsciiByteArray") << QString() << QByteArray("\xc3\xa9") << QString("\xc3\xa9");
2171     QTest::newRow( "nonAsciiByteArray2") << QString() << QByteArray("\xc3\xa9") << QString::fromUtf8("\xc3\xa9");
2172 }
2173
2174 void tst_QString::prepend_bytearray()
2175 {
2176     {
2177         QFETCH( QString, str );
2178         QFETCH( QByteArray, ba );
2179
2180         str.prepend( ba );
2181
2182         QFETCH( QString, res );
2183         QCOMPARE( str, res );
2184     }
2185     {
2186         QFETCH( QString, str );
2187         QFETCH( QByteArray, ba );
2188
2189         str.prepend( ba );
2190
2191         QTEST( str, "res" );
2192     }
2193
2194     QFETCH( QByteArray, ba );
2195     if (ba.constData()[ba.length()] == '\0') {
2196         QFETCH( QString, str );
2197
2198         str.prepend(ba.constData());
2199         QTEST( str, "res" );
2200     }
2201 }
2202
2203 void tst_QString::replace_uint_uint()
2204 {
2205     QFETCH( QString, string );
2206     QFETCH( int, index );
2207     QFETCH( int, len );
2208     QFETCH( QString, after );
2209
2210     QEXPECT_FAIL("overflow", "QTBUG-22967: integer overflow if (index + len) > INT_MAX", Abort);
2211
2212     QString s1 = string;
2213     s1.replace( (uint) index, (int) len, after );
2214     QTEST( s1, "result" );
2215
2216     QString s2 = string;
2217     s2.replace( (uint) index, (uint) len, after.unicode(), after.length() );
2218     QTEST( s2, "result" );
2219
2220     if ( after.length() == 1 ) {
2221         QString s3 = string;
2222         s3.replace( (uint) index, (uint) len, QChar(after[0]) );
2223         QTEST( s3, "result" );
2224
2225         QString s4 = string;
2226         s4.replace( (uint) index, (uint) len, QChar(after[0]).toLatin1() );
2227         QTEST( s4, "result" );
2228     }
2229 }
2230
2231 void tst_QString::replace_uint_uint_extra()
2232 {
2233     /*
2234         This test is designed to be extremely slow if QString::replace() doesn't optimize the case
2235         len == after.size().
2236     */
2237     QString str("dsfkljfdsjklsdjsfjklfsdjkldfjslkjsdfkllkjdsfjklsfdkjsdflkjlsdfjklsdfkjldsflkjsddlkj");
2238     for (int j = 1; j < 12; ++j)
2239         str += str;
2240
2241     QString str2("aaaaaaaaaaaaaaaaaaaa");
2242     for (int i = 0; i < 2000000; ++i) {
2243         str.replace(10, 20, str2);
2244     }
2245
2246     /*
2247         Make sure that replacing with itself works.
2248     */
2249     QString copy(str);
2250     copy.detach();
2251     str.replace(0, str.length(), str);
2252     QVERIFY(copy == str);
2253
2254     /*
2255         Make sure that replacing a part of oneself with itself works.
2256     */
2257     QString str3("abcdefghij");
2258     str3.replace(0, 1, str3);
2259     QCOMPARE(str3, QString("abcdefghijbcdefghij"));
2260
2261     QString str4("abcdefghij");
2262     str4.replace(1, 3, str4);
2263     QCOMPARE(str4, QString("aabcdefghijefghij"));
2264
2265     QString str5("abcdefghij");
2266     str5.replace(8, 10, str5);
2267     QCOMPARE(str5, QString("abcdefghabcdefghij"));
2268 }
2269
2270 void tst_QString::replace_string()
2271 {
2272     QFETCH( QString, string );
2273     QFETCH( QString, before );
2274     QFETCH( QString, after );
2275     QFETCH( bool, bcs );
2276
2277     Qt::CaseSensitivity cs = bcs ? Qt::CaseSensitive : Qt::CaseInsensitive;
2278
2279     if ( before.length() == 1 ) {
2280         QChar ch = before.at( 0 );
2281
2282         QString s1 = string;
2283         s1.replace( ch, after, cs );
2284         QTEST( s1, "result" );
2285
2286         if ( QChar(ch.toLatin1()) == ch ) {
2287             QString s2 = string;
2288             s2.replace( ch.toLatin1(), after, cs );
2289             QTEST( s2, "result" );
2290         }
2291     }
2292
2293     QString s3 = string;
2294     s3.replace( before, after, cs );
2295     QTEST( s3, "result" );
2296
2297     QString s4 = string;
2298     s4.replace( QRegExp(QRegExp::escape(before), cs), after );
2299     QTEST( s4, "result" );
2300
2301     QString s5 = string;
2302     s5.replace(QRegExp(before, cs, QRegExp::FixedString), after);
2303     QTEST( s5, "result" );
2304 }
2305
2306 void tst_QString::replace_regexp()
2307 {
2308     QFETCH( QString, string );
2309     QFETCH( QString, regexp );
2310     QFETCH( QString, after );
2311
2312     QString s2 = string;
2313     s2.replace( QRegExp(regexp), after );
2314     QTEST( s2, "result" );
2315     s2 = string;
2316     s2.replace( QRegularExpression(regexp), after );
2317     QTEST( s2, "result" );
2318 }
2319
2320 void tst_QString::remove_uint_uint()
2321 {
2322     QFETCH( QString, string );
2323     QFETCH( int, index );
2324     QFETCH( int, len );
2325     QFETCH( QString, after );
2326
2327     if ( after.length() == 0 ) {
2328         QString s1 = string;
2329         s1.remove( (uint) index, (uint) len );
2330         QTEST( s1, "result" );
2331     } else
2332         QCOMPARE( 0, 0 ); // shut QtTest
2333 }
2334
2335 void tst_QString::remove_string()
2336 {
2337     QFETCH( QString, string );
2338     QFETCH( QString, before );
2339     QFETCH( QString, after );
2340     QFETCH( bool, bcs );
2341
2342     Qt::CaseSensitivity cs = bcs ? Qt::CaseSensitive : Qt::CaseInsensitive;
2343
2344     if ( after.length() == 0 ) {
2345         if ( before.length() == 1 && cs ) {
2346             QChar ch = before.at( 0 );
2347
2348             QString s1 = string;
2349             s1.remove( ch );
2350             QTEST( s1, "result" );
2351
2352             if ( QChar(ch.toLatin1()) == ch ) {
2353                 QString s2 = string;
2354                 s2.remove( ch );
2355                 QTEST( s2, "result" );
2356             }
2357         }
2358
2359         QString s3 = string;
2360         s3.remove( before, cs );
2361         QTEST( s3, "result" );
2362
2363         QString s4 = string;
2364         s4.replace( QRegExp(QRegExp::escape(before), cs), after );
2365         QTEST( s4, "result" );
2366
2367         QString s5 = string;
2368         s5.replace( QRegExp(before, cs, QRegExp::FixedString), after );
2369         QTEST( s5, "result" );
2370     } else {
2371         QCOMPARE( 0, 0 ); // shut QtTest
2372     }
2373 }
2374
2375 void tst_QString::remove_regexp()
2376 {
2377     QFETCH( QString, string );
2378     QFETCH( QString, regexp );
2379     QFETCH( QString, after );
2380
2381     if ( after.length() == 0 ) {
2382         QString s2 = string;
2383         s2.remove( QRegExp(regexp) );
2384         QTEST( s2, "result" );
2385
2386         s2 = string;
2387         s2.remove( QRegularExpression(regexp) );
2388         QTEST( s2, "result" );
2389     } else {
2390         QCOMPARE( 0, 0 ); // shut QtTest
2391     }
2392 }
2393
2394 void tst_QString::toNum()
2395 {
2396 #if defined (Q_OS_WIN) && defined (Q_CC_MSVC)
2397 #define TEST_TO_INT(num, func) \
2398     a = #num; \
2399     QVERIFY2(a.func(&ok) == num ## i64 && ok, "Failed: num=" #num ", func=" #func);
2400 #else
2401 #define TEST_TO_INT(num, func) \
2402     a = #num; \
2403     QVERIFY2(a.func(&ok) == num ## LL && ok, "Failed: num=" #num ", func=" #func);
2404 #endif
2405
2406     QString a;
2407     bool ok = false;
2408
2409     TEST_TO_INT(0, toInt)
2410     TEST_TO_INT(-1, toInt)
2411     TEST_TO_INT(1, toInt)
2412     TEST_TO_INT(2147483647, toInt)
2413     TEST_TO_INT(-2147483648, toInt)
2414
2415     TEST_TO_INT(0, toShort)
2416     TEST_TO_INT(-1, toShort)
2417     TEST_TO_INT(1, toShort)
2418     TEST_TO_INT(32767, toShort)
2419     TEST_TO_INT(-32768, toShort)
2420
2421     TEST_TO_INT(0, toLong)
2422     TEST_TO_INT(-1, toLong)
2423     TEST_TO_INT(1, toLong)
2424     TEST_TO_INT(2147483647, toLong)
2425     TEST_TO_INT(-2147483648, toLong)
2426     TEST_TO_INT(0, toLongLong)
2427     TEST_TO_INT(-1, toLongLong)
2428     TEST_TO_INT(1, toLongLong)
2429     TEST_TO_INT(9223372036854775807, toLongLong)
2430     TEST_TO_INT(-9223372036854775807, toLongLong)
2431
2432 #undef TEST_TO_INT
2433
2434 #if defined (Q_OS_WIN) && defined (Q_CC_MSVC)
2435 #define TEST_TO_UINT(num, func) \
2436     a = #num; \
2437     QVERIFY2(a.func(&ok) == num ## i64 && ok, "Failed: num=" #num ", func=" #func);
2438 #else
2439 #define TEST_TO_UINT(num, func) \
2440     a = #num; \
2441     QVERIFY2(a.func(&ok) == num ## ULL && ok, "Failed: num=" #num ", func=" #func);
2442 #endif
2443
2444     TEST_TO_UINT(0, toUInt)
2445     TEST_TO_UINT(1, toUInt)
2446     TEST_TO_UINT(4294967295, toUInt)
2447
2448     TEST_TO_UINT(0, toUShort)
2449     TEST_TO_UINT(1, toUShort)
2450     TEST_TO_UINT(65535, toUShort)
2451
2452     TEST_TO_UINT(0, toULong)
2453     TEST_TO_UINT(1, toULong)
2454     TEST_TO_UINT(4294967295, toULong)
2455
2456     TEST_TO_UINT(0, toULongLong)
2457     TEST_TO_UINT(1, toULongLong)
2458     TEST_TO_UINT(18446744073709551615, toULongLong)
2459 #undef TEST_TO_UINT
2460
2461
2462 #define TEST_BASE(str, base, num) \
2463     a = str; \
2464     QVERIFY2(a.toInt(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toInt"); \
2465     QVERIFY2(a.toUInt(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toUInt"); \
2466     QVERIFY2(a.toShort(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toShort"); \
2467     QVERIFY2(a.toUShort(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toUShort"); \
2468     QVERIFY2(a.toLong(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toLong"); \
2469     QVERIFY2(a.toULong(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toULong"); \
2470     QVERIFY2(a.toLongLong(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toLongLong"); \
2471     QVERIFY2(a.toULongLong(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toULongLong");
2472
2473     TEST_BASE("FF", 16, 255)
2474     TEST_BASE("0xFF", 16, 255)
2475     TEST_BASE("77", 8, 63)
2476     TEST_BASE("077", 8, 63)
2477
2478     TEST_BASE("0xFF", 0, 255)
2479     TEST_BASE("077", 0, 63)
2480     TEST_BASE("255", 0, 255)
2481
2482     TEST_BASE(" FF", 16, 255)
2483     TEST_BASE(" 0xFF", 16, 255)
2484     TEST_BASE(" 77", 8, 63)
2485     TEST_BASE(" 077", 8, 63)
2486
2487     TEST_BASE(" 0xFF", 0, 255)
2488     TEST_BASE(" 077", 0, 63)
2489     TEST_BASE(" 255", 0, 255)
2490
2491     TEST_BASE("\tFF\t", 16, 255)
2492     TEST_BASE("\t0xFF  ", 16, 255)
2493     TEST_BASE("   77   ", 8, 63)
2494     TEST_BASE("77  ", 8, 63)
2495
2496 #undef TEST_BASE
2497
2498 #define TEST_NEG_BASE(str, base, num) \
2499     a = str; \
2500     QVERIFY2(a.toInt(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toInt"); \
2501     QVERIFY2(a.toShort(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toShort"); \
2502     QVERIFY2(a.toLong(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toLong"); \
2503     QVERIFY2(a.toLongLong(&ok, base) == num && ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toLongLong");
2504
2505     TEST_NEG_BASE("-FE", 16, -254)
2506     TEST_NEG_BASE("-0xFE", 16, -254)
2507     TEST_NEG_BASE("-77", 8, -63)
2508     TEST_NEG_BASE("-077", 8, -63)
2509
2510     TEST_NEG_BASE("-0xFE", 0, -254)
2511     TEST_NEG_BASE("-077", 0, -63)
2512     TEST_NEG_BASE("-254", 0, -254)
2513
2514 #undef TEST_NEG_BASE
2515
2516 #define TEST_DOUBLE(num, str) \
2517     a = str; \
2518     QCOMPARE(a.toDouble(&ok), num); \
2519     QVERIFY(ok);
2520
2521     TEST_DOUBLE(1.2345, "1.2345")
2522     TEST_DOUBLE(12.345, "1.2345e+01")
2523     TEST_DOUBLE(12.345, "1.2345E+01")
2524     TEST_DOUBLE(12345.6, "12345.6")
2525
2526 #undef TEST_DOUBLE
2527
2528
2529 #define TEST_BAD(str, func) \
2530     a = str; \
2531     a.func(&ok); \
2532     QVERIFY2(!ok, "Failed: str=" #str " func=" #func);
2533
2534     TEST_BAD("32768", toShort)
2535     TEST_BAD("-32769", toShort)
2536     TEST_BAD("65536", toUShort)
2537     TEST_BAD("2147483648", toInt)
2538     TEST_BAD("-2147483649", toInt)
2539     TEST_BAD("4294967296", toUInt)
2540     if (sizeof(long) == 4) {
2541         TEST_BAD("2147483648", toLong)
2542         TEST_BAD("-2147483649", toLong)
2543         TEST_BAD("4294967296", toULong)
2544     }
2545     TEST_BAD("9223372036854775808", toLongLong)
2546     TEST_BAD("-9223372036854775809", toLongLong)
2547     TEST_BAD("18446744073709551616", toULongLong)
2548     TEST_BAD("-1", toUShort)
2549     TEST_BAD("-1", toUInt)
2550     TEST_BAD("-1", toULong)
2551     TEST_BAD("-1", toULongLong)
2552 #undef TEST_BAD
2553
2554 #define TEST_BAD_ALL(str) \
2555     a = str; \
2556     a.toShort(&ok); \
2557     QVERIFY2(!ok, "Failed: str=" #str); \
2558     a.toUShort(&ok); \
2559     QVERIFY2(!ok, "Failed: str=" #str); \
2560     a.toInt(&ok); \
2561     QVERIFY2(!ok, "Failed: str=" #str); \
2562     a.toUInt(&ok); \
2563     QVERIFY2(!ok, "Failed: str=" #str); \
2564     a.toLong(&ok); \
2565     QVERIFY2(!ok, "Failed: str=" #str); \
2566     a.toULong(&ok); \
2567     QVERIFY2(!ok, "Failed: str=" #str); \
2568     a.toLongLong(&ok); \
2569     QVERIFY2(!ok, "Failed: str=" #str); \
2570     a.toULongLong(&ok); \
2571     QVERIFY2(!ok, "Failed: str=" #str); \
2572     a.toFloat(&ok); \
2573     QVERIFY2(!ok, "Failed: str=" #str); \
2574     a.toDouble(&ok); \
2575     QVERIFY2(!ok, "Failed: str=" #str);
2576
2577     TEST_BAD_ALL((const char*)0);
2578     TEST_BAD_ALL("");
2579     TEST_BAD_ALL(" ");
2580     TEST_BAD_ALL(".");
2581     TEST_BAD_ALL("-");
2582     TEST_BAD_ALL("hello");
2583     TEST_BAD_ALL("1.2.3");
2584     TEST_BAD_ALL("0x0x0x");
2585     TEST_BAD_ALL("123-^~<");
2586     TEST_BAD_ALL("123ThisIsNotANumber");
2587
2588 #undef TEST_BAD_ALL
2589
2590     a = "FF";
2591     a.toULongLong(&ok, 10);
2592     QVERIFY(!ok);
2593
2594     a = "FF";
2595     a.toULongLong(&ok, 0);
2596     QVERIFY(!ok);
2597
2598 #ifdef QT_NO_FPU
2599     double d = 3.40282346638528e+38; // slightly off FLT_MAX when using hardfloats
2600 #else
2601     double d = 3.4028234663852886e+38; // FLT_MAX
2602 #endif
2603     QString::number(d, 'e', 17).toFloat(&ok);
2604     QVERIFY(ok);
2605     QString::number(d + 1e32, 'e', 17).toFloat(&ok);
2606     QVERIFY(!ok);
2607     QString::number(-d, 'e', 17).toFloat(&ok);
2608     QVERIFY(ok);
2609     QString::number(-d - 1e32, 'e', 17).toFloat(&ok);
2610     QVERIFY(!ok);
2611     QString::number(d + 1e32, 'e', 17).toDouble(&ok);
2612     QVERIFY(ok);
2613     QString::number(-d - 1e32, 'e', 17).toDouble(&ok);
2614     QVERIFY(ok);
2615 }
2616
2617 void tst_QString::toUShort()
2618 {
2619     QString a;
2620     bool ok;
2621     QCOMPARE(a.toUShort(),(ushort)0);
2622     QCOMPARE(a.toUShort(&ok),(ushort)0);
2623     QVERIFY(!ok);
2624
2625     a="";
2626     QCOMPARE(a.toUShort(),(ushort)0);
2627     QCOMPARE(a.toUShort(&ok),(ushort)0);
2628     QVERIFY(!ok);
2629
2630     a="COMPARE";
2631     QCOMPARE(a.toUShort(),(ushort)0);
2632     QCOMPARE(a.toUShort(&ok),(ushort)0);
2633     QVERIFY(!ok);
2634
2635     a="123";
2636     QCOMPARE(a.toUShort(),(ushort)123);
2637     QCOMPARE(a.toUShort(&ok),(ushort)123);
2638     QVERIFY(ok);
2639
2640     a="123A";
2641     QCOMPARE(a.toUShort(),(ushort)0);
2642     QCOMPARE(a.toUShort(&ok),(ushort)0);
2643     QVERIFY(!ok);
2644
2645     a="1234567";
2646     QCOMPARE(a.toUShort(),(ushort)0);
2647     QCOMPARE(a.toUShort(&ok),(ushort)0);
2648     QVERIFY(!ok);
2649
2650     a = "aaa123aaa";
2651     QCOMPARE(a.toUShort(),(ushort)0);
2652     QCOMPARE(a.toUShort(&ok),(ushort)0);
2653     QVERIFY(!ok);
2654
2655     a = "aaa123";
2656     QCOMPARE(a.toUShort(),(ushort)0);
2657     QCOMPARE(a.toUShort(&ok),(ushort)0);
2658     QVERIFY(!ok);
2659
2660     a = "123aaa";
2661     QCOMPARE(a.toUShort(),(ushort)0);
2662     QCOMPARE(a.toUShort(&ok),(ushort)0);
2663     QVERIFY(!ok);
2664
2665     a = "32767";
2666     QCOMPARE(a.toUShort(),(ushort)32767);
2667     QCOMPARE(a.toUShort(&ok),(ushort)32767);
2668     QVERIFY(ok);
2669
2670     a = "-32767";
2671     QCOMPARE(a.toUShort(),(ushort)0);
2672     QCOMPARE(a.toUShort(&ok),(ushort)0);
2673     QVERIFY(!ok);
2674
2675     a = "65535";
2676     QCOMPARE(a.toUShort(),(ushort)65535);
2677     QCOMPARE(a.toUShort(&ok),(ushort)65535);
2678     QVERIFY(ok);
2679
2680     if (sizeof(short) == 2) {
2681         a = "65536";
2682         QCOMPARE(a.toUShort(),(ushort)0);
2683         QCOMPARE(a.toUShort(&ok),(ushort)0);
2684         QVERIFY(!ok);
2685
2686         a = "123456";
2687         QCOMPARE(a.toUShort(),(ushort)0);
2688         QCOMPARE(a.toUShort(&ok),(ushort)0);
2689         QVERIFY(!ok);
2690     }
2691 }
2692
2693 void tst_QString::toShort()
2694 {
2695     QString a;
2696     bool ok;
2697     QCOMPARE(a.toShort(),(short)0);
2698     QCOMPARE(a.toShort(&ok),(short)0);
2699     QVERIFY(!ok);
2700
2701     a="";
2702     QCOMPARE(a.toShort(),(short)0);
2703     QCOMPARE(a.toShort(&ok),(short)0);
2704     QVERIFY(!ok);
2705
2706     a="COMPARE";
2707     QCOMPARE(a.toShort(),(short)0);
2708     QCOMPARE(a.toShort(&ok),(short)0);
2709     QVERIFY(!ok);
2710
2711     a="123";
2712     QCOMPARE(a.toShort(),(short)123);
2713     QCOMPARE(a.toShort(&ok),(short)123);
2714     QVERIFY(ok);
2715
2716     a="123A";
2717     QCOMPARE(a.toShort(),(short)0);
2718     QCOMPARE(a.toShort(&ok),(short)0);
2719     QVERIFY(!ok);
2720
2721     a="1234567";
2722     QCOMPARE(a.toShort(),(short)0);
2723     QCOMPARE(a.toShort(&ok),(short)0);
2724     QVERIFY(!ok);
2725
2726     a = "aaa123aaa";
2727     QCOMPARE(a.toShort(),(short)0);
2728     QCOMPARE(a.toShort(&ok),(short)0);
2729     QVERIFY(!ok);
2730
2731     a = "aaa123";
2732     QCOMPARE(a.toShort(),(short)0);
2733     QCOMPARE(a.toShort(&ok),(short)0);
2734     QVERIFY(!ok);
2735
2736     a = "123aaa";
2737     QCOMPARE(a.toShort(),(short)0);
2738     QCOMPARE(a.toShort(&ok),(short)0);
2739     QVERIFY(!ok);
2740
2741     a = "32767";
2742     QCOMPARE(a.toShort(),(short)32767);
2743     QCOMPARE(a.toShort(&ok),(short)32767);
2744     QVERIFY(ok);
2745
2746     a = "-32767";
2747     QCOMPARE(a.toShort(),(short)-32767);
2748     QCOMPARE(a.toShort(&ok),(short)-32767);
2749     QVERIFY(ok);
2750
2751     a = "-32768";
2752     QCOMPARE(a.toShort(),(short)-32768);
2753     QCOMPARE(a.toShort(&ok),(short)-32768);
2754     QVERIFY(ok);
2755
2756     if (sizeof(short) == 2) {
2757         a = "32768";
2758         QCOMPARE(a.toShort(),(short)0);
2759         QCOMPARE(a.toShort(&ok),(short)0);
2760         QVERIFY(!ok);
2761
2762         a = "-32769";
2763         QCOMPARE(a.toShort(),(short)0);
2764         QCOMPARE(a.toShort(&ok),(short)0);
2765         QVERIFY(!ok);
2766     }
2767 }
2768
2769 void tst_QString::toInt()
2770 {
2771     QString a;
2772     bool ok;
2773     QCOMPARE(a.toInt(),0);
2774     QCOMPARE(a.toInt(&ok),0);
2775     QVERIFY(!ok);
2776
2777     a = "";
2778     QCOMPARE(a.toInt(),0);
2779     QCOMPARE(a.toInt(&ok),0);
2780     QVERIFY(!ok);
2781
2782     a="COMPARE";
2783     QCOMPARE(a.toInt(),0);
2784     QCOMPARE(a.toInt(&ok),0);
2785     QVERIFY(!ok);
2786
2787     a="123";
2788     QCOMPARE(a.toInt(),123);
2789     QCOMPARE(a.toInt(&ok),123);
2790     QVERIFY(ok);
2791
2792     a="123A";
2793     QCOMPARE(a.toInt(),0);
2794     QCOMPARE(a.toInt(&ok),0);
2795     QVERIFY(!ok);
2796
2797     a="1234567";
2798     QCOMPARE(a.toInt(),1234567);
2799     QCOMPARE(a.toInt(&ok),1234567);
2800     QVERIFY(ok);
2801
2802     a="12345678901234";
2803     QCOMPARE(a.toInt(),0);
2804     QCOMPARE(a.toInt(&ok),0);
2805     QVERIFY(!ok);
2806
2807     a="3234567890";
2808     QCOMPARE(a.toInt(),0);
2809     QCOMPARE(a.toInt(&ok),0);
2810     QVERIFY(!ok);
2811
2812     a = "aaa12345aaa";
2813     QCOMPARE(a.toInt(),0);
2814     QCOMPARE(a.toInt(&ok),0);
2815     QVERIFY(!ok);
2816
2817     a = "aaa12345";
2818     QCOMPARE(a.toInt(),0);
2819     QCOMPARE(a.toInt(&ok),0);
2820     QVERIFY(!ok);
2821
2822     a = "12345aaa";
2823     QCOMPARE(a.toInt(),0);
2824     QCOMPARE(a.toInt(&ok),0);
2825     QVERIFY(!ok);
2826
2827     a = "2147483647"; // 2**31 - 1
2828     QCOMPARE(a.toInt(),2147483647);
2829     QCOMPARE(a.toInt(&ok),2147483647);
2830     QVERIFY(ok);
2831
2832     if (sizeof(int) == 4) {
2833         a = "-2147483647"; // -(2**31 - 1)
2834         QCOMPARE(a.toInt(),-2147483647);
2835         QCOMPARE(a.toInt(&ok),-2147483647);
2836         QVERIFY(ok);
2837
2838         a = "2147483648"; // 2**31
2839         QCOMPARE(a.toInt(),0);
2840         QCOMPARE(a.toInt(&ok),0);
2841         QVERIFY(!ok);
2842
2843         a = "-2147483648"; // -2**31
2844         QCOMPARE(a.toInt(),-2147483647 - 1);
2845         QCOMPARE(a.toInt(&ok),-2147483647 - 1);
2846         QVERIFY(ok);
2847
2848         a = "2147483649"; // 2**31 + 1
2849         QCOMPARE(a.toInt(),0);
2850         QCOMPARE(a.toInt(&ok),0);
2851         QVERIFY(!ok);
2852     }
2853 }
2854
2855 void tst_QString::toUInt()
2856 {
2857     bool ok;
2858     QString a;
2859     a="3234567890";
2860     QCOMPARE(a.toUInt(&ok),3234567890u);
2861     QVERIFY(ok);
2862
2863     a = "-50";
2864     QCOMPARE(a.toUInt(),0u);
2865     QCOMPARE(a.toUInt(&ok),0u);
2866     QVERIFY(!ok);
2867
2868     a = "4294967295"; // 2**32 - 1
2869     QCOMPARE(a.toUInt(),4294967295u);
2870     QCOMPARE(a.toUInt(&ok),4294967295u);
2871     QVERIFY(ok);
2872
2873     if (sizeof(int) == 4) {
2874         a = "4294967296"; // 2**32
2875         QCOMPARE(a.toUInt(),0u);
2876         QCOMPARE(a.toUInt(&ok),0u);
2877         QVERIFY(!ok);
2878     }
2879 }
2880
2881 ///////////////////////////// to*Long //////////////////////////////////////
2882
2883 void tst_QString::toULong_data()
2884 {
2885     QTest::addColumn<QString>("str" );
2886     QTest::addColumn<int>("base" );
2887     QTest::addColumn<ulong>("result" );
2888     QTest::addColumn<bool>("ok" );
2889
2890     QTest::newRow( "default" ) << QString() << 10 << 0UL << false;
2891     QTest::newRow( "empty" ) << QString("") << 10 << 0UL << false;
2892     QTest::newRow( "ulong1" ) << QString("3234567890") << 10 << 3234567890UL << true;
2893     QTest::newRow( "ulong2" ) << QString("fFFfFfFf") << 16 << 0xFFFFFFFFUL << true;
2894 }
2895
2896 void tst_QString::toULong()
2897 {
2898     QFETCH( QString, str );
2899     QFETCH( int, base );
2900     QFETCH( ulong, result );
2901     QFETCH( bool, ok );
2902
2903     bool b;
2904     QCOMPARE( str.toULong( 0, base ), result );
2905     QCOMPARE( str.toULong( &b, base ), result );
2906     QCOMPARE( b, ok );
2907 }
2908
2909 void tst_QString::toLong_data()
2910 {
2911     QTest::addColumn<QString>("str" );
2912     QTest::addColumn<int>("base" );
2913     QTest::addColumn<long>("result" );
2914     QTest::addColumn<bool>("ok" );
2915
2916     QTest::newRow( "default" ) << QString() << 10 << 0L << false;
2917     QTest::newRow( "empty" ) << QString("") << 10 << 0L << false;
2918     QTest::newRow( "normal" ) << QString("7fFFfFFf") << 16 << 0x7fFFfFFfL << true;
2919     QTest::newRow( "long_max" ) << QString("2147483647") << 10 << 2147483647L << true;
2920     if (sizeof(long) == 4) {
2921         QTest::newRow( "long_max+1" ) << QString("2147483648") << 10 << 0L << false;
2922         QTest::newRow( "long_min-1" ) << QString("-80000001") << 16 << 0L << false;
2923     }
2924     QTest::newRow( "negative" ) << QString("-7fffffff") << 16 << -0x7fffffffL << true;
2925 //    QTest::newRow( "long_min" ) << QString("-80000000") << 16 << 0x80000000uL << true;
2926 }
2927
2928 void tst_QString::toLong()
2929 {
2930     QFETCH( QString, str );
2931     QFETCH( int, base );
2932     QFETCH( long, result );
2933     QFETCH( bool, ok );
2934
2935     bool b;
2936     QCOMPARE( str.toLong( 0, base ), result );
2937     QCOMPARE( str.toLong( &b, base ), result );
2938     QCOMPARE( b, ok );
2939 }
2940
2941
2942 ////////////////////////// to*LongLong //////////////////////////////////////
2943
2944 void tst_QString::toULongLong()
2945 {
2946     QString str;
2947     bool ok;
2948     str = "18446744073709551615"; // ULLONG_MAX
2949     QCOMPARE( str.toULongLong( 0 ), Q_UINT64_C(18446744073709551615) );
2950     QCOMPARE( str.toULongLong( &ok ), Q_UINT64_C(18446744073709551615) );
2951     QVERIFY( ok );
2952
2953     str = "18446744073709551616"; // ULLONG_MAX + 1
2954     QCOMPARE( str.toULongLong( 0 ), Q_UINT64_C(0) );
2955     QCOMPARE( str.toULongLong( &ok ), Q_UINT64_C(0) );
2956     QVERIFY( !ok );
2957
2958     str = "-150";
2959     QCOMPARE( str.toULongLong( 0 ), Q_UINT64_C(0) );
2960     QCOMPARE( str.toULongLong( &ok ), Q_UINT64_C(0) );
2961     QVERIFY( !ok );
2962 }
2963
2964 void tst_QString::toLongLong()
2965 {
2966     QString str;
2967     bool ok;
2968
2969     str = "9223372036854775807"; // LLONG_MAX
2970     QCOMPARE( str.toLongLong( 0 ), Q_INT64_C(9223372036854775807) );
2971     QCOMPARE( str.toLongLong( &ok ), Q_INT64_C(9223372036854775807) );
2972     QVERIFY( ok );
2973
2974     str = "-9223372036854775808"; // LLONG_MIN
2975     QCOMPARE( str.toLongLong( 0 ),
2976              -Q_INT64_C(9223372036854775807) - Q_INT64_C(1) );
2977     QCOMPARE( str.toLongLong( &ok ),
2978              -Q_INT64_C(9223372036854775807) - Q_INT64_C(1) );
2979     QVERIFY( ok );
2980
2981     str = "aaaa9223372036854775807aaaa";
2982     QCOMPARE( str.toLongLong( 0 ), Q_INT64_C(0) );
2983     QCOMPARE( str.toLongLong( &ok ), Q_INT64_C(0) );
2984     QVERIFY( !ok );
2985
2986     str = "9223372036854775807aaaa";
2987     QCOMPARE( str.toLongLong( 0 ), Q_INT64_C(0) );
2988     QCOMPARE( str.toLongLong( &ok ), Q_INT64_C(0) );
2989     QVERIFY( !ok );
2990
2991     str = "aaaa9223372036854775807";
2992     QCOMPARE( str.toLongLong( 0 ), Q_INT64_C(0) );
2993     QCOMPARE( str.toLongLong( &ok ), Q_INT64_C(0) );
2994     QVERIFY( !ok );
2995
2996     static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2997
2998     for (int i = 0; i < 36; ++i) {
2999         for (int j = 0; j < 36; ++j) {
3000             for (int k = 0; k < 36; ++k) {
3001                 QString str;
3002                 str += QChar(digits[i]);
3003                 str += QChar(digits[j]);
3004                 str += QChar(digits[k]);
3005                 qlonglong value = (((i * 36) + j) * 36) + k;
3006                 QVERIFY(str.toLongLong(0, 36) == value);
3007             }
3008         }
3009     }
3010 }
3011
3012 ////////////////////////////////////////////////////////////////////////////
3013
3014 void tst_QString::toFloat()
3015 {
3016     QString a;
3017     bool ok;
3018     a="0.000000000931322574615478515625";
3019     QCOMPARE(a.toFloat(&ok),(float)(0.000000000931322574615478515625));
3020     QVERIFY(ok);
3021 }
3022
3023 void tst_QString::toDouble_data()
3024 {
3025     QTest::addColumn<QString>("str" );
3026     QTest::addColumn<double>("result" );
3027     QTest::addColumn<bool>("result_ok" );
3028
3029     QTest::newRow( "ok00" ) << QString("0.000000000931322574615478515625") << 0.000000000931322574615478515625 << true;
3030     QTest::newRow( "ok01" ) << QString(" 123.45") << 123.45 << true;
3031
3032     QTest::newRow( "ok02" ) << QString("0.1e10") << 0.1e10 << true;
3033     QTest::newRow( "ok03" ) << QString("0.1e-10") << 0.1e-10 << true;
3034
3035     QTest::newRow( "ok04" ) << QString("1e10") << 1.0e10 << true;
3036     QTest::newRow( "ok05" ) << QString("1e+10") << 1.0e10 << true;
3037     QTest::newRow( "ok06" ) << QString("1e-10") << 1.0e-10 << true;
3038
3039     QTest::newRow( "ok07" ) << QString(" 1e10") << 1.0e10 << true;
3040     QTest::newRow( "ok08" ) << QString("  1e+10") << 1.0e10 << true;
3041     QTest::newRow( "ok09" ) << QString("   1e-10") << 1.0e-10 << true;
3042
3043     QTest::newRow( "ok10" ) << QString("1.") << 1.0 << true;
3044     QTest::newRow( "ok11" ) << QString(".1") << 0.1 << true;
3045
3046     QTest::newRow( "wrong00" ) << QString("123.45 ") << 123.45 << true;
3047     QTest::newRow( "wrong01" ) << QString(" 123.45 ") << 123.45 << true;
3048
3049     QTest::newRow( "wrong02" ) << QString("aa123.45aa") << 0.0 << false;
3050     QTest::newRow( "wrong03" ) << QString("123.45aa") << 0.0 << false;
3051     QTest::newRow( "wrong04" ) << QString("123erf") << 0.0 << false;
3052
3053     QTest::newRow( "wrong05" ) << QString("abc") << 0.0 << false;
3054     QTest::newRow( "wrong06" ) << QString() << 0.0 << false;
3055     QTest::newRow( "wrong07" ) << QString("") << 0.0 << false;
3056 }
3057
3058 void tst_QString::toDouble()
3059 {
3060     QFETCH( QString, str );
3061     QFETCH( bool, result_ok );
3062     bool ok;
3063     double d = str.toDouble( &ok );
3064     if ( result_ok ) {
3065         QTEST( d, "result" );
3066         QVERIFY( ok );
3067     } else {
3068         QVERIFY( !ok );
3069     }
3070 }
3071
3072 void tst_QString::setNum()
3073 {
3074     QString a;
3075     QCOMPARE(a.setNum(123),(QString)"123");
3076     QCOMPARE(a.setNum(-123),(QString)"-123");
3077     QCOMPARE(a.setNum(0x123,16),(QString)"123");
3078     QCOMPARE(a.setNum((short)123),(QString)"123");
3079     QCOMPARE(a.setNum(123L),(QString)"123");
3080     QCOMPARE(a.setNum(123UL),(QString)"123");
3081     QCOMPARE(a.setNum(2147483647L), QString("2147483647")); // 32 bit LONG_MAX
3082     QCOMPARE(a.setNum(-2147483647L), QString("-2147483647")); // LONG_MIN + 1
3083     QCOMPARE(a.setNum(-2147483647L-1L), QString("-2147483648")); // LONG_MIN
3084     QCOMPARE(a.setNum(1.23), QString("1.23"));
3085     QCOMPARE(a.setNum(1.234567), QString("1.23457"));
3086 #if defined(LONG_MAX) && defined(LLONG_MAX) && LONG_MAX == LLONG_MAX
3087     // LONG_MAX and LONG_MIN on 64 bit systems
3088     QCOMPARE(a.setNum(9223372036854775807L), QString("9223372036854775807"));
3089     QCOMPARE(a.setNum(-9223372036854775807L-1L), QString("-9223372036854775808"));
3090     QCOMPARE(a.setNum(18446744073709551615UL), QString("18446744073709551615"));
3091 #endif
3092     QCOMPARE(a.setNum(Q_INT64_C(123)), QString("123"));
3093     // 2^40 == 1099511627776
3094     QCOMPARE(a.setNum(Q_INT64_C(-1099511627776)), QString("-1099511627776"));
3095     QCOMPARE(a.setNum(Q_UINT64_C(1099511627776)), QString("1099511627776"));
3096     QCOMPARE(a.setNum(Q_INT64_C(9223372036854775807)), // LLONG_MAX
3097             QString("9223372036854775807"));
3098     QCOMPARE(a.setNum(-Q_INT64_C(9223372036854775807) - Q_INT64_C(1)),
3099             QString("-9223372036854775808"));
3100     QCOMPARE(a.setNum(Q_UINT64_C(18446744073709551615)), // ULLONG_MAX
3101             QString("18446744073709551615"));
3102     QCOMPARE(a.setNum(0.000000000931322574615478515625),QString("9.31323e-10"));
3103
3104 //  QCOMPARE(a.setNum(0.000000000931322574615478515625,'g',30),(QString)"9.31322574615478515625e-010");
3105 //  QCOMPARE(a.setNum(0.000000000931322574615478515625,'f',30),(QString)"0.00000000093132257461547852");
3106 }
3107
3108 void tst_QString::startsWith()
3109 {
3110     QString a;
3111     a = "AB";
3112     QVERIFY( a.startsWith("A") );
3113     QVERIFY( a.startsWith("AB") );
3114     QVERIFY( !a.startsWith("C") );
3115     QVERIFY( !a.startsWith("ABCDEF") );
3116     QVERIFY( a.startsWith("") );
3117     QVERIFY( a.startsWith(QString::null) );
3118     QVERIFY( a.startsWith('A') );
3119     QVERIFY( a.startsWith(QLatin1Char('A')) );
3120     QVERIFY( a.startsWith(QChar('A')) );
3121     QVERIFY( !a.startsWith('C') );
3122     QVERIFY( !a.startsWith(QChar()) );
3123     QVERIFY( !a.startsWith(QLatin1Char(0)) );
3124
3125     QVERIFY( a.startsWith(QLatin1String("A")) );
3126     QVERIFY( a.startsWith(QLatin1String("AB")) );
3127     QVERIFY( !a.startsWith(QLatin1String("C")) );
3128     QVERIFY( !a.startsWith(QLatin1String("ABCDEF")) );
3129     QVERIFY( a.startsWith(QLatin1String("")) );
3130     QVERIFY( a.startsWith(QLatin1String(0)) );
3131
3132     QVERIFY( a.startsWith("A", Qt::CaseSensitive) );
3133     QVERIFY( a.startsWith("A", Qt::CaseInsensitive) );
3134     QVERIFY( !a.startsWith("a", Qt::CaseSensitive) );
3135     QVERIFY( a.startsWith("a", Qt::CaseInsensitive) );
3136     QVERIFY( !a.startsWith("aB", Qt::CaseSensitive) );
3137     QVERIFY( a.startsWith("aB", Qt::CaseInsensitive) );
3138     QVERIFY( !a.startsWith("C", Qt::CaseSensitive) );
3139     QVERIFY( !a.startsWith("C", Qt::CaseInsensitive) );
3140     QVERIFY( !a.startsWith("c", Qt::CaseSensitive) );
3141     QVERIFY( !a.startsWith("c", Qt::CaseInsensitive) );
3142     QVERIFY( !a.startsWith("abcdef", Qt::CaseInsensitive) );
3143     QVERIFY( a.startsWith("", Qt::CaseInsensitive) );
3144     QVERIFY( a.startsWith(QString::null, Qt::CaseInsensitive) );
3145     QVERIFY( a.startsWith('a', Qt::CaseInsensitive) );
3146     QVERIFY( a.startsWith('A', Qt::CaseInsensitive) );
3147     QVERIFY( a.startsWith(QLatin1Char('a'), Qt::CaseInsensitive) );
3148     QVERIFY( a.startsWith(QChar('a'), Qt::CaseInsensitive) );
3149     QVERIFY( !a.startsWith('c', Qt::CaseInsensitive) );
3150     QVERIFY( !a.startsWith(QChar(), Qt::CaseInsensitive) );
3151     QVERIFY( !a.startsWith(QLatin1Char(0), Qt::CaseInsensitive) );
3152
3153     QVERIFY( a.startsWith(QLatin1String("A"), Qt::CaseSensitive) );
3154     QVERIFY( a.startsWith(QLatin1String("A"), Qt::CaseInsensitive) );
3155     QVERIFY( !a.startsWith(QLatin1String("a"), Qt::CaseSensitive) );
3156     QVERIFY( a.startsWith(QLatin1String("a"), Qt::CaseInsensitive) );
3157     QVERIFY( !a.startsWith(QLatin1String("aB"), Qt::CaseSensitive) );
3158     QVERIFY( a.startsWith(QLatin1String("aB"), Qt::CaseInsensitive) );
3159     QVERIFY( !a.startsWith(QLatin1String("C"), Qt::CaseSensitive) );
3160     QVERIFY( !a.startsWith(QLatin1String("C"), Qt::CaseInsensitive) );
3161     QVERIFY( !a.startsWith(QLatin1String("c"), Qt::CaseSensitive) );
3162     QVERIFY( !a.startsWith(QLatin1String("c"), Qt::CaseInsensitive) );
3163     QVERIFY( !a.startsWith(QLatin1String("abcdef"), Qt::CaseInsensitive) );
3164     QVERIFY( a.startsWith(QLatin1String(""), Qt::CaseInsensitive) );
3165     QVERIFY( a.startsWith(QLatin1String(0), Qt::CaseInsensitive) );
3166     QVERIFY( a.startsWith('A', Qt::CaseSensitive) );
3167     QVERIFY( a.startsWith(QLatin1Char('A'), Qt::CaseSensitive) );
3168     QVERIFY( a.startsWith(QChar('A'), Qt::CaseSensitive) );
3169     QVERIFY( !a.startsWith('a', Qt::CaseSensitive) );
3170     QVERIFY( !a.startsWith(QChar(), Qt::CaseSensitive) );
3171     QVERIFY( !a.startsWith(QLatin1Char(0), Qt::CaseSensitive) );
3172
3173 #define TEST_REF_STARTS_WITH(string, yes) { CREATE_REF(string); QCOMPARE(a.startsWith(ref), yes); }
3174
3175     TEST_REF_STARTS_WITH("A", true);
3176     TEST_REF_STARTS_WITH("AB", true);
3177     TEST_REF_STARTS_WITH("C", false);
3178     TEST_REF_STARTS_WITH("ABCDEF", false);
3179 #undef TEST_REF_STARTS_WITH
3180
3181     a = "";
3182     QVERIFY( a.startsWith("") );
3183     QVERIFY( a.startsWith(QString::null) );
3184     QVERIFY( !a.startsWith("ABC") );
3185
3186     QVERIFY( a.startsWith(QLatin1String("")) );
3187     QVERIFY( a.startsWith(QLatin1String(0)) );
3188     QVERIFY( !a.startsWith(QLatin1String("ABC")) );
3189
3190     QVERIFY( !a.startsWith(QLatin1Char(0)) );
3191     QVERIFY( !a.startsWith(QLatin1Char('x')) );
3192     QVERIFY( !a.startsWith(QChar()) );
3193
3194     a = QString::null;
3195     QVERIFY( !a.startsWith("") );
3196     QVERIFY( a.startsWith(QString::null) );
3197     QVERIFY( !a.startsWith("ABC") );
3198
3199     QVERIFY( !a.startsWith(QLatin1String("")) );
3200     QVERIFY( a.startsWith(QLatin1String(0)) );
3201     QVERIFY( !a.startsWith(QLatin1String("ABC")) );
3202
3203     QVERIFY( !a.startsWith(QLatin1Char(0)) );
3204     QVERIFY( !a.startsWith(QLatin1Char('x')) );
3205     QVERIFY( !a.startsWith(QChar()) );
3206
3207     // this test is independent of encoding
3208     a = "\xc3\xa9";
3209     QVERIFY( a.startsWith("\xc3\xa9") );
3210     QVERIFY( !a.startsWith("\xc3\xa1") );
3211
3212     // this one is dependent of encoding
3213     QVERIFY( a.startsWith("\xc3\x89", Qt::CaseInsensitive) );
3214 }
3215
3216 void tst_QString::endsWith()
3217 {
3218     QString a;
3219     a = "AB";
3220     QVERIFY( a.endsWith("B") );
3221     QVERIFY( a.endsWith("AB") );
3222     QVERIFY( !a.endsWith("C") );
3223     QVERIFY( !a.endsWith("ABCDEF") );
3224     QVERIFY( a.endsWith("") );
3225     QVERIFY( a.endsWith(QString::null) );
3226     QVERIFY( a.endsWith('B') );
3227     QVERIFY( a.endsWith(QLatin1Char('B')) );
3228     QVERIFY( a.endsWith(QChar('B')) );
3229     QVERIFY( !a.endsWith('C') );
3230     QVERIFY( !a.endsWith(QChar()) );
3231     QVERIFY( !a.endsWith(QLatin1Char(0)) );
3232
3233     QVERIFY( a.endsWith(QLatin1String("B")) );
3234     QVERIFY( a.endsWith(QLatin1String("AB")) );
3235     QVERIFY( !a.endsWith(QLatin1String("C")) );
3236     QVERIFY( !a.endsWith(QLatin1String("ABCDEF")) );
3237     QVERIFY( a.endsWith(QLatin1String("")) );
3238     QVERIFY( a.endsWith(QLatin1String(0)) );
3239
3240     QVERIFY( a.endsWith("B", Qt::CaseSensitive) );
3241     QVERIFY( a.endsWith("B", Qt::CaseInsensitive) );
3242     QVERIFY( !a.endsWith("b", Qt::CaseSensitive) );
3243     QVERIFY( a.endsWith("b", Qt::CaseInsensitive) );
3244     QVERIFY( !a.endsWith("aB", Qt::CaseSensitive) );
3245     QVERIFY( a.endsWith("aB", Qt::CaseInsensitive) );
3246     QVERIFY( !a.endsWith("C", Qt::CaseSensitive) );
3247     QVERIFY( !a.endsWith("C", Qt::CaseInsensitive) );
3248     QVERIFY( !a.endsWith("c", Qt::CaseSensitive) );
3249     QVERIFY( !a.endsWith("c", Qt::CaseInsensitive) );
3250     QVERIFY( !a.endsWith("abcdef", Qt::CaseInsensitive) );
3251     QVERIFY( a.endsWith("", Qt::CaseInsensitive) );
3252     QVERIFY( a.endsWith(QString::null, Qt::CaseInsensitive) );
3253     QVERIFY( a.endsWith('b', Qt::CaseInsensitive) );
3254     QVERIFY( a.endsWith('B', Qt::CaseInsensitive) );
3255     QVERIFY( a.endsWith(QLatin1Char('b'), Qt::CaseInsensitive) );
3256     QVERIFY( a.endsWith(QChar('b'), Qt::CaseInsensitive) );
3257     QVERIFY( !a.endsWith('c', Qt::CaseInsensitive) );
3258     QVERIFY( !a.endsWith(QChar(), Qt::CaseInsensitive) );
3259     QVERIFY( !a.endsWith(QLatin1Char(0), Qt::CaseInsensitive) );
3260
3261     QVERIFY( a.endsWith(QLatin1String("B"), Qt::CaseSensitive) );
3262     QVERIFY( a.endsWith(QLatin1String("B"), Qt::CaseInsensitive) );
3263     QVERIFY( !a.endsWith(QLatin1String("b"), Qt::CaseSensitive) );
3264     QVERIFY( a.endsWith(QLatin1String("b"), Qt::CaseInsensitive) );
3265     QVERIFY( !a.endsWith(QLatin1String("aB"), Qt::CaseSensitive) );
3266     QVERIFY( a.endsWith(QLatin1String("aB"), Qt::CaseInsensitive) );
3267     QVERIFY( !a.endsWith(QLatin1String("C"), Qt::CaseSensitive) );
3268     QVERIFY( !a.endsWith(QLatin1String("C"), Qt::CaseInsensitive) );
3269     QVERIFY( !a.endsWith(QLatin1String("c"), Qt::CaseSensitive) );
3270     QVERIFY( !a.endsWith(QLatin1String("c"), Qt::CaseInsensitive) );
3271     QVERIFY( !a.endsWith(QLatin1String("abcdef"), Qt::CaseInsensitive) );
3272     QVERIFY( a.endsWith(QLatin1String(""), Qt::CaseInsensitive) );
3273     QVERIFY( a.endsWith(QLatin1String(0), Qt::CaseInsensitive) );
3274     QVERIFY( a.endsWith('B', Qt::CaseSensitive) );
3275     QVERIFY( a.endsWith(QLatin1Char('B'), Qt::CaseSensitive) );
3276     QVERIFY( a.endsWith(QChar('B'), Qt::CaseSensitive) );
3277     QVERIFY( !a.endsWith('b', Qt::CaseSensitive) );
3278     QVERIFY( !a.endsWith(QChar(), Qt::CaseSensitive) );
3279     QVERIFY( !a.endsWith(QLatin1Char(0), Qt::CaseSensitive) );
3280
3281
3282 #define TEST_REF_ENDS_WITH(string, yes) { CREATE_REF(string); QCOMPARE(a.endsWith(ref), yes); }
3283     TEST_REF_ENDS_WITH(QLatin1String("B"), true);
3284     TEST_REF_ENDS_WITH(QLatin1String("AB"), true);
3285     TEST_REF_ENDS_WITH(QLatin1String("C"), false);
3286     TEST_REF_ENDS_WITH(QLatin1String("ABCDEF"), false);
3287     TEST_REF_ENDS_WITH(QLatin1String(""), true);
3288     TEST_REF_ENDS_WITH(QLatin1String(0), true);
3289
3290 #undef TEST_REF_STARTS_WITH
3291
3292     a = "";
3293     QVERIFY( a.endsWith("") );
3294     QVERIFY( a.endsWith(QString::null) );
3295     QVERIFY( !a.endsWith("ABC") );
3296     QVERIFY( !a.endsWith(QLatin1Char(0)) );
3297     QVERIFY( !a.endsWith(QLatin1Char('x')) );
3298     QVERIFY( !a.endsWith(QChar()) );
3299
3300     QVERIFY( a.endsWith(QLatin1String("")) );
3301     QVERIFY( a.endsWith(QLatin1String(0)) );
3302     QVERIFY( !a.endsWith(QLatin1String("ABC")) );
3303
3304     a = QString::null;
3305     QVERIFY( !a.endsWith("") );
3306     QVERIFY( a.endsWith(QString::null) );
3307     QVERIFY( !a.endsWith("ABC") );
3308
3309     QVERIFY( !a.endsWith(QLatin1String("")) );
3310     QVERIFY( a.endsWith(QLatin1String(0)) );
3311     QVERIFY( !a.endsWith(QLatin1String("ABC")) );
3312
3313     QVERIFY( !a.endsWith(QLatin1Char(0)) );
3314     QVERIFY( !a.endsWith(QLatin1Char('x')) );
3315     QVERIFY( !a.endsWith(QChar()) );
3316
3317     // this test is independent of encoding
3318     a = "\xc3\xa9";
3319     QVERIFY( a.endsWith("\xc3\xa9") );
3320     QVERIFY( !a.endsWith("\xc3\xa1") );
3321
3322     // this one is dependent of encoding
3323     QVERIFY( a.endsWith("\xc3\x89", Qt::CaseInsensitive) );
3324 }
3325
3326 void tst_QString::check_QDataStream()
3327 {
3328     QString a;
3329     QByteArray ar;
3330     {
3331         QDataStream out(&ar,QIODevice::WriteOnly);
3332         out << QString("COMPARE Text");
3333     }
3334     {
3335         QDataStream in(&ar,QIODevice::ReadOnly);
3336         in >> a;
3337         QCOMPARE(a,(QString)"COMPARE Text");
3338     }
3339 }
3340
3341 void tst_QString::check_QTextStream()
3342 {
3343     QString a;
3344     QByteArray ar;
3345     {
3346         QTextStream out(&ar,QIODevice::WriteOnly);
3347         out << QString("This is COMPARE Text");
3348     }
3349     {
3350         QTextStream in(&ar,QIODevice::ReadOnly);
3351         in >> a;
3352         QCOMPARE(a,(QString)"This");
3353     }
3354 }
3355
3356 void tst_QString::check_QTextIOStream()
3357 {
3358     QString a;
3359     {
3360         a="";
3361         QTextStream ts(&a);
3362         ts << "pi \261= " << 3.125;
3363         QCOMPARE(a, QString::fromLatin1("pi \261= 3.125"));
3364     }
3365     {
3366         a="123 456";
3367         int x,y;
3368         QTextStream(&a) >> x >> y;
3369         QCOMPARE(x,123);
3370         QCOMPARE(y,456);
3371     }
3372 }
3373
3374 void tst_QString::fromRawData()
3375 {
3376     const QChar ptr[] = { 0x1234, 0x0000 };
3377     QString cstr = QString::fromRawData(ptr, 1);
3378     QVERIFY(cstr.isDetached());
3379     QVERIFY(cstr.constData() == ptr);
3380     QVERIFY(cstr == QString(ptr, 1));
3381     cstr.squeeze();
3382     QVERIFY(cstr.constData() == ptr);
3383     cstr.detach();
3384     QVERIFY(cstr.size() == 1);
3385     QVERIFY(cstr.capacity() == 1);
3386     QVERIFY(cstr.constData() != ptr);
3387     QVERIFY(cstr.constData()[0] == QChar(0x1234));
3388     QVERIFY(cstr.constData()[1] == QChar(0x0000));
3389 }
3390
3391 void tst_QString::setRawData()
3392 {
3393     const QChar ptr[] = { 0x1234, 0x0000 };
3394     const QChar ptr2[] = { 0x4321, 0x0000 };
3395     QString cstr;
3396
3397     // This just tests the fromRawData() fallback
3398     QVERIFY(!cstr.isDetached());
3399     cstr.setRawData(ptr, 1);
3400     QVERIFY(cstr.isDetached());
3401     QVERIFY(cstr.constData() == ptr);
3402     QVERIFY(cstr == QString(ptr, 1));
3403
3404     // This actually tests the recycling of the shared data object
3405     QString::DataPtr csd = cstr.data_ptr();
3406     cstr.setRawData(ptr2, 1);
3407     QVERIFY(cstr.isDetached());
3408     QVERIFY(cstr.constData() == ptr2);
3409     QVERIFY(cstr == QString(ptr2, 1));
3410     QVERIFY(cstr.data_ptr() == csd);
3411
3412     // This tests the discarding of the shared data object
3413     cstr = "foo";
3414     QVERIFY(cstr.isDetached());
3415     QVERIFY(cstr.constData() != ptr2);
3416
3417     // Another test of the fallback
3418     csd = cstr.data_ptr();
3419     cstr.setRawData(ptr2, 1);
3420     QVERIFY(cstr.isDetached());
3421     QVERIFY(cstr.constData() == ptr2);
3422     QVERIFY(cstr == QString(ptr2, 1));
3423     QVERIFY(cstr.data_ptr() != csd);
3424 }
3425
3426 void tst_QString::fromStdString()
3427 {
3428 #ifdef Q_CC_HPACC
3429     QSKIP("This test crashes on HP-UX with aCC");
3430 #endif
3431     std::string stroustrup = "foo";
3432     QString eng = QString::fromStdString( stroustrup );
3433     QCOMPARE( eng, QString("foo") );
3434     const char cnull[] = "Embedded\0null\0character!";
3435     std::string stdnull( cnull, sizeof(cnull)-1 );
3436     QString qtnull = QString::fromStdString( stdnull );
3437     QCOMPARE( qtnull.size(), int(stdnull.size()) );
3438 }
3439
3440 void tst_QString::toStdString()
3441 {
3442 #ifdef Q_CC_HPACC
3443     QSKIP("This test crashes on HP-UX with aCC");
3444 #endif
3445     QString nord = "foo";
3446     std::string stroustrup1 = nord.toStdString();
3447     QVERIFY( qstrcmp(stroustrup1.c_str(), "foo") == 0 );
3448     // For now, most QString constructors are also broken with respect
3449     // to embedded null characters, had to find one that works...
3450     const QChar qcnull[] = {
3451         'E', 'm', 'b', 'e', 'd', 'd', 'e', 'd', '\0',
3452         'n', 'u', 'l', 'l', '\0',
3453         'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', '!'
3454     };
3455     QString qtnull( qcnull, sizeof(qcnull)/sizeof(QChar) );
3456     std::string stdnull = qtnull.toStdString();
3457     QCOMPARE( int(stdnull.size()), qtnull.size() );
3458 }
3459
3460 void tst_QString::utf8()
3461 {
3462     QFETCH( QByteArray, utf8 );
3463     QFETCH( QString, res );
3464
3465     QCOMPARE(res.toUtf8(), utf8);
3466 }
3467
3468 void tst_QString::stringRef_utf8_data()
3469 {
3470     utf8_data();
3471 }
3472
3473 void tst_QString::stringRef_utf8()
3474 {
3475     QFETCH( QByteArray, utf8 );
3476     QFETCH( QString, res );
3477
3478     QStringRef ref(&res, 0, res.length());
3479     QCOMPARE( utf8, QByteArray(ref.toUtf8()) );
3480 }
3481
3482 // copied to tst_QTextCodec::utf8Codec_data()
3483 void tst_QString::fromUtf8_data()
3484 {
3485     QTest::addColumn<QByteArray>("utf8");
3486     QTest::addColumn<QString>("res");
3487     QTest::addColumn<int>("len");
3488     QString str;
3489
3490     QTest::newRow("str0") << QByteArray("abcdefgh") << QString("abcdefgh") << -1;
3491     QTest::newRow("str0-len") << QByteArray("abcdefgh") << QString("abc") << 3;
3492     QTest::newRow("str1") << QByteArray("\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205")
3493                           << QString::fromLatin1("\366\344\374\326\304\334\370\346\345\330\306\305") << -1;
3494     QTest::newRow("str1-len") << QByteArray("\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205")
3495                               << QString::fromLatin1("\366\344\374\326\304") << 10;
3496
3497     str += QChar(0x05e9);
3498     str += QChar(0x05d3);
3499     str += QChar(0x05d2);
3500     QTest::newRow("str2") << QByteArray("\327\251\327\223\327\222") << str << -1;
3501
3502     str = QChar(0x05e9);
3503     QTest::newRow("str2-len") << QByteArray("\327\251\327\223\327\222") << str << 2;
3504
3505     str = QChar(0x20ac);
3506     str += " some text";
3507     QTest::newRow("str3") << QByteArray("\342\202\254 some text") << str << -1;
3508
3509     str = QChar(0x20ac);
3510     str += " some ";
3511     QTest::newRow("str3-len") << QByteArray("\342\202\254 some text") << str << 9;
3512
3513     str = "hello";
3514     str += QChar::ReplacementCharacter;
3515     str += QChar(0x68);
3516     str += QChar::ReplacementCharacter;
3517     str += QChar::ReplacementCharacter;
3518     str += QChar::ReplacementCharacter;
3519     str += QChar::ReplacementCharacter;
3520     str += QChar(0x61);
3521     str += QChar::ReplacementCharacter;
3522     QTest::newRow("invalid utf8") << QByteArray("hello\344h\344\344\366\344a\304") << str << -1;
3523     QTest::newRow("invalid utf8-len") << QByteArray("hello\344h\344\344\366\344a\304") << QString("hello") << 5;
3524
3525     str = "Prohl";
3526     str += QChar::ReplacementCharacter;
3527     str += QChar::ReplacementCharacter;
3528     str += "e";
3529     str += QChar::ReplacementCharacter;
3530     str += " plugin";
3531     str += QChar::ReplacementCharacter;
3532     str += " Netscape";
3533
3534     QTest::newRow("invalid utf8 2") << QByteArray("Prohl\355\276e\350 plugin\371 Netscape") << str << -1;
3535     QTest::newRow("invalid utf8-len 2") << QByteArray("Prohl\355\276e\350 plugin\371 Netscape") << QString("") << 0;
3536
3537     QTest::newRow("null-1") << QByteArray() << QString() << -1;
3538     QTest::newRow("null0") << QByteArray() << QString() << 0;
3539     QTest::newRow("null5") << QByteArray() << QString() << 5;
3540     QTest::newRow("empty-1") << QByteArray("\0abcd", 5) << QString() << -1;
3541     QTest::newRow("empty0") << QByteArray() << QString() << 0;
3542     QTest::newRow("empty5") << QByteArray("\0abcd", 5) << QString::fromLatin1("\0abcd", 5) << 5;
3543     QTest::newRow("other-1") << QByteArray("ab\0cd", 5) << QString::fromLatin1("ab") << -1;
3544     QTest::newRow("other5") << QByteArray("ab\0cd", 5) << QString::fromLatin1("ab\0cd", 5) << 5;
3545
3546     str = "Old Italic: ";
3547     str += QChar(0xd800);
3548     str += QChar(0xdf00);
3549     str += QChar(0xd800);
3550     str += QChar(0xdf01);
3551     str += QChar(0xd800);
3552     str += QChar(0xdf02);
3553     str += QChar(0xd800);
3554     str += QChar(0xdf03);
3555     str += QChar(0xd800);
3556     str += QChar(0xdf04);
3557     QTest::newRow("surrogate") << QByteArray("Old Italic: \360\220\214\200\360\220\214\201\360\220\214\202\360\220\214\203\360\220\214\204") << str << -1;
3558
3559     QTest::newRow("surrogate-len") << QByteArray("Old Italic: \360\220\214\200\360\220\214\201\360\220\214\202\360\220\214\203\360\220\214\204") << str.left(16) << 20;
3560
3561 }
3562
3563 void tst_QString::fromUtf8()
3564 {
3565     QFETCH(QByteArray, utf8);
3566     QFETCH(QString, res);
3567     QFETCH(int, len);
3568
3569     QCOMPARE(QString::fromUtf8(utf8.isNull() ? 0 : utf8.data(), len), res);
3570 }
3571
3572 void tst_QString::nullFromUtf8()
3573 {
3574     QString a;
3575     a = QString::fromUtf8(0);
3576     QVERIFY(a.isNull());
3577     QVERIFY(a.isEmpty());
3578     a = QString::fromUtf8("");
3579     QVERIFY(!a.isNull());
3580     QVERIFY(a.isEmpty());
3581 }
3582
3583 void tst_QString::fromLocal8Bit_data()
3584 {
3585     QTest::addColumn<QByteArray>("local8Bit");
3586     QTest::addColumn<int>("len");
3587     QTest::addColumn<QString>("result");
3588
3589     //QTest::newRow("nullString") << QByteArray() << -1 << QString();
3590     //QTest::newRow("emptyString") << QByteArray("") << -1 << QString("");
3591     //QTest::newRow("string") << QByteArray("test") << -1 << QString("test");
3592     //QTest::newRow("stringlen0") << QByteArray("test") << 0 << QString("");
3593     //QTest::newRow("stringlen3") << QByteArray("test") << 3 << QString("tes");
3594     QTest::newRow("stringlen99") << QByteArray("test\0foo", 8) << 8 << QString::fromLatin1("test\0foo", 8);
3595
3596     QByteArray longQByteArray;
3597     QString longQString;
3598
3599     for (int l=0;l<111;l++) {
3600         longQByteArray = longQByteArray + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
3601         longQString += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
3602     }
3603
3604     //QTest::newRow("longString") << longQByteArray << -1 << longQString;
3605     //QTest::newRow("longStringlen0") << longQByteArray << 0 << QString("");
3606     //QTest::newRow("longStringlen3") << longQByteArray << 3 << QString("aaa");
3607     //QTest::newRow("someNonAlphaChars") << QByteArray("d:/this/is/a/test.h") << -1 << QString("d:/this/is/a/test.h");
3608
3609     //QTest::newRow("null-1") << QByteArray() << -1 << QString();
3610     //QTest::newRow("null0") << QByteArray() << 0 << QString();
3611     //QTest::newRow("null5") << QByteArray() << 5 << QString();
3612     //QTest::newRow("empty-1") << QByteArray("\0abcd", 5) << -1 << QString();
3613     //QTest::newRow("empty0") << QByteArray() << 0 << QString();
3614     //QTest::newRow("empty5") << QByteArray("\0abcd", 5) << 5 << QString::fromAscii("\0abcd", 5);
3615     //QTest::newRow("other-1") << QByteArray("ab\0cd", 5) << -1 << QString::fromAscii("ab");
3616     //QTest::newRow("other5") << QByteArray("ab\0cd", 5) << 5 << QString::fromAscii("ab\0cd", 5);
3617 }
3618
3619 void tst_QString::fromLocal8Bit()
3620 {
3621     QFETCH(QByteArray, local8Bit);
3622     QFETCH(int, len);
3623     QFETCH(QString, result);
3624
3625     QCOMPARE(QString::fromLocal8Bit(local8Bit.isNull() ? 0 : local8Bit.data(), len).length(),
3626             result.length());
3627     QCOMPARE(QString::fromLocal8Bit(local8Bit.isNull() ? 0 : local8Bit.data(), len), result);
3628 }
3629
3630 void tst_QString::local8Bit_data()
3631 {
3632     QTest::addColumn<QString>("local8Bit");
3633     QTest::addColumn<QByteArray>("result");
3634
3635 /*
3636     QString::local8Bit() called on a null QString returns an _empty_
3637     QByteArray.
3638 */
3639     QTest::newRow("nullString") << QString() << QByteArray("");
3640     QTest::newRow("emptyString") << QString("") << QByteArray("");
3641     QTest::newRow("string") << QString("test") << QByteArray("test");
3642
3643     QByteArray longQByteArray;
3644     QString longQString;
3645
3646     for (int l=0;l<111;l++) {
3647         longQByteArray = longQByteArray + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
3648         longQString += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
3649     }
3650
3651     QTest::newRow("longString") << longQString << longQByteArray;
3652     QTest::newRow("someNonAlphaChars") << QString("d:/this/is/a/test.h") << QByteArray("d:/this/is/a/test.h");
3653 }
3654
3655 void tst_QString::local8Bit()
3656 {
3657     QFETCH(QString, local8Bit);
3658     QFETCH(QByteArray, result);
3659
3660     QCOMPARE(local8Bit.toLocal8Bit(), QByteArray(result));
3661 }
3662
3663 void tst_QString::stringRef_local8Bit_data()
3664 {
3665     local8Bit_data();
3666 }
3667
3668 void tst_QString::stringRef_local8Bit()
3669 {
3670     QFETCH(QString, local8Bit);
3671     QFETCH(QByteArray, result);
3672
3673     QStringRef ref(&local8Bit, 0, local8Bit.length());
3674     QCOMPARE(ref.toLocal8Bit(), QByteArray(result));
3675 }
3676
3677 void tst_QString::fromLatin1Roundtrip_data()
3678 {
3679     QTest::addColumn<QByteArray>("latin1");
3680     QTest::addColumn<QString>("unicode");
3681
3682     QTest::newRow("null") << QByteArray() << QString();
3683     QTest::newRow("empty") << QByteArray("") << "";
3684
3685     static const ushort unicode1[] = { 'H', 'e', 'l', 'l', 'o', 1, '\r', '\n', 0x7f };
3686     QTest::newRow("ascii-only") << QByteArray("Hello") << QString::fromUtf16(unicode1, 5);
3687     QTest::newRow("ascii+control") << QByteArray("Hello\1\r\n\x7f") << QString::fromUtf16(unicode1, 9);
3688
3689     static const ushort unicode3[] = { 'a', 0, 'z' };
3690     QTest::newRow("ascii+nul") << QByteArray("a\0z", 3) << QString::fromUtf16(unicode3, 3);
3691
3692     static const ushort unicode4[] = { 0x80, 0xc0, 0xff };
3693     QTest::newRow("non-ascii") << QByteArray("\x80\xc0\xff") << QString::fromUtf16(unicode4, 3);
3694 }
3695
3696 void tst_QString::fromLatin1Roundtrip()
3697 {
3698     QFETCH(QByteArray, latin1);
3699     QFETCH(QString, unicode);
3700
3701     // QtTest safety check:
3702     QCOMPARE(latin1.isNull(), unicode.isNull());
3703     QCOMPARE(latin1.isEmpty(), unicode.isEmpty());
3704     QCOMPARE(latin1.length(), unicode.length());
3705
3706     if (!latin1.isEmpty())
3707         while (latin1.length() < 128) {
3708             latin1 += latin1;
3709             unicode += unicode;
3710         }
3711
3712     // fromLatin1
3713     QCOMPARE(QString::fromLatin1(latin1, latin1.length()).length(), unicode.length());
3714     QCOMPARE(QString::fromLatin1(latin1, latin1.length()), unicode);
3715
3716     // and back:
3717     QCOMPARE(unicode.toLatin1().length(), latin1.length());
3718     QCOMPARE(unicode.toLatin1(), latin1);
3719 }
3720
3721 void tst_QString::toLatin1Roundtrip_data()
3722 {
3723     QTest::addColumn<QByteArray>("latin1");
3724     QTest::addColumn<QString>("unicodesrc");
3725     QTest::addColumn<QString>("unicodedst");
3726
3727     QTest::newRow("null") << QByteArray() << QString() << QString();
3728     QTest::newRow("empty") << QByteArray("") << "" << "";
3729
3730     static const ushort unicode1[] = { 'H', 'e', 'l', 'l', 'o', 1, '\r', '\n', 0x7f };
3731     QTest::newRow("ascii-only") << QByteArray("Hello") << QString::fromUtf16(unicode1, 5) << QString::fromUtf16(unicode1, 5);
3732     QTest::newRow("ascii+control") << QByteArray("Hello\1\r\n\x7f") << QString::fromUtf16(unicode1, 9)  << QString::fromUtf16(unicode1, 9);
3733
3734     static const ushort unicode3[] = { 'a', 0, 'z' };
3735     QTest::newRow("ascii+nul") << QByteArray("a\0z", 3) << QString::fromUtf16(unicode3, 3) << QString::fromUtf16(unicode3, 3);
3736
3737     static const ushort unicode4[] = { 0x80, 0xc0, 0xff };
3738     QTest::newRow("non-ascii") << QByteArray("\x80\xc0\xff") << QString::fromUtf16(unicode4, 3) << QString::fromUtf16(unicode4, 3);
3739
3740     static const ushort unicodeq[] = { '?', '?', '?', '?', '?' };
3741     const QString questionmarks = QString::fromUtf16(unicodeq, 5);
3742
3743     static const ushort unicode5[] = { 0x100, 0x101, 0x17f, 0x7f00, 0x7f7f };
3744     QTest::newRow("non-latin1a") << QByteArray("?????") << QString::fromUtf16(unicode5, 5) << questionmarks;
3745
3746     static const ushort unicode6[] = { 0x180, 0x1ff, 0x8001, 0x8080, 0xfffc };
3747     QTest::newRow("non-latin1b") << QByteArray("?????") << QString::fromUtf16(unicode6, 5) << questionmarks;
3748 }
3749
3750 void tst_QString::toLatin1Roundtrip()
3751 {
3752     QFETCH(QByteArray, latin1);
3753     QFETCH(QString, unicodesrc);
3754     QFETCH(QString, unicodedst);
3755
3756     // QtTest safety check:
3757     QCOMPARE(latin1.isNull(), unicodesrc.isNull());
3758     QCOMPARE(latin1.isEmpty(), unicodesrc.isEmpty());
3759     QCOMPARE(latin1.length(), unicodesrc.length());
3760     QCOMPARE(latin1.isNull(), unicodedst.isNull());
3761     QCOMPARE(latin1.isEmpty(), unicodedst.isEmpty());
3762     QCOMPARE(latin1.length(), unicodedst.length());
3763
3764     if (!latin1.isEmpty())
3765         while (latin1.length() < 128) {
3766             latin1 += latin1;
3767             unicodesrc += unicodesrc;
3768             unicodedst += unicodedst;
3769         }
3770
3771     // toLatin1
3772     QCOMPARE(unicodesrc.toLatin1().length(), latin1.length());
3773     QCOMPARE(unicodesrc.toLatin1(), latin1);
3774
3775     // and back:
3776     QCOMPARE(QString::fromLatin1(latin1, latin1.length()).length(), unicodedst.length());
3777     QCOMPARE(QString::fromLatin1(latin1, latin1.length()), unicodedst);
3778 }
3779
3780 void tst_QString::stringRef_toLatin1Roundtrip_data()
3781 {
3782     toLatin1Roundtrip_data();
3783 }
3784
3785 void tst_QString::stringRef_toLatin1Roundtrip()
3786 {
3787     QFETCH(QByteArray, latin1);
3788     QFETCH(QString, unicodesrc);
3789     QFETCH(QString, unicodedst);
3790
3791     // QtTest safety check:
3792     QCOMPARE(latin1.isNull(), unicodesrc.isNull());
3793     QCOMPARE(latin1.isEmpty(), unicodesrc.isEmpty());
3794     QCOMPARE(latin1.length(), unicodesrc.length());
3795     QCOMPARE(latin1.isNull(), unicodedst.isNull());
3796     QCOMPARE(latin1.isEmpty(), unicodedst.isEmpty());
3797     QCOMPARE(latin1.length(), unicodedst.length());
3798
3799     if (!latin1.isEmpty())
3800         while (latin1.length() < 128) {
3801             latin1 += latin1;
3802             unicodesrc += unicodesrc;
3803             unicodedst += unicodedst;
3804         }
3805
3806     // toLatin1
3807     QStringRef src(&unicodesrc, 0, unicodesrc.length());
3808     QCOMPARE(src.toLatin1().length(), latin1.length());
3809     QCOMPARE(src.toLatin1(), latin1);
3810 }
3811
3812 void tst_QString::fromLatin1()
3813 {
3814     QString a;
3815     a = QString::fromLatin1( 0 );
3816     QVERIFY( a.isNull() );
3817     QVERIFY( a.isEmpty() );
3818     a = QString::fromLatin1( "" );
3819     QVERIFY( !a.isNull() );
3820     QVERIFY( a.isEmpty() );
3821
3822     a = QString::fromLatin1(0, 0);
3823     QVERIFY(a.isNull());
3824     a = QString::fromLatin1(0, 5);
3825     QVERIFY(a.isNull());
3826     a = QString::fromLatin1("\0abcd", 0);
3827     QVERIFY(!a.isNull());
3828     QVERIFY(a.isEmpty());
3829     a = QString::fromLatin1("\0abcd", 5);
3830     QVERIFY(a.size() == 5);
3831 }
3832
3833 void tst_QString::fromAscii()
3834 {
3835     QString a;
3836     a = QString::fromAscii( 0 );
3837     QVERIFY( a.isNull() );
3838     QVERIFY( a.isEmpty() );
3839     a = QString::fromAscii( "" );
3840     QVERIFY( !a.isNull() );
3841     QVERIFY( a.isEmpty() );
3842
3843     a = QString::fromAscii(0, 0);
3844     QVERIFY(a.isNull());
3845     a = QString::fromAscii(0, 5);
3846     QVERIFY(a.isNull());
3847     a = QString::fromAscii("\0abcd", 0);
3848     QVERIFY(!a.isNull());
3849     QVERIFY(a.isEmpty());
3850     a = QString::fromAscii("\0abcd", 5);
3851     QVERIFY(a.size() == 5);
3852 }
3853
3854 void tst_QString::arg()
3855 {
3856 /*
3857     Warning: If any of these test fails, the warning given by QtTest
3858     is all messed up, because QtTest itself uses QString::arg().
3859 */
3860
3861     QLocale::setDefault(QString("de_DE"));
3862
3863     QString s4( "[%0]" );
3864     QString s5( "[%1]" );
3865     QString s6( "[%3]" );
3866     QString s7( "[%9]" );
3867     QString s8( "[%0 %1]" );
3868     QString s9( "[%0 %3]" );
3869     QString s10( "[%1 %2 %3]" );
3870     QString s11( "[%9 %3 %0]" );
3871     QString s12( "[%9 %1 %3 %9 %0 %8]" );
3872     QString s13( "%1% %x%c%2 %d%2-%" );
3873     QString s14( "%1%2%3" );
3874
3875     QCOMPARE( s4.arg("foo"), QString("[foo]") );
3876     QCOMPARE( s5.arg("foo"), QString("[foo]") );
3877     QCOMPARE( s6.arg("foo"), QString("[foo]") );
3878     QCOMPARE( s7.arg("foo"), QString("[foo]") );
3879     QCOMPARE( s8.arg("foo"), QString("[foo %1]") );
3880     QCOMPARE( s8.arg("foo").arg("bar"), QString("[foo bar]") );
3881     QCOMPARE( s8.arg("foo", "bar"), QString("[foo bar]") );
3882     QCOMPARE( s9.arg("foo"), QString("[foo %3]") );
3883     QCOMPARE( s9.arg("foo").arg("bar"), QString("[foo bar]") );
3884     QCOMPARE( s9.arg("foo", "bar"), QString("[foo bar]") );
3885     QCOMPARE( s10.arg("foo"), QString("[foo %2 %3]") );
3886     QCOMPARE( s10.arg("foo").arg("bar"), QString("[foo bar %3]") );
3887     QCOMPARE( s10.arg("foo", "bar"), QString("[foo bar %3]") );
3888     QCOMPARE( s10.arg("foo").arg("bar").arg("baz"), QString("[foo bar baz]") );
3889     QCOMPARE( s10.arg("foo", "bar", "baz"), QString("[foo bar baz]") );
3890     QCOMPARE( s11.arg("foo"), QString("[%9 %3 foo]") );
3891     QCOMPARE( s11.arg("foo").arg("bar"), QString("[%9 bar foo]") );
3892     QCOMPARE( s11.arg("foo", "bar"), QString("[%9 bar foo]") );
3893     QCOMPARE( s11.arg("foo").arg("bar").arg("baz"), QString("[baz bar foo]") );
3894     QCOMPARE( s11.arg("foo", "bar", "baz"), QString("[baz bar foo]") );
3895     QCOMPARE( s12.arg("a").arg("b").arg("c").arg("d").arg("e"),
3896              QString("[e b c e a d]") );
3897     QCOMPARE( s12.arg("a", "b", "c", "d").arg("e"), QString("[e b c e a d]") );
3898     QCOMPARE( s12.arg("a").arg("b", "c", "d", "e"), QString("[e b c e a d]") );
3899     QCOMPARE( s13.arg("alpha").arg("beta"),
3900              QString("alpha% %x%cbeta %dbeta-%") );
3901     QCOMPARE( s13.arg("alpha", "beta"), QString("alpha% %x%cbeta %dbeta-%") );
3902     QCOMPARE( s14.arg("a", "b", "c"), QString("abc") );
3903     QCOMPARE( s8.arg("%1").arg("foo"), QString("[foo foo]") );
3904     QCOMPARE( s8.arg("%1", "foo"), QString("[%1 foo]") );
3905     QCOMPARE( s4.arg("foo", 2), QString("[foo]") );
3906     QCOMPARE( s4.arg("foo", -2), QString("[foo]") );
3907     QCOMPARE( s4.arg("foo", 10), QString("[       foo]") );
3908     QCOMPARE( s4.arg("foo", -10), QString("[foo       ]") );
3909
3910     QString firstName( "James" );
3911     QString lastName( "Bond" );
3912     QString fullName = QString( "My name is %2, %1 %2" )
3913                        .arg( firstName ).arg( lastName );
3914     QCOMPARE( fullName, QString("My name is Bond, James Bond") );
3915
3916     // number overloads
3917     QCOMPARE( s4.arg(0), QString("[0]") );
3918     QCOMPARE( s4.arg(-1), QString("[-1]") );
3919     QCOMPARE( s4.arg(4294967295UL), QString("[4294967295]") ); // ULONG_MAX 32
3920     QCOMPARE( s4.arg(Q_INT64_C(9223372036854775807)), // LLONG_MAX
3921              QString("[9223372036854775807]") );
3922
3923     QCOMPARE( QString().arg(0), QString() );
3924     QCOMPARE( QString("").arg(0), QString("") );
3925     QCOMPARE( QString(" ").arg(0), QString(" ") );
3926     QCOMPARE( QString("%").arg(0), QString("%") );
3927     QCOMPARE( QString("%%").arg(0), QString("%%") );
3928     QCOMPARE( QString("%%%").arg(0), QString("%%%") );
3929     QCOMPARE( QString("%%%1%%%2").arg("foo").arg("bar"), QString("%%foo%%bar") );
3930
3931     QCOMPARE( QString("%1").arg("hello", -10), QString("hello     ") );
3932     QCOMPARE( QString("%1").arg("hello", -5), QString("hello") );
3933     QCOMPARE( QString("%1").arg("hello", -2), QString("hello") );
3934     QCOMPARE( QString("%1").arg("hello", 0), QString("hello") );
3935     QCOMPARE( QString("%1").arg("hello", 2), QString("hello") );
3936     QCOMPARE( QString("%1").arg("hello", 5), QString("hello") );
3937     QCOMPARE( QString("%1").arg("hello", 10), QString("     hello") );
3938     QCOMPARE( QString("%1%1").arg("hello"), QString("hellohello") );
3939     QCOMPARE( QString("%2%1").arg("hello"), QString("%2hello") );
3940     QCOMPARE( QString("%1%1").arg(QString::null), QString("") );
3941     QCOMPARE( QString("%2%1").arg(""), QString("%2") );
3942
3943     QCOMPARE( QString("%2 %L1").arg(12345.6789).arg(12345.6789),
3944              QString("12345.7 12.345,7") );
3945     QCOMPARE( QString("%2 %L1").arg(123456789).arg(123456789),
3946              QString("123456789 123.456.789") );
3947     QCOMPARE( QString("%L2 %L1 %3").arg(12345.7).arg(123456789).arg('c'),
3948              QString("123.456.789 12.345,7 c") );
3949
3950     // multi-digit replacement
3951     QString input("%%%L0 %1 %02 %3 %4 %5 %L6 %7 %8 %%% %090 %10 %11 %L12 %14 %L9888 %9999 %%%%%%%L");
3952     input = input.arg("A").arg("B").arg("C")
3953                  .arg("D").arg("E").arg("f")
3954                  .arg("g").arg("h").arg("i").arg("j")
3955                  .arg("k").arg("l").arg("m")
3956                  .arg("n").arg("o").arg("p");
3957
3958     QCOMPARE(input, QString("%%A B C D E f g h i %%% j0 k l m n o88 p99 %%%%%%%L"));
3959
3960     QString str("%1 %2 %3 %4 %5 %6 %7 %8 %9 foo %10 %11 bar");
3961     str = str.arg("one", "2", "3", "4", "5", "6", "7", "8", "9");
3962     str = str.arg("ahoy", "there");
3963     QCOMPARE(str, QString("one 2 3 4 5 6 7 8 9 foo ahoy there bar"));
3964
3965     QString str2("%123 %234 %345 %456 %567 %999 %1000 %1230");
3966     str2 = str2.arg("A", "B", "C", "D", "E", "F");
3967     QCOMPARE(str2, QString("A B C D E F %1000 %1230"));
3968
3969     QCOMPARE(QString("%1").arg(-1, 3, 10, QChar('0')), QString("-01"));
3970     QCOMPARE(QString("%1").arg(-100, 3, 10, QChar('0')), QString("-100"));
3971     QCOMPARE(QString("%1").arg(-1, 3, 10, QChar(' ')), QString(" -1"));
3972     QCOMPARE(QString("%1").arg(-100, 3, 10, QChar(' ')), QString("-100"));
3973     QCOMPARE(QString("%1").arg(1U, 3, 10, QChar(' ')), QString("  1"));
3974     QCOMPARE(QString("%1").arg(1000U, 3, 10, QChar(' ')), QString("1000"));
3975     QCOMPARE(QString("%1").arg(-1, 3, 10, QChar('x')), QString("x-1"));
3976     QCOMPARE(QString("%1").arg(-100, 3, 10, QChar('x')), QString("-100"));
3977     QCOMPARE(QString("%1").arg(1U, 3, 10, QChar('x')), QString("xx1"));
3978     QCOMPARE(QString("%1").arg(1000U, 3, 10, QChar('x')), QString("1000"));
3979
3980     QCOMPARE(QString("%1").arg(-1., 3, 'g', -1, QChar('0')), QString("-01"));
3981     QCOMPARE(QString("%1").arg(-100., 3, 'g', -1, QChar('0')), QString("-100"));
3982     QCOMPARE(QString("%1").arg(-1., 3, 'g', -1, QChar(' ')), QString(" -1"));
3983     QCOMPARE(QString("%1").arg(-100., 3, 'g', -1, QChar(' ')), QString("-100"));
3984     QCOMPARE(QString("%1").arg(1., 3, 'g', -1, QChar('x')), QString("xx1"));
3985     QCOMPARE(QString("%1").arg(1000., 3, 'g', -1, QChar('x')), QString("1000"));
3986     QCOMPARE(QString("%1").arg(-1., 3, 'g', -1, QChar('x')), QString("x-1"));
3987     QCOMPARE(QString("%1").arg(-100., 3, 'g', -1, QChar('x')), QString("-100"));
3988 }
3989
3990 void tst_QString::number()
3991 {
3992     QCOMPARE( QString::number(int(0)), QString("0") );
3993     QCOMPARE( QString::number((unsigned int)(11)), QString("11") );
3994     QCOMPARE( QString::number(-22L), QString("-22") );
3995     QCOMPARE( QString::number(333UL), QString("333") );
3996     QCOMPARE( QString::number(4.4), QString("4.4") );
3997     QCOMPARE( QString::number(Q_INT64_C(-555)), QString("-555") );
3998     QCOMPARE( QString::number(Q_UINT64_C(6666)), QString("6666") );
3999 }
4000
4001 void tst_QString::capacity_data()
4002 {
4003     length_data();
4004 }
4005
4006 void tst_QString::capacity()
4007 {
4008     QFETCH( QString, s1 );
4009     QFETCH( int, res );
4010
4011     QString s2( s1 );
4012     s2.reserve( res );
4013     QVERIFY( (int)s2.capacity() >= res );
4014     QCOMPARE( s2, s1 );
4015
4016     s2.reserve( res * 2 );
4017     QVERIFY( (int)s2.capacity() >=  res * 2 );
4018     QCOMPARE( s2, s1 );
4019
4020     s2.squeeze();
4021     QVERIFY( (int)s2.capacity() == res );
4022     QCOMPARE( s2, s1 );
4023
4024 }
4025
4026 void tst_QString::section_data()
4027 {
4028     QTest::addColumn<QString>("wholeString" );
4029     QTest::addColumn<QString>("sep" );
4030     QTest::addColumn<int>("start" );
4031     QTest::addColumn<int>("end" );
4032     QTest::addColumn<int>("flags" );
4033     QTest::addColumn<QString>("sectionString" );
4034     QTest::addColumn<bool>("regexp" );
4035
4036     QTest::newRow( "data0" ) << QString("forename,middlename,surname,phone") << QString(",") << 2 << 2 << int(QString::SectionDefault) << QString("surname") << false;
4037     QTest::newRow( "data1" ) << QString("/usr/local/bin/myapp") << QString("/") << 3 << 4 << int(QString::SectionDefault) << QString("bin/myapp") << false;
4038     QTest::newRow( "data2" ) << QString("/usr/local/bin/myapp") << QString("/") << 3 << 3 << int(QString::SectionSkipEmpty) << QString("myapp") << false;
4039     QTest::newRow( "data3" ) << QString("forename**middlename**surname**phone") << QString("**") << 2 << 2 << int(QString::SectionDefault) << QString("surname") << false;
4040     QTest::newRow( "data4" ) << QString("forename**middlename**surname**phone") << QString("**") << -3 << -2 << int(QString::SectionDefault) << QString("middlename**surname") << false;
4041     QTest::newRow( "data5" ) << QString("##Datt######wollen######wir######mal######sehen##") << QString("#") << 0 << 0 << int(QString::SectionSkipEmpty) << QString("Datt") << false;
4042     QTest::newRow( "data6" ) << QString("##Datt######wollen######wir######mal######sehen##") << QString("#") << 1 << 1 << int(QString::SectionSkipEmpty) << QString("wollen") << false;
4043     QTest::newRow( "data7" ) << QString("##Datt######wollen######wir######mal######sehen##") << QString("#") << 2 << 2 << int(QString::SectionSkipEmpty) << QString("wir") << false;
4044     QTest::newRow( "data8" ) << QString("##Datt######wollen######wir######mal######sehen##") << QString("#") << 3 << 3 << int(QString::SectionSkipEmpty) << QString("mal") << false;
4045     QTest::newRow( "data9" ) << QString("##Datt######wollen######wir######mal######sehen##") << QString("#") << 4 << 4 << int(QString::SectionSkipEmpty) << QString("sehen") << false;
4046     // not fixed for 3.1
4047     QTest::newRow( "data10" ) << QString("a/b/c/d") << QString("/") << 1 << -1 << int(QString::SectionIncludeLeadingSep | QString::SectionIncludeTrailingSep) << QString("/b/c/d") << false;
4048     QTest::newRow( "data11" ) << QString("aoLoboLocolod") << QString("olo") << -1 << -1 << int(QString::SectionCaseInsensitiveSeps) << QString("d") << false;
4049     QTest::newRow( "data12" ) << QString("F0") << QString("F") << 0 << 0 << int(QString::SectionSkipEmpty) << QString("0") << false;
4050     QTest::newRow( "foo1" ) << QString("foo;foo;") << QString(";") << 0 << 0
4051                          << int(QString::SectionIncludeLeadingSep) << QString("foo") << false;
4052     QTest::newRow( "foo2" ) << QString("foo;foo;") << QString(";") << 1 << 1
4053                          << int(QString::SectionIncludeLeadingSep) << QString(";foo") << false;
4054     QTest::newRow( "foo3" ) << QString("foo;foo;") << QString(";") << 2 << 2
4055                          << int(QString::SectionIncludeLeadingSep) << QString(";") << false;
4056     QTest::newRow( "foo1rx" ) << QString("foo;foo;") << QString(";") << 0 << 0
4057                            << int(QString::SectionIncludeLeadingSep) << QString("foo") << true;
4058     QTest::newRow( "foo2rx" ) << QString("foo;foo;") << QString(";") << 1 << 1
4059                            << int(QString::SectionIncludeLeadingSep) << QString(";foo") << true;
4060     QTest::newRow( "foo3rx" ) << QString("foo;foo;") << QString(";") << 2 << 2
4061                            << int(QString::SectionIncludeLeadingSep) << QString(";") << true;
4062
4063     QTest::newRow( "qmake_path" ) << QString("/Users/sam/troll/qt4.0/src/corelib/QtCore_debug.xcode/")
4064                                << QString("/") << 0 << -2 << int(QString::SectionDefault)
4065                                << QString("/Users/sam/troll/qt4.0/src/corelib/QtCore_debug.xcode") << false;
4066     QTest::newRow( "qmake_pathrx" ) << QString("/Users/sam/troll/qt4.0/src/corelib/QtCore_debug.xcode/")
4067                                  << QString("/") << 0 << -2 << int(QString::SectionDefault)
4068                                  << QString("/Users/sam/troll/qt4.0/src/corelib/QtCore_debug.xcode") << true;
4069     QTest::newRow( "data13" ) << QString("||2|3|||")
4070                               << QString("|") << 0 << 1 << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4071                               << QString("||") << false;
4072     QTest::newRow( "data14" ) << QString("||2|3|||")
4073                                 << QString("\\|") << 0 << 1 << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4074                                 << QString("||") << true;
4075     QTest::newRow( "data15" ) << QString("|1|2|")
4076                                 << QString("|") << 0 << 1 << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4077                                 << QString("|1|") << false;
4078     QTest::newRow( "data16" ) << QString("|1|2|")
4079                                 << QString("\\|") << 0 << 1 << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4080                                 << QString("|1|") << true;
4081     QTest::newRow( "normal1" ) << QString("o1o2o")
4082                             << QString("o") << 0 << 0
4083                             << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4084                             << QString("o") << false;
4085     QTest::newRow( "normal2" ) << QString("o1o2o")
4086                             << QString("o") << 1 << 1
4087                             << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4088                             << QString("o1o") << false;
4089     QTest::newRow( "normal3" ) << QString("o1o2o")
4090                             << QString("o") << 2 << 2
4091                             << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4092                             << QString("o2o") << false;
4093     QTest::newRow( "normal4" ) << QString("o1o2o")
4094                             << QString("o") << 2 << 3
4095                             << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4096                             << QString("o2o") << false;
4097     QTest::newRow( "normal5" ) << QString("o1o2o")
4098                             << QString("o") << 1 << 2
4099                             << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4100                             << QString("o1o2o") << false;
4101     QTest::newRow( "rx1" ) << QString("o1o2o")
4102                         << QString("[a-z]") << 0 << 0
4103                         << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4104                         << QString("o") << true;
4105     QTest::newRow( "rx2" ) << QString("o1o2o")
4106                         << QString("[a-z]") << 1 << 1
4107                         << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4108                         << QString("o1o") << true;
4109     QTest::newRow( "rx3" ) << QString("o1o2o")
4110                         << QString("[a-z]") << 2 << 2
4111                         << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4112                         << QString("o2o") << true;
4113     QTest::newRow( "rx4" ) << QString("o1o2o")
4114                         << QString("[a-z]") << 2 << 3
4115                         << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4116                         << QString("o2o") << true;
4117     QTest::newRow( "rx5" ) << QString("o1o2o")
4118                         << QString("[a-z]") << 1 << 2
4119                         << int(QString::SectionIncludeLeadingSep|QString::SectionIncludeTrailingSep)
4120                         << QString("o1o2o") << true;
4121     QTest::newRow( "data17" ) << QString("This is a story, a small story")
4122                         << QString("\\b") << 3 << 3
4123                         << int(QString::SectionDefault)
4124                         << QString("is") << true;
4125     QTest::newRow( "data18" ) << QString("99.0 42.3")
4126                         << QString("\\s*[AaBb]\\s*") << 1 << 1
4127                         << int(QString::SectionIncludeLeadingSep)
4128                         << QString() << true;
4129 }
4130
4131 void tst_QString::section()
4132 {
4133     QFETCH( QString, wholeString );
4134     QFETCH( QString, sep );
4135     QFETCH( int, start );
4136     QFETCH( int, end );
4137     QFETCH( int, flags );
4138     QFETCH( QString, sectionString );
4139     QFETCH( bool, regexp );
4140     if (regexp) {
4141         QCOMPARE( wholeString.section( QRegExp(sep), start, end, QString::SectionFlag(flags) ), sectionString );
4142         QCOMPARE( wholeString.section( QRegularExpression(sep), start, end, QString::SectionFlag(flags) ), sectionString );
4143     } else {
4144         QCOMPARE( wholeString.section( sep, start, end, QString::SectionFlag(flags) ), sectionString );
4145         QCOMPARE( wholeString.section( QRegExp(QRegExp::escape(sep)), start, end, QString::SectionFlag(flags) ), sectionString );
4146         QCOMPARE( wholeString.section( QRegularExpression(QRegularExpression::escape(sep)), start, end, QString::SectionFlag(flags) ), sectionString );
4147
4148     }
4149 }
4150
4151
4152 void tst_QString::operator_eqeq_nullstring()
4153 {
4154     /* Some of these might not be all that logical but it's the behaviour we've had since 3.0.0
4155        so we should probably stick with it. */
4156
4157     QVERIFY( QString() == "" );
4158     QVERIFY( "" == QString() );
4159
4160     QVERIFY( QString("") == "" );
4161     QVERIFY( "" == QString("") );
4162
4163     QVERIFY( QString().size() == 0 );
4164
4165     QVERIFY( QString("").size() == 0 );
4166
4167     QVERIFY( QString() == QString("") );
4168     QVERIFY( QString("") == QString() );
4169 }
4170
4171 void tst_QString::operator_smaller()
4172 {
4173     QString null;
4174     QString empty("");
4175     QString foo("foo");
4176
4177     QVERIFY( !(null < QString()) );
4178     QVERIFY( !(null > QString()) );
4179
4180     QVERIFY( !(empty < QString("")) );
4181     QVERIFY( !(empty > QString("")) );
4182
4183     QVERIFY( !(null < empty) );
4184     QVERIFY( !(null > empty) );
4185
4186     QVERIFY( null < foo );
4187     QVERIFY( !(null > foo) );
4188     QVERIFY( foo > null );
4189     QVERIFY( !(foo < null) );
4190
4191     QVERIFY( empty < foo );
4192     QVERIFY( !(empty > foo) );
4193     QVERIFY( foo > empty );
4194     QVERIFY( !(foo < empty) );
4195
4196     QVERIFY( !(null < QLatin1String(0)) );
4197     QVERIFY( !(null > QLatin1String(0)) );
4198     QVERIFY( !(null < QLatin1String("")) );
4199     QVERIFY( !(null > QLatin1String("")) );
4200
4201     QVERIFY( !(null < QLatin1String("")) );
4202     QVERIFY( !(null > QLatin1String("")) );
4203     QVERIFY( !(empty < QLatin1String("")) );
4204     QVERIFY( !(empty > QLatin1String("")) );
4205
4206     QVERIFY( !(QLatin1String(0) < null) );
4207     QVERIFY( !(QLatin1String(0) > null) );
4208     QVERIFY( !(QLatin1String("") < null) );
4209     QVERIFY( !(QLatin1String("") > null) );
4210
4211     QVERIFY( !(QLatin1String(0) < empty) );
4212     QVERIFY( !(QLatin1String(0) > empty) );
4213     QVERIFY( !(QLatin1String("") < empty) );
4214     QVERIFY( !(QLatin1String("") > empty) );
4215
4216     QVERIFY( QLatin1String(0) < foo );
4217     QVERIFY( !(QLatin1String(0) > foo) );
4218     QVERIFY( QLatin1String("") < foo );
4219     QVERIFY( !(QLatin1String("") > foo) );
4220
4221     QVERIFY( foo > QLatin1String(0) );
4222     QVERIFY( !(foo < QLatin1String(0)) );
4223     QVERIFY( foo > QLatin1String("") );
4224     QVERIFY( !(foo < QLatin1String("")) );
4225
4226     QVERIFY( QLatin1String(0) == empty);
4227     QVERIFY( QLatin1String(0) == null);
4228     QVERIFY( QLatin1String("") == empty);
4229     QVERIFY( QLatin1String("") == null);
4230
4231     QVERIFY( !(foo < QLatin1String("foo")));
4232     QVERIFY( !(foo > QLatin1String("foo")));
4233     QVERIFY( !(QLatin1String("foo") < foo));
4234     QVERIFY( !(QLatin1String("foo") > foo));
4235
4236     QVERIFY( !(foo < QLatin1String("a")));
4237     QVERIFY( (foo > QLatin1String("a")));
4238     QVERIFY( (QLatin1String("a") < foo));
4239     QVERIFY( !(QLatin1String("a") > foo));
4240
4241     QVERIFY( (foo < QLatin1String("z")));
4242     QVERIFY( !(foo > QLatin1String("z")));
4243     QVERIFY( !(QLatin1String("z") < foo));
4244     QVERIFY( (QLatin1String("z") > foo));
4245
4246     // operator< is not locale-aware (or shouldn't be)
4247     QVERIFY( foo < QString("\xc3\xa9") );
4248     QVERIFY( foo < "\xc3\xa9" );
4249 }
4250
4251 void tst_QString::integer_conversion_data()
4252 {
4253     QTest::addColumn<QString>("num_str");
4254     QTest::addColumn<int>("base");
4255     QTest::addColumn<bool>("good");
4256     QTest::addColumn<qlonglong>("num");
4257
4258     QTest::newRow("C empty 0")      << QString("")         << 0  << false << (qlonglong)0;
4259     QTest::newRow("C empty 8")      << QString("")         << 8  << false << (qlonglong)0;
4260     QTest::newRow("C empty 10")     << QString("")         << 10 << false << (qlonglong)0;
4261     QTest::newRow("C empty 16")     << QString("")         << 16 << false << (qlonglong)0;
4262
4263     QTest::newRow("C null 0")       << QString()           << 0  << false << (qlonglong)0;
4264     QTest::newRow("C null 8")       << QString()           << 8  << false << (qlonglong)0;
4265     QTest::newRow("C null 10")      << QString()           << 10 << false << (qlonglong)0;
4266     QTest::newRow("C null 16")      << QString()           << 16 << false << (qlonglong)0;
4267
4268     QTest::newRow("C   -0xf 0")     << QString("  -0xf")   << 0  << true  << (qlonglong)-15;
4269     QTest::newRow("C -0xf   0")     << QString("-0xf  ")   << 0  << true  << (qlonglong)-15;
4270     QTest::newRow("C \t0xf\t 0")    << QString("\t0xf\t")  << 0  << true  << (qlonglong)15;
4271     QTest::newRow("C   -010 0")     << QString("  -010")   << 0  << true  << (qlonglong)-8;
4272     QTest::newRow("C 010   0")      << QString("010  ")    << 0  << true  << (qlonglong)8;
4273     QTest::newRow("C \t-010\t 0")   << QString("\t-010\t") << 0  << true  << (qlonglong)-8;
4274     QTest::newRow("C   123 10")     << QString("  123")    << 10 << true  << (qlonglong)123;
4275     QTest::newRow("C 123   10")     << QString("123  ")    << 10 << true  << (qlonglong)123;
4276     QTest::newRow("C \t123\t 10")   << QString("\t123\t")  << 10 << true  << (qlonglong)123;
4277     QTest::newRow("C   -0xf 16")    << QString("  -0xf")   << 16 << true  << (qlonglong)-15;
4278     QTest::newRow("C -0xf   16")    << QString("-0xf  ")   << 16 << true  << (qlonglong)-15;
4279     QTest::newRow("C \t0xf\t 16")   << QString("\t0xf\t")  << 16 << true  << (qlonglong)15;
4280
4281     QTest::newRow("C -0 0")         << QString("-0")       << 0   << true << (qlonglong)0;
4282     QTest::newRow("C -0 8")         << QString("-0")       << 8   << true << (qlonglong)0;
4283     QTest::newRow("C -0 10")        << QString("-0")       << 10  << true << (qlonglong)0;
4284     QTest::newRow("C -0 16")        << QString("-0")       << 16  << true << (qlonglong)0;
4285
4286     QTest::newRow("C 1.234 10")     << QString("1.234")    << 10 << false << (qlonglong)0;
4287     QTest::newRow("C 1,234 10")     << QString("1,234")    << 10 << false << (qlonglong)0;
4288
4289     QTest::newRow("C 0x 0")         << QString("0x")       << 0  << false << (qlonglong)0;
4290     QTest::newRow("C 0x 16")        << QString("0x")       << 16 << false << (qlonglong)0;
4291
4292     QTest::newRow("C 10 0")         << QString("10")       << 0  << true  << (qlonglong)10;
4293     QTest::newRow("C 010 0")        << QString("010")      << 0  << true  << (qlonglong)8;
4294     QTest::newRow("C 0x10 0")       << QString("0x10")     << 0  << true  << (qlonglong)16;
4295     QTest::newRow("C 10 8")         << QString("10")       << 8  << true  << (qlonglong)8;
4296     QTest::newRow("C 010 8")        << QString("010")      << 8  << true  << (qlonglong)8;
4297     QTest::newRow("C 0x10 8")       << QString("0x10")     << 8  << false << (qlonglong)0;
4298     QTest::newRow("C 10 10")        << QString("10")       << 10 << true  << (qlonglong)10;
4299     QTest::newRow("C 010 10")       << QString("010")      << 10 << true  << (qlonglong)10;
4300     QTest::newRow("C 0x10 10")      << QString("0x10")     << 10 << false << (qlonglong)0;
4301     QTest::newRow("C 10 16")        << QString("10")       << 16 << true  << (qlonglong)16;
4302     QTest::newRow("C 010 16")       << QString("010")      << 16 << true  << (qlonglong)16;
4303     QTest::newRow("C 0x10 16")      << QString("0x10")     << 16 << true  << (qlonglong)16;
4304
4305     QTest::newRow("C -10 0")        << QString("-10")      << 0  << true  << (qlonglong)-10;
4306     QTest::newRow("C -010 0")       << QString("-010")     << 0  << true  << (qlonglong)-8;
4307     QTest::newRow("C -0x10 0")      << QString("-0x10")    << 0  << true  << (qlonglong)-16;
4308     QTest::newRow("C -10 8")        << QString("-10")      << 8  << true  << (qlonglong)-8;
4309     QTest::newRow("C -010 8")       << QString("-010")     << 8  << true  << (qlonglong)-8;
4310     QTest::newRow("C -0x10 8")      << QString("-0x10")    << 8  << false << (qlonglong)0;
4311     QTest::newRow("C -10 10")       << QString("-10")      << 10 << true  << (qlonglong)-10;
4312     QTest::newRow("C -010 10")      << QString("-010")     << 10 << true  << (qlonglong)-10;
4313     QTest::newRow("C -0x10 10")     << QString("-0x10")    << 10 << false << (qlonglong)0;
4314     QTest::newRow("C -10 16")       << QString("-10")      << 16 << true  << (qlonglong)-16;
4315     QTest::newRow("C -010 16")      << QString("-010")     << 16 << true  << (qlonglong)-16;
4316     QTest::newRow("C -0x10 16")     << QString("-0x10")    << 16 << true  << (qlonglong)-16;
4317
4318     // Let's try some Arabic
4319     const quint16 arabic_str[] = { 0x0661, 0x0662, 0x0663, 0x0664, 0x0000 }; // "1234"
4320     QTest::newRow("ar_SA 1234 0")  << QString::fromUtf16(arabic_str)  << 0  << false << (qlonglong)0;
4321 }
4322
4323 void tst_QString::integer_conversion()
4324 {
4325     QFETCH(QString, num_str);
4326     QFETCH(int, base);
4327     QFETCH(bool, good);
4328     QFETCH(qlonglong, num);
4329
4330     bool ok;
4331     qlonglong d = num_str.toLongLong(&ok, base);
4332     QCOMPARE(ok, good);
4333
4334     if (ok) {
4335         QCOMPARE(d, num);
4336     }
4337 }
4338
4339 void tst_QString::double_conversion_data()
4340 {
4341     QTest::addColumn<QString>("num_str");
4342     QTest::addColumn<bool>("good");
4343     QTest::addColumn<double>("num");
4344
4345     // The good...
4346
4347     QTest::newRow("C 1")             << QString("1")          << true  << 1.0;
4348     QTest::newRow("C 1.0")           << QString("1.0")        << true  << 1.0;
4349     QTest::newRow("C 1.234")         << QString("1.234")      << true  << 1.234;
4350     QTest::newRow("C 1.234e-10")     << QString("1.234e-10")  << true  << 1.234e-10;
4351     QTest::newRow("C 1.234E10")      << QString("1.234E10")   << true  << 1.234e10;
4352     QTest::newRow("C 1e10")          << QString("1e10")       << true  << 1.0e10;
4353
4354     // The bad...
4355
4356     QTest::newRow("C empty")         << QString("")           << false << 0.0;
4357     QTest::newRow("C null")          << QString()             << false << 0.0;
4358     QTest::newRow("C .")             << QString(".")          << false << 0.0;
4359     QTest::newRow("C 1e")            << QString("1e")         << false << 0.0;
4360     QTest::newRow("C 1,")            << QString("1,")         << false << 0.0;
4361     QTest::newRow("C 1,0")           << QString("1,0")        << false << 0.0;
4362     QTest::newRow("C 1,000")         << QString("1,000")      << false << 0.0;
4363     QTest::newRow("C 1e1.0")         << QString("1e1.0")      << false << 0.0;
4364     QTest::newRow("C 1e+")           << QString("1e+")        << false << 0.0;
4365     QTest::newRow("C 1e-")           << QString("1e-")        << false << 0.0;
4366     QTest::newRow("de_DE 1,0")       << QString("1,0")        << false << 0.0;
4367     QTest::newRow("de_DE 1,234")     << QString("1,234")      << false << 0.0;
4368     QTest::newRow("de_DE 1,234e-10") << QString("1,234e-10")  << false << 0.0;
4369     QTest::newRow("de_DE 1,234E10")  << QString("1,234E10")   << false << 0.0;
4370
4371     // And the ugly...
4372
4373     QTest::newRow("C .1")            << QString(".1")         << true  << 0.1;
4374     QTest::newRow("C -.1")           << QString("-.1")        << true  << -0.1;
4375     QTest::newRow("C 1.")            << QString("1.")         << true  << 1.0;
4376     QTest::newRow("C 1.E10")         << QString("1.E10")      << true  << 1.0e10;
4377     QTest::newRow("C 1e+10")         << QString("1e+10")      << true  << 1.0e+10;
4378     QTest::newRow("C   1")           << QString("  1")        << true  << 1.0;
4379     QTest::newRow("C 1  ")           << QString("1  ")        << true  << 1.0;
4380
4381     // Let's try some Arabic
4382     const quint16 arabic_str[] = { 0x0660, 0x066B, 0x0661, 0x0662,
4383                                     0x0663, 0x0664, 0x0065, 0x0662,
4384                                     0x0000 };                            // "0.1234e2"
4385     QTest::newRow("ar_SA") << QString::fromUtf16(arabic_str) << false << 0.0;
4386 }
4387
4388 void tst_QString::double_conversion()
4389 {
4390 #define MY_DOUBLE_EPSILON (2.22045e-16)
4391
4392     QFETCH(QString, num_str);
4393     QFETCH(bool, good);
4394     QFETCH(double, num);
4395
4396     bool ok;
4397     double d = num_str.toDouble(&ok);
4398     QCOMPARE(ok, good);
4399
4400     if (ok) {
4401         double diff = d - num;
4402         if (diff < 0)
4403             diff = -diff;
4404         QVERIFY(diff <= MY_DOUBLE_EPSILON);
4405     }
4406 }
4407
4408 #ifndef Q_MOC_RUN
4409 #include "double_data.h"
4410 #endif
4411
4412 void tst_QString::tortureSprintfDouble()
4413 {
4414     const SprintfDoubleData *data = g_sprintf_double_data;
4415
4416     QString s;
4417
4418     for (; data->fmt != 0; ++data) {
4419         double d;
4420         char *buff = (char *)&d;
4421 #        ifndef Q_BYTE_ORDER
4422 #            error "Q_BYTE_ORDER not defined"
4423 #        endif
4424
4425 #        if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
4426         for (uint i = 0; i < 8; ++i)
4427             buff[i] = data->bytes[i];
4428 #        else
4429         for (uint i = 0; i < 8; ++i)
4430             buff[7 - i] = data->bytes[i];
4431 #        endif
4432         s.sprintf(data->fmt, d);
4433 #ifdef QT_NO_FPU // reduced precision when running with hardfloats in qemu
4434         if (d - 0.1 < 1e12)
4435             QSKIP("clib sprintf doesn't fill with 0's on this platform");
4436         QCOMPARE(s.left(16), QString(data->expected).left(16));
4437 #else
4438         QCOMPARE(s, QString(data->expected));
4439 #endif
4440     }
4441 }
4442
4443 #include <locale.h>
4444
4445 void tst_QString::localeAwareCompare_data()
4446 {
4447 #ifdef Q_OS_WIN
4448     QTest::addColumn<ulong>("locale");
4449 #else
4450     QTest::addColumn<QString>("locale");
4451 #endif
4452     QTest::addColumn<QString>("s1");
4453     QTest::addColumn<QString>("s2");
4454     QTest::addColumn<int>("result");
4455
4456     /*
4457         The C locale performs pure byte comparisons for
4458         Latin-1-specific characters (I think). Compare with Swedish
4459         below.
4460     */
4461 #ifdef Q_OS_WIN // assume c locale to be english
4462     QTest::newRow("c1") << MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT) << QString::fromLatin1("\xe5") << QString::fromLatin1("\xe4") << 1;
4463     QTest::newRow("c2") << MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT) << QString::fromLatin1("\xe4") << QString::fromLatin1("\xf6") << -1;
4464     QTest::newRow("c3") << MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT) << QString::fromLatin1("\xe5") << QString::fromLatin1("\xf6") << -1;
4465 #else
4466     QTest::newRow("c1") << QString("C") << QString::fromLatin1("\xe5") << QString::fromLatin1("\xe4") << 1;
4467     QTest::newRow("c2") << QString("C") << QString::fromLatin1("\xe4") << QString::fromLatin1("\xf6") << -1;
4468     QTest::newRow("c3") << QString("C") << QString::fromLatin1("\xe5") << QString::fromLatin1("\xf6") << -1;
4469 #endif
4470
4471     /*
4472         It's hard to test English, because it's treated differently
4473         on different platforms. For example, on Linux, it uses the
4474         iso14651_t1 template file, which happens to provide good
4475         defaults for Swedish. Mac OS X seems to do a pure bytewise
4476         comparison of Latin-1 values, although I'm not sure. So I
4477         just test digits to make sure that it's not totally broken.
4478     */
4479 #ifdef Q_OS_WIN
4480     QTest::newRow("english1") << MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT) << QString("5") << QString("4") << 1;
4481     QTest::newRow("english2") << MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT) << QString("4") << QString("6") << -1;
4482     QTest::newRow("english3") << MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT) << QString("5") << QString("6") << -1;
4483 #else
4484     QTest::newRow("english1") << QString("en_US") << QString("5") << QString("4") << 1;
4485     QTest::newRow("english2") << QString("en_US") << QString("4") << QString("6") << -1;
4486     QTest::newRow("english3") << QString("en_US") << QString("5") << QString("6") << -1;
4487 #endif
4488     /*
4489         In Swedish, a with ring above (E5) comes before a with
4490         diaresis (E4), which comes before o diaresis (F6), which
4491         all come after z.
4492     */
4493 #ifdef Q_OS_MAC
4494     QTest::newRow("swedish1") << QString("sv_SE.ISO8859-1") << QString::fromLatin1("\xe5") << QString::fromLatin1("\xe4") << -1;
4495     QTest::newRow("swedish2") << QString("sv_SE.ISO8859-1") << QString::fromLatin1("\xe4") << QString::fromLatin1("\xf6") << -1;
4496     QTest::newRow("swedish3") << QString("sv_SE.ISO8859-1") << QString::fromLatin1("\xe5") << QString::fromLatin1("\xf6") << -1;
4497     QTest::newRow("swedish4") << QString("sv_SE.ISO8859-1") << QString::fromLatin1("z") << QString::fromLatin1("\xe5") << -1;
4498 #elif defined(Q_OS_WIN)
4499     QTest::newRow("swedish1") << MAKELCID(MAKELANGID(LANG_SWEDISH, SUBLANG_SWEDISH), SORT_DEFAULT) << QString::fromLatin1("\xe5") << QString::fromLatin1("\xe4") << -1;
4500     QTest::newRow("swedish2") << MAKELCID(MAKELANGID(LANG_SWEDISH, SUBLANG_SWEDISH), SORT_DEFAULT) << QString::fromLatin1("\xe4") << QString::fromLatin1("\xf6") << -1;
4501     QTest::newRow("swedish3") << MAKELCID(MAKELANGID(LANG_SWEDISH, SUBLANG_SWEDISH), SORT_DEFAULT) << QString::fromLatin1("\xe5") << QString::fromLatin1("\xf6") << -1;
4502     QTest::newRow("swedish4") << MAKELCID(MAKELANGID(LANG_SWEDISH, SUBLANG_SWEDISH), SORT_DEFAULT) << QString::fromLatin1("z") << QString::fromLatin1("\xe5") << -1;
4503 #else
4504     QTest::newRow("swedish1") << QString("sv_SE") << QString::fromLatin1("\xe5") << QString::fromLatin1("\xe4") << -1;
4505     QTest::newRow("swedish2") << QString("sv_SE") << QString::fromLatin1("\xe4") << QString::fromLatin1("\xf6") << -1;
4506     QTest::newRow("swedish3") << QString("sv_SE") << QString::fromLatin1("\xe5") << QString::fromLatin1("\xf6") << -1;
4507     QTest::newRow("swedish4") << QString("sv_SE") << QString::fromLatin1("z") << QString::fromLatin1("\xe5") << -1;
4508 #endif
4509
4510 #if 0
4511     /*
4512         In Norwegian, ae (E6) comes before o with stroke (D8), which
4513         comes before a with ring above (E5).
4514     */
4515     QTest::newRow("norwegian1") << QString("no_NO") << QString::fromLatin1("\xe6") << QString::fromLatin1("\xd8") << -1;
4516     QTest::newRow("norwegian2") << QString("no_NO") << QString::fromLatin1("\xd8") << QString::fromLatin1("\xe5") << -1;
4517     QTest::newRow("norwegian3") << QString("no_NO") << QString::fromLatin1("\xe6") << QString::fromLatin1("\xe5") << -1;
4518 #endif
4519
4520     /*
4521         In German, z comes *after* a with diaresis (E4),
4522         which comes before o diaresis (F6).
4523     */
4524 #ifdef Q_OS_MAC
4525     QTest::newRow("german1") << QString("de_DE.ISO8859-1") << QString::fromLatin1("z") << QString::fromLatin1("\xe4") << 1;
4526     QTest::newRow("german2") << QString("de_DE.ISO8859-1") << QString::fromLatin1("\xe4") << QString::fromLatin1("\xf6") << -1;
4527     QTest::newRow("german3") << QString("de_DE.ISO8859-1") << QString::fromLatin1("z") << QString::fromLatin1("\xf6") << 1;
4528 #elif defined(Q_OS_WIN)
4529     QTest::newRow("german1") << MAKELCID(MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN), SORT_DEFAULT) << QString::fromLatin1("z") << QString::fromLatin1("\xe4") << 1;
4530     QTest::newRow("german2") << MAKELCID(MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN), SORT_DEFAULT) << QString::fromLatin1("\xe4") << QString::fromLatin1("\xf6") << -1;
4531     QTest::newRow("german3") << MAKELCID(MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN), SORT_DEFAULT) << QString::fromLatin1("z") << QString::fromLatin1("\xf6") << 1;
4532 #else
4533     QTest::newRow("german1") << QString("de_DE") << QString::fromLatin1("z") << QString::fromLatin1("\xe4") << 1;
4534     QTest::newRow("german2") << QString("de_DE") << QString::fromLatin1("\xe4") << QString::fromLatin1("\xf6") << -1;
4535     QTest::newRow("german3") << QString("de_DE") << QString::fromLatin1("z") << QString::fromLatin1("\xf6") << 1;
4536 #endif
4537 }
4538
4539 void tst_QString::localeAwareCompare()
4540 {
4541 #ifdef Q_OS_WIN
4542 #   ifndef Q_OS_WINCE
4543        QSKIP("On others than Win CE, we cannot set the system or user locale.");
4544 #   endif
4545     QFETCH(ulong, locale);
4546 #else
4547     QFETCH(QString, locale);
4548 #endif
4549     QFETCH(QString, s1);
4550     QFETCH(QString, s2);
4551     QFETCH(int, result);
4552
4553     QStringRef r1(&s1, 0, s1.length());
4554     QStringRef r2(&s2, 0, s2.length());
4555
4556 #ifdef Q_OS_WIN
4557 #  if defined(Q_OS_WINCE)
4558     DWORD oldLcid = GetUserDefaultLCID();
4559     SetUserDefaultLCID(locale);
4560
4561     QCOMPARE(locale, GetUserDefaultLCID());
4562 #  else
4563     DWORD oldLcid = GetThreadLocale();
4564     SetThreadLocale(locale);
4565
4566     QCOMPARE(locale, GetThreadLocale());
4567 #  endif
4568
4569 #elif defined (Q_OS_MAC)
4570     QSKIP("Setting the locale is not supported on OS X (you can set the C locale, but that won't affect CFStringCompare which is used to compare strings)");
4571 #elif defined(QT_USE_ICU)
4572     QLocale::setDefault(QLocale(locale));
4573 #else
4574     if (!locale.isEmpty()) {
4575         const char *newLocale = setlocale(LC_ALL, locale.toLatin1());
4576         if (!newLocale) {
4577             setlocale(LC_ALL, "");
4578             QSKIP("Please install the proper locale on this machine to test properly");
4579         }
4580     }
4581 #endif
4582
4583 #ifdef QT_USE_ICU
4584     // ### for c1, ICU disagrees with libc on how to compare
4585     QEXPECT_FAIL("c1", "ICU disagrees with test", Abort);
4586 #endif
4587
4588     int testres = QString::localeAwareCompare(s1, s2);
4589     if (result < 0) {
4590         QVERIFY(testres < 0);
4591     } else if (result > 0) {
4592         QVERIFY(testres > 0);
4593     } else {
4594         QVERIFY(testres == 0);
4595     }
4596
4597     testres = QString::localeAwareCompare(s2, s1);
4598     if (result > 0) {
4599         QVERIFY(testres < 0);
4600     } else if (result < 0) {
4601         QVERIFY(testres > 0);
4602     } else {
4603         QVERIFY(testres == 0);
4604     }
4605
4606     testres = QString::localeAwareCompare(s1, r2);
4607     if (result < 0) {
4608         QVERIFY(testres < 0);
4609     } else if (result > 0) {
4610         QVERIFY(testres > 0);
4611     } else {
4612         QVERIFY(testres == 0);
4613     }
4614
4615     testres = QStringRef::localeAwareCompare(r1, r2);
4616     if (result < 0) {
4617         QVERIFY(testres < 0);
4618     } else if (result > 0) {
4619         QVERIFY(testres > 0);
4620     } else {
4621         QVERIFY(testres == 0);
4622     }
4623
4624     testres = QStringRef::localeAwareCompare(r2, r1);
4625     if (result > 0) {
4626         QVERIFY(testres < 0);
4627     } else if (result < 0) {
4628         QVERIFY(testres > 0);
4629     } else {
4630         QVERIFY(testres == 0);
4631     }
4632
4633 #ifdef Q_OS_WIN
4634 #  if defined(Q_OS_WINCE)
4635     SetUserDefaultLCID(oldLcid);
4636 #  else
4637     SetThreadLocale(oldLcid);
4638 #  endif
4639
4640 #else
4641     if (!locale.isEmpty())
4642             setlocale(LC_ALL, "");
4643 #endif
4644 }
4645
4646 void tst_QString::split_data()
4647 {
4648     QTest::addColumn<QString>("str");
4649     QTest::addColumn<QString>("sep");
4650     QTest::addColumn<QStringList>("result");
4651
4652     QTest::newRow("1") << "a,b,c" << "," << (QStringList() << "a" << "b" << "c");
4653     QTest::newRow("2") << QString("-rw-r--r--  1 0  0  519240 Jul  9  2002 bigfile")
4654                     << " "
4655                     << (QStringList() << "-rw-r--r--" << "" << "1" << "0" << "" << "0" << ""
4656                                       << "519240" << "Jul" << "" << "9" << "" << "2002" << "bigfile");
4657     QTest::newRow("one-empty") << "" << " " << (QStringList() << "");
4658     QTest::newRow("two-empty") << " " << " " << (QStringList() << "" << "");
4659     QTest::newRow("three-empty") << "  " << " " << (QStringList() << "" << "" << "");
4660
4661     QTest::newRow("all-empty") << "" << "" << (QStringList() << "" << "");
4662     QTest::newRow("sep-empty") << "abc" << "" << (QStringList() << "" << "a" << "b" << "c" << "");
4663 }
4664
4665 void tst_QString::split()
4666 {
4667     QFETCH(QString, str);
4668     QFETCH(QString, sep);
4669     QFETCH(QStringList, result);
4670
4671     QRegExp rx = QRegExp(QRegExp::escape(sep));
4672     QRegularExpression re(QRegularExpression::escape(sep));
4673
4674     QStringList list;
4675
4676     list = str.split(sep);
4677     QVERIFY(list == result);
4678     list = str.split(rx);
4679     QVERIFY(list == result);
4680     list = str.split(re);
4681     QVERIFY(list == result);
4682     if (sep.size() == 1) {
4683         list = str.split(sep.at(0));
4684         QVERIFY(list == result);
4685     }
4686
4687     list = str.split(sep, QString::KeepEmptyParts);
4688     QVERIFY(list == result);
4689     list = str.split(rx, QString::KeepEmptyParts);
4690     QVERIFY(list == result);
4691     list = str.split(re, QString::KeepEmptyParts);
4692     QVERIFY(list == result);
4693     if (sep.size() == 1) {
4694         list = str.split(sep.at(0), QString::KeepEmptyParts);
4695         QVERIFY(list == result);
4696     }
4697
4698     result.removeAll("");
4699     list = str.split(sep, QString::SkipEmptyParts);
4700     QVERIFY(list == result);
4701     list = str.split(rx, QString::SkipEmptyParts);
4702     QVERIFY(list == result);
4703     list = str.split(re, QString::SkipEmptyParts);
4704     QVERIFY(list == result);
4705     if (sep.size() == 1) {
4706         list = str.split(sep.at(0), QString::SkipEmptyParts);
4707         QVERIFY(list == result);
4708     }
4709 }
4710
4711 void tst_QString::split_regexp_data()
4712 {
4713     QTest::addColumn<QString>("string");
4714     QTest::addColumn<QString>("pattern");
4715     QTest::addColumn<QStringList>("result");
4716
4717     QTest::newRow("data01") << "Some  text\n\twith  strange whitespace."
4718                             << "\\s+"
4719                             << (QStringList() << "Some" << "text" << "with" << "strange" << "whitespace." );
4720
4721     QTest::newRow("data02") << "This time, a normal English sentence."
4722                             << "\\W+"
4723                             << (QStringList() << "This" << "time" << "a" << "normal" << "English" << "sentence" << "");
4724
4725     QTest::newRow("data03") << "Now: this sentence fragment."
4726                             << "\\b"
4727                             << (QStringList() << "" << "Now" << ": " << "this" << " " << "sentence" << " " << "fragment" << ".");
4728 }
4729
4730 void tst_QString::split_regexp()
4731 {
4732     QFETCH(QString, string);
4733     QFETCH(QString, pattern);
4734     QFETCH(QStringList, result);
4735
4736     QStringList list;
4737     list = string.split(QRegExp(pattern));
4738     QCOMPARE(list, result);
4739     list = string.split(QRegularExpression(pattern));
4740     QCOMPARE(list, result);
4741
4742     result.removeAll(QString());
4743
4744     list = string.split(QRegExp(pattern), QString::SkipEmptyParts);
4745     QCOMPARE(list, result);
4746     list = string.split(QRegularExpression(pattern), QString::SkipEmptyParts);
4747     QCOMPARE(list, result);
4748 }
4749
4750 void tst_QString::fromUtf16_data()
4751 {
4752     QTest::addColumn<QString>("ucs2");
4753     QTest::addColumn<QString>("res");
4754     QTest::addColumn<int>("len");
4755
4756     QTest::newRow("str0") << QString("abcdefgh") << QString("abcdefgh") << -1;
4757     QTest::newRow("str0-len") << QString("abcdefgh") << QString("abc") << 3;
4758 }
4759
4760 void tst_QString::fromUtf16()
4761 {
4762     QFETCH(QString, ucs2);
4763     QFETCH(QString, res);
4764     QFETCH(int, len);
4765
4766     QCOMPARE(QString::fromUtf16(ucs2.utf16(), len), res);
4767 }
4768
4769
4770 void tst_QString::latin1String()
4771 {
4772     QString s("Hello");
4773
4774     QVERIFY(s == QLatin1String("Hello"));
4775     QVERIFY(s != QLatin1String("Hello World"));
4776     QVERIFY(s < QLatin1String("Helloa"));
4777     QVERIFY(!(s > QLatin1String("Helloa")));
4778     QVERIFY(s > QLatin1String("Helln"));
4779     QVERIFY(s > QLatin1String("Hell"));
4780     QVERIFY(!(s < QLatin1String("Helln")));
4781     QVERIFY(!(s < QLatin1String("Hell")));
4782 }
4783
4784 void tst_QString::nanAndInf()
4785 {
4786     bool ok;
4787     double d;
4788
4789 #define CHECK_DOUBLE(str, expected_ok, expected_inf) \
4790     d = QString(str).toDouble(&ok); \
4791     QVERIFY(ok == expected_ok); \
4792     QVERIFY((d == INFINITY) == expected_inf);
4793
4794     CHECK_DOUBLE("inf", true, true)
4795     CHECK_DOUBLE("INF", true, true)
4796     CHECK_DOUBLE("inf  ", true, true)
4797     CHECK_DOUBLE("+inf", true, true)
4798     CHECK_DOUBLE("\t +INF", true, true)
4799     CHECK_DOUBLE("\t INF", true, true)
4800     CHECK_DOUBLE("inF  ", true, true)
4801     CHECK_DOUBLE("+iNf", true, true)
4802     CHECK_DOUBLE("INFe-10", false, false)
4803     CHECK_DOUBLE("0xINF", false, false)
4804     CHECK_DOUBLE("- INF", false, false)
4805     CHECK_DOUBLE("+ INF", false, false)
4806     CHECK_DOUBLE("-- INF", false, false)
4807     CHECK_DOUBLE("inf0", false, false)
4808     CHECK_DOUBLE("--INF", false, false)
4809     CHECK_DOUBLE("++INF", false, false)
4810     CHECK_DOUBLE("INF++", false, false)
4811     CHECK_DOUBLE("INF--", false, false)
4812     CHECK_DOUBLE("INF +", false, false)
4813     CHECK_DOUBLE("INF -", false, false)
4814     CHECK_DOUBLE("0INF", false, false)
4815 #undef CHECK_INF
4816
4817 #define CHECK_NAN(str, expected_ok, expected_nan) \
4818     d = QString(str).toDouble(&ok); \
4819     QVERIFY(ok == expected_ok); \
4820     QVERIFY((bool)isnan(d) == expected_nan); \
4821
4822     CHECK_NAN("nan", true, true)
4823     CHECK_NAN("NAN", true, true)
4824     CHECK_NAN("nan  ", true, true)
4825     CHECK_NAN("\t NAN", true, true)
4826     CHECK_NAN("\t NAN  ", true, true)
4827 #ifndef QT_QLOCALE_USES_FCVT //In case we use glibc this tests will fail
4828     CHECK_NAN("-nan", false, false)
4829     CHECK_NAN("+NAN", false, false)
4830 #endif
4831     CHECK_NAN("NaN", true, true)
4832     CHECK_NAN("nAn", true, true)
4833     CHECK_NAN("NANe-10", false, false)
4834     CHECK_NAN("0xNAN", false, false)
4835     CHECK_NAN("0NAN", false, false)
4836 #undef CHECK_NAN
4837
4838     d = QString("-INF").toDouble(&ok);
4839     QVERIFY(ok);
4840     QVERIFY(d == -INFINITY);
4841
4842     QString("INF").toLong(&ok);
4843     QVERIFY(!ok);
4844
4845     QString("INF").toLong(&ok, 36);
4846     QVERIFY(ok);
4847
4848     QString("INF0").toLong(&ok, 36);
4849     QVERIFY(ok);
4850
4851     QString("0INF0").toLong(&ok, 36);
4852     QVERIFY(ok);
4853 }
4854
4855 void tst_QString::arg_fillChar_data()
4856 {
4857     QTest::addColumn<QString>("pattern");
4858     QTest::addColumn<QList<QVariant> >("replaceValues");
4859     QTest::addColumn<IntList>("widths");
4860     QTest::addColumn<QString>("fillChars");
4861     QTest::addColumn<QString>("expected");
4862
4863     QList<QVariant> replaceValues;
4864     IntList widths;
4865     QString fillChars;
4866
4867     replaceValues << QVariant((int)5) << QVariant(QString("f")) << QVariant((int)0);
4868     widths << 3 << 2 << 5;
4869     QTest::newRow("str0") << QString("%1%2%3") << replaceValues << widths << QString("abc") << QString("aa5bfcccc0");
4870
4871     replaceValues.clear();
4872     widths.clear();
4873     replaceValues << QVariant((int)5.5) << QVariant(QString("foo")) << QVariant((qulonglong)INT_MAX);
4874     widths << 10 << 2 << 5;
4875     QTest::newRow("str1") << QString("%3.%1.%3.%2") << replaceValues << widths << QString("0 c")
4876                        << QString("2147483647.0000000005.2147483647.foo");
4877
4878     replaceValues.clear();
4879     widths.clear();
4880     replaceValues << QVariant(QString("fisk"));
4881     widths << 100;
4882     QTest::newRow("str2") << QString("%9 og poteter") << replaceValues << widths << QString("f")
4883                        << QString("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffisk og poteter");
4884 }
4885
4886 void tst_QString::arg_fillChar()
4887 {
4888     static const int base = 10;
4889     static const char fmt = 'g';
4890     static const int prec = -1;
4891
4892     QFETCH(QString, pattern);
4893     QFETCH(QList<QVariant>, replaceValues);
4894     QFETCH(IntList, widths);
4895     QFETCH(QString, fillChars);
4896     QFETCH(QString, expected);
4897     QCOMPARE(replaceValues.count(), fillChars.count());
4898     QCOMPARE(replaceValues.count(), widths.count());
4899
4900     QString actual = pattern;
4901     for (int i=0; i<replaceValues.count(); ++i) {
4902         const QVariant &var = replaceValues.at(i);
4903         const int width = widths.at(i);
4904         const QChar &fillChar = fillChars.at(i);
4905         switch (var.type()) {
4906         case QVariant::String: actual = actual.arg(var.toString(), width, fillChar); break;
4907         case QVariant::Int: actual = actual.arg(var.toInt(), width, base, fillChar); break;
4908         case QVariant::UInt: actual = actual.arg(var.toUInt(), width, base, fillChar); break;
4909         case QVariant::Double: actual = actual.arg(var.toDouble(), width, fmt, prec, fillChar); break;
4910         case QVariant::LongLong: actual = actual.arg(var.toLongLong(), width, base, fillChar); break;
4911         case QVariant::ULongLong: actual = actual.arg(var.toULongLong(), width, base, fillChar); break;
4912         default: QVERIFY(0); break;
4913         }
4914     }
4915
4916     QCOMPARE(actual, expected);
4917 }
4918
4919 static inline int sign(int x)
4920 {
4921     return x == 0 ? 0 : (x < 0 ? -1 : 1);
4922 }
4923
4924 void tst_QString::compare_data()
4925 {
4926     QTest::addColumn<QString>("s1");
4927     QTest::addColumn<QString>("s2");
4928     QTest::addColumn<int>("csr"); // case sensitive result
4929     QTest::addColumn<int>("cir"); // case insensitive result
4930
4931
4932     // null strings
4933     QTest::newRow("data0") << QString("") << QString("") << 0 << 0;
4934     QTest::newRow("data1") << QString("a") << QString("") << 1 << 1;
4935     QTest::newRow("data2") << QString("") << QString("a") << -1 << -1;
4936
4937     // equal length
4938     QTest::newRow("data3") << QString("abc") << QString("abc") << 0 << 0;
4939     QTest::newRow("data4") << QString("abC") << QString("abc") << -1 << 0;
4940     QTest::newRow("data5") << QString("abc") << QString("abC") << 1 << 0;
4941
4942     // different length
4943     QTest::newRow("data6") << QString("abcdef") << QString("abc") << 1 << 1;
4944     QTest::newRow("data7") << QString("abCdef") << QString("abc") << -1 << 1;
4945     QTest::newRow("data8") << QString("abc") << QString("abcdef") << -1 << -1;
4946
4947     QString upper;
4948     upper += QChar(QChar::highSurrogate(0x10400));
4949     upper += QChar(QChar::lowSurrogate(0x10400));
4950     QString lower;
4951     lower += QChar(QChar::highSurrogate(0x10428));
4952     lower += QChar(QChar::lowSurrogate(0x10428));
4953     QTest::newRow("data8") << upper << lower << -1 << 0;
4954
4955     // embedded nulls
4956     // These don't work as of now. It's OK that these don't work since \0 is not a valid unicode
4957     /*QTest::newRow("data10") << QString(QByteArray("\0", 1)) << QString(QByteArray("\0", 1)) << 0 << 0;
4958     QTest::newRow("data11") << QString(QByteArray("\0", 1)) << QString("") << 1 << 1;
4959     QTest::newRow("data12") << QString("") << QString(QByteArray("\0", 1)) << -1 << -1;
4960     QTest::newRow("data13") << QString("ab\0c") << QString(QByteArray("ab\0c", 4)) << 0 << 0;
4961     QTest::newRow("data14") << QString(QByteArray("ab\0c", 4)) << QString("abc") << -1 << -1;
4962     QTest::newRow("data15") << QString("abc") << QString(QByteArray("ab\0c", 4)) << 1 << 1;*/
4963 }
4964
4965 static bool isLatin(const QString &s)
4966 {
4967     for (int i = 0; i < s.length(); ++i)
4968         if (s.at(i).unicode() > 0xff)
4969             return false;
4970     return true;
4971 }
4972
4973 void tst_QString::compare()
4974 {
4975     QFETCH(QString, s1);
4976     QFETCH(QString, s2);
4977     QFETCH(int, csr);
4978     QFETCH(int, cir);
4979
4980     QStringRef r1(&s1, 0, s1.length());
4981     QStringRef r2(&s2, 0, s2.length());
4982
4983     QCOMPARE(sign(QString::compare(s1, s2)), csr);
4984     QCOMPARE(sign(QStringRef::compare(r1, r2)), csr);
4985     QCOMPARE(sign(s1.compare(s2)), csr);
4986     QCOMPARE(sign(s1.compare(r2)), csr);
4987     QCOMPARE(sign(r1.compare(r2)), csr);
4988
4989     QCOMPARE(sign(s1.compare(s2, Qt::CaseSensitive)), csr);
4990     QCOMPARE(sign(s1.compare(s2, Qt::CaseInsensitive)), cir);
4991     QCOMPARE(sign(s1.compare(r2, Qt::CaseSensitive)), csr);
4992     QCOMPARE(sign(s1.compare(r2, Qt::CaseInsensitive)), cir);
4993     QCOMPARE(sign(r1.compare(r2, Qt::CaseSensitive)), csr);
4994     QCOMPARE(sign(r1.compare(r2, Qt::CaseInsensitive)), cir);
4995
4996     QCOMPARE(sign(QString::compare(s1, s2, Qt::CaseSensitive)), csr);
4997     QCOMPARE(sign(QString::compare(s1, s2, Qt::CaseInsensitive)), cir);
4998     QCOMPARE(sign(QString::compare(s1, r2, Qt::CaseSensitive)), csr);
4999     QCOMPARE(sign(QString::compare(s1, r2, Qt::CaseInsensitive)), cir);
5000     QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseSensitive)), csr);
5001     QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseInsensitive)), cir);
5002
5003     if (csr == 0) {
5004         QVERIFY(qHash(s1) == qHash(s2));
5005         QVERIFY(qHash(s1) == qHash(r2));
5006         QVERIFY(qHash(r1) == qHash(s2));
5007         QVERIFY(qHash(r1) == qHash(r2));
5008     }
5009
5010     if (!cir) {
5011         QCOMPARE(s1.toCaseFolded(), s2.toCaseFolded());
5012     }
5013
5014     if (isLatin(s2)) {
5015         QCOMPARE(sign(QString::compare(s1, QLatin1String(s2.toLatin1()))), csr);
5016         QCOMPARE(sign(QString::compare(s1, QLatin1String(s2.toLatin1()), Qt::CaseInsensitive)), cir);
5017         QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(s2.toLatin1()))), csr);
5018         QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(s2.toLatin1()), Qt::CaseInsensitive)), cir);
5019         QByteArray l1 = s2.toLatin1();
5020         l1 += "x";
5021         QLatin1String l1str(l1.constData(), l1.size() - 1);
5022         QCOMPARE(sign(QString::compare(s1, l1str)), csr);
5023         QCOMPARE(sign(QString::compare(s1, l1str, Qt::CaseInsensitive)), cir);
5024         QCOMPARE(sign(QStringRef::compare(r1, l1str)), csr);
5025         QCOMPARE(sign(QStringRef::compare(r1, l1str, Qt::CaseInsensitive)), cir);
5026     }
5027
5028     if (isLatin(s1)) {
5029         QCOMPARE(sign(QString::compare(QLatin1String(s1.toLatin1()), s2)), csr);
5030         QCOMPARE(sign(QString::compare(QLatin1String(s1.toLatin1()), s2, Qt::CaseInsensitive)), cir);
5031     }
5032 }
5033
5034 void tst_QString::resizeAfterFromRawData()
5035 {
5036     QString buffer("hello world");
5037
5038     QString array = QString::fromRawData(buffer.constData(), buffer.size());
5039     QVERIFY(array.constData() == buffer.constData());
5040     array.resize(5);
5041     QVERIFY(array.constData() == buffer.constData());
5042 }
5043
5044 void tst_QString::resizeAfterReserve()
5045 {
5046
5047     QString s;
5048     s.reserve(100);
5049
5050     s += "hello world";
5051
5052     // resize should not affect capacity
5053     s.resize(s.size());
5054     QVERIFY(s.capacity() == 100);
5055
5056     // but squeeze does
5057     s.squeeze();
5058     QVERIFY(s.capacity() == s.size());
5059
5060     // clear does too
5061     s.clear();
5062     QVERIFY(s.capacity() == 0);
5063
5064     // test resize(0) border case
5065     s.reserve(100);
5066     s += "hello world";
5067     s.resize(0);
5068     QVERIFY(s.capacity() == 100);
5069 }
5070
5071 void tst_QString::resizeWithNegative() const
5072 {
5073     {
5074         QString string(QLatin1String("input"));
5075         string.resize(-1);
5076         QCOMPARE(string, QString());
5077     }
5078
5079     {
5080         QString string(QLatin1String("input"));
5081         string.resize(-9099);
5082         QCOMPARE(string, QString());
5083     }
5084
5085     {
5086         /* Example code from customer. */
5087         QString s(QLatin1String("hola"));
5088         s.reserve(1);
5089         s.resize(-1);
5090         QCOMPARE(s, QString());
5091     }
5092 }
5093
5094 void tst_QString::truncateWithNegative() const
5095 {
5096     {
5097         QString string(QLatin1String("input"));
5098         string.truncate(-1);
5099         QCOMPARE(string, QString());
5100     }
5101
5102     {
5103         QString string(QLatin1String("input"));
5104         string.truncate(-9099);
5105         QCOMPARE(string, QString());
5106     }
5107
5108     {
5109         /* Example code from customer. */
5110         QString test(QLatin1String("c"));
5111
5112         test.replace(QRegExp(QLatin1String("c")), QLatin1String("z"));
5113         test.truncate(-1);
5114         QCOMPARE(test, QString());
5115     }
5116 }
5117
5118 void tst_QString::QCharRefMutableUnicode() const
5119 {
5120     QString str;
5121     str.resize(3);
5122     str[0].unicode() = 115;
5123     str[1].unicode() = 116;
5124     str[2].unicode() = 114;
5125
5126     QCOMPARE(str, QString::fromLatin1("str"));
5127 }
5128
5129 void tst_QString::QCharRefDetaching() const
5130 {
5131     {
5132         QString str = QString::fromLatin1("str");
5133         QString copy;
5134         copy[0] = QLatin1Char('S');
5135
5136         QCOMPARE(str, QString::fromLatin1("str"));
5137     }
5138
5139     {
5140         ushort buf[] = { 's', 't', 'r' };
5141         QString str = QString::fromRawData((const QChar *)buf, 3);
5142         str[0] = QLatin1Char('S');
5143
5144         QCOMPARE(buf[0], ushort('s'));
5145     }
5146
5147     {
5148         static const ushort buf[] = { 's', 't', 'r' };
5149         QString str = QString::fromRawData((const QChar *)buf, 3);
5150
5151         // this causes a crash in most systems if the detaching doesn't work
5152         str[0] = QLatin1Char('S');
5153
5154         QCOMPARE(buf[0], ushort('s'));
5155     }
5156 }
5157
5158 void tst_QString::sprintfZU() const
5159 {
5160     {
5161         QString string;
5162         size_t s = 6;
5163         string.sprintf("%zu", s);
5164         QCOMPARE(string, QString::fromLatin1("6"));
5165     }
5166
5167     {
5168         QString string;
5169         string.sprintf("%s\n", "foo");
5170         QCOMPARE(string, QString::fromLatin1("foo\n"));
5171     }
5172
5173     {
5174         /* This code crashed. I don't know how to reduce it further. In other words,
5175          * both %zu and %s needs to be present. */
5176         size_t s = 6;
5177         QString string;
5178         string.sprintf("%zu%s", s, "foo");
5179         QCOMPARE(string, QString::fromLatin1("6foo"));
5180     }
5181
5182     {
5183         size_t s = 6;
5184         QString string;
5185         string.sprintf("%zu %s\n", s, "foo");
5186         QCOMPARE(string, QString::fromLatin1("6 foo\n"));
5187     }
5188 }
5189
5190 void tst_QString::repeatedSignature() const
5191 {
5192     /* repated() should be a const member. */
5193     const QString string;
5194     string.repeated(3);
5195 }
5196
5197 void tst_QString::repeated() const
5198 {
5199     QFETCH(QString, string);
5200     QFETCH(QString, expected);
5201     QFETCH(int, count);
5202
5203     QCOMPARE(string.repeated(count), expected);
5204 }
5205
5206 void tst_QString::repeated_data() const
5207 {
5208     QTest::addColumn<QString>("string" );
5209     QTest::addColumn<QString>("expected" );
5210     QTest::addColumn<int>("count" );
5211
5212     /* Empty strings. */
5213     QTest::newRow("data1")
5214         << QString()
5215         << QString()
5216         << 0;
5217
5218     QTest::newRow("data2")
5219         << QString()
5220         << QString()
5221         << -1004;
5222
5223     QTest::newRow("data3")
5224         << QString()
5225         << QString()
5226         << 1;
5227
5228     QTest::newRow("data4")
5229         << QString()
5230         << QString()
5231         << 5;
5232
5233     /* On simple string. */
5234     QTest::newRow("data5")
5235         << QString(QLatin1String("abc"))
5236         << QString()
5237         << -1004;
5238
5239     QTest::newRow("data6")
5240         << QString(QLatin1String("abc"))
5241         << QString()
5242         << -1;
5243
5244     QTest::newRow("data7")
5245         << QString(QLatin1String("abc"))
5246         << QString()
5247         << 0;
5248
5249     QTest::newRow("data8")
5250         << QString(QLatin1String("abc"))
5251         << QString(QLatin1String("abc"))
5252         << 1;
5253
5254     QTest::newRow("data9")
5255         << QString(QLatin1String("abc"))
5256         << QString(QLatin1String("abcabc"))
5257         << 2;
5258
5259     QTest::newRow("data10")
5260         << QString(QLatin1String("abc"))
5261         << QString(QLatin1String("abcabcabc"))
5262         << 3;
5263
5264     QTest::newRow("data11")
5265         << QString(QLatin1String("abc"))
5266         << QString(QLatin1String("abcabcabcabc"))
5267         << 4;
5268 }
5269
5270 void tst_QString::compareRef()
5271 {
5272     QString a = "ABCDEFGH";
5273
5274     QCOMPARE(QStringRef(&a, 1, 2).compare(QLatin1String("BC")), 0);
5275     QVERIFY(QStringRef(&a, 1, 2).compare(QLatin1String("BCD")) < 0);
5276     QCOMPARE(QStringRef(&a, 1, 2).compare(QLatin1String("Bc"), Qt::CaseInsensitive), 0);
5277     QVERIFY(QStringRef(&a, 1, 2).compare(QLatin1String("bCD"), Qt::CaseInsensitive) < 0);
5278
5279     QCOMPARE(QStringRef(&a, 1, 2).compare(QString::fromLatin1("BC")), 0);
5280     QVERIFY(QStringRef(&a, 1, 2).compare(QString::fromLatin1("BCD")) < 0);
5281     QCOMPARE(QStringRef(&a, 1, 2).compare(QString::fromLatin1("Bc"), Qt::CaseInsensitive), 0);
5282     QVERIFY(QStringRef(&a, 1, 2).compare(QString::fromLatin1("bCD"), Qt::CaseInsensitive) < 0);
5283
5284     QCOMPARE(QString::fromLatin1("BC").compare(QStringRef(&a, 1, 2)), 0);
5285     QVERIFY(QString::fromLatin1("BCD").compare(QStringRef(&a, 1, 2)) > 0);
5286     QCOMPARE(QString::fromLatin1("Bc").compare(QStringRef(&a, 1, 2), Qt::CaseInsensitive), 0);
5287     QVERIFY(QString::fromLatin1("bCD").compare(QStringRef(&a, 1, 2), Qt::CaseInsensitive) > 0);
5288
5289     QCOMPARE(QStringRef(&a, 1, 2).compare(QStringRef(&a, 1, 2)), 0);
5290     QVERIFY(QStringRef(&a, 1, 2).compare(QStringRef(&a, 1, 3)) < 0);
5291     QCOMPARE(QStringRef(&a, 1, 2).compare(QStringRef(&a, 1, 2), Qt::CaseInsensitive), 0);
5292     QVERIFY(QStringRef(&a, 1, 2).compare(QStringRef(&a, 1, 3), Qt::CaseInsensitive) < 0);
5293
5294     QString a2 = "ABCDEFGh";
5295     QCOMPARE(QStringRef(&a2, 1, 2).compare(QStringRef(&a, 1, 2)), 0);
5296     QVERIFY(QStringRef(&a2, 1, 2).compare(QStringRef(&a, 1, 3)) < 0);
5297     QCOMPARE(QStringRef(&a2, 1, 2).compare(QStringRef(&a, 1, 2), Qt::CaseInsensitive), 0);
5298     QVERIFY(QStringRef(&a2, 1, 2).compare(QStringRef(&a, 1, 3), Qt::CaseInsensitive) < 0);
5299 }
5300
5301 void tst_QString::arg_locale()
5302 {
5303     QLocale l(QLocale::English, QLocale::UnitedKingdom);
5304     l.setNumberOptions(QLocale::OmitGroupSeparator);
5305     QLocale::setDefault(l);
5306     QString str("*%L1*%L2*");
5307     str = str.arg(123456).arg(1234.56);
5308     QCOMPARE(str, QString::fromLatin1("*123456*1234.56*"));
5309     QLocale::setDefault(QLocale::C);
5310 }
5311
5312 void tst_QString::toUpperLower_icu()
5313 {
5314 #ifndef QT_USE_ICU
5315     QSKIP("Qt was built without ICU support");
5316 #endif
5317
5318     QString s = QString::fromLatin1("i");
5319
5320     QCOMPARE(s.toUpper(), QString::fromLatin1("I"));
5321     QCOMPARE(s.toLower(), QString::fromLatin1("i"));
5322
5323     QLocale::setDefault(QLocale(QLocale::Turkish, QLocale::Turkey));
5324
5325     QCOMPARE(s.toUpper(), QString::fromLatin1("I"));
5326     QCOMPARE(s.toLower(), QString::fromLatin1("i"));
5327
5328     // turkish locale has a capital I with a dot (U+0130, utf8 c4b0)
5329     QLocale l;
5330
5331     QCOMPARE(l.toUpper(s), QString::fromUtf8("\xc4\xb0"));
5332     QCOMPARE(l.toLower(QString::fromUtf8("\xc4\xb0")), s);
5333
5334     // nothing should happen here
5335     QCOMPARE(l.toLower(s), s);
5336     QCOMPARE(l.toUpper(QString::fromLatin1("I")), QString::fromLatin1("I"));
5337
5338     // U+0131, utf8 c4b1 is the lower-case i without a dot
5339     QString sup = QString::fromUtf8("\xc4\xb1");
5340
5341     QCOMPARE(l.toUpper(sup), QString::fromLatin1("I"));
5342     QCOMPARE(l.toLower(QString::fromLatin1("I")), sup);
5343
5344     // nothing should happen here
5345     QCOMPARE(l.toLower(sup), sup);
5346     QCOMPARE(l.toLower(QString::fromLatin1("i")), QString::fromLatin1("i"));
5347
5348     // the cleanup function will restore the default locale
5349 }
5350
5351 void tst_QString::literals()
5352 {
5353 #if defined(QT_UNICODE_LITERAL) && (defined(Q_COMPILER_LAMBDA) || defined(Q_CC_GNU))
5354     QString str(QStringLiteral("abcd"));
5355
5356     QVERIFY(str.length() == 4);
5357     QVERIFY(str == QLatin1String("abcd"));
5358     QVERIFY(str.data_ptr()->ref.isStatic());
5359     QVERIFY(str.data_ptr()->offset == sizeof(QStringData));
5360
5361     const QChar *s = str.constData();
5362     QString str2 = str;
5363     QVERIFY(str2.constData() == s);
5364
5365     // detach on non const access
5366     QVERIFY(str.data() != s);
5367
5368     QVERIFY(str2.constData() == s);
5369     QVERIFY(str2.data() != s);
5370
5371 #else
5372     QSKIP("Only tested on c++0x compliant compiler or gcc");
5373 #endif
5374 }
5375
5376 void tst_QString::eightBitLiterals_data()
5377 {
5378     QTest::addColumn<QByteArray>("data");
5379     QTest::addColumn<QString>("stringData");
5380
5381     QTest::newRow("null") << QByteArray() << QString();
5382     QTest::newRow("empty") << QByteArray("") << QString("");
5383     QTest::newRow("regular") << QByteArray("foo") << "foo";
5384     QTest::newRow("non-ascii") << QByteArray("\xc3\xa9") << QString::fromLatin1("\xe9");
5385 }
5386
5387 void tst_QString::eightBitLiterals()
5388 {
5389     QFETCH(QByteArray, data);
5390     QFETCH(QString, stringData);
5391
5392     {
5393         QString s(data);
5394         QCOMPARE(s, stringData);
5395     }
5396     {
5397         QString s(data.constData());
5398         QCOMPARE(s, stringData);
5399     }
5400     {
5401         QString s;
5402         s = data;
5403         QCOMPARE(s, stringData);
5404     }
5405     {
5406         QString s;
5407         s = data.constData();
5408         QCOMPARE(s, stringData);
5409     }
5410     {
5411         QString s;
5412         s.append(data);
5413         QCOMPARE(s, stringData);
5414     }
5415     {
5416         QString s;
5417         s.append(data.constData());
5418         QCOMPARE(s, stringData);
5419     }
5420     {
5421         QString s;
5422         s += data;
5423         QCOMPARE(s, stringData);
5424     }
5425     {
5426         QString s;
5427         s += data.constData();
5428         QCOMPARE(s, stringData);
5429     }
5430     {
5431         QString s;
5432         s.prepend(data);
5433         QCOMPARE(s, stringData);
5434     }
5435     {
5436         QString s;
5437         s.prepend(data.constData());
5438         QCOMPARE(s, stringData);
5439     }
5440     {
5441         QString s = QString() + data;
5442         QCOMPARE(s, stringData);
5443     }
5444     {
5445         QString s = QString() + data.constData();
5446         QCOMPARE(s, stringData);
5447     }
5448     {
5449         QString s = data + QString();
5450         QCOMPARE(s, stringData);
5451     }
5452     {
5453         QString s = QString() % data;
5454         QCOMPARE(s, stringData);
5455     }
5456     {
5457         QString s = QString() % data.constData();
5458         QCOMPARE(s, stringData);
5459     }
5460     {
5461         QString s = data % QString();
5462         QCOMPARE(s, stringData);
5463     }
5464
5465     {
5466         QVERIFY(stringData == data);
5467         QVERIFY(stringData == data.constData());
5468         QVERIFY(!(stringData != data));
5469         QVERIFY(!(stringData != data.constData()));
5470         QVERIFY(!(stringData < data));
5471         QVERIFY(!(stringData < data.constData()));
5472         QVERIFY(!(stringData > data));
5473         QVERIFY(!(stringData > data.constData()));
5474         QVERIFY(stringData <= data);
5475         QVERIFY(stringData <= data.constData());
5476         QVERIFY(stringData >= data);
5477         QVERIFY(stringData >= data.constData());
5478     }
5479 }
5480
5481 void tst_QString::reserve()
5482 {
5483     QString nil1, nil2;
5484     nil1.reserve(0);
5485     nil2.squeeze();
5486     nil1.squeeze();
5487     nil2.reserve(0);
5488 }
5489
5490 void tst_QString::toHtmlEscaped_data()
5491 {
5492     QTest::addColumn<QString>("original");
5493     QTest::addColumn<QString>("expected");
5494
5495     QTest::newRow("1") << "Hello World\n" << "Hello World\n";
5496     QTest::newRow("2") << "#include <QtCore>" << "#include &lt;QtCore&gt;";
5497     QTest::newRow("3") << "<p class=\"cool\"><a href=\"http://example.com/?foo=bar&amp;bar=foo\">plop --&gt; </a></p>"
5498                        << "&lt;p class=&quot;cool&quot;&gt;&lt;a href=&quot;http://example.com/?foo=bar&amp;amp;bar=foo&quot;&gt;plop --&amp;gt; &lt;/a&gt;&lt;/p&gt;";
5499     QTest::newRow("4") << QString::fromUtf8("<\320\222\321\201>") << QString::fromUtf8("&lt;\320\222\321\201&gt;");
5500 }
5501
5502 void tst_QString::toHtmlEscaped()
5503 {
5504     QFETCH(QString, original);
5505     QFETCH(QString, expected);
5506
5507     QCOMPARE(original.toHtmlEscaped(), expected);
5508 }
5509
5510 void tst_QString::operatorGreaterWithQLatin1String()
5511 {
5512     QLatin1String latin1foo("fooZZ", 3);
5513     QString stringfoo = QString::fromLatin1("foo");
5514     QVERIFY(stringfoo >= latin1foo);
5515     QVERIFY(!(stringfoo > latin1foo));
5516     QVERIFY(stringfoo <= latin1foo);
5517     QVERIFY(!(stringfoo < latin1foo));
5518 }
5519
5520 void tst_QString::compareQLatin1Strings()
5521 {
5522     QLatin1String abc("abc");
5523     QLatin1String abcd("abcd");
5524     QLatin1String cba("cba");
5525     QLatin1String de("de");
5526
5527     QVERIFY(abc == abc);
5528     QVERIFY(!(abc == cba));
5529     QVERIFY(!(cba == abc));
5530     QVERIFY(!(abc == abcd));
5531     QVERIFY(!(abcd == abc));
5532
5533     QVERIFY(abc != cba);
5534     QVERIFY(!(abc != abc));
5535     QVERIFY(cba != abc);
5536     QVERIFY(abc != abcd);
5537     QVERIFY(abcd != abc);
5538
5539     QVERIFY(abc < abcd);
5540     QVERIFY(abc < cba);
5541     QVERIFY(abc < de);
5542     QVERIFY(abcd < cba);
5543     QVERIFY(!(abc < abc));
5544     QVERIFY(!(abcd < abc));
5545     QVERIFY(!(de < cba));
5546
5547     QVERIFY(abcd > abc);
5548     QVERIFY(cba > abc);
5549     QVERIFY(de > abc);
5550     QVERIFY(!(abc > abc));
5551     QVERIFY(!(abc > abcd));
5552     QVERIFY(!(abcd > cba));
5553
5554     QVERIFY(abc <= abc);
5555     QVERIFY(abc <= abcd);
5556     QVERIFY(abc <= cba);
5557     QVERIFY(abc <= de);
5558     QVERIFY(!(abcd <= abc));
5559     QVERIFY(!(cba <= abc));
5560     QVERIFY(!(cba <= abcd));
5561     QVERIFY(!(de <= abc));
5562
5563     QVERIFY(abc >= abc);
5564     QVERIFY(abcd >= abc);
5565     QVERIFY(!(abc >= abcd));
5566     QVERIFY(cba >= abc);
5567     QVERIFY(!(abc >= cba));
5568     QVERIFY(de >= abc);
5569     QVERIFY(!(abc >= de));
5570
5571     QLatin1String subfoo("fooZZ", 3);
5572     QLatin1String foo("foo");
5573     QVERIFY(subfoo == foo);
5574     QVERIFY(foo == subfoo);
5575     QVERIFY(!(subfoo != foo));
5576     QVERIFY(!(foo != subfoo));
5577     QVERIFY(!(foo < subfoo));
5578     QVERIFY(!(subfoo < foo));
5579     QVERIFY(foo >= subfoo);
5580     QVERIFY(subfoo >= foo);
5581     QVERIFY(!(foo > subfoo));
5582     QVERIFY(!(subfoo > foo));
5583     QVERIFY(foo <= subfoo);
5584     QVERIFY(subfoo <= foo);
5585
5586     QLatin1String subabc("abcZZ", 3);
5587     QLatin1String subab("abcZZ", 2);
5588     QVERIFY(subabc != subab);
5589     QVERIFY(subab != subabc);
5590     QVERIFY(!(subabc == subab));
5591     QVERIFY(!(subab == subabc));
5592     QVERIFY(subab < subabc);
5593     QVERIFY(!(subabc < subab));
5594     QVERIFY(subabc > subab);
5595     QVERIFY(!(subab > subabc));
5596     QVERIFY(subab <= subabc);
5597     QVERIFY(!(subabc <= subab));
5598     QVERIFY(subabc >= subab);
5599     QVERIFY(!(subab >= subabc));
5600 }
5601
5602 void tst_QString::fromQLatin1StringWithLength()
5603 {
5604     QLatin1String latin1foo("foobar", 3);
5605     QString foo(latin1foo);
5606     QCOMPARE(foo.size(), latin1foo.size());
5607     QCOMPARE(foo, QString::fromLatin1("foo"));
5608 }
5609
5610 void tst_QString::assignQLatin1String()
5611 {
5612     QString empty = QLatin1String("");
5613     QVERIFY(empty.isEmpty());
5614     QVERIFY(!empty.isNull());
5615
5616     QString null = QLatin1String(0);
5617     QVERIFY(null.isEmpty());
5618     QVERIFY(null.isNull());
5619
5620     QLatin1String latin1foo("foo");
5621     QString foo = latin1foo;
5622     QCOMPARE(foo.size(), latin1foo.size());
5623     QCOMPARE(foo, QString::fromLatin1("foo"));
5624
5625     QLatin1String latin1subfoo("foobar", 3);
5626     foo = latin1subfoo;
5627     QCOMPARE(foo.size(), latin1subfoo.size());
5628     QCOMPARE(foo, QString::fromLatin1("foo"));
5629
5630 }
5631
5632 void tst_QString::isRightToLeft_data()
5633 {
5634     QTest::addColumn<QString>("unicode");
5635     QTest::addColumn<bool>("rtl");
5636
5637     QTest::newRow("null") << QString() << false;
5638     QTest::newRow("empty") << QString("") << false;
5639
5640     QTest::newRow("numbers-only") << QString("12345") << false;
5641     QTest::newRow("latin1-only") << QString("hello") << false;
5642     QTest::newRow("numbers-latin1") << (QString("12345") + QString("hello")) << false;
5643
5644     static const ushort unicode1[] = { 0x627, 0x627 };
5645     QTest::newRow("arabic-only") << QString::fromUtf16(unicode1, 2) << true;
5646     QTest::newRow("numbers-arabic") << (QString("12345") + QString::fromUtf16(unicode1, 2)) << true;
5647     QTest::newRow("numbers-latin1-arabic") << (QString("12345") + QString("hello") + QString::fromUtf16(unicode1, 2)) << false;
5648     QTest::newRow("numbers-arabic-latin1") << (QString("12345") + QString::fromUtf16(unicode1, 2) + QString("hello")) << true;
5649
5650     static const ushort unicode2[] = { QChar::highSurrogate(0xE01DAu), QChar::lowSurrogate(0xE01DAu), QChar::highSurrogate(0x2F800u), QChar::lowSurrogate(0x2F800u) };
5651     QTest::newRow("surrogates-VS-CJK") << QString::fromUtf16(unicode2, 4) << false;
5652
5653     static const ushort unicode3[] = { QChar::highSurrogate(0x10800u), QChar::lowSurrogate(0x10800u), QChar::highSurrogate(0x10805u), QChar::lowSurrogate(0x10805u) };
5654     QTest::newRow("surrogates-cypriot") << QString::fromUtf16(unicode3, 4) << true;
5655 }
5656
5657 void tst_QString::isRightToLeft()
5658 {
5659     QFETCH(QString, unicode);
5660     QFETCH(bool, rtl);
5661
5662     QCOMPARE(unicode.isRightToLeft(), rtl);
5663 }
5664
5665 QTEST_APPLESS_MAIN(tst_QString)
5666
5667 #include "tst_qstring.moc"