QString/QByteArray: Fix setNum docs and add tests

Amends 260168d9d7

Task-number: QTBUG-53706
Pick-to: 6.2
Change-Id: I01c8cdc6a3cb46ec8e49e15ad71b6d707c0d272f
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Mårten Nordheim 2021-07-28 19:19:42 +02:00
parent 65512f8505
commit 9b860ece42
2 changed files with 54 additions and 2 deletions
src/corelib/text
tests/auto/corelib/text/qbytearray

View File

@ -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

View File

@ -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