diff options
Diffstat (limited to 'tests/benchmarks/corelib/text/qlocale')
-rw-r--r-- | tests/benchmarks/corelib/text/qlocale/CMakeLists.txt | 5 | ||||
-rw-r--r-- | tests/benchmarks/corelib/text/qlocale/tst_bench_qlocale.cpp | 250 |
2 files changed, 227 insertions, 28 deletions
diff --git a/tests/benchmarks/corelib/text/qlocale/CMakeLists.txt b/tests/benchmarks/corelib/text/qlocale/CMakeLists.txt index f319c0b5bd..04c2f69b9f 100644 --- a/tests/benchmarks/corelib/text/qlocale/CMakeLists.txt +++ b/tests/benchmarks/corelib/text/qlocale/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + ##################################################################### ## tst_bench_qlocale Binary: ##################################################################### @@ -5,6 +8,6 @@ qt_internal_add_benchmark(tst_bench_qlocale SOURCES tst_bench_qlocale.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Test ) diff --git a/tests/benchmarks/corelib/text/qlocale/tst_bench_qlocale.cpp b/tests/benchmarks/corelib/text/qlocale/tst_bench_qlocale.cpp index 23a64526eb..3d13174c9c 100644 --- a/tests/benchmarks/corelib/text/qlocale/tst_bench_qlocale.cpp +++ b/tests/benchmarks/corelib/text/qlocale/tst_bench_qlocale.cpp @@ -1,34 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QLocale> #include <QTest> +using namespace Qt::StringLiterals; + class tst_QLocale : public QObject { Q_OBJECT @@ -54,6 +31,12 @@ private Q_SLOTS: void toUpper_QLocale_2(); void toUpper_QString(); void number_QString(); + void toLongLong_data(); + void toLongLong(); + void toULongLong_data(); + void toULongLong(); + void toDouble_data(); + void toDouble(); }; static QString data() @@ -392,6 +375,219 @@ void tst_QLocale::number_QString() } } +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") << u""_s << u"C"_s << false << Integer(0ull); + QTest::newRow("C: 0") << u"0"_s << u"C"_s << true << Integer(0ull); + QTest::newRow("C: 1234") << u"1234"_s << u"C"_s << true << Integer(1234ull); + // C locale omits grouping, but doesn't reject it. + QTest::newRow("C: 1,234") << u"1,234"_s << u"C"_s << true << Integer(1234ull); + QTest::newRow("C: 123456789") + << u"123456789"_s << u"C"_s << true << Integer(123456789ull); + QTest::newRow("C: 123,456,789") + << u"123,456,789"_s << u"C"_s << true << Integer(123456789ull); + + QTest::newRow("en: empty") << u""_s << u"en"_s << false << Integer(0ull); + QTest::newRow("en: 0") << u"0"_s << u"en"_s << true << Integer(0ull); + QTest::newRow("en: 1234") << u"1234"_s << u"en"_s << true << Integer(1234ull); + QTest::newRow("en: 1,234") << u"1,234"_s << u"en"_s << true << Integer(1234ull); + QTest::newRow("en: 123,456,789") + << u"123,456,789"_s << u"en"_s << true << Integer(123456789ull); + QTest::newRow("en: 123456789") + << u"123456789"_s << u"en"_s << true << Integer(123456789ull); + + QTest::newRow("de: empty") << u""_s << u"de"_s << false << Integer(0ull); + QTest::newRow("de: 0") << u"0"_s << u"de"_s << true << Integer(0ull); + QTest::newRow("de: 1234") << u"1234"_s << u"de"_s << true << Integer(1234ull); + QTest::newRow("de: 1.234") << u"1.234"_s << u"de"_s << true << Integer(1234ull); + QTest::newRow("de: 123.456.789") + << u"123.456.789"_s << u"de"_s << true << Integer(123456789ull); + QTest::newRow("de: 123456789") + << u"123456789"_s << u"de"_s << true << Integer(123456789ull); + + // Locales with non-single-character signs: + QTest::newRow("ar_EG: +403") // Arabic, Egypt + << u"\u061c+\u0664\u0660\u0663"_s << u"ar_EG"_s << true << Integer(403ull); + QTest::newRow("ar_EG: !403") // Only first character of the sign + << u"\u061c\u0664\u0660\u0663"_s << u"ar_EG"_s << false << Integer(0ull); + QTest::newRow("fa_IR: +403") // Farsi, Iran + << u"\u200e+\u06f4\u06f0\u06f3"_s << u"fa_IR"_s << true << Integer(403ull); + QTest::newRow("fa_IR: !403") // Only first character of sign + << u"\u200e\u06f4\u06f0\u06f3"_s << u"fa_IR"_s << false << Integer(0ull); +} + +void tst_QLocale::toLongLong_data() +{ + toWholeCommon_data<qlonglong>(); + + QTest::newRow("C: -1234") << u"-1234"_s << u"C"_s << true << -1234ll; + QTest::newRow("C: -123456789") << u"-123456789"_s << u"C"_s << true << -123456789ll; + QTest::newRow("C: qlonglong-max") + << u"9223372036854775807"_s << u"C"_s << true + << std::numeric_limits<qlonglong>::max(); + QTest::newRow("C: qlonglong-min") + << u"-9223372036854775808"_s << u"C"_s << true + << std::numeric_limits<qlonglong>::min(); + + // Locales with multi-character signs: + QTest::newRow("ar_EG: -403") // Arabic, Egypt + << u"\u061c-\u0664\u0660\u0663"_s << u"ar_EG"_s << true << -403ll; + QTest::newRow("fa_IR: -403") // Farsi, Iran + << u"\u200e\u2212\u06f4\u06f0\u06f3"_s << u"fa_IR"_s << 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); + } + QCOMPARE(ok, good); + QCOMPARE(actual, expected); +} + +void tst_QLocale::toULongLong_data() +{ + toWholeCommon_data<qulonglong>(); + + QTest::newRow("C: qlonglong-max + 1") + << u"9223372036854775808"_s << u"C"_s << true + << (qulonglong(std::numeric_limits<qlonglong>::max()) + 1); + QTest::newRow("C: qulonglong-max") + << u"18446744073709551615"_s << u"C"_s << 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); + } + 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") << u""_s << u"C"_s << false << 0.0; + QTest::newRow("C: 0") << u"0"_s << u"C"_s << true << 0.0; + QTest::newRow("C: 0.12340") << u"0.12340"_s << u"C"_s << true << 0.12340; + QTest::newRow("C: -0.12340") << u"-0.12340"_s << u"C"_s << true << -0.12340; + QTest::newRow("C: −0.12340") << u"\u2212" "0.12340"_s << u"C"_s << true << -0.12340; + QTest::newRow("C: 1.0e-4") << u"1.0e-4"_s << u"C"_s << true << 1.0e-4; + QTest::newRow("C: 1.0e−4") << u"1.0e\u2212" "4"_s << u"C"_s << true << 1.0e-4; + QTest::newRow("C: 1.0e+4") << u"1.0e+4"_s << u"C"_s << true << 1.0e+4; + QTest::newRow("C: 10.e+3") << u"10.e+3"_s << u"C"_s << true << 1.0e+4; + QTest::newRow("C: 10e+3.") << u"10e+3."_s << u"C"_s << false << 0.0; // exp...dot + QTest::newRow("C: 1e4") << u"1e4"_s << u"C"_s << true << 1.0e+4; + + // NaN and infinity: + QTest::newRow("C: nan") << u"nan"_s << u"C"_s << true << qQNaN(); + QTest::newRow("C: NaN") << u"NaN"_s << u"C"_s << true << qQNaN(); + QTest::newRow("C: -nan") << u"-nan"_s << u"C"_s << false << 0.0; + QTest::newRow("C: +nan") << u"+nan"_s << u"C"_s << false << 0.0; + QTest::newRow("C: inf") << u"inf"_s << u"C"_s << true << qInf(); + QTest::newRow("C: Inf") << u"Inf"_s << u"C"_s << true << qInf(); + QTest::newRow("C: +inf") << u"+inf"_s << u"C"_s << true << qInf(); + QTest::newRow("C: -inf") << u"-inf"_s << u"C"_s << true << -qInf(); + + // Wantonly long-form representations, with trailing and leading zeros: + QTest::newRow("C: 1e-64 long-form") + << (u"0."_s + QString(63, u'0') + u'1' + QString(962, u'0')) << u"C"_s << true << 1e-64; + QTest::newRow("C: 1e+64 long-form") + << (QString(961, u'0') + u'1' + QString(64, u'0') + u".0"_s) << u"C"_s << true << 1e+64; + QTest::newRow("C: long-form 1 via e+64") + << (u"0."_s + QString(63, u'0') + u'1' + QString(962, u'0') + u"e+64"_s) + << u"C"_s << true << 1.0; + QTest::newRow("C: long-form 1 via e-64") + << (QString(961, u'0') + u'1' + QString(64, u'0') + u".0e-64"_s) + << u"C"_s << true << 1.0; + QTest::newRow("C: 12345678.9") << u"12345678.9"_s << u"C"_s << true << 12345678.9; + + // With and without grouping, en vs de for flipped separators: + QTest::newRow("en: 12345678.9") << u"12345678.9"_s << u"en"_s << true << 12345678.9; + QTest::newRow("en: 12,345,678.9") << u"12,345,678.9"_s << u"en"_s << true << 12'345'678.9; + QTest::newRow("de: 12345678,9") << u"12345678,9"_s << u"de"_s << true << 12345678.9; + QTest::newRow("de: 12.345.678,9") << u"12.345.678,9"_s << u"de"_s << true << 12'345'678.9; + + // NaN and infinity are locale-independent (for now - QTBUG-95460) + QTest::newRow("cy: nan") << u"nan"_s << u"cy"_s << true << qQNaN(); + QTest::newRow("cy: NaN") << u"NaN"_s << u"cy"_s << true << qQNaN(); + QTest::newRow("cy: -nan") << u"-nan"_s << u"cy"_s << false << 0.0; + QTest::newRow("cy: +nan") << u"+nan"_s << u"cy"_s << false << 0.0; + QTest::newRow("cy: inf") << u"inf"_s << u"cy"_s << true << qInf(); + QTest::newRow("cy: Inf") << u"Inf"_s << u"cy"_s << true << qInf(); + QTest::newRow("cy: +inf") << u"+inf"_s << u"cy"_s << true << qInf(); + QTest::newRow("cy: -inf") << u"-inf"_s << u"cy"_s << true << -qInf(); + // Samples ready for QTBUG-95460: + QTest::newRow("en: ∞") << u"\u221e"_s << u"en"_s << true << qInf(); + QTest::newRow("ga: Nuimh") << u"Nuimh"_s << u"ga"_s << true << qQNaN(); + + // Locales with multi-character exponents: + QTest::newRow("sv_SE: 4e-3") // Swedish, Sweden + << u"4\u00d7" "10^\u2212" "03"_s << u"sv_SE"_s << true << 4e-3; + QTest::newRow("sv_SE: 4x-3") // Only first character of exponent + << u"4\u00d7\u2212" "03"_s << u"sv_SE"_s << false << 0.0; + QTest::newRow("se_NO: 4e-3") // Northern Sami, Norway + << u"4\u00b7" "10^\u2212" "03"_s << u"se_NO"_s << true << 4e-3; + QTest::newRow("se_NO: 4x-3") // Only first character of exponent + << u"4\u00b7\u2212" "03"_s << u"se_NO"_s << false << 0.0; + QTest::newRow("ar_EG: 4e-3") // Arabic, Egypt + << u"\u0664\u0627\u0633\u061c-\u0660\u0663"_s << u"ar_EG"_s << true << 4e-3; + QTest::newRow("ar_EG: 4x-3") // Only first character of exponent + << u"\u0664\u0627\u061c-\u0660\u0663"_s << u"ar_EG"_s << false << 0.0; + QTest::newRow("ar_EG: 4e!3") // Only first character of sign + << u"\u0664\u0627\u0633\u061c\u0660\u0663"_s << u"ar_EG"_s << false << 0.0; + QTest::newRow("ar_EG: 4x!3") // Only first character of sign and exponent + << u"\u0664\u0627\u061c\u0660\u0663"_s << u"ar_EG"_s << 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("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 "tst_bench_qlocale.moc" |