diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2021-07-28 19:19:42 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2021-08-10 13:21:27 +0000 |
commit | 2ca7830f50dbcf1d712f804d47eb809dd5ae2a3e (patch) | |
tree | d5fddd55e8148689cc701351c74626db7fab182e | |
parent | c4278a59d3b96f4f2e574a7d2c73eeab99d34f5d (diff) |
QString/QByteArray: Fix setNum docs and add tests
Amends 260168d9d7547a2e7586bff6cb42e11d54c9d06e
Task-number: QTBUG-53706
Change-Id: I01c8cdc6a3cb46ec8e49e15ad71b6d707c0d272f
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit 9b860ece42e4012821b7e2140f363dbe3e256062)
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/text/qstring.cpp | 3 | ||||
-rw-r--r-- | tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp | 53 |
2 files changed, 54 insertions, 2 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index dff1868742..d8dbc621d8 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -7175,8 +7175,7 @@ float QString::toFloat(bool *ok) const Sets the string to the printed value of \a n in the specified \a base, and returns a reference to the string. - The base is 10 by default and must be between 2 and 36. For bases - other than 10, \a n is treated as an unsigned integer. + The base is 10 by default and must be between 2 and 36. \snippet qstring/main.cpp 56 diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index 0db139b518..48a2a69e48 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -96,6 +96,8 @@ private slots: void toULongLong(); void number(); + void number_base_data(); + void number_base(); void toShort(); void toUShort(); void toInt_data(); @@ -1296,6 +1298,57 @@ void tst_QByteArray::number() QString(QByteArray("-9223372036854775808"))); } +void tst_QByteArray::number_base_data() +{ + QTest::addColumn<qlonglong>("n"); + QTest::addColumn<int>("base"); + QTest::addColumn<QByteArray>("expected"); + + QTest::newRow("base 10") << 12346LL << 10 << QByteArray("12346"); + QTest::newRow("base 2") << 12346LL << 2 << QByteArray("11000000111010"); + QTest::newRow("base 8") << 12346LL << 8 << QByteArray("30072"); + QTest::newRow("base 16") << 12346LL << 16 << QByteArray("303a"); + QTest::newRow("base 17") << 12346LL << 17 << QByteArray("28c4"); + QTest::newRow("base 36") << 2181789482LL << 36 << QByteArray("102zbje"); + + QTest::newRow("largeint, base 10") + << 123456789012LL << 10 << QByteArray("123456789012"); + QTest::newRow("largeint, base 2") + << 123456789012LL << 2 << QByteArray("1110010111110100110010001101000010100"); + QTest::newRow("largeint, base 8") + << 123456789012LL << 8 << QByteArray("1627646215024"); + QTest::newRow("largeint, base 16") + << 123456789012LL << 16 << QByteArray("1cbe991a14"); + QTest::newRow("largeint, base 17") + << 123456789012LL << 17 << QByteArray("10bec2b629"); +} + +void tst_QByteArray::number_base() +{ + QFETCH( qlonglong, n ); + QFETCH( int, base ); + QFETCH( QByteArray, expected ); + QCOMPARE(QByteArray::number(n, base), expected); + QCOMPARE(QByteArray::number(-n, base), '-' + expected); + + // check qlonglong->QByteArray->qlonglong round trip + for (int ibase = 2; ibase <= 36; ++ibase) { + auto stringrep = QByteArray::number(n, ibase); + QCOMPARE(QByteArray::number(-n, ibase), '-' + stringrep); + bool ok(false); + auto result = stringrep.toLongLong(&ok, ibase); + QVERIFY(ok); + QCOMPARE(n, result); + } + if (n <= std::numeric_limits<int>::max()) { + QCOMPARE(QByteArray::number(int(n), base), expected); + QCOMPARE(QByteArray::number(int(-n), base), '-' + expected); + } else if (n <= std::numeric_limits<long>::max()) { + QCOMPARE(QByteArray::number(long(n), base), expected); + QCOMPARE(QByteArray::number(long(-n), base), '-' + expected); + } +} + void tst_QByteArray::toShort() { bool ok = true; // opposite to the next expected result |