From beaef85b8d5cb4b0153f87736c56ef25eeec13d4 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 6 Dec 2019 12:03:25 +0100 Subject: Add toInt() and friends to QStringView Make the API more symmetric with regards to both QString and QStringRef. Having this available helps making QStringView more of a drop-in replacement for QStringRef. QStringRef is planned to get removed in Qt 6. Change-Id: Ife036c0b55970078f42e1335442ff9ee5f4a2f0d Reviewed-by: Volker Hilsheimer --- .../qstringapisymmetry/tst_qstringapisymmetry.cpp | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index ed01d46dcd..0fdad63b24 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -589,6 +589,20 @@ private Q_SLOTS: void trim_trimmed_QByteArray_data() { trimmed_data(); } void trim_trimmed_QByteArray() { trimmed_impl(); } +private: + void toNumber_data(); + template void toNumber_impl(); + +private Q_SLOTS: + void toNumber_QString_data() { toNumber_data(); } + void toNumber_QString() { toNumber_impl(); } + void toNumber_QStringRef_data() { toNumber_data(); } + void toNumber_QStringRef() { toNumber_impl(); } + void toNumber_QStringView_data() { toNumber_data(); } + void toNumber_QStringView() { toNumber_impl(); } + void toNumber_QByteArray_data() { toNumber_data(); } + void toNumber_QByteArray() { toNumber_impl(); } + // // UTF-16-only checks: // @@ -1600,6 +1614,105 @@ void tst_QStringApiSymmetry::trimmed_impl() } } +void tst_QStringApiSymmetry::toNumber_data() +{ + QTest::addColumn("data"); + QTest::addColumn("result"); + QTest::addColumn("ok"); + + QTest::addRow("0") << QString::fromUtf8("0") << qint64(0) << true; + QTest::addRow("a0") << QString::fromUtf8("a0") << qint64(0) << false; + QTest::addRow("10") << QString::fromUtf8("10") << qint64(10) << true; + QTest::addRow("-10") << QString::fromUtf8("-10") << qint64(-10) << true; + QTest::addRow("32767") << QString::fromUtf8("32767") << qint64(32767) << true; + QTest::addRow("32768") << QString::fromUtf8("32768") << qint64(32768) << true; + QTest::addRow("-32767") << QString::fromUtf8("-32767") << qint64(-32767) << true; + QTest::addRow("-32768") << QString::fromUtf8("-32768") << qint64(-32768) << true; + QTest::addRow("100x") << QString::fromUtf8("100x") << qint64(0) << false; + QTest::addRow("-100x") << QString::fromUtf8("-100x") << qint64(0) << false; +} + +template +bool inRange(qint64 n) +{ + bool checkMax = quint64(std::numeric_limits::max()) <= quint64(std::numeric_limits::max()); + if (checkMax && n > qint64(std::numeric_limits::max())) + return false; + return qint64(std::numeric_limits::min()) <= n; +} + +template +void tst_QStringApiSymmetry::toNumber_impl() +{ + QFETCH(const QString, data); + QFETCH(qint64, result); + QFETCH(bool, ok); + + const auto utf8 = data.toUtf8(); + const auto l1s = data.toLatin1(); + const auto l1 = l1s.isNull() ? QLatin1String() : QLatin1String(l1s); + + const auto ref = data.isNull() ? QStringRef() : QStringRef(&data); + const auto s = make(ref, l1, utf8); + + bool is_ok = false; + qint64 n = 0; + + n = s.toShort(&is_ok); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toUShort(&is_ok); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toInt(&is_ok); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toUInt(&is_ok); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toLong(&is_ok); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toULong(&is_ok); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toLongLong(&is_ok); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toULongLong(&is_ok); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + if (qint64(float(n)) == n) { + float f = s.toFloat(&is_ok); + QCOMPARE(is_ok, ok); + if (is_ok) + QCOMPARE(qint64(f), result); + } + + if (qint64(double(n)) == n) { + double d = s.toDouble(&is_ok); + QCOMPARE(is_ok, ok); + if (is_ok) + QCOMPARE(qint64(d), result); + } +} + // // // UTF-16-only checks: -- cgit v1.2.3