tests/auto/qtextstream/tst_qtextstream.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
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 /*-*-encoding:latin1-*-*/
       
    44 //#include <iostream>
       
    45 //using namespace std;
       
    46 
       
    47 #include <QtTest/QtTest>
       
    48 
       
    49 #ifdef Q_OS_UNIX
       
    50 #include <locale.h>
       
    51 #endif
       
    52 
       
    53 #include <QBuffer>
       
    54 #include <QByteArray>
       
    55 #include <QCoreApplication>
       
    56 #include <QDebug>
       
    57 #include <QFile>
       
    58 #include <QTcpSocket>
       
    59 #include <QTextStream>
       
    60 #include <QTextCodec>
       
    61 #include <QProcess>
       
    62 
       
    63 #include "../network-settings.h"
       
    64 
       
    65 static const char *TestFileName = "testfile";
       
    66 
       
    67 Q_DECLARE_METATYPE(qlonglong)
       
    68 Q_DECLARE_METATYPE(qulonglong)
       
    69 Q_DECLARE_METATYPE(QList<int>)
       
    70 
       
    71 QT_BEGIN_NAMESPACE
       
    72 template<> struct QMetaTypeId<QIODevice::OpenModeFlag>
       
    73 { enum { Defined = 1 }; static inline int qt_metatype_id() { return QMetaType::Int; } };
       
    74 QT_END_NAMESPACE
       
    75 
       
    76 //TESTED_CLASS=
       
    77 //TESTED_FILES=
       
    78 
       
    79 #ifdef Q_OS_SYMBIAN
       
    80 #define SRCDIR ""
       
    81 #endif
       
    82 
       
    83 class tst_QTextStream : public QObject
       
    84 {
       
    85     Q_OBJECT
       
    86 
       
    87 public:
       
    88     tst_QTextStream();
       
    89     virtual ~tst_QTextStream();
       
    90 
       
    91 public slots:
       
    92     void init();
       
    93     void cleanup();
       
    94 
       
    95 private slots:
       
    96     void getSetCheck();
       
    97     void construction();
       
    98 
       
    99     // lines
       
   100     void readLineFromDevice_data();
       
   101     void readLineFromDevice();
       
   102     void readLineFromString_data();
       
   103     void readLineFromString();
       
   104     void readLineFromTextDevice_data();
       
   105     void readLineFromTextDevice();
       
   106     void readLineUntilNull();
       
   107     void readLineMaxlen_data();
       
   108     void readLineMaxlen();
       
   109     void readLinesFromBufferCRCR();
       
   110 
       
   111     // all
       
   112     void readAllFromDevice_data();
       
   113     void readAllFromDevice();
       
   114     void readAllFromString_data();
       
   115     void readAllFromString();
       
   116     void readLineFromStringThenChangeString();
       
   117 
       
   118     // device tests
       
   119     void setDevice();
       
   120 
       
   121     // char operators
       
   122     void QChar_operators_FromDevice_data();
       
   123     void QChar_operators_FromDevice();
       
   124     void char_operators_FromDevice_data();
       
   125     void char_operators_FromDevice();
       
   126 
       
   127     // natural number read operator
       
   128     void signedShort_read_operator_FromDevice_data();
       
   129     void signedShort_read_operator_FromDevice();
       
   130     void unsignedShort_read_operator_FromDevice_data();
       
   131     void unsignedShort_read_operator_FromDevice();
       
   132     void signedInt_read_operator_FromDevice_data();
       
   133     void signedInt_read_operator_FromDevice();
       
   134     void unsignedInt_read_operator_FromDevice_data();
       
   135     void unsignedInt_read_operator_FromDevice();
       
   136     void qlonglong_read_operator_FromDevice_data();
       
   137     void qlonglong_read_operator_FromDevice();
       
   138     void qulonglong_read_operator_FromDevice_data();
       
   139     void qulonglong_read_operator_FromDevice();
       
   140 
       
   141     // natural number write operator
       
   142     void signedShort_write_operator_ToDevice_data();
       
   143     void signedShort_write_operator_ToDevice();
       
   144     void unsignedShort_write_operator_ToDevice_data();
       
   145     void unsignedShort_write_operator_ToDevice();
       
   146     void signedInt_write_operator_ToDevice_data();
       
   147     void signedInt_write_operator_ToDevice();
       
   148     void unsignedInt_write_operator_ToDevice_data();
       
   149     void unsignedInt_write_operator_ToDevice();
       
   150     void qlonglong_write_operator_ToDevice_data();
       
   151     void qlonglong_write_operator_ToDevice();
       
   152     void qulonglong_write_operator_ToDevice_data();
       
   153     void qulonglong_write_operator_ToDevice();
       
   154 
       
   155     void int_read_with_locale_data();
       
   156     void int_read_with_locale();
       
   157 
       
   158     void int_write_with_locale_data();
       
   159     void int_write_with_locale();
       
   160 
       
   161     // real number read operator
       
   162     void float_read_operator_FromDevice_data();
       
   163     void float_read_operator_FromDevice();
       
   164     void double_read_operator_FromDevice_data();
       
   165     void double_read_operator_FromDevice();
       
   166 
       
   167     // real number write operator
       
   168     void float_write_operator_ToDevice_data();
       
   169     void float_write_operator_ToDevice();
       
   170     void double_write_operator_ToDevice_data();
       
   171     void double_write_operator_ToDevice();
       
   172 
       
   173     void double_write_with_flags_data();
       
   174     void double_write_with_flags();
       
   175 
       
   176     void double_write_with_precision_data();
       
   177     void double_write_with_precision();
       
   178 
       
   179     // text read operators
       
   180     void charPtr_read_operator_FromDevice_data();
       
   181     void charPtr_read_operator_FromDevice();
       
   182     void stringRef_read_operator_FromDevice_data();
       
   183     void stringRef_read_operator_FromDevice();
       
   184     void byteArray_read_operator_FromDevice_data();
       
   185     void byteArray_read_operator_FromDevice();
       
   186 
       
   187     // text write operators
       
   188     void string_write_operator_ToDevice_data();
       
   189     void string_write_operator_ToDevice();
       
   190 
       
   191     // other
       
   192     void skipWhiteSpace_data();
       
   193     void skipWhiteSpace();
       
   194     void lineCount_data();
       
   195     void lineCount();
       
   196     void performance();
       
   197     void hexTest_data();
       
   198     void hexTest();
       
   199     void binTest_data();
       
   200     void binTest();
       
   201     void octTest_data();
       
   202     void octTest();
       
   203     void zeroTermination();
       
   204     void ws_manipulator();
       
   205     void stillOpenWhenAtEnd();
       
   206     void readNewlines_data();
       
   207     void readNewlines();
       
   208     void seek();
       
   209     void pos();
       
   210     void pos2();
       
   211     void readStdin();
       
   212     void readAllFromStdin();
       
   213     void readLineFromStdin();
       
   214     void read();
       
   215     void qbool();
       
   216     void forcePoint();
       
   217     void forceSign();
       
   218     void read0d0d0a();
       
   219     void numeralCase_data();
       
   220     void numeralCase();
       
   221     void nanInf();
       
   222     void utf8IncompleteAtBufferBoundary_data();
       
   223     void utf8IncompleteAtBufferBoundary();
       
   224 
       
   225     // status
       
   226     void status_real_read_data();
       
   227     void status_real_read();
       
   228     void status_integer_read();
       
   229     void status_word_read();
       
   230 
       
   231     // use case tests
       
   232     void useCase1();
       
   233     void useCase2();
       
   234 
       
   235     // manipulators
       
   236     void manipulators_data();
       
   237     void manipulators();
       
   238 
       
   239     // UTF-16 BOM (Byte Order Mark)
       
   240     void generateBOM();
       
   241     void readBomSeekBackReadBomAgain();
       
   242 
       
   243     // old tests
       
   244 #ifdef QT3_SUPPORT
       
   245     void qt3_operator_shiftleft_data();
       
   246     void qt3_operator_shiftleft();
       
   247     void qt3_operator_shiftright_data();
       
   248     void qt3_operator_shiftright();
       
   249     void qt3_operator_shift_QChar_data();
       
   250     void qt3_operator_shift_QChar();
       
   251     void qt3_operator_shift_char_data();
       
   252     void qt3_operator_shift_char();
       
   253     void qt3_operator_shift_short_data();
       
   254     void qt3_operator_shift_short();
       
   255     void qt3_operator_shift_ushort_data();
       
   256     void qt3_operator_shift_ushort();
       
   257     void qt3_operator_shift_int_data();
       
   258     void qt3_operator_shift_int();
       
   259     void qt3_operator_shift_uint_data();
       
   260     void qt3_operator_shift_uint();
       
   261     void qt3_operator_shift_long_data();
       
   262     void qt3_operator_shift_long();
       
   263     void qt3_operator_shift_ulong_data();
       
   264     void qt3_operator_shift_ulong();
       
   265     void qt3_operator_shift_float_data();
       
   266     void qt3_operator_shift_float();
       
   267     void qt3_operator_shift_double_data();
       
   268     void qt3_operator_shift_double();
       
   269     void qt3_operator_shift_QString_data();
       
   270     void qt3_operator_shift_QString();
       
   271     void qt3_operator_shift_QByteArray_data();
       
   272     void qt3_operator_shift_QByteArray();
       
   273 
       
   274     void qt3_writeDataToFileReadAsLines_data();
       
   275     void qt3_writeDataToFileReadAsLines();
       
   276     void qt3_writeDataToQStringReadAsLines_data();
       
   277     void qt3_writeDataToQStringReadAsLines();
       
   278 
       
   279     void qt3_readLineFromString();
       
   280 #endif
       
   281 
       
   282     // task-specific tests
       
   283     void task180679_alignAccountingStyle();
       
   284     void task178772_setCodec();
       
   285 
       
   286 private:
       
   287     void generateLineData(bool for_QString);
       
   288     void generateAllData(bool for_QString);
       
   289     void generateOperatorCharData(bool for_QString);
       
   290     void generateNaturalNumbersData(bool for_QString);
       
   291     void generateRealNumbersData(bool for_QString);
       
   292     void generateStringData(bool for_QString);
       
   293     void generateRealNumbersDataWrite();
       
   294 
       
   295     // Qt 3 privates
       
   296 #ifdef QT3_SUPPORT
       
   297     void qt3_createWriteStream( QTextStream *&os );
       
   298     void qt3_closeWriteStream( QTextStream *os );
       
   299     void qt3_createReadStream( QTextStream *&is );
       
   300     void qt3_closeReadStream( QTextStream *is );
       
   301     void qt3_read_QChar( QTextStream *s );
       
   302     void qt3_write_QChar( QTextStream *s );
       
   303     void qt3_read_char( QTextStream *s );
       
   304     void qt3_write_char( QTextStream *s );
       
   305     void qt3_read_short( QTextStream *s );
       
   306     void qt3_write_short( QTextStream *s );
       
   307     void qt3_read_ushort( QTextStream *s );
       
   308     void qt3_write_ushort( QTextStream *s );
       
   309     void qt3_read_int( QTextStream *s );
       
   310     void qt3_write_int( QTextStream *s );
       
   311     void qt3_read_uint( QTextStream *s );
       
   312     void qt3_write_uint( QTextStream *s );
       
   313     void qt3_read_long( QTextStream *s );
       
   314     void qt3_write_long( QTextStream *s );
       
   315     void qt3_read_ulong( QTextStream *s );
       
   316     void qt3_write_ulong( QTextStream *s );
       
   317     void qt3_read_float( QTextStream *s );
       
   318     void qt3_write_float( QTextStream *s );
       
   319     void qt3_read_double( QTextStream *s );
       
   320     void qt3_write_double( QTextStream *s );
       
   321     void qt3_read_QString( QTextStream *s );
       
   322     void qt3_write_QString( QTextStream *s );
       
   323     void qt3_read_QByteArray( QTextStream *s );
       
   324     void qt3_write_QByteArray( QTextStream *s );
       
   325     void qt3_operatorShift_data( QIODevice::OpenMode );
       
   326     void qt3_do_shiftleft( QTextStream *ts );
       
   327     QTextStream::Encoding qt3_toEncoding( const QString& );
       
   328     QString qt3_decodeString( QByteArray array, const QString& encoding );
       
   329 #endif
       
   330 
       
   331     // Qt 3 data
       
   332     QTextStream *os;
       
   333     QTextStream *is;
       
   334     QTextStream *ts;
       
   335     QFile *inFile;
       
   336     QFile *inResource;
       
   337     QFile *outFile;
       
   338     QByteArray *inArray;
       
   339     QBuffer *inBuffer;
       
   340     QString *inString;
       
   341     bool file_is_empty;
       
   342 };
       
   343 
       
   344 // Testing get/set functions
       
   345 void tst_QTextStream::getSetCheck()
       
   346 {
       
   347     // Initialize codecs
       
   348     int argc = 0;
       
   349     char **argv = 0;
       
   350     QCoreApplication app(argc, argv);
       
   351 
       
   352     QTextStream obj1;
       
   353     // QTextCodec * QTextStream::codec()
       
   354     // void QTextStream::setCodec(QTextCodec *)
       
   355     QTextCodec *var1 = QTextCodec::codecForName("en");
       
   356     obj1.setCodec(var1);
       
   357     QCOMPARE(var1, obj1.codec());
       
   358     obj1.setCodec((QTextCodec *)0);
       
   359     QCOMPARE((QTextCodec *)0, obj1.codec());
       
   360 
       
   361     // bool QTextStream::autoDetectUnicode()
       
   362     // void QTextStream::setAutoDetectUnicode(bool)
       
   363     obj1.setAutoDetectUnicode(false);
       
   364     QCOMPARE(false, obj1.autoDetectUnicode());
       
   365     obj1.setAutoDetectUnicode(true);
       
   366     QCOMPARE(true, obj1.autoDetectUnicode());
       
   367 
       
   368     // bool QTextStream::generateByteOrderMark()
       
   369     // void QTextStream::setGenerateByteOrderMark(bool)
       
   370     obj1.setGenerateByteOrderMark(false);
       
   371     QCOMPARE(false, obj1.generateByteOrderMark());
       
   372     obj1.setGenerateByteOrderMark(true);
       
   373     QCOMPARE(true, obj1.generateByteOrderMark());
       
   374 
       
   375     // QIODevice * QTextStream::device()
       
   376     // void QTextStream::setDevice(QIODevice *)
       
   377     QFile *var4 = new QFile;
       
   378     obj1.setDevice(var4);
       
   379     QCOMPARE(static_cast<QIODevice *>(var4), obj1.device());
       
   380     obj1.setDevice((QIODevice *)0);
       
   381     QCOMPARE((QIODevice *)0, obj1.device());
       
   382     delete var4;
       
   383 
       
   384     // Status QTextStream::status()
       
   385     // void QTextStream::setStatus(Status)
       
   386     obj1.setStatus(QTextStream::Status(QTextStream::Ok));
       
   387     QCOMPARE(QTextStream::Status(QTextStream::Ok), obj1.status());
       
   388     obj1.setStatus(QTextStream::Status(QTextStream::ReadPastEnd));
       
   389     QCOMPARE(QTextStream::Status(QTextStream::ReadPastEnd), obj1.status());
       
   390     obj1.resetStatus();
       
   391     obj1.setStatus(QTextStream::Status(QTextStream::ReadCorruptData));
       
   392     QCOMPARE(QTextStream::Status(QTextStream::ReadCorruptData), obj1.status());
       
   393 
       
   394     // FieldAlignment QTextStream::fieldAlignment()
       
   395     // void QTextStream::setFieldAlignment(FieldAlignment)
       
   396     obj1.setFieldAlignment(QTextStream::FieldAlignment(QTextStream::AlignLeft));
       
   397     QCOMPARE(QTextStream::FieldAlignment(QTextStream::AlignLeft), obj1.fieldAlignment());
       
   398     obj1.setFieldAlignment(QTextStream::FieldAlignment(QTextStream::AlignRight));
       
   399     QCOMPARE(QTextStream::FieldAlignment(QTextStream::AlignRight), obj1.fieldAlignment());
       
   400     obj1.setFieldAlignment(QTextStream::FieldAlignment(QTextStream::AlignCenter));
       
   401     QCOMPARE(QTextStream::FieldAlignment(QTextStream::AlignCenter), obj1.fieldAlignment());
       
   402     obj1.setFieldAlignment(QTextStream::FieldAlignment(QTextStream::AlignAccountingStyle));
       
   403     QCOMPARE(QTextStream::FieldAlignment(QTextStream::AlignAccountingStyle), obj1.fieldAlignment());
       
   404 
       
   405     // QChar QTextStream::padChar()
       
   406     // void QTextStream::setPadChar(QChar)
       
   407     QChar var7 = 'Q';
       
   408     obj1.setPadChar(var7);
       
   409     QCOMPARE(var7, obj1.padChar());
       
   410     obj1.setPadChar(QChar());
       
   411     QCOMPARE(QChar(), obj1.padChar());
       
   412 
       
   413     // int QTextStream::fieldWidth()
       
   414     // void QTextStream::setFieldWidth(int)
       
   415     obj1.setFieldWidth(0);
       
   416     QCOMPARE(0, obj1.fieldWidth());
       
   417     obj1.setFieldWidth(INT_MIN);
       
   418     QCOMPARE(INT_MIN, obj1.fieldWidth());
       
   419     obj1.setFieldWidth(INT_MAX);
       
   420     QCOMPARE(INT_MAX, obj1.fieldWidth());
       
   421 
       
   422     // NumberFlags QTextStream::numberFlags()
       
   423     // void QTextStream::setNumberFlags(NumberFlags)
       
   424     obj1.setNumberFlags(QTextStream::NumberFlags(QTextStream::ShowBase));
       
   425     QCOMPARE(QTextStream::NumberFlags(QTextStream::ShowBase), obj1.numberFlags());
       
   426     obj1.setNumberFlags(QTextStream::NumberFlags(QTextStream::ForcePoint));
       
   427     QCOMPARE(QTextStream::NumberFlags(QTextStream::ForcePoint), obj1.numberFlags());
       
   428     obj1.setNumberFlags(QTextStream::NumberFlags(QTextStream::ForceSign));
       
   429     QCOMPARE(QTextStream::NumberFlags(QTextStream::ForceSign), obj1.numberFlags());
       
   430     obj1.setNumberFlags(QTextStream::NumberFlags(QTextStream::UppercaseBase));
       
   431     QCOMPARE(QTextStream::NumberFlags(QTextStream::UppercaseBase), obj1.numberFlags());
       
   432     obj1.setNumberFlags(QTextStream::NumberFlags(QTextStream::UppercaseDigits));
       
   433     QCOMPARE(QTextStream::NumberFlags(QTextStream::UppercaseDigits), obj1.numberFlags());
       
   434 
       
   435     // int QTextStream::integerBase()
       
   436     // void QTextStream::setIntegerBase(int)
       
   437     obj1.setIntegerBase(0);
       
   438     QCOMPARE(0, obj1.integerBase());
       
   439     obj1.setIntegerBase(INT_MIN);
       
   440     QCOMPARE(INT_MIN, obj1.integerBase());
       
   441     obj1.setIntegerBase(INT_MAX);
       
   442     QCOMPARE(INT_MAX, obj1.integerBase());
       
   443 
       
   444     // RealNumberNotation QTextStream::realNumberNotation()
       
   445     // void QTextStream::setRealNumberNotation(RealNumberNotation)
       
   446     obj1.setRealNumberNotation(QTextStream::RealNumberNotation(QTextStream::SmartNotation));
       
   447     QCOMPARE(QTextStream::RealNumberNotation(QTextStream::SmartNotation), obj1.realNumberNotation());
       
   448     obj1.setRealNumberNotation(QTextStream::RealNumberNotation(QTextStream::FixedNotation));
       
   449     QCOMPARE(QTextStream::RealNumberNotation(QTextStream::FixedNotation), obj1.realNumberNotation());
       
   450     obj1.setRealNumberNotation(QTextStream::RealNumberNotation(QTextStream::ScientificNotation));
       
   451     QCOMPARE(QTextStream::RealNumberNotation(QTextStream::ScientificNotation), obj1.realNumberNotation());
       
   452 
       
   453     // int QTextStream::realNumberPrecision()
       
   454     // void QTextStream::setRealNumberPrecision(int)
       
   455     obj1.setRealNumberPrecision(0);
       
   456     QCOMPARE(0, obj1.realNumberPrecision());
       
   457     obj1.setRealNumberPrecision(INT_MIN);
       
   458     QCOMPARE(6, obj1.realNumberPrecision()); // Setting a negative precision reverts it to the default value (6).
       
   459     obj1.setRealNumberPrecision(INT_MAX);
       
   460     QCOMPARE(INT_MAX, obj1.realNumberPrecision());
       
   461 }
       
   462 
       
   463 tst_QTextStream::tst_QTextStream()
       
   464 {
       
   465     // Initialize Qt 3 data
       
   466     ts = 0;
       
   467     os = 0;
       
   468     is = 0;
       
   469     outFile = 0;
       
   470     inFile = 0;
       
   471     inResource = 0;
       
   472     inArray = 0;
       
   473     inBuffer = 0;
       
   474     inString = 0;
       
   475     file_is_empty = FALSE;
       
   476 
       
   477     Q_SET_DEFAULT_IAP
       
   478 }
       
   479 
       
   480 tst_QTextStream::~tst_QTextStream()
       
   481 {
       
   482 }
       
   483 
       
   484 void tst_QTextStream::init()
       
   485 {
       
   486     // Initialize Qt 3 data
       
   487     ts = 0;
       
   488     os = 0;
       
   489     is = 0;
       
   490     inFile = 0;
       
   491     inResource = 0;
       
   492     outFile = 0;
       
   493     inArray = 0;
       
   494     inBuffer = 0;
       
   495     inString = 0;
       
   496     file_is_empty = FALSE;
       
   497 }
       
   498 
       
   499 void tst_QTextStream::cleanup()
       
   500 {
       
   501     // Clean up Qt 3 data
       
   502     delete ts;
       
   503     ts = 0;
       
   504     delete os;
       
   505     os = 0;
       
   506     delete is;
       
   507     is = 0;
       
   508     delete inResource;
       
   509     inResource = 0;
       
   510     delete inFile;
       
   511     inFile = 0;
       
   512     delete outFile;
       
   513     outFile = 0;
       
   514     delete inArray;
       
   515     inArray = 0;
       
   516     delete inBuffer;
       
   517     inBuffer = 0;
       
   518     delete inString;
       
   519     inString = 0;
       
   520 
       
   521     QCoreApplication::instance()->processEvents();
       
   522 }
       
   523 
       
   524 // ------------------------------------------------------------------------------
       
   525 void tst_QTextStream::construction()
       
   526 {
       
   527     QTextStream stream;
       
   528     QCOMPARE(stream.codec(), QTextCodec::codecForLocale());
       
   529     QCOMPARE(stream.device(), static_cast<QIODevice *>(0));
       
   530     QCOMPARE(stream.string(), static_cast<QString *>(0));
       
   531 
       
   532     QTest::ignoreMessage(QtWarningMsg, "QTextStream: No device");
       
   533     QVERIFY(stream.atEnd());
       
   534 
       
   535     QTest::ignoreMessage(QtWarningMsg, "QTextStream: No device");
       
   536     QCOMPARE(stream.readAll(), QString());
       
   537 
       
   538 }
       
   539 
       
   540 void tst_QTextStream::generateLineData(bool for_QString)
       
   541 {
       
   542     QTest::addColumn<QByteArray>("data");
       
   543     QTest::addColumn<QStringList>("lines");
       
   544 
       
   545     // latin-1
       
   546     QTest::newRow("emptyer") << QByteArray() << QStringList();
       
   547     QTest::newRow("lf") << QByteArray("\n") << (QStringList() << "");
       
   548     QTest::newRow("crlf") << QByteArray("\r\n") << (QStringList() << "");
       
   549     QTest::newRow("oneline/nothing") << QByteArray("ole") << (QStringList() << "ole");
       
   550     QTest::newRow("oneline/lf") << QByteArray("ole\n") << (QStringList() << "ole");
       
   551     QTest::newRow("oneline/crlf") << QByteArray("ole\r\n") << (QStringList() << "ole");
       
   552     QTest::newRow("twolines/lf/lf") << QByteArray("ole\ndole\n") << (QStringList() << "ole" << "dole");
       
   553     QTest::newRow("twolines/crlf/crlf") << QByteArray("ole\r\ndole\r\n") << (QStringList() << "ole" << "dole");
       
   554     QTest::newRow("twolines/lf/crlf") << QByteArray("ole\ndole\r\n") << (QStringList() << "ole" << "dole");
       
   555     QTest::newRow("twolines/lf/nothing") << QByteArray("ole\ndole") << (QStringList() << "ole" << "dole");
       
   556     QTest::newRow("twolines/crlf/nothing") << QByteArray("ole\r\ndole") << (QStringList() << "ole" << "dole");
       
   557     QTest::newRow("threelines/lf/lf/lf") << QByteArray("ole\ndole\ndoffen\n") << (QStringList() << "ole" << "dole" << "doffen");
       
   558     QTest::newRow("threelines/crlf/crlf/crlf") << QByteArray("ole\r\ndole\r\ndoffen\r\n") << (QStringList() << "ole" << "dole" << "doffen");
       
   559     QTest::newRow("threelines/crlf/crlf/nothing") << QByteArray("ole\r\ndole\r\ndoffen") << (QStringList() << "ole" << "dole" << "doffen");
       
   560 
       
   561     if (!for_QString) {
       
   562         // utf-8
       
   563         QTest::newRow("utf8/twolines")
       
   564             << QByteArray("\xef\xbb\xbf"
       
   565                           "\x66\x67\x65\x0a"
       
   566                           "\x66\x67\x65\x0a", 11)
       
   567             << (QStringList() << "fge" << "fge");
       
   568 
       
   569         // utf-16
       
   570         // one line
       
   571         QTest::newRow("utf16-BE/nothing")
       
   572             << QByteArray("\xfe\xff"
       
   573                           "\x00\xe5\x00\x67\x00\x65", 8) << (QStringList() << "\345ge");
       
   574         QTest::newRow("utf16-LE/nothing")
       
   575             << QByteArray("\xff\xfe"
       
   576                           "\xe5\x00\x67\x00\x65\x00", 8) << (QStringList() << "\345ge");
       
   577         QTest::newRow("utf16-BE/lf")
       
   578             << QByteArray("\xfe\xff"
       
   579                           "\x00\xe5\x00\x67\x00\x65\x00\x0a", 10) << (QStringList() << "\345ge");
       
   580         QTest::newRow("utf16-LE/lf")
       
   581             << QByteArray("\xff\xfe"
       
   582                           "\xe5\x00\x67\x00\x65\x00\x0a\x00", 10) << (QStringList() << "\345ge");
       
   583 
       
   584         // two lines
       
   585         QTest::newRow("utf16-BE/twolines")
       
   586             << QByteArray("\xfe\xff"
       
   587                           "\x00\xe5\x00\x67\x00\x65\x00\x0a"
       
   588                           "\x00\xe5\x00\x67\x00\x65\x00\x0a", 18)
       
   589             << (QStringList() << "\345ge" << "\345ge");
       
   590         QTest::newRow("utf16-LE/twolines")
       
   591             << QByteArray("\xff\xfe"
       
   592                           "\xe5\x00\x67\x00\x65\x00\x0a\x00"
       
   593                           "\xe5\x00\x67\x00\x65\x00\x0a\x00", 18)
       
   594             << (QStringList() << "\345ge" << "\345ge");
       
   595 
       
   596         // three lines
       
   597         QTest::newRow("utf16-BE/threelines")
       
   598             << QByteArray("\xfe\xff"
       
   599                           "\x00\xe5\x00\x67\x00\x65\x00\x0a"
       
   600                           "\x00\xe5\x00\x67\x00\x65\x00\x0a"
       
   601                           "\x00\xe5\x00\x67\x00\x65\x00\x0a", 26)
       
   602             << (QStringList() << "\345ge" << "\345ge" << "\345ge");
       
   603         QTest::newRow("utf16-LE/threelines")
       
   604             << QByteArray("\xff\xfe"
       
   605                           "\xe5\x00\x67\x00\x65\x00\x0a\x00"
       
   606                           "\xe5\x00\x67\x00\x65\x00\x0a\x00"
       
   607                           "\xe5\x00\x67\x00\x65\x00\x0a\x00", 26)
       
   608             << (QStringList() << "\345ge" << "\345ge" << "\345ge");
       
   609 
       
   610         // utf-32
       
   611         QTest::newRow("utf32-BE/twolines")
       
   612             << QByteArray("\x00\x00\xfe\xff"
       
   613                           "\x00\x00\x00\xe5\x00\x00\x00\x67\x00\x00\x00\x65\x00\x00\x00\x0a"
       
   614                           "\x00\x00\x00\xe5\x00\x00\x00\x67\x00\x00\x00\x65\x00\x00\x00\x0a", 36)
       
   615             << (QStringList() << "\345ge" << "\345ge");
       
   616         QTest::newRow("utf32-LE/twolines")
       
   617             << QByteArray("\xff\xfe\x00\x00"
       
   618                           "\xe5\x00\x00\x00\x67\x00\x00\x00\x65\x00\x00\x00\x0a\x00\x00\x00"
       
   619                           "\xe5\x00\x00\x00\x67\x00\x00\x00\x65\x00\x00\x00\x0a\x00\x00\x00", 36)
       
   620             << (QStringList() << "\345ge" << "\345ge");
       
   621     }
       
   622 
       
   623     // partials
       
   624     QTest::newRow("cr") << QByteArray("\r") << (QStringList() << "");
       
   625     QTest::newRow("oneline/cr") << QByteArray("ole\r") << (QStringList() << "ole");
       
   626     if (!for_QString)
       
   627         QTest::newRow("utf16-BE/cr") << QByteArray("\xfe\xff\x00\xe5\x00\x67\x00\x65\x00\x0d", 10) << (QStringList() << "\345ge");
       
   628 }
       
   629 
       
   630 // ------------------------------------------------------------------------------
       
   631 void tst_QTextStream::readLineFromDevice_data()
       
   632 {
       
   633     generateLineData(false);
       
   634 }
       
   635 
       
   636 // ------------------------------------------------------------------------------
       
   637 void tst_QTextStream::readLineFromDevice()
       
   638 {
       
   639     QFETCH(QByteArray, data);
       
   640     QFETCH(QStringList, lines);
       
   641 
       
   642     QFile::remove(TestFileName);
       
   643     QFile file(TestFileName);
       
   644     QVERIFY(file.open(QFile::ReadWrite));
       
   645     QCOMPARE(file.write(data), qlonglong(data.size()));
       
   646     QVERIFY(file.flush());
       
   647     file.seek(0);
       
   648 
       
   649     QTextStream stream(&file);
       
   650     QStringList list;
       
   651     while (!stream.atEnd())
       
   652         list << stream.readLine();
       
   653 
       
   654     QCOMPARE(list, lines);
       
   655 }
       
   656 
       
   657 // ------------------------------------------------------------------------------
       
   658 void tst_QTextStream::readLineMaxlen_data()
       
   659 {
       
   660     QTest::addColumn<QString>("input");
       
   661     QTest::addColumn<QStringList>("lines");
       
   662 
       
   663     QTest::newRow("Hey")
       
   664         << QString("Hey")
       
   665         << (QStringList() << QString("Hey") << QString(""));
       
   666     QTest::newRow("Hey\\n")
       
   667         << QString("Hey\n")
       
   668         << (QStringList() << QString("Hey") << QString(""));
       
   669     QTest::newRow("HelloWorld")
       
   670         << QString("HelloWorld")
       
   671         << (QStringList() << QString("Hello") << QString("World"));
       
   672     QTest::newRow("Helo\\nWorlds")
       
   673         << QString("Helo\nWorlds")
       
   674         << (QStringList() << QString("Helo") << QString("World"));
       
   675     QTest::newRow("AAAAA etc.")
       
   676         << QString(16385, QLatin1Char('A'))
       
   677         << (QStringList() << QString("AAAAA") << QString("AAAAA"));
       
   678     QTest::newRow("multibyte string")
       
   679         << QString::fromUtf8("\341\233\222\341\233\226\341\232\251\341\232\271\341\232\242\341\233\232\341\232\240\n")
       
   680         << (QStringList() << QString::fromUtf8("\341\233\222\341\233\226\341\232\251\341\232\271\341\232\242")
       
   681             << QString::fromUtf8("\341\233\232\341\232\240"));
       
   682 }
       
   683 
       
   684 // ------------------------------------------------------------------------------
       
   685 void tst_QTextStream::readLineMaxlen()
       
   686 {
       
   687     QFETCH(QString, input);
       
   688     QFETCH(QStringList, lines);
       
   689     for (int i = 0; i < 2; ++i) {
       
   690         bool useDevice = (i == 1);
       
   691         QTextStream stream;
       
   692         QFile::remove("testfile");
       
   693         QFile file("testfile");
       
   694         if (useDevice) {
       
   695             file.open(QIODevice::ReadWrite);
       
   696             file.write(input.toUtf8());
       
   697             file.seek(0);
       
   698             stream.setDevice(&file);
       
   699             stream.setCodec("utf-8");
       
   700         } else {
       
   701             stream.setString(&input);
       
   702         }
       
   703 
       
   704         QStringList list;
       
   705         list << stream.readLine(5);
       
   706         list << stream.readLine(5);
       
   707 
       
   708         QCOMPARE(list, lines);
       
   709     }
       
   710 }
       
   711 
       
   712 // ------------------------------------------------------------------------------
       
   713 void tst_QTextStream::readLinesFromBufferCRCR()
       
   714 {
       
   715     QBuffer buffer;
       
   716     buffer.open(QIODevice::WriteOnly);
       
   717     QByteArray data("0123456789\r\r\n");
       
   718 
       
   719     for (int i = 0; i < 10000; ++i)
       
   720         buffer.write(data);
       
   721 
       
   722     buffer.close();
       
   723     if (buffer.open(QIODevice::ReadOnly|QIODevice::Text)) {
       
   724         QTextStream stream(&buffer);
       
   725         while (!stream.atEnd())
       
   726             QCOMPARE(stream.readLine(), QString("0123456789"));
       
   727     }
       
   728 }
       
   729 
       
   730 // ------------------------------------------------------------------------------
       
   731 void tst_QTextStream::readLineFromString_data()
       
   732 {
       
   733     generateLineData(true);
       
   734 }
       
   735 
       
   736 // ------------------------------------------------------------------------------
       
   737 void tst_QTextStream::readLineFromString()
       
   738 {
       
   739     QFETCH(QByteArray, data);
       
   740     QFETCH(QStringList, lines);
       
   741 
       
   742     QString dataString = data;
       
   743 
       
   744     QTextStream stream(&dataString, QIODevice::ReadOnly);
       
   745     QStringList list;
       
   746     while (!stream.atEnd())
       
   747         list << stream.readLine();
       
   748 
       
   749     QCOMPARE(list, lines);
       
   750 }
       
   751 
       
   752 // ------------------------------------------------------------------------------
       
   753 void tst_QTextStream::readLineFromStringThenChangeString()
       
   754 {
       
   755     QString first = "First string";
       
   756     QString second = "Second string";
       
   757 
       
   758     QTextStream stream(&first, QIODevice::ReadOnly);
       
   759     QString result = stream.readLine();
       
   760     QCOMPARE(first, result);
       
   761 
       
   762     stream.setString(&second, QIODevice::ReadOnly);
       
   763     result = stream.readLine();
       
   764     QCOMPARE(second, result);
       
   765 }
       
   766 
       
   767 // ------------------------------------------------------------------------------
       
   768 void tst_QTextStream::setDevice()
       
   769 {
       
   770     // Check that the read buffer is reset after setting a new device
       
   771     QByteArray data1("Hello World");
       
   772     QByteArray data2("How are you");
       
   773 
       
   774     QBuffer bufferOld(&data1);
       
   775     bufferOld.open(QIODevice::ReadOnly);
       
   776 
       
   777     QBuffer bufferNew(&data2);
       
   778     bufferNew.open(QIODevice::ReadOnly);
       
   779 
       
   780     QString text;
       
   781     QTextStream stream(&bufferOld);
       
   782     stream >> text;
       
   783     QCOMPARE(text, QString("Hello"));
       
   784 
       
   785     stream.setDevice(&bufferNew);
       
   786     stream >> text;
       
   787     QCOMPARE(text, QString("How"));
       
   788 }
       
   789 
       
   790 // ------------------------------------------------------------------------------
       
   791 void tst_QTextStream::readLineFromTextDevice_data()
       
   792 {
       
   793     generateLineData(false);
       
   794 }
       
   795 
       
   796 // ------------------------------------------------------------------------------
       
   797 void tst_QTextStream::readLineFromTextDevice()
       
   798 {
       
   799     QFETCH(QByteArray, data);
       
   800     QFETCH(QStringList, lines);
       
   801 
       
   802     for (int i = 0; i < 8; ++i) {
       
   803         QBuffer buffer(&data);
       
   804         if (i < 4)
       
   805             QVERIFY(buffer.open(QIODevice::ReadOnly | QIODevice::Text));
       
   806         else
       
   807             QVERIFY(buffer.open(QIODevice::ReadOnly));
       
   808 
       
   809         QTextStream stream(&buffer);
       
   810         QStringList list;
       
   811         while (!stream.atEnd()) {
       
   812             stream.pos(); // <- triggers side effects
       
   813             QString line;
       
   814 
       
   815             if (i & 1) {
       
   816                 QChar c;
       
   817                 while (!stream.atEnd()) {
       
   818                     stream >> c;
       
   819                     if (stream.status() == QTextStream::Ok) {
       
   820                         if (c != QLatin1Char('\n') && c != QLatin1Char('\r'))
       
   821                             line += c;
       
   822                         if (c == QLatin1Char('\n'))
       
   823                             break;
       
   824                     }
       
   825                 }
       
   826             } else {
       
   827                 line = stream.readLine();
       
   828             }
       
   829 
       
   830             if ((i & 3) == 3 && !QString(QTest::currentDataTag()).contains("utf16"))
       
   831                 stream.seek(stream.pos());
       
   832             list << line;
       
   833         }
       
   834         QCOMPARE(list, lines);
       
   835     }
       
   836 }
       
   837 
       
   838 // ------------------------------------------------------------------------------
       
   839 void tst_QTextStream::generateAllData(bool for_QString)
       
   840 {
       
   841     QTest::addColumn<QByteArray>("input");
       
   842     QTest::addColumn<QString>("output");
       
   843 
       
   844     // latin-1
       
   845     QTest::newRow("empty") << QByteArray() << QString();
       
   846     QTest::newRow("latin1-a") << QByteArray("a") << QString("a");
       
   847     QTest::newRow("latin1-a\\r") << QByteArray("a\r") << QString("a\r");
       
   848     QTest::newRow("latin1-a\\r\\n") << QByteArray("a\r\n") << QString("a\r\n");
       
   849     QTest::newRow("latin1-a\\n") << QByteArray("a\n") << QString("a\n");
       
   850 
       
   851     // utf-16
       
   852     if (!for_QString) {
       
   853         // one line
       
   854         QTest::newRow("utf16-BE/nothing")
       
   855             << QByteArray("\xfe\xff"
       
   856                           "\x00\xe5\x00\x67\x00\x65", 8) << QString("\345ge");
       
   857         QTest::newRow("utf16-LE/nothing")
       
   858             << QByteArray("\xff\xfe"
       
   859                           "\xe5\x00\x67\x00\x65\x00", 8) << QString("\345ge");
       
   860         QTest::newRow("utf16-BE/lf")
       
   861             << QByteArray("\xfe\xff"
       
   862                           "\x00\xe5\x00\x67\x00\x65\x00\x0a", 10) << QString("\345ge\n");
       
   863         QTest::newRow("utf16-LE/lf")
       
   864             << QByteArray("\xff\xfe"
       
   865                           "\xe5\x00\x67\x00\x65\x00\x0a\x00", 10) << QString("\345ge\n");
       
   866         QTest::newRow("utf16-BE/crlf")
       
   867             << QByteArray("\xfe\xff"
       
   868                           "\x00\xe5\x00\x67\x00\x65\x00\x0d\x00\x0a", 12) << QString("\345ge\r\n");
       
   869         QTest::newRow("utf16-LE/crlf")
       
   870             << QByteArray("\xff\xfe"
       
   871                           "\xe5\x00\x67\x00\x65\x00\x0d\x00\x0a\x00", 12) << QString("\345ge\r\n");
       
   872 
       
   873         // two lines
       
   874         QTest::newRow("utf16-BE/twolines")
       
   875             << QByteArray("\xfe\xff"
       
   876                           "\x00\xe5\x00\x67\x00\x65\x00\x0a"
       
   877                           "\x00\xe5\x00\x67\x00\x65\x00\x0a", 18)
       
   878             << QString("\345ge\n\345ge\n");
       
   879         QTest::newRow("utf16-LE/twolines")
       
   880             << QByteArray("\xff\xfe"
       
   881                           "\xe5\x00\x67\x00\x65\x00\x0a\x00"
       
   882                           "\xe5\x00\x67\x00\x65\x00\x0a\x00", 18)
       
   883             << QString("\345ge\n\345ge\n");
       
   884 
       
   885         // three lines
       
   886         QTest::newRow("utf16-BE/threelines")
       
   887             << QByteArray("\xfe\xff"
       
   888                           "\x00\xe5\x00\x67\x00\x65\x00\x0a"
       
   889                           "\x00\xe5\x00\x67\x00\x65\x00\x0a"
       
   890                           "\x00\xe5\x00\x67\x00\x65\x00\x0a", 26)
       
   891             << QString("\345ge\n\345ge\n\345ge\n");
       
   892         QTest::newRow("utf16-LE/threelines")
       
   893             << QByteArray("\xff\xfe"
       
   894                           "\xe5\x00\x67\x00\x65\x00\x0a\x00"
       
   895                           "\xe5\x00\x67\x00\x65\x00\x0a\x00"
       
   896                           "\xe5\x00\x67\x00\x65\x00\x0a\x00", 26)
       
   897             << QString("\345ge\n\345ge\n\345ge\n");
       
   898     }
       
   899 }
       
   900 
       
   901 // ------------------------------------------------------------------------------
       
   902 void tst_QTextStream::readLineUntilNull()
       
   903 {
       
   904     QFile file(SRCDIR "rfc3261.txt");
       
   905     QVERIFY(file.open(QFile::ReadOnly));
       
   906 
       
   907     QTextStream stream(&file);
       
   908     for (int i = 0; i < 15066; ++i) {
       
   909         QString line = stream.readLine();
       
   910         QVERIFY(!line.isNull());
       
   911         QVERIFY(!line.isNull());
       
   912     }
       
   913     QVERIFY(!stream.readLine().isNull());
       
   914     QVERIFY(stream.readLine().isNull());
       
   915 }
       
   916 
       
   917 // ------------------------------------------------------------------------------
       
   918 void tst_QTextStream::readAllFromDevice_data()
       
   919 {
       
   920     generateAllData(false);
       
   921 }
       
   922 
       
   923 // ------------------------------------------------------------------------------
       
   924 void tst_QTextStream::readAllFromDevice()
       
   925 {
       
   926     QFETCH(QByteArray, input);
       
   927     QFETCH(QString, output);
       
   928 
       
   929     QBuffer buffer(&input);
       
   930     buffer.open(QBuffer::ReadOnly);
       
   931 
       
   932     QTextStream stream(&buffer);
       
   933     QCOMPARE(stream.readAll(), output);
       
   934 }
       
   935 
       
   936 // ------------------------------------------------------------------------------
       
   937 void tst_QTextStream::readAllFromString_data()
       
   938 {
       
   939     generateAllData(true);
       
   940 }
       
   941 
       
   942 // ------------------------------------------------------------------------------
       
   943 void tst_QTextStream::readAllFromString()
       
   944 {
       
   945     QFETCH(QByteArray, input);
       
   946     QFETCH(QString, output);
       
   947 
       
   948     QString str = input;
       
   949 
       
   950     QTextStream stream(&str);
       
   951     QCOMPARE(stream.readAll(), output);
       
   952 }
       
   953 
       
   954 // ------------------------------------------------------------------------------
       
   955 void tst_QTextStream::skipWhiteSpace_data()
       
   956 {
       
   957     QTest::addColumn<QByteArray>("input");
       
   958     QTest::addColumn<QChar>("output");
       
   959 
       
   960     // latin1
       
   961     QTest::newRow("empty") << QByteArray() << QChar('\0');
       
   962     QTest::newRow(" one") << QByteArray(" one") << QChar('o');
       
   963     QTest::newRow("\\none") << QByteArray("\none") << QChar('o');
       
   964     QTest::newRow("\\n one") << QByteArray("\n one") << QChar('o');
       
   965     QTest::newRow(" \\r\\n one") << QByteArray(" \r\n one") << QChar('o');
       
   966 
       
   967     // utf-16
       
   968     QTest::newRow("utf16-BE (empty)") << QByteArray("\xfe\xff", 2) << QChar('\0');
       
   969     QTest::newRow("utf16-BE ( one)") << QByteArray("\xfe\xff\x00 \x00o\x00n\x00e", 10) << QChar('o');
       
   970     QTest::newRow("utf16-BE (\\none)") << QByteArray("\xfe\xff\x00\n\x00o\x00n\x00e", 10) << QChar('o');
       
   971     QTest::newRow("utf16-BE (\\n one)") << QByteArray("\xfe\xff\x00\n\x00 \x00o\x00n\x00e", 12) << QChar('o');
       
   972     QTest::newRow("utf16-BE ( \\r\\n one)") << QByteArray("\xfe\xff\x00 \x00\r\x00\n\x00 \x00o\x00n\x00e", 20) << QChar('o');
       
   973 
       
   974     QTest::newRow("utf16-LE (empty)") << QByteArray("\xff\xfe", 2) << QChar('\0');
       
   975     QTest::newRow("utf16-LE ( one)") << QByteArray("\xff\xfe \x00o\x00n\x00e\x00", 10) << QChar('o');
       
   976     QTest::newRow("utf16-LE (\\none)") << QByteArray("\xff\xfe\n\x00o\x00n\x00e\x00", 10) << QChar('o');
       
   977     QTest::newRow("utf16-LE (\\n one)") << QByteArray("\xff\xfe\n\x00 \x00o\x00n\x00e\x00", 12) << QChar('o');
       
   978     QTest::newRow("utf16-LE ( \\r\\n one)") << QByteArray("\xff\xfe \x00\r\x00\n\x00 \x00o\x00n\x00e\x00", 20) << QChar('o');
       
   979 }
       
   980 
       
   981 // ------------------------------------------------------------------------------
       
   982 void tst_QTextStream::skipWhiteSpace()
       
   983 {
       
   984     QFETCH(QByteArray, input);
       
   985     QFETCH(QChar, output);
       
   986 
       
   987     QBuffer buffer(&input);
       
   988     buffer.open(QBuffer::ReadOnly);
       
   989 
       
   990     QTextStream stream(&buffer);
       
   991     stream.skipWhiteSpace();
       
   992 
       
   993     QChar tmp;
       
   994     stream >> tmp;
       
   995 
       
   996     QCOMPARE(tmp, output);
       
   997 
       
   998     QString str = input;
       
   999     QTextStream stream2(&input);
       
  1000     stream2.skipWhiteSpace();
       
  1001 
       
  1002     stream2 >> tmp;
       
  1003 
       
  1004     QCOMPARE(tmp, output);
       
  1005 }
       
  1006 
       
  1007 // ------------------------------------------------------------------------------
       
  1008 void tst_QTextStream::lineCount_data()
       
  1009 {
       
  1010     QTest::addColumn<QByteArray>("data");
       
  1011     QTest::addColumn<int>("lineCount");
       
  1012 
       
  1013     QTest::newRow("empty") << QByteArray() << 0;
       
  1014     QTest::newRow("oneline") << QByteArray("a\n") << 1;
       
  1015     QTest::newRow("twolines") << QByteArray("a\nb\n") << 2;
       
  1016     QTest::newRow("oneemptyline") << QByteArray("\n") << 1;
       
  1017     QTest::newRow("twoemptylines") << QByteArray("\n\n") << 2;
       
  1018     QTest::newRow("buffersize-1 line") << QByteArray(16382, '\n') << 16382;
       
  1019     QTest::newRow("buffersize line") << QByteArray(16383, '\n') << 16383;
       
  1020     QTest::newRow("buffersize+1 line") << QByteArray(16384, '\n') << 16384;
       
  1021     QTest::newRow("buffersize+2 line") << QByteArray(16385, '\n') << 16385;
       
  1022 
       
  1023     QFile file(SRCDIR "rfc3261.txt"); file.open(QFile::ReadOnly);
       
  1024     QTest::newRow("rfc3261") << file.readAll() << 15067;
       
  1025 }
       
  1026 
       
  1027 // ------------------------------------------------------------------------------
       
  1028 void tst_QTextStream::lineCount()
       
  1029 {
       
  1030     QFETCH(QByteArray, data);
       
  1031     QFETCH(int, lineCount);
       
  1032 
       
  1033     QFile out("out.txt");
       
  1034     out.open(QFile::WriteOnly);
       
  1035 
       
  1036     QTextStream lineReader(data);
       
  1037     int lines = 0;
       
  1038     while (!lineReader.atEnd()) {
       
  1039         QString line = lineReader.readLine();
       
  1040         out.write(line.toLatin1() + "\n");
       
  1041         ++lines;
       
  1042     }
       
  1043 
       
  1044     out.close();
       
  1045     QCOMPARE(lines, lineCount);
       
  1046 }
       
  1047 
       
  1048 // ------------------------------------------------------------------------------
       
  1049 void tst_QTextStream::performance()
       
  1050 {
       
  1051     // Phase #1 - test speed of reading a huge text file with QFile.
       
  1052     QTime stopWatch;
       
  1053 
       
  1054     int elapsed1 = 0;
       
  1055     int elapsed2 = 0;
       
  1056 
       
  1057         stopWatch.restart();
       
  1058         int nlines1 = 0;
       
  1059         QFile file(SRCDIR "rfc3261.txt");
       
  1060         QVERIFY(file.open(QFile::ReadOnly));
       
  1061 
       
  1062         while (!file.atEnd()) {
       
  1063             ++nlines1;
       
  1064             file.readLine();
       
  1065         }
       
  1066 
       
  1067         elapsed1 += stopWatch.elapsed();
       
  1068         stopWatch.restart();
       
  1069 
       
  1070         int nlines2 = 0;
       
  1071         QFile file2(SRCDIR "rfc3261.txt");
       
  1072         QVERIFY(file2.open(QFile::ReadOnly));
       
  1073 
       
  1074         QTextStream stream(&file2);
       
  1075         while (!stream.atEnd()) {
       
  1076             ++nlines2;
       
  1077             stream.readLine();
       
  1078         }
       
  1079 
       
  1080         elapsed2 += stopWatch.elapsed();
       
  1081         QCOMPARE(nlines1, nlines2);
       
  1082 
       
  1083     qDebug("QFile used %.2f seconds to read the file",
       
  1084            elapsed1 / 1000.0);
       
  1085 
       
  1086     qDebug("QTextStream used %.2f seconds to read the file",
       
  1087            elapsed2 / 1000.0);
       
  1088     if (elapsed2 > elapsed1) {
       
  1089         qDebug("QFile is %.2fx faster than QTextStream",
       
  1090                double(elapsed2) / double(elapsed1));
       
  1091     } else {
       
  1092         qDebug("QTextStream is %.2fx faster than QFile",
       
  1093                double(elapsed1) / double(elapsed2));
       
  1094     }
       
  1095 }
       
  1096 
       
  1097 // ------------------------------------------------------------------------------
       
  1098 void tst_QTextStream::hexTest_data()
       
  1099 {
       
  1100     QTest::addColumn<qlonglong>("number");
       
  1101     QTest::addColumn<QByteArray>("data");
       
  1102 
       
  1103     QTest::newRow("0") << Q_INT64_C(0) << QByteArray("0x0");
       
  1104     QTest::newRow("1") << Q_INT64_C(1) << QByteArray("0x1");
       
  1105     QTest::newRow("2") << Q_INT64_C(2) << QByteArray("0x2");
       
  1106     QTest::newRow("3") << Q_INT64_C(3) << QByteArray("0x3");
       
  1107     QTest::newRow("4") << Q_INT64_C(4) << QByteArray("0x4");
       
  1108     QTest::newRow("5") << Q_INT64_C(5) << QByteArray("0x5");
       
  1109     QTest::newRow("6") << Q_INT64_C(6) << QByteArray("0x6");
       
  1110     QTest::newRow("7") << Q_INT64_C(7) << QByteArray("0x7");
       
  1111     QTest::newRow("8") << Q_INT64_C(8) << QByteArray("0x8");
       
  1112     QTest::newRow("9") << Q_INT64_C(9) << QByteArray("0x9");
       
  1113     QTest::newRow("a") << Q_INT64_C(0xa) << QByteArray("0xa");
       
  1114     QTest::newRow("b") << Q_INT64_C(0xb) << QByteArray("0xb");
       
  1115     QTest::newRow("c") << Q_INT64_C(0xc) << QByteArray("0xc");
       
  1116     QTest::newRow("d") << Q_INT64_C(0xd) << QByteArray("0xd");
       
  1117     QTest::newRow("e") << Q_INT64_C(0xe) << QByteArray("0xe");
       
  1118     QTest::newRow("f") << Q_INT64_C(0xf) << QByteArray("0xf");
       
  1119     QTest::newRow("-1") << Q_INT64_C(-1) << QByteArray("-0x1");
       
  1120     QTest::newRow("0xffffffff") << Q_INT64_C(0xffffffff) << QByteArray("0xffffffff");
       
  1121     QTest::newRow("0xfffffffffffffffe") << Q_INT64_C(0xfffffffffffffffe) << QByteArray("-0x2");
       
  1122     QTest::newRow("0xffffffffffffffff") << Q_INT64_C(0xffffffffffffffff) << QByteArray("-0x1");
       
  1123     QTest::newRow("0x7fffffffffffffff") << Q_INT64_C(0x7fffffffffffffff) << QByteArray("0x7fffffffffffffff");
       
  1124 }
       
  1125 
       
  1126 // ------------------------------------------------------------------------------
       
  1127 void tst_QTextStream::hexTest()
       
  1128 {
       
  1129     QFETCH(qlonglong, number);
       
  1130     QFETCH(QByteArray, data);
       
  1131 
       
  1132     QByteArray array;
       
  1133     QTextStream stream(&array);
       
  1134 
       
  1135     stream << showbase << hex << number;
       
  1136     stream.flush();
       
  1137     QCOMPARE(array, data);
       
  1138 }
       
  1139 
       
  1140 // ------------------------------------------------------------------------------
       
  1141 void tst_QTextStream::binTest_data()
       
  1142 {
       
  1143     QTest::addColumn<int>("number");
       
  1144     QTest::addColumn<QByteArray>("data");
       
  1145 
       
  1146     QTest::newRow("0") << 0 << QByteArray("0b0");
       
  1147     QTest::newRow("1") << 1 << QByteArray("0b1");
       
  1148     QTest::newRow("2") << 2 << QByteArray("0b10");
       
  1149     QTest::newRow("5") << 5 << QByteArray("0b101");
       
  1150     QTest::newRow("-1") << -1 << QByteArray("-0b1");
       
  1151     QTest::newRow("11111111") << 0xff << QByteArray("0b11111111");
       
  1152     QTest::newRow("1111111111111111") << 0xffff << QByteArray("0b1111111111111111");
       
  1153     QTest::newRow("1111111011111110") << 0xfefe << QByteArray("0b1111111011111110");
       
  1154 }
       
  1155 
       
  1156 // ------------------------------------------------------------------------------
       
  1157 void tst_QTextStream::binTest()
       
  1158 {
       
  1159     QFETCH(int, number);
       
  1160     QFETCH(QByteArray, data);
       
  1161 
       
  1162     QByteArray array;
       
  1163     QTextStream stream(&array);
       
  1164 
       
  1165     stream << showbase << bin << number;
       
  1166     stream.flush();
       
  1167     QCOMPARE(array.constData(), data.constData());
       
  1168 }
       
  1169 
       
  1170 // ------------------------------------------------------------------------------
       
  1171 void tst_QTextStream::octTest_data()
       
  1172 {
       
  1173     QTest::addColumn<int>("number");
       
  1174     QTest::addColumn<QByteArray>("data");
       
  1175 
       
  1176     QTest::newRow("0") << 0 << QByteArray("00");
       
  1177 }
       
  1178 
       
  1179 // ------------------------------------------------------------------------------
       
  1180 void tst_QTextStream::octTest()
       
  1181 {
       
  1182     QFETCH(int, number);
       
  1183     QFETCH(QByteArray, data);
       
  1184 
       
  1185     QByteArray array;
       
  1186     QTextStream stream(&array);
       
  1187 
       
  1188     stream << showbase << oct << number;
       
  1189     stream.flush();
       
  1190     QCOMPARE(array, data);
       
  1191 }
       
  1192 
       
  1193 // ------------------------------------------------------------------------------
       
  1194 void tst_QTextStream::zeroTermination()
       
  1195 {
       
  1196     QTextStream stream;
       
  1197     char c = '@';
       
  1198 
       
  1199     QTest::ignoreMessage(QtWarningMsg, "QTextStream: No device");
       
  1200     stream >> c;
       
  1201     QCOMPARE(c, '\0');
       
  1202 
       
  1203     c = '@';
       
  1204 
       
  1205     QTest::ignoreMessage(QtWarningMsg, "QTextStream: No device");
       
  1206     stream >> &c;
       
  1207     QCOMPARE(c, '\0');
       
  1208 }
       
  1209 
       
  1210 // ------------------------------------------------------------------------------
       
  1211 void tst_QTextStream::ws_manipulator()
       
  1212 {
       
  1213     {
       
  1214         QString string = "a b c d";
       
  1215         QTextStream stream(&string);
       
  1216 
       
  1217         char a, b, c, d;
       
  1218         stream >> a >> b >> c >> d;
       
  1219         QCOMPARE(a, 'a');
       
  1220         QCOMPARE(b, ' ');
       
  1221         QCOMPARE(c, 'b');
       
  1222         QCOMPARE(d, ' ');
       
  1223     }
       
  1224     {
       
  1225         QString string = "a b c d";
       
  1226         QTextStream stream(&string);
       
  1227 
       
  1228         char a, b, c, d;
       
  1229         stream >> a >> ws >> b >> ws >> c >> ws >> d;
       
  1230         QCOMPARE(a, 'a');
       
  1231         QCOMPARE(b, 'b');
       
  1232         QCOMPARE(c, 'c');
       
  1233         QCOMPARE(d, 'd');
       
  1234     }
       
  1235 }
       
  1236 
       
  1237 // ------------------------------------------------------------------------------
       
  1238 void tst_QTextStream::stillOpenWhenAtEnd()
       
  1239 {
       
  1240     int argc = 0;
       
  1241     char **argv = 0;
       
  1242     QCoreApplication app(argc, argv);
       
  1243 
       
  1244     QFile file(SRCDIR "tst_qtextstream.cpp");
       
  1245     QVERIFY(file.open(QFile::ReadOnly));
       
  1246 
       
  1247     QTextStream stream(&file);
       
  1248     while (!stream.readLine().isNull()) {}
       
  1249     QVERIFY(file.isOpen());
       
  1250 
       
  1251 #ifdef Q_OS_WINCE
       
  1252     QSKIP("Qt/CE: Cannot test network on emulator", SkipAll);
       
  1253 #endif
       
  1254     QTcpSocket socket;
       
  1255     socket.connectToHost(QtNetworkSettings::serverName(), 143);
       
  1256 #if defined(Q_OS_SYMBIAN)
       
  1257     // This number is determined in an arbitrary way; whatever it takes
       
  1258     // to make the test pass.
       
  1259     QVERIFY(socket.waitForReadyRead(30000));
       
  1260 #else
       
  1261     QVERIFY(socket.waitForReadyRead(5000));
       
  1262 #endif
       
  1263 
       
  1264     QTextStream stream2(&socket);
       
  1265     while (!stream2.readLine().isNull()) {}
       
  1266     QVERIFY(socket.isOpen());
       
  1267 }
       
  1268 
       
  1269 // ------------------------------------------------------------------------------
       
  1270 void tst_QTextStream::readNewlines_data()
       
  1271 {
       
  1272     QTest::addColumn<QByteArray>("input");
       
  1273     QTest::addColumn<QString>("output");
       
  1274 
       
  1275     QTest::newRow("empty") << QByteArray() << QString();
       
  1276     QTest::newRow("\\r\\n") << QByteArray("\r\n") << QString("\n");
       
  1277     QTest::newRow("\\r\\r\\n") << QByteArray("\r\r\n") << QString("\n");
       
  1278     QTest::newRow("\\r\\n\\r\\n") << QByteArray("\r\n\r\n") << QString("\n\n");
       
  1279     QTest::newRow("\\n") << QByteArray("\n") << QString("\n");
       
  1280     QTest::newRow("\\n\\n") << QByteArray("\n\n") << QString("\n\n");
       
  1281 }
       
  1282 
       
  1283 // ------------------------------------------------------------------------------
       
  1284 void tst_QTextStream::readNewlines()
       
  1285 {
       
  1286     QFETCH(QByteArray, input);
       
  1287     QFETCH(QString, output);
       
  1288 
       
  1289     QBuffer buffer(&input);
       
  1290     buffer.open(QBuffer::ReadOnly | QBuffer::Text);
       
  1291     QTextStream stream(&buffer);
       
  1292     QCOMPARE(stream.readAll(), output);
       
  1293 }
       
  1294 
       
  1295 // ------------------------------------------------------------------------------
       
  1296 void tst_QTextStream::seek()
       
  1297 {
       
  1298     QFile file(SRCDIR "rfc3261.txt");
       
  1299     QVERIFY(file.open(QFile::ReadOnly));
       
  1300 
       
  1301     QTextStream stream(&file);
       
  1302     QString tmp;
       
  1303     stream >> tmp;
       
  1304     QCOMPARE(tmp, QString::fromLatin1("Network"));
       
  1305 
       
  1306     // QTextStream::seek(0) should both clear its internal read/write buffers
       
  1307     // and seek the device.
       
  1308     for (int i = 0; i < 4; ++i) {
       
  1309         stream.seek(12 + i);
       
  1310         stream >> tmp;
       
  1311         QCOMPARE(tmp, QString("Network").mid(i));
       
  1312     }
       
  1313     for (int i = 0; i < 4; ++i) {
       
  1314         stream.seek(16 - i);
       
  1315         stream >> tmp;
       
  1316         QCOMPARE(tmp, QString("Network").mid(4 - i));
       
  1317     }
       
  1318     stream.seek(139181);
       
  1319     stream >> tmp;
       
  1320     QCOMPARE(tmp, QString("information"));
       
  1321     stream.seek(388683);
       
  1322     stream >> tmp;
       
  1323     QCOMPARE(tmp, QString("telephone"));
       
  1324 
       
  1325     // Also test this with a string
       
  1326     QString words = QLatin1String("thisisa");
       
  1327     QTextStream stream2(&words, QIODevice::ReadOnly);
       
  1328     stream2 >> tmp;
       
  1329     QCOMPARE(tmp, QString::fromLatin1("thisisa"));
       
  1330 
       
  1331     for (int i = 0; i < 4; ++i) {
       
  1332         stream2.seek(i);
       
  1333         stream2 >> tmp;
       
  1334         QCOMPARE(tmp, QString("thisisa").mid(i));
       
  1335     }
       
  1336     for (int i = 0; i < 4; ++i) {
       
  1337         stream2.seek(4 - i);
       
  1338         stream2 >> tmp;
       
  1339         QCOMPARE(tmp, QString("thisisa").mid(4 - i));
       
  1340     }
       
  1341 }
       
  1342 
       
  1343 // ------------------------------------------------------------------------------
       
  1344 void tst_QTextStream::pos()
       
  1345 {
       
  1346     int argc = 1;
       
  1347     QCoreApplication app(argc, 0);
       
  1348     {
       
  1349         // Strings
       
  1350         QString str("this is a test");
       
  1351         QTextStream stream(&str, QIODevice::ReadWrite);
       
  1352 
       
  1353         QCOMPARE(stream.pos(), qint64(0));
       
  1354         for (int i = 0; i <= str.size(); ++i) {
       
  1355             QVERIFY(stream.seek(i));
       
  1356             QCOMPARE(stream.pos(), qint64(i));
       
  1357         }
       
  1358         for (int j = str.size(); j >= 0; --j) {
       
  1359             QVERIFY(stream.seek(j));
       
  1360             QCOMPARE(stream.pos(), qint64(j));
       
  1361         }
       
  1362 
       
  1363         QVERIFY(stream.seek(0));
       
  1364 
       
  1365         QChar ch;
       
  1366         stream >> ch;
       
  1367         QCOMPARE(ch, QChar('t'));
       
  1368 
       
  1369         QCOMPARE(stream.pos(), qint64(1));
       
  1370         QVERIFY(stream.seek(1));
       
  1371         QCOMPARE(stream.pos(), qint64(1));
       
  1372         QVERIFY(stream.seek(0));
       
  1373 
       
  1374         QString strtmp;
       
  1375         stream >> strtmp;
       
  1376         QCOMPARE(strtmp, QString("this"));
       
  1377 
       
  1378         QCOMPARE(stream.pos(), qint64(4));
       
  1379         stream.seek(0);
       
  1380         stream.seek(4);
       
  1381 
       
  1382         stream >> ch;
       
  1383         QCOMPARE(ch, QChar(' '));
       
  1384         QCOMPARE(stream.pos(), qint64(5));
       
  1385 
       
  1386         stream.seek(10);
       
  1387         stream >> strtmp;
       
  1388         QCOMPARE(strtmp, QString("test"));
       
  1389         QCOMPARE(stream.pos(), qint64(14));
       
  1390     }
       
  1391     {
       
  1392         // Latin1 device
       
  1393         QFile file(SRCDIR "rfc3261.txt");
       
  1394         QVERIFY(file.open(QIODevice::ReadOnly));
       
  1395 
       
  1396         QTextStream stream(&file);
       
  1397 
       
  1398         QCOMPARE(stream.pos(), qint64(0));
       
  1399 
       
  1400         for (int i = 0; i <= file.size(); i += 7) {
       
  1401             QVERIFY(stream.seek(i));
       
  1402             QCOMPARE(stream.pos(), qint64(i));
       
  1403         }
       
  1404         for (int j = file.size(); j >= 0; j -= 7) {
       
  1405             QVERIFY(stream.seek(j));
       
  1406             QCOMPARE(stream.pos(), qint64(j));
       
  1407         }
       
  1408 
       
  1409         stream.seek(0);
       
  1410 
       
  1411         QString strtmp;
       
  1412         stream >> strtmp;
       
  1413         QCOMPARE(strtmp, QString("Network"));
       
  1414         QCOMPARE(stream.pos(), qint64(19));
       
  1415 
       
  1416         stream.seek(2598);
       
  1417         QCOMPARE(stream.pos(), qint64(2598));
       
  1418         stream >> strtmp;
       
  1419         QCOMPARE(stream.pos(), qint64(2607));
       
  1420         QCOMPARE(strtmp, QString("locations"));
       
  1421     }
       
  1422     {
       
  1423         // Shift-JIS device
       
  1424         for (int i = 0; i < 2; ++i) {
       
  1425             QFile file(SRCDIR "shift-jis.txt");
       
  1426             if (i == 0)
       
  1427                 QVERIFY(file.open(QIODevice::ReadOnly));
       
  1428             else
       
  1429                 QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));
       
  1430 
       
  1431             QTextStream stream(&file);
       
  1432             stream.setCodec("Shift-JIS");
       
  1433             QVERIFY(stream.codec());
       
  1434 
       
  1435             QCOMPARE(stream.pos(), qint64(0));
       
  1436             for (int i = 0; i <= file.size(); i += 7) {
       
  1437                 QVERIFY(stream.seek(i));
       
  1438                 QCOMPARE(stream.pos(), qint64(i));
       
  1439             }
       
  1440             for (int j = file.size(); j >= 0; j -= 7) {
       
  1441                 QVERIFY(stream.seek(j));
       
  1442                 QCOMPARE(stream.pos(), qint64(j));
       
  1443             }
       
  1444 
       
  1445             stream.seek(2089);
       
  1446             QString strtmp;
       
  1447             stream >> strtmp;
       
  1448             QCOMPARE(strtmp, QString("AUnicode"));
       
  1449             QCOMPARE(stream.pos(), qint64(2097));
       
  1450 
       
  1451             stream.seek(43325);
       
  1452             stream >> strtmp;
       
  1453             QCOMPARE(strtmp, QString("Shift-JIS"));
       
  1454             stream >> strtmp;
       
  1455             QCOMPARE(strtmp, QString::fromUtf8("\343\201\247\346\233\270\343\201\213\343\202\214\343\201\237"));
       
  1456             QCOMPARE(stream.pos(), qint64(43345));
       
  1457             stream >> strtmp;
       
  1458             QCOMPARE(strtmp, QString("POD"));
       
  1459             QCOMPARE(stream.pos(), qint64(43349));
       
  1460         }
       
  1461     }
       
  1462 }
       
  1463 
       
  1464 // ------------------------------------------------------------------------------
       
  1465 void tst_QTextStream::pos2()
       
  1466 {
       
  1467     QByteArray data("abcdef\r\nghijkl\r\n");
       
  1468     QBuffer buffer(&data);
       
  1469     QVERIFY(buffer.open(QIODevice::ReadOnly | QIODevice::Text));
       
  1470 
       
  1471     QTextStream stream(&buffer);
       
  1472 
       
  1473     QChar ch;
       
  1474 
       
  1475     QCOMPARE(stream.pos(), qint64(0));
       
  1476     stream >> ch;
       
  1477     QCOMPARE(ch, QChar('a'));
       
  1478     QCOMPARE(stream.pos(), qint64(1));
       
  1479 
       
  1480     QString str;
       
  1481     stream >> str;
       
  1482     QCOMPARE(str, QString("bcdef"));
       
  1483     QCOMPARE(stream.pos(), qint64(6));
       
  1484 
       
  1485     stream >> str;
       
  1486     QCOMPARE(str, QString("ghijkl"));
       
  1487     QCOMPARE(stream.pos(), qint64(14));
       
  1488 
       
  1489     // Seek back and try again
       
  1490     stream.seek(1);
       
  1491     QCOMPARE(stream.pos(), qint64(1));
       
  1492     stream >> str;
       
  1493     QCOMPARE(str, QString("bcdef"));
       
  1494     QCOMPARE(stream.pos(), qint64(6));
       
  1495 
       
  1496     stream.seek(6);
       
  1497     stream >> str;
       
  1498     QCOMPARE(str, QString("ghijkl"));
       
  1499     QCOMPARE(stream.pos(), qint64(14));
       
  1500 }
       
  1501 
       
  1502 // ------------------------------------------------------------------------------
       
  1503 void tst_QTextStream::readStdin()
       
  1504 {
       
  1505 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
       
  1506     QSKIP("Qt/CE and Symbian have no stdin/out support for processes", SkipAll);
       
  1507 #endif
       
  1508     QProcess stdinProcess;
       
  1509     stdinProcess.start("stdinProcess/stdinProcess");
       
  1510     stdinProcess.setReadChannel(QProcess::StandardError);
       
  1511 
       
  1512     QTextStream stream(&stdinProcess);
       
  1513     stream << "1" << endl;
       
  1514     stream << "2" << endl;
       
  1515     stream << "3" << endl;
       
  1516 
       
  1517     stdinProcess.closeWriteChannel();
       
  1518 
       
  1519     QVERIFY(stdinProcess.waitForFinished(5000));
       
  1520 
       
  1521     int a, b, c;
       
  1522     stream >> a >> b >> c;
       
  1523     QCOMPARE(a, 1);
       
  1524     QCOMPARE(b, 2);
       
  1525     QCOMPARE(c, 3);
       
  1526 }
       
  1527 
       
  1528 // ------------------------------------------------------------------------------
       
  1529 void tst_QTextStream::readAllFromStdin()
       
  1530 {
       
  1531 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
       
  1532     QSKIP("Qt/CE and Symbian have no stdin/out support for processes", SkipAll);
       
  1533 #endif
       
  1534     QProcess stdinProcess;
       
  1535     stdinProcess.start("readAllStdinProcess/readAllStdinProcess", QIODevice::ReadWrite | QIODevice::Text);
       
  1536     stdinProcess.setReadChannel(QProcess::StandardError);
       
  1537 
       
  1538     QTextStream stream(&stdinProcess);
       
  1539     stream.setCodec("ISO-8859-1");
       
  1540     stream << "hello world" << flush;
       
  1541 
       
  1542     stdinProcess.closeWriteChannel();
       
  1543 
       
  1544     QVERIFY(stdinProcess.waitForFinished(5000));
       
  1545     QChar quoteChar('"');
       
  1546     QCOMPARE(stream.readAll(), QString::fromLatin1("%1hello world%2 \n").arg(quoteChar).arg(quoteChar));
       
  1547 }
       
  1548 
       
  1549 // ------------------------------------------------------------------------------
       
  1550 void tst_QTextStream::readLineFromStdin()
       
  1551 {
       
  1552 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
       
  1553     QSKIP("Qt/CE and Symbian have no stdin/out support for processes", SkipAll);
       
  1554 #endif
       
  1555     QProcess stdinProcess;
       
  1556     stdinProcess.start("readLineStdinProcess/readLineStdinProcess", QIODevice::ReadWrite | QIODevice::Text);
       
  1557     stdinProcess.setReadChannel(QProcess::StandardError);
       
  1558 
       
  1559     stdinProcess.write("abc\n");
       
  1560     QVERIFY(stdinProcess.waitForReadyRead(5000));
       
  1561     QCOMPARE(stdinProcess.readAll().data(), QByteArray("abc").data());
       
  1562 
       
  1563     stdinProcess.write("def\n");
       
  1564     QVERIFY(stdinProcess.waitForReadyRead(5000));
       
  1565     QCOMPARE(stdinProcess.readAll(), QByteArray("def"));
       
  1566 
       
  1567     stdinProcess.closeWriteChannel();
       
  1568 
       
  1569     QVERIFY(stdinProcess.waitForFinished(5000));
       
  1570 }
       
  1571 
       
  1572 // ------------------------------------------------------------------------------
       
  1573 void tst_QTextStream::read()
       
  1574 {
       
  1575     {
       
  1576         QFile::remove("testfile");
       
  1577         QFile file("testfile");
       
  1578         file.open(QFile::WriteOnly);
       
  1579         file.write("4.15 abc ole");
       
  1580         file.close();
       
  1581 
       
  1582         QVERIFY(file.open(QFile::ReadOnly));
       
  1583         QTextStream stream(&file);
       
  1584         QCOMPARE(stream.read(0), QString(""));
       
  1585         QCOMPARE(stream.read(4), QString("4.15"));
       
  1586         QCOMPARE(stream.read(4), QString(" abc"));
       
  1587         stream.seek(1);
       
  1588         QCOMPARE(stream.read(4), QString(".15 "));
       
  1589         stream.seek(1);
       
  1590         QCOMPARE(stream.read(4), QString(".15 "));
       
  1591         stream.seek(2);
       
  1592         QCOMPARE(stream.read(4), QString("15 a"));
       
  1593         // ### add tests for reading \r\n etc..
       
  1594     }
       
  1595 
       
  1596     {
       
  1597         // File larger than QTEXTSTREAM_BUFFERSIZE
       
  1598         QFile::remove("testfile");
       
  1599         QFile file("testfile");
       
  1600         file.open(QFile::WriteOnly);
       
  1601         for (int i = 0; i < 16384 / 8; ++i)
       
  1602             file.write("01234567");
       
  1603         file.write("0");
       
  1604         file.close();
       
  1605 
       
  1606         QVERIFY(file.open(QFile::ReadOnly));
       
  1607         QTextStream stream(&file);
       
  1608         QCOMPARE(stream.read(10), QString("0123456701"));
       
  1609         QCOMPARE(stream.read(10), QString("2345670123"));
       
  1610         QCOMPARE(stream.readAll().size(), 16385-20);
       
  1611     }
       
  1612 }
       
  1613 
       
  1614 // ------------------------------------------------------------------------------
       
  1615 void tst_QTextStream::qbool()
       
  1616 {
       
  1617     QString s;
       
  1618     QTextStream stream(&s);
       
  1619     stream << s.contains(QString("hei"));
       
  1620     QCOMPARE(s, QString("0"));
       
  1621 }
       
  1622 
       
  1623 // ------------------------------------------------------------------------------
       
  1624 void tst_QTextStream::forcePoint()
       
  1625 {
       
  1626     QString str;
       
  1627     QTextStream stream(&str);
       
  1628     stream << fixed << forcepoint << 1.0 << " " << 1 << " " << 0 << " " << -1.0 << " " << -1;
       
  1629     QCOMPARE(str, QString("1.000000 1 0 -1.000000 -1"));
       
  1630 
       
  1631     str.clear();
       
  1632     stream.seek(0);
       
  1633     stream << scientific << forcepoint << 1.0 << " " << 1 << " " << 0 << " " << -1.0 << " " << -1;
       
  1634     QCOMPARE(str, QString("1.000000e+00 1 0 -1.000000e+00 -1"));
       
  1635 
       
  1636     str.clear();
       
  1637     stream.seek(0);
       
  1638     stream.setRealNumberNotation(QTextStream::SmartNotation);
       
  1639     stream << forcepoint << 1.0 << " " << 1 << " " << 0 << " " << -1.0 << " " << -1;
       
  1640     QCOMPARE(str, QString("1.00000 1 0 -1.00000 -1"));
       
  1641 
       
  1642 }
       
  1643 
       
  1644 // ------------------------------------------------------------------------------
       
  1645 void tst_QTextStream::forceSign()
       
  1646 {
       
  1647     QString str;
       
  1648     QTextStream stream(&str);
       
  1649     stream << forcesign << 1.2 << " " << -1.2 << " " << 0;
       
  1650     QCOMPARE(str, QString("+1.2 -1.2 +0"));
       
  1651 }
       
  1652 
       
  1653 // ------------------------------------------------------------------------------
       
  1654 void tst_QTextStream::read0d0d0a()
       
  1655 {
       
  1656     QFile file("task113817.txt");
       
  1657     file.open(QIODevice::ReadOnly | QIODevice::Text);
       
  1658 
       
  1659     QTextStream stream(&file);
       
  1660     while (!stream.atEnd())
       
  1661         stream.readLine();
       
  1662 }
       
  1663 
       
  1664 // ------------------------------------------------------------------------------
       
  1665 
       
  1666 Q_DECLARE_METATYPE(QTextStreamFunction);
       
  1667 
       
  1668 QTextStream &noop(QTextStream &s) { return s; }
       
  1669 
       
  1670 void tst_QTextStream::numeralCase_data()
       
  1671 {
       
  1672     QTextStreamFunction noop_ = noop;
       
  1673     QTextStreamFunction bin_  = bin;
       
  1674     QTextStreamFunction oct_  = oct;
       
  1675     QTextStreamFunction hex_  = hex;
       
  1676     QTextStreamFunction base  = showbase;
       
  1677     QTextStreamFunction ucb   = uppercasebase;
       
  1678     QTextStreamFunction lcb   = lowercasebase;
       
  1679     QTextStreamFunction ucd   = uppercasedigits;
       
  1680     QTextStreamFunction lcd   = lowercasedigits;
       
  1681 
       
  1682     QTest::addColumn<QTextStreamFunction>("func1");
       
  1683     QTest::addColumn<QTextStreamFunction>("func2");
       
  1684     QTest::addColumn<QTextStreamFunction>("func3");
       
  1685     QTest::addColumn<QTextStreamFunction>("func4");
       
  1686     QTest::addColumn<int>("value");
       
  1687     QTest::addColumn<QString>("expected");
       
  1688     QTest::newRow("dec 1") << noop_ << noop_ << noop_ << noop_ << 31 << "31";
       
  1689     QTest::newRow("dec 2") << noop_ << base  << noop_ << noop_ << 31 << "31";
       
  1690 
       
  1691     QTest::newRow("hex 1")  << hex_  << noop_ << noop_ << noop_ << 31 << "1f";
       
  1692     QTest::newRow("hex 2")  << hex_  << noop_ << noop_ << lcd   << 31 << "1f";
       
  1693     QTest::newRow("hex 3")  << hex_  << noop_ << ucb   << noop_ << 31 << "1f";
       
  1694     QTest::newRow("hex 4")  << hex_  << noop_ << noop_ << ucd   << 31 << "1F";
       
  1695     QTest::newRow("hex 5")  << hex_  << noop_ << lcb   << ucd   << 31 << "1F";
       
  1696     QTest::newRow("hex 6")  << hex_  << noop_ << ucb   << ucd   << 31 << "1F";
       
  1697     QTest::newRow("hex 7")  << hex_  << base  << noop_ << noop_ << 31 << "0x1f";
       
  1698     QTest::newRow("hex 8")  << hex_  << base  << lcb   << lcd   << 31 << "0x1f";
       
  1699     QTest::newRow("hex 9")  << hex_  << base  << ucb   << noop_ << 31 << "0X1f";
       
  1700     QTest::newRow("hex 10") << hex_  << base  << ucb   << lcd   << 31 << "0X1f";
       
  1701     QTest::newRow("hex 11") << hex_  << base  << noop_ << ucd   << 31 << "0x1F";
       
  1702     QTest::newRow("hex 12") << hex_  << base  << lcb   << ucd   << 31 << "0x1F";
       
  1703     QTest::newRow("hex 13") << hex_  << base  << ucb   << ucd   << 31 << "0X1F";
       
  1704 
       
  1705     QTest::newRow("bin 1") << bin_  << noop_ << noop_ << noop_ << 31 << "11111";
       
  1706     QTest::newRow("bin 2") << bin_  << base  << noop_ << noop_ << 31 << "0b11111";
       
  1707     QTest::newRow("bin 3") << bin_  << base  << lcb   << noop_ << 31 << "0b11111";
       
  1708     QTest::newRow("bin 4") << bin_  << base  << ucb   << noop_ << 31 << "0B11111";
       
  1709     QTest::newRow("bin 5") << bin_  << base  << noop_ << ucd   << 31 << "0b11111";
       
  1710     QTest::newRow("bin 6") << bin_  << base  << lcb   << ucd   << 31 << "0b11111";
       
  1711     QTest::newRow("bin 7") << bin_  << base  << ucb   << ucd   << 31 << "0B11111";
       
  1712 
       
  1713     QTest::newRow("oct 1") << oct_  << noop_ << noop_ << noop_ << 31 << "37";
       
  1714     QTest::newRow("oct 2") << oct_  << base  << noop_ << noop_ << 31 << "037";
       
  1715 }
       
  1716 
       
  1717 // From Task 125496
       
  1718 void tst_QTextStream::numeralCase()
       
  1719 {
       
  1720     QFETCH(QTextStreamFunction, func1);
       
  1721     QFETCH(QTextStreamFunction, func2);
       
  1722     QFETCH(QTextStreamFunction, func3);
       
  1723     QFETCH(QTextStreamFunction, func4);
       
  1724     QFETCH(int, value);
       
  1725     QFETCH(QString, expected);
       
  1726 
       
  1727     QString str;
       
  1728     QTextStream stream(&str);
       
  1729     stream << func1 << func2 << func3 << func4 << value;
       
  1730     QCOMPARE(str, expected);
       
  1731 }
       
  1732 
       
  1733 // ------------------------------------------------------------------------------
       
  1734 void tst_QTextStream::nanInf()
       
  1735 {
       
  1736     // Cannot use test data in this function, as comparing nans and infs isn't
       
  1737     // well defined.
       
  1738     QString str("nan NAN nAn +nan +NAN +nAn -nan -NAN -nAn"
       
  1739                 " inf INF iNf +inf +INF +iNf -inf -INF -iNf");
       
  1740 
       
  1741     QTextStream stream(&str);
       
  1742 
       
  1743     double tmpD = 0;
       
  1744     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1745     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1746     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1747     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1748     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1749     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1750     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1751     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1752     stream >> tmpD; QVERIFY(qIsNaN(tmpD)); tmpD = 0;
       
  1753     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD > 0); tmpD = 0;
       
  1754     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD > 0); tmpD = 0;
       
  1755     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD > 0); tmpD = 0;
       
  1756     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD > 0); tmpD = 0;
       
  1757     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD > 0); tmpD = 0;
       
  1758     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD > 0); tmpD = 0;
       
  1759     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD < 0); tmpD = 0;
       
  1760     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD < 0); tmpD = 0;
       
  1761     stream >> tmpD; QVERIFY(qIsInf(tmpD)); QVERIFY(tmpD < 0); tmpD = 0;
       
  1762 
       
  1763     stream.seek(0);
       
  1764 
       
  1765     float tmpF = 0;
       
  1766     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1767     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1768     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1769     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1770     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1771     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1772     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1773     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1774     stream >> tmpF; QVERIFY(qIsNaN(tmpF)); tmpD = 0;
       
  1775     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF > 0); tmpD = 0;
       
  1776     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF > 0); tmpD = 0;
       
  1777     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF > 0); tmpD = 0;
       
  1778     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF > 0); tmpD = 0;
       
  1779     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF > 0); tmpD = 0;
       
  1780     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF > 0); tmpD = 0;
       
  1781     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF < 0); tmpD = 0;
       
  1782     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF < 0); tmpD = 0;
       
  1783     stream >> tmpF; QVERIFY(qIsInf(tmpF)); QVERIFY(tmpF < 0);
       
  1784 
       
  1785     QString s;
       
  1786     QTextStream out(&s);
       
  1787     out << qInf() << " " << -qInf() << " " << qQNaN()
       
  1788         << uppercasedigits << " "
       
  1789         << qInf() << " " << -qInf() << " " << qQNaN()
       
  1790         << flush;
       
  1791 
       
  1792     QCOMPARE(s, QString("inf -inf nan INF -INF NAN"));
       
  1793 }
       
  1794 
       
  1795 // ------------------------------------------------------------------------------
       
  1796 void tst_QTextStream::utf8IncompleteAtBufferBoundary_data()
       
  1797 {
       
  1798     QTest::addColumn<bool>("useLocale");
       
  1799 
       
  1800     QTest::newRow("utf8") << false;
       
  1801 
       
  1802     // is this locale UTF-8?
       
  1803     if (QString(QChar::ReplacementCharacter).toLocal8Bit() == "\xef\xbf\xbd")
       
  1804         QTest::newRow("locale") << true;
       
  1805 }
       
  1806 
       
  1807 void tst_QTextStream::utf8IncompleteAtBufferBoundary()
       
  1808 {
       
  1809     QFile::remove(TestFileName);
       
  1810     QFile data(TestFileName);
       
  1811 
       
  1812     QTextCodec *utf8Codec = QTextCodec::codecForMib(106);
       
  1813     QString lineContents = QString::fromUtf8("\342\200\223" // U+2013 EN DASH
       
  1814                                              "\342\200\223"
       
  1815                                              "\342\200\223"
       
  1816                                              "\342\200\223"
       
  1817                                              "\342\200\223"
       
  1818                                              "\342\200\223");
       
  1819 
       
  1820     data.open(QFile::WriteOnly | QFile::Truncate);
       
  1821     {
       
  1822         QTextStream out(&data);
       
  1823         out.setCodec(utf8Codec);
       
  1824         out.setFieldWidth(3);
       
  1825 
       
  1826         for (int i = 0; i < 1000; ++i) {
       
  1827             out << i << lineContents << endl;
       
  1828         }
       
  1829     }
       
  1830     data.close();
       
  1831 
       
  1832     data.open(QFile::ReadOnly);
       
  1833     QTextStream in(&data);
       
  1834 
       
  1835     QFETCH(bool, useLocale);
       
  1836     if (!useLocale)
       
  1837         in.setCodec(utf8Codec); // QUtf8Codec
       
  1838     else
       
  1839         in.setCodec(QTextCodec::codecForLocale());
       
  1840 
       
  1841     int i = 0;
       
  1842     do {
       
  1843         QString line = in.readLine().trimmed();
       
  1844         ++i;
       
  1845         QVERIFY2(line.endsWith(lineContents), QString("Line %1: %2").arg(i).arg(line).toLocal8Bit());
       
  1846     } while (!in.atEnd());
       
  1847 }
       
  1848 
       
  1849 // ------------------------------------------------------------------------------
       
  1850 void tst_QTextStream::generateOperatorCharData(bool for_QString)
       
  1851 {
       
  1852     QTest::addColumn<QByteArray>("input");
       
  1853     QTest::addColumn<QChar>("qchar_output");
       
  1854     QTest::addColumn<char>("char_output");
       
  1855     QTest::addColumn<QByteArray>("write_output");
       
  1856 
       
  1857     QTest::newRow("empty") << QByteArray() << QChar('\0') << '\0' << QByteArray("\0", 1);
       
  1858     QTest::newRow("a") << QByteArray("a") << QChar('a') << 'a' << QByteArray("a");
       
  1859     QTest::newRow("\\na") << QByteArray("\na") << QChar('\n') << '\n' << QByteArray("\n");
       
  1860     QTest::newRow("\\0") << QByteArray("\0") << QChar('\0') << '\0' << QByteArray("\0", 1);
       
  1861     QTest::newRow("\\xff") << QByteArray("\xff") << QChar('\xff') << '\xff' << QByteArray("\xff");
       
  1862     QTest::newRow("\\xfe") << QByteArray("\xfe") << QChar('\xfe') << '\xfe' << QByteArray("\xfe");
       
  1863 
       
  1864     if (!for_QString) {
       
  1865         QTest::newRow("utf16-BE (empty)") << QByteArray("\xff\xfe", 2) << QChar('\0') << '\0' << QByteArray("\0", 1);
       
  1866         QTest::newRow("utf16-BE (a)") << QByteArray("\xff\xfe\x61\x00", 4) << QChar('a') << 'a' << QByteArray("a");
       
  1867         QTest::newRow("utf16-LE (empty)") << QByteArray("\xfe\xff", 2) << QChar('\0') << '\0' << QByteArray("\0", 1);
       
  1868         QTest::newRow("utf16-LE (a)") << QByteArray("\xfe\xff\x00\x61", 4) << QChar('a') << 'a' << QByteArray("a");
       
  1869     }
       
  1870 }
       
  1871 
       
  1872 // ------------------------------------------------------------------------------
       
  1873 void tst_QTextStream::QChar_operators_FromDevice_data()
       
  1874 {
       
  1875     generateOperatorCharData(false);
       
  1876 }
       
  1877 
       
  1878 // ------------------------------------------------------------------------------
       
  1879 void tst_QTextStream::QChar_operators_FromDevice()
       
  1880 {
       
  1881     QFETCH(QByteArray, input);
       
  1882     QFETCH(QChar, qchar_output);
       
  1883     QFETCH(QByteArray, write_output);
       
  1884 
       
  1885     QBuffer buf(&input);
       
  1886     buf.open(QBuffer::ReadOnly);
       
  1887     QTextStream stream(&buf);
       
  1888     stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  1889     QChar tmp;
       
  1890     stream >> tmp;
       
  1891     QCOMPARE(tmp, qchar_output);
       
  1892 
       
  1893     QBuffer writeBuf;
       
  1894     writeBuf.open(QBuffer::WriteOnly);
       
  1895 
       
  1896     QTextStream writeStream(&writeBuf);
       
  1897     writeStream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  1898     writeStream << qchar_output;
       
  1899     writeStream.flush();
       
  1900 
       
  1901     QCOMPARE(writeBuf.buffer().size(), write_output.size());
       
  1902     QCOMPARE(writeBuf.buffer().constData(), write_output.constData());
       
  1903 }
       
  1904 
       
  1905 // ------------------------------------------------------------------------------
       
  1906 void tst_QTextStream::char_operators_FromDevice_data()
       
  1907 {
       
  1908     generateOperatorCharData(false);
       
  1909 }
       
  1910 
       
  1911 // ------------------------------------------------------------------------------
       
  1912 void tst_QTextStream::char_operators_FromDevice()
       
  1913 {
       
  1914     QFETCH(QByteArray, input);
       
  1915     QFETCH(char, char_output);
       
  1916     QFETCH(QByteArray, write_output);
       
  1917 
       
  1918     QBuffer buf(&input);
       
  1919     buf.open(QBuffer::ReadOnly);
       
  1920     QTextStream stream(&buf);
       
  1921     stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  1922     char tmp;
       
  1923     stream >> tmp;
       
  1924     QCOMPARE(tmp, char_output);
       
  1925 
       
  1926     QBuffer writeBuf;
       
  1927     writeBuf.open(QBuffer::WriteOnly);
       
  1928 
       
  1929     QTextStream writeStream(&writeBuf);
       
  1930     writeStream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  1931     writeStream << char_output;
       
  1932     writeStream.flush();
       
  1933 
       
  1934     QCOMPARE(writeBuf.buffer().size(), write_output.size());
       
  1935     QCOMPARE(writeBuf.buffer().constData(), write_output.constData());
       
  1936 }
       
  1937 
       
  1938 // ------------------------------------------------------------------------------
       
  1939 void tst_QTextStream::generateNaturalNumbersData(bool for_QString)
       
  1940 {
       
  1941     QTest::addColumn<QByteArray>("input");
       
  1942     QTest::addColumn<qulonglong>("output");
       
  1943 
       
  1944     QTest::newRow("empty") << QByteArray() << qulonglong(0);
       
  1945     QTest::newRow("a") << QByteArray("a") << qulonglong(0);
       
  1946     QTest::newRow(" ") << QByteArray(" ") << qulonglong(0);
       
  1947     QTest::newRow("0") << QByteArray("0") << qulonglong(0);
       
  1948     QTest::newRow("1") << QByteArray("1") << qulonglong(1);
       
  1949     QTest::newRow("12") << QByteArray("12") << qulonglong(12);
       
  1950     QTest::newRow("-12") << QByteArray("-12") << qulonglong(-12);
       
  1951     QTest::newRow("-0") << QByteArray("-0") << qulonglong(0);
       
  1952     QTest::newRow(" 1") << QByteArray(" 1") << qulonglong(1);
       
  1953     QTest::newRow(" \\r\\n\\r\\n123") << QByteArray(" \r\n\r\n123") << qulonglong(123);
       
  1954 
       
  1955     // bit boundary tests
       
  1956     QTest::newRow("127") << QByteArray("127") << qulonglong(127);
       
  1957     QTest::newRow("128") << QByteArray("128") << qulonglong(128);
       
  1958     QTest::newRow("129") << QByteArray("129") << qulonglong(129);
       
  1959     QTest::newRow("-127") << QByteArray("-127") << qulonglong(-127);
       
  1960     QTest::newRow("-128") << QByteArray("-128") << qulonglong(-128);
       
  1961     QTest::newRow("-129") << QByteArray("-129") << qulonglong(-129);
       
  1962     QTest::newRow("32767") << QByteArray("32767") << qulonglong(32767);
       
  1963     QTest::newRow("32768") << QByteArray("32768") << qulonglong(32768);
       
  1964     QTest::newRow("32769") << QByteArray("32769") << qulonglong(32769);
       
  1965     QTest::newRow("-32767") << QByteArray("-32767") << qulonglong(-32767);
       
  1966     QTest::newRow("-32768") << QByteArray("-32768") << qulonglong(-32768);
       
  1967     QTest::newRow("-32769") << QByteArray("-32769") << qulonglong(-32769);
       
  1968     QTest::newRow("65537") << QByteArray("65537") << qulonglong(65537);
       
  1969     QTest::newRow("65536") << QByteArray("65536") << qulonglong(65536);
       
  1970     QTest::newRow("65535") << QByteArray("65535") << qulonglong(65535);
       
  1971     QTest::newRow("-65537") << QByteArray("-65537") << qulonglong(-65537);
       
  1972     QTest::newRow("-65536") << QByteArray("-65536") << qulonglong(-65536);
       
  1973     QTest::newRow("-65535") << QByteArray("-65535") << qulonglong(-65535);
       
  1974     QTest::newRow("2147483646") << QByteArray("2147483646") << qulonglong(2147483646);
       
  1975     QTest::newRow("2147483647") << QByteArray("2147483647") << qulonglong(2147483647);
       
  1976     QTest::newRow("2147483648") << QByteArray("2147483648") << Q_UINT64_C(2147483648);
       
  1977     QTest::newRow("-2147483646") << QByteArray("-2147483646") << qulonglong(-2147483646);
       
  1978     QTest::newRow("-2147483647") << QByteArray("-2147483647") << qulonglong(-2147483647);
       
  1979     QTest::newRow("-2147483648") << QByteArray("-2147483648") << Q_UINT64_C(-2147483648);
       
  1980     QTest::newRow("4294967296") << QByteArray("4294967296") << Q_UINT64_C(4294967296);
       
  1981     QTest::newRow("4294967297") << QByteArray("4294967297") << Q_UINT64_C(4294967297);
       
  1982     QTest::newRow("4294967298") << QByteArray("4294967298") << Q_UINT64_C(4294967298);
       
  1983     QTest::newRow("-4294967296") << QByteArray("-4294967296") << Q_UINT64_C(-4294967296);
       
  1984     QTest::newRow("-4294967297") << QByteArray("-4294967297") << Q_UINT64_C(-4294967297);
       
  1985     QTest::newRow("-4294967298") << QByteArray("-4294967298") << Q_UINT64_C(-4294967298);
       
  1986     QTest::newRow("9223372036854775807") << QByteArray("9223372036854775807") << Q_UINT64_C(9223372036854775807);
       
  1987     QTest::newRow("9223372036854775808") << QByteArray("9223372036854775808") << Q_UINT64_C(9223372036854775808);
       
  1988     QTest::newRow("9223372036854775809") << QByteArray("9223372036854775809") << Q_UINT64_C(9223372036854775809);
       
  1989     QTest::newRow("18446744073709551615") << QByteArray("18446744073709551615") << Q_UINT64_C(18446744073709551615);
       
  1990     QTest::newRow("18446744073709551616") << QByteArray("18446744073709551616") << Q_UINT64_C(0);
       
  1991     QTest::newRow("18446744073709551617") << QByteArray("18446744073709551617") << Q_UINT64_C(1);
       
  1992     // 18446744073709551617 bytes should be enough for anyone.... ;-)
       
  1993 
       
  1994     // hex tests
       
  1995     QTest::newRow("0x0") << QByteArray("0x0") << qulonglong(0);
       
  1996     QTest::newRow("0x") << QByteArray("0x") << qulonglong(0);
       
  1997     QTest::newRow("0x1") << QByteArray("0x1") << qulonglong(1);
       
  1998     QTest::newRow("0xf") << QByteArray("0xf") << qulonglong(15);
       
  1999     QTest::newRow("0xdeadbeef") << QByteArray("0xdeadbeef") << Q_UINT64_C(3735928559);
       
  2000     QTest::newRow("0XDEADBEEF") << QByteArray("0XDEADBEEF") << Q_UINT64_C(3735928559);
       
  2001     QTest::newRow("0xdeadbeefZzzzz") << QByteArray("0xdeadbeefZzzzz") << Q_UINT64_C(3735928559);
       
  2002     QTest::newRow("  0xdeadbeefZzzzz") << QByteArray("  0xdeadbeefZzzzz") << Q_UINT64_C(3735928559);
       
  2003 
       
  2004     // oct tests
       
  2005     QTest::newRow("00") << QByteArray("00") << qulonglong(0);
       
  2006     QTest::newRow("0141") << QByteArray("0141") << qulonglong(97);
       
  2007     QTest::newRow("01419999") << QByteArray("01419999") << qulonglong(97);
       
  2008     QTest::newRow("  01419999") << QByteArray("  01419999") << qulonglong(97);
       
  2009 
       
  2010     // bin tests
       
  2011     QTest::newRow("0b0") << QByteArray("0b0") << qulonglong(0);
       
  2012     QTest::newRow("0b1") << QByteArray("0b1") << qulonglong(1);
       
  2013     QTest::newRow("0b10") << QByteArray("0b10") << qulonglong(2);
       
  2014     QTest::newRow("0B10") << QByteArray("0B10") << qulonglong(2);
       
  2015     QTest::newRow("0b101010") << QByteArray("0b101010") << qulonglong(42);
       
  2016     QTest::newRow("0b1010102345") << QByteArray("0b1010102345") << qulonglong(42);
       
  2017     QTest::newRow("  0b1010102345") << QByteArray("  0b1010102345") << qulonglong(42);
       
  2018 
       
  2019     // utf-16 tests
       
  2020     if (!for_QString) {
       
  2021         QTest::newRow("utf16-BE (empty)") << QByteArray("\xfe\xff", 2) << qulonglong(0);
       
  2022         QTest::newRow("utf16-BE (0xdeadbeef)")
       
  2023             << QByteArray("\xfe\xff"
       
  2024                           "\x00\x30\x00\x78\x00\x64\x00\x65\x00\x61\x00\x64\x00\x62\x00\x65\x00\x65\x00\x66", 22)
       
  2025             << Q_UINT64_C(3735928559);
       
  2026         QTest::newRow("utf16-LE (empty)") << QByteArray("\xff\xfe", 2) << Q_UINT64_C(0);
       
  2027         QTest::newRow("utf16-LE (0xdeadbeef)")
       
  2028             << QByteArray("\xff\xfe"
       
  2029                           "\x30\x00\x78\x00\x64\x00\x65\x00\x61\x00\x64\x00\x62\x00\x65\x00\x65\x00\x66\x00", 22)
       
  2030             << Q_UINT64_C(3735928559);
       
  2031     }
       
  2032 }
       
  2033 
       
  2034 // ------------------------------------------------------------------------------
       
  2035 #define IMPLEMENT_STREAM_RIGHT_INT_OPERATOR_TEST(texttype, type) \
       
  2036     void tst_QTextStream:: texttype##_read_operator_FromDevice_data() \
       
  2037     { generateNaturalNumbersData(false); } \
       
  2038     void tst_QTextStream:: texttype##_read_operator_FromDevice() \
       
  2039     { \
       
  2040         QFETCH(QByteArray, input); \
       
  2041         QFETCH(qulonglong, output); \
       
  2042         type sh; \
       
  2043         QTextStream stream(&input); \
       
  2044         stream >> sh; \
       
  2045         QCOMPARE(sh, (type)output); \
       
  2046     }
       
  2047 IMPLEMENT_STREAM_RIGHT_INT_OPERATOR_TEST(signedShort, signed short)
       
  2048 IMPLEMENT_STREAM_RIGHT_INT_OPERATOR_TEST(unsignedShort, unsigned short)
       
  2049 IMPLEMENT_STREAM_RIGHT_INT_OPERATOR_TEST(signedInt, signed int)
       
  2050 IMPLEMENT_STREAM_RIGHT_INT_OPERATOR_TEST(unsignedInt, unsigned int)
       
  2051 IMPLEMENT_STREAM_RIGHT_INT_OPERATOR_TEST(qlonglong, qlonglong)
       
  2052 IMPLEMENT_STREAM_RIGHT_INT_OPERATOR_TEST(qulonglong, qulonglong)
       
  2053     ;
       
  2054 
       
  2055 // ------------------------------------------------------------------------------
       
  2056 void tst_QTextStream::generateRealNumbersData(bool for_QString)
       
  2057 {
       
  2058     QTest::addColumn<QByteArray>("input");
       
  2059     QTest::addColumn<double>("output");
       
  2060 
       
  2061     QTest::newRow("empty") << QByteArray() << 0.0;
       
  2062     QTest::newRow("a") << QByteArray("a") << 0.0;
       
  2063     QTest::newRow("1.0") << QByteArray("1.0") << 1.0;
       
  2064     QTest::newRow(" 1") << QByteArray(" 1") << 1.0;
       
  2065     QTest::newRow(" \\r\\n1.2") << QByteArray(" \r\n1.2") << 1.2;
       
  2066     QTest::newRow("3.14") << QByteArray("3.14") << 3.14;
       
  2067     QTest::newRow("-3.14") << QByteArray("-3.14") << -3.14;
       
  2068     QTest::newRow(" -3.14") << QByteArray(" -3.14") << -3.14;
       
  2069     QTest::newRow("314e-02") << QByteArray("314e-02") << 3.14;
       
  2070     QTest::newRow("314E-02") << QByteArray("314E-02") << 3.14;
       
  2071     QTest::newRow("314e+02") << QByteArray("314e+02") << 31400.;
       
  2072     QTest::newRow("314E+02") << QByteArray("314E+02") << 31400.;
       
  2073 
       
  2074     // ### add numbers with exponents
       
  2075 
       
  2076     if (!for_QString) {
       
  2077         QTest::newRow("utf16-BE (empty)") << QByteArray("\xff\xfe", 2) << 0.0;
       
  2078         QTest::newRow("utf16-LE (empty)") << QByteArray("\xfe\xff", 2) << 0.0;
       
  2079     }
       
  2080 }
       
  2081 
       
  2082 // ------------------------------------------------------------------------------
       
  2083 #define IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR_TEST(texttype, type) \
       
  2084     void tst_QTextStream:: texttype##_read_operator_FromDevice_data() \
       
  2085     { generateRealNumbersData(false); } \
       
  2086     void tst_QTextStream:: texttype##_read_operator_FromDevice() \
       
  2087     { \
       
  2088         QFETCH(QByteArray, input); \
       
  2089         QFETCH(double, output); \
       
  2090         type sh; \
       
  2091         QTextStream stream(&input); \
       
  2092         stream >> sh; \
       
  2093         QCOMPARE(sh, (type)output); \
       
  2094     }
       
  2095 IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR_TEST(float, float)
       
  2096 IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR_TEST(double, double)
       
  2097     ;
       
  2098 
       
  2099 // ------------------------------------------------------------------------------
       
  2100 void tst_QTextStream::generateStringData(bool for_QString)
       
  2101 {
       
  2102     QTest::addColumn<QByteArray>("input");
       
  2103     QTest::addColumn<QByteArray>("array_output");
       
  2104     QTest::addColumn<QString>("string_output");
       
  2105 
       
  2106     QTest::newRow("empty") << QByteArray() << QByteArray() << QString();
       
  2107     QTest::newRow("a") << QByteArray("a") << QByteArray("a") << QString("a");
       
  2108     QTest::newRow("a b") << QByteArray("a b") << QByteArray("a") << QString("a");
       
  2109     QTest::newRow(" a b") << QByteArray(" a b") << QByteArray("a") << QString("a");
       
  2110     QTest::newRow("a1") << QByteArray("a1") << QByteArray("a1") << QString("a1");
       
  2111     QTest::newRow("a1 b1") << QByteArray("a1 b1") << QByteArray("a1") << QString("a1");
       
  2112     QTest::newRow(" a1 b1") << QByteArray(" a1 b1") << QByteArray("a1") << QString("a1");
       
  2113     QTest::newRow("\\n\\n\\nole i dole\\n") << QByteArray("\n\n\nole i dole\n") << QByteArray("ole") << QString("ole");
       
  2114 
       
  2115     if (!for_QString) {
       
  2116         QTest::newRow("utf16-BE (empty)") << QByteArray("\xff\xfe", 2) << QByteArray() << QString();
       
  2117         QTest::newRow("utf16-BE (corrupt)") << QByteArray("\xff", 1) << QByteArray("\xff") << QString("\xff");
       
  2118         QTest::newRow("utf16-LE (empty)") << QByteArray("\xfe\xff", 2) << QByteArray() << QString();
       
  2119         QTest::newRow("utf16-LE (corrupt)") << QByteArray("\xfe", 1) << QByteArray("\xfe") << QString("\xfe");
       
  2120     }
       
  2121 }
       
  2122 
       
  2123 // ------------------------------------------------------------------------------
       
  2124 void tst_QTextStream::charPtr_read_operator_FromDevice_data()
       
  2125 {
       
  2126     generateStringData(false);
       
  2127 }
       
  2128 
       
  2129 // ------------------------------------------------------------------------------
       
  2130 void tst_QTextStream::charPtr_read_operator_FromDevice()
       
  2131 {
       
  2132     QFETCH(QByteArray, input);
       
  2133     QFETCH(QByteArray, array_output);
       
  2134 
       
  2135     QBuffer buffer(&input);
       
  2136     buffer.open(QBuffer::ReadOnly);
       
  2137     QTextStream stream(&buffer);
       
  2138     stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2139     stream.setAutoDetectUnicode(true);
       
  2140 
       
  2141     char buf[1024];
       
  2142     stream >> buf;
       
  2143 
       
  2144     QCOMPARE((const char *)buf, array_output.constData());
       
  2145 }
       
  2146 
       
  2147 // ------------------------------------------------------------------------------
       
  2148 void tst_QTextStream::stringRef_read_operator_FromDevice_data()
       
  2149 {
       
  2150     generateStringData(false);
       
  2151 }
       
  2152 
       
  2153 // ------------------------------------------------------------------------------
       
  2154 void tst_QTextStream::stringRef_read_operator_FromDevice()
       
  2155 {
       
  2156     QFETCH(QByteArray, input);
       
  2157     QFETCH(QString, string_output);
       
  2158 
       
  2159     QBuffer buffer(&input);
       
  2160     buffer.open(QBuffer::ReadOnly);
       
  2161     QTextStream stream(&buffer);
       
  2162     stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2163     stream.setAutoDetectUnicode(true);
       
  2164 
       
  2165     QString tmp;
       
  2166     stream >> tmp;
       
  2167 
       
  2168     QCOMPARE(tmp, string_output);
       
  2169 }
       
  2170 
       
  2171 // ------------------------------------------------------------------------------
       
  2172 void tst_QTextStream::byteArray_read_operator_FromDevice_data()
       
  2173 {
       
  2174     generateStringData(false);
       
  2175 }
       
  2176 
       
  2177 // ------------------------------------------------------------------------------
       
  2178 void tst_QTextStream::byteArray_read_operator_FromDevice()
       
  2179 {
       
  2180     QFETCH(QByteArray, input);
       
  2181     QFETCH(QByteArray, array_output);
       
  2182 
       
  2183     QBuffer buffer(&input);
       
  2184     buffer.open(QBuffer::ReadOnly);
       
  2185     QTextStream stream(&buffer);
       
  2186     stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2187     stream.setAutoDetectUnicode(true);
       
  2188 
       
  2189     QByteArray array;
       
  2190     stream >> array;
       
  2191 
       
  2192     QCOMPARE(array, array_output);
       
  2193 }
       
  2194 
       
  2195 // ------------------------------------------------------------------------------
       
  2196 #define IMPLEMENT_STREAM_LEFT_INT_OPERATOR_TEST(texttype, type) \
       
  2197     void tst_QTextStream:: texttype##_write_operator_ToDevice() \
       
  2198     { \
       
  2199         QFETCH(qulonglong, number); \
       
  2200         QFETCH(QByteArray, data); \
       
  2201         \
       
  2202         QBuffer buffer; \
       
  2203         buffer.open(QBuffer::WriteOnly); \
       
  2204         QTextStream stream(&buffer); \
       
  2205         stream << (type)number; \
       
  2206         stream.flush(); \
       
  2207         \
       
  2208         QCOMPARE(buffer.data().constData(), data.constData()); \
       
  2209     }
       
  2210 
       
  2211 // ------------------------------------------------------------------------------
       
  2212 void tst_QTextStream::signedShort_write_operator_ToDevice_data()
       
  2213 {
       
  2214     QTest::addColumn<qulonglong>("number");
       
  2215     QTest::addColumn<QByteArray>("data");
       
  2216 
       
  2217     QTest::newRow("0") << Q_UINT64_C(0) << QByteArray("0");
       
  2218     QTest::newRow("1") << Q_UINT64_C(1) << QByteArray("1");
       
  2219     QTest::newRow("-1") << Q_UINT64_C(-1) << QByteArray("-1");
       
  2220     QTest::newRow("32767") << Q_UINT64_C(32767) << QByteArray("32767");
       
  2221     QTest::newRow("32768") << Q_UINT64_C(32768) << QByteArray("-32768");
       
  2222     QTest::newRow("32769") << Q_UINT64_C(32769) << QByteArray("-32767");
       
  2223     QTest::newRow("65535") << Q_UINT64_C(65535) << QByteArray("-1");
       
  2224     QTest::newRow("65536") << Q_UINT64_C(65536) << QByteArray("0");
       
  2225     QTest::newRow("65537") << Q_UINT64_C(65537) << QByteArray("1");
       
  2226 }
       
  2227 IMPLEMENT_STREAM_LEFT_INT_OPERATOR_TEST(signedShort, signed short)
       
  2228     ;
       
  2229 
       
  2230 // ------------------------------------------------------------------------------
       
  2231 void tst_QTextStream::unsignedShort_write_operator_ToDevice_data()
       
  2232 {
       
  2233     QTest::addColumn<qulonglong>("number");
       
  2234     QTest::addColumn<QByteArray>("data");
       
  2235 
       
  2236     QTest::newRow("0") << Q_UINT64_C(0) << QByteArray("0");
       
  2237     QTest::newRow("1") << Q_UINT64_C(1) << QByteArray("1");
       
  2238     QTest::newRow("-1") << Q_UINT64_C(-1) << QByteArray("65535");
       
  2239     QTest::newRow("32767") << Q_UINT64_C(32767) << QByteArray("32767");
       
  2240     QTest::newRow("32768") << Q_UINT64_C(32768) << QByteArray("32768");
       
  2241     QTest::newRow("32769") << Q_UINT64_C(32769) << QByteArray("32769");
       
  2242     QTest::newRow("65535") << Q_UINT64_C(65535) << QByteArray("65535");
       
  2243     QTest::newRow("65536") << Q_UINT64_C(65536) << QByteArray("0");
       
  2244     QTest::newRow("65537") << Q_UINT64_C(65537) << QByteArray("1");
       
  2245 }
       
  2246 IMPLEMENT_STREAM_LEFT_INT_OPERATOR_TEST(unsignedShort, unsigned short)
       
  2247     ;
       
  2248 
       
  2249 // ------------------------------------------------------------------------------
       
  2250 void tst_QTextStream::signedInt_write_operator_ToDevice_data()
       
  2251 {
       
  2252     QTest::addColumn<qulonglong>("number");
       
  2253     QTest::addColumn<QByteArray>("data");
       
  2254 
       
  2255     QTest::newRow("0") << Q_UINT64_C(0) << QByteArray("0");
       
  2256     QTest::newRow("1") << Q_UINT64_C(1) << QByteArray("1");
       
  2257     QTest::newRow("-1") << Q_UINT64_C(-1) << QByteArray("-1");
       
  2258     QTest::newRow("32767") << Q_UINT64_C(32767) << QByteArray("32767");
       
  2259     QTest::newRow("32768") << Q_UINT64_C(32768) << QByteArray("32768");
       
  2260     QTest::newRow("32769") << Q_UINT64_C(32769) << QByteArray("32769");
       
  2261     QTest::newRow("65535") << Q_UINT64_C(65535) << QByteArray("65535");
       
  2262     QTest::newRow("65536") << Q_UINT64_C(65536) << QByteArray("65536");
       
  2263     QTest::newRow("65537") << Q_UINT64_C(65537) << QByteArray("65537");
       
  2264     QTest::newRow("2147483647") << Q_UINT64_C(2147483647) << QByteArray("2147483647");
       
  2265     QTest::newRow("2147483648") << Q_UINT64_C(2147483648) << QByteArray("-2147483648");
       
  2266     QTest::newRow("2147483649") << Q_UINT64_C(2147483649) << QByteArray("-2147483647");
       
  2267     QTest::newRow("4294967295") << Q_UINT64_C(4294967295) << QByteArray("-1");
       
  2268     QTest::newRow("4294967296") << Q_UINT64_C(4294967296) << QByteArray("0");
       
  2269     QTest::newRow("4294967297") << Q_UINT64_C(4294967297) << QByteArray("1");
       
  2270 }
       
  2271 IMPLEMENT_STREAM_LEFT_INT_OPERATOR_TEST(signedInt, signed int)
       
  2272     ;
       
  2273 
       
  2274 // ------------------------------------------------------------------------------
       
  2275 void tst_QTextStream::unsignedInt_write_operator_ToDevice_data()
       
  2276 {
       
  2277     QTest::addColumn<qulonglong>("number");
       
  2278     QTest::addColumn<QByteArray>("data");
       
  2279 
       
  2280     QTest::newRow("0") << Q_UINT64_C(0) << QByteArray("0");
       
  2281     QTest::newRow("1") << Q_UINT64_C(1) << QByteArray("1");
       
  2282     QTest::newRow("-1") << Q_UINT64_C(-1) << QByteArray("4294967295");
       
  2283     QTest::newRow("32767") << Q_UINT64_C(32767) << QByteArray("32767");
       
  2284     QTest::newRow("32768") << Q_UINT64_C(32768) << QByteArray("32768");
       
  2285     QTest::newRow("32769") << Q_UINT64_C(32769) << QByteArray("32769");
       
  2286     QTest::newRow("65535") << Q_UINT64_C(65535) << QByteArray("65535");
       
  2287     QTest::newRow("65536") << Q_UINT64_C(65536) << QByteArray("65536");
       
  2288     QTest::newRow("65537") << Q_UINT64_C(65537) << QByteArray("65537");
       
  2289     QTest::newRow("2147483647") << Q_UINT64_C(2147483647) << QByteArray("2147483647");
       
  2290     QTest::newRow("2147483648") << Q_UINT64_C(2147483648) << QByteArray("2147483648");
       
  2291     QTest::newRow("2147483649") << Q_UINT64_C(2147483649) << QByteArray("2147483649");
       
  2292     QTest::newRow("4294967295") << Q_UINT64_C(4294967295) << QByteArray("4294967295");
       
  2293     QTest::newRow("4294967296") << Q_UINT64_C(4294967296) << QByteArray("0");
       
  2294     QTest::newRow("4294967297") << Q_UINT64_C(4294967297) << QByteArray("1");
       
  2295 }
       
  2296 IMPLEMENT_STREAM_LEFT_INT_OPERATOR_TEST(unsignedInt, unsigned int)
       
  2297     ;
       
  2298 
       
  2299 // ------------------------------------------------------------------------------
       
  2300 void tst_QTextStream::qlonglong_write_operator_ToDevice_data()
       
  2301 {
       
  2302     QTest::addColumn<qulonglong>("number");
       
  2303     QTest::addColumn<QByteArray>("data");
       
  2304 
       
  2305     QTest::newRow("0") << Q_UINT64_C(0) << QByteArray("0");
       
  2306     QTest::newRow("1") << Q_UINT64_C(1) << QByteArray("1");
       
  2307     QTest::newRow("-1") << Q_UINT64_C(-1) << QByteArray("-1");
       
  2308     QTest::newRow("32767") << Q_UINT64_C(32767) << QByteArray("32767");
       
  2309     QTest::newRow("32768") << Q_UINT64_C(32768) << QByteArray("32768");
       
  2310     QTest::newRow("32769") << Q_UINT64_C(32769) << QByteArray("32769");
       
  2311     QTest::newRow("65535") << Q_UINT64_C(65535) << QByteArray("65535");
       
  2312     QTest::newRow("65536") << Q_UINT64_C(65536) << QByteArray("65536");
       
  2313     QTest::newRow("65537") << Q_UINT64_C(65537) << QByteArray("65537");
       
  2314     QTest::newRow("2147483647") << Q_UINT64_C(2147483647) << QByteArray("2147483647");
       
  2315     QTest::newRow("2147483648") << Q_UINT64_C(2147483648) << QByteArray("2147483648");
       
  2316     QTest::newRow("2147483649") << Q_UINT64_C(2147483649) << QByteArray("2147483649");
       
  2317     QTest::newRow("4294967295") << Q_UINT64_C(4294967295) << QByteArray("4294967295");
       
  2318     QTest::newRow("4294967296") << Q_UINT64_C(4294967296) << QByteArray("4294967296");
       
  2319     QTest::newRow("4294967297") << Q_UINT64_C(4294967297) << QByteArray("4294967297");
       
  2320     QTest::newRow("9223372036854775807") << Q_UINT64_C(9223372036854775807) << QByteArray("9223372036854775807");
       
  2321     QTest::newRow("9223372036854775808") << Q_UINT64_C(9223372036854775808) << QByteArray("-9223372036854775808");
       
  2322     QTest::newRow("9223372036854775809") << Q_UINT64_C(9223372036854775809) << QByteArray("-9223372036854775807");
       
  2323     QTest::newRow("18446744073709551615") << Q_UINT64_C(18446744073709551615) << QByteArray("-1");
       
  2324 }
       
  2325 IMPLEMENT_STREAM_LEFT_INT_OPERATOR_TEST(qlonglong, qlonglong)
       
  2326     ;
       
  2327 
       
  2328 // ------------------------------------------------------------------------------
       
  2329 void tst_QTextStream::qulonglong_write_operator_ToDevice_data()
       
  2330 {
       
  2331     QTest::addColumn<qulonglong>("number");
       
  2332     QTest::addColumn<QByteArray>("data");
       
  2333 
       
  2334     QTest::newRow("0") << Q_UINT64_C(0) << QByteArray("0");
       
  2335     QTest::newRow("1") << Q_UINT64_C(1) << QByteArray("1");
       
  2336     QTest::newRow("-1") << Q_UINT64_C(-1) << QByteArray("18446744073709551615");
       
  2337     QTest::newRow("32767") << Q_UINT64_C(32767) << QByteArray("32767");
       
  2338     QTest::newRow("32768") << Q_UINT64_C(32768) << QByteArray("32768");
       
  2339     QTest::newRow("32769") << Q_UINT64_C(32769) << QByteArray("32769");
       
  2340     QTest::newRow("65535") << Q_UINT64_C(65535) << QByteArray("65535");
       
  2341     QTest::newRow("65536") << Q_UINT64_C(65536) << QByteArray("65536");
       
  2342     QTest::newRow("65537") << Q_UINT64_C(65537) << QByteArray("65537");
       
  2343     QTest::newRow("2147483647") << Q_UINT64_C(2147483647) << QByteArray("2147483647");
       
  2344     QTest::newRow("2147483648") << Q_UINT64_C(2147483648) << QByteArray("2147483648");
       
  2345     QTest::newRow("2147483649") << Q_UINT64_C(2147483649) << QByteArray("2147483649");
       
  2346     QTest::newRow("4294967295") << Q_UINT64_C(4294967295) << QByteArray("4294967295");
       
  2347     QTest::newRow("4294967296") << Q_UINT64_C(4294967296) << QByteArray("4294967296");
       
  2348     QTest::newRow("4294967297") << Q_UINT64_C(4294967297) << QByteArray("4294967297");
       
  2349     QTest::newRow("9223372036854775807") << Q_UINT64_C(9223372036854775807) << QByteArray("9223372036854775807");
       
  2350     QTest::newRow("9223372036854775808") << Q_UINT64_C(9223372036854775808) << QByteArray("9223372036854775808");
       
  2351     QTest::newRow("9223372036854775809") << Q_UINT64_C(9223372036854775809) << QByteArray("9223372036854775809");
       
  2352     QTest::newRow("18446744073709551615") << Q_UINT64_C(18446744073709551615) << QByteArray("18446744073709551615");
       
  2353 }
       
  2354 IMPLEMENT_STREAM_LEFT_INT_OPERATOR_TEST(qulonglong, qulonglong)
       
  2355     ;
       
  2356 
       
  2357 
       
  2358 // ------------------------------------------------------------------------------
       
  2359 void tst_QTextStream::generateRealNumbersDataWrite()
       
  2360 {
       
  2361     QTest::addColumn<double>("number");
       
  2362     QTest::addColumn<QByteArray>("data");
       
  2363 
       
  2364     QTest::newRow("0") << 0.0 << QByteArray("0");
       
  2365     QTest::newRow("3.14") << 3.14 << QByteArray("3.14");
       
  2366     QTest::newRow("-3.14") << -3.14 << QByteArray("-3.14");
       
  2367     QTest::newRow("1.2e+10") << 1.2e+10 << QByteArray("1.2e+10");
       
  2368     QTest::newRow("-1.2e+10") << -1.2e+10 << QByteArray("-1.2e+10");
       
  2369 }
       
  2370 
       
  2371 // ------------------------------------------------------------------------------
       
  2372 #define IMPLEMENT_STREAM_LEFT_REAL_OPERATOR_TEST(texttype, type) \
       
  2373     void tst_QTextStream:: texttype##_write_operator_ToDevice_data() \
       
  2374     { generateRealNumbersDataWrite(); } \
       
  2375     void tst_QTextStream:: texttype##_write_operator_ToDevice() \
       
  2376     { \
       
  2377         QFETCH(double, number); \
       
  2378         QFETCH(QByteArray, data); \
       
  2379         \
       
  2380         QBuffer buffer; \
       
  2381         buffer.open(QBuffer::WriteOnly); \
       
  2382         QTextStream stream(&buffer); \
       
  2383         float f = (float)number; \
       
  2384         stream << f; \
       
  2385         stream.flush(); \
       
  2386         QCOMPARE(buffer.data().constData(), data.constData()); \
       
  2387     }
       
  2388 IMPLEMENT_STREAM_LEFT_REAL_OPERATOR_TEST(float, float)
       
  2389 IMPLEMENT_STREAM_LEFT_REAL_OPERATOR_TEST(double, float)
       
  2390     ;
       
  2391 
       
  2392 // ------------------------------------------------------------------------------
       
  2393 void tst_QTextStream::string_write_operator_ToDevice_data()
       
  2394 {
       
  2395     QTest::addColumn<QByteArray>("bytedata");
       
  2396     QTest::addColumn<QString>("stringdata");
       
  2397     QTest::addColumn<QByteArray>("result");
       
  2398 
       
  2399     QTest::newRow("empty") << QByteArray("", 1) << QString(1, '\0') << QByteArray("", 1);
       
  2400     QTest::newRow("a") << QByteArray("a") << QString("a") << QByteArray("a");
       
  2401     QTest::newRow("a cow jumped over the moon")
       
  2402         << QByteArray("a cow jumped over the moon")
       
  2403         << QString("a cow jumped over the moon")
       
  2404         << QByteArray("a cow jumped over the moon");
       
  2405 
       
  2406     // ### get the utf16-be test on its legs.
       
  2407     /*
       
  2408     QTest::newRow("utf16-BE (a cow jumped over the moon)")
       
  2409         << QByteArray("\xff\xfe\x00\x61\x00\x20\x00\x63\x00\x6f\x00\x77\x00\x20\x00\x6a\x00\x75\x00\x6d\x00\x70\x00\x65\x00\x64\x00\x20\x00\x6f\x00\x76\x00\x65\x00\x72\x00\x20\x00\x74\x00\x68\x00\x65\x00\x20\x00\x6d\x00\x6f\x00\x6f\x00\x6e\x00\x0a", 56)
       
  2410         << QString("a cow jumped over the moon")
       
  2411         << QByteArray("a cow jumped over the moon");
       
  2412     */
       
  2413 }
       
  2414 
       
  2415 // ------------------------------------------------------------------------------
       
  2416 void tst_QTextStream::string_write_operator_ToDevice()
       
  2417 {
       
  2418     QFETCH(QByteArray, bytedata);
       
  2419     QFETCH(QString, stringdata);
       
  2420     QFETCH(QByteArray, result);
       
  2421 
       
  2422     {
       
  2423         // char*
       
  2424         QBuffer buf;
       
  2425         buf.open(QBuffer::WriteOnly);
       
  2426         QTextStream stream(&buf);
       
  2427         stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2428         stream.setAutoDetectUnicode(true);
       
  2429 
       
  2430         stream << bytedata.constData();
       
  2431         stream.flush();
       
  2432         QCOMPARE(buf.buffer().constData(), result.constData());
       
  2433     }
       
  2434     {
       
  2435         // QByteArray
       
  2436         QBuffer buf;
       
  2437         buf.open(QBuffer::WriteOnly);
       
  2438         QTextStream stream(&buf);
       
  2439         stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2440         stream.setAutoDetectUnicode(true);
       
  2441 
       
  2442         stream << bytedata;
       
  2443         stream.flush();
       
  2444         QCOMPARE(buf.buffer().constData(), result.constData());
       
  2445     }
       
  2446     {
       
  2447         // QString
       
  2448         QBuffer buf;
       
  2449         buf.open(QBuffer::WriteOnly);
       
  2450         QTextStream stream(&buf);
       
  2451         stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2452         stream.setAutoDetectUnicode(true);
       
  2453 
       
  2454         stream << stringdata;
       
  2455         stream.flush();
       
  2456         QCOMPARE(buf.buffer().constData(), result.constData());
       
  2457     }
       
  2458 }
       
  2459 
       
  2460 // ------------------------------------------------------------------------------
       
  2461 void tst_QTextStream::useCase1()
       
  2462 {
       
  2463     QFile::remove("testfile");
       
  2464     QFile file("testfile");
       
  2465     QVERIFY(file.open(QFile::ReadWrite));
       
  2466 
       
  2467     {
       
  2468         QTextStream stream(&file);
       
  2469         stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2470         stream.setAutoDetectUnicode(true);
       
  2471 
       
  2472         stream << 4.15 << " " << QByteArray("abc") << " " << QString("ole");
       
  2473     }
       
  2474 
       
  2475     file.seek(0);
       
  2476     QCOMPARE(file.readAll(), QByteArray("4.15 abc ole"));
       
  2477     file.seek(0);
       
  2478 
       
  2479     {
       
  2480         double d;
       
  2481         QByteArray a;
       
  2482         QString s;
       
  2483         QTextStream stream(&file);
       
  2484         stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2485         stream.setAutoDetectUnicode(true);
       
  2486 
       
  2487         stream >> d;
       
  2488         stream >> a;
       
  2489         stream >> s;
       
  2490 
       
  2491         QCOMPARE(d, 4.15);
       
  2492         QCOMPARE(a, QByteArray("abc"));
       
  2493         QCOMPARE(s, QString("ole"));
       
  2494     }
       
  2495 }
       
  2496 
       
  2497 // ------------------------------------------------------------------------------
       
  2498 void tst_QTextStream::useCase2()
       
  2499 {
       
  2500     QFile::remove("testfile");
       
  2501     QFile file("testfile");
       
  2502     QVERIFY(file.open(QFile::ReadWrite));
       
  2503 
       
  2504     QTextStream stream(&file);
       
  2505     stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2506     stream.setAutoDetectUnicode(true);
       
  2507 
       
  2508     stream << 4.15 << " " << QByteArray("abc") << " " << QString("ole");
       
  2509 
       
  2510     file.close();
       
  2511     QVERIFY(file.open(QFile::ReadWrite));
       
  2512 
       
  2513     QCOMPARE(file.readAll(), QByteArray("4.15 abc ole"));
       
  2514 
       
  2515     file.close();
       
  2516     QVERIFY(file.open(QFile::ReadWrite));
       
  2517 
       
  2518     double d;
       
  2519     QByteArray a;
       
  2520     QString s;
       
  2521     QTextStream stream2(&file);
       
  2522     stream2.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2523     stream2.setAutoDetectUnicode(true);
       
  2524 
       
  2525     stream2 >> d;
       
  2526     stream2 >> a;
       
  2527     stream2 >> s;
       
  2528 
       
  2529     QCOMPARE(d, 4.15);
       
  2530     QCOMPARE(a, QByteArray("abc"));
       
  2531     QCOMPARE(s, QString("ole"));
       
  2532 }
       
  2533 
       
  2534 // ------------------------------------------------------------------------------
       
  2535 void tst_QTextStream::manipulators_data()
       
  2536 {
       
  2537     QTest::addColumn<int>("flags");
       
  2538     QTest::addColumn<int>("width");
       
  2539     QTest::addColumn<double>("realNumber");
       
  2540     QTest::addColumn<int>("intNumber");
       
  2541     QTest::addColumn<QString>("textData");
       
  2542     QTest::addColumn<QByteArray>("result");
       
  2543 
       
  2544     QTest::newRow("no flags") << 0 << 0  << 5.0 << 5 << QString("five") << QByteArray("55five");
       
  2545     QTest::newRow("rightadjust") << 0 << 10 << 5.0 << 5 << QString("five") << QByteArray("         5         5      five");
       
  2546 
       
  2547     // ### FIX
       
  2548 //    QTest::newRow("leftadjust") << int(QTextStream::left) << 10 << 5.0 << 5 << QString("five") << QByteArray("5         5         five      ");
       
  2549 //    QTest::newRow("showpos") << int(QTextStream::showpos) << 10 << 5.0 << 5 << QString("five") << QByteArray("        +5        +5      five");
       
  2550 //    QTest::newRow("showpos2") << int(QTextStream::showpos) << 5 << 3.14 << -5 << QString("five") << QByteArray("+3.14   -5 five");
       
  2551 //    QTest::newRow("hex") << int(QTextStream::hex | QTextStream::showbase) << 5 << 3.14 << -5 << QString("five") << QByteArray(" 3.14 -0x5 five");
       
  2552 //    QTest::newRow("hex uppercase") << int(QTextStream::hex | QTextStream::uppercase | QTextStream::showbase) << 5 << 3.14 << -5 << QString("five") << QByteArray(" 3.14 -0X5 five");
       
  2553 }
       
  2554 
       
  2555 // ------------------------------------------------------------------------------
       
  2556 void tst_QTextStream::manipulators()
       
  2557 {
       
  2558 //    QFETCH(int, flags);
       
  2559     QFETCH(int, width);
       
  2560     QFETCH(double, realNumber);
       
  2561     QFETCH(int, intNumber);
       
  2562     QFETCH(QString, textData);
       
  2563     QFETCH(QByteArray, result);
       
  2564 
       
  2565     QBuffer buffer;
       
  2566     buffer.open(QBuffer::WriteOnly);
       
  2567 
       
  2568     QTextStream stream(&buffer);
       
  2569     stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2570     stream.setAutoDetectUnicode(true);
       
  2571 
       
  2572 //    stream.setFlags(flags);
       
  2573     stream.setFieldWidth(width);
       
  2574     stream << realNumber;
       
  2575     stream << intNumber;
       
  2576     stream << textData;
       
  2577     stream.flush();
       
  2578 
       
  2579     QCOMPARE(buffer.data().constData(), result.constData());
       
  2580 }
       
  2581 
       
  2582 void tst_QTextStream::generateBOM()
       
  2583 {
       
  2584     QFile::remove("bom.txt");
       
  2585     {
       
  2586         QFile file("bom.txt");
       
  2587         QVERIFY(file.open(QFile::ReadWrite | QFile::Truncate));
       
  2588 
       
  2589         QTextStream stream(&file);
       
  2590         stream.setCodec(QTextCodec::codecForName("UTF-16LE"));
       
  2591         stream << "Hello" << endl;
       
  2592 
       
  2593         file.close();
       
  2594         QVERIFY(file.open(QFile::ReadOnly));
       
  2595         QCOMPARE(file.readAll(), QByteArray("\x48\x00\x65\00\x6c\00\x6c\00\x6f\x00\x0a\x00", 12));
       
  2596     }
       
  2597 
       
  2598     QFile::remove("bom.txt");
       
  2599     {
       
  2600         QFile file("bom.txt");
       
  2601         QVERIFY(file.open(QFile::ReadWrite | QFile::Truncate));
       
  2602 
       
  2603         QTextStream stream(&file);
       
  2604         stream.setCodec(QTextCodec::codecForName("UTF-16LE"));
       
  2605         stream << bom << "Hello" << endl;
       
  2606 
       
  2607         file.close();
       
  2608         QVERIFY(file.open(QFile::ReadOnly));
       
  2609         QCOMPARE(file.readAll(), QByteArray("\xff\xfe\x48\x00\x65\00\x6c\00\x6c\00\x6f\x00\x0a\x00", 14));
       
  2610     }
       
  2611 }
       
  2612 
       
  2613 void tst_QTextStream::readBomSeekBackReadBomAgain()
       
  2614 {
       
  2615     QFile::remove("utf8bom");
       
  2616     QFile file("utf8bom");
       
  2617     QVERIFY(file.open(QFile::ReadWrite));
       
  2618     file.write("\xef\xbb\xbf""Andreas");
       
  2619     file.seek(0);
       
  2620     QCOMPARE(file.pos(), qint64(0));
       
  2621 
       
  2622     QTextStream stream(&file);
       
  2623     stream.setCodec("UTF-8");
       
  2624     QString Andreas;
       
  2625     stream >> Andreas;
       
  2626     QCOMPARE(Andreas, QString("Andreas"));
       
  2627     stream.seek(0);
       
  2628     stream >> Andreas;
       
  2629     QCOMPARE(Andreas, QString("Andreas"));
       
  2630 }
       
  2631 
       
  2632 // ------------------------------------------------------------------------------
       
  2633 // QT 3 tests
       
  2634 // ------------------------------------------------------------------------------
       
  2635 #ifdef QT3_SUPPORT
       
  2636 
       
  2637 void tst_QTextStream::qt3_readLineFromString()
       
  2638 {
       
  2639     QString data = "line 1e\nline 2e\nline 3e";
       
  2640     QStringList list;
       
  2641     QTextStream stream(&data, QIODevice::ReadOnly);
       
  2642     stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  2643     stream.setAutoDetectUnicode(true);
       
  2644 
       
  2645     while (!stream.atEnd()) {
       
  2646         QString line = stream.readLine();
       
  2647         QCOMPARE(QChar(line[line.size()-1]), QChar('e'));
       
  2648         list << line;
       
  2649     }
       
  2650     QVERIFY(list.count() == 3);
       
  2651 }
       
  2652 
       
  2653 // ------------------------------------------------------------------------------
       
  2654 void tst_QTextStream::qt3_operator_shiftright_data()
       
  2655 {
       
  2656     qt3_operatorShift_data( QIODevice::ReadOnly );
       
  2657 }
       
  2658 
       
  2659 // ------------------------------------------------------------------------------
       
  2660 void tst_QTextStream::qt3_operator_shiftleft_data()
       
  2661 {
       
  2662     qt3_operatorShift_data( QIODevice::WriteOnly );
       
  2663 }
       
  2664 
       
  2665 static const double doubleDummy = 567.89;
       
  2666 static const int intDummy = 1234;
       
  2667 static const QString stringDummy = "ABCD";
       
  2668 
       
  2669 struct tst_IODevice {
       
  2670     char *n;
       
  2671     QIODevice::OpenModeFlag modes;
       
  2672     QString name() const { return QString::fromLatin1(n); }
       
  2673 } devices[] = {
       
  2674     { "file", QIODevice::ReadWrite },
       
  2675     { "bytearray", QIODevice::ReadWrite },
       
  2676     { "buffer", QIODevice::ReadWrite },
       
  2677     { "string", QIODevice::ReadWrite },
       
  2678     { "resource", QIODevice::ReadOnly }, //See discussion (resources) below
       
  2679     { 0, (QIODevice::OpenModeFlag)0 }
       
  2680 };
       
  2681 
       
  2682 /* Resources:
       
  2683 
       
  2684    This will test QTextStream's ability to interact with resources,
       
  2685    however the trouble is the resources must be created correctly and
       
  2686    built into the executable (but this executable also creates the
       
  2687    resource files). So there is a chicken and egg problem, to create
       
  2688    resources (if the file formats change at all):
       
  2689 
       
  2690    1) p4 edit resources/...
       
  2691 
       
  2692    2) Change QIODevice::ReadOnly above in the devices[] for "resource"
       
  2693       to QIODevice::WriteOnly
       
  2694 
       
  2695    3) run the test
       
  2696 
       
  2697    4) revert this file, qmake, make, and run again.
       
  2698 
       
  2699    5) Enjoy.
       
  2700 */
       
  2701 
       
  2702 static inline QString resourceDir()
       
  2703 {
       
  2704     QString ret = "resources/";
       
  2705     if(QSysInfo::ByteOrder == QSysInfo::BigEndian)
       
  2706         ret += "big_endian/";
       
  2707     else
       
  2708         ret += "little_endian/";
       
  2709     return ret;
       
  2710 }
       
  2711 
       
  2712 static const char *const encodings[] = {
       
  2713     "Locale",
       
  2714     "Latin1",
       
  2715     "Unicode",
       
  2716     "UnicodeNetworkOrder",
       
  2717     "UnicodeReverse",
       
  2718     "RawUnicode",
       
  2719     "UnicodeUTF8",
       
  2720     0
       
  2721 };
       
  2722 
       
  2723 // ------------------------------------------------------------------------------
       
  2724 void tst_QTextStream::qt3_operatorShift_data( QIODevice::OpenMode mode )
       
  2725 {
       
  2726     QTest::addColumn<QString>("device");
       
  2727     QTest::addColumn<QString>("encoding");
       
  2728     QTest::addColumn<QString>("type");
       
  2729     QTest::addColumn<double>("doubleVal");
       
  2730     QTest::addColumn<int>("intVal");
       
  2731     QTest::addColumn<QString>("stringVal");
       
  2732     QTest::addColumn<QByteArray>("encoded");
       
  2733 
       
  2734     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  2735         tst_IODevice *device = devices+i;
       
  2736         if(!(device->modes & mode))
       
  2737             continue;
       
  2738 
       
  2739 	/*
       
  2740 	  We first test each type at least once.
       
  2741 	*/
       
  2742 	QTest::newRow( device->name() + "0" ) << device->name() << QString("UnicodeUTF8") << QString("QChar")
       
  2743 		<< doubleDummy << (int) 'Z' << stringDummy
       
  2744 		<< QByteArray( QByteArray("Z") );
       
  2745 	QTest::newRow( device->name() + "1" ) << device->name() << QString("UnicodeUTF8") << QString("char")
       
  2746 		<< doubleDummy << (int) 'Z' << stringDummy
       
  2747 		<< QByteArray( QByteArray("Z") );
       
  2748 	QTest::newRow( device->name() + "2" ) << device->name() << QString("UnicodeUTF8") << QString("signed short")
       
  2749 		<< doubleDummy << 12345 << stringDummy
       
  2750 		<< QByteArray( QByteArray("12345") );
       
  2751 	QTest::newRow( device->name() + "3" ) << device->name() << QString("UnicodeUTF8") << QString("unsigned short")
       
  2752 		<< doubleDummy << 12345 << stringDummy
       
  2753 		<< QByteArray( QByteArray("12345") );
       
  2754 	QTest::newRow( device->name() + "4" ) << device->name() << QString("UnicodeUTF8") << QString("signed int")
       
  2755 		<< doubleDummy << 12345 << stringDummy
       
  2756 		<< QByteArray( QByteArray("12345") );
       
  2757 	QTest::newRow( device->name() + "5" ) << device->name() << QString("UnicodeUTF8") << QString("unsigned int")
       
  2758 		<< doubleDummy << 12345 << stringDummy
       
  2759 		<< QByteArray( QByteArray("12345") );
       
  2760 	QTest::newRow( device->name() + "6" ) << device->name() << QString("UnicodeUTF8") << QString("signed long")
       
  2761 		<< doubleDummy << 12345 << stringDummy
       
  2762 		<< QByteArray( QByteArray("12345") );
       
  2763 	QTest::newRow( device->name() + "7" ) << device->name() << QString("UnicodeUTF8") << QString("unsigned long")
       
  2764 		<< doubleDummy << 12345 << stringDummy
       
  2765 		<< QByteArray( QByteArray("12345") );
       
  2766 	QTest::newRow( device->name() + "8" ) << device->name() << QString("UnicodeUTF8") << QString("float")
       
  2767 		<< (double)3.1415f << intDummy << stringDummy
       
  2768 		<< QByteArray( QByteArray("3.1415") );
       
  2769 	QTest::newRow( device->name() + "9" ) << device->name() << QString("UnicodeUTF8") << QString("double")
       
  2770 		<< 3.1415 << intDummy << stringDummy
       
  2771 		<< QByteArray( QByteArray("3.1415") );
       
  2772 	QTest::newRow( device->name() + "10" ) << device->name() << QString("UnicodeUTF8") << QString("char *")
       
  2773 		<< doubleDummy << intDummy << QString("I-am-a-string")
       
  2774 		<< QByteArray( QByteArray("I-am-a-string") );
       
  2775 	QTest::newRow( device->name() + "11" ) << device->name() << QString("UnicodeUTF8") << QString("QString")
       
  2776 		<< doubleDummy << intDummy << QString("I-am-a-string")
       
  2777 		<< QByteArray( QByteArray("I-am-a-string") );
       
  2778 	QTest::newRow( device->name() + "12" ) << device->name() << QString("UnicodeUTF8") << QString("QByteArray")
       
  2779 		<< doubleDummy << intDummy << QString("I-am-a-string")
       
  2780 		<< QByteArray( QByteArray("I-am-a-string") );
       
  2781 
       
  2782 	/*
       
  2783 	  Then we test some special cases that have caused problems in the past.
       
  2784 	*/
       
  2785 	QTest::newRow( device->name() + "20" ) << device->name() << QString("UnicodeUTF8") << QString("QChar")
       
  2786 		<< doubleDummy << 0xff8c << stringDummy
       
  2787 		<< QByteArray( QByteArray("\xef\xbe\x8c") );
       
  2788 	QTest::newRow( device->name() + "21" ) << device->name() << QString("UnicodeUTF8") << QString("QChar")
       
  2789 		<< doubleDummy << 0x8cff << stringDummy
       
  2790 		<< QByteArray( QByteArray("\xe8\xb3\xbf") );
       
  2791     }
       
  2792 }
       
  2793 
       
  2794 // ------------------------------------------------------------------------------
       
  2795 void tst_QTextStream::qt3_do_shiftleft( QTextStream *ts )
       
  2796 {
       
  2797     QFETCH( QString, encoding );
       
  2798     QFETCH( QString, type );
       
  2799     QFETCH( double,  doubleVal );
       
  2800     QFETCH( int, intVal );
       
  2801     QFETCH( QString,  stringVal );
       
  2802 
       
  2803     ts->setEncoding( qt3_toEncoding(encoding) );
       
  2804 
       
  2805     if ( type == "QChar" ) {
       
  2806 	if ( intVal >= 0 && intVal <= 0xffff )
       
  2807 	    *ts << QChar( intVal );
       
  2808     } else if ( type == "char" ) {
       
  2809 	*ts << (char) intVal;
       
  2810     } else if ( type == "signed short" ) {
       
  2811 	*ts << (signed short) intVal;
       
  2812     } else if ( type == "unsigned short" ) {
       
  2813 	*ts << (unsigned short) intVal;
       
  2814     } else if ( type == "signed int" ) {
       
  2815 	*ts << (signed int) intVal;
       
  2816     } else if ( type == "unsigned int" ) {
       
  2817 	*ts << (unsigned int) intVal;
       
  2818     } else if ( type == "signed long" ) {
       
  2819 	*ts << (signed long) intVal;
       
  2820     } else if ( type == "unsigned long" ) {
       
  2821 	*ts << (unsigned long) intVal;
       
  2822     } else if ( type == "float" ) {
       
  2823 	*ts << (float) doubleVal;
       
  2824     } else if ( type == "double" ) {
       
  2825 	*ts << (double) doubleVal;
       
  2826     } else if ( type == "char *" ) {
       
  2827 	*ts << stringVal.latin1();
       
  2828     } else if ( type == "QString" ) {
       
  2829 	*ts << stringVal;
       
  2830     } else if ( type == "QByteArray" ) {
       
  2831 	*ts << QByteArray( stringVal.latin1() );
       
  2832     } else {
       
  2833 	QWARN( "Unknown type: %s" + type );
       
  2834     }
       
  2835 
       
  2836     ts->flush();
       
  2837 }
       
  2838 
       
  2839 // ------------------------------------------------------------------------------
       
  2840 void tst_QTextStream::qt3_operator_shiftleft()
       
  2841 {
       
  2842     QFETCH( QString, device );
       
  2843     QFETCH( QString, encoding );
       
  2844     QFETCH( QByteArray, encoded );
       
  2845 
       
  2846     if ( device == "file" ) {
       
  2847 	QFile outFile( "qtextstream.out" );
       
  2848 	QVERIFY( outFile.open( QIODevice::WriteOnly ) );
       
  2849 	QTextStream ts( &outFile );
       
  2850 
       
  2851 	qt3_do_shiftleft( &ts );
       
  2852 	outFile.close();
       
  2853 
       
  2854 	QFile inFile( "qtextstream.out" );
       
  2855 	QVERIFY( inFile.open( QIODevice::ReadOnly ) );
       
  2856         QCOMPARE( inFile.readAll().constData(), encoded.constData() );
       
  2857     } else if(device == "resource" ) { //ONLY TO CREATE THE RESOURCE!! NOT A TEST!
       
  2858 	QFile outFile( resourceDir() + "operator_shiftright_" + QTest::currentDataTag() + ".data" );
       
  2859 	QVERIFY( outFile.open( QIODevice::WriteOnly ) );
       
  2860 	QTextStream ts( &outFile );
       
  2861 	qt3_do_shiftleft( &ts );
       
  2862 	outFile.close();
       
  2863     } else if ( device == "bytearray" ) {
       
  2864 	QByteArray outArray;
       
  2865 	QTextStream ts(&outArray, QIODevice::WriteOnly );
       
  2866 
       
  2867 	qt3_do_shiftleft( &ts );
       
  2868 
       
  2869 	QVERIFY( outArray == encoded );
       
  2870     } else if ( device == "buffer" ) {
       
  2871 	QByteArray outArray;
       
  2872 	QBuffer outBuffer(&outArray);
       
  2873 	QVERIFY( outBuffer.open(QIODevice::WriteOnly) );
       
  2874 	QTextStream ts( &outBuffer );
       
  2875 
       
  2876 	qt3_do_shiftleft( &ts );
       
  2877 
       
  2878 	QCOMPARE( outArray, encoded );
       
  2879     } else if ( device == "string" ) {
       
  2880 	QString outString;
       
  2881 	QTextStream ts( &outString, QIODevice::WriteOnly );
       
  2882 
       
  2883 	qt3_do_shiftleft( &ts );
       
  2884 
       
  2885 	QString decodedString = qt3_decodeString( encoded, encoding );
       
  2886 	QCOMPARE( outString, decodedString );
       
  2887     } else {
       
  2888 	QWARN( "Unknown device type: " + device );
       
  2889     }
       
  2890 }
       
  2891 
       
  2892 // ------------------------------------------------------------------------------
       
  2893 void tst_QTextStream::qt3_operator_shiftright()
       
  2894 {
       
  2895     QFETCH( QString, device );
       
  2896     QFETCH( QString, encoding );
       
  2897     QFETCH( QString, type );
       
  2898     QFETCH( double,  doubleVal );
       
  2899     QFETCH( int, intVal );
       
  2900     QFETCH( QString,  stringVal );
       
  2901     QFETCH( QByteArray, encoded );
       
  2902 
       
  2903     if ( device == "file" ) {
       
  2904 	QFile outFile( "qtextstream.out" );
       
  2905 	QVERIFY( outFile.open( QIODevice::WriteOnly ) );
       
  2906 	QVERIFY( (int) outFile.write(encoded) == (int) encoded.size() );
       
  2907 	outFile.close();
       
  2908 
       
  2909 	inFile = new QFile( "qtextstream.out" );
       
  2910 	QVERIFY( inFile->open( QIODevice::ReadOnly ) );
       
  2911 	ts = new QTextStream( inFile );
       
  2912     } else if(device == "resource" ) {
       
  2913 	inResource = new QFile( ":/tst_textstream/" + resourceDir() + "operator_shiftright_" + QTest::currentDataTag() + ".data" );
       
  2914 	QVERIFY( inResource->open( QIODevice::ReadOnly ) );
       
  2915 	ts = new QTextStream( inResource );
       
  2916     } else if ( device == "bytearray" ) {
       
  2917 	ts = new QTextStream(&encoded, QIODevice::ReadOnly);
       
  2918     } else if ( device == "buffer" ) {
       
  2919 	inBuffer = new QBuffer(&encoded);
       
  2920 	QVERIFY( inBuffer->open(QIODevice::ReadOnly) );
       
  2921 	ts = new QTextStream( inBuffer );
       
  2922     } else if ( device == "string" ) {
       
  2923 	inString = new QString( qt3_decodeString(encoded, encoding) );
       
  2924 	ts = new QTextStream(inString, QIODevice::ReadOnly);
       
  2925     } else {
       
  2926 	QWARN( "Unknown device type: " + device );
       
  2927     }
       
  2928 
       
  2929     ts->setEncoding( qt3_toEncoding(encoding) );
       
  2930 
       
  2931     if ( type == "QChar" ) {
       
  2932 	QChar c;
       
  2933 	if ( intVal >= 0 && intVal <= 0xffff )
       
  2934 	    *ts >> c;
       
  2935 	QCOMPARE( c.toLatin1(), QChar(intVal).toLatin1() );
       
  2936     } else if ( type == "char" ) {
       
  2937 	char c;
       
  2938 	*ts >> c;
       
  2939 	QCOMPARE( c, (char) intVal );
       
  2940     } else if ( type == "signed short" ) {
       
  2941 	signed short h;
       
  2942 	*ts >> h;
       
  2943 	QCOMPARE( h, (signed short) intVal );
       
  2944     } else if ( type == "unsigned short" ) {
       
  2945 	unsigned short h;
       
  2946 	*ts >> h;
       
  2947 	QCOMPARE( h, (unsigned short) intVal );
       
  2948     } else if ( type == "signed int" ) {
       
  2949 	signed int i;
       
  2950 	*ts >> i;
       
  2951 	QCOMPARE( i, (signed int) intVal );
       
  2952     } else if ( type == "unsigned int" ) {
       
  2953 	unsigned int i;
       
  2954 	*ts >> i;
       
  2955 	QCOMPARE( i, (unsigned int) intVal );
       
  2956     } else if ( type == "signed long" ) {
       
  2957 	signed long ell;
       
  2958 	*ts >> ell;
       
  2959 	QCOMPARE( ell, (signed long) intVal );
       
  2960     } else if ( type == "unsigned long" ) {
       
  2961 	unsigned long ell;
       
  2962 	*ts >> ell;
       
  2963 	QCOMPARE( ell, (unsigned long) intVal );
       
  2964     } else if ( type == "float" ) {
       
  2965 	float f;
       
  2966 	*ts >> f;
       
  2967 	QCOMPARE( f, (float) doubleVal );
       
  2968     } else if ( type == "double" ) {
       
  2969 	double d;
       
  2970 	*ts >> d;
       
  2971 	QCOMPARE( d, (double) doubleVal );
       
  2972     } else if ( type == "char *" ) {
       
  2973 	char *cp = new char[2048];
       
  2974 	*ts >> cp;
       
  2975 	QVERIFY( qstrcmp(cp, stringVal.latin1()) == 0 );
       
  2976 	delete[] cp;
       
  2977     } else if ( type == "QString" ) {
       
  2978 	QString s;
       
  2979 	*ts >> s;
       
  2980 	QCOMPARE( s, stringVal );
       
  2981     } else if ( type == "QByteArray" ) {
       
  2982 	QByteArray s;
       
  2983 	*ts >> s;
       
  2984 	QCOMPARE( QString::fromLatin1(s), stringVal );
       
  2985     } else {
       
  2986 	QWARN( "Unknown type: %s" + type );
       
  2987     }
       
  2988 }
       
  2989 
       
  2990 // ------------------------------------------------------------------------------
       
  2991 QTextStream::Encoding tst_QTextStream::qt3_toEncoding( const QString &str )
       
  2992 {
       
  2993     if ( str == "Locale" )
       
  2994 	return QTextStream::Locale;
       
  2995     else if ( str == "Latin1" )
       
  2996 	return QTextStream::Latin1;
       
  2997     else if ( str == "Unicode" )
       
  2998 	return QTextStream::Unicode;
       
  2999     else if ( str == "UnicodeNetworkOrder" )
       
  3000 	return QTextStream::UnicodeNetworkOrder;
       
  3001     else if ( str == "UnicodeReverse" )
       
  3002 	return QTextStream::UnicodeReverse;
       
  3003     else if ( str == "RawUnicode" )
       
  3004 	return QTextStream::RawUnicode;
       
  3005     else if ( str == "UnicodeUTF8" )
       
  3006 	return QTextStream::UnicodeUTF8;
       
  3007 
       
  3008     QWARN( "No such encoding " + str );
       
  3009     return QTextStream::Latin1;
       
  3010 }
       
  3011 
       
  3012 // ------------------------------------------------------------------------------
       
  3013 QString tst_QTextStream::qt3_decodeString( QByteArray array, const QString& encoding )
       
  3014 {
       
  3015     switch ( qt3_toEncoding(encoding) ) {
       
  3016     case QTextStream::Locale:
       
  3017 	return QString::fromLocal8Bit( array.data(), array.size() );
       
  3018     case QTextStream::Latin1:
       
  3019 	return QString::fromLatin1( array.data(), array.size() );
       
  3020     case QTextStream::Unicode:
       
  3021     case QTextStream::UnicodeNetworkOrder:
       
  3022     case QTextStream::UnicodeReverse:
       
  3023     case QTextStream::RawUnicode:
       
  3024 	QWARN( "Unicode not implemented ###" );
       
  3025 	return QString();
       
  3026     case QTextStream::UnicodeUTF8:
       
  3027 	return QString::fromUtf8( array.data(), array.size() );
       
  3028     default:
       
  3029 	QWARN( "Unhandled encoding" );
       
  3030 	return QString();
       
  3031     }
       
  3032 }
       
  3033 
       
  3034 // ************************************************
       
  3035 
       
  3036 // ------------------------------------------------------------------------------
       
  3037 void tst_QTextStream::qt3_createWriteStream( QTextStream *&os )
       
  3038 {
       
  3039     QFETCH( QString, device );
       
  3040 
       
  3041     if ( device == "file" ) {
       
  3042 	outFile = new QFile( "qtextstream.out" );
       
  3043 	QVERIFY( outFile->open( QIODevice::WriteOnly ) );
       
  3044 	os = new QTextStream( outFile );
       
  3045         os->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3046         os->setAutoDetectUnicode(true);
       
  3047     } else if(device == "resource" ) { //ONLY TO CREATE THE RESOURCE!! NOT A TEST!
       
  3048 	outFile = new QFile( resourceDir() +
       
  3049                              QTest::currentTestFunction() +
       
  3050                              "_" + QTest::currentDataTag() + ".data" );
       
  3051 	QVERIFY( outFile->open( QIODevice::WriteOnly ) );
       
  3052 	os = new QTextStream( outFile );
       
  3053         os->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3054         os->setAutoDetectUnicode(true);
       
  3055     } else if ( device == "bytearray" ) {
       
  3056 	inArray = new QByteArray;
       
  3057 	os = new QTextStream(inArray, QIODevice::WriteOnly);
       
  3058         os->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3059         os->setAutoDetectUnicode(true);
       
  3060     } else if ( device == "buffer" ) {
       
  3061 	inBuffer = new QBuffer;
       
  3062 	QVERIFY( inBuffer->open(QIODevice::WriteOnly) );
       
  3063 	os = new QTextStream( inBuffer );
       
  3064         os->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3065         os->setAutoDetectUnicode(true);
       
  3066     } else if ( device == "string" ) {
       
  3067 	inString = new QString;
       
  3068 	os = new QTextStream( inString, QIODevice::WriteOnly );
       
  3069         os->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3070         os->setAutoDetectUnicode(true);
       
  3071     } else {
       
  3072 	QWARN( "Error creating write stream: Unknown device type '" + device + "'" );
       
  3073     }
       
  3074 
       
  3075     QFETCH( QString, encoding );
       
  3076     os->setEncoding( qt3_toEncoding( encoding ));
       
  3077 }
       
  3078 
       
  3079 // ------------------------------------------------------------------------------
       
  3080 void tst_QTextStream::qt3_closeWriteStream( QTextStream *os )
       
  3081 {
       
  3082     QFETCH( QString, device );
       
  3083 
       
  3084     os->flush();
       
  3085     if ( os->device() )
       
  3086 	os->device()->close();
       
  3087 }
       
  3088 
       
  3089 // ------------------------------------------------------------------------------
       
  3090 void tst_QTextStream::qt3_createReadStream( QTextStream *&is )
       
  3091 {
       
  3092     QFETCH( QString, device );
       
  3093 
       
  3094     if ( device == "file" ) {
       
  3095 	inFile = new QFile( "qtextstream.out" );
       
  3096 	is = new QTextStream( inFile );
       
  3097         is->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3098         is->setAutoDetectUnicode(true);
       
  3099 	QVERIFY( inFile->open( QIODevice::ReadOnly ) );
       
  3100     } else if(device == "resource") {
       
  3101 	inResource = new QFile( ":/tst_textstream/" + resourceDir() +
       
  3102                                 QTest::currentTestFunction() +
       
  3103                                 "_" + QTest::currentDataTag() + ".data" );
       
  3104 	is = new QTextStream( inResource );
       
  3105         is->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3106         is->setAutoDetectUnicode(true);
       
  3107 	QVERIFY( inResource->open( QIODevice::ReadOnly ) );
       
  3108     } else if ( device == "bytearray" ) {
       
  3109 	is = new QTextStream(inArray, QIODevice::ReadOnly);
       
  3110         is->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3111         is->setAutoDetectUnicode(true);
       
  3112     } else if ( device == "buffer" ) {
       
  3113 	QVERIFY( inBuffer->open(QIODevice::ReadOnly) );
       
  3114 	is = new QTextStream( inBuffer );
       
  3115         is->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3116         is->setAutoDetectUnicode(true);
       
  3117     } else if ( device == "string" ) {
       
  3118 	is = new QTextStream( inString, QIODevice::ReadOnly );
       
  3119         is->setCodec(QTextCodec::codecForName("ISO-8859-1"));
       
  3120         is->setAutoDetectUnicode(true);
       
  3121     } else {
       
  3122 	QWARN( "Error creating read stream: Unknown device type '" + device + "'" );
       
  3123     }
       
  3124 
       
  3125     QFETCH( QString, encoding );
       
  3126     is->setEncoding( qt3_toEncoding( encoding ));
       
  3127 
       
  3128     if (!file_is_empty) {
       
  3129 	QVERIFY( !is->atEnd() );
       
  3130     }
       
  3131 }
       
  3132 
       
  3133 // ------------------------------------------------------------------------------
       
  3134 void tst_QTextStream::qt3_closeReadStream( QTextStream *is )
       
  3135 {
       
  3136     QVERIFY( is->atEnd() );
       
  3137 
       
  3138     if ( is->device() )
       
  3139 	is->device()->close();
       
  3140 }
       
  3141 
       
  3142 // **************** QChar ****************
       
  3143 
       
  3144 // ------------------------------------------------------------------------------
       
  3145 void tst_QTextStream::qt3_operator_shift_QChar_data()
       
  3146 {
       
  3147     QTest::addColumn<int>("modes");
       
  3148     QTest::addColumn<QString>("device");
       
  3149     QTest::addColumn<QString>("encoding");
       
  3150     QTest::addColumn<ushort>("qchar");
       
  3151 
       
  3152     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3153         tst_IODevice *device = devices+i;
       
  3154 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3155 	    QString encoding = encodings[e];
       
  3156 
       
  3157 	    QString tag = device->name() + "_" + encoding + "_";
       
  3158 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << QChar( 'A' ).unicode();
       
  3159 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << QChar( 'B' ).unicode();
       
  3160 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << QChar( 'Z' ).unicode();
       
  3161 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << QChar( 'z' ).unicode();
       
  3162 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << QChar( '@' ).unicode();
       
  3163 	}
       
  3164     }
       
  3165 }
       
  3166 
       
  3167 // ------------------------------------------------------------------------------
       
  3168 void tst_QTextStream::qt3_operator_shift_QChar()
       
  3169 {
       
  3170     QFETCH( int, modes );
       
  3171     if(modes & QIODevice::WriteOnly) {
       
  3172         qt3_createWriteStream( os );
       
  3173         qt3_write_QChar( os );
       
  3174         qt3_closeWriteStream( os );
       
  3175     }
       
  3176     if(modes & QIODevice::ReadOnly) {
       
  3177         qt3_createReadStream( is );
       
  3178         qt3_read_QChar( is );
       
  3179         qt3_closeReadStream( is );
       
  3180     }
       
  3181 }
       
  3182 
       
  3183 // ------------------------------------------------------------------------------
       
  3184 void tst_QTextStream::qt3_read_QChar( QTextStream *s )
       
  3185 {
       
  3186     QFETCH( ushort, qchar );
       
  3187     QChar expected( qchar );
       
  3188     QChar actual;
       
  3189     *s >> actual;
       
  3190     QCOMPARE( actual, expected );
       
  3191 }
       
  3192 
       
  3193 // ------------------------------------------------------------------------------
       
  3194 void tst_QTextStream::qt3_write_QChar( QTextStream *s )
       
  3195 {
       
  3196     QFETCH( ushort, qchar );
       
  3197     QChar actual( qchar );
       
  3198     *s << actual;
       
  3199 }
       
  3200 
       
  3201 // **************** char ****************
       
  3202 
       
  3203 // ------------------------------------------------------------------------------
       
  3204 void tst_QTextStream::qt3_operator_shift_char_data()
       
  3205 {
       
  3206     QTest::addColumn<int>("modes");
       
  3207     QTest::addColumn<QString>("device");
       
  3208     QTest::addColumn<QString>("encoding");
       
  3209     QTest::addColumn<int>("ch");
       
  3210 
       
  3211     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3212         tst_IODevice *device = devices+i;
       
  3213 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3214 	    QString encoding = encodings[e];
       
  3215 
       
  3216 	    QString tag = device->name() + "_" + encoding + "_";
       
  3217 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << int('A');
       
  3218 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << int('B');
       
  3219 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << int('Z');
       
  3220 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << int(14);
       
  3221 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << int('0');
       
  3222 	}
       
  3223     }
       
  3224 }
       
  3225 
       
  3226 // ------------------------------------------------------------------------------
       
  3227 void tst_QTextStream::qt3_operator_shift_char()
       
  3228 {
       
  3229     QFETCH( int, modes );
       
  3230     if(modes & QIODevice::WriteOnly) {
       
  3231         qt3_createWriteStream( os );
       
  3232         qt3_write_char( os );
       
  3233         qt3_closeWriteStream( os );
       
  3234     }
       
  3235     if(modes & QIODevice::ReadOnly) {
       
  3236         qt3_createReadStream( is );
       
  3237         qt3_read_char( is );
       
  3238         qt3_closeReadStream( is );
       
  3239     }
       
  3240 }
       
  3241 
       
  3242 // ------------------------------------------------------------------------------
       
  3243 void tst_QTextStream::qt3_read_char( QTextStream *s )
       
  3244 {
       
  3245     QFETCH( int, ch );
       
  3246     char c(ch);
       
  3247     char exp;
       
  3248     *s >> exp;
       
  3249     QCOMPARE( exp, c );
       
  3250 }
       
  3251 
       
  3252 // ------------------------------------------------------------------------------
       
  3253 void tst_QTextStream::qt3_write_char( QTextStream *s )
       
  3254 {
       
  3255     QFETCH( int, ch );
       
  3256     char c(ch);
       
  3257     *s << c;
       
  3258 }
       
  3259 
       
  3260 // **************** short ****************
       
  3261 
       
  3262 // ------------------------------------------------------------------------------
       
  3263 void tst_QTextStream::qt3_operator_shift_short_data()
       
  3264 {
       
  3265     QTest::addColumn<int>("modes");
       
  3266     QTest::addColumn<QString>("device");
       
  3267     QTest::addColumn<QString>("encoding");
       
  3268     QTest::addColumn<short>("ss");
       
  3269 
       
  3270     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3271         tst_IODevice *device = devices+i;
       
  3272 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3273 	    QString encoding = encodings[e];
       
  3274 
       
  3275 	    QString tag = device->name() + "_" + encoding + "_";
       
  3276 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << short(0);
       
  3277 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << short(-1);
       
  3278 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << short(1);
       
  3279 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << short(255);
       
  3280 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << short(-254);
       
  3281 	}
       
  3282     }
       
  3283 }
       
  3284 
       
  3285 // ------------------------------------------------------------------------------
       
  3286 void tst_QTextStream::qt3_operator_shift_short()
       
  3287 {
       
  3288     QFETCH( int, modes );
       
  3289     if(modes & QIODevice::WriteOnly) {
       
  3290         qt3_createWriteStream( os );
       
  3291         qt3_write_short( os );
       
  3292         qt3_closeWriteStream( os );
       
  3293     }
       
  3294     if(modes & QIODevice::ReadOnly) {
       
  3295         qt3_createReadStream( is );
       
  3296         qt3_read_short( is );
       
  3297         qt3_closeReadStream( is );
       
  3298     }
       
  3299 }
       
  3300 
       
  3301 // ------------------------------------------------------------------------------
       
  3302 void tst_QTextStream::qt3_read_short( QTextStream *s )
       
  3303 {
       
  3304     QFETCH( short, ss );
       
  3305     short exp;
       
  3306     QString A, B;
       
  3307     *s >> A >> exp >> B;
       
  3308     QCOMPARE( A, QString("A") );
       
  3309     QCOMPARE( B, QString("B") );
       
  3310     QCOMPARE( exp, ss );
       
  3311     s->skipWhiteSpace();
       
  3312 }
       
  3313 
       
  3314 // ------------------------------------------------------------------------------
       
  3315 void tst_QTextStream::qt3_write_short( QTextStream *s )
       
  3316 {
       
  3317     QFETCH( short, ss );
       
  3318     *s << " A " << ss << " B ";
       
  3319 }
       
  3320 
       
  3321 // **************** ushort ****************
       
  3322 
       
  3323 // ------------------------------------------------------------------------------
       
  3324 void tst_QTextStream::qt3_operator_shift_ushort_data()
       
  3325 {
       
  3326     QTest::addColumn<int>("modes");
       
  3327     QTest::addColumn<QString>("device");
       
  3328     QTest::addColumn<QString>("encoding");
       
  3329     QTest::addColumn<ushort>("us");
       
  3330 
       
  3331     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3332         tst_IODevice *device = devices+i;
       
  3333 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3334 	    QString encoding = encodings[e];
       
  3335 
       
  3336 	    QString tag = device->name() + "_" + encoding + "_";
       
  3337 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << ushort(0);
       
  3338 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << ushort(1);
       
  3339 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << ushort(10);
       
  3340 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << ushort(255);
       
  3341 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << ushort(512);
       
  3342 	}
       
  3343     }
       
  3344 }
       
  3345 
       
  3346 // ------------------------------------------------------------------------------
       
  3347 void tst_QTextStream::qt3_operator_shift_ushort()
       
  3348 {
       
  3349     QFETCH( int, modes );
       
  3350     if(modes & QIODevice::WriteOnly) {
       
  3351         qt3_createWriteStream( os );
       
  3352         qt3_write_ushort( os );
       
  3353         qt3_closeWriteStream( os );
       
  3354     }
       
  3355     if(modes & QIODevice::ReadOnly) {
       
  3356         qt3_createReadStream( is );
       
  3357         qt3_read_ushort( is );
       
  3358         qt3_closeReadStream( is );
       
  3359     }
       
  3360 }
       
  3361 
       
  3362 // ------------------------------------------------------------------------------
       
  3363 void tst_QTextStream::qt3_read_ushort( QTextStream *s )
       
  3364 {
       
  3365     QFETCH( ushort, us );
       
  3366     ushort exp;
       
  3367     QString A, B;
       
  3368     *s >> A >> exp >> B;
       
  3369     QCOMPARE( A, QString("A") );
       
  3370     QCOMPARE( B, QString("B") );
       
  3371     QCOMPARE( exp, us );
       
  3372     s->skipWhiteSpace();
       
  3373 }
       
  3374 
       
  3375 // ------------------------------------------------------------------------------
       
  3376 void tst_QTextStream::qt3_write_ushort( QTextStream *s )
       
  3377 {
       
  3378     QFETCH( ushort, us );
       
  3379     *s << " A " << us << " B ";
       
  3380 }
       
  3381 
       
  3382 // **************** int ****************
       
  3383 
       
  3384 // ------------------------------------------------------------------------------
       
  3385 void tst_QTextStream::qt3_operator_shift_int_data()
       
  3386 {
       
  3387     QTest::addColumn<int>("modes");
       
  3388     QTest::addColumn<QString>("device");
       
  3389     QTest::addColumn<QString>("encoding");
       
  3390     QTest::addColumn<int>("si");
       
  3391 
       
  3392     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3393         tst_IODevice *device = devices+i;
       
  3394 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3395 	    QString encoding = encodings[e];
       
  3396 
       
  3397 	    QString tag = device->name() + "_" + encoding + "_";
       
  3398 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << int(0);
       
  3399 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << int(1);
       
  3400 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << int(10);
       
  3401 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << int(255);
       
  3402 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << int(512);
       
  3403 	    QTest::newRow( tag + "5" ) << device->modes << device->name() << encoding << int(-1);
       
  3404 	    QTest::newRow( tag + "6" ) << device->modes << device->name() << encoding << int(-10);
       
  3405 	    QTest::newRow( tag + "7" ) << device->modes << device->name() << encoding << int(-255);
       
  3406 	    QTest::newRow( tag + "8" ) << device->modes << device->name() << encoding << int(-512);
       
  3407 	}
       
  3408     }
       
  3409 }
       
  3410 
       
  3411 // ------------------------------------------------------------------------------
       
  3412 void tst_QTextStream::qt3_operator_shift_int()
       
  3413 {
       
  3414     QFETCH( int, modes );
       
  3415     if(modes & QIODevice::WriteOnly) {
       
  3416         qt3_createWriteStream( os );
       
  3417         qt3_write_int( os );
       
  3418         qt3_closeWriteStream( os );
       
  3419     }
       
  3420     if(modes & QIODevice::ReadOnly) {
       
  3421         qt3_createReadStream( is );
       
  3422         qt3_read_int( is );
       
  3423         qt3_closeReadStream( is );
       
  3424     }
       
  3425 }
       
  3426 
       
  3427 // ------------------------------------------------------------------------------
       
  3428 void tst_QTextStream::qt3_read_int( QTextStream *s )
       
  3429 {
       
  3430     QFETCH( int, si );
       
  3431     int exp;
       
  3432     QString A, B;
       
  3433     *s >> A >> exp >> B;
       
  3434     QCOMPARE( A, QString("A") );
       
  3435     QCOMPARE( B, QString("B") );
       
  3436     QCOMPARE( exp, si );
       
  3437     s->skipWhiteSpace();
       
  3438 }
       
  3439 
       
  3440 // ------------------------------------------------------------------------------
       
  3441 void tst_QTextStream::qt3_write_int( QTextStream *s )
       
  3442 {
       
  3443     QFETCH( int, si );
       
  3444     *s << " A " << si << " B ";
       
  3445 }
       
  3446 
       
  3447 // **************** uint ****************
       
  3448 
       
  3449 // ------------------------------------------------------------------------------
       
  3450 void tst_QTextStream::qt3_operator_shift_uint_data()
       
  3451 {
       
  3452     QTest::addColumn<int>("modes");
       
  3453     QTest::addColumn<QString>("device");
       
  3454     QTest::addColumn<QString>("encoding");
       
  3455     QTest::addColumn<uint>("ui");
       
  3456 
       
  3457     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3458         tst_IODevice *device = devices+i;
       
  3459 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3460 	    QString encoding = encodings[e];
       
  3461 
       
  3462 	    QString tag = device->name() + "_" + encoding + "_";
       
  3463 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << uint(0);
       
  3464 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << uint(1);
       
  3465 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << uint(10);
       
  3466 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << uint(255);
       
  3467 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << uint(512);
       
  3468 	}
       
  3469     }
       
  3470 }
       
  3471 
       
  3472 // ------------------------------------------------------------------------------
       
  3473 void tst_QTextStream::qt3_operator_shift_uint()
       
  3474 {
       
  3475     QFETCH( int, modes );
       
  3476     if(modes & QIODevice::WriteOnly) {
       
  3477         qt3_createWriteStream( os );
       
  3478         qt3_write_uint( os );
       
  3479         qt3_closeWriteStream( os );
       
  3480     }
       
  3481     if(modes & QIODevice::ReadOnly) {
       
  3482         qt3_createReadStream( is );
       
  3483         qt3_read_uint( is );
       
  3484         qt3_closeReadStream( is );
       
  3485     }
       
  3486 }
       
  3487 
       
  3488 // ------------------------------------------------------------------------------
       
  3489 void tst_QTextStream::qt3_read_uint( QTextStream *s )
       
  3490 {
       
  3491     QFETCH( uint, ui );
       
  3492     uint exp;
       
  3493     QString A, B;
       
  3494     *s >> A >> exp >> B;
       
  3495     QCOMPARE( A, QString("A") );
       
  3496     QCOMPARE( B, QString("B") );
       
  3497     QCOMPARE( exp, ui );
       
  3498     s->skipWhiteSpace();
       
  3499 }
       
  3500 
       
  3501 // ------------------------------------------------------------------------------
       
  3502 void tst_QTextStream::qt3_write_uint( QTextStream *s )
       
  3503 {
       
  3504     QFETCH( uint, ui );
       
  3505     *s << " A " << ui << " B ";
       
  3506 }
       
  3507 
       
  3508 // **************** long ****************
       
  3509 
       
  3510 // ------------------------------------------------------------------------------
       
  3511 void tst_QTextStream::qt3_operator_shift_long_data()
       
  3512 {
       
  3513     QTest::addColumn<int>("modes");
       
  3514     QTest::addColumn<QString>("device");
       
  3515     QTest::addColumn<QString>("encoding");
       
  3516     QTest::addColumn<long>("sl");
       
  3517 
       
  3518     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3519         tst_IODevice *device = devices+i;
       
  3520 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3521 	    QString encoding = encodings[e];
       
  3522 
       
  3523 	    QString tag = device->name() + "_" + encoding + "_";
       
  3524 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << long(0);
       
  3525 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << long(1);
       
  3526 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << long(10);
       
  3527 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << long(255);
       
  3528 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << long(65535);
       
  3529 	    QTest::newRow( tag + "5" ) << device->modes << device->name() << encoding << long(-1);
       
  3530 	    QTest::newRow( tag + "6" ) << device->modes << device->name() << encoding << long(-10);
       
  3531 	    QTest::newRow( tag + "7" ) << device->modes << device->name() << encoding << long(-255);
       
  3532 	    QTest::newRow( tag + "8" ) << device->modes << device->name() << encoding << long(-65534);
       
  3533 	}
       
  3534     }
       
  3535 }
       
  3536 
       
  3537 // ------------------------------------------------------------------------------
       
  3538 void tst_QTextStream::qt3_operator_shift_long()
       
  3539 {
       
  3540     QFETCH( int, modes );
       
  3541     if(modes & QIODevice::WriteOnly) {
       
  3542         qt3_createWriteStream( os );
       
  3543         qt3_write_long( os );
       
  3544         qt3_closeWriteStream( os );
       
  3545     }
       
  3546     if(modes & QIODevice::ReadOnly) {
       
  3547         qt3_createReadStream( is );
       
  3548         qt3_read_long( is );
       
  3549         qt3_closeReadStream( is );
       
  3550     }
       
  3551 }
       
  3552 
       
  3553 // ------------------------------------------------------------------------------
       
  3554 void tst_QTextStream::qt3_read_long( QTextStream *s )
       
  3555 {
       
  3556     QFETCH( long, sl );
       
  3557     long exp;
       
  3558     QString A, B;
       
  3559     *s >> A >> exp >> B;
       
  3560     QCOMPARE( A, QString("A") );
       
  3561     QCOMPARE( B, QString("B") );
       
  3562     QCOMPARE( exp, sl );
       
  3563     s->skipWhiteSpace();
       
  3564 }
       
  3565 
       
  3566 // ------------------------------------------------------------------------------
       
  3567 void tst_QTextStream::qt3_write_long( QTextStream *s )
       
  3568 {
       
  3569     QFETCH( long, sl );
       
  3570     *s << " A " << sl << " B ";
       
  3571 }
       
  3572 
       
  3573 // **************** long ****************
       
  3574 
       
  3575 // ------------------------------------------------------------------------------
       
  3576 void tst_QTextStream::qt3_operator_shift_ulong_data()
       
  3577 {
       
  3578     QTest::addColumn<int>("modes");
       
  3579     QTest::addColumn<QString>("device");
       
  3580     QTest::addColumn<QString>("encoding");
       
  3581     QTest::addColumn<ulong>("ul");
       
  3582 
       
  3583     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3584         tst_IODevice *device = devices+i;
       
  3585 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3586 	    QString encoding = encodings[e];
       
  3587 
       
  3588 	    QString tag = device->name() + "_" + encoding + "_";
       
  3589 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << ulong(0);
       
  3590 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << ulong(1);
       
  3591 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << ulong(10);
       
  3592 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << ulong(255);
       
  3593 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << ulong(65535);
       
  3594 	}
       
  3595     }
       
  3596 }
       
  3597 
       
  3598 // ------------------------------------------------------------------------------
       
  3599 void tst_QTextStream::qt3_operator_shift_ulong()
       
  3600 {
       
  3601     QFETCH( int, modes );
       
  3602     if(modes & QIODevice::WriteOnly) {
       
  3603         qt3_createWriteStream( os );
       
  3604         qt3_write_ulong( os );
       
  3605         qt3_closeWriteStream( os );
       
  3606     }
       
  3607     if(modes & QIODevice::ReadOnly) {
       
  3608         qt3_createReadStream( is );
       
  3609         qt3_read_ulong( is );
       
  3610         qt3_closeReadStream( is );
       
  3611     }
       
  3612 }
       
  3613 
       
  3614 // ------------------------------------------------------------------------------
       
  3615 void tst_QTextStream::qt3_read_ulong( QTextStream *s )
       
  3616 {
       
  3617     QFETCH( ulong, ul );
       
  3618     ulong exp;
       
  3619     QString A, B;
       
  3620     *s >> A >> exp >> B;
       
  3621     QCOMPARE( A, QString("A") );
       
  3622     QCOMPARE( B, QString("B") );
       
  3623     QCOMPARE( exp, ul );
       
  3624     s->skipWhiteSpace();
       
  3625 }
       
  3626 
       
  3627 // ------------------------------------------------------------------------------
       
  3628 void tst_QTextStream::qt3_write_ulong( QTextStream *s )
       
  3629 {
       
  3630     QFETCH( ulong, ul );
       
  3631     *s << " A " << ul << " B ";
       
  3632 }
       
  3633 
       
  3634 // **************** float ****************
       
  3635 
       
  3636 // ------------------------------------------------------------------------------
       
  3637 void tst_QTextStream::qt3_operator_shift_float_data()
       
  3638 {
       
  3639     QTest::addColumn<int>("modes");
       
  3640     QTest::addColumn<QString>("device");
       
  3641     QTest::addColumn<QString>("encoding");
       
  3642     QTest::addColumn<float>("f");
       
  3643 
       
  3644     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3645         tst_IODevice *device = devices+i;
       
  3646 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3647 	    QString encoding = encodings[e];
       
  3648 
       
  3649 	    QString tag = device->name() + "_" + encoding + "_";
       
  3650 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << float(0.0);
       
  3651 	    QTest::newRow( tag + "1" ) << device->modes << device->name()  << encoding << float(0.0001);
       
  3652 	    QTest::newRow( tag + "2" ) << device->modes << device->name()  << encoding << float(-0.0001);
       
  3653 	    QTest::newRow( tag + "3" ) << device->modes << device->name()  << encoding << float(3.45678);
       
  3654 	    QTest::newRow( tag + "4" ) << device->modes << device->name()  << encoding << float(-3.45678);
       
  3655 	}
       
  3656     }
       
  3657 }
       
  3658 
       
  3659 // ------------------------------------------------------------------------------
       
  3660 void tst_QTextStream::qt3_operator_shift_float()
       
  3661 {
       
  3662     QFETCH( int, modes );
       
  3663     if(modes & QIODevice::WriteOnly) {
       
  3664         qt3_createWriteStream( os );
       
  3665         qt3_write_float( os );
       
  3666         qt3_closeWriteStream( os );
       
  3667     }
       
  3668     if(modes & QIODevice::ReadOnly) {
       
  3669         qt3_createReadStream( is );
       
  3670         qt3_read_float( is );
       
  3671         qt3_closeReadStream( is );
       
  3672     }
       
  3673 }
       
  3674 
       
  3675 // ------------------------------------------------------------------------------
       
  3676 void tst_QTextStream::qt3_read_float( QTextStream *s )
       
  3677 {
       
  3678     QFETCH( float, f );
       
  3679     float exp;
       
  3680     QString A, B;
       
  3681     *s >> A >> exp >> B;
       
  3682     QCOMPARE( A, QString("A") );
       
  3683     QCOMPARE( B, QString("B") );
       
  3684     QCOMPARE( exp, f );
       
  3685     s->skipWhiteSpace();
       
  3686 }
       
  3687 
       
  3688 // ------------------------------------------------------------------------------
       
  3689 void tst_QTextStream::qt3_write_float( QTextStream *s )
       
  3690 {
       
  3691     QFETCH( float, f );
       
  3692     *s << " A " << f << " B ";
       
  3693 }
       
  3694 
       
  3695 // **************** double ****************
       
  3696 
       
  3697 // ------------------------------------------------------------------------------
       
  3698 void tst_QTextStream::qt3_operator_shift_double_data()
       
  3699 {
       
  3700     QTest::addColumn<int>("modes");
       
  3701     QTest::addColumn<QString>("device");
       
  3702     QTest::addColumn<QString>("encoding");
       
  3703     QTest::addColumn<double>("d");
       
  3704 
       
  3705     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3706         tst_IODevice *device = devices+i;
       
  3707 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3708 	    QString encoding = encodings[e];
       
  3709 
       
  3710 	    QString tag = device->name() + "_" + encoding + "_";
       
  3711 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << double(0.0);
       
  3712 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << double(0.0001);
       
  3713 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << double(-0.0001);
       
  3714 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << double(3.45678);
       
  3715 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << double(-3.45678);
       
  3716 	    QTest::newRow( tag + "5" ) << device->modes << device->name() << encoding << double(1.23456789);
       
  3717 	    QTest::newRow( tag + "6" ) << device->modes << device->name() << encoding << double(-1.23456789);
       
  3718 	}
       
  3719     }
       
  3720 }
       
  3721 
       
  3722 // ------------------------------------------------------------------------------
       
  3723 void tst_QTextStream::qt3_operator_shift_double()
       
  3724 {
       
  3725     QFETCH( int, modes );
       
  3726     if(modes & QIODevice::WriteOnly) {
       
  3727         qt3_createWriteStream( os );
       
  3728         os->precision( 10 );
       
  3729         qt3_write_double( os );
       
  3730         qt3_closeWriteStream( os );
       
  3731     }
       
  3732     if(modes & QIODevice::ReadOnly) {
       
  3733         qt3_createReadStream( is );
       
  3734         is->precision( 10 );
       
  3735         qt3_read_double( is );
       
  3736         qt3_closeReadStream( is );
       
  3737     }
       
  3738 }
       
  3739 
       
  3740 // ------------------------------------------------------------------------------
       
  3741 void tst_QTextStream::qt3_read_double( QTextStream *s )
       
  3742 {
       
  3743     QFETCH( double, d );
       
  3744     double exp;
       
  3745     QString A, B;
       
  3746     *s >> A >> exp >> B;
       
  3747     QCOMPARE( A, QString("A") );
       
  3748     QCOMPARE( B, QString("B") );
       
  3749     QCOMPARE( exp, d );
       
  3750     s->skipWhiteSpace();
       
  3751 }
       
  3752 
       
  3753 // ------------------------------------------------------------------------------
       
  3754 void tst_QTextStream::qt3_write_double( QTextStream *s )
       
  3755 {
       
  3756     QFETCH( double, d );
       
  3757     *s << " A " << d << " B ";
       
  3758 }
       
  3759 
       
  3760 // **************** QString ****************
       
  3761 
       
  3762 // ------------------------------------------------------------------------------
       
  3763 void tst_QTextStream::qt3_operator_shift_QString_data()
       
  3764 {
       
  3765     QTest::addColumn<int>("modes");
       
  3766     QTest::addColumn<QString>("device");
       
  3767     QTest::addColumn<QString>("encoding");
       
  3768     QTest::addColumn<QString>("str");
       
  3769     QTest::addColumn<bool>("multi_str");
       
  3770     QTest::addColumn<bool>("zero_length");
       
  3771 
       
  3772     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3773         tst_IODevice *device = devices+i;
       
  3774 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3775 	    QString encoding = encodings[e];
       
  3776 
       
  3777 	    QString tag = device->name() + "_" + encoding + "_";
       
  3778 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << QString("") << bool(FALSE) << bool(TRUE);
       
  3779 	    QTest::newRow( tag + "1" ) << device->modes << device->name()  << encoding << QString() << bool(FALSE) << bool(TRUE);
       
  3780 	    QTest::newRow( tag + "2" ) << device->modes << device->name()  << encoding << QString("foo") << bool(FALSE) << bool(FALSE);
       
  3781 	    QTest::newRow( tag + "3" ) << device->modes << device->name()  << encoding << QString("foo\nbar") << bool(TRUE) << bool(FALSE);
       
  3782 	    QTest::newRow( tag + "4" ) << device->modes << device->name()  << encoding << QString("cjacka ckha cka ckah ckac kahckadhcbkgdk vkzdfbvajef vkahv") << bool(TRUE) << bool(FALSE);
       
  3783 	}
       
  3784     }
       
  3785 }
       
  3786 
       
  3787 // ------------------------------------------------------------------------------
       
  3788 void tst_QTextStream::qt3_operator_shift_QString()
       
  3789 {
       
  3790     QFETCH( bool, zero_length );
       
  3791     file_is_empty = zero_length;
       
  3792 
       
  3793     QFETCH( int, modes );
       
  3794     if(modes & QIODevice::WriteOnly) {
       
  3795         qt3_createWriteStream( os );
       
  3796         qt3_write_QString( os );
       
  3797         qt3_closeWriteStream( os );
       
  3798     }
       
  3799     if(modes & QIODevice::ReadOnly) {
       
  3800         qt3_createReadStream( is );
       
  3801         qt3_read_QString( is );
       
  3802         qt3_closeReadStream( is );
       
  3803     }
       
  3804 }
       
  3805 
       
  3806 // ------------------------------------------------------------------------------
       
  3807 void tst_QTextStream::qt3_read_QString( QTextStream *s )
       
  3808 {
       
  3809     QString exp;
       
  3810     QFETCH( QString, str );
       
  3811     if (str.isNull())
       
  3812 	str = "";
       
  3813 
       
  3814     QFETCH( bool, multi_str );
       
  3815     if (!multi_str) {
       
  3816 	*s >> exp;
       
  3817 	QCOMPARE( exp, str );
       
  3818     } else {
       
  3819 	QStringList l;
       
  3820 	l = QStringList::split( " ", str );
       
  3821 	if (l.count() < 2)
       
  3822 	    l = QStringList::split( "\n", str );
       
  3823 	for (int i=0; i<l.count(); i++) {
       
  3824 	    *s >> exp;
       
  3825 	    QCOMPARE( exp, l[i] );
       
  3826 	}
       
  3827     }
       
  3828 }
       
  3829 
       
  3830 // ------------------------------------------------------------------------------
       
  3831 void tst_QTextStream::qt3_write_QString( QTextStream *s )
       
  3832 {
       
  3833     QFETCH( QString, str );
       
  3834     *s << str;
       
  3835 }
       
  3836 
       
  3837 // **************** QByteArray ****************
       
  3838 
       
  3839 // ------------------------------------------------------------------------------
       
  3840 void tst_QTextStream::qt3_operator_shift_QByteArray_data()
       
  3841 {
       
  3842     QTest::addColumn<int>("modes");
       
  3843     QTest::addColumn<QString>("device");
       
  3844     QTest::addColumn<QString>("encoding");
       
  3845     QTest::addColumn<QByteArray>("cs");
       
  3846     QTest::addColumn<bool>("multi_str");
       
  3847     QTest::addColumn<bool>("zero_length");
       
  3848 
       
  3849     for ( int i=0; !devices[i].name().isNull(); i++ ) {
       
  3850         tst_IODevice *device = devices+i;
       
  3851 	for ( int e=0; encodings[e] != 0; e++ ) {
       
  3852 	    QString encoding = encodings[e];
       
  3853 
       
  3854 	    QString tag = device->name() + "_" + encoding + "_";
       
  3855 	    QTest::newRow( tag + "0" ) << device->modes << device->name() << encoding << QByteArray("") << bool(FALSE) << bool(TRUE);
       
  3856 	    QTest::newRow( tag + "1" ) << device->modes << device->name() << encoding << QByteArray(0) << bool(FALSE) << bool(TRUE);
       
  3857 	    QTest::newRow( tag + "2" ) << device->modes << device->name() << encoding << QByteArray("foo") << bool(FALSE) << bool(FALSE);
       
  3858 	    QTest::newRow( tag + "3" ) << device->modes << device->name() << encoding << QByteArray("foo\nbar") << bool(TRUE) << bool(FALSE);
       
  3859 	    QTest::newRow( tag + "4" ) << device->modes << device->name() << encoding << QByteArray("cjacka ckha cka ckah ckac kahckadhcbkgdk vkzdfbvajef vkahv") << bool(TRUE) << bool(FALSE);
       
  3860 	}
       
  3861     }
       
  3862 }
       
  3863 
       
  3864 // ------------------------------------------------------------------------------
       
  3865 void tst_QTextStream::qt3_operator_shift_QByteArray()
       
  3866 {
       
  3867     QFETCH( bool, zero_length );
       
  3868     file_is_empty = zero_length;
       
  3869 
       
  3870     QFETCH( int, modes );
       
  3871     if(modes & QIODevice::WriteOnly) {
       
  3872         qt3_createWriteStream( os );
       
  3873         qt3_write_QByteArray( os );
       
  3874         qt3_closeWriteStream( os );
       
  3875     }
       
  3876     if(modes & QIODevice::ReadOnly) {
       
  3877         qt3_createReadStream( is );
       
  3878         qt3_read_QByteArray( is );
       
  3879         qt3_closeReadStream( is );
       
  3880     }
       
  3881 }
       
  3882 
       
  3883 // ------------------------------------------------------------------------------
       
  3884 void tst_QTextStream::qt3_read_QByteArray( QTextStream *s )
       
  3885 {
       
  3886 /*
       
  3887     QFETCH( QByteArray, cs );
       
  3888     QByteArray exp;
       
  3889     *s >> exp;
       
  3890     QCOMPARE( exp, cs );
       
  3891 */
       
  3892     QByteArray exp;
       
  3893     QFETCH( QByteArray, cs );
       
  3894     if (cs.isNull())
       
  3895 	cs = "";
       
  3896 
       
  3897     QFETCH( bool, multi_str );
       
  3898     if (!multi_str) {
       
  3899 	*s >> exp;
       
  3900 	QCOMPARE( exp.constData(), cs.constData() );
       
  3901     } else {
       
  3902 	QStringList l;
       
  3903 	l = QStringList::split( " ", cs );
       
  3904 	if (l.count() < 2)
       
  3905 	    l = QStringList::split( "\n", cs );
       
  3906 	for (int i=0; i<l.count(); i++) {
       
  3907 	    *s >> exp;
       
  3908 	    QCOMPARE( exp, QByteArray(l[i].toAscii()) );
       
  3909 	}
       
  3910     }
       
  3911 }
       
  3912 
       
  3913 // ------------------------------------------------------------------------------
       
  3914 static void qt3_generateLineData( bool for_QString )
       
  3915 {
       
  3916     QTest::addColumn<QByteArray>("input");
       
  3917     QTest::addColumn<QStringList>("output");
       
  3918 
       
  3919     QTest::newRow("emptyer") << QByteArray() << QStringList();
       
  3920     QTest::newRow("lf") << QByteArray("\n") << (QStringList() << "");
       
  3921     QTest::newRow("crlf") << QByteArray("\r\n") << (QStringList() << "");
       
  3922     QTest::newRow("oneline/nothing") << QByteArray("ole") << (QStringList() << "ole");
       
  3923     QTest::newRow("oneline/lf") << QByteArray("ole\n") << (QStringList() << "ole");
       
  3924     QTest::newRow("oneline/crlf") << QByteArray("ole\r\n") << (QStringList() << "ole");
       
  3925     QTest::newRow("twolines/lf/lf") << QByteArray("ole\ndole\n") << (QStringList() << "ole" << "dole");
       
  3926     QTest::newRow("twolines/crlf/crlf") << QByteArray("ole\r\ndole\r\n") << (QStringList() << "ole" << "dole");
       
  3927     QTest::newRow("twolines/lf/crlf") << QByteArray("ole\ndole\r\n") << (QStringList() << "ole" << "dole");
       
  3928     QTest::newRow("twolines/lf/nothing") << QByteArray("ole\ndole") << (QStringList() << "ole" << "dole");
       
  3929     QTest::newRow("twolines/crlf/nothing") << QByteArray("ole\r\ndole") << (QStringList() << "ole" << "dole");
       
  3930     QTest::newRow("threelines/lf/lf/lf") << QByteArray("ole\ndole\ndoffen\n") << (QStringList() << "ole" << "dole" << "doffen");
       
  3931     QTest::newRow("threelines/crlf/crlf/crlf") << QByteArray("ole\r\ndole\r\ndoffen\r\n") << (QStringList() << "ole" << "dole" << "doffen");
       
  3932     QTest::newRow("threelines/crlf/crlf/nothing") << QByteArray("ole\r\ndole\r\ndoffen") << (QStringList() << "ole" << "dole" << "doffen");
       
  3933 
       
  3934     if (!for_QString) {
       
  3935         QTest::newRow("unicode/nothing") << QByteArray("\xfe\xff\x00\xe5\x00\x67\x00\x65", 8) << (QStringList() << "\345ge");
       
  3936         QTest::newRow("unicode-little/nothing") << QByteArray("\xff\xfe\xe5\x00\x67\x00\x65\x00", 8) << (QStringList() << "\345ge");
       
  3937         QTest::newRow("unicode/lf")        << QByteArray("\xfe\xff\x00\xe5\x00\x67\x00\x65\x00\x0a", 10) << (QStringList() << "\345ge");
       
  3938         QTest::newRow("unicode-little/lf") << QByteArray("\xff\xfe\xe5\x00\x67\x00\x65\x00\x0a\x00", 10) << (QStringList() << "\345ge");
       
  3939 
       
  3940         QTest::newRow("unicode/twolines")  << QByteArray("\xfe\xff\x00\xe5\x00\x67\x00\x65\x00\x0a\x00\xe5\x00\x67\x00\x65\x00\x0a", 18) << (QStringList() << "\345ge" << "\345ge");
       
  3941         QTest::newRow("unicode-little/twolines")  << QByteArray("\xff\xfe\xe5\x00\x67\x00\x65\x00\x0a\x00\xe5\x00\x67\x00\x65\x00\x0a\x00", 18) << (QStringList() << "\345ge" << "\345ge");
       
  3942 
       
  3943         QTest::newRow("unicode/threelines")
       
  3944             << QByteArray("\xfe\xff"
       
  3945                           "\x00\xe5\x00\x67\x00\x65\x00\x0a"
       
  3946                           "\x00\xe5\x00\x67\x00\x65\x00\x0a"
       
  3947                           "\x00\xe5\x00\x67\x00\x65\x00\x0a", 26)
       
  3948             << (QStringList() << "\345ge" << "\345ge" << "\345ge");
       
  3949         QTest::newRow("unicode-little/threelines")
       
  3950             << QByteArray("\xff\xfe"
       
  3951                           "\xe5\x00\x67\x00\x65\x00\x0a\x00"
       
  3952                           "\xe5\x00\x67\x00\x65\x00\x0a\x00"
       
  3953                           "\xe5\x00\x67\x00\x65\x00\x0a\x00", 26)
       
  3954             << (QStringList() << "\345ge" << "\345ge" << "\345ge");
       
  3955     }
       
  3956 
       
  3957     // partials
       
  3958     QTest::newRow("cr") << QByteArray("\r") << (QStringList() << "");
       
  3959     QTest::newRow("oneline/cr") << QByteArray("ole\r") << (QStringList() << "ole");
       
  3960     if (!for_QString)
       
  3961         QTest::newRow("unicode/cr") << QByteArray("\xfe\xff\x00\xe5\x00\x67\x00\x65\x00\x0d", 10) << (QStringList() << "\345ge");
       
  3962 }
       
  3963 
       
  3964 // ------------------------------------------------------------------------------
       
  3965 void tst_QTextStream::qt3_writeDataToFileReadAsLines_data()
       
  3966 {
       
  3967     qt3_generateLineData(false);
       
  3968 }
       
  3969 
       
  3970 // ------------------------------------------------------------------------------
       
  3971 QByteArray pretty(const QString &input)
       
  3972 {
       
  3973     QByteArray data;
       
  3974 
       
  3975     QByteArray arr = input.toLatin1();
       
  3976     for (int i = 0; i < arr.size(); ++i) {
       
  3977         char buf[64];
       
  3978         memset(buf, 0, sizeof(buf));
       
  3979         char ch = arr.at(i);
       
  3980         sprintf(buf, "\\%2hhx", ch);
       
  3981         data += buf;
       
  3982     }
       
  3983 
       
  3984     return data;
       
  3985 }
       
  3986 
       
  3987 // ------------------------------------------------------------------------------
       
  3988 void tst_QTextStream::qt3_writeDataToFileReadAsLines()
       
  3989 {
       
  3990     QFETCH(QByteArray, input);
       
  3991     QFETCH(QStringList, output);
       
  3992 
       
  3993     QFile::remove("eoltest");
       
  3994 
       
  3995     // Create the file
       
  3996     QFile writer("eoltest");
       
  3997     QVERIFY2(writer.open(QFile::WriteOnly | QFile::Truncate), ("When creating a file: " + writer.errorString()).latin1());
       
  3998     QCOMPARE(writer.write(input), qlonglong(input.size()));
       
  3999     writer.close();
       
  4000 
       
  4001     // Read from it using QTextStream
       
  4002     QVERIFY2(writer.open(QFile::ReadOnly), ("When reading a file: " + writer.errorString()).latin1());
       
  4003     QTextStream stream(&writer);
       
  4004     QStringList tmp;
       
  4005 
       
  4006     while (!stream.atEnd())
       
  4007         tmp << stream.readLine();
       
  4008 
       
  4009     QCOMPARE(tmp, output);
       
  4010 }
       
  4011 
       
  4012 // ------------------------------------------------------------------------------
       
  4013 void tst_QTextStream::qt3_writeDataToQStringReadAsLines_data()
       
  4014 {
       
  4015     qt3_generateLineData(true);
       
  4016 }
       
  4017 
       
  4018 // ------------------------------------------------------------------------------
       
  4019 void tst_QTextStream::qt3_writeDataToQStringReadAsLines()
       
  4020 {
       
  4021     QFETCH(QByteArray, input);
       
  4022     QFETCH(QStringList, output);
       
  4023 
       
  4024     QString s = input;
       
  4025     QTextStream stream(&s, QIODevice::ReadOnly);
       
  4026 
       
  4027     QStringList tmp;
       
  4028     while (!stream.atEnd())
       
  4029         tmp << stream.readLine();
       
  4030 
       
  4031     QCOMPARE(tmp, output);
       
  4032 }
       
  4033 
       
  4034 // ------------------------------------------------------------------------------
       
  4035 void tst_QTextStream::qt3_write_QByteArray( QTextStream *s )
       
  4036 {
       
  4037     QFETCH( QByteArray, cs );
       
  4038     *s << cs;
       
  4039 }
       
  4040 #endif
       
  4041 
       
  4042 // ------------------------------------------------------------------------------
       
  4043 void tst_QTextStream::status_real_read_data()
       
  4044 {
       
  4045     QTest::addColumn<QString>("input");
       
  4046     QTest::addColumn<double>("expected_f");
       
  4047     QTest::addColumn<QString>("expected_w");
       
  4048     QTest::addColumn<QList<int> >("results");
       
  4049 
       
  4050     QTest::newRow("1.23 abc   ") << QString("1.23 abc   ") << 1.23 << QString("abc")
       
  4051                               << (QList<int>()
       
  4052                                   << (int)QTextStream::Ok
       
  4053                                   << (int)QTextStream::ReadCorruptData
       
  4054                                   << (int)QTextStream::Ok
       
  4055                                   << (int)QTextStream::Ok
       
  4056                                   << (int)QTextStream::ReadPastEnd);
       
  4057 }
       
  4058 
       
  4059 void tst_QTextStream::status_real_read()
       
  4060 {
       
  4061     QFETCH(QString, input);
       
  4062     QFETCH(double, expected_f);
       
  4063     QFETCH(QString, expected_w);
       
  4064     QFETCH(QList<int>, results);
       
  4065 
       
  4066     QTextStream s(&input);
       
  4067     double f = 0.0;
       
  4068     QString w;
       
  4069     s >> f;
       
  4070     QCOMPARE((int)s.status(), results.at(0));
       
  4071     QCOMPARE(f, expected_f);
       
  4072     s >> f;
       
  4073     QCOMPARE((int)s.status(), results.at(1));
       
  4074     s.resetStatus();
       
  4075     QCOMPARE((int)s.status(), results.at(2));
       
  4076     s >> w;
       
  4077     QCOMPARE((int)s.status(), results.at(3));
       
  4078     QCOMPARE(w, expected_w);
       
  4079     s >> f;
       
  4080     QCOMPARE((int)s.status(), results.at(4));
       
  4081 }
       
  4082 
       
  4083 void tst_QTextStream::status_integer_read()
       
  4084 {
       
  4085 #ifdef Q_OS_WINCE
       
  4086     QString text = QLatin1String("123 abc   ");
       
  4087     QTextStream s(&text);
       
  4088 #else
       
  4089     QTextStream s("123 abc   ");
       
  4090 #endif
       
  4091     int i;
       
  4092     QString w;
       
  4093     s >> i;
       
  4094     QCOMPARE(s.status(), QTextStream::Ok);
       
  4095     s >> i;
       
  4096     QCOMPARE(s.status(), QTextStream::ReadCorruptData);
       
  4097     s.resetStatus();
       
  4098     QCOMPARE(s.status(), QTextStream::Ok);
       
  4099     s >> w;
       
  4100     QCOMPARE(s.status(), QTextStream::Ok);
       
  4101     QCOMPARE(w, QString("abc"));
       
  4102     s >> i;
       
  4103     QCOMPARE(s.status(), QTextStream::ReadPastEnd);
       
  4104 }
       
  4105 
       
  4106 void tst_QTextStream::status_word_read()
       
  4107 {
       
  4108 #ifdef Q_OS_WINCE
       
  4109     QString text = QLatin1String("abc ");
       
  4110     QTextStream s(&text);
       
  4111 #else
       
  4112     QTextStream s("abc ");
       
  4113 #endif
       
  4114     QString w;
       
  4115     s >> w;
       
  4116     QCOMPARE(s.status(), QTextStream::Ok);
       
  4117     s >> w;
       
  4118     QCOMPARE(s.status(), QTextStream::ReadPastEnd);
       
  4119 }
       
  4120 
       
  4121 void tst_QTextStream::task180679_alignAccountingStyle()
       
  4122 {
       
  4123     {
       
  4124     QString result;
       
  4125     QTextStream out(&result);
       
  4126     out.setFieldAlignment(QTextStream::AlignAccountingStyle);
       
  4127     out.setFieldWidth(4);
       
  4128     out.setPadChar('0');
       
  4129     out << -1;
       
  4130     QCOMPARE(result, QLatin1String("-001"));
       
  4131     }
       
  4132 
       
  4133     {
       
  4134     QString result;
       
  4135     QTextStream out(&result);
       
  4136     out.setFieldAlignment(QTextStream::AlignAccountingStyle);
       
  4137     out.setFieldWidth(4);
       
  4138     out.setPadChar('0');
       
  4139     out << "-1";
       
  4140     QCOMPARE(result, QLatin1String("00-1"));
       
  4141     }
       
  4142 
       
  4143     {
       
  4144     QString result;
       
  4145     QTextStream out(&result);
       
  4146     out.setFieldAlignment(QTextStream::AlignAccountingStyle);
       
  4147     out.setFieldWidth(6);
       
  4148     out.setPadChar('0');
       
  4149     out << -1.2;
       
  4150     QCOMPARE(result, QLatin1String("-001.2"));
       
  4151     }
       
  4152 
       
  4153     {
       
  4154     QString result;
       
  4155     QTextStream out(&result);
       
  4156     out.setFieldAlignment(QTextStream::AlignAccountingStyle);
       
  4157     out.setFieldWidth(6);
       
  4158     out.setPadChar('0');
       
  4159     out << "-1.2";
       
  4160     QCOMPARE(result, QLatin1String("00-1.2"));
       
  4161     }
       
  4162 }
       
  4163 
       
  4164 void tst_QTextStream::task178772_setCodec()
       
  4165 {
       
  4166     QByteArray ba("\xe5 v\xe6r\n\xc3\xa5 v\xc3\xa6r\n");
       
  4167     QString res = QLatin1String("\xe5 v\xe6r");
       
  4168 
       
  4169     QTextStream stream(ba);
       
  4170     stream.setCodec("ISO 8859-1");
       
  4171     QCOMPARE(stream.readLine(),res);
       
  4172     stream.setCodec("UTF-8");
       
  4173     QCOMPARE(stream.readLine(),res);
       
  4174 }
       
  4175 
       
  4176 void tst_QTextStream::double_write_with_flags_data()
       
  4177 {
       
  4178     QTest::addColumn<double>("number");
       
  4179     QTest::addColumn<QString>("output");
       
  4180     QTest::addColumn<int>("numberFlags");
       
  4181     QTest::addColumn<int>("realNumberNotation");
       
  4182 
       
  4183     QTest::newRow("-ForceSign") << -1.23 << QString("-1.23") << (int)QTextStream::ForceSign << 0;
       
  4184     QTest::newRow("+ForceSign") << 1.23 << QString("+1.23") << (int)QTextStream::ForceSign << 0;
       
  4185     QTest::newRow("inf") << qInf() << QString("inf") << 0 << 0;
       
  4186     QTest::newRow("-inf") << -qInf() << QString("-inf") << 0 << 0;
       
  4187     QTest::newRow("inf uppercase") << qInf() << QString("INF") << (int)QTextStream::UppercaseDigits << 0;
       
  4188     QTest::newRow("-inf uppercase") << -qInf() << QString("-INF") << (int)QTextStream::UppercaseDigits << 0;
       
  4189     QTest::newRow("nan") << qQNaN() << QString("nan") << 0 << 0;
       
  4190     QTest::newRow("nan") << qQNaN() << QString("NAN") << (int)QTextStream::UppercaseDigits << 0;
       
  4191     QTest::newRow("scientific") << 1.234567e+02 << QString("1.234567e+02") << 0  << (int)QTextStream::ScientificNotation;
       
  4192     QTest::newRow("scientific2") << 1.234567e+02 << QString("1.234567e+02") << (int)QTextStream::UppercaseBase << (int)QTextStream::ScientificNotation;
       
  4193     QTest::newRow("scientific uppercase") << 1.234567e+02 << QString("1.234567E+02") << (int)QTextStream::UppercaseDigits << (int)QTextStream::ScientificNotation;
       
  4194 }
       
  4195 
       
  4196 void tst_QTextStream::double_write_with_flags()
       
  4197 {
       
  4198     QFETCH(double, number);
       
  4199     QFETCH(QString, output);
       
  4200     QFETCH(int, numberFlags);
       
  4201     QFETCH(int, realNumberNotation);
       
  4202 
       
  4203     QString buf;
       
  4204     QTextStream stream(&buf);
       
  4205     if (numberFlags)
       
  4206         stream.setNumberFlags(QTextStream::NumberFlag(numberFlags));
       
  4207     if (realNumberNotation)
       
  4208         stream.setRealNumberNotation(QTextStream::RealNumberNotation(realNumberNotation));
       
  4209     stream << number;
       
  4210     QCOMPARE(buf, output);
       
  4211 }
       
  4212 
       
  4213 void tst_QTextStream::double_write_with_precision_data()
       
  4214 {
       
  4215     QTest::addColumn<int>("precision");
       
  4216     QTest::addColumn<double>("value");
       
  4217     QTest::addColumn<QString>("result");
       
  4218 
       
  4219     QTest::ignoreMessage(QtWarningMsg, "QTextStream::setRealNumberPrecision: Invalid precision (-1)");
       
  4220     QTest::newRow("-1") << -1 << 3.14159 << QString("3.14159");
       
  4221     QTest::newRow("0") << 0 << 3.14159 << QString("3");
       
  4222     QTest::newRow("1") << 1 << 3.14159 << QString("3");
       
  4223     QTest::newRow("2") << 2 << 3.14159 << QString("3.1");
       
  4224     QTest::newRow("3") << 3 << 3.14159 << QString("3.14");
       
  4225     QTest::newRow("5") << 5 << 3.14159 << QString("3.1416");
       
  4226     QTest::newRow("6") << 6 << 3.14159 << QString("3.14159");
       
  4227     QTest::newRow("7") << 7 << 3.14159 << QString("3.14159");
       
  4228     QTest::newRow("10") << 10 << 3.14159 << QString("3.14159");
       
  4229 }
       
  4230 
       
  4231 void tst_QTextStream::double_write_with_precision()
       
  4232 {
       
  4233     QFETCH(int, precision);
       
  4234     QFETCH(double, value);
       
  4235     QFETCH(QString, result);
       
  4236 
       
  4237     QString buf;
       
  4238     QTextStream stream(&buf);
       
  4239     stream.setRealNumberPrecision(precision);
       
  4240     stream << value;
       
  4241     QCOMPARE(buf, result);
       
  4242 }
       
  4243 
       
  4244 void tst_QTextStream::int_read_with_locale_data()
       
  4245 {
       
  4246     QTest::addColumn<QString>("locale");
       
  4247     QTest::addColumn<QString>("input");
       
  4248     QTest::addColumn<int>("output");
       
  4249 
       
  4250     QTest::newRow("C -123") << QString("C") << QString("-123") << -123;
       
  4251     QTest::newRow("C +123") << QString("C") << QString("+123") << 123;
       
  4252     QTest::newRow("C 12345") << QString("C") << QString("12345") << 12345;
       
  4253     QTest::newRow("C 12,345") << QString("C") << QString("12,345") << 12;
       
  4254     QTest::newRow("C 12.345") << QString("C") << QString("12.345") << 12;
       
  4255 
       
  4256     QTest::newRow("de_DE -123") << QString("de_DE") << QString("-123") << -123;
       
  4257     QTest::newRow("de_DE +123") << QString("de_DE") << QString("+123") << 123;
       
  4258     QTest::newRow("de_DE 12345") << QString("de_DE") << QString("12345") << 12345;
       
  4259     QTest::newRow("de_DE 12.345") << QString("de_DE") << QString("12.345") << 12345;
       
  4260     QTest::newRow("de_DE .12345") << QString("de_DE") << QString(".12345") << 0;
       
  4261 }
       
  4262 
       
  4263 void tst_QTextStream::int_read_with_locale()
       
  4264 {
       
  4265     QFETCH(QString, locale);
       
  4266     QFETCH(QString, input);
       
  4267     QFETCH(int, output);
       
  4268 
       
  4269     QTextStream stream(&input);
       
  4270     stream.setLocale(locale);
       
  4271     int result;
       
  4272     stream >> result;
       
  4273     QCOMPARE(result, output);
       
  4274 }
       
  4275 
       
  4276 void tst_QTextStream::int_write_with_locale_data()
       
  4277 {
       
  4278     QTest::addColumn<QString>("locale");
       
  4279     QTest::addColumn<int>("numberFlags");
       
  4280     QTest::addColumn<int>("input");
       
  4281     QTest::addColumn<QString>("output");
       
  4282 
       
  4283     QTest::newRow("C -123") << QString("C") << 0 << -123 << QString("-123");
       
  4284     QTest::newRow("C +123") << QString("C") << (int)QTextStream::ForceSign << 123 << QString("+123");
       
  4285     QTest::newRow("C 12345") << QString("C") << 0 << 12345 << QString("12345");
       
  4286 
       
  4287     QTest::newRow("de_DE -123") << QString("de_DE") << 0 << -123 << QString("-123");
       
  4288     QTest::newRow("de_DE +123") << QString("de_DE") << (int)QTextStream::ForceSign << 123 << QString("+123");
       
  4289     QTest::newRow("de_DE 12345") << QString("de_DE") << 0 << 12345 << QString("12.345");
       
  4290 }
       
  4291 
       
  4292 void tst_QTextStream::int_write_with_locale()
       
  4293 {
       
  4294     QFETCH(QString, locale);
       
  4295     QFETCH(int, numberFlags);
       
  4296     QFETCH(int, input);
       
  4297     QFETCH(QString, output);
       
  4298 
       
  4299     QString result;
       
  4300     QTextStream stream(&result);
       
  4301     stream.setLocale(locale);
       
  4302     if (numberFlags)
       
  4303         stream.setNumberFlags(QTextStream::NumberFlags(numberFlags));
       
  4304     stream << input;
       
  4305     QCOMPARE(result, output);
       
  4306 }
       
  4307 
       
  4308 // ------------------------------------------------------------------------------
       
  4309 
       
  4310 // like QTEST_APPLESS_MAIN, but initialising the locale on Unix
       
  4311 int main(int argc, char *argv[])
       
  4312 {
       
  4313 #if defined (Q_OS_UNIX) && !defined (Q_OS_SYMBIAN)
       
  4314     extern bool qt_locale_initialized;
       
  4315     ::setlocale(LC_ALL, "");
       
  4316     qt_locale_initialized = true;
       
  4317 #endif
       
  4318     tst_QTextStream tc;
       
  4319     return QTest::qExec(&tc, argc, argv);
       
  4320 }
       
  4321 
       
  4322 #include "tst_qtextstream.moc"
       
  4323