diff options
Diffstat (limited to 'tests/benchmarks/corelib')
-rw-r--r-- | tests/benchmarks/corelib/text/qlocale/main.cpp | 272 | ||||
-rw-r--r-- | tests/benchmarks/corelib/text/qstring/main.cpp | 158 | ||||
-rw-r--r-- | tests/benchmarks/corelib/time/qdatetime/main.cpp | 105 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qcryptographichash/main.cpp | 141 |
4 files changed, 569 insertions, 107 deletions
diff --git a/tests/benchmarks/corelib/text/qlocale/main.cpp b/tests/benchmarks/corelib/text/qlocale/main.cpp index 38d94af143..c7bd37a2d3 100644 --- a/tests/benchmarks/corelib/text/qlocale/main.cpp +++ b/tests/benchmarks/corelib/text/qlocale/main.cpp @@ -28,6 +28,7 @@ #include <QLocale> #include <QTest> +#include <limits> class tst_QLocale : public QObject { @@ -37,6 +38,12 @@ private Q_SLOTS: void toUpper_QLocale_1(); void toUpper_QLocale_2(); void toUpper_QString(); + void toLongLong_data(); + void toLongLong(); + void toULongLong_data(); + void toULongLong(); + void toDouble_data(); + void toDouble(); }; static QString data() @@ -65,6 +72,271 @@ void tst_QLocale::toUpper_QString() QBENCHMARK { LOOP(s.toUpper()) } } +template <typename Integer> +void toWholeCommon_data() +{ + QTest::addColumn<QString>("text"); + QTest::addColumn<QString>("locale"); + QTest::addColumn<bool>("good"); + QTest::addColumn<Integer>("expected"); + + QTest::newRow("C: empty") + << QStringLiteral("") << QStringLiteral("C") << false << Integer(0ull); + QTest::newRow("C: 0") << QStringLiteral("0") << QStringLiteral("C") << true << Integer(0ull); + QTest::newRow("C: 1234") + << QStringLiteral("1234") << QStringLiteral("C") << true << Integer(1234ull); + // C locale omits grouping, but doesn't reject it. + QTest::newRow("C: 1,234") + << QStringLiteral("1,234") << QStringLiteral("C") << true << Integer(1234ull); + QTest::newRow("C: 123456789") + << QStringLiteral("123456789") << QStringLiteral("C") << true << Integer(123456789ull); + QTest::newRow("C: 123,456,789") + << QStringLiteral("123,456,789") + << QStringLiteral("C") << true << Integer(123456789ull); + + QTest::newRow("en: empty") + << QStringLiteral("") << QStringLiteral("en") << false << Integer(0ull); + QTest::newRow("en: 0") + << QStringLiteral("0") << QStringLiteral("en") << true << Integer(0ull); + QTest::newRow("en: 1234") + << QStringLiteral("1234") << QStringLiteral("en") << true << Integer(1234ull); + QTest::newRow("en: 1,234") + << QStringLiteral("1,234") << QStringLiteral("en") << true << Integer(1234ull); + QTest::newRow("en: 123,456,789") + << QStringLiteral("123,456,789") + << QStringLiteral("en") << true << Integer(123456789ull); + QTest::newRow("en: 123456789") + << QStringLiteral("123456789") << QStringLiteral("en") << true << Integer(123456789ull); + + QTest::newRow("de: empty") + << QStringLiteral("") << QStringLiteral("de") << false << Integer(0ull); + QTest::newRow("de: 0") << QStringLiteral("0") << QStringLiteral("de") << true << Integer(0ull); + QTest::newRow("de: 1234") + << QStringLiteral("1234") << QStringLiteral("de") << true << Integer(1234ull); + QTest::newRow("de: 1.234") + << QStringLiteral("1.234") << QStringLiteral("de") << true << Integer(1234ull); + QTest::newRow("de: 123.456.789") + << QStringLiteral("123.456.789") + << QStringLiteral("de") << true << Integer(123456789ull); + QTest::newRow("de: 123456789") + << QStringLiteral("123456789") << QStringLiteral("de") << true << Integer(123456789ull); + + // Locales with non-single-character signs: + QTest::newRow("ar_EG: +403") // Arabic, Egypt + << QStringLiteral("\u061c+\u0664\u0660\u0663") + << QStringLiteral("ar_EG") << true << Integer(403ull); + QTest::newRow("ar_EG: !403") // Only first character of the sign + << QStringLiteral("\u061c\u0664\u0660\u0663") + << QStringLiteral("ar_EG") << false << Integer(0ull); + QTest::newRow("fa_IR: +403") // Farsi, Iran + << QStringLiteral("\u200e+\u06f4\u06f0\u06f3") + << QStringLiteral("fa_IR") << true << Integer(403ull); + QTest::newRow("fa_IR: !403") // Only first character of sign + << QStringLiteral("\u200e\u06f4\u06f0\u06f3") + << QStringLiteral("fa_IR") << false << Integer(0ull); +} + +void tst_QLocale::toLongLong_data() +{ + toWholeCommon_data<qlonglong>(); + + QTest::newRow("C: -1234") << QStringLiteral("-1234") << QStringLiteral("C") << true << -1234ll; + QTest::newRow("C: -123456789") + << QStringLiteral("-123456789") << QStringLiteral("C") << true << -123456789ll; + QTest::newRow("C: qlonglong-max") + << QStringLiteral("9223372036854775807") << QStringLiteral("C") << true + << std::numeric_limits<qlonglong>::max(); + QTest::newRow("C: qlonglong-min") + << QStringLiteral("-9223372036854775808") << QStringLiteral("C") << true + << std::numeric_limits<qlonglong>::min(); + + // Locales with multi-character signs: + QTest::newRow("ar_EG: -403") // Arabic, Egypt + << QStringLiteral("\u061c-\u0664\u0660\u0663") + << QStringLiteral("ar_EG") << true << -403ll; + QTest::newRow("fa_IR: -403") // Farsi, Iran + << QStringLiteral("\u200e\u2212\u06f4\u06f0\u06f3") + << QStringLiteral("fa_IR") << true << -403ll; +} + +void tst_QLocale::toLongLong() +{ + QFETCH(QString, text); + QFETCH(QString, locale); + QFETCH(bool, good); + QFETCH(qlonglong, expected); + + const QLocale loc(locale); + qlonglong actual = expected; + bool ok = false; + QBENCHMARK { + actual = loc.toLongLong(text, &ok); + } + QEXPECT_FAIL("ar_EG: +403", "Code wrongly assumes single character, QTBUG-107801", Abort); + QEXPECT_FAIL("ar_EG: -403", "Code wrongly assumes single character, QTBUG-107801", Abort); + QEXPECT_FAIL("fa_IR: +403", "Code wrongly assumes single character, QTBUG-107801", Abort); + QEXPECT_FAIL("fa_IR: -403", "Code wrongly assumes single character, QTBUG-107801", Abort); + QCOMPARE(ok, good); + QCOMPARE(actual, expected); +} + +void tst_QLocale::toULongLong_data() +{ + toWholeCommon_data<qulonglong>(); + + QTest::newRow("C: qlonglong-max + 1") + << QStringLiteral("9223372036854775808") << QStringLiteral("C") << true + << (qulonglong(std::numeric_limits<qlonglong>::max()) + 1); + QTest::newRow("C: qulonglong-max") + << QStringLiteral("18446744073709551615") << QStringLiteral("C") << true + << std::numeric_limits<qulonglong>::max(); +} + +void tst_QLocale::toULongLong() +{ + QFETCH(QString, text); + QFETCH(QString, locale); + QFETCH(bool, good); + QFETCH(qulonglong, expected); + + const QLocale loc(locale); + qulonglong actual = expected; + bool ok = false; + QBENCHMARK { + actual = loc.toULongLong(text, &ok); + } + QEXPECT_FAIL("ar_EG: +403", "Code wrongly assumes single character, QTBUG-107801", Abort); + QEXPECT_FAIL("fa_IR: +403", "Code wrongly assumes single character, QTBUG-107801", Abort); + QCOMPARE(ok, good); + QCOMPARE(actual, expected); +} + + +void tst_QLocale::toDouble_data() +{ + QTest::addColumn<QString>("text"); + QTest::addColumn<QString>("locale"); + QTest::addColumn<bool>("good"); + QTest::addColumn<double>("expected"); + + QTest::newRow("C: empty") << QStringLiteral("") << QStringLiteral("C") << false << 0.0; + QTest::newRow("C: 0") << QStringLiteral("0") << QStringLiteral("C") << true << 0.0; + QTest::newRow("C: 0.12340") + << QStringLiteral("0.12340") << QStringLiteral("C") << true << 0.12340; + QTest::newRow("C: -0.12340") + << QStringLiteral("-0.12340") << QStringLiteral("C") << true << -0.12340; + QTest::newRow("C: −0.12340") + << QStringLiteral("\u2212" "0.12340") << QStringLiteral("C") << true << -0.12340; + QTest::newRow("C: 1.0e-4") << QStringLiteral("1.0e-4") << QStringLiteral("C") << true << 1.0e-4; + QTest::newRow("C: 1.0e−4") + << QStringLiteral("1.0e\u2212" "4") << QStringLiteral("C") << true << 1.0e-4; + QTest::newRow("C: 1.0e+4") << QStringLiteral("1.0e+4") << QStringLiteral("C") << true << 1.0e+4; + QTest::newRow("C: 10.e+3") << QStringLiteral("10.e+3") << QStringLiteral("C") << true << 1.0e+4; + QTest::newRow("C: 10e+3.") + << QStringLiteral("10e+3.") << QStringLiteral("C") << false << 0.0; // exp...dot + QTest::newRow("C: 1e4") << QStringLiteral("1e4") << QStringLiteral("C") << true << 1.0e+4; + + // NaN and infinity: + QTest::newRow("C: nan") << QStringLiteral("nan") << QStringLiteral("C") << true << qQNaN(); + QTest::newRow("C: NaN") << QStringLiteral("NaN") << QStringLiteral("C") << true << qQNaN(); + QTest::newRow("C: -nan") << QStringLiteral("-nan") << QStringLiteral("C") << false << 0.0; + QTest::newRow("C: +nan") << QStringLiteral("+nan") << QStringLiteral("C") << false << 0.0; + QTest::newRow("C: inf") << QStringLiteral("inf") << QStringLiteral("C") << true << qInf(); + QTest::newRow("C: Inf") << QStringLiteral("Inf") << QStringLiteral("C") << true << qInf(); + QTest::newRow("C: +inf") << QStringLiteral("+inf") << QStringLiteral("C") << true << qInf(); + QTest::newRow("C: -inf") << QStringLiteral("-inf") << QStringLiteral("C") << true << -qInf(); + + // Wantonly long-form representations, with trailing and leading zeros: + QTest::newRow("C: 1e-64 long-form") + << (QStringLiteral("0.") + QString(63, u'0') + u'1' + QString(962, u'0')) + << QStringLiteral("C") << true << 1e-64; + QTest::newRow("C: 1e+64 long-form") + << (QString(961, u'0') + u'1' + QString(64, u'0') + QStringLiteral(".0")) + << QStringLiteral("C") << true << 1e+64; + QTest::newRow("C: long-form 1 via e+64") + << (QStringLiteral("0.") + QString(63, u'0') + u'1' + QString(962, u'0') + + QStringLiteral("e+64")) + << QStringLiteral("C") << true << 1.0; + QTest::newRow("C: long-form 1 via e-64") + << (QString(961, u'0') + u'1' + QString(64, u'0') + QStringLiteral(".0e-64")) + << QStringLiteral("C") << true << 1.0; + QTest::newRow("C: 12345678.9") + << QStringLiteral("12345678.9") << QStringLiteral("C") << true << 12345678.9; + + // With and without grouping, en vs de for flipped separators: + QTest::newRow("en: 12345678.9") + << QStringLiteral("12345678.9") << QStringLiteral("en") << true << 12345678.9; + QTest::newRow("en: 12,345,678.9") + << QStringLiteral("12,345,678.9") << QStringLiteral("en") << true << 12345678.9; + QTest::newRow("de: 12345678,9") + << QStringLiteral("12345678,9") << QStringLiteral("de") << true << 12345678.9; + QTest::newRow("de: 12.345.678,9") + << QStringLiteral("12.345.678,9") << QStringLiteral("de") << true << 12345678.9; + + // NaN and infinity are locale-independent (for now - QTBUG-95460) + QTest::newRow("cy: nan") << QStringLiteral("nan") << QStringLiteral("cy") << true << qQNaN(); + QTest::newRow("cy: NaN") << QStringLiteral("NaN") << QStringLiteral("cy") << true << qQNaN(); + QTest::newRow("cy: -nan") << QStringLiteral("-nan") << QStringLiteral("cy") << false << 0.0; + QTest::newRow("cy: +nan") << QStringLiteral("+nan") << QStringLiteral("cy") << false << 0.0; + QTest::newRow("cy: inf") << QStringLiteral("inf") << QStringLiteral("cy") << true << qInf(); + QTest::newRow("cy: Inf") << QStringLiteral("Inf") << QStringLiteral("cy") << true << qInf(); + QTest::newRow("cy: +inf") << QStringLiteral("+inf") << QStringLiteral("cy") << true << qInf(); + QTest::newRow("cy: -inf") << QStringLiteral("-inf") << QStringLiteral("cy") << true << -qInf(); + // Samples ready for QTBUG-95460: + QTest::newRow("en: ∞") + << QStringLiteral("\u221e") << QStringLiteral("en") << true << qInf(); + QTest::newRow("ga: Nuimh") + << QStringLiteral("Nuimh") << QStringLiteral("ga") << true << qQNaN(); + + // Locales with multi-character exponents: + QTest::newRow("sv_SE: 4e-3") // Swedish, Sweden + << QStringLiteral("4\u00d7" "10^\u2212" "03") + << QStringLiteral("sv_SE") << true << 4e-3; + QTest::newRow("sv_SE: 4x-3") // Only first character of exponent + << QStringLiteral("4\u00d7\u2212" "03") + << QStringLiteral("sv_SE") << false << 0.0; + QTest::newRow("se_NO: 4e-3") // Northern Sami, Norway + << QStringLiteral("4\u00b7" "10^\u2212" "03") + << QStringLiteral("se_NO") << true << 4e-3; + QTest::newRow("se_NO: 4x-3") // Only first character of exponent + << QStringLiteral("4\u00b7\u2212" "03") + << QStringLiteral("se_NO") << false << 0.0; + QTest::newRow("ar_EG: 4e-3") // Arabic, Egypt + << QStringLiteral("\u0664\u0627\u0633\u061c-\u0660\u0663") + << QStringLiteral("ar_EG") << true << 4e-3; + QTest::newRow("ar_EG: 4x-3") // Only first character of exponent + << QStringLiteral("\u0664\u0627\u061c-\u0660\u0663") + << QStringLiteral("ar_EG") << false << 0.0; + QTest::newRow("ar_EG: 4e!3") // Only first character of sign + << QStringLiteral("\u0664\u0627\u0633\u061c\u0660\u0663") + << QStringLiteral("ar_EG") << false << 0.0; + QTest::newRow("ar_EG: 4x!3") // Only first character of sign and exponent + << QStringLiteral("\u0664\u0627\u061c\u0660\u0663") + << QStringLiteral("ar_EG") << false << 0.0; +} + +void tst_QLocale::toDouble() +{ + QFETCH(QString, text); + QFETCH(QString, locale); + QFETCH(bool, good); + QFETCH(double, expected); + + const QLocale loc(locale); + double actual = expected; + bool ok = false; + QBENCHMARK { + actual = loc.toDouble(text, &ok); + } + QEXPECT_FAIL("sv_SE: 4e-3", "Code wrongly assumes single character, QTBUG-107801", Abort); + QEXPECT_FAIL("se_NO: 4e-3", "Code wrongly assumes single character, QTBUG-107801", Abort); + QEXPECT_FAIL("ar_EG: 4e-3", "Code wrongly assumes single character, QTBUG-107801", Abort); + QEXPECT_FAIL("en: ∞", "Localized infinity support missing: QTBUG-95460", Abort); + QEXPECT_FAIL("ga: Nuimh", "Localized NaN support missing: QTBUG-95460", Abort); + QCOMPARE(ok, good); + QCOMPARE(actual, expected); +} + QTEST_MAIN(tst_QLocale) #include "main.moc" diff --git a/tests/benchmarks/corelib/text/qstring/main.cpp b/tests/benchmarks/corelib/text/qstring/main.cpp index 826a843c10..1c7c3d690e 100644 --- a/tests/benchmarks/corelib/text/qstring/main.cpp +++ b/tests/benchmarks/corelib/text/qstring/main.cpp @@ -28,6 +28,7 @@ #include <QStringList> #include <QFile> #include <QtTest/QtTest> +#include <limits> class tst_QString: public QObject { @@ -49,6 +50,14 @@ private slots: void toCaseFolded_data(); void toCaseFolded(); + // Parsing: + void toLongLong_data(); + void toLongLong(); + void toULongLong_data(); + void toULongLong(); + void toDouble_data(); + void toDouble(); + private: void section_data_impl(bool includeRegExOnly = true); template <typename RX> void section_impl(); @@ -188,6 +197,155 @@ void tst_QString::toCaseFolded() } } +template <typename Integer> +void toWholeCommon_data() +{ + QTest::addColumn<QString>("text"); + QTest::addColumn<int>("base"); + QTest::addColumn<bool>("good"); + QTest::addColumn<Integer>("expected"); + + QTest::newRow("empty") << QStringLiteral("") << 10 << false << Integer(0ull); + QTest::newRow("0") << QStringLiteral("0") << 10 << true << Integer(0ull); + QTest::newRow("1234") << QStringLiteral("1234") << 10 << true << Integer(1234ull); + QTest::newRow("1,234") << QStringLiteral("1,234") << 10 << false << Integer(0ull); + QTest::newRow("123456789") + << QStringLiteral("123456789") << 10 << true << Integer(123456789ull); + QTest::newRow("bad1dea, base 16") + << QStringLiteral("bad1dea") << 16 << true << Integer(0xBAD1DEAull); + QTest::newRow("bad1dea, base 10") << QStringLiteral("bad1dea") << 10 << false << Integer(0ull); + QTest::newRow("42, base 13") << QStringLiteral("42") << 13 << true << Integer(6ull * 9ull); + QTest::newRow("242, base 8") << QStringLiteral("242") << 8 << true << Integer(0242ull); + QTest::newRow("495, base 8") << QStringLiteral("495") << 8 << false << Integer(0ull); + QTest::newRow("101101, base 2") + << QStringLiteral("101101") << 2 << true << Integer(0b101101ull); + QTest::newRow("ad, base 30") << QStringLiteral("ad") << 30 << true << Integer(313ull); +} + +void tst_QString::toLongLong_data() +{ + toWholeCommon_data<qlonglong>(); + + QTest::newRow("-1234") << QStringLiteral("-1234") << 10 << true << -1234ll; + QTest::newRow("-123456789") << QStringLiteral("-123456789") << 10 << true << -123456789ll; + QTest::newRow("-bad1dea, base 16") << QStringLiteral("-bad1dea") << 16 << true << -0xBAD1DEAll; + QTest::newRow("-242, base 8") << QStringLiteral("-242") << 8 << true << -0242ll; + QTest::newRow("-101101, base 2") << QStringLiteral("-101101") << 2 << true << -0b101101ll; + QTest::newRow("-ad, base 30") << QStringLiteral("-ad") << 30 << true << -313ll; + + QTest::newRow("qlonglong-max") + << QStringLiteral("9223372036854775807") << 10 << true + << std::numeric_limits<qlonglong>::max(); + QTest::newRow("qlonglong-min") + << QStringLiteral("-9223372036854775808") << 10 << true + << std::numeric_limits<qlonglong>::min(); + QTest::newRow("qlonglong-max, base 2") + << QString(63, u'1') << 2 << true << std::numeric_limits<qlonglong>::max(); + QTest::newRow("qlonglong-min, base 2") + << (QStringLiteral("-1") + QString(63, u'0')) << 2 << true + << std::numeric_limits<qlonglong>::min(); + QTest::newRow("qlonglong-max, base 16") + << (QChar(u'7') + QString(15, u'f')) << 16 << true + << std::numeric_limits<qlonglong>::max(); + QTest::newRow("qlonglong-min, base 16") + << (QStringLiteral("-8") + QString(15, u'0')) << 16 << true + << std::numeric_limits<qlonglong>::min(); +} + +void tst_QString::toLongLong() +{ + QFETCH(QString, text); + QFETCH(int, base); + QFETCH(bool, good); + QFETCH(qlonglong, expected); + + qlonglong actual = expected; + bool ok = false; + QBENCHMARK { + actual = text.toLongLong(&ok, base); + } + QCOMPARE(ok, good); + QCOMPARE(actual, expected); +} + +void tst_QString::toULongLong_data() +{ + toWholeCommon_data<qulonglong>(); + + QTest::newRow("qlonglong-max + 1") + << QStringLiteral("9223372036854775808") << 10 << true + << (qulonglong(std::numeric_limits<qlonglong>::max()) + 1); + QTest::newRow("qulonglong-max") + << QStringLiteral("18446744073709551615") << 10 << true + << std::numeric_limits<qulonglong>::max(); + QTest::newRow("qulonglong-max, base 2") + << QString(64, u'1') << 2 << true << std::numeric_limits<qulonglong>::max(); + QTest::newRow("qulonglong-max, base 16") + << QString(16, u'f') << 16 << true << std::numeric_limits<qulonglong>::max(); +} + +void tst_QString::toULongLong() +{ + QFETCH(QString, text); + QFETCH(int, base); + QFETCH(bool, good); + QFETCH(qulonglong, expected); + + qulonglong actual = expected; + bool ok = false; + QBENCHMARK { + actual = text.toULongLong(&ok, base); + } + QCOMPARE(ok, good); + QCOMPARE(actual, expected); +} + +void tst_QString::toDouble_data() +{ + QTest::addColumn<QString>("text"); + QTest::addColumn<bool>("good"); + QTest::addColumn<double>("expected"); + + QTest::newRow("empty") << QStringLiteral("") << false << 0.0; + QTest::newRow("0") << QStringLiteral("0") << true << 0.0; + QTest::newRow("0.12340") << QStringLiteral("0.12340") << true << 0.12340; + QTest::newRow("-0.12340") << QStringLiteral("-0.12340") << true << -0.12340; + QTest::newRow("epsilon") + << QStringLiteral("2.220446049e-16") << true << std::numeric_limits<double>::epsilon(); + QTest::newRow("1.0e-4") << QStringLiteral("1.0e-4") << true << 1.0e-4; + QTest::newRow("1.0e+4") << QStringLiteral("1.0e+4") << true << 1.0e+4; + QTest::newRow("10.e+3") << QStringLiteral("10.e+3") << true << 1.0e+4; + QTest::newRow("10e+3.") << QStringLiteral("10e+3.") << false << 0.0; + QTest::newRow("1e4") << QStringLiteral("1e4") << true << 1.0e+4; + QTest::newRow("1.0e-8") << QStringLiteral("1.0e-8") << true << 1.0e-8; + QTest::newRow("1.0e+8") << QStringLiteral("1.0e+8") << true << 1.0e+8; + + // NaN and infinity: + QTest::newRow("nan") << QStringLiteral("nan") << true << qQNaN(); + QTest::newRow("NaN") << QStringLiteral("NaN") << true << qQNaN(); + QTest::newRow("-nan") << QStringLiteral("-nan") << false << 0.0; + QTest::newRow("+nan") << QStringLiteral("+nan") << false << 0.0; + QTest::newRow("inf") << QStringLiteral("inf") << true << qInf(); + QTest::newRow("Inf") << QStringLiteral("Inf") << true << qInf(); + QTest::newRow("+inf") << QStringLiteral("+inf") << true << qInf(); + QTest::newRow("-inf") << QStringLiteral("-inf") << true << -qInf(); +} + +void tst_QString::toDouble() +{ + QFETCH(QString, text); + QFETCH(bool, good); + QFETCH(double, expected); + + double actual = expected; + bool ok = false; + QBENCHMARK { + actual = text.toDouble(&ok); + } + QCOMPARE(ok, good); + QCOMPARE(actual, expected); +} + QTEST_APPLESS_MAIN(tst_QString) #include "main.moc" diff --git a/tests/benchmarks/corelib/time/qdatetime/main.cpp b/tests/benchmarks/corelib/time/qdatetime/main.cpp index 740e08cc46..14e7c43a99 100644 --- a/tests/benchmarks/corelib/time/qdatetime/main.cpp +++ b/tests/benchmarks/corelib/time/qdatetime/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2022 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -36,24 +36,12 @@ class tst_QDateTime : public QObject { Q_OBJECT - enum : qint64 - { - SECS_PER_DAY = 86400, - MSECS_PER_DAY = 86400000, - JULIAN_DAY_1950 = 2433283, - JULIAN_DAY_1960 = 2436935, - JULIAN_DAY_1970 = 2440588, // Epoch - JULIAN_DAY_2010 = 2455198, - JULIAN_DAY_2011 = 2455563, - JULIAN_DAY_2020 = 2458850, - JULIAN_DAY_2050 = 2469808, - JULIAN_DAY_2060 = 2473460 - }; - static QVector<QDateTime> daily(qint64 start, qint64 end); static QVector<QDateTime> norse(qint64 start, qint64 end); + void decade_data(); private Q_SLOTS: + void create_data() { decade_data(); } void create(); void isNull(); void isValid(); @@ -62,12 +50,10 @@ private Q_SLOTS: void timeSpec(); void offsetFromUtc(); void timeZoneAbbreviation(); + void toMSecsSinceEpoch_data() { decade_data(); } void toMSecsSinceEpoch(); - void toMSecsSinceEpoch1950(); - void toMSecsSinceEpoch2050(); + void toMSecsSinceEpochTz_data() { decade_data(); } void toMSecsSinceEpochTz(); - void toMSecsSinceEpoch1950Tz(); - void toMSecsSinceEpoch2050Tz(); void setDate(); void setTime(); void setTimeSpec(); @@ -102,6 +88,33 @@ private Q_SLOTS: void fromMSecsSinceEpochTz(); }; +constexpr qint64 SECS_PER_DAY = 86400; +constexpr qint64 MSECS_PER_DAY = 86400000; +constexpr qint64 JULIAN_DAY_1 = 1721426; +constexpr qint64 JULIAN_DAY_11 = 1725078; +constexpr qint64 JULIAN_DAY_1890 = 2411369; +constexpr qint64 JULIAN_DAY_1900 = 2415021; +constexpr qint64 JULIAN_DAY_1950 = 2433283; +constexpr qint64 JULIAN_DAY_1960 = 2436935; +constexpr qint64 JULIAN_DAY_1970 = 2440588; // Epoch +constexpr qint64 JULIAN_DAY_2010 = 2455198; +constexpr qint64 JULIAN_DAY_2011 = 2455563; +constexpr qint64 JULIAN_DAY_2020 = 2458850; +constexpr qint64 JULIAN_DAY_2050 = 2469808; +constexpr qint64 JULIAN_DAY_2060 = 2473460; + +void tst_QDateTime::decade_data() +{ + QTest::addColumn<qint64>("startJd"); + QTest::addColumn<qint64>("stopJd"); + + QTest::newRow("first-decade-CE") << JULIAN_DAY_1 << JULIAN_DAY_11; + QTest::newRow("1890s") << JULIAN_DAY_1890 << JULIAN_DAY_1900; + QTest::newRow("1950s") << JULIAN_DAY_1950 << JULIAN_DAY_1960; + QTest::newRow("2010s") << JULIAN_DAY_2010 << JULIAN_DAY_2020; + QTest::newRow("2050s") << JULIAN_DAY_2050 << JULIAN_DAY_2060; +} + QVector<QDateTime> tst_QDateTime::daily(qint64 start, qint64 end) { QVector<QDateTime> list; @@ -123,9 +136,12 @@ QVector<QDateTime> tst_QDateTime::norse(qint64 start, qint64 end) void tst_QDateTime::create() { + QFETCH(const qint64, startJd); + QFETCH(const qint64, stopJd); + const QTime noon = QTime::fromMSecsSinceStartOfDay(43200); QBENCHMARK { - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) { - QDateTime test(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0)); + for (int jd = startJd; jd < stopJd; ++jd) { + QDateTime test(QDate::fromJulianDay(jd), noon); Q_UNUSED(test) } } @@ -196,25 +212,9 @@ void tst_QDateTime::timeZoneAbbreviation() void tst_QDateTime::toMSecsSinceEpoch() { - const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); - QBENCHMARK { - for (const QDateTime &test : list) - test.toMSecsSinceEpoch(); - } -} - -void tst_QDateTime::toMSecsSinceEpoch1950() -{ - const auto list = daily(JULIAN_DAY_1950, JULIAN_DAY_1960); - QBENCHMARK { - for (const QDateTime &test : list) - test.toMSecsSinceEpoch(); - } -} - -void tst_QDateTime::toMSecsSinceEpoch2050() -{ - const auto list = daily(JULIAN_DAY_2050, JULIAN_DAY_2060); + QFETCH(const qint64, startJd); + QFETCH(const qint64, stopJd); + const auto list = daily(startJd, stopJd); QBENCHMARK { for (const QDateTime &test : list) test.toMSecsSinceEpoch(); @@ -223,30 +223,11 @@ void tst_QDateTime::toMSecsSinceEpoch2050() void tst_QDateTime::toMSecsSinceEpochTz() { - qint64 result; - const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); - QBENCHMARK { - for (const QDateTime &test : list) - result = test.toMSecsSinceEpoch(); - } - Q_UNUSED(result); -} - -void tst_QDateTime::toMSecsSinceEpoch1950Tz() -{ - qint64 result; - const auto list = norse(JULIAN_DAY_1950, JULIAN_DAY_1960); - QBENCHMARK { - for (const QDateTime &test : list) - result = test.toMSecsSinceEpoch(); - } - Q_UNUSED(result); -} + QFETCH(const qint64, startJd); + QFETCH(const qint64, stopJd); + const auto list = norse(startJd, stopJd); -void tst_QDateTime::toMSecsSinceEpoch2050Tz() -{ qint64 result; - const auto list = norse(JULIAN_DAY_2050, JULIAN_DAY_2060); QBENCHMARK { for (const QDateTime &test : list) result = test.toMSecsSinceEpoch(); diff --git a/tests/benchmarks/corelib/tools/qcryptographichash/main.cpp b/tests/benchmarks/corelib/tools/qcryptographichash/main.cpp index 1d414161d1..a29c409a44 100644 --- a/tests/benchmarks/corelib/tools/qcryptographichash/main.cpp +++ b/tests/benchmarks/corelib/tools/qcryptographichash/main.cpp @@ -1,5 +1,6 @@ /**************************************************************************** ** +** Copyright (C) 2023 The Qt Company Ltd. ** Copyright (C) 2017 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** @@ -29,10 +30,15 @@ #include <QByteArray> #include <QCryptographicHash> #include <QFile> +#include <QMetaEnum> +#include <QMessageAuthenticationCode> #include <QRandomGenerator> #include <QString> #include <QtTest> +#include <functional> +#include <numeric> + #include <time.h> class tst_bench_QCryptographicHash : public QObject @@ -40,6 +46,8 @@ class tst_bench_QCryptographicHash : public QObject Q_OBJECT QByteArray blockOfData; + using Algorithm = QCryptographicHash::Algorithm; + public: tst_bench_QCryptographicHash(); @@ -50,47 +58,25 @@ private Q_SLOTS: void addData(); void addDataChunked_data() { hash_data(); } void addDataChunked(); + + // QMessageAuthenticationCode: + void hmac_hash_data() { hash_data(); } + void hmac_hash(); + void hmac_addData_data() { hash_data(); } + void hmac_addData(); + void hmac_setKey_data(); + void hmac_setKey(); }; -const int MaxCryptoAlgorithm = QCryptographicHash::Sha3_512; const int MaxBlockSize = 65536; -const char *algoname(int i) +static void for_each_algorithm(std::function<void(QCryptographicHash::Algorithm, const char*)> f) { - switch (QCryptographicHash::Algorithm(i)) { - case QCryptographicHash::Md4: - return "md4-"; - case QCryptographicHash::Md5: - return "md5-"; - case QCryptographicHash::Sha1: - return "sha1-"; - case QCryptographicHash::Sha224: - return "sha2_224-"; - case QCryptographicHash::Sha256: - return "sha2_256-"; - case QCryptographicHash::Sha384: - return "sha2_384-"; - case QCryptographicHash::Sha512: - return "sha2_512-"; - case QCryptographicHash::Sha3_224: - return "sha3_224-"; - case QCryptographicHash::Sha3_256: - return "sha3_256-"; - case QCryptographicHash::Sha3_384: - return "sha3_384-"; - case QCryptographicHash::Sha3_512: - return "sha3_512-"; - case QCryptographicHash::Keccak_224: - return "keccak_224-"; - case QCryptographicHash::Keccak_256: - return "keccak_256-"; - case QCryptographicHash::Keccak_384: - return "keccak_384-"; - case QCryptographicHash::Keccak_512: - return "keccak_512-"; - } - Q_UNREACHABLE(); - return 0; + Q_ASSERT(f); + using A = QCryptographicHash::Algorithm; + static const auto metaEnum = QMetaEnum::fromType<A>(); + for (int i = 0, value = metaEnum.value(i); value != -1; value = metaEnum.value(++i)) + f(A(value), metaEnum.key(i)); } tst_bench_QCryptographicHash::tst_bench_QCryptographicHash() @@ -110,7 +96,7 @@ tst_bench_QCryptographicHash::tst_bench_QCryptographicHash() void tst_bench_QCryptographicHash::hash_data() { - QTest::addColumn<int>("algorithm"); + QTest::addColumn<Algorithm>("algo"); QTest::addColumn<QByteArray>("data"); static const int datasizes[] = { 0, 1, 64, 65, 512, 4095, 4096, 4097, 65536 }; @@ -118,17 +104,17 @@ void tst_bench_QCryptographicHash::hash_data() Q_ASSERT(datasizes[i] < MaxBlockSize); QByteArray data = QByteArray::fromRawData(blockOfData.constData(), datasizes[i]); - for (int algo = QCryptographicHash::Md4; algo <= MaxCryptoAlgorithm; ++algo) - QTest::newRow(algoname(algo) + QByteArray::number(datasizes[i])) << algo << data; + for_each_algorithm([&] (Algorithm algo, const char *name) { + QTest::addRow("%s-%d", name, datasizes[i]) << algo << data; + }); } } void tst_bench_QCryptographicHash::hash() { - QFETCH(int, algorithm); + QFETCH(const Algorithm, algo); QFETCH(QByteArray, data); - QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm); QBENCHMARK { QCryptographicHash::hash(data, algo); } @@ -136,10 +122,9 @@ void tst_bench_QCryptographicHash::hash() void tst_bench_QCryptographicHash::addData() { - QFETCH(int, algorithm); + QFETCH(const Algorithm, algo); QFETCH(QByteArray, data); - QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm); QCryptographicHash hash(algo); QBENCHMARK { hash.reset(); @@ -150,10 +135,9 @@ void tst_bench_QCryptographicHash::addData() void tst_bench_QCryptographicHash::addDataChunked() { - QFETCH(int, algorithm); + QFETCH(const Algorithm, algo); QFETCH(QByteArray, data); - QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm); QCryptographicHash hash(algo); QBENCHMARK { hash.reset(); @@ -167,6 +151,73 @@ void tst_bench_QCryptographicHash::addDataChunked() } } +static QByteArray hmacKey() { + static QByteArray key = [] { + QByteArray result(277, Qt::Uninitialized); + std::iota(result.begin(), result.end(), uchar(0)); // uchar so wraps after UCHAR_MAX + return result; + }(); + return key; +} + +void tst_bench_QCryptographicHash::hmac_hash() +{ + QFETCH(const Algorithm, algo); + QFETCH(const QByteArray, data); + + const auto key = hmacKey(); + QBENCHMARK { + auto r = QMessageAuthenticationCode::hash(data, key, algo); + Q_UNUSED(r); + } +} + +void tst_bench_QCryptographicHash::hmac_addData() +{ + QFETCH(const Algorithm, algo); + QFETCH(const QByteArray, data); + + const auto key = hmacKey(); + QMessageAuthenticationCode mac(algo, key); + QBENCHMARK { + mac.reset(); + mac.addData(data); + auto r = mac.result(); + Q_UNUSED(r); + } +} + +void tst_bench_QCryptographicHash::hmac_setKey_data() +{ + QTest::addColumn<Algorithm>("algo"); + for_each_algorithm([] (Algorithm algo, const char *name) { + QTest::addRow("%s", name) << algo; + }); +} + +void tst_bench_QCryptographicHash::hmac_setKey() +{ + QFETCH(const Algorithm, algo); + + const QByteArrayList keys = [] { + QByteArrayList result; + const auto fullKey = hmacKey(); + result.reserve(fullKey.size()); + for (auto i = fullKey.size(); i > 0; --i) + result.push_back(fullKey.mid(i)); + return result; + }(); + + QMessageAuthenticationCode mac(algo); + QBENCHMARK { + for (const auto &key : keys) { + mac.setKey(key); + mac.addData("abc", 3); // avoid lazy setKey() + } + } +} + + QTEST_APPLESS_MAIN(tst_bench_QCryptographicHash) #include "main.moc" |