diff options
Diffstat (limited to 'tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp')
-rw-r--r-- | tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp | 110 |
1 files changed, 90 insertions, 20 deletions
diff --git a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp index 6d80efe956..411084a36c 100644 --- a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2020 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -12,6 +12,7 @@ #include <QDebug> #include <QElapsedTimer> #include <QFile> +#include <QTemporaryFile> #include <QStringConverter> #include <QTcpSocket> #include <QTemporaryDir> @@ -202,6 +203,9 @@ private slots: void textModeOnEmptyRead(); + void autodetectUnicode_data(); + void autodetectUnicode(); + private: void generateLineData(bool for_QString); void generateAllData(bool for_QString); @@ -546,7 +550,7 @@ void tst_QTextStream::readLineMaxlen() QFile::remove("testfile"); QFile file("testfile"); if (useDevice) { - file.open(QIODevice::ReadWrite); + QVERIFY(file.open(QIODevice::ReadWrite)); file.write(input.toUtf8()); file.seek(0); stream.setDevice(&file); @@ -931,7 +935,8 @@ void tst_QTextStream::lineCount_data() QTest::newRow("buffersize+1 line") << QByteArray(16384, '\n') << 16384; QTest::newRow("buffersize+2 line") << QByteArray(16385, '\n') << 16385; - QFile file(m_rfc3261FilePath); file.open(QFile::ReadOnly); + QFile file(m_rfc3261FilePath); + QVERIFY(file.open(QFile::ReadOnly)); QTest::newRow("rfc3261") << file.readAll() << 15067; } @@ -942,7 +947,7 @@ void tst_QTextStream::lineCount() QFETCH(int, lineCount); QFile out("out.txt"); - out.open(QFile::WriteOnly); + QVERIFY(out.open(QFile::WriteOnly)); QTextStream lineReader(data); int lines = 0; @@ -1404,18 +1409,18 @@ void tst_QTextStream::pos3LargeFile() { QFile file(testFileName); - file.open(QIODevice::WriteOnly | QIODevice::Text); + QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text)); QTextStream out( &file ); // NOTE: The unusual spacing is to ensure non-1-character whitespace. QString lineString = " 0 1 2\t3 4\t \t5 6 7 8 9 \n"; // Approximate 50kb text file - const int NbLines = (50*1024) / lineString.length() + 1; + const int NbLines = (50*1024) / lineString.size() + 1; for (int line = 0; line < NbLines; ++line) out << lineString; // File is automatically flushed and closed on destruction. } QFile file(testFileName); - file.open(QIODevice::ReadOnly | QIODevice::Text); + QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); QTextStream in( &file ); const int testValues[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int value; @@ -1512,7 +1517,7 @@ void tst_QTextStream::read() { QFile::remove("testfile"); QFile file("testfile"); - file.open(QFile::WriteOnly); + QVERIFY(file.open(QFile::WriteOnly)); file.write("4.15 abc ole"); file.close(); @@ -1534,7 +1539,7 @@ void tst_QTextStream::read() // File larger than QTEXTSTREAM_BUFFERSIZE QFile::remove("testfile"); QFile file("testfile"); - file.open(QFile::WriteOnly); + QVERIFY(file.open(QFile::WriteOnly)); for (int i = 0; i < 16384 / 8; ++i) file.write("01234567"); file.write("0"); @@ -1590,8 +1595,8 @@ void tst_QTextStream::forceSign() // ------------------------------------------------------------------------------ void tst_QTextStream::read0d0d0a() { - QFile file("task113817.txt"); - file.open(QIODevice::ReadOnly | QIODevice::Text); + QFile file(QFINDTESTDATA("task113817.txt")); + QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); QTextStream stream(&file); while (!stream.atEnd()) @@ -1755,7 +1760,7 @@ void tst_QTextStream::utf8IncompleteAtBufferBoundary() "\342\200\223" "\342\200\223"); - data.open(QFile::WriteOnly | QFile::Truncate); + QVERIFY(data.open(QFile::WriteOnly | QFile::Truncate)); { QTextStream out(&data); out.setEncoding(QStringConverter::Utf8); @@ -1767,7 +1772,7 @@ void tst_QTextStream::utf8IncompleteAtBufferBoundary() } data.close(); - data.open(QFile::ReadOnly); + QVERIFY(data.open(QFile::ReadOnly)); QTextStream in(&data); QFETCH(bool, useLocale); @@ -2649,13 +2654,27 @@ void tst_QTextStream::manipulators_data() QTest::addColumn<QString>("textData"); QTest::addColumn<QByteArray>("result"); - QTest::newRow("no flags") << 10 << 0 << 0 << 0 << 5.0 << 5 << QString("five") << QByteArray("55five"); - QTest::newRow("rightadjust") << 10 << int(QTextStream::AlignRight) << 0 << 10 << 5.0 << 5 << QString("five") << QByteArray(" 5 5 five"); - QTest::newRow("leftadjust") << 10 << int(QTextStream::AlignLeft) << 0 << 10 << 5.0 << 5 << QString("five") << QByteArray("5 5 five "); - QTest::newRow("showpos") << 10 << int(QTextStream::AlignRight) << int(QTextStream::ForceSign) << 10 << 5.0 << 5 << QString("five") << QByteArray(" +5 +5 five"); - QTest::newRow("showpos2") << 10 << int(QTextStream::AlignRight) << int(QTextStream::ForceSign) << 5 << 3.14 << -5 << QString("five") << QByteArray("+3.14 -5 five"); - QTest::newRow("hex") << 16 << int(QTextStream::AlignRight) << int(QTextStream::ShowBase) << 5 << 3.14 << -5 << QString("five") << QByteArray(" 3.14 -0x5 five"); - QTest::newRow("hex") << 16 << int(QTextStream::AlignRight) << int(QTextStream::ShowBase | QTextStream::UppercaseBase) << 5 << 3.14 << -5 << QString("five") << QByteArray(" 3.14 -0X5 five"); + QTest::newRow("no flags") + << 10 << 0 << 0 << 0 << 5.0 << 5 << QString("five") << QByteArray("55five"); + QTest::newRow("rightadjust") + << 10 << int(QTextStream::AlignRight) << 0 << 10 << 5.0 << 5 << QString("five") + << QByteArray(" 5 5 five"); + QTest::newRow("leftadjust") + << 10 << int(QTextStream::AlignLeft) << 0 << 10 << 5.0 << 5 << QString("five") + << QByteArray("5 5 five "); + QTest::newRow("showpos-wide") + << 10 << int(QTextStream::AlignRight) << int(QTextStream::ForceSign) << 10 << 5.0 << 5 << + QString("five") << QByteArray(" +5 +5 five"); + QTest::newRow("showpos-pi") + << 10 << int(QTextStream::AlignRight) << int(QTextStream::ForceSign) << 5 << 3.14 << -5 << + QString("five") << QByteArray("+3.14 -5 five"); + QTest::newRow("hex-lower") + << 16 << int(QTextStream::AlignRight) << int(QTextStream::ShowBase) << 5 << 3.14 << -5 << + QString("five") << QByteArray(" 3.14 -0x5 five"); + QTest::newRow("hex-upper") + << 16 << int(QTextStream::AlignRight) + << int(QTextStream::ShowBase | QTextStream::UppercaseBase) + << 5 << 3.14 << -5 << QString("five") << QByteArray(" 3.14 -0X5 five"); } // ------------------------------------------------------------------------------ @@ -3043,6 +3062,57 @@ void tst_QTextStream::textModeOnEmptyRead() QVERIFY(file.isTextModeEnabled()); } +void tst_QTextStream::autodetectUnicode_data() +{ + QTest::addColumn<QStringConverter::Encoding>("encoding"); + QTest::newRow("Utf8") << QStringConverter::Utf8; + QTest::newRow("Utf16BE") << QStringConverter::Utf16BE; + QTest::newRow("Utf16LE") << QStringConverter::Utf16LE; + QTest::newRow("Utf32BE") << QStringConverter::Utf32BE; + QTest::newRow("Utf32LE") << QStringConverter::Utf32LE; +} + +void tst_QTextStream::autodetectUnicode() +{ + QFETCH(QStringConverter::Encoding, encoding); + + QTemporaryFile file; + QVERIFY(file.open()); + + QString original("HelloWorldđź‘‹"); + + { + QTextStream out(&file); + out.setGenerateByteOrderMark(true); + out.setEncoding(encoding); + out << original; + } + file.seek(0); + { + QTextStream in(&file); + QString actual; + in >> actual; + QCOMPARE(actual, original); + QCOMPARE(in.encoding(), encoding); + } + file.seek(0); + // Again, but change order of calls to QTextStream... + { + QTextStream out(&file); + out.setEncoding(encoding); + out.setGenerateByteOrderMark(true); + out << original; + } + file.seek(0); + { + QTextStream in(&file); + QString actual; + in >> actual; + QCOMPARE(actual, original); + QCOMPARE(in.encoding(), encoding); + } +} + // ------------------------------------------------------------------------------ |