diff options
-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 1e0f57c005..6674d7a026 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -7179,8 +7179,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 aa21a43d78..b0d55f7542 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -100,6 +100,8 @@ private slots: void number(); void number_double_data(); void number_double(); + void number_base_data(); + void number_base(); void toShort(); void toUShort(); void toInt_data(); @@ -1322,6 +1324,57 @@ void tst_QByteArray::number_double() QTEST(QByteArray::number(value, format, precision), "expected"); } +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 |