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