tests/auto/qfile/tst_qfile.cpp
branchRCL_3
changeset 4 3b1da2848fc7
parent 3 41300fa6a67c
child 13 c0432d11811c
equal deleted inserted replaced
3:41300fa6a67c 4:3b1da2848fc7
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the test suite of the Qt Toolkit.
     7 ** This file is part of the test suite of the Qt Toolkit.
     8 **
     8 **
   127     void setSizeSeek();
   127     void setSizeSeek();
   128     void atEnd();
   128     void atEnd();
   129     void readLine();
   129     void readLine();
   130     void readLine2();
   130     void readLine2();
   131     void readLineNullInLine();
   131     void readLineNullInLine();
       
   132     void readAll_data();
       
   133     void readAll();
       
   134     void readAllBuffer();
   132     void readAllStdin();
   135     void readAllStdin();
   133     void readLineStdin();
   136     void readLineStdin();
   134     void readLineStdin_lineByLine();
   137     void readLineStdin_lineByLine();
   135     void text();
   138     void text();
   136     void missingEndOfLine();
   139     void missingEndOfLine();
   386     QFile::remove("tst_qfile_copy.cpp");
   389     QFile::remove("tst_qfile_copy.cpp");
   387     QFile::remove("nullinline.txt");
   390     QFile::remove("nullinline.txt");
   388     QFile::remove("myLink2.lnk");
   391     QFile::remove("myLink2.lnk");
   389     QFile::remove("resources");
   392     QFile::remove("resources");
   390     QFile::remove("qfile_map_testfile");
   393     QFile::remove("qfile_map_testfile");
       
   394     QFile::remove("readAllBuffer.txt");
   391 }
   395 }
   392 
   396 
   393 //------------------------------------------
   397 //------------------------------------------
   394 // The 'testfile' is currently just a
   398 // The 'testfile' is currently just a
   395 // testfile. The path of this file, the
   399 // testfile. The path of this file, the
   748     QCOMPARE(file.readLine(), QByteArray("linewith\0null\n", 14));
   752     QCOMPARE(file.readLine(), QByteArray("linewith\0null\n", 14));
   749     QCOMPARE(file.readLine(), QByteArray("anotherline\0withnull\n", 21));
   753     QCOMPARE(file.readLine(), QByteArray("anotherline\0withnull\n", 21));
   750     QCOMPARE(file.readLine(), QByteArray("\0\n", 2));
   754     QCOMPARE(file.readLine(), QByteArray("\0\n", 2));
   751     QCOMPARE(file.readLine(), QByteArray("null\0", 5));
   755     QCOMPARE(file.readLine(), QByteArray("null\0", 5));
   752     QCOMPARE(file.readLine(), QByteArray());
   756     QCOMPARE(file.readLine(), QByteArray());
       
   757 }
       
   758 
       
   759 void tst_QFile::readAll_data()
       
   760 {
       
   761     QTest::addColumn<bool>("textMode");
       
   762     QTest::addColumn<QString>("fileName");
       
   763     QTest::newRow( "TextMode unixfile" ) <<  true << SRCDIR "testfile.txt";
       
   764     QTest::newRow( "BinaryMode unixfile" ) <<  false << SRCDIR "testfile.txt";
       
   765     QTest::newRow( "TextMode dosfile" ) <<  true << SRCDIR "dosfile.txt";
       
   766     QTest::newRow( "BinaryMode dosfile" ) <<  false << SRCDIR "dosfile.txt";
       
   767     QTest::newRow( "TextMode bigfile" ) <<  true << SRCDIR "tst_qfile.cpp";
       
   768     QTest::newRow( "BinaryMode  bigfile" ) <<  false << SRCDIR "tst_qfile.cpp";
       
   769     QVERIFY(QFile(SRCDIR "tst_qfile.cpp").size() > 64*1024);
       
   770 }
       
   771 
       
   772 void tst_QFile::readAll()
       
   773 {
       
   774     QFETCH( bool, textMode );
       
   775     QFETCH( QString, fileName );
       
   776 
       
   777     QFile file(fileName);
       
   778     if (textMode)
       
   779         QVERIFY(file.open(QFile::Text | QFile::ReadOnly));
       
   780     else
       
   781         QVERIFY(file.open(QFile::ReadOnly));
       
   782 
       
   783     QByteArray a = file.readAll();
       
   784     file.reset();
       
   785     QVERIFY(file.pos() == 0);
       
   786 
       
   787     QVERIFY(file.bytesAvailable() > 7);
       
   788     QByteArray b = file.read(1);
       
   789     char x;
       
   790     file.getChar(&x);
       
   791     b.append(x);
       
   792     b.append(file.read(5));
       
   793     b.append(file.readAll());
       
   794 
       
   795     QCOMPARE(a, b);
       
   796 }
       
   797 
       
   798 void tst_QFile::readAllBuffer()
       
   799 {
       
   800     QString fileName = QLatin1String("readAllBuffer.txt");
       
   801 
       
   802     QFile::remove(fileName);
       
   803 
       
   804     QFile writer(fileName);
       
   805     QFile reader(fileName);
       
   806 
       
   807     QByteArray data1("This is arguably a very simple text.");
       
   808     QByteArray data2("This is surely not as simple a test.");
       
   809 
       
   810     QVERIFY( writer.open(QIODevice::ReadWrite | QIODevice::Unbuffered) );
       
   811     QVERIFY( reader.open(QIODevice::ReadOnly) );
       
   812 
       
   813     QCOMPARE( writer.write(data1), qint64(data1.size()) );
       
   814     QVERIFY( writer.seek(0) );
       
   815 
       
   816     QByteArray result;
       
   817     result = reader.read(18);
       
   818     QCOMPARE( result.size(), 18 );
       
   819 
       
   820     QCOMPARE( writer.write(data2), qint64(data2.size()) ); // new data, old version buffered in reader
       
   821     QCOMPARE( writer.write(data2), qint64(data2.size()) ); // new data, unbuffered in reader
       
   822 
       
   823     result += reader.readAll();
       
   824 
       
   825     QCOMPARE( result, data1 + data2 );
       
   826 
       
   827     QFile::remove(fileName);
   753 }
   828 }
   754 
   829 
   755 void tst_QFile::readAllStdin()
   830 void tst_QFile::readAllStdin()
   756 {
   831 {
   757 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
   832 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
  2042 
  2117 
  2043     char c = 0;
  2118     char c = 0;
  2044     file.write(&c, 0);
  2119     file.write(&c, 0);
  2045     QVERIFY(!file.flush());
  2120     QVERIFY(!file.flush());
  2046     QCOMPARE(file.error(), QFile::ResourceError);
  2121     QCOMPARE(file.error(), QFile::ResourceError);
  2047     file.write(&c, 1);
  2122     QCOMPARE(file.write(&c, 1), qint64(1));
  2048     QVERIFY(!file.flush());
  2123     QVERIFY(!file.flush());
  2049     QCOMPARE(file.error(), QFile::ResourceError);
  2124     QCOMPARE(file.error(), QFile::ResourceError);
  2050 
  2125 
  2051     file.close();
  2126     file.close();
  2052     QVERIFY(!file.isOpen());
  2127     QVERIFY(!file.isOpen());
  2053     QCOMPARE(file.error(), QFile::ResourceError);
  2128     QCOMPARE(file.error(), QFile::ResourceError);
       
  2129 
  2054     file.open(QIODevice::WriteOnly);
  2130     file.open(QIODevice::WriteOnly);
  2055     QCOMPARE(file.error(), QFile::NoError);
  2131     QCOMPARE(file.error(), QFile::NoError);
       
  2132     QVERIFY(file.flush()); // Shouldn't inherit write buffer
  2056     file.close();
  2133     file.close();
  2057     QCOMPARE(file.error(), QFile::NoError);
  2134     QCOMPARE(file.error(), QFile::NoError);
  2058 
  2135 
  2059     // try again without flush:
  2136     // try again without flush:
  2060     QVERIFY(file.open(QIODevice::WriteOnly));
  2137     QVERIFY(file.open(QIODevice::WriteOnly));