From a0858c639ea1e0a8eae451cf58ef1512383377b6 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 12 Apr 2022 09:30:18 +0200 Subject: [PATCH] tst_qstringapisymmetry: check toInt() etc also with base != 0 This shows we're lacking support for the 0b prefix, and, as it turns out, there's a request for it. Task-number: QTBUG-85002 Pick-to: 6.3 Change-Id: Ie201c84bf906a7e482b929301699ceb429b53c14 Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot --- .../tst_qstringapisymmetry.cpp | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 46691b45ff..c92cb0ad92 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -743,6 +743,8 @@ private Q_SLOTS: private: void toNumber_data(); template void toNumber_impl(); + void toNumberWithBases_data(); + template void toNumberWithBases_impl(); private Q_SLOTS: void toNumber_QString_data() { toNumber_data(); } @@ -754,6 +756,15 @@ private Q_SLOTS: void toNumber_QLatin1String_data() { toNumber_data(); } void toNumber_QLatin1String() { toNumber_impl(); } + void toNumberWithBases_QString_data() { toNumberWithBases_data(); } + void toNumberWithBases_QString() { toNumberWithBases_impl(); } + void toNumberWithBases_QStringView_data() { toNumberWithBases_data(); } + void toNumberWithBases_QStringView() { toNumberWithBases_impl(); } + void toNumberWithBases_QByteArray_data() { toNumberWithBases_data(); } + void toNumberWithBases_QByteArray() { toNumberWithBases_impl(); } + void toNumberWithBases_QLatin1String_data() { toNumberWithBases_data(); } + void toNumberWithBases_QLatin1String() { toNumberWithBases_impl(); } + private: void count_data(); template @@ -2632,6 +2643,114 @@ void tst_QStringApiSymmetry::toNumber_impl() } } +void tst_QStringApiSymmetry::toNumberWithBases_data() +{ + QTest::addColumn("data"); + QTest::addColumn("base"); + QTest::addColumn("result"); + QTest::addColumn("ok"); + + constexpr struct { + const char prefix[3]; + int base; + } bases[] = { + { "", 2 }, // should be {"0b", 2}, but Qt lacks support for the 0b prefix (QTBUG-85002) + { "0", 8 }, + { "", 10 }, + { "0x", 16 }, + }; + + const auto check = [&](const char *input, qint64 n2, qint64 n8, qint64 n10, qint64 n16, bool result) { + + for (const auto &e : bases) { + const QString data = QLatin1StringView(e.prefix) + QString::fromUtf8(input); + const auto row = [&](int base) { + const auto select = [&](int base) { + switch (base) { + case 2: return n2; + case 8: return n8; + case 10: return n10; + case 16: return n16; + } + Q_UNREACHABLE(); + }; + QTest::addRow("base%2d: %s%s", base, e.prefix, input) + << data << base << select(e.base /* NOT base! */) << result; + }; + row(e.base); // explicit base + if (e.base == 2) + continue; // Qt doesn't know 0b (yet, QTBUG-85002), so nothing to auto-detect + row(0); // automatically detected base + } + }; + + check("0", 0, 0, 0, 0, true); + check("y0", 0, 0, 0, 0, false); + check("0y", 0, 0, 0, 0, false); + check("10", 2, 8, 10, 16, true); + check("11", 3, 9, 11, 17, true); + check("100", 4, 64, 100, 256, true); +} + +template +void tst_QStringApiSymmetry::toNumberWithBases_impl() +{ + QFETCH(const QString, data); + QFETCH(const int, base); + QFETCH(const qint64, result); + QFETCH(const 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() ? QStringView() : QStringView(data); + const auto s = make(ref, l1, utf8); + + bool is_ok = false; + qint64 n = 0; + + n = s.toShort(&is_ok, base); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toUShort(&is_ok, base); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toInt(&is_ok, base); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toUInt(&is_ok, base); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toLong(&is_ok, base); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toULong(&is_ok, base); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toLongLong(&is_ok, base); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); + + n = s.toULongLong(&is_ok, base); + QCOMPARE(is_ok, ok && inRange(result)); + if (is_ok) + QCOMPARE(n, result); +} + void tst_QStringApiSymmetry::count_data() { QTest::addColumn("data");