1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifdef QT_NO_CAST_FROM_ASCII
43 # undef QT_NO_CAST_FROM_ASCII
45 #ifdef QT_NO_CAST_TO_ASCII
46 # undef QT_NO_CAST_TO_ASCII
48 #ifdef QT_ASCII_CAST_WARNINGS
49 # undef QT_ASCII_CAST_WARNINGS
52 #include <QtTest/QtTest>
54 #include <qregularexpression.h>
55 #include <qtextcodec.h>
56 #include <qtextstream.h>
57 #include <qstringlist.h>
58 #include <qstringmatcher.h>
59 #include <qbytearraymatcher.h>
66 Q_DECLARE_METATYPE(qlonglong)
68 #define CREATE_REF(string) \
69 const QString padded = QString::fromLatin1(" %1 ").arg(string); \
70 QStringRef ref = padded.midRef(1, padded.size() - 2);
72 class tst_QString : public QObject
83 void check_QTextIOStream();
84 void check_QTextStream();
85 void check_QDataStream();
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();
123 void prepend_bytearray_data();
124 void prepend_bytearray();
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();
135 void simplified_data();
141 void rightJustified();
142 void leftJustified();
152 void lastIndexOf_data();
156 void indexOf2_data();
158 void indexOf3_data();
164 void constructorQByteArray_data();
165 void constructorQByteArray();
174 void fromUtf8_data();
177 void fromLocal8Bit_data();
178 void fromLocal8Bit();
179 void local8Bit_data();
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();
195 void arg_fillChar_data();
197 void capacity_data();
201 void double_conversion_data();
202 void double_conversion();
203 void integer_conversion_data();
204 void integer_conversion();
205 void tortureSprintfDouble();
207 void localeAwareCompare_data();
208 void localeAwareCompare();
211 void split_regexp_data();
213 void fromUtf16_data();
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;
231 void toUpperLower_icu();
233 void eightBitLiterals_data();
234 void eightBitLiterals();
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();
246 template <class T> const T &verifyZeroTermination(const T &t) { return t; }
248 QString verifyZeroTermination(const QString &str)
250 // This test does some evil stuff, it's all supposed to work.
252 QString::DataPtr strDataPtr = const_cast<QString &>(str).data_ptr();
254 // Skip if isStatic() or fromRawData(), as those offer no guarantees
255 if (strDataPtr->ref.isStatic()
256 || strDataPtr->offset != QString().data_ptr()->offset)
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'));
266 // Skip mutating checks on shared strings
267 if (strDataPtr->ref.isShared())
270 const QChar *strData = str.constData();
271 const QString strCopy(strData, strSize); // Deep copy
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);
278 if (str != strCopy) {
279 return QString::fromAscii( "*** Result ('%1') differs from its copy "
280 "after null-terminator was replaced ***").arg(str);
282 const_cast<QChar *>(strData)[strSize] = QChar('\0'); // Restore sanity
287 // Overriding QTest's QCOMPARE, to check QString for null termination
289 #define QCOMPARE(actual, expected) \
291 if (!QTest::qCompare(verifyZeroTermination(actual), expected, \
292 #actual, #expected, __FILE__, __LINE__)) \
297 #define QTEST(actual, testElement) \
299 if (!QTest::qTest(verifyZeroTermination(actual), testElement, \
300 #actual, #testElement, __FILE__, __LINE__)) \
305 typedef QList<int> IntList;
307 Q_DECLARE_METATYPE(QList<QVariant>)
308 Q_DECLARE_METATYPE(IntList)
310 // This next bit is needed for the NAN and INF in string -> number conversion tests
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)
321 # define isnan(d) _isnan(d)
324 #if defined (Q_OS_MAC) && !defined isnan
325 #define isnan(d) __isnand(d)
327 #if defined (Q_OS_SOLARIS)
330 #if defined (Q_OS_OSF) && (defined(__DECC) || defined(__DECCXX))
331 # define INFINITY DBL_INFINITY
332 # define NAN DBL_QNAN
334 #if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
336 # define isnan(d) isnand(d)
343 # if Q_BYTE_ORDER == Q_BIG_ENDIAN
344 , ByteOrder = BigEndian
345 # elif Q_BYTE_ORDER == Q_LITTLE_ENDIAN
346 , ByteOrder = LittleEndian
348 # error "undefined byte order"
353 static const unsigned int one = 1;
354 static const bool ByteOrder = ((*((unsigned char *) &one) == 0) ? BigEndian : LittleEndian);
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()
361 if (ByteOrder == BigEndian)
362 return *reinterpret_cast<const double *>(be_inf_bytes);
363 return *reinterpret_cast<const double *>(le_inf_bytes);
365 # define INFINITY (::inf())
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()
372 if (ByteOrder == BigEndian)
373 return *reinterpret_cast<const double *>(be_nan_bytes);
374 return *reinterpret_cast<const double *>(le_nan_bytes);
376 # define NAN (::nan())
379 tst_QString::tst_QString()
381 QTextCodec::setCodecForLocale(QTextCodec::codecForName("ISO 8859-1"));
384 void tst_QString::cleanup()
386 QLocale::setDefault(QString("C"));
389 void tst_QString::remove_uint_uint_data()
391 replace_uint_uint_data();
394 void tst_QString::remove_string_data()
396 replace_string_data();
399 void tst_QString::remove_regexp_data()
401 replace_regexp_data();
404 void tst_QString::indexOf3_data()
409 void tst_QString::length_data()
411 QTest::addColumn<QString>("s1" );
412 QTest::addColumn<int>("res" );
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;
428 void tst_QString::replace_qchar_qchar_data()
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" );
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");
452 void tst_QString::replace_qchar_qchar()
454 QFETCH(QString, src);
455 QFETCH(QChar, before);
456 QFETCH(QChar, after);
458 QFETCH(QString, expected);
460 QCOMPARE(src.replace(before, after, Qt::CaseSensitivity(cs)), expected);
463 void tst_QString::replace_qchar_qstring_data()
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" );
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");
489 void tst_QString::replace_qchar_qstring()
491 QFETCH(QString, src);
492 QFETCH(QChar, before);
493 QFETCH(QString, after);
495 QFETCH(QString, expected);
497 QCOMPARE(src.replace(before, after, Qt::CaseSensitivity(cs)), expected);
500 void tst_QString::replace_uint_uint_data()
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" );
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");
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");
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");
537 void tst_QString::replace_string_data()
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" );
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;
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;
589 void tst_QString::replace_regexp_data()
591 QTest::addColumn<QString>("string" );
592 QTest::addColumn<QString>("regexp" );
593 QTest::addColumn<QString>("after" );
594 QTest::addColumn<QString>("result" );
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();
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");
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");
630 void tst_QString::utf8_data()
633 QTest::addColumn<QByteArray>("utf8" );
634 QTest::addColumn<QString>("res" );
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")
646 str = QChar( 0x20ac );
648 QTest::newRow( "str3" ) << QByteArray("\342\202\254 some text")
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;
665 void tst_QString::length()
667 QFETCH( QString, s1 );
668 QTEST( (int)s1.length(), "res" );
673 void tst_QString::acc_01()
677 QString bb; //bb((int)0);
678 QString c("String C");
695 QString e("String E");
699 QCOMPARE(e,(QString)"String E");
700 char text[]="String f";
703 QCOMPARE(f,(QString)"String f");
705 QCOMPARE(text[7],'!');
709 QCOMPARE(a,(QString)"A");
710 QCOMPARE(a.length(),1);
712 QCOMPARE(a,(QString)"AB");
713 QCOMPARE(a.length(),2);
715 QCOMPARE(a,(QString)"ABC");
716 QCOMPARE(a.length(),3);
720 QCOMPARE(a,(QString)"A");
721 QCOMPARE(a.length(),1);
723 QCOMPARE(a,(QString)"AB");
724 QCOMPARE(a.length(),2);
726 QCOMPARE(a,(QString)"ABC");
727 QCOMPARE(a.length(),3);
732 QCOMPARE(a,(QString)"223");
734 QCOMPARE(b,(QString)"456");
735 QCOMPARE(a,(QString)"253");
740 QCOMPARE(a,(QString)"TEXT");
741 QCOMPARE(a,(QString)t);
743 QCOMPARE(a,(QString)"XEXT");
746 QCOMPARE(a,(QString)"XEXT");
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");
755 // non-member operators
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");
782 QVERIFY(*a.toLatin1().constData() == '\0');
784 #if defined(Q_OS_WINCE)
786 QCoreApplication app(argc, 0);
788 QFile f("COMPARE.txt");
789 f.open(QIODevice::ReadOnly);
790 QTextStream ts( &f );
791 ts.setCodec(QTextCodec::codecForName("UTF-16"));
797 # pragma GCC diagnostic push
798 # pragma GCC diagnostic ignored "-Wformat-security"
801 void tst_QString::isNull()
806 const char *zero = 0;
808 QVERIFY(!a.isNull());
812 # pragma GCC diagnostic pop
815 void tst_QString::isEmpty()
818 QVERIFY(a.isEmpty());
819 QString c("Not empty");
820 QVERIFY(!c.isEmpty());
823 void tst_QString::constructor()
827 QString c("String C");
846 QVERIFY(a == (QString)"");
849 QCOMPARE(d,(QString)"String D");
852 QVERIFY( nullStr.isNull() );
853 QVERIFY( nullStr.isEmpty() );
855 QVERIFY( !empty.isNull() );
856 QVERIFY( empty.isEmpty() );
859 void tst_QString::constructorQByteArray_data()
861 QTest::addColumn<QByteArray>("src" );
862 QTest::addColumn<QString>("expected" );
864 QByteArray ba( 4, 0 );
870 QTest::newRow( "1" ) << ba << QString("COMP");
872 QByteArray ba1( 7, 0 );
881 QTest::newRow( "2" ) << ba1 << QString("abc");
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");
889 void tst_QString::constructorQByteArray()
891 QFETCH(QByteArray, src);
892 QFETCH(QString, expected);
895 QCOMPARE(str1.length(), expected.length());
896 QCOMPARE( str1, expected );
899 QCOMPARE( strBA, expected );
901 // test operator= too
902 if (src.constData()[src.length()] == '\0') {
904 str1 = src.constData();
905 QCOMPARE( str1, expected );
910 QCOMPARE( strBA, expected );
913 void tst_QString::STL()
915 std::string stdstr( "QString" );
917 QString stlqt = QString::fromStdString(stdstr);
918 QCOMPARE(stlqt, QString::fromLatin1(stdstr.c_str()));
919 QCOMPARE(stlqt.toStdString(), stdstr);
921 const wchar_t arr[] = {'h', 'e', 'l', 'l', 'o', 0};
922 std::wstring stlStr = arr;
924 QString s = QString::fromStdWString(stlStr);
926 QCOMPARE(s, QString::fromLatin1("hello"));
927 QCOMPARE(stlStr, s.toStdWString());
930 void tst_QString::truncate()
932 QString e("String E");
934 QCOMPARE(e,(QString)"Stri");
938 QCOMPARE(e,(QString)"");
939 QVERIFY(e.isEmpty());
940 QVERIFY(!e.isNull());
944 void tst_QString::fill()
948 QCOMPARE(e,(QString)"e");
951 QCOMPARE(f,(QString)"fff");
953 QCOMPARE(f,(QString)"FFF");
956 void tst_QString::sprintf()
959 a.sprintf("COMPARE");
960 QCOMPARE(a,(QString)"COMPARE");
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"));
976 S1.sprintf("%d %ld %f",i,l,f);
977 QCOMPARE(S1,QString("6 -2 4.023000"));
979 double d = -514.25683;
981 QCOMPARE(S1, QString("-514.256830"));
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");
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"));
995 a.sprintf("%s%n%s", "hello", &n1, "goodbye");
997 QCOMPARE(a, QString("hellogoodbye"));
999 a.sprintf("%s%s%lln%s", "foo", "bar", &n2, "whiz");
1000 QCOMPARE((int)n2, 6);
1001 QCOMPARE(a, QString("foobarwhiz"));
1005 indexOf() and indexOf02() test QString::indexOf(),
1006 QString::lastIndexOf(), and their QByteArray equivalents.
1008 lastIndexOf() tests QString::lastIndexOf() more in depth, but it
1009 should probably be rewritten to use a data table.
1012 void tst_QString::indexOf_data()
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" );
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;
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;
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;
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;
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;
1089 QTest::newRow( "data58" ) << s1 << s2 << 0 << false << 3;
1091 QTest::newRow( "data59" ) << s1 << s2 << 0 << false << 2;
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;
1103 QTest::newRow("BoyerMooreInsensitiveStressTest") << veryBigHaystack << veryBigHaystack << 0 << false << 0;
1107 void tst_QString::indexOf()
1109 QFETCH( QString, haystack );
1110 QFETCH( QString, needle );
1111 QFETCH( int, startpos );
1112 QFETCH( bool, bcs );
1113 QFETCH( int, resultpos );
1116 Qt::CaseSensitivity cs = bcs ? Qt::CaseSensitive : Qt::CaseInsensitive;
1118 bool needleIsLatin = (QString::fromLatin1(needle.toLatin1()) == needle);
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 );
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);
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 );
1148 QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption;
1150 options |= QRegularExpression::CaseInsensitiveOption;
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 );
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 );
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 );
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);
1181 void tst_QString::indexOf2_data()
1183 QTest::addColumn<QString>("haystack" );
1184 QTest::addColumn<QString>("needle" );
1185 QTest::addColumn<int>("resultpos" );
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;
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;
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;
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();
1232 void tst_QString::indexOf2()
1234 QFETCH( QString, haystack );
1235 QFETCH( QString, needle );
1236 QFETCH( int, resultpos );
1239 QByteArray chaystack = haystack.toLatin1();
1240 QByteArray cneedle = needle.toLatin1();
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) );
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) );
1264 void tst_QString::lastIndexOf_data()
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" );
1272 QString a = "ABCDEFGHIEfGEFG";
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;
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;
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;
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;
1303 QTest::newRow("27") << a << "" << a.size() << a.size() << false;
1304 QTest::newRow("28") << a << "" << a.size() + 10 << -1 << false;
1307 void tst_QString::lastIndexOf()
1309 QFETCH(QString, haystack);
1310 QFETCH(QString, needle);
1312 QFETCH(int, expected);
1313 QFETCH(bool, caseSensitive);
1316 Qt::CaseSensitivity cs = (caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
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);
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.
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);
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);
1347 QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption;
1349 options |= QRegularExpression::CaseInsensitiveOption;
1351 QRegularExpression re(QRegularExpression::escape(needle), options);
1352 QCOMPARE(haystack.lastIndexOf(re, from), expected);
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);
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);
1368 if (needle.size() == 1) {
1369 QCOMPARE(haystack.lastIndexOf(needle.at(0), from), expected);
1370 QCOMPARE(haystack.lastIndexOf(ref.at(0), from), expected);
1374 void tst_QString::count()
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);
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);
1402 void tst_QString::contains()
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]")));
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));
1430 void tst_QString::left()
1433 a="ABCDEFGHIEfGEFG"; // 15 chars
1434 QCOMPARE(a.left(3),(QString)"ABC");
1435 QVERIFY(!a.left(0).isNull());
1436 QCOMPARE(a.left(0),(QString)"");
1439 QVERIFY(n.left(3).isNull());
1440 QVERIFY(n.left(0).isNull());
1441 QVERIFY(n.left(0).isNull());
1444 QCOMPARE(l.left(-1), l);
1445 QCOMPARE(l.left(100), l);
1448 void tst_QString::leftRef()
1451 a="ABCDEFGHIEfGEFG"; // 15 chars
1452 QCOMPARE(a.leftRef(3).toString(),(QString)"ABC");
1454 QVERIFY(a.leftRef(0).toString().isEmpty());
1455 QCOMPARE(a.leftRef(0).toString(),(QString)"");
1458 QVERIFY(n.leftRef(3).toString().isEmpty());
1459 QVERIFY(n.leftRef(0).toString().isEmpty());
1460 QVERIFY(n.leftRef(0).toString().isEmpty());
1463 QCOMPARE(l.leftRef(-1).toString(), l);
1464 QCOMPARE(l.leftRef(100).toString(), l);
1467 void tst_QString::right()
1470 a="ABCDEFGHIEfGEFG"; // 15 chars
1471 QCOMPARE(a.right(3),(QString)"EFG");
1472 QCOMPARE(a.right(0),(QString)"");
1475 QVERIFY(n.right(3).isNull());
1476 QVERIFY(n.right(0).isNull());
1478 QString r = "Right";
1479 QCOMPARE(r.right(-1), r);
1480 QCOMPARE(r.right(100), r);
1483 void tst_QString::rightRef()
1486 a="ABCDEFGHIEfGEFG"; // 15 chars
1487 QCOMPARE(a.rightRef(3).toString(),(QString)"EFG");
1488 QCOMPARE(a.rightRef(0).toString(),(QString)"");
1491 QVERIFY(n.rightRef(3).toString().isEmpty());
1492 QVERIFY(n.rightRef(0).toString().isEmpty());
1494 QString r = "Right";
1495 QCOMPARE(r.rightRef(-1).toString(), r);
1496 QCOMPARE(r.rightRef(100).toString(), r);
1499 void tst_QString::mid()
1502 a="ABCDEFGHIEfGEFG"; // 15 chars
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());
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);
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());
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());
1555 QString x = "Nine pineapples";
1556 QCOMPARE(x.mid(5, 4), QString("pine"));
1557 QCOMPARE(x.mid(5), QString("pineapples"));
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);
1578 void tst_QString::midRef()
1581 a="ABCDEFGHIEfGEFG"; // 15 chars
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());
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);
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());
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());
1634 QString x = "Nine pineapples";
1635 QCOMPARE(x.midRef(5, 4).toString(), QString("pine"));
1636 QCOMPARE(x.midRef(5).toString(), QString("pineapples"));
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);
1657 void tst_QString::stringRef()
1660 a="ABCDEFGHIEfGEFG"; // 15 chars
1662 QVERIFY(QStringRef(&a, 0, 0) == (QString)"");
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));
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));
1678 QString s_alpha("alpha");
1679 QString s_beta("beta");
1680 QStringRef alpha(&s_alpha);
1681 QStringRef beta(&s_beta);
1683 QVERIFY(alpha < beta);
1684 QVERIFY(alpha <= beta);
1685 QVERIFY(alpha <= alpha);
1686 QVERIFY(beta > alpha);
1687 QVERIFY(beta >= alpha);
1688 QVERIFY(beta >= beta);
1690 QString s_alpha2("alpha");
1692 QMap<QStringRef, QString> map;
1693 map.insert(alpha, "alpha");
1694 map.insert(beta, "beta");
1695 QVERIFY(alpha == map.value(QStringRef(&s_alpha2)));
1697 QHash<QStringRef, QString> hash;
1698 hash.insert(alpha, "alpha");
1699 hash.insert(beta, "beta");
1701 QVERIFY(alpha == hash.value(QStringRef(&s_alpha2)));
1704 void tst_QString::leftJustified()
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");
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)"");
1725 void tst_QString::rightJustified()
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");
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");
1748 void tst_QString::toUpper()
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{"));
1767 QCOMPARE( QString(1, QChar(0xdf)).toUpper(), QString("SS"));
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);
1777 QCOMPARE( lower.toUpper(), upper);
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);
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);
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()));
1807 void tst_QString::toLower()
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{"));
1826 QCOMPARE( QString(1, QChar(0x130)).toLower(), QString(QString(1, QChar(0x69)) + QChar(0x307)));
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);
1836 QCOMPARE( upper.toLower(), lower);
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);
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);
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()));
1866 void tst_QString::toCaseFolded()
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{"));
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);
1893 QCOMPARE( upper.toCaseFolded(), lower);
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);
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()));
1918 void tst_QString::trimmed()
1922 QCOMPARE(a,(QString)"Text");
1923 QCOMPARE(a.trimmed(),(QString)"Text");
1924 QCOMPARE(a,(QString)"Text");
1926 QCOMPARE(a.trimmed(),(QString)"");
1927 QCOMPARE(a,(QString)" ");
1929 QCOMPARE(a.trimmed(),(QString)"a");
1932 void tst_QString::simplified_data()
1934 QTest::addColumn<QString>("full" );
1935 QTest::addColumn<QString>("simple" );
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";
1962 void tst_QString::simplified()
1964 QFETCH(QString, full);
1965 QFETCH(QString, simple);
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));
1973 QCOMPARE(result, simple);
1975 QVERIFY(result.isSharedWith(full));
1979 void tst_QString::insert()
1984 QCOMPARE(a.insert(1,'e'), QString("Yes"));
1985 QCOMPARE(a.insert(3,'!'), QString("Yes!"));
1986 QCOMPARE(a.insert(5,'?'), QString("Yes! ?"));
1989 QCOMPARE(a.insert(5,"DEF"), QString("ABC DEF"));
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"));
1997 QCOMPARE(a, QString("ABCABCABCABC"));
1998 QCOMPARE(a.insert(0,'<'), QString("<ABCABCABCABC"));
1999 QCOMPARE(a.insert(1,'>'), QString("<>ABCABCABCABC"));
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"));
2010 void tst_QString::append()
2013 a = "<>ABCABCABCABC";
2014 QCOMPARE(a.append(">"),(QString)"<>ABCABCABCABC>");
2017 void tst_QString::append_bytearray_data()
2019 QTest::addColumn<QString>("str" );
2020 QTest::addColumn<QByteArray>("ba" );
2021 QTest::addColumn<QString>("res" );
2023 QByteArray ba( 5, 0 );
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");
2035 // byte array with only a 0
2038 QByteArray ba2("foobar ");
2040 QTest::newRow( "emptyString" ) << QString("foobar ") << ba << QString(ba2);
2044 QTest::newRow( "emptyByteArray" ) << QString("foobar ") << ba << QString("foobar ");
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");
2051 void tst_QString::append_bytearray()
2054 QFETCH( QString, str );
2055 QFETCH( QByteArray, ba );
2059 QTEST( str, "res" );
2062 QFETCH( QString, str );
2063 QFETCH( QByteArray, ba );
2067 QTEST( str, "res" );
2070 QFETCH( QByteArray, ba );
2071 if (ba.constData()[ba.length()] == '\0') {
2072 QFETCH( QString, str );
2074 str.append(ba.constData());
2075 QTEST( str, "res" );
2079 void tst_QString::operator_pluseq_bytearray_data()
2081 append_bytearray_data();
2084 void tst_QString::operator_pluseq_bytearray()
2087 QFETCH( QString, str );
2088 QFETCH( QByteArray, ba );
2092 QTEST( str, "res" );
2095 QFETCH( QString, str );
2096 QFETCH( QByteArray, ba );
2100 QTEST( str, "res" );
2103 QFETCH( QByteArray, ba );
2104 if (ba.constData()[ba.length()] == '\0') {
2105 QFETCH( QString, str );
2107 str += ba.constData();
2108 QTEST( str, "res" );
2112 void tst_QString::operator_eqeq_bytearray_data()
2114 constructorQByteArray_data();
2117 void tst_QString::operator_eqeq_bytearray()
2119 QFETCH(QByteArray, src);
2120 QFETCH(QString, expected);
2122 QVERIFY(expected == src);
2123 QVERIFY(!(expected != src));
2125 if (src.constData()[src.length()] == '\0') {
2126 QVERIFY(expected == src.constData());
2127 QVERIFY(!(expected != src.constData()));
2131 void tst_QString::swap()
2137 QCOMPARE(s1,QLatin1String("s2"));
2138 QCOMPARE(s2,QLatin1String("s1"));
2141 void tst_QString::prepend()
2144 a = "<>ABCABCABCABC>";
2145 QCOMPARE(a.prepend("-"),(QString)"-<>ABCABCABCABC>");
2148 void tst_QString::prepend_bytearray_data()
2150 QTest::addColumn<QString>("str" );
2151 QTest::addColumn<QByteArray>("ba" );
2152 QTest::addColumn<QString>("res" );
2154 QByteArray ba( 5, 0 );
2160 // byte array with only a 0
2163 QTest::newRow( "emptyString" ) << QString("foobar ") << ba << QString("foobar ");
2167 QTest::newRow( "emptyByteArray" ) << QString(" foobar") << ba << QString(" foobar");
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");
2174 void tst_QString::prepend_bytearray()
2177 QFETCH( QString, str );
2178 QFETCH( QByteArray, ba );
2182 QFETCH( QString, res );
2183 QCOMPARE( str, res );
2186 QFETCH( QString, str );
2187 QFETCH( QByteArray, ba );
2191 QTEST( str, "res" );
2194 QFETCH( QByteArray, ba );
2195 if (ba.constData()[ba.length()] == '\0') {
2196 QFETCH( QString, str );
2198 str.prepend(ba.constData());
2199 QTEST( str, "res" );
2203 void tst_QString::replace_uint_uint()
2205 QFETCH( QString, string );
2206 QFETCH( int, index );
2208 QFETCH( QString, after );
2210 QEXPECT_FAIL("overflow", "QTBUG-22967: integer overflow if (index + len) > INT_MAX", Abort);
2212 QString s1 = string;
2213 s1.replace( (uint) index, (int) len, after );
2214 QTEST( s1, "result" );
2216 QString s2 = string;
2217 s2.replace( (uint) index, (uint) len, after.unicode(), after.length() );
2218 QTEST( s2, "result" );
2220 if ( after.length() == 1 ) {
2221 QString s3 = string;
2222 s3.replace( (uint) index, (uint) len, QChar(after[0]) );
2223 QTEST( s3, "result" );
2225 QString s4 = string;
2226 s4.replace( (uint) index, (uint) len, QChar(after[0]).toLatin1() );
2227 QTEST( s4, "result" );
2231 void tst_QString::replace_uint_uint_extra()
2234 This test is designed to be extremely slow if QString::replace() doesn't optimize the case
2235 len == after.size().
2237 QString str("dsfkljfdsjklsdjsfjklfsdjkldfjslkjsdfkllkjdsfjklsfdkjsdflkjlsdfjklsdfkjldsflkjsddlkj");
2238 for (int j = 1; j < 12; ++j)
2241 QString str2("aaaaaaaaaaaaaaaaaaaa");
2242 for (int i = 0; i < 2000000; ++i) {
2243 str.replace(10, 20, str2);
2247 Make sure that replacing with itself works.
2251 str.replace(0, str.length(), str);
2252 QVERIFY(copy == str);
2255 Make sure that replacing a part of oneself with itself works.
2257 QString str3("abcdefghij");
2258 str3.replace(0, 1, str3);
2259 QCOMPARE(str3, QString("abcdefghijbcdefghij"));
2261 QString str4("abcdefghij");
2262 str4.replace(1, 3, str4);
2263 QCOMPARE(str4, QString("aabcdefghijefghij"));
2265 QString str5("abcdefghij");
2266 str5.replace(8, 10, str5);
2267 QCOMPARE(str5, QString("abcdefghabcdefghij"));
2270 void tst_QString::replace_string()
2272 QFETCH( QString, string );
2273 QFETCH( QString, before );
2274 QFETCH( QString, after );
2275 QFETCH( bool, bcs );
2277 Qt::CaseSensitivity cs = bcs ? Qt::CaseSensitive : Qt::CaseInsensitive;
2279 if ( before.length() == 1 ) {
2280 QChar ch = before.at( 0 );
2282 QString s1 = string;
2283 s1.replace( ch, after, cs );
2284 QTEST( s1, "result" );
2286 if ( QChar(ch.toLatin1()) == ch ) {
2287 QString s2 = string;
2288 s2.replace( ch.toLatin1(), after, cs );
2289 QTEST( s2, "result" );
2293 QString s3 = string;
2294 s3.replace( before, after, cs );
2295 QTEST( s3, "result" );
2297 QString s4 = string;
2298 s4.replace( QRegExp(QRegExp::escape(before), cs), after );
2299 QTEST( s4, "result" );
2301 QString s5 = string;
2302 s5.replace(QRegExp(before, cs, QRegExp::FixedString), after);
2303 QTEST( s5, "result" );
2306 void tst_QString::replace_regexp()
2308 QFETCH( QString, string );
2309 QFETCH( QString, regexp );
2310 QFETCH( QString, after );
2312 QString s2 = string;
2313 s2.replace( QRegExp(regexp), after );
2314 QTEST( s2, "result" );
2316 s2.replace( QRegularExpression(regexp), after );
2317 QTEST( s2, "result" );
2320 void tst_QString::remove_uint_uint()
2322 QFETCH( QString, string );
2323 QFETCH( int, index );
2325 QFETCH( QString, after );
2327 if ( after.length() == 0 ) {
2328 QString s1 = string;
2329 s1.remove( (uint) index, (uint) len );
2330 QTEST( s1, "result" );
2332 QCOMPARE( 0, 0 ); // shut QtTest
2335 void tst_QString::remove_string()
2337 QFETCH( QString, string );
2338 QFETCH( QString, before );
2339 QFETCH( QString, after );
2340 QFETCH( bool, bcs );
2342 Qt::CaseSensitivity cs = bcs ? Qt::CaseSensitive : Qt::CaseInsensitive;
2344 if ( after.length() == 0 ) {
2345 if ( before.length() == 1 && cs ) {
2346 QChar ch = before.at( 0 );
2348 QString s1 = string;
2350 QTEST( s1, "result" );
2352 if ( QChar(ch.toLatin1()) == ch ) {
2353 QString s2 = string;
2355 QTEST( s2, "result" );
2359 QString s3 = string;
2360 s3.remove( before, cs );
2361 QTEST( s3, "result" );
2363 QString s4 = string;
2364 s4.replace( QRegExp(QRegExp::escape(before), cs), after );
2365 QTEST( s4, "result" );
2367 QString s5 = string;
2368 s5.replace( QRegExp(before, cs, QRegExp::FixedString), after );
2369 QTEST( s5, "result" );
2371 QCOMPARE( 0, 0 ); // shut QtTest
2375 void tst_QString::remove_regexp()
2377 QFETCH( QString, string );
2378 QFETCH( QString, regexp );
2379 QFETCH( QString, after );
2381 if ( after.length() == 0 ) {
2382 QString s2 = string;
2383 s2.remove( QRegExp(regexp) );
2384 QTEST( s2, "result" );
2387 s2.remove( QRegularExpression(regexp) );
2388 QTEST( s2, "result" );
2390 QCOMPARE( 0, 0 ); // shut QtTest
2394 void tst_QString::toNum()
2396 #if defined (Q_OS_WIN) && defined (Q_CC_MSVC)
2397 #define TEST_TO_INT(num, func) \
2399 QVERIFY2(a.func(&ok) == num ## i64 && ok, "Failed: num=" #num ", func=" #func);
2401 #define TEST_TO_INT(num, func) \
2403 QVERIFY2(a.func(&ok) == num ## LL && ok, "Failed: num=" #num ", func=" #func);
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)
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)
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)
2434 #if defined (Q_OS_WIN) && defined (Q_CC_MSVC)
2435 #define TEST_TO_UINT(num, func) \
2437 QVERIFY2(a.func(&ok) == num ## i64 && ok, "Failed: num=" #num ", func=" #func);
2439 #define TEST_TO_UINT(num, func) \
2441 QVERIFY2(a.func(&ok) == num ## ULL && ok, "Failed: num=" #num ", func=" #func);
2444 TEST_TO_UINT(0, toUInt)
2445 TEST_TO_UINT(1, toUInt)
2446 TEST_TO_UINT(4294967295, toUInt)
2448 TEST_TO_UINT(0, toUShort)
2449 TEST_TO_UINT(1, toUShort)
2450 TEST_TO_UINT(65535, toUShort)
2452 TEST_TO_UINT(0, toULong)
2453 TEST_TO_UINT(1, toULong)
2454 TEST_TO_UINT(4294967295, toULong)
2456 TEST_TO_UINT(0, toULongLong)
2457 TEST_TO_UINT(1, toULongLong)
2458 TEST_TO_UINT(18446744073709551615, toULongLong)
2462 #define TEST_BASE(str, base, num) \
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");
2473 TEST_BASE("FF", 16, 255)
2474 TEST_BASE("0xFF", 16, 255)
2475 TEST_BASE("77", 8, 63)
2476 TEST_BASE("077", 8, 63)
2478 TEST_BASE("0xFF", 0, 255)
2479 TEST_BASE("077", 0, 63)
2480 TEST_BASE("255", 0, 255)
2482 TEST_BASE(" FF", 16, 255)
2483 TEST_BASE(" 0xFF", 16, 255)
2484 TEST_BASE(" 77", 8, 63)
2485 TEST_BASE(" 077", 8, 63)
2487 TEST_BASE(" 0xFF", 0, 255)
2488 TEST_BASE(" 077", 0, 63)
2489 TEST_BASE(" 255", 0, 255)
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)
2498 #define TEST_NEG_BASE(str, base, num) \
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");
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)
2510 TEST_NEG_BASE("-0xFE", 0, -254)
2511 TEST_NEG_BASE("-077", 0, -63)
2512 TEST_NEG_BASE("-254", 0, -254)
2514 #undef TEST_NEG_BASE
2516 #define TEST_DOUBLE(num, str) \
2518 QCOMPARE(a.toDouble(&ok), num); \
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")
2529 #define TEST_BAD(str, func) \
2532 QVERIFY2(!ok, "Failed: str=" #str " func=" #func);
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)
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)
2554 #define TEST_BAD_ALL(str) \
2557 QVERIFY2(!ok, "Failed: str=" #str); \
2559 QVERIFY2(!ok, "Failed: str=" #str); \
2561 QVERIFY2(!ok, "Failed: str=" #str); \
2563 QVERIFY2(!ok, "Failed: str=" #str); \
2565 QVERIFY2(!ok, "Failed: str=" #str); \
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); \
2573 QVERIFY2(!ok, "Failed: str=" #str); \
2575 QVERIFY2(!ok, "Failed: str=" #str);
2577 TEST_BAD_ALL((const char*)0);
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");
2591 a.toULongLong(&ok, 10);
2595 a.toULongLong(&ok, 0);
2599 double d = 3.40282346638528e+38; // slightly off FLT_MAX when using hardfloats
2601 double d = 3.4028234663852886e+38; // FLT_MAX
2603 QString::number(d, 'e', 17).toFloat(&ok);
2605 QString::number(d + 1e32, 'e', 17).toFloat(&ok);
2607 QString::number(-d, 'e', 17).toFloat(&ok);
2609 QString::number(-d - 1e32, 'e', 17).toFloat(&ok);
2611 QString::number(d + 1e32, 'e', 17).toDouble(&ok);
2613 QString::number(-d - 1e32, 'e', 17).toDouble(&ok);
2617 void tst_QString::toUShort()
2621 QCOMPARE(a.toUShort(),(ushort)0);
2622 QCOMPARE(a.toUShort(&ok),(ushort)0);
2626 QCOMPARE(a.toUShort(),(ushort)0);
2627 QCOMPARE(a.toUShort(&ok),(ushort)0);
2631 QCOMPARE(a.toUShort(),(ushort)0);
2632 QCOMPARE(a.toUShort(&ok),(ushort)0);
2636 QCOMPARE(a.toUShort(),(ushort)123);
2637 QCOMPARE(a.toUShort(&ok),(ushort)123);
2641 QCOMPARE(a.toUShort(),(ushort)0);
2642 QCOMPARE(a.toUShort(&ok),(ushort)0);
2646 QCOMPARE(a.toUShort(),(ushort)0);
2647 QCOMPARE(a.toUShort(&ok),(ushort)0);
2651 QCOMPARE(a.toUShort(),(ushort)0);
2652 QCOMPARE(a.toUShort(&ok),(ushort)0);
2656 QCOMPARE(a.toUShort(),(ushort)0);
2657 QCOMPARE(a.toUShort(&ok),(ushort)0);
2661 QCOMPARE(a.toUShort(),(ushort)0);
2662 QCOMPARE(a.toUShort(&ok),(ushort)0);
2666 QCOMPARE(a.toUShort(),(ushort)32767);
2667 QCOMPARE(a.toUShort(&ok),(ushort)32767);
2671 QCOMPARE(a.toUShort(),(ushort)0);
2672 QCOMPARE(a.toUShort(&ok),(ushort)0);
2676 QCOMPARE(a.toUShort(),(ushort)65535);
2677 QCOMPARE(a.toUShort(&ok),(ushort)65535);
2680 if (sizeof(short) == 2) {
2682 QCOMPARE(a.toUShort(),(ushort)0);
2683 QCOMPARE(a.toUShort(&ok),(ushort)0);
2687 QCOMPARE(a.toUShort(),(ushort)0);
2688 QCOMPARE(a.toUShort(&ok),(ushort)0);
2693 void tst_QString::toShort()
2697 QCOMPARE(a.toShort(),(short)0);
2698 QCOMPARE(a.toShort(&ok),(short)0);
2702 QCOMPARE(a.toShort(),(short)0);
2703 QCOMPARE(a.toShort(&ok),(short)0);
2707 QCOMPARE(a.toShort(),(short)0);
2708 QCOMPARE(a.toShort(&ok),(short)0);
2712 QCOMPARE(a.toShort(),(short)123);
2713 QCOMPARE(a.toShort(&ok),(short)123);
2717 QCOMPARE(a.toShort(),(short)0);
2718 QCOMPARE(a.toShort(&ok),(short)0);
2722 QCOMPARE(a.toShort(),(short)0);
2723 QCOMPARE(a.toShort(&ok),(short)0);
2727 QCOMPARE(a.toShort(),(short)0);
2728 QCOMPARE(a.toShort(&ok),(short)0);
2732 QCOMPARE(a.toShort(),(short)0);
2733 QCOMPARE(a.toShort(&ok),(short)0);
2737 QCOMPARE(a.toShort(),(short)0);
2738 QCOMPARE(a.toShort(&ok),(short)0);
2742 QCOMPARE(a.toShort(),(short)32767);
2743 QCOMPARE(a.toShort(&ok),(short)32767);
2747 QCOMPARE(a.toShort(),(short)-32767);
2748 QCOMPARE(a.toShort(&ok),(short)-32767);
2752 QCOMPARE(a.toShort(),(short)-32768);
2753 QCOMPARE(a.toShort(&ok),(short)-32768);
2756 if (sizeof(short) == 2) {
2758 QCOMPARE(a.toShort(),(short)0);
2759 QCOMPARE(a.toShort(&ok),(short)0);
2763 QCOMPARE(a.toShort(),(short)0);
2764 QCOMPARE(a.toShort(&ok),(short)0);
2769 void tst_QString::toInt()
2773 QCOMPARE(a.toInt(),0);
2774 QCOMPARE(a.toInt(&ok),0);
2778 QCOMPARE(a.toInt(),0);
2779 QCOMPARE(a.toInt(&ok),0);
2783 QCOMPARE(a.toInt(),0);
2784 QCOMPARE(a.toInt(&ok),0);
2788 QCOMPARE(a.toInt(),123);
2789 QCOMPARE(a.toInt(&ok),123);
2793 QCOMPARE(a.toInt(),0);
2794 QCOMPARE(a.toInt(&ok),0);
2798 QCOMPARE(a.toInt(),1234567);
2799 QCOMPARE(a.toInt(&ok),1234567);
2803 QCOMPARE(a.toInt(),0);
2804 QCOMPARE(a.toInt(&ok),0);
2808 QCOMPARE(a.toInt(),0);
2809 QCOMPARE(a.toInt(&ok),0);
2813 QCOMPARE(a.toInt(),0);
2814 QCOMPARE(a.toInt(&ok),0);
2818 QCOMPARE(a.toInt(),0);
2819 QCOMPARE(a.toInt(&ok),0);
2823 QCOMPARE(a.toInt(),0);
2824 QCOMPARE(a.toInt(&ok),0);
2827 a = "2147483647"; // 2**31 - 1
2828 QCOMPARE(a.toInt(),2147483647);
2829 QCOMPARE(a.toInt(&ok),2147483647);
2832 if (sizeof(int) == 4) {
2833 a = "-2147483647"; // -(2**31 - 1)
2834 QCOMPARE(a.toInt(),-2147483647);
2835 QCOMPARE(a.toInt(&ok),-2147483647);
2838 a = "2147483648"; // 2**31
2839 QCOMPARE(a.toInt(),0);
2840 QCOMPARE(a.toInt(&ok),0);
2843 a = "-2147483648"; // -2**31
2844 QCOMPARE(a.toInt(),-2147483647 - 1);
2845 QCOMPARE(a.toInt(&ok),-2147483647 - 1);
2848 a = "2147483649"; // 2**31 + 1
2849 QCOMPARE(a.toInt(),0);
2850 QCOMPARE(a.toInt(&ok),0);
2855 void tst_QString::toUInt()
2860 QCOMPARE(a.toUInt(&ok),3234567890u);
2864 QCOMPARE(a.toUInt(),0u);
2865 QCOMPARE(a.toUInt(&ok),0u);
2868 a = "4294967295"; // 2**32 - 1
2869 QCOMPARE(a.toUInt(),4294967295u);
2870 QCOMPARE(a.toUInt(&ok),4294967295u);
2873 if (sizeof(int) == 4) {
2874 a = "4294967296"; // 2**32
2875 QCOMPARE(a.toUInt(),0u);
2876 QCOMPARE(a.toUInt(&ok),0u);
2881 ///////////////////////////// to*Long //////////////////////////////////////
2883 void tst_QString::toULong_data()
2885 QTest::addColumn<QString>("str" );
2886 QTest::addColumn<int>("base" );
2887 QTest::addColumn<ulong>("result" );
2888 QTest::addColumn<bool>("ok" );
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;
2896 void tst_QString::toULong()
2898 QFETCH( QString, str );
2899 QFETCH( int, base );
2900 QFETCH( ulong, result );
2904 QCOMPARE( str.toULong( 0, base ), result );
2905 QCOMPARE( str.toULong( &b, base ), result );
2909 void tst_QString::toLong_data()
2911 QTest::addColumn<QString>("str" );
2912 QTest::addColumn<int>("base" );
2913 QTest::addColumn<long>("result" );
2914 QTest::addColumn<bool>("ok" );
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;
2924 QTest::newRow( "negative" ) << QString("-7fffffff") << 16 << -0x7fffffffL << true;
2925 // QTest::newRow( "long_min" ) << QString("-80000000") << 16 << 0x80000000uL << true;
2928 void tst_QString::toLong()
2930 QFETCH( QString, str );
2931 QFETCH( int, base );
2932 QFETCH( long, result );
2936 QCOMPARE( str.toLong( 0, base ), result );
2937 QCOMPARE( str.toLong( &b, base ), result );
2942 ////////////////////////// to*LongLong //////////////////////////////////////
2944 void tst_QString::toULongLong()
2948 str = "18446744073709551615"; // ULLONG_MAX
2949 QCOMPARE( str.toULongLong( 0 ), Q_UINT64_C(18446744073709551615) );
2950 QCOMPARE( str.toULongLong( &ok ), Q_UINT64_C(18446744073709551615) );
2953 str = "18446744073709551616"; // ULLONG_MAX + 1
2954 QCOMPARE( str.toULongLong( 0 ), Q_UINT64_C(0) );
2955 QCOMPARE( str.toULongLong( &ok ), Q_UINT64_C(0) );
2959 QCOMPARE( str.toULongLong( 0 ), Q_UINT64_C(0) );
2960 QCOMPARE( str.toULongLong( &ok ), Q_UINT64_C(0) );
2964 void tst_QString::toLongLong()
2969 str = "9223372036854775807"; // LLONG_MAX
2970 QCOMPARE( str.toLongLong( 0 ), Q_INT64_C(9223372036854775807) );
2971 QCOMPARE( str.toLongLong( &ok ), Q_INT64_C(9223372036854775807) );
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) );
2981 str = "aaaa9223372036854775807aaaa";
2982 QCOMPARE( str.toLongLong( 0 ), Q_INT64_C(0) );
2983 QCOMPARE( str.toLongLong( &ok ), Q_INT64_C(0) );
2986 str = "9223372036854775807aaaa";
2987 QCOMPARE( str.toLongLong( 0 ), Q_INT64_C(0) );
2988 QCOMPARE( str.toLongLong( &ok ), Q_INT64_C(0) );
2991 str = "aaaa9223372036854775807";
2992 QCOMPARE( str.toLongLong( 0 ), Q_INT64_C(0) );
2993 QCOMPARE( str.toLongLong( &ok ), Q_INT64_C(0) );
2996 static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2998 for (int i = 0; i < 36; ++i) {
2999 for (int j = 0; j < 36; ++j) {
3000 for (int k = 0; k < 36; ++k) {
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);
3012 ////////////////////////////////////////////////////////////////////////////
3014 void tst_QString::toFloat()
3018 a="0.000000000931322574615478515625";
3019 QCOMPARE(a.toFloat(&ok),(float)(0.000000000931322574615478515625));
3023 void tst_QString::toDouble_data()
3025 QTest::addColumn<QString>("str" );
3026 QTest::addColumn<double>("result" );
3027 QTest::addColumn<bool>("result_ok" );
3029 QTest::newRow( "ok00" ) << QString("0.000000000931322574615478515625") << 0.000000000931322574615478515625 << true;
3030 QTest::newRow( "ok01" ) << QString(" 123.45") << 123.45 << true;
3032 QTest::newRow( "ok02" ) << QString("0.1e10") << 0.1e10 << true;
3033 QTest::newRow( "ok03" ) << QString("0.1e-10") << 0.1e-10 << true;
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;
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;
3043 QTest::newRow( "ok10" ) << QString("1.") << 1.0 << true;
3044 QTest::newRow( "ok11" ) << QString(".1") << 0.1 << true;
3046 QTest::newRow( "wrong00" ) << QString("123.45 ") << 123.45 << true;
3047 QTest::newRow( "wrong01" ) << QString(" 123.45 ") << 123.45 << true;
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;
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;
3058 void tst_QString::toDouble()
3060 QFETCH( QString, str );
3061 QFETCH( bool, result_ok );
3063 double d = str.toDouble( &ok );
3065 QTEST( d, "result" );
3072 void tst_QString::setNum()
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"));
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"));
3104 // QCOMPARE(a.setNum(0.000000000931322574615478515625,'g',30),(QString)"9.31322574615478515625e-010");
3105 // QCOMPARE(a.setNum(0.000000000931322574615478515625,'f',30),(QString)"0.00000000093132257461547852");
3108 void tst_QString::startsWith()
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)) );
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)) );
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) );
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) );
3173 #define TEST_REF_STARTS_WITH(string, yes) { CREATE_REF(string); QCOMPARE(a.startsWith(ref), yes); }
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
3182 QVERIFY( a.startsWith("") );
3183 QVERIFY( a.startsWith(QString::null) );
3184 QVERIFY( !a.startsWith("ABC") );
3186 QVERIFY( a.startsWith(QLatin1String("")) );
3187 QVERIFY( a.startsWith(QLatin1String(0)) );
3188 QVERIFY( !a.startsWith(QLatin1String("ABC")) );
3190 QVERIFY( !a.startsWith(QLatin1Char(0)) );
3191 QVERIFY( !a.startsWith(QLatin1Char('x')) );
3192 QVERIFY( !a.startsWith(QChar()) );
3195 QVERIFY( !a.startsWith("") );
3196 QVERIFY( a.startsWith(QString::null) );
3197 QVERIFY( !a.startsWith("ABC") );
3199 QVERIFY( !a.startsWith(QLatin1String("")) );
3200 QVERIFY( a.startsWith(QLatin1String(0)) );
3201 QVERIFY( !a.startsWith(QLatin1String("ABC")) );
3203 QVERIFY( !a.startsWith(QLatin1Char(0)) );
3204 QVERIFY( !a.startsWith(QLatin1Char('x')) );
3205 QVERIFY( !a.startsWith(QChar()) );
3207 // this test is independent of encoding
3209 QVERIFY( a.startsWith("\xc3\xa9") );
3210 QVERIFY( !a.startsWith("\xc3\xa1") );
3212 // this one is dependent of encoding
3213 QVERIFY( a.startsWith("\xc3\x89", Qt::CaseInsensitive) );
3216 void tst_QString::endsWith()
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)) );
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)) );
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) );
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) );
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);
3290 #undef TEST_REF_STARTS_WITH
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()) );
3300 QVERIFY( a.endsWith(QLatin1String("")) );
3301 QVERIFY( a.endsWith(QLatin1String(0)) );
3302 QVERIFY( !a.endsWith(QLatin1String("ABC")) );
3305 QVERIFY( !a.endsWith("") );
3306 QVERIFY( a.endsWith(QString::null) );
3307 QVERIFY( !a.endsWith("ABC") );
3309 QVERIFY( !a.endsWith(QLatin1String("")) );
3310 QVERIFY( a.endsWith(QLatin1String(0)) );
3311 QVERIFY( !a.endsWith(QLatin1String("ABC")) );
3313 QVERIFY( !a.endsWith(QLatin1Char(0)) );
3314 QVERIFY( !a.endsWith(QLatin1Char('x')) );
3315 QVERIFY( !a.endsWith(QChar()) );
3317 // this test is independent of encoding
3319 QVERIFY( a.endsWith("\xc3\xa9") );
3320 QVERIFY( !a.endsWith("\xc3\xa1") );
3322 // this one is dependent of encoding
3323 QVERIFY( a.endsWith("\xc3\x89", Qt::CaseInsensitive) );
3326 void tst_QString::check_QDataStream()
3331 QDataStream out(&ar,QIODevice::WriteOnly);
3332 out << QString("COMPARE Text");
3335 QDataStream in(&ar,QIODevice::ReadOnly);
3337 QCOMPARE(a,(QString)"COMPARE Text");
3341 void tst_QString::check_QTextStream()
3346 QTextStream out(&ar,QIODevice::WriteOnly);
3347 out << QString("This is COMPARE Text");
3350 QTextStream in(&ar,QIODevice::ReadOnly);
3352 QCOMPARE(a,(QString)"This");
3356 void tst_QString::check_QTextIOStream()
3362 ts << "pi \261= " << 3.125;
3363 QCOMPARE(a, QString::fromLatin1("pi \261= 3.125"));
3368 QTextStream(&a) >> x >> y;
3374 void tst_QString::fromRawData()
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));
3382 QVERIFY(cstr.constData() == ptr);
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));
3391 void tst_QString::setRawData()
3393 const QChar ptr[] = { 0x1234, 0x0000 };
3394 const QChar ptr2[] = { 0x4321, 0x0000 };
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));
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);
3412 // This tests the discarding of the shared data object
3414 QVERIFY(cstr.isDetached());
3415 QVERIFY(cstr.constData() != ptr2);
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);
3426 void tst_QString::fromStdString()
3429 QSKIP("This test crashes on HP-UX with aCC");
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()) );
3440 void tst_QString::toStdString()
3443 QSKIP("This test crashes on HP-UX with aCC");
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', '!'
3455 QString qtnull( qcnull, sizeof(qcnull)/sizeof(QChar) );
3456 std::string stdnull = qtnull.toStdString();
3457 QCOMPARE( int(stdnull.size()), qtnull.size() );
3460 void tst_QString::utf8()
3462 QFETCH( QByteArray, utf8 );
3463 QFETCH( QString, res );
3465 QCOMPARE(res.toUtf8(), utf8);
3468 void tst_QString::stringRef_utf8_data()
3473 void tst_QString::stringRef_utf8()
3475 QFETCH( QByteArray, utf8 );
3476 QFETCH( QString, res );
3478 QStringRef ref(&res, 0, res.length());
3479 QCOMPARE( utf8, QByteArray(ref.toUtf8()) );
3482 // copied to tst_QTextCodec::utf8Codec_data()
3483 void tst_QString::fromUtf8_data()
3485 QTest::addColumn<QByteArray>("utf8");
3486 QTest::addColumn<QString>("res");
3487 QTest::addColumn<int>("len");
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;
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;
3502 str = QChar(0x05e9);
3503 QTest::newRow("str2-len") << QByteArray("\327\251\327\223\327\222") << str << 2;
3505 str = QChar(0x20ac);
3506 str += " some text";
3507 QTest::newRow("str3") << QByteArray("\342\202\254 some text") << str << -1;
3509 str = QChar(0x20ac);
3511 QTest::newRow("str3-len") << QByteArray("\342\202\254 some text") << str << 9;
3514 str += QChar::ReplacementCharacter;
3516 str += QChar::ReplacementCharacter;
3517 str += QChar::ReplacementCharacter;
3518 str += QChar::ReplacementCharacter;
3519 str += QChar::ReplacementCharacter;
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;
3526 str += QChar::ReplacementCharacter;
3527 str += QChar::ReplacementCharacter;
3529 str += QChar::ReplacementCharacter;
3531 str += QChar::ReplacementCharacter;
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;
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;
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;
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;
3563 void tst_QString::fromUtf8()
3565 QFETCH(QByteArray, utf8);
3566 QFETCH(QString, res);
3569 QCOMPARE(QString::fromUtf8(utf8.isNull() ? 0 : utf8.data(), len), res);
3572 void tst_QString::nullFromUtf8()
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());
3583 void tst_QString::fromLocal8Bit_data()
3585 QTest::addColumn<QByteArray>("local8Bit");
3586 QTest::addColumn<int>("len");
3587 QTest::addColumn<QString>("result");
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);
3596 QByteArray longQByteArray;
3597 QString longQString;
3599 for (int l=0;l<111;l++) {
3600 longQByteArray = longQByteArray + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
3601 longQString += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
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");
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);
3619 void tst_QString::fromLocal8Bit()
3621 QFETCH(QByteArray, local8Bit);
3623 QFETCH(QString, result);
3625 QCOMPARE(QString::fromLocal8Bit(local8Bit.isNull() ? 0 : local8Bit.data(), len).length(),
3627 QCOMPARE(QString::fromLocal8Bit(local8Bit.isNull() ? 0 : local8Bit.data(), len), result);
3630 void tst_QString::local8Bit_data()
3632 QTest::addColumn<QString>("local8Bit");
3633 QTest::addColumn<QByteArray>("result");
3636 QString::local8Bit() called on a null QString returns an _empty_
3639 QTest::newRow("nullString") << QString() << QByteArray("");
3640 QTest::newRow("emptyString") << QString("") << QByteArray("");
3641 QTest::newRow("string") << QString("test") << QByteArray("test");
3643 QByteArray longQByteArray;
3644 QString longQString;
3646 for (int l=0;l<111;l++) {
3647 longQByteArray = longQByteArray + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
3648 longQString += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
3651 QTest::newRow("longString") << longQString << longQByteArray;
3652 QTest::newRow("someNonAlphaChars") << QString("d:/this/is/a/test.h") << QByteArray("d:/this/is/a/test.h");
3655 void tst_QString::local8Bit()
3657 QFETCH(QString, local8Bit);
3658 QFETCH(QByteArray, result);
3660 QCOMPARE(local8Bit.toLocal8Bit(), QByteArray(result));
3663 void tst_QString::stringRef_local8Bit_data()
3668 void tst_QString::stringRef_local8Bit()
3670 QFETCH(QString, local8Bit);
3671 QFETCH(QByteArray, result);
3673 QStringRef ref(&local8Bit, 0, local8Bit.length());
3674 QCOMPARE(ref.toLocal8Bit(), QByteArray(result));
3677 void tst_QString::fromLatin1Roundtrip_data()
3679 QTest::addColumn<QByteArray>("latin1");
3680 QTest::addColumn<QString>("unicode");
3682 QTest::newRow("null") << QByteArray() << QString();
3683 QTest::newRow("empty") << QByteArray("") << "";
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);
3689 static const ushort unicode3[] = { 'a', 0, 'z' };
3690 QTest::newRow("ascii+nul") << QByteArray("a\0z", 3) << QString::fromUtf16(unicode3, 3);
3692 static const ushort unicode4[] = { 0x80, 0xc0, 0xff };
3693 QTest::newRow("non-ascii") << QByteArray("\x80\xc0\xff") << QString::fromUtf16(unicode4, 3);
3696 void tst_QString::fromLatin1Roundtrip()
3698 QFETCH(QByteArray, latin1);
3699 QFETCH(QString, unicode);
3701 // QtTest safety check:
3702 QCOMPARE(latin1.isNull(), unicode.isNull());
3703 QCOMPARE(latin1.isEmpty(), unicode.isEmpty());
3704 QCOMPARE(latin1.length(), unicode.length());
3706 if (!latin1.isEmpty())
3707 while (latin1.length() < 128) {
3713 QCOMPARE(QString::fromLatin1(latin1, latin1.length()).length(), unicode.length());
3714 QCOMPARE(QString::fromLatin1(latin1, latin1.length()), unicode);
3717 QCOMPARE(unicode.toLatin1().length(), latin1.length());
3718 QCOMPARE(unicode.toLatin1(), latin1);
3721 void tst_QString::toLatin1Roundtrip_data()
3723 QTest::addColumn<QByteArray>("latin1");
3724 QTest::addColumn<QString>("unicodesrc");
3725 QTest::addColumn<QString>("unicodedst");
3727 QTest::newRow("null") << QByteArray() << QString() << QString();
3728 QTest::newRow("empty") << QByteArray("") << "" << "";
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);
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);
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);
3740 static const ushort unicodeq[] = { '?', '?', '?', '?', '?' };
3741 const QString questionmarks = QString::fromUtf16(unicodeq, 5);
3743 static const ushort unicode5[] = { 0x100, 0x101, 0x17f, 0x7f00, 0x7f7f };
3744 QTest::newRow("non-latin1a") << QByteArray("?????") << QString::fromUtf16(unicode5, 5) << questionmarks;
3746 static const ushort unicode6[] = { 0x180, 0x1ff, 0x8001, 0x8080, 0xfffc };
3747 QTest::newRow("non-latin1b") << QByteArray("?????") << QString::fromUtf16(unicode6, 5) << questionmarks;
3750 void tst_QString::toLatin1Roundtrip()
3752 QFETCH(QByteArray, latin1);
3753 QFETCH(QString, unicodesrc);
3754 QFETCH(QString, unicodedst);
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());
3764 if (!latin1.isEmpty())
3765 while (latin1.length() < 128) {
3767 unicodesrc += unicodesrc;
3768 unicodedst += unicodedst;
3772 QCOMPARE(unicodesrc.toLatin1().length(), latin1.length());
3773 QCOMPARE(unicodesrc.toLatin1(), latin1);
3776 QCOMPARE(QString::fromLatin1(latin1, latin1.length()).length(), unicodedst.length());
3777 QCOMPARE(QString::fromLatin1(latin1, latin1.length()), unicodedst);
3780 void tst_QString::stringRef_toLatin1Roundtrip_data()
3782 toLatin1Roundtrip_data();
3785 void tst_QString::stringRef_toLatin1Roundtrip()
3787 QFETCH(QByteArray, latin1);
3788 QFETCH(QString, unicodesrc);
3789 QFETCH(QString, unicodedst);
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());
3799 if (!latin1.isEmpty())
3800 while (latin1.length() < 128) {
3802 unicodesrc += unicodesrc;
3803 unicodedst += unicodedst;
3807 QStringRef src(&unicodesrc, 0, unicodesrc.length());
3808 QCOMPARE(src.toLatin1().length(), latin1.length());
3809 QCOMPARE(src.toLatin1(), latin1);
3812 void tst_QString::fromLatin1()
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() );
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);
3833 void tst_QString::fromAscii()
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() );
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);
3854 void tst_QString::arg()
3857 Warning: If any of these test fails, the warning given by QtTest
3858 is all messed up, because QtTest itself uses QString::arg().
3861 QLocale::setDefault(QString("de_DE"));
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" );
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 ]") );
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") );
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]") );
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") );
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") );
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") );
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");
3958 QCOMPARE(input, QString("%%A B C D E f g h i %%% j0 k l m n o88 p99 %%%%%%%L"));
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"));
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"));
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"));
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"));
3990 void tst_QString::number()
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") );
4001 void tst_QString::capacity_data()
4006 void tst_QString::capacity()
4008 QFETCH( QString, s1 );
4013 QVERIFY( (int)s2.capacity() >= res );
4016 s2.reserve( res * 2 );
4017 QVERIFY( (int)s2.capacity() >= res * 2 );
4021 QVERIFY( (int)s2.capacity() == res );
4026 void tst_QString::section_data()
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" );
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;
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;
4131 void tst_QString::section()
4133 QFETCH( QString, wholeString );
4134 QFETCH( QString, sep );
4135 QFETCH( int, start );
4137 QFETCH( int, flags );
4138 QFETCH( QString, sectionString );
4139 QFETCH( bool, 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 );
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 );
4152 void tst_QString::operator_eqeq_nullstring()
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. */
4157 QVERIFY( QString() == "" );
4158 QVERIFY( "" == QString() );
4160 QVERIFY( QString("") == "" );
4161 QVERIFY( "" == QString("") );
4163 QVERIFY( QString().size() == 0 );
4165 QVERIFY( QString("").size() == 0 );
4167 QVERIFY( QString() == QString("") );
4168 QVERIFY( QString("") == QString() );
4171 void tst_QString::operator_smaller()
4177 QVERIFY( !(null < QString()) );
4178 QVERIFY( !(null > QString()) );
4180 QVERIFY( !(empty < QString("")) );
4181 QVERIFY( !(empty > QString("")) );
4183 QVERIFY( !(null < empty) );
4184 QVERIFY( !(null > empty) );
4186 QVERIFY( null < foo );
4187 QVERIFY( !(null > foo) );
4188 QVERIFY( foo > null );
4189 QVERIFY( !(foo < null) );
4191 QVERIFY( empty < foo );
4192 QVERIFY( !(empty > foo) );
4193 QVERIFY( foo > empty );
4194 QVERIFY( !(foo < empty) );
4196 QVERIFY( !(null < QLatin1String(0)) );
4197 QVERIFY( !(null > QLatin1String(0)) );
4198 QVERIFY( !(null < QLatin1String("")) );
4199 QVERIFY( !(null > QLatin1String("")) );
4201 QVERIFY( !(null < QLatin1String("")) );
4202 QVERIFY( !(null > QLatin1String("")) );
4203 QVERIFY( !(empty < QLatin1String("")) );
4204 QVERIFY( !(empty > QLatin1String("")) );
4206 QVERIFY( !(QLatin1String(0) < null) );
4207 QVERIFY( !(QLatin1String(0) > null) );
4208 QVERIFY( !(QLatin1String("") < null) );
4209 QVERIFY( !(QLatin1String("") > null) );
4211 QVERIFY( !(QLatin1String(0) < empty) );
4212 QVERIFY( !(QLatin1String(0) > empty) );
4213 QVERIFY( !(QLatin1String("") < empty) );
4214 QVERIFY( !(QLatin1String("") > empty) );
4216 QVERIFY( QLatin1String(0) < foo );
4217 QVERIFY( !(QLatin1String(0) > foo) );
4218 QVERIFY( QLatin1String("") < foo );
4219 QVERIFY( !(QLatin1String("") > foo) );
4221 QVERIFY( foo > QLatin1String(0) );
4222 QVERIFY( !(foo < QLatin1String(0)) );
4223 QVERIFY( foo > QLatin1String("") );
4224 QVERIFY( !(foo < QLatin1String("")) );
4226 QVERIFY( QLatin1String(0) == empty);
4227 QVERIFY( QLatin1String(0) == null);
4228 QVERIFY( QLatin1String("") == empty);
4229 QVERIFY( QLatin1String("") == null);
4231 QVERIFY( !(foo < QLatin1String("foo")));
4232 QVERIFY( !(foo > QLatin1String("foo")));
4233 QVERIFY( !(QLatin1String("foo") < foo));
4234 QVERIFY( !(QLatin1String("foo") > foo));
4236 QVERIFY( !(foo < QLatin1String("a")));
4237 QVERIFY( (foo > QLatin1String("a")));
4238 QVERIFY( (QLatin1String("a") < foo));
4239 QVERIFY( !(QLatin1String("a") > foo));
4241 QVERIFY( (foo < QLatin1String("z")));
4242 QVERIFY( !(foo > QLatin1String("z")));
4243 QVERIFY( !(QLatin1String("z") < foo));
4244 QVERIFY( (QLatin1String("z") > foo));
4246 // operator< is not locale-aware (or shouldn't be)
4247 QVERIFY( foo < QString("\xc3\xa9") );
4248 QVERIFY( foo < "\xc3\xa9" );
4251 void tst_QString::integer_conversion_data()
4253 QTest::addColumn<QString>("num_str");
4254 QTest::addColumn<int>("base");
4255 QTest::addColumn<bool>("good");
4256 QTest::addColumn<qlonglong>("num");
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;
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;
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;
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;
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;
4289 QTest::newRow("C 0x 0") << QString("0x") << 0 << false << (qlonglong)0;
4290 QTest::newRow("C 0x 16") << QString("0x") << 16 << false << (qlonglong)0;
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;
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;
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;
4323 void tst_QString::integer_conversion()
4325 QFETCH(QString, num_str);
4328 QFETCH(qlonglong, num);
4331 qlonglong d = num_str.toLongLong(&ok, base);
4339 void tst_QString::double_conversion_data()
4341 QTest::addColumn<QString>("num_str");
4342 QTest::addColumn<bool>("good");
4343 QTest::addColumn<double>("num");
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;
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;
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;
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;
4388 void tst_QString::double_conversion()
4390 #define MY_DOUBLE_EPSILON (2.22045e-16)
4392 QFETCH(QString, num_str);
4394 QFETCH(double, num);
4397 double d = num_str.toDouble(&ok);
4401 double diff = d - num;
4404 QVERIFY(diff <= MY_DOUBLE_EPSILON);
4409 #include "double_data.h"
4412 void tst_QString::tortureSprintfDouble()
4414 const SprintfDoubleData *data = g_sprintf_double_data;
4418 for (; data->fmt != 0; ++data) {
4420 char *buff = (char *)&d;
4421 # ifndef Q_BYTE_ORDER
4422 # error "Q_BYTE_ORDER not defined"
4425 # if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
4426 for (uint i = 0; i < 8; ++i)
4427 buff[i] = data->bytes[i];
4429 for (uint i = 0; i < 8; ++i)
4430 buff[7 - i] = data->bytes[i];
4432 s.sprintf(data->fmt, d);
4433 #ifdef QT_NO_FPU // reduced precision when running with hardfloats in qemu
4435 QSKIP("clib sprintf doesn't fill with 0's on this platform");
4436 QCOMPARE(s.left(16), QString(data->expected).left(16));
4438 QCOMPARE(s, QString(data->expected));
4445 void tst_QString::localeAwareCompare_data()
4448 QTest::addColumn<ulong>("locale");
4450 QTest::addColumn<QString>("locale");
4452 QTest::addColumn<QString>("s1");
4453 QTest::addColumn<QString>("s2");
4454 QTest::addColumn<int>("result");
4457 The C locale performs pure byte comparisons for
4458 Latin-1-specific characters (I think). Compare with Swedish
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;
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;
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.
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;
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;
4489 In Swedish, a with ring above (E5) comes before a with
4490 diaresis (E4), which comes before o diaresis (F6), which
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;
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;
4512 In Norwegian, ae (E6) comes before o with stroke (D8), which
4513 comes before a with ring above (E5).
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;
4521 In German, z comes *after* a with diaresis (E4),
4522 which comes before o diaresis (F6).
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;
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;
4539 void tst_QString::localeAwareCompare()
4543 QSKIP("On others than Win CE, we cannot set the system or user locale.");
4545 QFETCH(ulong, locale);
4547 QFETCH(QString, locale);
4549 QFETCH(QString, s1);
4550 QFETCH(QString, s2);
4551 QFETCH(int, result);
4553 QStringRef r1(&s1, 0, s1.length());
4554 QStringRef r2(&s2, 0, s2.length());
4557 # if defined(Q_OS_WINCE)
4558 DWORD oldLcid = GetUserDefaultLCID();
4559 SetUserDefaultLCID(locale);
4561 QCOMPARE(locale, GetUserDefaultLCID());
4563 DWORD oldLcid = GetThreadLocale();
4564 SetThreadLocale(locale);
4566 QCOMPARE(locale, GetThreadLocale());
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));
4574 if (!locale.isEmpty()) {
4575 const char *newLocale = setlocale(LC_ALL, locale.toLatin1());
4577 setlocale(LC_ALL, "");
4578 QSKIP("Please install the proper locale on this machine to test properly");
4584 // ### for c1, ICU disagrees with libc on how to compare
4585 QEXPECT_FAIL("c1", "ICU disagrees with test", Abort);
4588 int testres = QString::localeAwareCompare(s1, s2);
4590 QVERIFY(testres < 0);
4591 } else if (result > 0) {
4592 QVERIFY(testres > 0);
4594 QVERIFY(testres == 0);
4597 testres = QString::localeAwareCompare(s2, s1);
4599 QVERIFY(testres < 0);
4600 } else if (result < 0) {
4601 QVERIFY(testres > 0);
4603 QVERIFY(testres == 0);
4606 testres = QString::localeAwareCompare(s1, r2);
4608 QVERIFY(testres < 0);
4609 } else if (result > 0) {
4610 QVERIFY(testres > 0);
4612 QVERIFY(testres == 0);
4615 testres = QStringRef::localeAwareCompare(r1, r2);
4617 QVERIFY(testres < 0);
4618 } else if (result > 0) {
4619 QVERIFY(testres > 0);
4621 QVERIFY(testres == 0);
4624 testres = QStringRef::localeAwareCompare(r2, r1);
4626 QVERIFY(testres < 0);
4627 } else if (result < 0) {
4628 QVERIFY(testres > 0);
4630 QVERIFY(testres == 0);
4634 # if defined(Q_OS_WINCE)
4635 SetUserDefaultLCID(oldLcid);
4637 SetThreadLocale(oldLcid);
4641 if (!locale.isEmpty())
4642 setlocale(LC_ALL, "");
4646 void tst_QString::split_data()
4648 QTest::addColumn<QString>("str");
4649 QTest::addColumn<QString>("sep");
4650 QTest::addColumn<QStringList>("result");
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")
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() << "" << "" << "");
4661 QTest::newRow("all-empty") << "" << "" << (QStringList() << "" << "");
4662 QTest::newRow("sep-empty") << "abc" << "" << (QStringList() << "" << "a" << "b" << "c" << "");
4665 void tst_QString::split()
4667 QFETCH(QString, str);
4668 QFETCH(QString, sep);
4669 QFETCH(QStringList, result);
4671 QRegExp rx = QRegExp(QRegExp::escape(sep));
4672 QRegularExpression re(QRegularExpression::escape(sep));
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);
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);
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);
4711 void tst_QString::split_regexp_data()
4713 QTest::addColumn<QString>("string");
4714 QTest::addColumn<QString>("pattern");
4715 QTest::addColumn<QStringList>("result");
4717 QTest::newRow("data01") << "Some text\n\twith strange whitespace."
4719 << (QStringList() << "Some" << "text" << "with" << "strange" << "whitespace." );
4721 QTest::newRow("data02") << "This time, a normal English sentence."
4723 << (QStringList() << "This" << "time" << "a" << "normal" << "English" << "sentence" << "");
4725 QTest::newRow("data03") << "Now: this sentence fragment."
4727 << (QStringList() << "" << "Now" << ": " << "this" << " " << "sentence" << " " << "fragment" << ".");
4730 void tst_QString::split_regexp()
4732 QFETCH(QString, string);
4733 QFETCH(QString, pattern);
4734 QFETCH(QStringList, result);
4737 list = string.split(QRegExp(pattern));
4738 QCOMPARE(list, result);
4739 list = string.split(QRegularExpression(pattern));
4740 QCOMPARE(list, result);
4742 result.removeAll(QString());
4744 list = string.split(QRegExp(pattern), QString::SkipEmptyParts);
4745 QCOMPARE(list, result);
4746 list = string.split(QRegularExpression(pattern), QString::SkipEmptyParts);
4747 QCOMPARE(list, result);
4750 void tst_QString::fromUtf16_data()
4752 QTest::addColumn<QString>("ucs2");
4753 QTest::addColumn<QString>("res");
4754 QTest::addColumn<int>("len");
4756 QTest::newRow("str0") << QString("abcdefgh") << QString("abcdefgh") << -1;
4757 QTest::newRow("str0-len") << QString("abcdefgh") << QString("abc") << 3;
4760 void tst_QString::fromUtf16()
4762 QFETCH(QString, ucs2);
4763 QFETCH(QString, res);
4766 QCOMPARE(QString::fromUtf16(ucs2.utf16(), len), res);
4770 void tst_QString::latin1String()
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")));
4784 void tst_QString::nanAndInf()
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);
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)
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); \
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)
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)
4838 d = QString("-INF").toDouble(&ok);
4840 QVERIFY(d == -INFINITY);
4842 QString("INF").toLong(&ok);
4845 QString("INF").toLong(&ok, 36);
4848 QString("INF0").toLong(&ok, 36);
4851 QString("0INF0").toLong(&ok, 36);
4855 void tst_QString::arg_fillChar_data()
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");
4863 QList<QVariant> replaceValues;
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");
4871 replaceValues.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");
4878 replaceValues.clear();
4880 replaceValues << QVariant(QString("fisk"));
4882 QTest::newRow("str2") << QString("%9 og poteter") << replaceValues << widths << QString("f")
4883 << QString("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffisk og poteter");
4886 void tst_QString::arg_fillChar()
4888 static const int base = 10;
4889 static const char fmt = 'g';
4890 static const int prec = -1;
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());
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;
4916 QCOMPARE(actual, expected);
4919 static inline int sign(int x)
4921 return x == 0 ? 0 : (x < 0 ? -1 : 1);
4924 void tst_QString::compare_data()
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
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;
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;
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;
4948 upper += QChar(QChar::highSurrogate(0x10400));
4949 upper += QChar(QChar::lowSurrogate(0x10400));
4951 lower += QChar(QChar::highSurrogate(0x10428));
4952 lower += QChar(QChar::lowSurrogate(0x10428));
4953 QTest::newRow("data8") << upper << lower << -1 << 0;
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;*/
4965 static bool isLatin(const QString &s)
4967 for (int i = 0; i < s.length(); ++i)
4968 if (s.at(i).unicode() > 0xff)
4973 void tst_QString::compare()
4975 QFETCH(QString, s1);
4976 QFETCH(QString, s2);
4980 QStringRef r1(&s1, 0, s1.length());
4981 QStringRef r2(&s2, 0, s2.length());
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);
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);
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);
5004 QVERIFY(qHash(s1) == qHash(s2));
5005 QVERIFY(qHash(s1) == qHash(r2));
5006 QVERIFY(qHash(r1) == qHash(s2));
5007 QVERIFY(qHash(r1) == qHash(r2));
5011 QCOMPARE(s1.toCaseFolded(), s2.toCaseFolded());
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();
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);
5029 QCOMPARE(sign(QString::compare(QLatin1String(s1.toLatin1()), s2)), csr);
5030 QCOMPARE(sign(QString::compare(QLatin1String(s1.toLatin1()), s2, Qt::CaseInsensitive)), cir);
5034 void tst_QString::resizeAfterFromRawData()
5036 QString buffer("hello world");
5038 QString array = QString::fromRawData(buffer.constData(), buffer.size());
5039 QVERIFY(array.constData() == buffer.constData());
5041 QVERIFY(array.constData() == buffer.constData());
5044 void tst_QString::resizeAfterReserve()
5052 // resize should not affect capacity
5054 QVERIFY(s.capacity() == 100);
5058 QVERIFY(s.capacity() == s.size());
5062 QVERIFY(s.capacity() == 0);
5064 // test resize(0) border case
5068 QVERIFY(s.capacity() == 100);
5071 void tst_QString::resizeWithNegative() const
5074 QString string(QLatin1String("input"));
5076 QCOMPARE(string, QString());
5080 QString string(QLatin1String("input"));
5081 string.resize(-9099);
5082 QCOMPARE(string, QString());
5086 /* Example code from customer. */
5087 QString s(QLatin1String("hola"));
5090 QCOMPARE(s, QString());
5094 void tst_QString::truncateWithNegative() const
5097 QString string(QLatin1String("input"));
5098 string.truncate(-1);
5099 QCOMPARE(string, QString());
5103 QString string(QLatin1String("input"));
5104 string.truncate(-9099);
5105 QCOMPARE(string, QString());
5109 /* Example code from customer. */
5110 QString test(QLatin1String("c"));
5112 test.replace(QRegExp(QLatin1String("c")), QLatin1String("z"));
5114 QCOMPARE(test, QString());
5118 void tst_QString::QCharRefMutableUnicode() const
5122 str[0].unicode() = 115;
5123 str[1].unicode() = 116;
5124 str[2].unicode() = 114;
5126 QCOMPARE(str, QString::fromLatin1("str"));
5129 void tst_QString::QCharRefDetaching() const
5132 QString str = QString::fromLatin1("str");
5134 copy[0] = QLatin1Char('S');
5136 QCOMPARE(str, QString::fromLatin1("str"));
5140 ushort buf[] = { 's', 't', 'r' };
5141 QString str = QString::fromRawData((const QChar *)buf, 3);
5142 str[0] = QLatin1Char('S');
5144 QCOMPARE(buf[0], ushort('s'));
5148 static const ushort buf[] = { 's', 't', 'r' };
5149 QString str = QString::fromRawData((const QChar *)buf, 3);
5151 // this causes a crash in most systems if the detaching doesn't work
5152 str[0] = QLatin1Char('S');
5154 QCOMPARE(buf[0], ushort('s'));
5158 void tst_QString::sprintfZU() const
5163 string.sprintf("%zu", s);
5164 QCOMPARE(string, QString::fromLatin1("6"));
5169 string.sprintf("%s\n", "foo");
5170 QCOMPARE(string, QString::fromLatin1("foo\n"));
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. */
5178 string.sprintf("%zu%s", s, "foo");
5179 QCOMPARE(string, QString::fromLatin1("6foo"));
5185 string.sprintf("%zu %s\n", s, "foo");
5186 QCOMPARE(string, QString::fromLatin1("6 foo\n"));
5190 void tst_QString::repeatedSignature() const
5192 /* repated() should be a const member. */
5193 const QString string;
5197 void tst_QString::repeated() const
5199 QFETCH(QString, string);
5200 QFETCH(QString, expected);
5203 QCOMPARE(string.repeated(count), expected);
5206 void tst_QString::repeated_data() const
5208 QTest::addColumn<QString>("string" );
5209 QTest::addColumn<QString>("expected" );
5210 QTest::addColumn<int>("count" );
5212 /* Empty strings. */
5213 QTest::newRow("data1")
5218 QTest::newRow("data2")
5223 QTest::newRow("data3")
5228 QTest::newRow("data4")
5233 /* On simple string. */
5234 QTest::newRow("data5")
5235 << QString(QLatin1String("abc"))
5239 QTest::newRow("data6")
5240 << QString(QLatin1String("abc"))
5244 QTest::newRow("data7")
5245 << QString(QLatin1String("abc"))
5249 QTest::newRow("data8")
5250 << QString(QLatin1String("abc"))
5251 << QString(QLatin1String("abc"))
5254 QTest::newRow("data9")
5255 << QString(QLatin1String("abc"))
5256 << QString(QLatin1String("abcabc"))
5259 QTest::newRow("data10")
5260 << QString(QLatin1String("abc"))
5261 << QString(QLatin1String("abcabcabc"))
5264 QTest::newRow("data11")
5265 << QString(QLatin1String("abc"))
5266 << QString(QLatin1String("abcabcabcabc"))
5270 void tst_QString::compareRef()
5272 QString a = "ABCDEFGH";
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);
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);
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);
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);
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);
5301 void tst_QString::arg_locale()
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);
5312 void tst_QString::toUpperLower_icu()
5315 QSKIP("Qt was built without ICU support");
5318 QString s = QString::fromLatin1("i");
5320 QCOMPARE(s.toUpper(), QString::fromLatin1("I"));
5321 QCOMPARE(s.toLower(), QString::fromLatin1("i"));
5323 QLocale::setDefault(QLocale(QLocale::Turkish, QLocale::Turkey));
5325 QCOMPARE(s.toUpper(), QString::fromLatin1("I"));
5326 QCOMPARE(s.toLower(), QString::fromLatin1("i"));
5328 // turkish locale has a capital I with a dot (U+0130, utf8 c4b0)
5331 QCOMPARE(l.toUpper(s), QString::fromUtf8("\xc4\xb0"));
5332 QCOMPARE(l.toLower(QString::fromUtf8("\xc4\xb0")), s);
5334 // nothing should happen here
5335 QCOMPARE(l.toLower(s), s);
5336 QCOMPARE(l.toUpper(QString::fromLatin1("I")), QString::fromLatin1("I"));
5338 // U+0131, utf8 c4b1 is the lower-case i without a dot
5339 QString sup = QString::fromUtf8("\xc4\xb1");
5341 QCOMPARE(l.toUpper(sup), QString::fromLatin1("I"));
5342 QCOMPARE(l.toLower(QString::fromLatin1("I")), sup);
5344 // nothing should happen here
5345 QCOMPARE(l.toLower(sup), sup);
5346 QCOMPARE(l.toLower(QString::fromLatin1("i")), QString::fromLatin1("i"));
5348 // the cleanup function will restore the default locale
5351 void tst_QString::literals()
5353 #if defined(QT_UNICODE_LITERAL) && (defined(Q_COMPILER_LAMBDA) || defined(Q_CC_GNU))
5354 QString str(QStringLiteral("abcd"));
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));
5361 const QChar *s = str.constData();
5363 QVERIFY(str2.constData() == s);
5365 // detach on non const access
5366 QVERIFY(str.data() != s);
5368 QVERIFY(str2.constData() == s);
5369 QVERIFY(str2.data() != s);
5372 QSKIP("Only tested on c++0x compliant compiler or gcc");
5376 void tst_QString::eightBitLiterals_data()
5378 QTest::addColumn<QByteArray>("data");
5379 QTest::addColumn<QString>("stringData");
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");
5387 void tst_QString::eightBitLiterals()
5389 QFETCH(QByteArray, data);
5390 QFETCH(QString, stringData);
5394 QCOMPARE(s, stringData);
5397 QString s(data.constData());
5398 QCOMPARE(s, stringData);
5403 QCOMPARE(s, stringData);
5407 s = data.constData();
5408 QCOMPARE(s, stringData);
5413 QCOMPARE(s, stringData);
5417 s.append(data.constData());
5418 QCOMPARE(s, stringData);
5423 QCOMPARE(s, stringData);
5427 s += data.constData();
5428 QCOMPARE(s, stringData);
5433 QCOMPARE(s, stringData);
5437 s.prepend(data.constData());
5438 QCOMPARE(s, stringData);
5441 QString s = QString() + data;
5442 QCOMPARE(s, stringData);
5445 QString s = QString() + data.constData();
5446 QCOMPARE(s, stringData);
5449 QString s = data + QString();
5450 QCOMPARE(s, stringData);
5453 QString s = QString() % data;
5454 QCOMPARE(s, stringData);
5457 QString s = QString() % data.constData();
5458 QCOMPARE(s, stringData);
5461 QString s = data % QString();
5462 QCOMPARE(s, stringData);
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());
5481 void tst_QString::reserve()
5490 void tst_QString::toHtmlEscaped_data()
5492 QTest::addColumn<QString>("original");
5493 QTest::addColumn<QString>("expected");
5495 QTest::newRow("1") << "Hello World\n" << "Hello World\n";
5496 QTest::newRow("2") << "#include <QtCore>" << "#include <QtCore>";
5497 QTest::newRow("3") << "<p class=\"cool\"><a href=\"http://example.com/?foo=bar&bar=foo\">plop --> </a></p>"
5498 << "<p class="cool"><a href="http://example.com/?foo=bar&amp;bar=foo">plop --&gt; </a></p>";
5499 QTest::newRow("4") << QString::fromUtf8("<\320\222\321\201>") << QString::fromUtf8("<\320\222\321\201>");
5502 void tst_QString::toHtmlEscaped()
5504 QFETCH(QString, original);
5505 QFETCH(QString, expected);
5507 QCOMPARE(original.toHtmlEscaped(), expected);
5510 void tst_QString::operatorGreaterWithQLatin1String()
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));
5520 void tst_QString::compareQLatin1Strings()
5522 QLatin1String abc("abc");
5523 QLatin1String abcd("abcd");
5524 QLatin1String cba("cba");
5525 QLatin1String de("de");
5527 QVERIFY(abc == abc);
5528 QVERIFY(!(abc == cba));
5529 QVERIFY(!(cba == abc));
5530 QVERIFY(!(abc == abcd));
5531 QVERIFY(!(abcd == abc));
5533 QVERIFY(abc != cba);
5534 QVERIFY(!(abc != abc));
5535 QVERIFY(cba != abc);
5536 QVERIFY(abc != abcd);
5537 QVERIFY(abcd != abc);
5539 QVERIFY(abc < abcd);
5542 QVERIFY(abcd < cba);
5543 QVERIFY(!(abc < abc));
5544 QVERIFY(!(abcd < abc));
5545 QVERIFY(!(de < cba));
5547 QVERIFY(abcd > abc);
5550 QVERIFY(!(abc > abc));
5551 QVERIFY(!(abc > abcd));
5552 QVERIFY(!(abcd > cba));
5554 QVERIFY(abc <= abc);
5555 QVERIFY(abc <= abcd);
5556 QVERIFY(abc <= cba);
5558 QVERIFY(!(abcd <= abc));
5559 QVERIFY(!(cba <= abc));
5560 QVERIFY(!(cba <= abcd));
5561 QVERIFY(!(de <= abc));
5563 QVERIFY(abc >= abc);
5564 QVERIFY(abcd >= abc);
5565 QVERIFY(!(abc >= abcd));
5566 QVERIFY(cba >= abc);
5567 QVERIFY(!(abc >= cba));
5569 QVERIFY(!(abc >= de));
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);
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));
5602 void tst_QString::fromQLatin1StringWithLength()
5604 QLatin1String latin1foo("foobar", 3);
5605 QString foo(latin1foo);
5606 QCOMPARE(foo.size(), latin1foo.size());
5607 QCOMPARE(foo, QString::fromLatin1("foo"));
5610 void tst_QString::assignQLatin1String()
5612 QString empty = QLatin1String("");
5613 QVERIFY(empty.isEmpty());
5614 QVERIFY(!empty.isNull());
5616 QString null = QLatin1String(0);
5617 QVERIFY(null.isEmpty());
5618 QVERIFY(null.isNull());
5620 QLatin1String latin1foo("foo");
5621 QString foo = latin1foo;
5622 QCOMPARE(foo.size(), latin1foo.size());
5623 QCOMPARE(foo, QString::fromLatin1("foo"));
5625 QLatin1String latin1subfoo("foobar", 3);
5627 QCOMPARE(foo.size(), latin1subfoo.size());
5628 QCOMPARE(foo, QString::fromLatin1("foo"));
5632 void tst_QString::isRightToLeft_data()
5634 QTest::addColumn<QString>("unicode");
5635 QTest::addColumn<bool>("rtl");
5637 QTest::newRow("null") << QString() << false;
5638 QTest::newRow("empty") << QString("") << false;
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;
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;
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;
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;
5657 void tst_QString::isRightToLeft()
5659 QFETCH(QString, unicode);
5662 QCOMPARE(unicode.isRightToLeft(), rtl);
5665 QTEST_APPLESS_MAIN(tst_QString)
5667 #include "tst_qstring.moc"