tests/auto/qbytearray/tst_qbytearray.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 
       
    45 #include <qbytearray.h>
       
    46 #include <qfile.h>
       
    47 #include <limits.h>
       
    48 #include <private/qtools_p.h>
       
    49 #if defined(Q_OS_WINCE)
       
    50 #include <qcoreapplication.h>
       
    51 #endif
       
    52 
       
    53 //TESTED_CLASS=
       
    54 //TESTED_FILES=
       
    55 
       
    56 #if defined(Q_OS_SYMBIAN)
       
    57 #define SRCDIR ""
       
    58 #endif
       
    59 
       
    60 class tst_QByteArray : public QObject
       
    61 {
       
    62     Q_OBJECT
       
    63 
       
    64 public:
       
    65     tst_QByteArray();
       
    66     virtual ~tst_QByteArray();
       
    67 
       
    68 public slots:
       
    69     void initTestCase();
       
    70     void cleanupTestCase();
       
    71     void init();
       
    72     void cleanup();
       
    73 private slots:
       
    74     void qCompress_data();
       
    75 #ifndef QT_NO_COMPRESS
       
    76     void qCompress();
       
    77     void qUncompress_data();
       
    78     void qUncompress();
       
    79 #endif
       
    80     void constByteArray();
       
    81     void leftJustified();
       
    82     void rightJustified();
       
    83     void startsWith_data();
       
    84     void startsWith();
       
    85     void startsWith_char();
       
    86     void endsWith_data();
       
    87     void endsWith();
       
    88     void endsWith_char();
       
    89     void split_data();
       
    90     void split();
       
    91     void base64_data();
       
    92     void base64();
       
    93     void fromBase64_data();
       
    94     void fromBase64();
       
    95     void qvsnprintf();
       
    96     void qstrlen();
       
    97     void qstrnlen();
       
    98     void qstrcpy();
       
    99     void qstrncpy();
       
   100     void qstricmp_data();
       
   101     void qstricmp();
       
   102     void qstricmp_singularities();
       
   103     void qstrnicmp_singularities();
       
   104     void chop_data();
       
   105     void chop();
       
   106     void prepend();
       
   107     void append();
       
   108     void insert();
       
   109     void remove_data();
       
   110     void remove();
       
   111     void replace_data();
       
   112     void replace();
       
   113     void indexOf_data();
       
   114     void indexOf();
       
   115     void lastIndexOf_data();
       
   116     void lastIndexOf();
       
   117     void toULong_data();
       
   118     void toULong();
       
   119     void toULongLong_data();
       
   120     void toULongLong();
       
   121 
       
   122     void number();
       
   123     void toInt_data();
       
   124     void toInt();
       
   125     void qAllocMore();
       
   126 
       
   127     void resizeAfterFromRawData();
       
   128     void appendAfterFromRawData();
       
   129     void toFromHex_data();
       
   130     void toFromHex();
       
   131     void toFromPercentEncoding();
       
   132 
       
   133     void compare_data();
       
   134     void compare();
       
   135     void compareCharStar_data();
       
   136     void compareCharStar();
       
   137 
       
   138     void repeatedSignature() const;
       
   139     void repeated() const;
       
   140     void repeated_data() const;
       
   141 
       
   142     void byteRefDetaching() const;
       
   143 };
       
   144 
       
   145 tst_QByteArray::tst_QByteArray()
       
   146 {
       
   147     qRegisterMetaType<qulonglong>("qulonglong");
       
   148 }
       
   149 
       
   150 tst_QByteArray::~tst_QByteArray()
       
   151 {
       
   152 }
       
   153 
       
   154 void tst_QByteArray::initTestCase()
       
   155 {
       
   156 }
       
   157 
       
   158 void tst_QByteArray::cleanupTestCase()
       
   159 {
       
   160 }
       
   161 
       
   162 void tst_QByteArray::init()
       
   163 {
       
   164 }
       
   165 
       
   166 void tst_QByteArray::cleanup()
       
   167 {
       
   168 }
       
   169 
       
   170 void tst_QByteArray::qCompress_data()
       
   171 {
       
   172     QTest::addColumn<QByteArray>("ba");
       
   173 
       
   174 #ifndef Q_OS_WINCE
       
   175     const int size1 = 1024*1024;
       
   176 #else
       
   177     const int size1 = 1024;
       
   178 #endif
       
   179     QByteArray ba1( size1, 0 );
       
   180 
       
   181     QTest::newRow( "00" ) << QByteArray();
       
   182 
       
   183     int i;
       
   184     for ( i=0; i<size1; i++ )
       
   185 	ba1[i] = (char)( i / 1024 );
       
   186     QTest::newRow( "01" ) << ba1;
       
   187 
       
   188     for ( i=0; i<size1; i++ )
       
   189 	ba1[i] = (char)( i % 256 );
       
   190     QTest::newRow( "02" ) << ba1;
       
   191 
       
   192     ba1.fill( 'A' );
       
   193     QTest::newRow( "03" ) << ba1;
       
   194 
       
   195 #if defined(Q_OS_WINCE)
       
   196     int tmpArgc = 0;
       
   197     char** tmpArgv = 0;
       
   198     QCoreApplication app(tmpArgc, tmpArgv);
       
   199 #endif
       
   200     QFile file( SRCDIR "rfc3252.txt" );
       
   201     QVERIFY( file.open(QIODevice::ReadOnly) );
       
   202     QTest::newRow( "04" ) << file.readAll();
       
   203 }
       
   204 
       
   205 #ifndef QT_NO_COMPRESS
       
   206 void tst_QByteArray::qCompress()
       
   207 {
       
   208     QFETCH( QByteArray, ba );
       
   209     QByteArray compressed = ::qCompress( ba );
       
   210     //qDebug( "size uncompressed: %d    size compressed: %d", ba.size(), compressed.size() );
       
   211     QTEST( ::qUncompress( compressed ), "ba" );
       
   212 }
       
   213 
       
   214 /*
       
   215     Just making sure it doesn't crash on invalid data.
       
   216 */
       
   217 void tst_QByteArray::qUncompress_data()
       
   218 {
       
   219     QTest::addColumn<QByteArray>("in");
       
   220     QTest::addColumn<QByteArray>("out");
       
   221 
       
   222     QTest::newRow("0x00000000") << QByteArray("\x00\x00\x00\x00") << QByteArray();
       
   223     QTest::newRow("0x000000ff") << QByteArray("\x00\x00\x00\xff") << QByteArray();
       
   224     QTest::newRow("0x3f000000") << QByteArray("\x3f\x00\x00\x00") << QByteArray();
       
   225     QTest::newRow("0x3fffffff") << QByteArray("\x3f\xff\xff\xff") << QByteArray();
       
   226     QTest::newRow("0x7fffff00") << QByteArray("\x7f\xff\xff\x00") << QByteArray();
       
   227     QTest::newRow("0x7fffffff") << QByteArray("\x7f\xff\xff\xff") << QByteArray();
       
   228     QTest::newRow("0x80000000") << QByteArray("\x80\x00\x00\x00") << QByteArray();
       
   229     QTest::newRow("0x800000ff") << QByteArray("\x80\x00\x00\xff") << QByteArray();
       
   230     QTest::newRow("0xcf000000") << QByteArray("\xcf\x00\x00\x00") << QByteArray();
       
   231     QTest::newRow("0xcfffffff") << QByteArray("\xcf\xff\xff\xff") << QByteArray();
       
   232     QTest::newRow("0xffffff00") << QByteArray("\xff\xff\xff\x00") << QByteArray();
       
   233     QTest::newRow("0xffffffff") << QByteArray("\xff\xff\xff\xff") << QByteArray();
       
   234 }
       
   235 
       
   236 void tst_QByteArray::qUncompress()
       
   237 {
       
   238     QFETCH(QByteArray, in);
       
   239     QFETCH(QByteArray, out);
       
   240 
       
   241 #if defined Q_OS_HPUX && !defined __ia64 && defined Q_CC_GNU
       
   242     QSKIP("Corrupt data causes this tests to lock up on HP-UX / PA-RISC with gcc", SkipAll);
       
   243 #elif defined Q_OS_SOLARIS
       
   244     QSKIP("Corrupt data causes this tests to lock up on Solaris", SkipAll);
       
   245 #elif defined Q_OS_QNX
       
   246     QSKIP("Corrupt data causes this test to lock up on QNX", SkipAll);
       
   247 #endif
       
   248 
       
   249     QByteArray res;
       
   250     QT_TRY {
       
   251         res = ::qUncompress(in);
       
   252     } QT_CATCH(const std::bad_alloc &) {
       
   253         res = QByteArray();
       
   254     }
       
   255     QCOMPARE(res, out);
       
   256 
       
   257     QT_TRY {
       
   258         res = ::qUncompress(in + "blah");
       
   259     } QT_CATCH(const std::bad_alloc &) {
       
   260         res = QByteArray();
       
   261     }
       
   262     QCOMPARE(res, out);
       
   263 }
       
   264 #endif
       
   265 
       
   266 void tst_QByteArray::constByteArray()
       
   267 {
       
   268     const char *ptr = "abc";
       
   269     QByteArray cba = QByteArray::fromRawData(ptr, 3);
       
   270     QVERIFY(cba.constData() == ptr);
       
   271     cba.squeeze();
       
   272     QVERIFY(cba.constData() == ptr);
       
   273     cba.detach();
       
   274     QVERIFY(cba.size() == 3);
       
   275     QVERIFY(cba.capacity() == 3);
       
   276     QVERIFY(cba.constData() != ptr);
       
   277     QVERIFY(cba.constData()[0] == 'a');
       
   278     QVERIFY(cba.constData()[1] == 'b');
       
   279     QVERIFY(cba.constData()[2] == 'c');
       
   280     QVERIFY(cba.constData()[3] == '\0');
       
   281 }
       
   282 
       
   283 void tst_QByteArray::leftJustified()
       
   284 {
       
   285     QByteArray a;
       
   286     a = "ABC";
       
   287     QCOMPARE(a.leftJustified(5,'-'), QByteArray("ABC--"));
       
   288     QCOMPARE(a.leftJustified(4,'-'), QByteArray("ABC-"));
       
   289     QCOMPARE(a.leftJustified(4), QByteArray("ABC "));
       
   290     QCOMPARE(a.leftJustified(3), QByteArray("ABC"));
       
   291     QCOMPARE(a.leftJustified(2), QByteArray("ABC"));
       
   292     QCOMPARE(a.leftJustified(1), QByteArray("ABC"));
       
   293     QCOMPARE(a.leftJustified(0), QByteArray("ABC"));
       
   294 
       
   295     QByteArray n;
       
   296     QVERIFY(!n.leftJustified(3).isNull());    // I expected true
       
   297     QCOMPARE(a.leftJustified(4,' ',true), QByteArray("ABC "));
       
   298     QCOMPARE(a.leftJustified(3,' ',true), QByteArray("ABC"));
       
   299     QCOMPARE(a.leftJustified(2,' ',true), QByteArray("AB"));
       
   300     QCOMPARE(a.leftJustified(1,' ',true), QByteArray("A"));
       
   301     QCOMPARE(a.leftJustified(0,' ',true), QByteArray(""));
       
   302 }
       
   303 
       
   304 void tst_QByteArray::rightJustified()
       
   305 {
       
   306     QByteArray a;
       
   307     a="ABC";
       
   308     QCOMPARE(a.rightJustified(5,'-'),QByteArray("--ABC"));
       
   309     QCOMPARE(a.rightJustified(4,'-'),QByteArray("-ABC"));
       
   310     QCOMPARE(a.rightJustified(4),QByteArray(" ABC"));
       
   311     QCOMPARE(a.rightJustified(3),QByteArray("ABC"));
       
   312     QCOMPARE(a.rightJustified(2),QByteArray("ABC"));
       
   313     QCOMPARE(a.rightJustified(1),QByteArray("ABC"));
       
   314     QCOMPARE(a.rightJustified(0),QByteArray("ABC"));
       
   315 
       
   316     QByteArray n;
       
   317     QVERIFY(!n.rightJustified(3).isNull());  // I expected true
       
   318     QCOMPARE(a.rightJustified(4,'-',true),QByteArray("-ABC"));
       
   319     QCOMPARE(a.rightJustified(4,' ',true),QByteArray(" ABC"));
       
   320     QCOMPARE(a.rightJustified(3,' ',true),QByteArray("ABC"));
       
   321     QCOMPARE(a.rightJustified(2,' ',true),QByteArray("AB"));
       
   322     QCOMPARE(a.rightJustified(1,' ',true),QByteArray("A"));
       
   323     QCOMPARE(a.rightJustified(0,' ',true),QByteArray(""));
       
   324     QCOMPARE(a,QByteArray("ABC"));
       
   325 }
       
   326 
       
   327 void tst_QByteArray::startsWith_data()
       
   328 {
       
   329     QTest::addColumn<QByteArray>("ba");
       
   330     QTest::addColumn<QByteArray>("sw");
       
   331     QTest::addColumn<bool>("result");
       
   332 
       
   333     QTest::newRow("01") << QByteArray() << QByteArray() << true;
       
   334     QTest::newRow("02") << QByteArray() << QByteArray("") << true;
       
   335     QTest::newRow("03") << QByteArray() << QByteArray("hallo") << false;
       
   336 
       
   337     QTest::newRow("04") << QByteArray("") << QByteArray() << true;
       
   338     QTest::newRow("05") << QByteArray("") << QByteArray("") << true;
       
   339     QTest::newRow("06") << QByteArray("") << QByteArray("h") << false;
       
   340 
       
   341     QTest::newRow("07") << QByteArray("hallo") << QByteArray("h") << true;
       
   342     QTest::newRow("08") << QByteArray("hallo") << QByteArray("hallo") << true;
       
   343     QTest::newRow("09") << QByteArray("hallo") << QByteArray("") << true;
       
   344     QTest::newRow("10") << QByteArray("hallo") << QByteArray("hallohallo") << false;
       
   345     QTest::newRow("11") << QByteArray("hallo") << QByteArray() << true;
       
   346 }
       
   347 
       
   348 void tst_QByteArray::startsWith()
       
   349 {
       
   350     QFETCH(QByteArray, ba);
       
   351     QFETCH(QByteArray, sw);
       
   352     QFETCH(bool, result);
       
   353 
       
   354     QVERIFY(ba.startsWith(sw) == result);
       
   355 
       
   356     if (sw.isNull()) {
       
   357         QVERIFY(ba.startsWith((char*)0) == result);
       
   358     } else {
       
   359         QVERIFY(ba.startsWith(sw.data()) == result);
       
   360     }
       
   361 }
       
   362 
       
   363 void tst_QByteArray::startsWith_char()
       
   364 {
       
   365     QVERIFY(QByteArray("hallo").startsWith('h'));
       
   366     QVERIFY(!QByteArray("hallo").startsWith('\0'));
       
   367     QVERIFY(!QByteArray("hallo").startsWith('o'));
       
   368     QVERIFY(QByteArray("h").startsWith('h'));
       
   369     QVERIFY(!QByteArray("h").startsWith('\0'));
       
   370     QVERIFY(!QByteArray("h").startsWith('o'));
       
   371     QVERIFY(!QByteArray("hallo").startsWith('l'));
       
   372     QVERIFY(!QByteArray("").startsWith('\0'));
       
   373     QVERIFY(!QByteArray("").startsWith('a'));
       
   374     QVERIFY(!QByteArray().startsWith('a'));
       
   375     QVERIFY(!QByteArray().startsWith('\0'));
       
   376 }
       
   377 
       
   378 void tst_QByteArray::endsWith_data()
       
   379 {
       
   380     QTest::addColumn<QByteArray>("ba");
       
   381     QTest::addColumn<QByteArray>("sw");
       
   382     QTest::addColumn<bool>("result");
       
   383 
       
   384     QTest::newRow("01") << QByteArray() << QByteArray() << true;
       
   385     QTest::newRow("02") << QByteArray() << QByteArray("") << true;
       
   386     QTest::newRow("03") << QByteArray() << QByteArray("hallo") << false;
       
   387 
       
   388     QTest::newRow("04") << QByteArray("") << QByteArray() << true;
       
   389     QTest::newRow("05") << QByteArray("") << QByteArray("") << true;
       
   390     QTest::newRow("06") << QByteArray("") << QByteArray("h") << false;
       
   391 
       
   392     QTest::newRow("07") << QByteArray("hallo") << QByteArray("o") << true;
       
   393     QTest::newRow("08") << QByteArray("hallo") << QByteArray("hallo") << true;
       
   394     QTest::newRow("09") << QByteArray("hallo") << QByteArray("") << true;
       
   395     QTest::newRow("10") << QByteArray("hallo") << QByteArray("hallohallo") << false;
       
   396     QTest::newRow("11") << QByteArray("hallo") << QByteArray() << true;
       
   397 }
       
   398 
       
   399 void tst_QByteArray::endsWith()
       
   400 {
       
   401     QFETCH(QByteArray, ba);
       
   402     QFETCH(QByteArray, sw);
       
   403     QFETCH(bool, result);
       
   404 
       
   405     QVERIFY(ba.endsWith(sw) == result);
       
   406 
       
   407     if (sw.isNull()) {
       
   408         QVERIFY(ba.endsWith((char*)0) == result);
       
   409     } else {
       
   410         QVERIFY(ba.endsWith(sw.data()) == result);
       
   411     }
       
   412 }
       
   413 
       
   414 void tst_QByteArray::endsWith_char()
       
   415 {
       
   416     QVERIFY(QByteArray("hallo").endsWith('o'));
       
   417     QVERIFY(!QByteArray("hallo").endsWith('\0'));
       
   418     QVERIFY(!QByteArray("hallo").endsWith('h'));
       
   419     QVERIFY(QByteArray("h").endsWith('h'));
       
   420     QVERIFY(!QByteArray("h").endsWith('\0'));
       
   421     QVERIFY(!QByteArray("h").endsWith('o'));
       
   422     QVERIFY(!QByteArray("hallo").endsWith('l'));
       
   423     QVERIFY(!QByteArray("").endsWith('\0'));
       
   424     QVERIFY(!QByteArray("").endsWith('a'));
       
   425     QVERIFY(!QByteArray().endsWith('a'));
       
   426     QVERIFY(!QByteArray().endsWith('\0'));
       
   427 }
       
   428 
       
   429 void tst_QByteArray::split_data()
       
   430 {
       
   431     QTest::addColumn<QByteArray>("sample");
       
   432     QTest::addColumn<int>("size");
       
   433 
       
   434     QTest::newRow("1") << QByteArray("-rw-r--r--  1 0  0  519240 Jul  9  2002 bigfile")
       
   435 		    << 14;
       
   436     QTest::newRow("2") << QByteArray("abcde") << 1;
       
   437     QTest::newRow("one empty") << QByteArray("") << 1;
       
   438     QTest::newRow("two empty") << QByteArray(" ") << 2;
       
   439     QTest::newRow("three empty") << QByteArray("  ") << 3;
       
   440 
       
   441 }
       
   442 
       
   443 void tst_QByteArray::split()
       
   444 {
       
   445     QFETCH(QByteArray, sample);
       
   446     QFETCH(int, size);
       
   447 
       
   448     QList<QByteArray> list = sample.split(' ');
       
   449     QCOMPARE(list.count(), size);
       
   450 }
       
   451 
       
   452 void tst_QByteArray::base64_data()
       
   453 {
       
   454     QTest::addColumn<QByteArray>("rawdata");
       
   455     QTest::addColumn<QByteArray>("base64");
       
   456 
       
   457     QTest::newRow("1") << QByteArray("") << QByteArray("");
       
   458     QTest::newRow("2") << QByteArray("1") << QByteArray("MQ==");
       
   459     QTest::newRow("3") << QByteArray("12") << QByteArray("MTI=");
       
   460     QTest::newRow("4") << QByteArray("123") << QByteArray("MTIz");
       
   461     QTest::newRow("5") << QByteArray("1234") << QByteArray("MTIzNA==");
       
   462     QTest::newRow("6") << QByteArray("\n") << QByteArray("Cg==");
       
   463     QTest::newRow("7") << QByteArray("a\n") << QByteArray("YQo=");
       
   464     QTest::newRow("8") << QByteArray("ab\n") << QByteArray("YWIK");
       
   465     QTest::newRow("9") << QByteArray("abc\n") << QByteArray("YWJjCg==");
       
   466     QTest::newRow("a") << QByteArray("abcd\n") << QByteArray("YWJjZAo=");
       
   467     QTest::newRow("b") << QByteArray("abcde\n") << QByteArray("YWJjZGUK");
       
   468     QTest::newRow("c") << QByteArray("abcdef\n") << QByteArray("YWJjZGVmCg==");
       
   469     QTest::newRow("d") << QByteArray("abcdefg\n") << QByteArray("YWJjZGVmZwo=");
       
   470     QTest::newRow("e") << QByteArray("abcdefgh\n") << QByteArray("YWJjZGVmZ2gK");
       
   471 
       
   472     QByteArray ba;
       
   473     ba.resize(256);
       
   474     for (int i = 0; i < 256; ++i)
       
   475         ba[i] = i;
       
   476     QTest::newRow("f") << ba << QByteArray("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==");
       
   477 
       
   478     QTest::newRow("g") << QByteArray("foo\0bar", 7) << QByteArray("Zm9vAGJhcg==");
       
   479     QTest::newRow("h") << QByteArray("f\xd1oo\x9ctar") << QByteArray("ZtFvb5x0YXI=");
       
   480     QTest::newRow("i") << QByteArray("\"\0\0\0\0\0\0\"", 8) << QByteArray("IgAAAAAAACI=");
       
   481 }
       
   482 
       
   483 
       
   484 void tst_QByteArray::base64()
       
   485 {
       
   486     QFETCH(QByteArray, rawdata);
       
   487     QFETCH(QByteArray, base64);
       
   488 
       
   489     QByteArray arr = QByteArray::fromBase64(base64);
       
   490     QCOMPARE(arr, rawdata);
       
   491 
       
   492     QByteArray arr64 = rawdata.toBase64();
       
   493     QCOMPARE(arr64, base64);
       
   494 }
       
   495 
       
   496 //different from the previous test as the input are invalid
       
   497 void tst_QByteArray::fromBase64_data()
       
   498 {
       
   499     QTest::addColumn<QByteArray>("rawdata");
       
   500     QTest::addColumn<QByteArray>("base64");
       
   501 
       
   502     QTest::newRow("1") << QByteArray("") << QByteArray("  ");
       
   503     QTest::newRow("2") << QByteArray("1") << QByteArray("MQ");
       
   504     QTest::newRow("3") << QByteArray("12") << QByteArray("MTI       ");
       
   505     QTest::newRow("4") << QByteArray("123") << QByteArray("M=TIz");
       
   506     QTest::newRow("5") << QByteArray("1234") << QByteArray("MTI zN A ");
       
   507     QTest::newRow("6") << QByteArray("\n") << QByteArray("Cg");
       
   508     QTest::newRow("7") << QByteArray("a\n") << QByteArray("======YQo=");
       
   509     QTest::newRow("8") << QByteArray("ab\n") << QByteArray("Y\nWIK");
       
   510     QTest::newRow("9") << QByteArray("abc\n") << QByteArray("YWJjCg==");
       
   511     QTest::newRow("a") << QByteArray("abcd\n") << QByteArray("YWJ\1j\x9cZAo=");
       
   512     QTest::newRow("b") << QByteArray("abcde\n") << QByteArray("YW JjZ\n G\tUK");
       
   513     QTest::newRow("c") << QByteArray("abcdef\n") << QByteArray("YWJjZGVmCg=");
       
   514     QTest::newRow("d") << QByteArray("abcdefg\n") << QByteArray("YWJ\rjZGVmZwo");
       
   515     QTest::newRow("e") << QByteArray("abcdefgh\n") << QByteArray("YWJjZGVmZ2gK");
       
   516 
       
   517     QByteArray ba;
       
   518     ba.resize(256);
       
   519     for (int i = 0; i < 256; ++i)
       
   520         ba[i] = i;
       
   521     QTest::newRow("f") << ba << QByteArray("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Nj\n"
       
   522                                            "c4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1u\n"
       
   523                                            "b3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpa\n"
       
   524                                            "anqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd\n"
       
   525                                            "3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==                            ");
       
   526 
       
   527 
       
   528     QTest::newRow("g") << QByteArray("foo\0bar", 7) << QByteArray("Zm9vAGJhcg");
       
   529     QTest::newRow("h") << QByteArray("f\xd1oo\x9ctar") << QByteArray("ZtFvb5x0YXI=");
       
   530     QTest::newRow("i") << QByteArray("\"\0\0\0\0\0\0\"", 8) << QByteArray("IgAAAAAAACI");
       
   531 
       
   532 }
       
   533 
       
   534 
       
   535 void tst_QByteArray::fromBase64()
       
   536 {
       
   537     QFETCH(QByteArray, rawdata);
       
   538     QFETCH(QByteArray, base64);
       
   539 
       
   540     QByteArray arr = QByteArray::fromBase64(base64);
       
   541     QCOMPARE(arr, rawdata);
       
   542 }
       
   543 
       
   544 void tst_QByteArray::qvsnprintf()
       
   545 {
       
   546     char buf[20];
       
   547     qMemSet(buf, 42, sizeof(buf));
       
   548 
       
   549     QCOMPARE(::qsnprintf(buf, 10, "%s", "bubu"), 4);
       
   550     QCOMPARE(static_cast<const char *>(buf), "bubu");
       
   551     QCOMPARE(buf[5], char(42));
       
   552 
       
   553     qMemSet(buf, 42, sizeof(buf));
       
   554     QCOMPARE(::qsnprintf(buf, 5, "%s", "bubu"), 4);
       
   555     QCOMPARE(static_cast<const char *>(buf), "bubu");
       
   556     QCOMPARE(buf[5], char(42));
       
   557 
       
   558     qMemSet(buf, 42, sizeof(buf));
       
   559 #ifdef Q_OS_WIN
       
   560     // VS 2005 uses the Qt implementation of vsnprintf.
       
   561 # if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(Q_OS_WINCE)
       
   562     QCOMPARE(::qsnprintf(buf, 3, "%s", "bubu"), -1);
       
   563     QCOMPARE(static_cast<const char*>(buf), "bu");
       
   564 # else
       
   565     // windows has to do everything different, of course.
       
   566     QCOMPARE(::qsnprintf(buf, 3, "%s", "bubu"), -1);
       
   567     buf[19] = '\0';
       
   568     QCOMPARE(static_cast<const char *>(buf), "bub****************");
       
   569 # endif
       
   570 #else
       
   571 #ifdef Q_OS_IRIX
       
   572     // Irix reports back the amount of characters written without the \0
       
   573     QCOMPARE(::qsnprintf(buf, 3, "%s", "bubu"), 2);
       
   574 #else
       
   575     // Every other system in this world reports the amount of data that could have been written
       
   576     QCOMPARE(::qsnprintf(buf, 3, "%s", "bubu"), 4);
       
   577 #endif
       
   578     QCOMPARE(static_cast<const char*>(buf), "bu");
       
   579 #endif
       
   580     QCOMPARE(buf[4], char(42));
       
   581 
       
   582 #ifndef Q_OS_WIN
       
   583     qMemSet(buf, 42, sizeof(buf));
       
   584     QCOMPARE(::qsnprintf(buf, 10, ""), 0);
       
   585 #endif
       
   586 }
       
   587 
       
   588 
       
   589 void tst_QByteArray::qstrlen()
       
   590 {
       
   591     const char *src = "Something about ... \0 a string.";
       
   592     QCOMPARE(::qstrlen((char*)0), (uint)0);
       
   593     QCOMPARE(::qstrlen(src), (uint)20);
       
   594 }
       
   595 
       
   596 void tst_QByteArray::qstrnlen()
       
   597 {
       
   598     const char *src = "Something about ... \0 a string.";
       
   599     QCOMPARE(::qstrnlen((char*)0, 1), (uint)0);
       
   600     QCOMPARE(::qstrnlen(src, 31), (uint)20);
       
   601     QCOMPARE(::qstrnlen(src, 19), (uint)19);
       
   602     QCOMPARE(::qstrnlen(src, 21), (uint)20);
       
   603     QCOMPARE(::qstrnlen(src, 20), (uint)20);
       
   604 }
       
   605 
       
   606 void tst_QByteArray::qstrcpy()
       
   607 {
       
   608     const char *src = "Something about ... \0 a string.";
       
   609     const char *expected = "Something about ... ";
       
   610     char dst[128];
       
   611 
       
   612     QCOMPARE(::qstrcpy(0, 0), (char*)0);
       
   613     QCOMPARE(::qstrcpy(dst, 0), (char*)0);
       
   614 
       
   615     QCOMPARE(::qstrcpy(dst ,src), (char *)dst);
       
   616     QCOMPARE((char *)dst, const_cast<char *>(expected));
       
   617 }
       
   618 
       
   619 void tst_QByteArray::qstrncpy()
       
   620 {
       
   621     QByteArray src(1024, 'a'), dst(1024, 'b');
       
   622 
       
   623     // singularities
       
   624     QCOMPARE(::qstrncpy(0, 0,0), (char*)0);
       
   625     QCOMPARE(::qstrncpy(dst.data(), 0, 0), (char*)0);
       
   626 
       
   627     // normal copy
       
   628     QCOMPARE(::qstrncpy(dst.data(), src.data(), src.size()), dst.data());
       
   629 
       
   630     src = QByteArray( "Tumdelidum" );
       
   631     QCOMPARE(QByteArray(::qstrncpy(dst.data(), src.data(), src.size())),
       
   632             QByteArray("Tumdelidu"));
       
   633 
       
   634     // normal copy with length is longer than neccessary
       
   635     src = QByteArray( "Tumdelidum\0foo" );
       
   636     dst.resize(128*1024);
       
   637     QCOMPARE(QByteArray(::qstrncpy(dst.data(), src.data(), dst.size())),
       
   638             QByteArray("Tumdelidum"));
       
   639 }
       
   640 
       
   641 void tst_QByteArray::qstricmp_data()
       
   642 {
       
   643     QTest::addColumn<QString>("str1");
       
   644     QTest::addColumn<QString>("str2");
       
   645 
       
   646     QTest::newRow("equal   1") << "abcEdb"  << "abcEdb";
       
   647     QTest::newRow("equal   2") << "abcEdb"  << "ABCeDB";
       
   648     QTest::newRow("equal   3") << "ABCEDB"  << "abcedb";
       
   649     QTest::newRow("less    1") << "abcdef"  << "abcdefg";
       
   650     QTest::newRow("less    2") << "abcdeF"  << "abcdef";
       
   651     QTest::newRow("greater 1") << "abcdef"  << "abcdeF";
       
   652     QTest::newRow("greater 2") << "abcdefg" << "abcdef";
       
   653 }
       
   654 
       
   655 void tst_QByteArray::qstricmp()
       
   656 {
       
   657     QFETCH(QString, str1);
       
   658     QFETCH(QString, str2);
       
   659 
       
   660     int expected = strcmp(str1.toUpper().toLatin1(),
       
   661                           str2.toUpper().toLatin1());
       
   662     if ( expected != 0 ) {
       
   663         expected = (expected < 0 ? -1 : 1);
       
   664     }
       
   665     int actual = ::qstricmp(str1.toLatin1(), str2.toLatin1());
       
   666     if ( actual != 0 ) {
       
   667         actual = (actual < 0 ? -1 : 1);
       
   668     }
       
   669     QCOMPARE(expected, actual);
       
   670 }
       
   671 
       
   672 void tst_QByteArray::qstricmp_singularities()
       
   673 {
       
   674     QCOMPARE(::qstricmp(0, 0), 0);
       
   675     QVERIFY(::qstricmp(0, "a") != 0);
       
   676     QVERIFY(::qstricmp("a", 0) != 0);
       
   677     QCOMPARE(::qstricmp("", ""), 0);
       
   678 }
       
   679 
       
   680 void tst_QByteArray::qstrnicmp_singularities()
       
   681 {
       
   682     QCOMPARE(::qstrnicmp(0, 0, 123), 0);
       
   683     QVERIFY(::qstrnicmp(0, "a", 123) != 0);
       
   684     QVERIFY(::qstrnicmp("a", 0, 123) != 0);
       
   685     QCOMPARE(::qstrnicmp("", "", 123), 0);
       
   686     QCOMPARE(::qstrnicmp("a", "B", 0), 0);
       
   687 }
       
   688 
       
   689 void tst_QByteArray::chop_data()
       
   690 {
       
   691     QTest::addColumn<QByteArray>("src");
       
   692     QTest::addColumn<int>("choplength");
       
   693     QTest::addColumn<QByteArray>("expected");
       
   694 
       
   695     QTest::newRow("1") << QByteArray("short1") << 128 << QByteArray();
       
   696     QTest::newRow("2") << QByteArray("short2") << int(strlen("short2"))
       
   697                     << QByteArray();
       
   698     QTest::newRow("3") << QByteArray("abcdef\0foo", 10) << 2
       
   699                     << QByteArray("abcdef\0f", 8);
       
   700     QTest::newRow("4") << QByteArray("STARTTLS\r\n") << 2
       
   701                     << QByteArray("STARTTLS");
       
   702     QTest::newRow("5") << QByteArray("") << 1 << QByteArray();
       
   703     QTest::newRow("6") << QByteArray("foo") << 0 << QByteArray("foo");
       
   704     QTest::newRow("7") << QByteArray(0) << 28 << QByteArray();
       
   705 }
       
   706 
       
   707 void tst_QByteArray::chop()
       
   708 {
       
   709     QFETCH(QByteArray, src);
       
   710     QFETCH(int, choplength);
       
   711     QFETCH(QByteArray, expected);
       
   712 
       
   713     src.chop(choplength);
       
   714     QCOMPARE(src, expected);
       
   715 }
       
   716 
       
   717 void tst_QByteArray::prepend()
       
   718 {
       
   719     QByteArray ba("foo");
       
   720     QCOMPARE(ba.prepend((char*)0), QByteArray("foo"));
       
   721     QCOMPARE(ba.prepend(QByteArray()), QByteArray("foo"));
       
   722     QCOMPARE(ba.prepend("1"), QByteArray("1foo"));
       
   723     QCOMPARE(ba.prepend(QByteArray("2")), QByteArray("21foo"));
       
   724     QCOMPARE(ba.prepend('3'), QByteArray("321foo"));
       
   725     QCOMPARE(ba.prepend("\0 ", 2), QByteArray::fromRawData("\0 321foo", 8));
       
   726 }
       
   727 
       
   728 void tst_QByteArray::append()
       
   729 {
       
   730     QByteArray ba("foo");
       
   731     QCOMPARE(ba.append((char*)0), QByteArray("foo"));
       
   732     QCOMPARE(ba.append(QByteArray()), QByteArray("foo"));
       
   733     QCOMPARE(ba.append("1"), QByteArray("foo1"));
       
   734     QCOMPARE(ba.append(QByteArray("2")), QByteArray("foo12"));
       
   735     QCOMPARE(ba.append('3'), QByteArray("foo123"));
       
   736     QCOMPARE(ba.append("\0"), QByteArray("foo123"));
       
   737     QCOMPARE(ba.append("\0", 1), QByteArray::fromRawData("foo123\0", 7));
       
   738     QCOMPARE(ba.size(), 7);
       
   739 }
       
   740 
       
   741 void tst_QByteArray::insert()
       
   742 {
       
   743     QByteArray ba("Meal");
       
   744     QCOMPARE(ba.insert(1, QByteArray("ontr")), QByteArray("Montreal"));
       
   745     QCOMPARE(ba.insert(ba.size(), "foo"), QByteArray("Montrealfoo"));
       
   746 
       
   747     ba = QByteArray("13");
       
   748     QCOMPARE(ba.insert(1, QByteArray("2")), QByteArray("123"));
       
   749 
       
   750     ba = "ac";
       
   751     QCOMPARE(ba.insert(1, 'b'), QByteArray("abc"));
       
   752     QCOMPARE(ba.size(), 3);
       
   753 
       
   754     ba = "ikl";
       
   755     QCOMPARE(ba.insert(1, "j"), QByteArray("ijkl"));
       
   756     QCOMPARE(ba.size(), 4);
       
   757 
       
   758     ba = "ab";
       
   759     QCOMPARE(ba.insert(1, "\0X\0", 3), QByteArray::fromRawData("a\0X\0b", 5));
       
   760     QCOMPARE(ba.size(), 5);
       
   761 }
       
   762 
       
   763 void tst_QByteArray::remove_data()
       
   764 {
       
   765     QTest::addColumn<QByteArray>("src");
       
   766     QTest::addColumn<int>("position");
       
   767     QTest::addColumn<int>("length");
       
   768     QTest::addColumn<QByteArray>("expected");
       
   769 
       
   770     QTest::newRow("1") << QByteArray("Montreal") << 1 << 4
       
   771                     << QByteArray("Meal");
       
   772     QTest::newRow("2") << QByteArray() << 10 << 10 << QByteArray();
       
   773     QTest::newRow("3") << QByteArray("hi") << 0 << 10 << QByteArray();
       
   774     QTest::newRow("4") << QByteArray("Montreal") << 4 << 100
       
   775                     << QByteArray("Mont");
       
   776 
       
   777     // index out of range
       
   778     QTest::newRow("5") << QByteArray("Montreal") << 8 << 1
       
   779                     << QByteArray("Montreal");
       
   780     QTest::newRow("6") << QByteArray("Montreal") << 18 << 4
       
   781                     << QByteArray("Montreal");
       
   782 }
       
   783 
       
   784 void tst_QByteArray::remove()
       
   785 {
       
   786     QFETCH(QByteArray, src);
       
   787     QFETCH(int, position);
       
   788     QFETCH(int, length);
       
   789     QFETCH(QByteArray, expected);
       
   790     QCOMPARE(src.remove(position, length), expected);
       
   791 }
       
   792 
       
   793 void tst_QByteArray::replace_data()
       
   794 {
       
   795     QTest::addColumn<QByteArray>("src");
       
   796     QTest::addColumn<int>("pos");
       
   797     QTest::addColumn<int>("len");
       
   798     QTest::addColumn<QByteArray>("after");
       
   799     QTest::addColumn<QByteArray>("expected");
       
   800 
       
   801     QTest::newRow("1") << QByteArray("Say yes!") << 4 << 3
       
   802                     << QByteArray("no") << QByteArray("Say no!");
       
   803     QTest::newRow("2") << QByteArray("rock and roll") << 5 << 3
       
   804                     << QByteArray("&") << QByteArray("rock & roll");
       
   805     QTest::newRow("3") << QByteArray("foo") << 3 << 0 << QByteArray("bar")
       
   806                     << QByteArray("foobar");
       
   807     QTest::newRow("4") << QByteArray() << 0 << 0 << QByteArray() << QByteArray();
       
   808     // index out of range
       
   809     QTest::newRow("5") << QByteArray() << 3 << 0 << QByteArray("hi")
       
   810                     << QByteArray("   hi");
       
   811     // Optimized path
       
   812     QTest::newRow("6") << QByteArray("abcdef") << 3 << 12 << QByteArray("abcdefghijkl") << QByteArray("abcabcdefghijkl");
       
   813     QTest::newRow("7") << QByteArray("abcdef") << 3 << 4  << QByteArray("abcdefghijkl") << QByteArray("abcabcdefghijkl");
       
   814     QTest::newRow("8") << QByteArray("abcdef") << 3 << 3  << QByteArray("abcdefghijkl") << QByteArray("abcabcdefghijkl");
       
   815     QTest::newRow("9") << QByteArray("abcdef") << 3 << 2  << QByteArray("abcdefghijkl") << QByteArray("abcabcdefghijklf");
       
   816     QTest::newRow("10") << QByteArray("abcdef") << 2 << 2  << QByteArray("xx") << QByteArray("abxxef");
       
   817 }
       
   818 
       
   819 void tst_QByteArray::replace()
       
   820 {
       
   821     QFETCH(QByteArray, src);
       
   822     QFETCH(int, pos);
       
   823     QFETCH(int, len);
       
   824     QFETCH(QByteArray, after);
       
   825     QFETCH(QByteArray, expected);
       
   826 
       
   827     QByteArray str1 = src;
       
   828     QByteArray str2 = src;
       
   829 
       
   830     QCOMPARE(str1.replace(pos, len, after).constData(), expected.constData());
       
   831     QCOMPARE(str2.replace(pos, len, after.data()), expected);
       
   832 }
       
   833 
       
   834 void tst_QByteArray::indexOf_data()
       
   835 {
       
   836     QTest::addColumn<QByteArray>("haystack");
       
   837     QTest::addColumn<QByteArray>("needle");
       
   838     QTest::addColumn<int>("startpos");
       
   839     QTest::addColumn<int>("expected");
       
   840 
       
   841     QTest::newRow( "1" ) << QByteArray("abc") << QByteArray("a") << 0 << 0;
       
   842     QTest::newRow( "2" ) << QByteArray("abc") << QByteArray("A") << 0 << -1;
       
   843     QTest::newRow( "3" ) << QByteArray("abc") << QByteArray("a") << 1 << -1;
       
   844     QTest::newRow( "4" ) << QByteArray("abc") << QByteArray("A") << 1 << -1;
       
   845     QTest::newRow( "5" ) << QByteArray("abc") << QByteArray("b") << 0 << 1;
       
   846     QTest::newRow( "6" ) << QByteArray("abc") << QByteArray("B") << 0 << -1;
       
   847     QTest::newRow( "7" ) << QByteArray("abc") << QByteArray("b") << 1 << 1;
       
   848     QTest::newRow( "8" ) << QByteArray("abc") << QByteArray("B") << 1 << -1;
       
   849     QTest::newRow( "9" ) << QByteArray("abc") << QByteArray("b") << 2 << -1;
       
   850     QTest::newRow( "10" ) << QByteArray("abc") << QByteArray("c") << 0 << 2;
       
   851     QTest::newRow( "11" ) << QByteArray("abc") << QByteArray("C") << 0 << -1;
       
   852     QTest::newRow( "12" ) << QByteArray("abc") << QByteArray("c") << 1 << 2;
       
   853     QTest::newRow( "13" ) << QByteArray("abc") << QByteArray("C") << 1 << -1;
       
   854     QTest::newRow( "14" ) << QByteArray("abc") << QByteArray("c") << 2 << 2;
       
   855     QTest::newRow( "15" ) << QByteArray("aBc") << QByteArray("bc") << 0 << -1;
       
   856     QTest::newRow( "16" ) << QByteArray("aBc") << QByteArray("Bc") << 0 << 1;
       
   857     QTest::newRow( "17" ) << QByteArray("aBc") << QByteArray("bC") << 0 << -1;
       
   858     QTest::newRow( "18" ) << QByteArray("aBc") << QByteArray("BC") << 0 << -1;
       
   859 
       
   860     // task 203692
       
   861     static const char h19[] = {'x', 0x00, 0xe7, 0x25, 0x1c, 0x0a};
       
   862     static const char n19[] = {0x00, 0x00, 0x01, 0x00};
       
   863     QTest::newRow( "19" ) << QByteArray(h19, sizeof(h19))
       
   864                           << QByteArray(n19, sizeof(n19)) << 0 << -1;
       
   865 
       
   866     QTest::newRow( "empty" ) << QByteArray("") << QByteArray("x") << 0 << -1;
       
   867     QTest::newRow( "null" ) << QByteArray() << QByteArray("x") << 0 << -1;
       
   868     QTest::newRow( "null-in-null") << QByteArray() << QByteArray() << 0 << 0;
       
   869     QTest::newRow( "empty-in-null") << QByteArray() << QByteArray("") << 0 << 0;
       
   870     QTest::newRow( "null-in-empty") << QByteArray("") << QByteArray() << 0 << 0;
       
   871     QTest::newRow( "empty-in-empty") << QByteArray("") << QByteArray("") << 0 << 0;
       
   872 
       
   873     QByteArray veryBigHaystack(500, 'a');
       
   874     veryBigHaystack += 'B';
       
   875     QTest::newRow("BoyerMooreStressTest") << veryBigHaystack << veryBigHaystack << 0 << 0;
       
   876     QTest::newRow("BoyerMooreStressTest2") << veryBigHaystack + 'c' << veryBigHaystack << 0 << 0;
       
   877     QTest::newRow("BoyerMooreStressTest3") << 'c' + veryBigHaystack << veryBigHaystack << 0 << 1;
       
   878     QTest::newRow("BoyerMooreStressTest4") << veryBigHaystack << veryBigHaystack + 'c' << 0 << -1;
       
   879     QTest::newRow("BoyerMooreStressTest5") << veryBigHaystack << 'c' + veryBigHaystack << 0 << -1;
       
   880     QTest::newRow("BoyerMooreStressTest6") << 'd' + veryBigHaystack << 'c' + veryBigHaystack << 0 << -1;
       
   881     QTest::newRow("BoyerMooreStressTest6") << veryBigHaystack + 'c' << 'c' + veryBigHaystack << 0 << -1;
       
   882 }
       
   883 
       
   884 void tst_QByteArray::indexOf()
       
   885 {
       
   886     QFETCH( QByteArray, haystack );
       
   887     QFETCH( QByteArray, needle );
       
   888     QFETCH( int, startpos );
       
   889     QFETCH( int, expected );
       
   890 
       
   891     bool hasNull = needle.contains('\0');
       
   892 
       
   893     QCOMPARE( haystack.indexOf(needle, startpos), expected );
       
   894     if (!hasNull)
       
   895         QCOMPARE( haystack.indexOf(needle.data(), startpos), expected );
       
   896     if (needle.size() == 1)
       
   897         QCOMPARE( haystack.indexOf(needle.at(0), startpos), expected );
       
   898 
       
   899     if (startpos == 0) {
       
   900         QCOMPARE( haystack.indexOf(needle), expected );
       
   901         if (!hasNull)
       
   902             QCOMPARE( haystack.indexOf(needle.data()), expected );
       
   903         if (needle.size() == 1)
       
   904             QCOMPARE( haystack.indexOf(needle.at(0)), expected );
       
   905     }
       
   906 }
       
   907 
       
   908 void tst_QByteArray::lastIndexOf_data()
       
   909 {
       
   910     QTest::addColumn<QByteArray>("haystack");
       
   911     QTest::addColumn<QByteArray>("needle");
       
   912     QTest::addColumn<int>("startpos");
       
   913     QTest::addColumn<int>("expected");
       
   914 
       
   915     QTest::newRow( "1" ) << QByteArray("abc") << QByteArray("a") << 0 << 0;
       
   916     QTest::newRow( "2" ) << QByteArray("abc") << QByteArray("A") << 0 << -1;
       
   917     QTest::newRow( "3" ) << QByteArray("abc") << QByteArray("a") << 1 << 0;
       
   918     QTest::newRow( "4" ) << QByteArray("abc") << QByteArray("A") << 1 << -1;
       
   919     QTest::newRow( "5" ) << QByteArray("abc") << QByteArray("a") << -1 << 0;
       
   920     QTest::newRow( "6" ) << QByteArray("abc") << QByteArray("b") << 0 << -1;
       
   921     QTest::newRow( "7" ) << QByteArray("abc") << QByteArray("B") << 0 << -1;
       
   922     QTest::newRow( "8" ) << QByteArray("abc") << QByteArray("b") << 1 << 1;
       
   923     QTest::newRow( "9" ) << QByteArray("abc") << QByteArray("B") << 1 << -1;
       
   924     QTest::newRow( "10" ) << QByteArray("abc") << QByteArray("b") << 2 << 1;
       
   925     QTest::newRow( "11" ) << QByteArray("abc") << QByteArray("b") << -1 << 1;
       
   926     QTest::newRow( "12" ) << QByteArray("abc") << QByteArray("c") << 0 << -1;
       
   927     QTest::newRow( "13" ) << QByteArray("abc") << QByteArray("C") << 0 << -1;
       
   928     QTest::newRow( "14" ) << QByteArray("abc") << QByteArray("c") << 1 << -1;
       
   929     QTest::newRow( "15" ) << QByteArray("abc") << QByteArray("C") << 1 << -1;
       
   930     QTest::newRow( "16" ) << QByteArray("abc") << QByteArray("c") << 2 << 2;
       
   931     QTest::newRow( "17" ) << QByteArray("abc") << QByteArray("c") << -1 << 2;
       
   932     QTest::newRow( "18" ) << QByteArray("aBc") << QByteArray("bc") << 0 << -1;
       
   933     QTest::newRow( "19" ) << QByteArray("aBc") << QByteArray("Bc") << 0 << -1;
       
   934     QTest::newRow( "20" ) << QByteArray("aBc") << QByteArray("Bc") << 2 << 1;
       
   935     QTest::newRow( "21" ) << QByteArray("aBc") << QByteArray("Bc") << 1 << 1;
       
   936     QTest::newRow( "22" ) << QByteArray("aBc") << QByteArray("Bc") << -1 << 1;
       
   937     QTest::newRow( "23" ) << QByteArray("aBc") << QByteArray("bC") << 0 << -1;
       
   938     QTest::newRow( "24" ) << QByteArray("aBc") << QByteArray("BC") << 0 << -1;
       
   939 
       
   940     // task 203692
       
   941     static const char h25[] = {0x00, 0xbc, 0x03, 0x10, 0x0a };
       
   942     static const char n25[] = {0x00, 0x00, 0x01, 0x00};
       
   943     QTest::newRow( "25" ) << QByteArray(h25, sizeof(h25))
       
   944                           << QByteArray(n25, sizeof(n25)) << 0 << -1;
       
   945 
       
   946     QTest::newRow( "empty" ) << QByteArray("") << QByteArray("x") << -1 << -1;
       
   947     QTest::newRow( "null" ) << QByteArray() << QByteArray("x") << -1 << -1;
       
   948     QTest::newRow( "null-in-null") << QByteArray() << QByteArray() << -1 << 0;
       
   949     QTest::newRow( "empty-in-null") << QByteArray() << QByteArray("") << -1 << 0;
       
   950     QTest::newRow( "null-in-empty") << QByteArray("") << QByteArray() << -1 << 0;
       
   951     QTest::newRow( "empty-in-empty") << QByteArray("") << QByteArray("") << -1 << 0;
       
   952 }
       
   953 
       
   954 void tst_QByteArray::lastIndexOf()
       
   955 {
       
   956     QFETCH( QByteArray, haystack );
       
   957     QFETCH( QByteArray, needle );
       
   958     QFETCH( int, startpos );
       
   959     QFETCH( int, expected );
       
   960 
       
   961     bool hasNull = needle.contains('\0');
       
   962 
       
   963     QCOMPARE( haystack.lastIndexOf(needle, startpos), expected );
       
   964     if (!hasNull)
       
   965         QCOMPARE( haystack.lastIndexOf(needle.data(), startpos), expected );
       
   966     if (needle.size() == 1)
       
   967 	QCOMPARE( haystack.lastIndexOf(needle.at(0), startpos), expected );
       
   968 
       
   969     if (startpos == -1) {
       
   970 	QCOMPARE( haystack.lastIndexOf(needle), expected );
       
   971         if (!hasNull)
       
   972             QCOMPARE( haystack.lastIndexOf(needle.data()), expected );
       
   973 	if (needle.size() == 1)
       
   974 	    QCOMPARE( haystack.lastIndexOf(needle.at(0)), expected );
       
   975     }
       
   976 }
       
   977 
       
   978 void tst_QByteArray::number()
       
   979 {
       
   980     QCOMPARE(QString(QByteArray::number((quint64) 0)),
       
   981 	    QString(QByteArray("0")));
       
   982     QCOMPARE(QString(QByteArray::number(Q_UINT64_C(0xFFFFFFFFFFFFFFFF))),
       
   983 	    QString(QByteArray("18446744073709551615")));
       
   984     QCOMPARE(QString(QByteArray::number(Q_INT64_C(0xFFFFFFFFFFFFFFFF))),
       
   985 	    QString(QByteArray("-1")));
       
   986     QCOMPARE(QString(QByteArray::number(qint64(0))),
       
   987 	    QString(QByteArray("0")));
       
   988     QCOMPARE(QString(QByteArray::number(Q_INT64_C(0x7FFFFFFFFFFFFFFF))),
       
   989 	    QString(QByteArray("9223372036854775807")));
       
   990     QCOMPARE(QString(QByteArray::number(Q_INT64_C(0x8000000000000000))),
       
   991 	    QString(QByteArray("-9223372036854775808")));
       
   992 }
       
   993 
       
   994 // defined later
       
   995 extern const char globalChar;
       
   996 
       
   997 void tst_QByteArray::toInt_data()
       
   998 {
       
   999     QTest::addColumn<QByteArray>("string");
       
  1000     QTest::addColumn<int>("base");
       
  1001     QTest::addColumn<int>("expectednumber");
       
  1002     QTest::addColumn<bool>("expectedok");
       
  1003 
       
  1004     QTest::newRow("base 10") << QByteArray("100") << 10 << int(100) << true;
       
  1005     QTest::newRow("base 16") << QByteArray("100") << 16 << int(256) << true;
       
  1006     QTest::newRow("base 16") << QByteArray("0400") << 16 << int(1024) << true;
       
  1007     QTest::newRow("base 2") << QByteArray("1111") << 2 << int(15) << true;
       
  1008     QTest::newRow("base 8") << QByteArray("100") << 8 << int(64) << true;
       
  1009     QTest::newRow("base 0") << QByteArray("0x10") << 0 << int(16) << true;
       
  1010     QTest::newRow("base 0") << QByteArray("10") << 0 << int(10) << true;
       
  1011     QTest::newRow("base 0") << QByteArray("010") << 0 << int(8) << true;
       
  1012     QTest::newRow("empty") << QByteArray() << 0 << int(0) << false;
       
  1013 
       
  1014     // using fromRawData
       
  1015     QTest::newRow("raw1") << QByteArray::fromRawData("1", 1) << 10 << 1 << true;
       
  1016     QTest::newRow("raw2") << QByteArray::fromRawData("1foo", 1) << 10 << 1 << true;
       
  1017     QTest::newRow("raw3") << QByteArray::fromRawData("12", 1) << 10 << 1 << true;
       
  1018     QTest::newRow("raw4") << QByteArray::fromRawData("123456789", 1) << 10 << 1 << true;
       
  1019     QTest::newRow("raw4") << QByteArray::fromRawData("123456789", 2) << 10 << 12 << true;
       
  1020 
       
  1021     QTest::newRow("raw-static") << QByteArray::fromRawData(&globalChar, 1) << 10 << 1 << true;
       
  1022 }
       
  1023 
       
  1024 void tst_QByteArray::toInt()
       
  1025 {
       
  1026     QFETCH( QByteArray, string );
       
  1027     QFETCH( int, base );
       
  1028     QFETCH( int, expectednumber );
       
  1029     QFETCH( bool, expectedok );
       
  1030 
       
  1031     bool ok;
       
  1032     int number = string.toInt(&ok, base);
       
  1033 
       
  1034     QCOMPARE( ok, expectedok );
       
  1035     QCOMPARE( number, expectednumber );
       
  1036 }
       
  1037 
       
  1038 Q_DECLARE_METATYPE(qulonglong)
       
  1039 void tst_QByteArray::toULong_data()
       
  1040 {
       
  1041     QTest::addColumn<QByteArray>("str");
       
  1042     QTest::addColumn<int>("base");
       
  1043     QTest::addColumn<ulong>("result");
       
  1044     QTest::addColumn<bool>("ok");
       
  1045 
       
  1046     ulong LongMaxPlusOne = (ulong)LONG_MAX + 1;
       
  1047     QTest::newRow("LONG_MAX+1") << QString::number(LongMaxPlusOne).toLatin1() << 10 << LongMaxPlusOne << true;
       
  1048     QTest::newRow("default") << QByteArray() << 10 << 0UL << false;
       
  1049     QTest::newRow("empty") << QByteArray("") << 10 << 0UL << false;
       
  1050     QTest::newRow("ulong1") << QByteArray("3234567890") << 10 << 3234567890UL << true;
       
  1051     QTest::newRow("ulong2") << QByteArray("fFFfFfFf") << 16 << 0xFFFFFFFFUL << true;
       
  1052 }
       
  1053 
       
  1054 void tst_QByteArray::toULong()
       
  1055 {
       
  1056     QFETCH(QByteArray, str);
       
  1057     QFETCH(int, base);
       
  1058     QFETCH(ulong, result);
       
  1059     QFETCH(bool, ok);
       
  1060 
       
  1061     bool b;
       
  1062     QCOMPARE(str.toULong(0, base), result);
       
  1063     QCOMPARE(str.toULong(&b, base), result);
       
  1064     QCOMPARE(b, ok);
       
  1065 }
       
  1066 
       
  1067 void tst_QByteArray::toULongLong_data()
       
  1068 {
       
  1069     QTest::addColumn<QByteArray>("str");
       
  1070     QTest::addColumn<int>("base");
       
  1071     QTest::addColumn<qulonglong>("result");
       
  1072     QTest::addColumn<bool>("ok");
       
  1073 
       
  1074     QTest::newRow("default") << QByteArray() << 10 << (qulonglong)0 << false;
       
  1075     QTest::newRow("out of base bound") << QByteArray("c") << 10 << (qulonglong)0 << false;
       
  1076 
       
  1077 }
       
  1078 
       
  1079 void tst_QByteArray::toULongLong()
       
  1080 {
       
  1081     QFETCH(QByteArray, str);
       
  1082     QFETCH(int, base);
       
  1083     QFETCH(qulonglong, result);
       
  1084     QFETCH(bool, ok);
       
  1085 
       
  1086     bool b;
       
  1087     QCOMPARE(str.toULongLong(0, base), result);
       
  1088     QCOMPARE(str.toULongLong(&b, base), result);
       
  1089     QCOMPARE(b, ok);
       
  1090 }
       
  1091 
       
  1092 // global function defined in qbytearray.cpp
       
  1093 void tst_QByteArray::qAllocMore()
       
  1094 {
       
  1095     static const int t[] = {
       
  1096         INT_MIN, INT_MIN + 1, -1234567, -66000, -1025,
       
  1097         -3, -1, 0, +1, +3, +1025, +66000, +1234567, INT_MAX - 1, INT_MAX,
       
  1098         INT_MAX/3
       
  1099     };
       
  1100     static const int N = sizeof(t)/sizeof(t[0]);
       
  1101 
       
  1102     // make sure qAllocMore() doesn't loop infinitely on any input
       
  1103     for (int i = 0; i < N; ++i) {
       
  1104         for (int j = 0; j < N; ++j) {
       
  1105             ::qAllocMore(t[i], t[j]);
       
  1106         }
       
  1107     }
       
  1108 }
       
  1109 
       
  1110 void tst_QByteArray::resizeAfterFromRawData()
       
  1111 {
       
  1112     QByteArray buffer("hello world");
       
  1113 
       
  1114     QByteArray array = QByteArray::fromRawData(buffer.constData(), buffer.size());
       
  1115     QVERIFY(array.constData() == buffer.constData());
       
  1116     array.resize(5);
       
  1117     QVERIFY(array.constData() == buffer.constData());
       
  1118 }
       
  1119 
       
  1120 void tst_QByteArray::appendAfterFromRawData()
       
  1121 {
       
  1122     QByteArray arr;
       
  1123     {
       
  1124         char data[] = "X";
       
  1125         arr += QByteArray::fromRawData(data, sizeof(data));
       
  1126         data[0] = 'Y';
       
  1127     }
       
  1128     QVERIFY(arr.at(0) == 'X');
       
  1129 }
       
  1130 
       
  1131 void tst_QByteArray::toFromHex_data()
       
  1132 {
       
  1133     QTest::addColumn<QByteArray>("str");
       
  1134     QTest::addColumn<QByteArray>("hex");
       
  1135     QTest::addColumn<QByteArray>("hex_alt1");
       
  1136 
       
  1137     QTest::newRow("Qt is great!")
       
  1138         << QByteArray("Qt is great!")
       
  1139         << QByteArray("517420697320677265617421")
       
  1140         << QByteArray("51 74 20 69 73 20 67 72 65 61 74 21");
       
  1141 
       
  1142     QTest::newRow("Qt is so great!")
       
  1143         << QByteArray("Qt is so great!")
       
  1144         << QByteArray("517420697320736f20677265617421")
       
  1145         << QByteArray("51 74 20 69 73 20 73 6f 20 67 72 65 61 74 21");
       
  1146 
       
  1147     QTest::newRow("default-constructed")
       
  1148         << QByteArray()
       
  1149         << QByteArray()
       
  1150         << QByteArray();
       
  1151 
       
  1152     QTest::newRow("empty")
       
  1153         << QByteArray("")
       
  1154         << QByteArray("")
       
  1155         << QByteArray("");
       
  1156 
       
  1157     QTest::newRow("array-of-null")
       
  1158         << QByteArray("\0", 1)
       
  1159         << QByteArray("00")
       
  1160         << QByteArray("0");
       
  1161 
       
  1162     QTest::newRow("no-leading-zero")
       
  1163         << QByteArray("\xf")
       
  1164         << QByteArray("0f")
       
  1165         << QByteArray("f");
       
  1166 
       
  1167     QTest::newRow("single-byte")
       
  1168         << QByteArray("\xaf")
       
  1169         << QByteArray("af")
       
  1170         << QByteArray("xaf");
       
  1171 
       
  1172     QTest::newRow("no-leading-zero")
       
  1173         << QByteArray("\xd\xde\xad\xc0\xde")
       
  1174         << QByteArray("0ddeadc0de")
       
  1175         << QByteArray("ddeadc0de");
       
  1176 
       
  1177     QTest::newRow("garbage")
       
  1178         << QByteArray("\xC\xde\xeC\xea\xee\xDe\xee\xee")
       
  1179         << QByteArray("0cdeeceaeedeeeee")
       
  1180         << QByteArray("Code less. Create more. Deploy everywhere.");
       
  1181 
       
  1182     QTest::newRow("under-defined-1")
       
  1183         << QByteArray("\x1\x23")
       
  1184         << QByteArray("0123")
       
  1185         << QByteArray("x123");
       
  1186 
       
  1187     QTest::newRow("under-defined-2")
       
  1188         << QByteArray("\x12\x34")
       
  1189         << QByteArray("1234")
       
  1190         << QByteArray("x1234");
       
  1191 }
       
  1192 
       
  1193 void tst_QByteArray::toFromHex()
       
  1194 {
       
  1195     QFETCH(QByteArray, str);
       
  1196     QFETCH(QByteArray, hex);
       
  1197     QFETCH(QByteArray, hex_alt1);
       
  1198 
       
  1199     {
       
  1200         const QByteArray th = str.toHex();
       
  1201         QCOMPARE(th.size(), hex.size());
       
  1202         QCOMPARE(th, hex);
       
  1203     }
       
  1204 
       
  1205     {
       
  1206         const QByteArray fh = QByteArray::fromHex(hex);
       
  1207         QCOMPARE(fh.size(), str.size());
       
  1208         QCOMPARE(fh, str);
       
  1209     }
       
  1210 
       
  1211     QCOMPARE(QByteArray::fromHex(hex_alt1), str);
       
  1212 }
       
  1213 
       
  1214 void tst_QByteArray::toFromPercentEncoding()
       
  1215 {
       
  1216     QByteArray arr("Qt is great!");
       
  1217 /*
       
  1218     QByteArray data = arr.toPercentEncoding();
       
  1219     QCOMPARE(QString(data), QString("Qt%20is%20great%21"));
       
  1220     QCOMPARE(QByteArray::fromPercentEncoding(data), arr);
       
  1221 */
       
  1222     QByteArray data = arr.toPercentEncoding("! ", "Qt");
       
  1223     QCOMPARE(QString(data), QString("%51%74 is grea%74!"));
       
  1224     QCOMPARE(QByteArray::fromPercentEncoding(data), arr);
       
  1225 
       
  1226     data = arr.toPercentEncoding(QByteArray(), "abcdefghijklmnopqrstuvwxyz", 'Q');
       
  1227     QCOMPARE(QString(data), QString("Q51Q74Q20Q69Q73Q20Q67Q72Q65Q61Q74Q21"));
       
  1228     QCOMPARE(QByteArray::fromPercentEncoding(data, 'Q'), arr);
       
  1229 
       
  1230     // verify that to/from percent encoding preserves nullity
       
  1231     arr = "";
       
  1232     QVERIFY(arr.isEmpty());
       
  1233     QVERIFY(!arr.isNull());
       
  1234     QVERIFY(arr.toPercentEncoding().isEmpty());
       
  1235     QVERIFY(!arr.toPercentEncoding().isNull());
       
  1236     QVERIFY(QByteArray::fromPercentEncoding("").isEmpty());
       
  1237     QVERIFY(!QByteArray::fromPercentEncoding("").isNull());
       
  1238 
       
  1239     arr = QByteArray();
       
  1240     QVERIFY(arr.isEmpty());
       
  1241     QVERIFY(arr.isNull());
       
  1242     QVERIFY(arr.toPercentEncoding().isEmpty());
       
  1243     QVERIFY(arr.toPercentEncoding().isNull());
       
  1244     QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isEmpty());
       
  1245     QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isNull());
       
  1246 }
       
  1247 
       
  1248 void tst_QByteArray::compare_data()
       
  1249 {
       
  1250     QTest::addColumn<QByteArray>("str1");
       
  1251     QTest::addColumn<QByteArray>("str2");
       
  1252     QTest::addColumn<int>("result");
       
  1253 
       
  1254     QTest::newRow("null")      << QByteArray() << QByteArray() << 0;
       
  1255     QTest::newRow("null-empty")<< QByteArray() << QByteArray("") << 0;
       
  1256     QTest::newRow("empty-null")<< QByteArray("") << QByteArray() << 0;
       
  1257     QTest::newRow("null-full") << QByteArray() << QByteArray("abc") << -1;
       
  1258     QTest::newRow("full-null") << QByteArray("abc") << QByteArray() << +1;
       
  1259     QTest::newRow("empty-full")<< QByteArray("") << QByteArray("abc") << -1;
       
  1260     QTest::newRow("full-empty")<< QByteArray("abc") << QByteArray("") << +1;
       
  1261     QTest::newRow("rawempty-full") << QByteArray::fromRawData("abc", 0) << QByteArray("abc") << -1;
       
  1262     QTest::newRow("full-rawempty") << QByteArray("abc") << QByteArray::fromRawData("abc", 0) << +1;
       
  1263 
       
  1264     QTest::newRow("equal   1") << QByteArray("abc") << QByteArray("abc") << 0;
       
  1265     QTest::newRow("equal   2") << QByteArray::fromRawData("abc", 3) << QByteArray("abc") << 0;
       
  1266     QTest::newRow("equal   3") << QByteArray::fromRawData("abcdef", 3) << QByteArray("abc") << 0;
       
  1267     QTest::newRow("equal   4") << QByteArray("abc") << QByteArray::fromRawData("abc", 3) << 0;
       
  1268     QTest::newRow("equal   5") << QByteArray("abc") << QByteArray::fromRawData("abcdef", 3) << 0;
       
  1269     QTest::newRow("equal   6") << QByteArray("a\0bc", 4) << QByteArray("a\0bc", 4) << 0;
       
  1270     QTest::newRow("equal   7") << QByteArray::fromRawData("a\0bcdef", 4) << QByteArray("a\0bc", 4) << 0;
       
  1271     QTest::newRow("equal   8") << QByteArray("a\0bc", 4) << QByteArray::fromRawData("a\0bcdef", 4) << 0;
       
  1272 
       
  1273     QTest::newRow("less    1") << QByteArray("000") << QByteArray("abc") << -1;
       
  1274     QTest::newRow("less    2") << QByteArray::fromRawData("00", 3) << QByteArray("abc") << -1;
       
  1275     QTest::newRow("less    3") << QByteArray("000") << QByteArray::fromRawData("abc", 3) << -1;
       
  1276     QTest::newRow("less    4") << QByteArray("abc", 3) << QByteArray("abc", 4) << -1;
       
  1277     QTest::newRow("less    5") << QByteArray::fromRawData("abc\0", 3) << QByteArray("abc\0", 4) << -1;
       
  1278     QTest::newRow("less    6") << QByteArray("a\0bc", 4) << QByteArray("a\0bd", 4) << -1;
       
  1279 
       
  1280     QTest::newRow("greater 1") << QByteArray("abc") << QByteArray("000") << +1;
       
  1281     QTest::newRow("greater 2") << QByteArray("abc") << QByteArray::fromRawData("00", 3) << +1;
       
  1282     QTest::newRow("greater 3") << QByteArray("abcd") << QByteArray::fromRawData("abcd", 3) << +1;
       
  1283     QTest::newRow("greater 4") << QByteArray("a\0bc", 4) << QByteArray("a\0bb", 4) << +1;
       
  1284 }
       
  1285 
       
  1286 void tst_QByteArray::compare()
       
  1287 {
       
  1288     QFETCH(QByteArray, str1);
       
  1289     QFETCH(QByteArray, str2);
       
  1290     QFETCH(int, result);
       
  1291 
       
  1292     const bool isEqual   = result == 0;
       
  1293     const bool isLess    = result < 0;
       
  1294     const bool isGreater = result > 0;
       
  1295 
       
  1296     // basic tests:
       
  1297     QCOMPARE(str1 == str2, isEqual);
       
  1298     QCOMPARE(str1 < str2, isLess);
       
  1299     QCOMPARE(str1 > str2, isGreater);
       
  1300 
       
  1301     // composed tests:
       
  1302     QCOMPARE(str1 <= str2, isLess || isEqual);
       
  1303     QCOMPARE(str1 >= str2, isGreater || isEqual);
       
  1304     QCOMPARE(str1 != str2, !isEqual);
       
  1305 
       
  1306     // inverted tests:
       
  1307     QCOMPARE(str2 == str1, isEqual);
       
  1308     QCOMPARE(str2 < str1, isGreater);
       
  1309     QCOMPARE(str2 > str1, isLess);
       
  1310 
       
  1311     // composed, inverted tests:
       
  1312     QCOMPARE(str2 <= str1, isGreater || isEqual);
       
  1313     QCOMPARE(str2 >= str1, isLess || isEqual);
       
  1314     QCOMPARE(str2 != str1, !isEqual);
       
  1315 }
       
  1316 
       
  1317 void tst_QByteArray::compareCharStar_data()
       
  1318 {
       
  1319     QTest::addColumn<QByteArray>("str1");
       
  1320     QTest::addColumn<QString>("string2");
       
  1321     QTest::addColumn<int>("result");
       
  1322 
       
  1323     QTest::newRow("null-null") << QByteArray() << QString() << 0;
       
  1324     QTest::newRow("null-empty") << QByteArray() << "" << 0;
       
  1325     QTest::newRow("null-full") << QByteArray() << "abc" << -1;
       
  1326     QTest::newRow("empty-null") << QByteArray("") << QString() << 0;
       
  1327     QTest::newRow("empty-empty") << QByteArray("") << "" << 0;
       
  1328     QTest::newRow("empty-full") << QByteArray("") << "abc" << -1;
       
  1329     QTest::newRow("raw-null") << QByteArray::fromRawData("abc", 0) << QString() << 0;
       
  1330     QTest::newRow("raw-empty") << QByteArray::fromRawData("abc", 0) << QString("") << 0;
       
  1331     QTest::newRow("raw-full") << QByteArray::fromRawData("abc", 0) << "abc" << -1;
       
  1332 
       
  1333     QTest::newRow("full-null") << QByteArray("abc") << QString() << +1;
       
  1334     QTest::newRow("full-empty") << QByteArray("abc") << "" << +1;
       
  1335 
       
  1336     QTest::newRow("equal1") << QByteArray("abc") << "abc" << 0;
       
  1337     QTest::newRow("equal2") << QByteArray("abcd", 3) << "abc" << 0;
       
  1338     QTest::newRow("equal3") << QByteArray::fromRawData("abcd", 3) << "abc" << 0;
       
  1339 
       
  1340     QTest::newRow("less1") << QByteArray("ab") << "abc" << -1;
       
  1341     QTest::newRow("less2") << QByteArray("abb") << "abc" << -1;
       
  1342     QTest::newRow("less3") << QByteArray::fromRawData("abc", 2) << "abc" << -1;
       
  1343     QTest::newRow("less4") << QByteArray("", 1) << "abc" << -1;
       
  1344     QTest::newRow("less5") << QByteArray::fromRawData("", 1) << "abc" << -1;
       
  1345     QTest::newRow("less6") << QByteArray("a\0bc", 4) << "a.bc" << -1;
       
  1346 
       
  1347     QTest::newRow("greater1") << QByteArray("ac") << "abc" << +1;
       
  1348     QTest::newRow("greater2") << QByteArray("abd") << "abc" << +1;
       
  1349     QTest::newRow("greater3") << QByteArray("abcd") << "abc" << +1;
       
  1350     QTest::newRow("greater4") << QByteArray::fromRawData("abcd", 4) << "abc" << +1;
       
  1351 }
       
  1352 
       
  1353 void tst_QByteArray::compareCharStar()
       
  1354 {
       
  1355     QFETCH(QByteArray, str1);
       
  1356     QFETCH(QString, string2);
       
  1357     QFETCH(int, result);
       
  1358 
       
  1359     const bool isEqual   = result == 0;
       
  1360     const bool isLess    = result < 0;
       
  1361     const bool isGreater = result > 0;
       
  1362     QByteArray qba = string2.toLatin1();
       
  1363     const char *str2 = qba.constData();
       
  1364     if (string2.isNull())
       
  1365         str2 = 0;
       
  1366 
       
  1367     // basic tests:
       
  1368     QCOMPARE(str1 == str2, isEqual);
       
  1369     QCOMPARE(str1 < str2, isLess);
       
  1370     QCOMPARE(str1 > str2, isGreater);
       
  1371 
       
  1372     // composed tests:
       
  1373     QCOMPARE(str1 <= str2, isLess || isEqual);
       
  1374     QCOMPARE(str1 >= str2, isGreater || isEqual);
       
  1375     QCOMPARE(str1 != str2, !isEqual);
       
  1376 
       
  1377     // inverted tests:
       
  1378     QCOMPARE(str2 == str1, isEqual);
       
  1379     QCOMPARE(str2 < str1, isGreater);
       
  1380     QCOMPARE(str2 > str1, isLess);
       
  1381 
       
  1382     // composed, inverted tests:
       
  1383     QCOMPARE(str2 <= str1, isGreater || isEqual);
       
  1384     QCOMPARE(str2 >= str1, isLess || isEqual);
       
  1385     QCOMPARE(str2 != str1, !isEqual);
       
  1386 }
       
  1387 
       
  1388 void tst_QByteArray::repeatedSignature() const
       
  1389 {
       
  1390     /* repated() should be a const member. */
       
  1391     const QByteArray string;
       
  1392     string.repeated(3);
       
  1393 }
       
  1394 
       
  1395 void tst_QByteArray::repeated() const
       
  1396 {
       
  1397     QFETCH(QByteArray, string);
       
  1398     QFETCH(QByteArray, expected);
       
  1399     QFETCH(int, count);
       
  1400 
       
  1401     QCOMPARE(string.repeated(count), expected);
       
  1402 }
       
  1403 
       
  1404 void tst_QByteArray::repeated_data() const
       
  1405 {
       
  1406     QTest::addColumn<QByteArray>("string" );
       
  1407     QTest::addColumn<QByteArray>("expected" );
       
  1408     QTest::addColumn<int>("count" );
       
  1409 
       
  1410     /* Empty strings. */
       
  1411     QTest::newRow("")
       
  1412         << QByteArray()
       
  1413         << QByteArray()
       
  1414         << 0;
       
  1415 
       
  1416     QTest::newRow("")
       
  1417         << QByteArray()
       
  1418         << QByteArray()
       
  1419         << -1004;
       
  1420 
       
  1421     QTest::newRow("")
       
  1422         << QByteArray()
       
  1423         << QByteArray()
       
  1424         << 1;
       
  1425 
       
  1426     QTest::newRow("")
       
  1427         << QByteArray()
       
  1428         << QByteArray()
       
  1429         << 5;
       
  1430 
       
  1431     /* On simple string. */
       
  1432     QTest::newRow("")
       
  1433         << QByteArray("abc")
       
  1434         << QByteArray()
       
  1435         << -1004;
       
  1436 
       
  1437     QTest::newRow("")
       
  1438         << QByteArray("abc")
       
  1439         << QByteArray()
       
  1440         << -1;
       
  1441 
       
  1442     QTest::newRow("")
       
  1443         << QByteArray("abc")
       
  1444         << QByteArray()
       
  1445         << 0;
       
  1446 
       
  1447     QTest::newRow("")
       
  1448         << QByteArray("abc")
       
  1449         << QByteArray("abc")
       
  1450         << 1;
       
  1451 
       
  1452     QTest::newRow("")
       
  1453         << QByteArray(("abc"))
       
  1454         << QByteArray(("abcabc"))
       
  1455         << 2;
       
  1456 
       
  1457     QTest::newRow("")
       
  1458         << QByteArray(("abc"))
       
  1459         << QByteArray(("abcabcabc"))
       
  1460         << 3;
       
  1461 
       
  1462     QTest::newRow("")
       
  1463         << QByteArray(("abc"))
       
  1464         << QByteArray(("abcabcabcabc"))
       
  1465         << 4;
       
  1466 }
       
  1467 
       
  1468 void tst_QByteArray::byteRefDetaching() const
       
  1469 {
       
  1470     {
       
  1471         QByteArray str = "str";
       
  1472         QByteArray copy;
       
  1473         copy[0] = 'S';
       
  1474 
       
  1475         QCOMPARE(str, QByteArray("str"));
       
  1476     }
       
  1477 
       
  1478     {
       
  1479         char buf[] = { 's', 't', 'r' };
       
  1480         QByteArray str = QByteArray::fromRawData(buf, 3);
       
  1481         str[0] = 'S';
       
  1482 
       
  1483         QCOMPARE(buf[0], char('s'));
       
  1484     }
       
  1485 
       
  1486     {
       
  1487         static const char buf[] = { 's', 't', 'r' };
       
  1488         QByteArray str = QByteArray::fromRawData(buf, 3);
       
  1489 
       
  1490         // this causes a crash in most systems if the detaching doesn't work
       
  1491         str[0] = 'S';
       
  1492 
       
  1493         QCOMPARE(buf[0], char('s'));
       
  1494     }
       
  1495 }
       
  1496 
       
  1497 const char globalChar = '1';
       
  1498 
       
  1499 QTEST_APPLESS_MAIN(tst_QByteArray)
       
  1500 #include "tst_qbytearray.moc"