diff options
Diffstat (limited to 'tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp')
-rw-r--r-- | tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp | 188 |
1 files changed, 137 insertions, 51 deletions
diff --git a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp index f9de9eeebf..411084a36c 100644 --- a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp @@ -1,32 +1,7 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtTest/QtTest> +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QTest> #ifdef Q_OS_UNIX #include <locale.h> @@ -37,6 +12,7 @@ #include <QDebug> #include <QElapsedTimer> #include <QFile> +#include <QTemporaryFile> #include <QStringConverter> #include <QTcpSocket> #include <QTemporaryDir> @@ -45,7 +21,7 @@ # include <QProcess> #endif #include "../../../network-settings.h" -#include "emulationdetector.h" +#include <QtTest/private/qemulationdetector_p.h> QT_BEGIN_NAMESPACE template<> struct QMetaTypeId<QIODevice::OpenModeFlag> @@ -94,6 +70,8 @@ private slots: // char operators void QChar_operators_FromDevice_data(); void QChar_operators_FromDevice(); + void char16_t_operators_FromDevice_data(); + void char16_t_operators_FromDevice(); void char_operators_FromDevice_data(); void char_operators_FromDevice(); @@ -225,6 +203,9 @@ private slots: void textModeOnEmptyRead(); + void autodetectUnicode_data(); + void autodetectUnicode(); + private: void generateLineData(bool for_QString); void generateAllData(bool for_QString); @@ -569,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); @@ -954,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; } @@ -965,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; @@ -1211,11 +1193,16 @@ void tst_QTextStream::stillOpenWhenAtEnd() while (!stream.readLine().isNull()) {} QVERIFY(file.isOpen()); +#ifdef QT_TEST_SERVER + if (!QtNetworkSettings::verifyConnection(QtNetworkSettings::imapServerName(), 143)) + QSKIP("No network test server available"); +#else if (!QtNetworkSettings::verifyTestNetworkSettings()) QSKIP("No network test server available"); +#endif QTcpSocket socket; - socket.connectToHost(QtNetworkSettings::serverName(), 143); + socket.connectToHost(QtNetworkSettings::imapServerName(), 143); QVERIFY(socket.waitForReadyRead(5000)); QTextStream stream2(&socket); @@ -1417,23 +1404,23 @@ void tst_QTextStream::pos2() // ------------------------------------------------------------------------------ void tst_QTextStream::pos3LargeFile() { - if (EmulationDetector::isRunningArmOnX86()) + if (QTestPrivate::isRunningArmOnX86()) QSKIP("Running QTextStream::pos() in tight loop is too slow on emulator"); { 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; @@ -1530,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(); @@ -1552,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"); @@ -1608,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()) @@ -1773,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); @@ -1785,7 +1772,7 @@ void tst_QTextStream::utf8IncompleteAtBufferBoundary() } data.close(); - data.open(QFile::ReadOnly); + QVERIFY(data.open(QFile::ReadOnly)); QTextStream in(&data); QFETCH(bool, useLocale); @@ -1914,6 +1901,40 @@ void tst_QTextStream::QChar_operators_FromDevice() } // ------------------------------------------------------------------------------ +void tst_QTextStream::char16_t_operators_FromDevice_data() +{ + generateOperatorCharData(false); +} + +// ------------------------------------------------------------------------------ +void tst_QTextStream::char16_t_operators_FromDevice() +{ + QFETCH(QByteArray, input); + QFETCH(const QChar, qchar_output); + QFETCH(const QByteArray, write_output); + const char16_t char16_t_output = qchar_output.unicode(); + + QBuffer buf(&input); + buf.open(QBuffer::ReadOnly); + QTextStream stream(&buf); + stream.setEncoding(QStringConverter::Latin1); + char16_t tmp; + stream >> tmp; + QCOMPARE(tmp, qchar_output); + + QBuffer writeBuf; + writeBuf.open(QBuffer::WriteOnly); + + QTextStream writeStream(&writeBuf); + writeStream.setEncoding(QStringConverter::Latin1); + writeStream << char16_t_output; + writeStream.flush(); + + QCOMPARE(writeBuf.buffer().size(), write_output.size()); + QCOMPARE(writeBuf.buffer().constData(), write_output.constData()); +} + +// ------------------------------------------------------------------------------ void tst_QTextStream::char_operators_FromDevice_data() { generateOperatorCharData(false); @@ -2633,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"); } // ------------------------------------------------------------------------------ @@ -3027,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); + } +} + // ------------------------------------------------------------------------------ |