From 640c5ca088319d9a372072c180eef35c80a552e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Mon, 19 Jul 2021 15:24:51 +0200 Subject: Add benchmarks for QString::number While we're here: - remove the GCC precondition for compiling this benchmark. Task-number: QTBUG-88484 Change-Id: I14f3ea7e4708e274d032a6297e9d4a87ae5dc1c0 Reviewed-by: Edward Welbourne --- tests/benchmarks/corelib/text/CMakeLists.txt | 4 +- .../corelib/text/qstring/tst_bench_qstring.cpp | 132 +++++++++++++++++++++ 2 files changed, 133 insertions(+), 3 deletions(-) (limited to 'tests/benchmarks') diff --git a/tests/benchmarks/corelib/text/CMakeLists.txt b/tests/benchmarks/corelib/text/CMakeLists.txt index 8942b898d0..d0a2373822 100644 --- a/tests/benchmarks/corelib/text/CMakeLists.txt +++ b/tests/benchmarks/corelib/text/CMakeLists.txt @@ -7,6 +7,4 @@ add_subdirectory(qstringbuilder) add_subdirectory(qstringlist) add_subdirectory(qstringtokenizer) add_subdirectory(qregularexpression) -if(GCC) - add_subdirectory(qstring) -endif() +add_subdirectory(qstring) diff --git a/tests/benchmarks/corelib/text/qstring/tst_bench_qstring.cpp b/tests/benchmarks/corelib/text/qstring/tst_bench_qstring.cpp index 054f52c214..0fe387154e 100644 --- a/tests/benchmarks/corelib/text/qstring/tst_bench_qstring.cpp +++ b/tests/benchmarks/corelib/text/qstring/tst_bench_qstring.cpp @@ -28,6 +28,7 @@ #include #include #include +#include class tst_QString: public QObject { @@ -48,9 +49,18 @@ private slots: void toCaseFolded_data(); void toCaseFolded(); + void number_qlonglong_data(); + void number_qlonglong() { number_impl(); } + void number_qulonglong_data(); + void number_qulonglong() { number_impl(); } + + void number_double_data(); + void number_double(); + private: void section_data_impl(bool includeRegExOnly = true); template void section_impl(); + template void number_impl(); }; tst_QString::tst_QString() @@ -187,6 +197,128 @@ void tst_QString::toCaseFolded() } } +template +void tst_QString::number_impl() +{ + QFETCH(Integer, number); + QFETCH(int, base); + QFETCH(QString, expected); + + QString actual; + QBENCHMARK { + actual = QString::number(number, base); + } + QCOMPARE(actual, expected); +} + +template +void number_integer_common() +{ + QTest::newRow("0") << Integer(0ull) << 10 << QStringLiteral("0"); + QTest::newRow("1234") << Integer(1234ull) << 10 << QStringLiteral("1234"); + QTest::newRow("123456789") << Integer(123456789ull) << 10 << QStringLiteral("123456789"); + QTest::newRow("bad1dea, base 16") << Integer(0xBAD1DEAull) << 16 << QStringLiteral("bad1dea"); + QTest::newRow("242, base 8") << Integer(0242ull) << 8 << QStringLiteral("242"); + QTest::newRow("101101, base 2") << Integer(0b101101ull) << 2 << QStringLiteral("101101"); + QTest::newRow("ad, base 30") << Integer(313ull) << 30 << QStringLiteral("ad"); +} + +void tst_QString::number_qlonglong_data() +{ + QTest::addColumn("number"); + QTest::addColumn("base"); + QTest::addColumn("expected"); + + number_integer_common(); + + QTest::newRow("-1234") << -1234ll << 10 << QStringLiteral("-1234"); + QTest::newRow("-123456789") << -123456789ll << 10 << QStringLiteral("-123456789"); + QTest::newRow("-bad1dea, base 16") << -0xBAD1DEAll << 16 << QStringLiteral("-bad1dea"); + QTest::newRow("-242, base 8") << -0242ll << 8 << QStringLiteral("-242"); + QTest::newRow("-101101, base 2") << -0b101101ll << 2 << QStringLiteral("-101101"); + QTest::newRow("-ad, base 30") << -313ll << 30 << QStringLiteral("-ad"); + + QTest::newRow("qlonglong-max") + << std::numeric_limits::max() << 10 << QStringLiteral("9223372036854775807"); + QTest::newRow("qlonglong-min") + << std::numeric_limits::min() << 10 + << QStringLiteral("-9223372036854775808"); + QTest::newRow("qlonglong-max, base 2") + << std::numeric_limits::max() << 2 << QString(63, u'1'); + QTest::newRow("qlonglong-min, base 2") << std::numeric_limits::min() << 2 + << (QStringLiteral("-1") + QString(63, u'0')); + QTest::newRow("qlonglong-max, base 16") + << std::numeric_limits::max() << 16 << (QChar(u'7') + QString(15, u'f')); + QTest::newRow("qlonglong-min, base 16") << std::numeric_limits::min() << 16 + << (QStringLiteral("-8") + QString(15, u'0')); +} + +void tst_QString::number_qulonglong_data() +{ + QTest::addColumn("number"); + QTest::addColumn("base"); + QTest::addColumn("expected"); + + number_integer_common(); + + QTest::newRow("qlonglong-max + 1") + << (qulonglong(std::numeric_limits::max()) + 1) << 10 + << QStringLiteral("9223372036854775808"); + QTest::newRow("qulonglong-max") + << std::numeric_limits::max() << 10 + << QStringLiteral("18446744073709551615"); + QTest::newRow("qulonglong-max, base 2") + << std::numeric_limits::max() << 2 << QString(64, u'1'); + QTest::newRow("qulonglong-max, base 16") + << std::numeric_limits::max() << 16 << QString(16, u'f'); +} + +void tst_QString::number_double_data() +{ + QTest::addColumn("number"); + QTest::addColumn("format"); + QTest::addColumn("precision"); + QTest::addColumn("expected"); + + struct + { + double d; + char f; + int p; + QString expected; + } data[] = { + { 0.0, 'f', 0, QStringLiteral("0") }, + { 0.0001, 'f', 0, QStringLiteral("0") }, + { 0.1234, 'f', 5, QStringLiteral("0.12340") }, + { -0.1234, 'f', 5, QStringLiteral("-0.12340") }, + { 0.5 + qSqrt(1.25), 'f', 15, QStringLiteral("1.618033988749895") }, + { std::numeric_limits::epsilon(), 'g', 10, QStringLiteral("2.220446049e-16") }, + { 0.0001, 'E', 1, QStringLiteral("1.0E-04") }, + { 1e8, 'E', 1, QStringLiteral("1.0E+08") }, + { -1e8, 'E', 1, QStringLiteral("-1.0E+08") }, + }; + + for (auto &datum : data) { + QTest::addRow("%ls, format '%c', precision %d", qUtf16Printable(datum.expected), datum.f, + datum.p) + << datum.d << datum.f << datum.p << datum.expected; + } +} + +void tst_QString::number_double() +{ + QFETCH(double, number); + QFETCH(char, format); + QFETCH(int, precision); + QFETCH(QString, expected); + + QString actual; + QBENCHMARK { + actual = QString::number(number, format, precision); + } + QCOMPARE(actual, expected); +} + QTEST_APPLESS_MAIN(tst_QString) #include "tst_bench_qstring.moc" -- cgit v1.2.3