summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/text/qlocale/tst_qlocale.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/text/qlocale/tst_qlocale.cpp')
-rw-r--r--tests/auto/corelib/text/qlocale/tst_qlocale.cpp2025
1 files changed, 1552 insertions, 473 deletions
diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp
index a72d163c67..946a7e6821 100644
--- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp
+++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp
@@ -1,30 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite 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) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QLocale>
@@ -46,16 +21,14 @@
#include <float.h>
#include <math.h>
+#include <fenv.h>
-#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
-# include <fenv.h>
-# define QT_USE_FENV
-#endif
-
-#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
+#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
# include <stdlib.h>
#endif
+using namespace Qt::StringLiterals;
+
Q_DECLARE_METATYPE(QLocale::FormatType)
class tst_QLocale : public QObject
@@ -70,15 +43,18 @@ private slots:
#if defined(Q_OS_WIN)
void windowsDefaultLocale();
#endif
-#ifdef Q_OS_MAC
+#ifdef Q_OS_DARWIN
void macDefaultLocale();
#endif
+ void ctor_data();
void ctor();
- void emptyCtor_data();
- void emptyCtor();
+ void ctor_match_land();
+ void systemLocale_data();
+ void systemLocale();
void consistentC();
void matchingLocales();
+
void stringToDouble_data();
void stringToDouble();
void stringToFloat_data();
@@ -90,10 +66,11 @@ private slots:
void long_long_conversion_data();
void long_long_conversion();
void long_long_conversion_extra();
- void testInfAndNan();
+ void infNaN();
void fpExceptions();
void negativeZero_data();
void negativeZero();
+
void dayOfWeek();
void dayOfWeek_data();
void formatDate();
@@ -105,6 +82,15 @@ private slots:
void formatTimeZone();
void toDateTime_data();
void toDateTime();
+ void toDate_data();
+ void toDate();
+ void toTime_data();
+ void toTime();
+
+ void doubleRoundTrip_data();
+ void doubleRoundTrip();
+ void integerRoundTrip_data();
+ void integerRoundTrip();
void negativeNumbers();
void numberOptions();
void dayName_data();
@@ -119,6 +105,15 @@ private slots:
void monthName();
void standaloneMonthName();
+ void languageToString_data();
+ void languageToString();
+ void scriptToString_data();
+ void scriptToString();
+ void territoryToString_data();
+ void territoryToString();
+ void endonym_data();
+ void endonym();
+
void defaultNumberingSystem_data();
void defaultNumberingSystem();
@@ -143,14 +138,18 @@ private slots:
void bcp47Name_data();
void bcp47Name();
- void systemLocale_data();
- void systemLocale();
-
#ifndef QT_NO_SYSTEMLOCALE
+# ifdef QT_BUILD_INTERNAL
+ void mySystemLocale_data();
+ void mySystemLocale();
+# endif
+
void systemLocaleDayAndMonthNames_data();
void systemLocaleDayAndMonthNames();
#endif
+ void numberGrouping_data();
+ void numberGrouping();
void numberGroupingIndia();
void numberFormatChakma();
@@ -171,37 +170,40 @@ private:
QString m_decimal, m_thousand, m_sdate, m_ldate, m_time;
QString m_sysapp;
QStringList cleanEnv;
- bool europeanTimeZone;
+ const bool europeanTimeZone;
void toReal_data();
using TransientLocale = QTestLocaleChange::TransientLocale;
};
tst_QLocale::tst_QLocale()
+ // Some tests are specific to CET, test if it applies:
+ : europeanTimeZone(
+ QDate(2013, 1, 1).startOfDay().offsetFromUtc() == 3600
+ && QDate(2013, 6, 1).startOfDay().offsetFromUtc() == 7200
+ // ICU in a zone not currently doing DST may ignore any historical DST
+ // excursions in its display-names (Africa/Tripoli).
+ && QDate(QDate::currentDate().year(), 1, 1).startOfDay().offsetFromUtc() == 3600
+ && QDate(QDate::currentDate().year(), 7, 1).startOfDay().offsetFromUtc() == 7200)
{
qRegisterMetaType<QLocale::FormatType>("QLocale::FormatType");
-
- // Test if in Central European Time zone
- uint x1 = QDateTime(QDate(1990, 1, 1), QTime()).toSecsSinceEpoch();
- uint x2 = QDateTime(QDate(1990, 6, 1), QTime()).toSecsSinceEpoch();
- europeanTimeZone = (x1 == 631148400 && x2 == 644191200);
}
void tst_QLocale::initTestCase()
{
-#if QT_CONFIG(process)
-# ifdef Q_OS_ANDROID
- m_sysapp = QCoreApplication::applicationDirPath() + "/libsyslocaleapp.so";
-# else // !defined(Q_OS_ANDROID)
+#ifdef Q_OS_ANDROID
+ // We can't start a QProcess on Android, and we anyway skip the test
+ // that uses m_sysapp. So no need to initialize it properly.
+ return;
+#elif QT_CONFIG(process)
const QString syslocaleapp_dir = QFINDTESTDATA("syslocaleapp");
QVERIFY2(!syslocaleapp_dir.isEmpty(),
qPrintable(QStringLiteral("Cannot find 'syslocaleapp' starting from ")
+ QDir::toNativeSeparators(QDir::currentPath())));
m_sysapp = syslocaleapp_dir + QStringLiteral("/syslocaleapp");
-# ifdef Q_OS_WIN
+#ifdef Q_OS_WIN
m_sysapp += QStringLiteral(".exe");
-# endif
-# endif // Q_OS_ANDROID
+#endif
const QFileInfo fi(m_sysapp);
QVERIFY2(fi.exists() && fi.isExecutable(),
qPrintable(QDir::toNativeSeparators(m_sysapp)
@@ -209,7 +211,8 @@ void tst_QLocale::initTestCase()
// Get an environment free of any locale-related variables
cleanEnv.clear();
- foreach (QString const& entry, QProcess::systemEnvironment()) {
+ const QStringList sysenv = QProcess::systemEnvironment();
+ for (const QString &entry : sysenv) {
if (entry.startsWith("LANG=") || entry.startsWith("LC_") || entry.startsWith("LANGUAGE="))
continue;
cleanEnv << entry;
@@ -217,74 +220,146 @@ void tst_QLocale::initTestCase()
#endif // QT_CONFIG(process)
}
-void tst_QLocale::ctor()
+void tst_QLocale::ctor_data()
{
- QLocale default_locale = QLocale::system();
- QLocale::Language default_lang = default_locale.language();
- QLocale::Territory default_country = default_locale.territory();
+ QTest::addColumn<QLocale::Language>("reqLang");
+ QTest::addColumn<QLocale::Script>("reqText");
+ QTest::addColumn<QLocale::Territory>("reqLand");
+ QTest::addColumn<QLocale::Language>("expLang");
+ QTest::addColumn<QLocale::Script>("expText");
+ QTest::addColumn<QLocale::Territory>("expLand");
+
+ // Exact match
+#define ECHO(name, lang, text, land) \
+ QTest::newRow(name) \
+ << QLocale::lang << QLocale::text << QLocale::land \
+ << QLocale::lang << QLocale::text << QLocale::land
+
+ ECHO("zh_Hans_CN", Chinese, SimplifiedHanScript, China);
+ ECHO("zh_Hant_TW", Chinese, TraditionalHanScript, Taiwan);
+ ECHO("zh_Hant_HK", Chinese, TraditionalHanScript, HongKong);
+#undef ECHO
+
+ // Determine territory from language and script:
+#define WHATLAND(name, lang, text, land) \
+ QTest::newRow(name) \
+ << QLocale::lang << QLocale::text << QLocale::AnyTerritory \
+ << QLocale::lang << QLocale::text << QLocale::land
+
+ WHATLAND("zh_Hans", Chinese, SimplifiedHanScript, China);
+ WHATLAND("zh_Hant", Chinese, TraditionalHanScript, Taiwan);
+#undef WHATLAND
+
+ // Determine script from language and territory:
+#define WHATTEXT(name, lang, text, land) \
+ QTest::newRow(name) \
+ << QLocale::lang << QLocale::AnyScript << QLocale::land \
+ << QLocale::lang << QLocale::text << QLocale::land
+
+ WHATTEXT("zh_CN", Chinese, SimplifiedHanScript, China);
+ WHATTEXT("zh_TW", Chinese, TraditionalHanScript, Taiwan);
+ WHATTEXT("zh_HK", Chinese, TraditionalHanScript, HongKong);
+#undef WHATTEXT
+
+ // No exact match, fix by change of territory:
+#define FIXLAND(name, lang, text, land, fixed) \
+ QTest::newRow(name) \
+ << QLocale::lang << QLocale::text << QLocale::land \
+ << QLocale::lang << QLocale::text << QLocale::fixed
+
+ FIXLAND("zh_Hans_TW", Chinese, SimplifiedHanScript, Taiwan, China);
+ FIXLAND("zh_Hans_US", Chinese, SimplifiedHanScript, UnitedStates, China);
+ FIXLAND("zh_Hant_CN", Chinese, TraditionalHanScript, China, Taiwan);
+ FIXLAND("zh_Hant_US", Chinese, TraditionalHanScript, UnitedStates, Taiwan);
+#undef FIXLAND
+
+ // No exact match, fix by change of script:
+#define FIXTEXT(name, lang, text, land, fixed) \
+ QTest::newRow(name) \
+ << QLocale::lang << QLocale::text << QLocale::land \
+ << QLocale::lang << QLocale::fixed << QLocale::land
+
+ FIXTEXT("zh_Latn_CN", Chinese, LatinScript, China, SimplifiedHanScript);
+ FIXTEXT("zh_Latn_TW", Chinese, LatinScript, Taiwan, TraditionalHanScript);
+#undef FIXTEXT
+
+ // No exact match, preserve language:
+#define KEEPLANG(name, lang, text, land, fixtext, fixland) \
+ QTest::newRow(name) \
+ << QLocale::lang << QLocale::text << QLocale::land \
+ << QLocale::lang << QLocale::fixtext << QLocale::fixland
+
+ KEEPLANG("zh_US", Chinese, AnyScript, UnitedStates, SimplifiedHanScript, China);
+ KEEPLANG("zh_Latn_US", Chinese, LatinScript, UnitedStates, SimplifiedHanScript, China);
+#undef KEEPLANG
+
+ // Only territory - likely subtags imply language and script:
+#define LANDFILL(name, lang, text, land) \
+ QTest::newRow(name) \
+ << QLocale::AnyLanguage << QLocale::AnyScript << QLocale::land \
+ << QLocale::lang << QLocale::text << QLocale::land
+
+ LANDFILL("und_CN", Chinese, SimplifiedHanScript, China);
+ LANDFILL("und_TW", Chinese, TraditionalHanScript, Taiwan);
+ LANDFILL("und_CA", English, LatinScript, Canada);
+ LANDFILL("und_US", English, LatinScript, UnitedStates);
+ LANDFILL("und_GB", English, LatinScript, UnitedKingdom);
+#undef LANDFILL
+}
- qDebug("Default: %s/%s", QLocale::languageToString(default_lang).toLatin1().constData(),
- QLocale::territoryToString(default_country).toLatin1().constData());
+void tst_QLocale::ctor()
+{
+ QFETCH(const QLocale::Language, reqLang);
+ QFETCH(const QLocale::Script, reqText);
+ QFETCH(const QLocale::Territory, reqLand);
{
- QLocale l;
- QVERIFY(l.language() == default_lang);
- QVERIFY(l.territory() == default_country);
+ const QLocale l(reqLang, reqText, reqLand);
+ QTEST(l.language(), "expLang");
+ QTEST(l.script(), "expText");
+ QTEST(l.territory(), "expLand");
}
+ const QLatin1String request(QTest::currentDataTag());
+ if (!request.startsWith(u"und_")) {
+ const QLocale l(request);
+ QTEST(l.language(), "expLang");
+ QTEST(l.script(), "expText");
+ QTEST(l.territory(), "expLand");
+ }
+}
-#define TEST_CTOR(req_lang, req_script, req_country, exp_lang, exp_script, exp_country) \
- do { \
- QLocale l(QLocale::req_lang, QLocale::req_script, QLocale::req_country); \
- QCOMPARE(l.language(), QLocale::exp_lang); \
- QCOMPARE(l.script(), QLocale::exp_script); \
- QCOMPARE(l.territory(), QLocale::exp_country); \
- } while (false)
-
- // Exact matches
- TEST_CTOR(Chinese, SimplifiedHanScript, China,
- Chinese, SimplifiedHanScript, China);
- TEST_CTOR(Chinese, TraditionalHanScript, Taiwan,
- Chinese, TraditionalHanScript, Taiwan);
- TEST_CTOR(Chinese, TraditionalHanScript, HongKong,
- Chinese, TraditionalHanScript, HongKong);
-
- // Best match for AnyTerritory
- TEST_CTOR(Chinese, SimplifiedHanScript, AnyTerritory,
- Chinese, SimplifiedHanScript, China);
- TEST_CTOR(Chinese, TraditionalHanScript, AnyTerritory,
- Chinese, TraditionalHanScript, Taiwan);
-
- // Best match for AnyScript (and change country to supported one, if necessary)
- TEST_CTOR(Chinese, AnyScript, China,
- Chinese, SimplifiedHanScript, China);
- TEST_CTOR(Chinese, AnyScript, Taiwan,
- Chinese, TraditionalHanScript, Taiwan);
- TEST_CTOR(Chinese, AnyScript, HongKong,
- Chinese, TraditionalHanScript, HongKong);
- TEST_CTOR(Chinese, AnyScript, UnitedStates,
- Chinese, SimplifiedHanScript, China);
-
- // Fully-specified not found; find best alternate country
- TEST_CTOR(Chinese, SimplifiedHanScript, Taiwan,
- Chinese, SimplifiedHanScript, China);
- TEST_CTOR(Chinese, SimplifiedHanScript, UnitedStates,
- Chinese, SimplifiedHanScript, China);
- TEST_CTOR(Chinese, TraditionalHanScript, China,
- Chinese, TraditionalHanScript, Taiwan);
- TEST_CTOR(Chinese, TraditionalHanScript, UnitedStates,
- Chinese, TraditionalHanScript, Taiwan);
-
- // Fully-specified not found; find best alternate script
- TEST_CTOR(Chinese, LatinScript, China,
- Chinese, SimplifiedHanScript, China);
- TEST_CTOR(Chinese, LatinScript, Taiwan,
- Chinese, TraditionalHanScript, Taiwan);
-
- // Fully-specified not found; find best alternate country and script
- TEST_CTOR(Chinese, LatinScript, UnitedStates,
- Chinese, SimplifiedHanScript, China);
-
-#undef TEST_CTOR
+void tst_QLocale::ctor_match_land()
+{
+ // QTBUG-64940: QLocale(Any, Any, land).territory() should normally be land:
+ constexpr QLocale::Territory exceptions[] = {
+ // There are, however, some exceptions:
+ QLocale::AmericanSamoa,
+ QLocale::Antarctica,
+ QLocale::AscensionIsland,
+ QLocale::BouvetIsland,
+ QLocale::CaribbeanNetherlands,
+ QLocale::ClippertonIsland,
+ QLocale::Curacao,
+ QLocale::Europe,
+ QLocale::EuropeanUnion,
+ QLocale::FrenchSouthernTerritories,
+ QLocale::Haiti,
+ QLocale::HeardAndMcDonaldIslands,
+ QLocale::OutlyingOceania,
+ QLocale::Palau,
+ QLocale::Samoa,
+ QLocale::SouthGeorgiaAndSouthSandwichIslands,
+ QLocale::TokelauTerritory,
+ QLocale::TristanDaCunha,
+ QLocale::TuvaluTerritory,
+ QLocale::Vanuatu
+ };
+ for (int i = int(QLocale::AnyTerritory) + 1; i <= int(QLocale::LastTerritory); ++i) {
+ const auto land = QLocale::Territory(i);
+ if (std::find(std::begin(exceptions), std::end(exceptions), land) != std::end(exceptions))
+ continue;
+ QCOMPARE(QLocale(QLocale::AnyLanguage, QLocale::AnyScript, land).territory(), land);
+ }
}
void tst_QLocale::defaulted_ctor()
@@ -293,8 +368,14 @@ void tst_QLocale::defaulted_ctor()
QLocale::Language default_lang = default_locale.language();
QLocale::Territory default_country = default_locale.territory();
- qDebug("Default: %s/%s", QLocale::languageToString(default_lang).toLatin1().constData(),
- QLocale::territoryToString(default_country).toLatin1().constData());
+ qDebug("Default: %s/%s", QLocale::languageToString(default_lang).toUtf8().constData(),
+ QLocale::territoryToString(default_country).toUtf8().constData());
+
+ {
+ QLocale l;
+ QCOMPARE(l.language(), default_lang);
+ QCOMPARE(l.territory(), default_country);
+ }
{
QLocale l(QLocale::C, QLocale::AnyTerritory);
@@ -302,200 +383,181 @@ void tst_QLocale::defaulted_ctor()
QCOMPARE(l.territory(), QLocale::AnyTerritory);
}
+#define CHECK_DEFAULT(lang, terr) \
+ do { \
+ const QLocale l; \
+ QCOMPARE(l.language(), lang); \
+ QCOMPARE(l.territory(), terr); \
+ } while (false)
+
#define TEST_CTOR(req_lang, req_country, exp_lang, exp_country) \
- { \
- QLocale l(QLocale::req_lang, QLocale::req_country); \
- QCOMPARE((int)l.language(), (int)exp_lang); \
- QCOMPARE((int)l.territory(), (int)exp_country); \
- }
+ do { \
+ const QLocale l(QLocale::req_lang, QLocale::req_country); \
+ QCOMPARE(l.language(), exp_lang); \
+ QCOMPARE(l.territory(), exp_country); \
+ } while (false)
- TEST_CTOR(AnyLanguage, AnyTerritory, default_lang, default_country)
- TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory)
- TEST_CTOR(Aymara, AnyTerritory, default_lang, default_country)
- TEST_CTOR(Aymara, France, default_lang, default_country)
+ TEST_CTOR(AnyLanguage, AnyTerritory, default_lang, default_country);
+ TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory);
+ TEST_CTOR(Aymara, AnyTerritory, default_lang, default_country);
+ TEST_CTOR(Aymara, France, default_lang, default_country);
- TEST_CTOR(English, AnyTerritory, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, UnitedStates, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, France, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom)
+ TEST_CTOR(English, AnyTerritory, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, UnitedStates, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, France, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom);
- TEST_CTOR(French, France, QLocale::French, QLocale::France)
- TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory)
+ TEST_CTOR(French, France, QLocale::French, QLocale::France);
+ TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory);
TEST_CTOR(Spanish, LatinAmerica, QLocale::Spanish,
- QLocale::LatinAmerica)
+ QLocale::LatinAmerica);
QLocale::setDefault(QLocale(QLocale::English, QLocale::France));
+ CHECK_DEFAULT(QLocale::English, QLocale::UnitedStates);
- {
- QLocale l;
- QVERIFY(l.language() == QLocale::English);
- QVERIFY(l.territory() == QLocale::UnitedStates);
- }
+ TEST_CTOR(French, France, QLocale::French, QLocale::France);
+ TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom);
- TEST_CTOR(French, France, QLocale::French, QLocale::France)
- TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom)
-
- TEST_CTOR(French, France, QLocale::French, QLocale::France)
- TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory)
- TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory)
- TEST_CTOR(Aymara, AnyTerritory, QLocale::English, QLocale::UnitedStates)
+ TEST_CTOR(French, France, QLocale::French, QLocale::France);
+ TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory);
+ TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory);
+ TEST_CTOR(Aymara, AnyTerritory, QLocale::English, QLocale::UnitedStates);
QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedKingdom));
+ CHECK_DEFAULT(QLocale::English, QLocale::UnitedKingdom);
- {
- QLocale l;
- QVERIFY(l.language() == QLocale::English);
- QVERIFY(l.territory() == QLocale::UnitedKingdom);
- }
-
- TEST_CTOR(French, France, QLocale::French, QLocale::France)
- TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom)
+ TEST_CTOR(French, France, QLocale::French, QLocale::France);
+ TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom);
- TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory)
- TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory)
+ TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory);
+ TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory);
QLocale::setDefault(QLocale(QLocale::Aymara, QLocale::France));
+ CHECK_DEFAULT(QLocale::English, QLocale::UnitedKingdom);
- {
- QLocale l;
- QVERIFY(l.language() == QLocale::English);
- QVERIFY(l.territory() == QLocale::UnitedKingdom);
- }
+ TEST_CTOR(Aymara, AnyTerritory, QLocale::English, QLocale::UnitedKingdom);
+ TEST_CTOR(Aymara, France, QLocale::English, QLocale::UnitedKingdom);
- TEST_CTOR(Aymara, AnyTerritory, QLocale::English, QLocale::UnitedKingdom)
- TEST_CTOR(Aymara, France, QLocale::English, QLocale::UnitedKingdom)
+ TEST_CTOR(English, AnyTerritory, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, UnitedStates, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, France, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom);
- TEST_CTOR(English, AnyTerritory, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, UnitedStates, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, France, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom)
-
- TEST_CTOR(French, France, QLocale::French, QLocale::France)
- TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory)
- TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory)
+ TEST_CTOR(French, France, QLocale::French, QLocale::France);
+ TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory);
+ TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory);
QLocale::setDefault(QLocale(QLocale::Aymara, QLocale::AnyTerritory));
-
- {
- QLocale l;
- QVERIFY(l.language() == QLocale::English);
- QVERIFY(l.territory() == QLocale::UnitedKingdom);
- }
-
- TEST_CTOR(Aymara, AnyTerritory, QLocale::English, QLocale::UnitedKingdom)
- TEST_CTOR(Aymara, France, QLocale::English, QLocale::UnitedKingdom)
-
- TEST_CTOR(English, AnyTerritory, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, UnitedStates, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, France, QLocale::English, QLocale::UnitedStates)
- TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom)
-
- TEST_CTOR(French, France, QLocale::French, QLocale::France)
- TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory)
- TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory)
-
- TEST_CTOR(Arabic, AnyTerritory, QLocale::Arabic, QLocale::Egypt)
- TEST_CTOR(Dutch, AnyTerritory, QLocale::Dutch, QLocale::Netherlands)
- TEST_CTOR(German, AnyTerritory, QLocale::German, QLocale::Germany)
- TEST_CTOR(Greek, AnyTerritory, QLocale::Greek, QLocale::Greece)
- TEST_CTOR(Malay, AnyTerritory, QLocale::Malay, QLocale::Malaysia)
- TEST_CTOR(Persian, AnyTerritory, QLocale::Persian, QLocale::Iran)
- TEST_CTOR(Portuguese, AnyTerritory, QLocale::Portuguese, QLocale::Brazil)
- TEST_CTOR(Serbian, AnyTerritory, QLocale::Serbian, QLocale::Serbia)
- TEST_CTOR(Somali, AnyTerritory, QLocale::Somali, QLocale::Somalia)
- TEST_CTOR(Spanish, AnyTerritory, QLocale::Spanish, QLocale::Spain)
- TEST_CTOR(Swedish, AnyTerritory, QLocale::Swedish, QLocale::Sweden)
- TEST_CTOR(Uzbek, AnyTerritory, QLocale::Uzbek, QLocale::Uzbekistan)
+ CHECK_DEFAULT(QLocale::English, QLocale::UnitedKingdom);
+
+ TEST_CTOR(Aymara, AnyTerritory, QLocale::English, QLocale::UnitedKingdom);
+ TEST_CTOR(Aymara, France, QLocale::English, QLocale::UnitedKingdom);
+
+ TEST_CTOR(English, AnyTerritory, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, UnitedStates, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, France, QLocale::English, QLocale::UnitedStates);
+ TEST_CTOR(English, UnitedKingdom, QLocale::English, QLocale::UnitedKingdom);
+
+ TEST_CTOR(French, France, QLocale::French, QLocale::France);
+ TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory);
+ TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory);
+
+ TEST_CTOR(Arabic, AnyTerritory, QLocale::Arabic, QLocale::Egypt);
+ TEST_CTOR(Dutch, AnyTerritory, QLocale::Dutch, QLocale::Netherlands);
+ TEST_CTOR(German, AnyTerritory, QLocale::German, QLocale::Germany);
+ TEST_CTOR(Greek, AnyTerritory, QLocale::Greek, QLocale::Greece);
+ TEST_CTOR(Malay, AnyTerritory, QLocale::Malay, QLocale::Malaysia);
+ TEST_CTOR(Persian, AnyTerritory, QLocale::Persian, QLocale::Iran);
+ TEST_CTOR(Portuguese, AnyTerritory, QLocale::Portuguese, QLocale::Brazil);
+ TEST_CTOR(Serbian, AnyTerritory, QLocale::Serbian, QLocale::Serbia);
+ TEST_CTOR(Somali, AnyTerritory, QLocale::Somali, QLocale::Somalia);
+ TEST_CTOR(Spanish, AnyTerritory, QLocale::Spanish, QLocale::Spain);
+ TEST_CTOR(Swedish, AnyTerritory, QLocale::Swedish, QLocale::Sweden);
+ TEST_CTOR(Uzbek, AnyTerritory, QLocale::Uzbek, QLocale::Uzbekistan);
#undef TEST_CTOR
#define TEST_CTOR(req_lc, exp_lang, exp_country) \
- { \
- QLocale l(req_lc); \
- QVERIFY2(l.language() == QLocale::exp_lang \
- && l.territory() == QLocale::exp_country, \
- QString("requested: \"" + QString(req_lc) + "\", got: " \
- + QLocale::languageToString(l.language()) \
- + QLatin1Char('/') \
- + QLocale::territoryToString(l.territory())).toLatin1().constData()); \
- QCOMPARE(l, QLocale(QLocale::exp_lang, QLocale::exp_country)); \
- QCOMPARE(qHash(l), qHash(QLocale(QLocale::exp_lang, QLocale::exp_country))); \
- }
+ do { \
+ const QLocale l(req_lc); \
+ QCOMPARE(l.language(), QLocale::exp_lang); \
+ QCOMPARE(l.territory(), QLocale::exp_country); \
+ const QLocale m(QLocale::exp_lang, QLocale::exp_country); \
+ QCOMPARE(l, m); \
+ QCOMPARE(qHash(l), qHash(m)); \
+ } while (false)
QLocale::setDefault(QLocale(QLocale::C));
const QString empty;
- TEST_CTOR("C", C, AnyTerritory)
- TEST_CTOR("bla", C, AnyTerritory)
- TEST_CTOR("zz", C, AnyTerritory)
- TEST_CTOR("zz_zz", C, AnyTerritory)
- TEST_CTOR("zz...", C, AnyTerritory)
- TEST_CTOR("", C, AnyTerritory)
- TEST_CTOR("en/", C, AnyTerritory)
- TEST_CTOR(empty, C, AnyTerritory)
- TEST_CTOR("en", English, UnitedStates)
- TEST_CTOR("en", English, UnitedStates)
- TEST_CTOR("en.", English, UnitedStates)
- TEST_CTOR("en@", English, UnitedStates)
- TEST_CTOR("en.@", English, UnitedStates)
- TEST_CTOR("en_", English, UnitedStates)
- TEST_CTOR("en_U", English, UnitedStates)
- TEST_CTOR("en_.", English, UnitedStates)
- TEST_CTOR("en_.@", English, UnitedStates)
- TEST_CTOR("en.bla", English, UnitedStates)
- TEST_CTOR("en@bla", English, UnitedStates)
- TEST_CTOR("en_blaaa", English, UnitedStates)
- TEST_CTOR("en_zz", English, UnitedStates)
- TEST_CTOR("en_GB", English, UnitedKingdom)
- TEST_CTOR("en_GB.bla", English, UnitedKingdom)
- TEST_CTOR("en_GB@.bla", English, UnitedKingdom)
- TEST_CTOR("en_GB@bla", English, UnitedKingdom)
- TEST_CTOR("en-GB", English, UnitedKingdom)
- TEST_CTOR("en-GB@bla", English, UnitedKingdom)
- TEST_CTOR("eo", Esperanto, World)
- TEST_CTOR("yi", Yiddish, World)
-
- TEST_CTOR("no", NorwegianBokmal, Norway)
- TEST_CTOR("nb", NorwegianBokmal, Norway)
- TEST_CTOR("nn", NorwegianNynorsk, Norway)
- TEST_CTOR("no_NO", NorwegianBokmal, Norway)
- TEST_CTOR("nb_NO", NorwegianBokmal, Norway)
- TEST_CTOR("nn_NO", NorwegianNynorsk, Norway)
- TEST_CTOR("es_ES", Spanish, Spain)
- TEST_CTOR("es_419", Spanish, LatinAmerica)
- TEST_CTOR("es-419", Spanish, LatinAmerica)
- TEST_CTOR("fr_MA", French, Morocco)
+ TEST_CTOR("C", C, AnyTerritory);
+ TEST_CTOR("bla", C, AnyTerritory);
+ TEST_CTOR("zz", C, AnyTerritory);
+ TEST_CTOR("zz_zz", C, AnyTerritory);
+ TEST_CTOR("zz...", C, AnyTerritory);
+ TEST_CTOR("", C, AnyTerritory);
+ TEST_CTOR("en/", C, AnyTerritory);
+ TEST_CTOR(empty, C, AnyTerritory);
+ TEST_CTOR("en", English, UnitedStates);
+ TEST_CTOR("en", English, UnitedStates);
+ TEST_CTOR("en.", English, UnitedStates);
+ TEST_CTOR("en@", English, UnitedStates);
+ TEST_CTOR("en.@", English, UnitedStates);
+ TEST_CTOR("en_", English, UnitedStates);
+ TEST_CTOR("en_U", English, UnitedStates);
+ TEST_CTOR("en_.", English, UnitedStates);
+ TEST_CTOR("en_.@", English, UnitedStates);
+ TEST_CTOR("en.bla", English, UnitedStates);
+ TEST_CTOR("en@bla", English, UnitedStates);
+ TEST_CTOR("en_blaaa", English, UnitedStates);
+ TEST_CTOR("en_zz", English, UnitedStates);
+ TEST_CTOR("en_GB", English, UnitedKingdom);
+ TEST_CTOR("en_GB.bla", English, UnitedKingdom);
+ TEST_CTOR("en_GB@.bla", English, UnitedKingdom);
+ TEST_CTOR("en_GB@bla", English, UnitedKingdom);
+ TEST_CTOR("en-GB", English, UnitedKingdom);
+ TEST_CTOR("en-GB@bla", English, UnitedKingdom);
+ TEST_CTOR("eo", Esperanto, World);
+ TEST_CTOR("yi", Yiddish, Ukraine);
+
+ TEST_CTOR("no", NorwegianBokmal, Norway);
+ TEST_CTOR("nb", NorwegianBokmal, Norway);
+ TEST_CTOR("nn", NorwegianNynorsk, Norway);
+ TEST_CTOR("no_NO", NorwegianBokmal, Norway);
+ TEST_CTOR("nb_NO", NorwegianBokmal, Norway);
+ TEST_CTOR("nn_NO", NorwegianNynorsk, Norway);
+ TEST_CTOR("es_ES", Spanish, Spain);
+ TEST_CTOR("es_419", Spanish, LatinAmerica);
+ TEST_CTOR("es-419", Spanish, LatinAmerica);
+ TEST_CTOR("fr_MA", French, Morocco);
// test default countries for languages
- TEST_CTOR("zh", Chinese, China)
- TEST_CTOR("zh-Hans", Chinese, China)
- TEST_CTOR("ne", Nepali, Nepal)
+ TEST_CTOR("zh", Chinese, China);
+ TEST_CTOR("zh-Hans", Chinese, China);
+ TEST_CTOR("ne", Nepali, Nepal);
#undef TEST_CTOR
#define TEST_CTOR(req_lc, exp_lang, exp_script, exp_country) \
- { \
- QLocale l(req_lc); \
- QVERIFY2(l.language() == QLocale::exp_lang \
- && l.script() == QLocale::exp_script \
- && l.territory() == QLocale::exp_country, \
- QString("requested: \"" + QString(req_lc) + "\", got: " \
- + QLocale::languageToString(l.language()) \
- + QLatin1Char('/') + QLocale::scriptToString(l.script()) \
- + QLatin1Char('/') + QLocale::territoryToString(l.territory())).toLatin1().constData()); \
- }
+ do { \
+ const QLocale l(req_lc); \
+ QCOMPARE(l.language(), QLocale::exp_lang); \
+ QCOMPARE(l.script(), QLocale::exp_script); \
+ QCOMPARE(l.territory(), QLocale::exp_country); \
+ } while (false)
- TEST_CTOR("zh_CN", Chinese, SimplifiedHanScript, China)
- TEST_CTOR("zh_Hans_CN", Chinese, SimplifiedHanScript, China)
- TEST_CTOR("zh_Hans", Chinese, SimplifiedHanScript, China)
- TEST_CTOR("zh_Hant", Chinese, TraditionalHanScript, Taiwan)
- TEST_CTOR("zh_Hans_MO", Chinese, SimplifiedHanScript, Macau)
- TEST_CTOR("zh_Hant_MO", Chinese, TraditionalHanScript, Macau)
- TEST_CTOR("az_Latn_AZ", Azerbaijani, LatinScript, Azerbaijan)
- TEST_CTOR("ha_NG", Hausa, LatinScript, Nigeria)
+ TEST_CTOR("zh_CN", Chinese, SimplifiedHanScript, China);
+ TEST_CTOR("zh_Hans_CN", Chinese, SimplifiedHanScript, China);
+ TEST_CTOR("zh_Hans", Chinese, SimplifiedHanScript, China);
+ TEST_CTOR("zh_Hant", Chinese, TraditionalHanScript, Taiwan);
+ TEST_CTOR("zh_Hans_MO", Chinese, SimplifiedHanScript, Macau);
+ TEST_CTOR("zh_Hant_MO", Chinese, TraditionalHanScript, Macau);
+ TEST_CTOR("az_Latn_AZ", Azerbaijani, LatinScript, Azerbaijan);
+ TEST_CTOR("ha_NG", Hausa, LatinScript, Nigeria);
- TEST_CTOR("ru", Russian, CyrillicScript, RussianFederation)
- TEST_CTOR("ru_Cyrl", Russian, CyrillicScript, RussianFederation)
+ TEST_CTOR("ru", Russian, CyrillicScript, RussianFederation);
+ TEST_CTOR("ru_Cyrl", Russian, CyrillicScript, RussianFederation);
#undef TEST_CTOR
+#undef CHECK_DEFAULT
}
#if QT_CONFIG(process)
@@ -555,13 +617,13 @@ static inline bool runSysAppTest(const QString &binary,
}
#endif
-void tst_QLocale::emptyCtor_data()
+void tst_QLocale::systemLocale_data()
{
#if !QT_CONFIG(process)
- QSKIP("No qprocess support", SkipAll);
+ QSKIP("No qprocess support");
#endif
#ifdef Q_OS_ANDROID
- QSKIP("This test crashes on Android");
+ QSKIP("Can't start QProcess to run a custom user binary on Android");
#endif
QTest::addColumn<QString>("expected");
@@ -573,45 +635,49 @@ void tst_QLocale::emptyCtor_data()
// Note that the accepted values for fields are implementation-dependent;
// the template is language[_territory][.codeset][@modifier]
+ // "Ordibehesht" is the name (as adapted to English, German or Norsk) of the
+ // second month of the Jalali calendar. If you see anything in Arabic,
+ // setDefault(Persian) has interfered with the system locale setup.
+
// Vanilla:
- ADD_CTOR_TEST("C", "C");
+ ADD_CTOR_TEST("C", "C Ordibehesht");
// Standard forms:
- ADD_CTOR_TEST("en", "en_US");
- ADD_CTOR_TEST("en_GB", "en_GB");
- ADD_CTOR_TEST("de", "de_DE");
+ ADD_CTOR_TEST("en", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en_GB", "en_GB Ordibehesht");
+ ADD_CTOR_TEST("de", "de_DE Ordibehescht");
// Norsk has some quirks:
- ADD_CTOR_TEST("no", "nb_NO");
- ADD_CTOR_TEST("nb", "nb_NO");
- ADD_CTOR_TEST("nn", "nn_NO");
- ADD_CTOR_TEST("no_NO", "nb_NO");
- ADD_CTOR_TEST("nb_NO", "nb_NO");
- ADD_CTOR_TEST("nn_NO", "nn_NO");
+ ADD_CTOR_TEST("no", "nb_NO ordibehesht");
+ ADD_CTOR_TEST("nb", "nb_NO ordibehesht");
+ ADD_CTOR_TEST("nn", "nn_NO ordibehesht");
+ ADD_CTOR_TEST("no_NO", "nb_NO ordibehesht");
+ ADD_CTOR_TEST("nb_NO", "nb_NO ordibehesht");
+ ADD_CTOR_TEST("nn_NO", "nn_NO ordibehesht");
// Not too fussy about case:
- ADD_CTOR_TEST("DE", "de_DE");
- ADD_CTOR_TEST("EN", "en_US");
+ ADD_CTOR_TEST("DE", "de_DE Ordibehescht");
+ ADD_CTOR_TEST("EN", "en_US Ordibehesht");
// Invalid fields
- ADD_CTOR_TEST("bla", "C");
- ADD_CTOR_TEST("zz", "C");
- ADD_CTOR_TEST("zz_zz", "C");
- ADD_CTOR_TEST("zz...", "C");
- ADD_CTOR_TEST("en.bla", "en_US");
- ADD_CTOR_TEST("en@bla", "en_US");
- ADD_CTOR_TEST("en_blaaa", "en_US");
- ADD_CTOR_TEST("en_zz", "en_US");
- ADD_CTOR_TEST("en_GB.bla", "en_GB");
- ADD_CTOR_TEST("en_GB@.bla", "en_GB");
- ADD_CTOR_TEST("en_GB@bla", "en_GB");
+ ADD_CTOR_TEST("bla", "C Ordibehesht");
+ ADD_CTOR_TEST("zz", "C Ordibehesht");
+ ADD_CTOR_TEST("zz_zz", "C Ordibehesht");
+ ADD_CTOR_TEST("zz...", "C Ordibehesht");
+ ADD_CTOR_TEST("en.bla", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en@bla", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en_blaaa", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en_zz", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en_GB.bla", "en_GB Ordibehesht");
+ ADD_CTOR_TEST("en_GB@.bla", "en_GB Ordibehesht");
+ ADD_CTOR_TEST("en_GB@bla", "en_GB Ordibehesht");
// Empty optional fields, but with punctuators supplied
- ADD_CTOR_TEST("en.", "en_US");
- ADD_CTOR_TEST("en@", "en_US");
- ADD_CTOR_TEST("en.@", "en_US");
- ADD_CTOR_TEST("en_", "en_US");
- ADD_CTOR_TEST("en_.", "en_US");
- ADD_CTOR_TEST("en_.@", "en_US");
+ ADD_CTOR_TEST("en.", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en@", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en.@", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en_", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en_.", "en_US Ordibehesht");
+ ADD_CTOR_TEST("en_.@", "en_US Ordibehesht");
#undef ADD_CTOR_TEST
#if QT_CONFIG(process) // for runSysApp
@@ -620,7 +686,7 @@ void tst_QLocale::emptyCtor_data()
QString errorMessage;
if (runSysApp(m_sysapp, QStringList(), cleanEnv, &defaultLoc, &errorMessage)) {
#if defined(Q_OS_MACOS)
- QString localeForInvalidLocale = "C";
+ QString localeForInvalidLocale = "C Ordibehesht";
#else
QString localeForInvalidLocale = defaultLoc;
#endif
@@ -635,7 +701,7 @@ void tst_QLocale::emptyCtor_data()
#endif // process
}
-void tst_QLocale::emptyCtor()
+void tst_QLocale::systemLocale()
{
#if QT_CONFIG(process) // for runSysAppTest
QLatin1String request(QTest::currentDataTag());
@@ -657,22 +723,18 @@ void tst_QLocale::legacyNames()
QLocale::setDefault(QLocale(QLocale::C));
#define TEST_CTOR(req_lc, exp_lang, exp_country) \
- { \
- QLocale l(req_lc); \
- QVERIFY2(l.language() == QLocale::exp_lang \
- && l.territory() == QLocale::exp_country, \
- QString("requested: \"" + QString(req_lc) + "\", got: " \
- + QLocale::languageToString(l.language()) \
- + QLatin1Char('/') \
- + QLocale::territoryToString(l.territory())).toLatin1().constData()); \
- }
+ do { \
+ const QLocale l(req_lc); \
+ QCOMPARE(l.language(), QLocale::exp_lang); \
+ QCOMPARE(l.territory(), QLocale::exp_country); \
+ } while (false)
- TEST_CTOR("mo_MD", Romanian, Moldova)
- TEST_CTOR("no", NorwegianBokmal, Norway)
- TEST_CTOR("sh_ME", Serbian, Montenegro)
- TEST_CTOR("tl", Filipino, Philippines)
- TEST_CTOR("iw", Hebrew, Israel)
- TEST_CTOR("in", Indonesian, Indonesia)
+ TEST_CTOR("mo_MD", Romanian, Moldova);
+ TEST_CTOR("no", NorwegianBokmal, Norway);
+ TEST_CTOR("sh_ME", Serbian, Montenegro);
+ TEST_CTOR("tl", Filipino, Philippines);
+ TEST_CTOR("iw", Hebrew, Israel);
+ TEST_CTOR("in", Indonesian, Indonesia);
#undef TEST_CTOR
}
@@ -738,14 +800,24 @@ void tst_QLocale::unixLocaleName_data()
void tst_QLocale::unixLocaleName()
{
- QFETCH(QLocale::Language, lang);
- QFETCH(QLocale::Territory, land);
- QFETCH(QString, expect);
+ QFETCH(const QLocale::Language, lang);
+ QFETCH(const QLocale::Territory, land);
+ QFETCH(const QString, expect);
+ const auto expected = [expect](QChar ch) {
+ // Kludge around QString::replace() not being const.
+ QString copy = expect;
+ return copy.replace(u'_', ch);
+ };
QLocale::setDefault(QLocale(QLocale::C));
- QLocale locale(lang, land);
+ const QLocale locale(lang, land);
QCOMPARE(locale.name(), expect);
+ QCOMPARE(locale.name(QLocale::TagSeparator::Dash), expected(u'-'));
+ QCOMPARE(locale.name(QLocale::TagSeparator{'|'}), expected(u'|'));
+ QTest::ignoreMessage(QtWarningMsg, "QLocale::name(): "
+ "Using non-ASCII separator '\u00ff' (ff) is unsupported");
+ QCOMPARE(locale.name(QLocale::TagSeparator{'\xff'}), QString());
}
void tst_QLocale::toReal_data()
@@ -870,6 +942,44 @@ void tst_QLocale::toReal_data()
QTest::newRow("de_DE 9.876543,0e-2") << QString("de_DE") << QString("9.876543,0e-2") << false << 0.0;
QTest::newRow("de_DE 9.876543e--2") << QString("de_DE") << QString("9.876543e")+QChar(8722)+QString("2") << false << 0.0;
QTest::newRow("de_DE 9.876543,0e--2") << QString("de_DE") << QString("9.876543,0e")+QChar(8722)+QString("2") << false << 0.0;
+
+ // Signs and exponent separator aren't single characters:
+ QTest::newRow("sv_SE 4e-3") // Swedish, Sweden
+ << u"sv_SE"_s << u"4\u00d7" "10^\u2212" "03"_s << true << 4e-3;
+ QTest::newRow("sv_SE 4x-3") // Only first character of exponent
+ << u"sv_SE"_s << u"4\u00d7\u2212" "03"_s << false << 0.0;
+ QTest::newRow("se_NO 4e-3") // Northern Sami, Norway
+ << u"se_NO"_s << u"4\u00b7" "10^\u2212" "03"_s << true << 4e-3;
+ QTest::newRow("se_NO 4x-3") // Only first character of exponent
+ << u"se_NO"_s << u"4\u00b7\u2212" "03"_s << false << 0.0;
+ QTest::newRow("ar_EG 4e-3") // Arabic, Egypt
+ << u"ar_EG"_s << u"\u0664\u0623\u0633\u061c-\u0660\u0663"_s << true << 4e-3;
+ QTest::newRow("ar_EG 4e!3") // Only first character of sign:
+ << u"ar_EG"_s << u"\u0664\u0623\u0633\u061c\u0660\u0663"_s << false << 0.0;
+ QTest::newRow("ar_EG 4x-3") // Only first character of exponent
+ << u"ar_EG"_s << u"\u0664\u0623\u061c-\u0660\u0663"_s << false << 0.0;
+ QTest::newRow("ar_EG 4x!3") // Only first character of exponent and sign
+ << u"ar_EG"_s << u"\u0664\u0623\u061c\u0660\u0663"_s << false << 0.0;
+ QTest::newRow("fa_IR 4e-3") // Farsi, Iran
+ << u"fa_IR"_s << u"\u06f4\u00d7\u06f1\u06f0^\u200e\u2212\u06f0\u06f3"_s << true << 4e-3;
+ QTest::newRow("fa_IR 4e!3") // Only first character of sign:
+ << u"fa_IR"_s << u"\u06f4\u00d7\u06f1\u06f0^\u200e\u06f0\u06f3"_s << false << 0.0;
+ QTest::newRow("fa_IR 4x-3") // Only first character of exponent
+ << u"fa_IR"_s << u"\u06f4\u00d7\u200e\u2212\u06f0\u06f3"_s << false << 0.0;
+ QTest::newRow("fa_IR 4x!3") // Only first character of exponent and sign
+ << u"fa_IR"_s << u"\u06f4\u00d7\u200e\u06f0\u06f3"_s << false << 0.0;
+
+ // Cyrillic has its own E; only officially used by Ukrainian as exponent,
+ // with other Cyrillic locales using the Latin E. QLocale allows that there
+ // may be some cross-over between these.
+ QTest::newRow("uk_UA Cyrillic E") << u"uk_UA"_s << u"4\u0415-3"_s << true << 4e-3; // Official
+ QTest::newRow("uk_UA Latin E") << u"uk_UA"_s << u"4E-3"_s << true << 4e-3;
+ QTest::newRow("uk_UA Cyrilic e") << u"uk_UA"_s << u"4\u0435-3"_s << true << 4e-3;
+ QTest::newRow("uk_UA Latin e") << u"uk_UA"_s << u"4e-3"_s << true << 4e-3;
+ QTest::newRow("ru_RU Latin E") << u"ru_RU"_s << u"4E-3"_s << true << 4e-3; // Official
+ QTest::newRow("ru_RU Cyrillic E") << u"ru_RU"_s << u"4\u0415-3"_s << true << 4e-3;
+ QTest::newRow("ru_RU Latin e") << u"ru_RU"_s << u"4e-3"_s << true << 4e-3;
+ QTest::newRow("ru_RU Cyrilic e") << u"ru_RU"_s << u"4\u0435-3"_s << true << 4e-3;
}
void tst_QLocale::stringToDouble_data()
@@ -896,6 +1006,13 @@ void tst_QLocale::stringToDouble_data()
// Underflow:
QTest::newRow("C tiny") << QString("C") << QString("2e-324") << false << 0.;
QTest::newRow("C -tiny") << QString("C") << QString("-2e-324") << false << 0.;
+
+ // Test a tiny fraction (well beyond denomal) with a huge exponent:
+ const QString zeros(500, '0');
+ QTest::newRow("C tiny fraction, huge exponent")
+ << u"C"_s << u"0."_s + zeros + u"123e501"_s << true << 1.23;
+ QTest::newRow("uk_UA tiny fraction, huge exponent")
+ << u"uk_UA"_s << u"0,"_s + zeros + u"123\u0415" "501"_s << true << 1.23;
}
void tst_QLocale::stringToDouble()
@@ -927,7 +1044,7 @@ void tst_QLocale::stringToDouble()
QCOMPARE(d, num);
if (std::isfinite(num)) {
double diff = d > num ? d - num : num - d;
- QVERIFY(diff <= MY_DOUBLE_EPSILON);
+ QCOMPARE_LE(diff, MY_DOUBLE_EPSILON);
}
}
@@ -938,7 +1055,7 @@ void tst_QLocale::stringToDouble()
QCOMPARE(d, num);
if (std::isfinite(num)) {
double diff = d > num ? d - num : num - d;
- QVERIFY(diff <= MY_DOUBLE_EPSILON);
+ QCOMPARE_LE(diff, MY_DOUBLE_EPSILON);
}
}
#undef MY_DOUBLE_EPSILON
@@ -982,6 +1099,13 @@ void tst_QLocale::stringToFloat_data()
// Underflow double, too:
QTest::newRow("C tiny") << C << QString("2e-324") << false << 0.;
QTest::newRow("C -tiny") << C << QString("-2e-324") << false << 0.;
+
+ // Test a small fraction (well beyond denomal) with a big exponent:
+ const QString zeros(80, '0');
+ QTest::newRow("C small fraction, big exponent")
+ << u"C"_s << u"0."_s + zeros + u"123e81"_s << true << 1.23;
+ QTest::newRow("uk_UA small fraction, big exponent")
+ << u"uk_UA"_s << u"0,"_s + zeros + u"123\u0415" "81"_s << true << 1.23;
}
void tst_QLocale::stringToFloat()
@@ -998,10 +1122,24 @@ void tst_QLocale::stringToFloat()
QLocale locale(locale_name);
QCOMPARE(locale.name(), locale_name);
+ if constexpr (std::numeric_limits<float>::has_denorm != std::denorm_present) {
+ if (qstrcmp(QTest::currentDataTag(), "C float -min") == 0
+ || qstrcmp(QTest::currentDataTag(), "C float min") == 0)
+ QSKIP("Skipping 'denorm' as this type lacks denormals on this system");
+ }
bool ok;
float f = locale.toFloat(num_str, &ok);
QCOMPARE(ok, good);
+ if constexpr (std::numeric_limits<double>::has_denorm != std::denorm_present) {
+ if (qstrcmp(QTest::currentDataTag(), "C double min") == 0
+ || qstrcmp(QTest::currentDataTag(), "C double -min") == 0
+ || qstrcmp(QTest::currentDataTag(), "C tiny") == 0
+ || qstrcmp(QTest::currentDataTag(), "C -tiny") == 0) {
+ QSKIP("Skipping 'denorm' as this type lacks denormals on this system");
+ }
+ }
+
{
// Make sure result is independent of locale:
TransientLocale ignoreme(LC_ALL, "ar_SA.UTF-8");
@@ -1014,7 +1152,7 @@ void tst_QLocale::stringToFloat()
QCOMPARE(f, fnum);
if (std::isfinite(fnum)) {
float diff = f > fnum ? f - fnum : fnum - f;
- QVERIFY(diff <= MY_FLOAT_EPSILON);
+ QCOMPARE_LE(diff, MY_FLOAT_EPSILON);
}
}
@@ -1025,7 +1163,7 @@ void tst_QLocale::stringToFloat()
QCOMPARE(f, fnum);
if (std::isfinite(fnum)) {
float diff = f > fnum ? f - fnum : fnum - f;
- QVERIFY(diff <= MY_FLOAT_EPSILON);
+ QCOMPARE_LE(diff, MY_FLOAT_EPSILON);
}
}
#undef MY_FLOAT_EPSILON
@@ -1041,6 +1179,52 @@ void tst_QLocale::doubleToString_data()
int shortest = QLocale::FloatingPointShortest;
+ QTest::newRow("C 0 f 0") << QString("C") << QString("0") << 0.0 << 'f' << 0;
+ QTest::newRow("C 0 f 5") << QString("C") << QString("0.00000") << 0.0 << 'f' << 5;
+ QTest::newRow("C 0 f -") << QString("C") << QString("0") << 0.0 << 'f' << shortest;
+ QTest::newRow("C 0 e 0") << QString("C") << QString("0e+00") << 0.0 << 'e' << 0;
+ QTest::newRow("C 0 e 5") << QString("C") << QString("0.00000e+00") << 0.0 << 'e' << 5;
+ QTest::newRow("C 0 e -") << QString("C") << QString("0e+00") << 0.0 << 'e' << shortest;
+ QTest::newRow("C 0 g 0") << QString("C") << QString("0") << 0.0 << 'g' << 0;
+ QTest::newRow("C 0 g 5") << QString("C") << QString("0") << 0.0 << 'g' << 5;
+ QTest::newRow("C 0 g -") << QString("C") << QString("0") << 0.0 << 'g' << shortest;
+
+ double d = std::numeric_limits<double>::max();
+ static const char doublemaxfixed[] =
+ "1797693134862315708145274237317043567980705675258449965989174768031572607800285387605"
+ "8955863276687817154045895351438246423432132688946418276846754670353751698604991057655"
+ "1282076245490090389328944075868508455133942304583236903222948165808559332123348274797"
+ "826204144723168738177180919299881250404026184124858368";
+
+ QTest::newRow("C max f 0") << QString("C") << QString(doublemaxfixed) << d << 'f' << 0;
+ QTest::newRow("C max f 5") << QString("C") << doublemaxfixed + QString(".00000") << d << 'f' << 5;
+ QTest::newRow("C max e 0") << QString("C") << QString("2e+308") << d << 'e' << 0;
+ QTest::newRow("C max g 0") << QString("C") << QString("2e+308") << d << 'g' << 0;
+ QTest::newRow("C max e 5") << QString("C") << QString("1.79769e+308") << d << 'e' << 5;
+ QTest::newRow("C max g 5") << QString("C") << QString("1.7977e+308") << d << 'g' << 5;
+#if QT_CONFIG(doubleconversion)
+ QTest::newRow("C max e -") << QString("C") << QString("1.7976931348623157e+308") << d << 'e' << shortest;
+ QTest::newRow("C max g -") << QString("C") << QString("1.7976931348623157e+308") << d << 'g' << shortest;
+ QTest::newRow("C max f -") << QString("C")
+ << QString("%1").arg("17976931348623157", -int(strlen(doublemaxfixed)), u'0')
+ << d << 'f' << shortest;
+#endif
+
+ d = std::numeric_limits<double>::min();
+ QTest::newRow("C min f 0") << QString("C") << QString("0") << d << 'f' << 0;
+ QTest::newRow("C min f 5") << QString("C") << QString("0.00000") << d << 'f' << 5;
+ QTest::newRow("C min e 0") << QString("C") << QString("2e-308") << d << 'e' << 0;
+ QTest::newRow("C min g 0") << QString("C") << QString("2e-308") << d << 'g' << 0;
+ QTest::newRow("C min e 5") << QString("C") << QString("2.22507e-308") << d << 'e' << 5;
+ QTest::newRow("C min g 5") << QString("C") << QString("2.2251e-308") << d << 'g' << 5;
+#if QT_CONFIG(doubleconversion)
+ QTest::newRow("C min e -") << QString("C") << QString("2.2250738585072014e-308") << d << 'e' << shortest;
+ QTest::newRow("C min f -") << QString("C")
+ << QString("0.%1").arg("22250738585072014", 308 - 1 + std::numeric_limits<double>::max_digits10, u'0')
+ << d << 'f' << shortest;
+ QTest::newRow("C min g -") << QString("C") << QString("2.2250738585072014e-308") << d << 'g' << shortest;
+#endif
+
QTest::newRow("C 3.4 f 5") << QString("C") << QString("3.40000") << 3.4 << 'f' << 5;
QTest::newRow("C 3.4 f 0") << QString("C") << QString("3") << 3.4 << 'f' << 0;
QTest::newRow("C 3.4 e 5") << QString("C") << QString("3.40000e+00") << 3.4 << 'e' << 5;
@@ -1095,7 +1279,9 @@ void tst_QLocale::doubleToString_data()
QTest::newRow("de_DE 1245678900 g -") << QString("de_DE") << QString("1.245.678.900") << 12456789e2 << 'g' << shortest;
QTest::newRow("de_DE 12456789100 g -") << QString("de_DE") << QString("12.456.789.100") << 124567891e2 << 'g' << shortest;
QTest::newRow("de_DE 12456789000 g -") << QString("de_DE") << QString("1,2456789E+10") << 12456789e3 << 'g' << shortest;
- QTest::newRow("de_DE 120000 g -") << QString("de_DE") << QString("120.000") << 12e4 << 'g' << shortest;
+ QTest::newRow("de_DE 12000 g -")
+ << QString("de_DE") << QString("12.000") << 12e3 << 'g' << shortest;
+ // 12e4 has "120.000" and "1.2E+05" of equal length; which shortest picks is unspecified.
QTest::newRow("de_DE 1200000 g -") << QString("de_DE") << QString("1,2E+06") << 12e5 << 'g' << shortest;
QTest::newRow("de_DE 1000 g -") << QString("de_DE") << QString("1.000") << 1e3 << 'g' << shortest;
QTest::newRow("de_DE 10000 g -") << QString("de_DE") << QString("1E+04") << 1e4 << 'g' << shortest;
@@ -1106,6 +1292,19 @@ void tst_QLocale::doubleToString_data()
QTest::newRow("C 0.000003945 e 0") << QString("C") << QString("4e-06") << 0.000003945 << 'e' << 0;
QTest::newRow("C 0.000003945 g 7") << QString("C") << QString("3.945e-06") << 0.000003945 << 'g' << 7;
QTest::newRow("C 0.000003945 g 1") << QString("C") << QString("4e-06") << 0.000003945 << 'g' << 1;
+ QTest::newRow("sv_SE 0.000003945 g 1") // Swedish, Sweden (among others)
+ << u"sv_SE"_s << u"4\u00d7" "10^\u2212" "06"_s << 0.000003945 << 'g' << 1;
+ QTest::newRow("sv_SE 3945e3 g 1")
+ << u"sv_SE"_s << u"4\u00d7" "10^+06"_s << 3945e3 << 'g' << 1;
+ QTest::newRow("se 0.000003945 g 1") // Northern Sami
+ << u"se"_s << u"4\u00b7" "10^\u2212" "06"_s << 0.000003945 << 'g' << 1;
+ QTest::newRow("ar_EG 0.000003945 g 1") // Arabic, Egypt (among others)
+ << u"ar_EG"_s << u"\u0664\u0623\u0633\u061c-\u0660\u0666"_s << 0.000003945 << 'g' << 1;
+ QTest::newRow("ar_EG 3945e3 g 1")
+ << u"ar_EG"_s << u"\u0664\u0623\u0633\u061c+\u0660\u0666"_s << 3945e3 << 'g' << 1;
+ QTest::newRow("fa_IR 0.000003945 g 1") // Farsi, Iran (same for Afghanistan)
+ << u"fa_IR"_s << u"\u06f4\u00d7\u06f1\u06f0^\u200e\u2212\u06f0\u06f6"_s
+ << 0.000003945 << 'g' << 1;
QTest::newRow("C 0.000003945 f 9") << QString("C") << QString("0.000003945") << 0.000003945 << 'f' << 9;
QTest::newRow("C 0.000003945 f -") << QString("C") << QString("0.000003945") << 0.000003945 << 'f' << shortest;
@@ -1207,6 +1406,14 @@ void tst_QLocale::strtod_data()
QTest::newRow("12456789012") << QString("12456789012") << 12456789012.0 << 11 << true;
QTest::newRow("1.2456789012e10") << QString("1.2456789012e10") << 12456789012.0 << 15 << true;
+ // Overflow - fails but reports right length:
+ QTest::newRow("1e2000") << QString("1e2000") << qInf() << 6 << false;
+ QTest::newRow("-1e2000") << QString("-1e2000") << -qInf() << 7 << false;
+
+ // Underflow - fails but reports right length:
+ QTest::newRow("1e-2000") << QString("1e-2000") << 0.0 << 7 << false;
+ QTest::newRow("-1e-2000") << QString("-1e-2000") << 0.0 << 8 << false;
+
// starts with junk, fails
QTest::newRow("a0") << QString("a0") << 0.0 << 0 << false;
QTest::newRow("a0.") << QString("a0.") << 0.0 << 0 << false;
@@ -1238,6 +1445,18 @@ void tst_QLocale::strtod_data()
QTest::newRow("12456789012f") << QString("12456789012f") << 12456789012.0 << 11 << true;
QTest::newRow("1.2456789012e10g") << QString("1.2456789012e10g") << 12456789012.0 << 15 << true;
+ // Overflow, ends with cruft - fails but reports right length:
+ QTest::newRow("1e2000 cruft") << QString("1e2000 cruft") << qInf() << 6 << false;
+ QTest::newRow("-1e2000 cruft") << QString("-1e2000 cruft") << -qInf() << 7 << false;
+
+ // NaN and nan
+ QTest::newRow("NaN") << QString("NaN") << qQNaN() << 3 << true;
+ QTest::newRow("nan") << QString("nan") << qQNaN() << 3 << true;
+
+ // Underflow, ends with cruft - fails but reports right length:
+ QTest::newRow("1e-2000 cruft") << QString("1e-2000 cruft") << 0.0 << 7 << false;
+ QTest::newRow("-1e-2000 cruft") << QString("-1e-2000 cruft") << 0.0 << 8 << false;
+
// "0x" prefix, success but only for the "0" before "x"
QTest::newRow("0x0") << QString("0x0") << 0.0 << 1 << true;
QTest::newRow("0x0.") << QString("0x0.") << 0.0 << 1 << true;
@@ -1261,7 +1480,7 @@ void tst_QLocale::strtod()
QFETCH(int, processed);
QFETCH(bool, ok);
- QByteArray numData = num_str.toLatin1();
+ QByteArray numData = num_str.toUtf8();
const char *end = nullptr;
bool actualOk = false;
double result = qstrtod(numData.constData(), &end, &actualOk);
@@ -1270,9 +1489,9 @@ void tst_QLocale::strtod()
QCOMPARE(actualOk, ok);
QCOMPARE(static_cast<int>(end - numData.constData()), processed);
- // make sure neither QByteArray, QString or QLocale also work
- // (but they don't support incomplete parsing)
- if (processed == num_str.size() || processed == 0) {
+ // Make sure QByteArray, QString and QLocale also work.
+ // (They don't support incomplete parsing, and give 0 for overflow.)
+ if (ok && (processed == num_str.size() || processed == 0)) {
actualOk = false;
QCOMPARE(num_str.toDouble(&actualOk), num);
QCOMPARE(actualOk, ok);
@@ -1395,52 +1614,109 @@ void tst_QLocale::long_long_conversion_extra()
QCOMPARE(l.toString((qulonglong)12345), QString("12,345"));
}
-void tst_QLocale::testInfAndNan()
+void tst_QLocale::infNaN()
{
- double neginf = log(0.0);
- double nan = sqrt(-1.0);
-
-#ifdef Q_OS_WIN
- // these cause INVALID floating point exception so we want to clear the status.
- _clear87();
-#endif
-
- QVERIFY(qIsInf(-neginf));
- QVERIFY(!qIsNaN(-neginf));
- QVERIFY(!qIsFinite(-neginf));
+ // TODO: QTBUG-95460 -- could support localized forms of inf/NaN
+ const QLocale c(QLocale::C);
+ QCOMPARE(c.toString(qQNaN()), u"nan");
+ QCOMPARE(c.toString(qQNaN(), 'e'), u"nan");
+ QCOMPARE(c.toString(qQNaN(), 'f'), u"nan");
+ QCOMPARE(c.toString(qQNaN(), 'g'), u"nan");
+ QCOMPARE(c.toString(qQNaN(), 'E'), u"NAN");
+ QCOMPARE(c.toString(qQNaN(), 'F'), u"NAN");
+ QCOMPARE(c.toString(qQNaN(), 'G'), u"NAN");
+
+ QCOMPARE(c.toString(qInf()), u"inf");
+ QCOMPARE(c.toString(qInf(), 'e'), u"inf");
+ QCOMPARE(c.toString(qInf(), 'f'), u"inf");
+ QCOMPARE(c.toString(qInf(), 'g'), u"inf");
+ QCOMPARE(c.toString(qInf(), 'E'), u"INF");
+ QCOMPARE(c.toString(qInf(), 'F'), u"INF");
+ QCOMPARE(c.toString(qInf(), 'G'), u"INF");
+
+ // Precision is ignored for inf and NaN:
+ QCOMPARE(c.toString(qQNaN(), 'g', 42), u"nan");
+ QCOMPARE(c.toString(qQNaN(), 'G', 42), u"NAN");
+ QCOMPARE(c.toString(qInf(), 'g', 42), u"inf");
+ QCOMPARE(c.toString(qInf(), 'G', 42), u"INF");
+
+ // Case is ignored when parsing inf and NaN:
+ bool ok = false;
+ QCOMPARE(c.toDouble("inf", &ok), qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("INF", &ok), qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("Inf", &ok), qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("+inf", &ok), qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("+INF", &ok), qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("+inF", &ok), qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("-inf", &ok), -qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("-INF", &ok), -qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("-iNf", &ok), -qInf());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("nan", &ok), qQNaN());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("NaN", &ok), qQNaN());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("NAN", &ok), qQNaN());
+ QVERIFY(ok);
+ QCOMPARE(c.toDouble("nAn", &ok), qQNaN());
+ QVERIFY(ok);
+ // Sign is invalid for NaN:
+ QCOMPARE(c.toDouble("-nan", &ok), 0.0);
+ QVERIFY(!ok);
+ QCOMPARE(c.toDouble("+nan", &ok), 0.0);
+ QVERIFY(!ok);
- QVERIFY(!qIsInf(nan));
- QVERIFY(qIsNaN(nan));
- QVERIFY(!qIsFinite(nan));
- QVERIFY(!qIsInf(1.234));
- QVERIFY(!qIsNaN(1.234));
- QVERIFY(qIsFinite(1.234));
+ // Case is ignored when parsing inf and NaN:
+ QCOMPARE(c.toFloat("inf", &ok), float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("INF", &ok), float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("Inf", &ok), float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("+inf", &ok), float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("+INF", &ok), float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("+inF", &ok), float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("-inf", &ok), -float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("-INF", &ok), -float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("-iNf", &ok), -float(qInf()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("nan", &ok), float(qQNaN()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("NaN", &ok), float(qQNaN()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("NAN", &ok), float(qQNaN()));
+ QVERIFY(ok);
+ QCOMPARE(c.toFloat("nAn", &ok), float(qQNaN()));
+ QVERIFY(ok);
+ // Sign is invalid for NaN:
+ QCOMPARE(c.toFloat("-nan", &ok), 0.0f);
+ QVERIFY(!ok);
+ QCOMPARE(c.toFloat("+nan", &ok), 0.0f);
+ QVERIFY(!ok);
}
void tst_QLocale::fpExceptions()
{
-#ifndef _MCW_EM
-#define _MCW_EM 0x0008001F
-#endif
-#ifndef _EM_INEXACT
-#define _EM_INEXACT 0x00000001
-#endif
-
- // check that double-to-string conversion doesn't throw floating point exceptions when they are
- // enabled
-#ifdef Q_OS_WIN
- _clear87();
- unsigned int oldbits = _control87(0, 0);
- _control87( 0 | _EM_INEXACT, _MCW_EM );
-#endif
-
-#ifdef QT_USE_FENV
+#if defined(FE_ALL_EXCEPT) && FE_ALL_EXCEPT != 0
+ // Check that double-to-string conversion doesn't throw floating point
+ // exceptions when they are enabled.
fenv_t envp;
fegetenv(&envp);
feclearexcept(FE_ALL_EXCEPT);
- feenableexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID);
-#endif
QString::number(1000.1245);
QString::number(1.1);
@@ -1448,12 +1724,7 @@ void tst_QLocale::fpExceptions()
QVERIFY(true);
-#ifdef Q_OS_WIN
- _clear87();
- _control87(oldbits, 0xFFFFF);
-#endif
-
-#ifdef QT_USE_FENV
+ QCOMPARE(fetestexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID), 0);
fesetenv(&envp);
#endif
}
@@ -1602,7 +1873,7 @@ void tst_QLocale::formatTime_data()
QTest::newRow("C-quote-dquote-H") << QTime(1, 2, 3) << "C" << "'\"H\"'" << "\"H\"";
QTest::newRow("C-H:m:s.z") << QTime(1, 2, 3, 456) << "C" << "H:m:s.z" << "1:2:3.456";
- QTest::newRow("C-H:m:s.zz") << QTime(1, 2, 3, 456) << "C" << "H:m:s.zz" << "1:2:3.456456";
+ QTest::newRow("C-H:m:s.zz") << QTime(1, 2, 3, 456) << "C" << "H:m:s.zz" << "1:2:3.456";
QTest::newRow("C-H:m:s.zzz") << QTime(1, 2, 3, 456) << "C" << "H:m:s.zzz" << "1:2:3.456";
QTest::newRow("C-H:m:s.z=400") << QTime(1, 2, 3, 400) << "C" << "H:m:s.z" << "1:2:3.4";
QTest::newRow("C-H:m:s.zzz=400") << QTime(1, 2, 3, 400) << "C" << "H:m:s.zzz" << "1:2:3.400";
@@ -1828,30 +2099,36 @@ void tst_QLocale::formatTimeZone()
{
QLocale enUS("en_US");
- QDateTime dt1(QDate(2013, 1, 1), QTime(1, 0, 0), Qt::OffsetFromUTC, 60 * 60);
+ QDateTime dt1(QDate(2013, 1, 1), QTime(1, 0), QTimeZone::fromSecondsAheadOfUtc(60 * 60));
QCOMPARE(enUS.toString(dt1, "t"), QLatin1String("UTC+01:00"));
- QDateTime dt2(QDate(2013, 1, 1), QTime(1, 0, 0), Qt::OffsetFromUTC, -60 * 60);
+ QDateTime dt2(QDate(2013, 1, 1), QTime(1, 0), QTimeZone::fromSecondsAheadOfUtc(-60 * 60));
QCOMPARE(enUS.toString(dt2, "t"), QLatin1String("UTC-01:00"));
- QDateTime dt3(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime dt3(QDate(2013, 1, 1), QTime(0, 0), QTimeZone::UTC);
QCOMPARE(enUS.toString(dt3, "t"), QLatin1String("UTC"));
// LocalTime should vary
if (europeanTimeZone) {
// Time definitely in Standard Time
- QDateTime dt4(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
-#ifdef Q_OS_WIN
- QEXPECT_FAIL("", "Windows only returns long name (QTBUG-32759)", Continue);
-#endif // Q_OS_WIN
- QCOMPARE(enUS.toString(dt4, "t"), QLatin1String("CET"));
+ const QStringList knownCETus = {
+ u"GMT+1"_s, // ICU
+ u"Central Europe Standard Time"_s, // MS (lacks abbreviations)
+ u"Central European Standard Time"_s,
+ u"CET"_s // Standard abbreviation
+ };
+ const QString cet = enUS.toString(QDate(2013, 1, 1).startOfDay(), u"t");
+ QVERIFY2(knownCETus.contains(cet), qPrintable(cet));
// Time definitely in Daylight Time
- QDateTime dt5(QDate(2013, 6, 1), QTime(0, 0, 0), Qt::LocalTime);
-#ifdef Q_OS_WIN
- QEXPECT_FAIL("", "Windows only returns long name (QTBUG-32759)", Continue);
-#endif // Q_OS_WIN
- QCOMPARE(enUS.toString(dt5, "t"), QLatin1String("CEST"));
+ const QStringList knownCESTus = {
+ u"GMT+2"_s, // ICU
+ u"Central Europe Summer Time"_s, // MS (lacks abbreviations)
+ u"Central European Summer Time"_s,
+ u"CEST"_s // Standard abbreviation
+ };
+ const QString cest = enUS.toString(QDate(2013, 6, 1).startOfDay(), u"t");
+ QVERIFY2(knownCESTus.contains(cest), qPrintable(cest));
} else {
qDebug("(Skipped some CET-only tests)");
}
@@ -1861,17 +2138,22 @@ void tst_QLocale::formatTimeZone()
const QDateTime jan(QDate(2010, 1, 1).startOfDay(berlin));
const QDateTime jul(QDate(2010, 7, 1).startOfDay(berlin));
- QCOMPARE(enUS.toString(jan, "t"), berlin.abbreviation(jan));
- QCOMPARE(enUS.toString(jul, "t"), berlin.abbreviation(jul));
+ QCOMPARE(enUS.toString(jan, "t"), berlin.displayName(jan, QTimeZone::ShortName, enUS));
+ QCOMPARE(enUS.toString(jul, "t"), berlin.displayName(jul, QTimeZone::ShortName, enUS));
#endif
- // Current datetime should return current abbreviation
- QCOMPARE(enUS.toString(QDateTime::currentDateTime(), "t"),
- QDateTime::currentDateTime().timeZoneAbbreviation());
+ // Current datetime should use current zone's abbreviation:
+ const auto now = QDateTime::currentDateTime();
+ QString zone;
+#if QT_CONFIG(timezone) // Match logic in QDTP's startsWithLocalTimeZone() helper.
+ zone = now.timeRepresentation().displayName(now, QTimeZone::ShortName, enUS);
+ if (zone.isEmpty()) // Fall back to unlocalized from when no timezone backend:
+#endif
+ zone = now.timeZoneAbbreviation();
+ QCOMPARE(enUS.toString(now, "t"), zone);
- // Time on its own will always be current local time zone
- QCOMPARE(enUS.toString(QTime(1, 2, 3), "t"),
- QDateTime::currentDateTime().timeZoneAbbreviation());
+ // Time on its own will always use the current local time zone:
+ QCOMPARE(enUS.toString(now.time(), "t"), zone);
}
void tst_QLocale::toDateTime_data()
@@ -1880,7 +2162,8 @@ void tst_QLocale::toDateTime_data()
QTest::addColumn<QDateTime>("result");
QTest::addColumn<QString>("format");
QTest::addColumn<QString>("string");
- QTest::addColumn<bool>("clean"); // No non-format letters in format string
+ // No non-format letters in format string, no time-zone (t format):
+ QTest::addColumn<bool>("clean");
QTest::newRow("1C") << "C" << QDateTime(QDate(1974, 12, 1), QTime(5, 14, 0))
<< "d/M/yyyy hh:h:mm" << "1/12/1974 05:5:14" << true;
@@ -1934,6 +2217,18 @@ void tst_QLocale::toDateTime_data()
QTest::newRow("12no_NO") << "no_NO" << QDateTime(QDate(1974, 12, 1), QTime(15, 0, 0))
<< "d'd'dd/M/yyh" << "1d01/12/7415" << false;
+ QTest::newRow("short-ss") // QTBUG-102199: trips over an assert in CET
+ << "C" << QDateTime() // Single-digit seconds does not match ss format.
+ << u"ddd, d MMM yyyy HH:mm:ss"_s << u"Sun, 29 Mar 2020 02:26:3"_s << true;
+
+ QTest::newRow("short-ss-Z") // Same, but with a valid date-time:
+ << "C" << QDateTime()
+ << u"ddd, d MMM yyyy HH:mm:ss t"_s << u"Sun, 29 Mar 2020 02:26:3 Z"_s << false;
+
+ QTest::newRow("s-Z") // Same, but with a format that accepts the single digit:
+ << "C" << QDateTime(QDate(2020, 3, 29), QTime(2, 26, 3), QTimeZone::UTC)
+ << u"ddd, d MMM yyyy HH:mm:s t"_s << u"Sun, 29 Mar 2020 02:26:3 Z"_s << false;
+
QTest::newRow("RFC-1123")
<< "C" << QDateTime(QDate(2007, 11, 1), QTime(18, 8, 30))
<< "ddd, dd MMM yyyy hh:mm:ss 'GMT'" << "Thu, 01 Nov 2007 18:08:30 GMT" << false;
@@ -2025,7 +2320,354 @@ void tst_QLocale::toDateTime()
QCOMPARE(l.toDateTime(string, QLocale::ShortFormat), result);
}
-#ifdef Q_OS_MAC
+void tst_QLocale::toDate_data()
+{
+ QTest::addColumn<QLocale>("locale");
+ QTest::addColumn<QDate>("result");
+ QTest::addColumn<QString>("format");
+ QTest::addColumn<QString>("string");
+ // No non-format letters in format string:
+ QTest::addColumn<bool>("clean");
+
+ const auto C = QLocale::c();
+ QTest::newRow("C-d/M/yyyy")
+ << C << QDate(1974, 12, 1) << u"d/M/yyyy"_s << u"1/12/1974"_s << true;
+ QTest::newRow("C-d/M/yyyyy")
+ << C << QDate(1974, 12, 1) << u"d/M/yyyyy"_s << u"1/12/1974y"_s << false;
+ QTest::newRow("C-dd/MM/yyy")
+ << C << QDate(1974, 1, 1) << u"dd/MM/yyy"_s << u"01/01/74y"_s << false;
+ QTest::newRow("C-ddddd/MMMMM/yy")
+ << C << QDate(1974, 12, 2) << u"ddddd/MMMMM/yy"_s << u"Monday2/December12/74"_s
+ << true;
+ QTest::newRow("C-'dddd'/MMMM/yy")
+ << C << QDate(1974, 12, 1) << u"'dddd'/MMMM/yy"_s << u"dddd/December/74"_s << false;
+ QTest::newRow("C-d'dd'd/MMMM/yyy")
+ << C << QDate(1974, 12, 1) << u"d'dd'd/MMMM/yyy"_s << u"1dd1/December/74y"_s << false;
+ QTest::newRow("C-d'dd'd/MMM'M'/yy")
+ << C << QDate(1974, 12, 1) << u"d'dd'd/MMM'M'/yy"_s << u"1dd1/DecM/74"_s << false;
+ QTest::newRow("C-d'd'dd/M/yy")
+ << C << QDate(1974, 12, 1) << u"d'd'dd/M/yy"_s << u"1d01/12/74"_s << false;
+ // Unpadded value for fixed-width field is wrong:
+ QTest::newRow("bad-day-C")
+ << C << QDate() << u"dd-MMM-yy"_s << u"4-Jun-11"_s << true;
+ QTest::newRow("bad-month-C")
+ << C << QDate() << u"d-MM-yy"_s << u"4-6-11"_s << true;
+ QTest::newRow("bad-year-C")
+ << C << QDate() << u"d-MMM-yyyy"_s << u"4-Jun-11"_s << true;
+ QTest::newRow("ok-C")
+ << C << QDate(1911, 6, 4) << u"d-MMM-yy"_s << u"4-Jun-11"_s << true;
+
+ // Locale-specific details frozen to avoid CLDR update breakage.
+ // However, updating to match CLDR from time to time would be constructive.
+ const QLocale norsk{QLocale::NorwegianBokmal, QLocale::Norway};
+ QTest::newRow("no_NO-d/M/yyyy")
+ << norsk << QDate(1974, 12, 1) << u"d/M/yyyy"_s << u"1/12/1974"_s << true;
+ QTest::newRow("no_NO-d/M/yyyyy")
+ << norsk << QDate(1974, 12, 1) << u"d/M/yyyyy"_s << u"1/12/1974y"_s << false;
+ QTest::newRow("no_NO-dd/MM/yyy")
+ << norsk << QDate(1974, 1, 1) << u"dd/MM/yyy"_s << u"01/01/74y"_s << false;
+ QTest::newRow("no_NO-ddddd/MMMMM/yy")
+ << norsk << QDate(1974, 12, 2) << u"ddddd/MMMMM/yy"_s << u"mandag2/desember12/74"_s
+ << true;
+ QTest::newRow("no_NO-'dddd'/MMMM/yy")
+ << norsk << QDate(1974, 12, 1) << u"'dddd'/MMMM/yy"_s << u"dddd/desember/74"_s
+ << false;
+ QTest::newRow("no_NO-d'dd'd/MMMM/yyy")
+ << norsk << QDate(1974, 12, 1) << u"d'dd'd/MMMM/yyy"_s << u"1dd1/desember/74y"_s
+ << false;
+ QTest::newRow("no_NO-d'dd'd/MMM'M'/yy")
+ << norsk << QDate(1974, 12, 1) << u"d'dd'd/MMM'M'/yy"_s << u"1dd1/des.M/74"_s
+ << false;
+ QTest::newRow("no_NO-d'd'dd/M/yy")
+ << norsk << QDate(1974, 12, 1) << u"d'd'dd/M/yy"_s << u"1d01/12/74"_s << false;
+
+ QTest::newRow("RFC-1123")
+ << C << QDate(2007, 11, 1) << u"ddd, dd MMM yyyy 'GMT'"_s << u"Thu, 01 Nov 2007 GMT"_s
+ << false;
+
+ const QLocale usa{QLocale::English, QLocale::UnitedStates};
+ QTest::newRow("longFormat")
+ << usa << QDate(2009, 1, 5) << u"dddd, MMMM d, yyyy"_s
+ << u"Monday, January 5, 2009"_s << true;
+ QTest::newRow("shortFormat") // Use of two-digit year considered harmful.
+ << usa << QDate(1909, 1, 5) << u"M/d/yy"_s << u"1/5/09"_s << true;
+
+ const QDate date(2017, 02, 25);
+ QTest::newRow("C:long")
+ << C << date << "dddd, d MMMM yyyy" << u"Saturday, 25 February 2017"_s << true;
+ QTest::newRow("C:short")
+ << C << date << u"d MMM yyyy"_s << u"25 Feb 2017"_s << true;
+ QTest::newRow("C:narrow")
+ << C << date << u"d MMM yyyy"_s << u"25 Feb 2017"_s << true;
+
+ // Test the same again with unicode and emoji.
+ QTest::newRow("C:long with emoji")
+ << C << date << u8"dddd, d💪MMMM yyyy" << u8"Saturday, 25💪February 2017" << true;
+ QTest::newRow("C:short with emoji")
+ << C << date << u8"d📞MMM📞yyyy" << u8"25📞Feb📞2017" << true;
+ QTest::newRow("C:narrow with emoji")
+ << C << date << u8"🇬🇧d MMM yyyy🇬🇧"
+ << u8"🇬🇧25 Feb 2017🇬🇧" << true;
+
+ const QLocale fr{QLocale::French};
+ QTest::newRow("fr:long")
+ << fr << date << "dddd d MMMM yyyy" << u"Samedi 25 février 2017"_s << true;
+ QTest::newRow("fr:short")
+ << fr << date << u"dd/MM/yyyy"_s << u"25/02/2017"_s << true;
+
+ // In Turkish, the word for Friday ("Cuma") is a prefix for the word for
+ // Saturday ("Cumartesi")
+ const QLocale turk(QLocale::Turkish);
+ QTest::newRow("tr:long-Cumartesi")
+ << turk << date << u"d MMMM yyyy dddd"_s << u"25 Şubat 2017 Cumartesi"_s << true;
+ QTest::newRow("tr:long-Cuma")
+ << turk << date.addDays(-1) << "d MMMM yyyy dddd" << u"24 Şubat 2017 Cuma"_s << true;
+ QTest::newRow("tr:mashed-Cumartesi")
+ << turk << date << u"d MMMMyyyydddd"_s << u"25 Şubat2017Cumartesi"_s << true;
+ QTest::newRow("tr:mashed-Cuma")
+ << turk << date.addDays(-1) << "ddddd MMMMyyyy" << u"Cuma24 Şubat2017"_s << true;
+ QTest::newRow("tr:short")
+ << turk << date << u"d.MM.yyyy"_s << u"25.02.2017"_s << true;
+
+ const QLocale chakma{QLocale::Chakma};
+ QTest::newRow("ccp:short")
+ << chakma << date << "dd/M/yy"
+ // "𑄸𑄻/𑄸/𑄷𑄽"
+ << QString::fromUcs4(U"\U00011138\U0001113b/\U00011138/\U00011137\U0001113d") << true;
+ QTest::newRow("ccp:long")
+ << chakma << date << "dddd, d MMMM, yyyy"
+ // "𑄥𑄧𑄚𑄨𑄝𑄢𑄴, 𑄸𑄻 𑄜𑄬𑄛𑄴𑄝𑄳𑄢𑄪𑄠𑄢𑄨, 𑄸𑄶𑄷𑄽"
+ << QString::fromUcs4(U"\U00011125\U00011127\U0001111a\U00011128\U0001111d\U00011122"
+ U"\U00011134, \U00011138\U0001113b \U0001111c\U0001112c\U0001111b"
+ U"\U00011134\U0001111d\U00011133\U00011122\U0001112a\U00011120"
+ U"\U00011122\U00011128, \U00011138\U00011136\U00011137\U0001113d")
+ << true;
+}
+
+void tst_QLocale::toDate()
+{
+ QFETCH(const QLocale, locale);
+ QFETCH(const QDate, result);
+ QFETCH(const QString, format);
+ QFETCH(const QString, string);
+ QFETCH(const bool, clean);
+
+ QEXPECT_FAIL("ccp:short", "QTBUG-87111: Handling of code points outside BMP is broken", Abort);
+ QEXPECT_FAIL("ccp:long", "QTBUG-87111: Handling of code points outside BMP is broken", Abort);
+ QCOMPARE(locale.toDate(string, format), result);
+ if (clean) {
+ QCOMPARE(locale.toDate(string.toLower(), format), result);
+ QCOMPARE(locale.toDate(string.toUpper(), format), result);
+ }
+
+ if (locale.dateFormat(QLocale::LongFormat) == format)
+ QCOMPARE(locale.toDate(string, QLocale::LongFormat), result);
+ if (locale.dateFormat(QLocale::ShortFormat) == format)
+ QCOMPARE(locale.toDate(string, QLocale::ShortFormat), result);
+}
+
+void tst_QLocale::toTime_data()
+{
+ QTest::addColumn<QLocale>("locale");
+ QTest::addColumn<QTime>("result");
+ QTest::addColumn<QString>("format");
+ QTest::addColumn<QString>("string");
+ // No non-format letters in format string:
+ QTest::addColumn<bool>("clean");
+
+ const auto C = QLocale::c();
+ QTest::newRow("C-hh:h:mm")
+ << C << QTime(5, 14) << u"hh:h:mm"_s << u"05:5:14"_s << true;
+ QTest::newRow("C-h")
+ << C << QTime(15, 0) << u"h"_s << u"15"_s << true;
+ QTest::newRow("C-zzz")
+ << C << QTime(0, 0, 0, 1) << u"zzz"_s << u"001"_s << true;
+ QTest::newRow("C-z/001")
+ << C << QTime(0, 0, 0, 1) << u"z"_s << u"001"_s << true;
+ QTest::newRow("C-z/1")
+ << C << QTime(0, 0, 0, 100) << u"z"_s << u"1"_s << true;
+ QTest::newRow("C-ss")
+ << C << QTime(0, 0, 13) << u"ss"_s << u"13"_s << true;
+ QTest::newRow("C-s")
+ << C << QTime(0, 0, 13) << u"s"_s << u"13"_s << true;
+ QTest::newRow("C-m'm'mm")
+ << C << QTime(0, 4) << u"m'm'mm"_s << u"4m04"_s << false;
+ QTest::newRow("C-hhmmsss")
+ << C << QTime(0, 0, 3) << u"hhmmsss"_s << u"0000033"_s << true;
+ // Unpadded value for fixed-width field is wrong:
+ QTest::newRow("bad-hour-C")
+ << C << QTime() << u"hh:m"_s << u"1:2"_s << true;
+ QTest::newRow("bad-min-C")
+ << C << QTime() << u"h:mm"_s << u"1:2"_s << true;
+ QTest::newRow("bad-sec-C")
+ << C << QTime() << u"d-MMM-yy h:m:ss"_s << u"4-Jun-11 1:2:3"_s << true;
+ QTest::newRow("bad-milli-C")
+ << C << QTime() << u"h:m:s.zzz"_s << u"1:2:3.4"_s << true;
+ QTest::newRow("ok-C")
+ << C << QTime(1, 2, 3, 400) << u"h:m:s.z"_s << u"1:2:3.4"_s << true;
+
+ // Locale-specific details frozen to avoid CLDR update breakage.
+ // However, updating to match CLDR from time to time would be constructive.
+ const QLocale norsk{QLocale::NorwegianBokmal, QLocale::Norway};
+ QTest::newRow("nb_NO-hh:h:mm")
+ << norsk << QTime(5, 14) << u"hh:h:mm"_s << u"05:5:14"_s << true;
+ QTest::newRow("nb_NO-h")
+ <<norsk << QTime(15, 0) << u"h"_s << u"15"_s << true;
+ QTest::newRow("nb_NO-zzz")
+ <<norsk << QTime(0, 0) << u"zzz"_s << u"000"_s << true;
+ QTest::newRow("nb_NO-z")
+ <<norsk << QTime(0, 0) << u"z"_s << u"0"_s << true;
+ QTest::newRow("nb_NO-ss")
+ <<norsk << QTime(0, 0, 13) << u"ss"_s << u"13"_s << true;
+ QTest::newRow("nb_NO-s")
+ <<norsk << QTime(0, 0, 13) << u"s"_s << u"13"_s << true;
+ QTest::newRow("nb_NO-m'm'mm")
+ <<norsk << QTime(0, 4) << u"m'm'mm"_s << u"4m04"_s << false;
+ QTest::newRow("nb_NO-hhmmsss")
+ <<norsk << QTime(0, 0, 3) << u"hhmmsss"_s << u"0000033"_s << true;
+
+ QTest::newRow("short-ss") // Single-digit seconds does not match ss format.
+ << C << QTime() << u"HH:mm:ss"_s << u"02:26:3"_s << true;
+ QTest::newRow("RFC-1123")
+ << C << QTime(18, 8, 30) << u"hh:mm:ss 'GMT'"_s << u"18:08:30 GMT"_s << false;
+
+ const QLocale usa{QLocale::English, QLocale::UnitedStates};
+ QTest::newRow("longFormat-AM")
+ << usa << QTime(4, 43, 32) << u"h:mm:ss AP "_s << u"4:43:32 AM "_s << true;
+ QTest::newRow("shortFormat-AM")
+ << usa << QTime(4, 43) << u"h:mm AP "_s << u"4:43 AM "_s << true;
+ QTest::newRow("longFormat-PM")
+ << usa << QTime(16, 43, 32) << u"h:mm:ss AP "_s << u"4:43:32 PM "_s << true;
+ QTest::newRow("shortFormat-PM")
+ << usa << QTime(16, 43) << u"h:mm AP "_s << u"4:43 PM "_s << true;
+ // Some locales use a narrow non-breaking space as separator, but
+ // the user can't see the difference from a space (QTBUG-114909):
+ QTest::newRow("shortFormat-AM-mixspace")
+ << usa << QTime(4, 43) << u"h:mm\u202F" "AP "_s << u"4:43 AM "_s << true;
+
+ // Parsing am/pm indicators case-insensitively:
+ const QLocale czech{QLocale::Czech, QLocale::Czechia};
+ QTest::newRow("am-cs_CZ")
+ << czech << QTime(8, 15, 44, 400) << u"hh:mm:ss.z aP"_s << u"08:15:44.4 dOp."_s
+ << true;
+ QTest::newRow("pm-cs_CZ")
+ << czech << QTime(12, 0) << u"hh:mm aP"_s << u"12:00 OdP."_s << true;
+
+ const QTime time(17, 21, 25);
+ QTest::newRow("C:long")
+ << C << time << "HH:mm:ss" << u"17:21:25"_s << true;
+ QTest::newRow("C:short")
+ << C << time << u"HH:mm:ss"_s << u"17:21:25"_s << true;
+ QTest::newRow("C:narrow")
+ << C << time << u"HH:mm:ss"_s << u"17:21:25"_s << true;
+
+ // Test the same again with unicode and emoji.
+ QTest::newRow("C:long with emoji")
+ << C << time << u8"HH💪mm💪ss" << u8"17💪21💪25" << true;
+ QTest::newRow("C:short with emoji")
+ << C << time << u8"HH📞mm📞ss" << u8"17📞21📞25" << true;
+ QTest::newRow("C:narrow with emoji")
+ << C << time << u8"🇬🇧HH:mm:ss🇬🇧"
+ << u8"🇬🇧17:21:25🇬🇧" << true;
+
+ const QLocale fr{QLocale::French};
+ QTest::newRow("fr:long")
+ << fr << time << "HH:mm:ss" << u"17:21:25"_s << true;
+ QTest::newRow("fr:short")
+ << fr << time.addSecs(-25) << u"HH:mm"_s << u"17:21"_s << true;
+ QTest::newRow("tr:short")
+ << QLocale(QLocale::Turkish) << time.addSecs(-25) << u"HH:mm"_s << u"17:21"_s << true;
+
+ const QLocale chakma{QLocale::Chakma};
+ QTest::newRow("ccp:short")
+ << chakma << time << "h:mm AP"
+ // "𑄸𑄻/𑄸/𑄷𑄽 𑄻:𑄸𑄷 PM"
+ << QString::fromUcs4(U"\U0001113b:\U00011138\U00011137 PM") << true;
+ QTest::newRow("ccp:long")
+ << chakma << time << "h:mm:ss AP"
+ // "𑄻:𑄸𑄷:𑄸𑄻 PM"
+ << QString::fromUcs4(U"\U0001113b:\U00011138\U00011137:\U00011138\U0001113b PM") << true;
+}
+
+void tst_QLocale::toTime()
+{
+ QFETCH(const QLocale, locale);
+ QFETCH(const QTime, result);
+ QFETCH(const QString, format);
+ QFETCH(const QString, string);
+ QFETCH(const bool, clean);
+
+ QEXPECT_FAIL("ccp:short", "QTBUG-87111: Handling of code points outside BMP is broken", Abort);
+ QEXPECT_FAIL("ccp:long", "QTBUG-87111: Handling of code points outside BMP is broken", Abort);
+ QCOMPARE(locale.toTime(string, format), result);
+ if (clean) {
+ QCOMPARE(locale.toTime(string.toLower(), format), result);
+ QCOMPARE(locale.toTime(string.toUpper(), format), result);
+ }
+
+ if (locale.timeFormat(QLocale::LongFormat) == format)
+ QCOMPARE(locale.toTime(string, QLocale::LongFormat), result);
+ if (locale.timeFormat(QLocale::ShortFormat) == format)
+ QCOMPARE(locale.toTime(string, QLocale::ShortFormat), result);
+}
+
+void tst_QLocale::doubleRoundTrip_data()
+{
+ QTest::addColumn<QString>("localeName");
+ QTest::addColumn<QString>("numberText");
+ QTest::addColumn<char>("numberFormat");
+
+ // Signs and exponent separator aren't single characters:
+ QTest::newRow("sv_SE 4e-06 g") // Swedish, Sweden
+ << u"sv_SE"_s << u"4\u00d7" "10^\u2212" "06"_s << 'g';
+ QTest::newRow("se_NO 4e-06 g") // Northern Sami, Norway
+ << u"se_NO"_s << u"4\u00b7" "10^\u2212" "06"_s << 'g';
+ QTest::newRow("ar_EG 4e-06 g") // Arabic, Egypt
+ << u"ar_EG"_s << u"\u0664\u0623\u0633\u061c-\u0660\u0666"_s << 'g';
+ QTest::newRow("fa_IR 4e-06 g") // Farsi, Iran
+ << u"fa_IR"_s << u"\u06f4\u00d7\u06f1\u06f0^\u200e\u2212\u06f0\u06f6"_s << 'g';
+}
+
+void tst_QLocale::doubleRoundTrip()
+{
+ QFETCH(QString, localeName);
+ QFETCH(QString, numberText);
+ QFETCH(char, numberFormat);
+
+ QLocale locale(localeName);
+ bool ok;
+
+ double number = locale.toDouble(numberText, &ok);
+ QVERIFY(ok);
+ QCOMPARE(locale.toString(number, numberFormat), numberText);
+}
+
+void tst_QLocale::integerRoundTrip_data()
+{
+ QTest::addColumn<QString>("localeName");
+ QTest::addColumn<QString>("numberText");
+
+ // Two-character signs:
+ // Arabic, Egypt
+ QTest::newRow("ar_EG -406") << u"ar_EG"_s << u"\u061c-\u0664\u0660\u0666"_s;
+ // Farsi, Iran
+ QTest::newRow("fa_IR -406") << u"fa_IR"_s << u"\u200e\u2212\u06f4\u06f0\u06f6"_s;
+}
+
+void tst_QLocale::integerRoundTrip()
+{
+ QFETCH(QString, localeName);
+ QFETCH(QString, numberText);
+
+ QLocale locale(localeName);
+ bool ok;
+
+ qlonglong number = locale.toLongLong(numberText, &ok);
+ QVERIFY(ok);
+ QCOMPARE(locale.toString(number), numberText);
+}
+
+#ifdef Q_OS_DARWIN
// Format number string according to system locale settings.
// Expected in format is US "1,234.56".
@@ -2067,7 +2709,7 @@ void tst_QLocale::macDefaultLocale()
|| locale.groupSeparator() == QStringView(u"\xA0") // no-breaking space
|| locale.groupSeparator() == QStringView(u"'")
|| locale.groupSeparator().isEmpty());
- QVERIFY(locale.decimalPoint() != locale.groupSeparator());
+ QCOMPARE_NE(locale.decimalPoint(), locale.groupSeparator());
// make sure we are using the system to parse them
QCOMPARE(locale.toString(1234.56), systemLocaleFormatNumber(QString("1,234.56")));
@@ -2127,7 +2769,7 @@ void tst_QLocale::macDefaultLocale()
QCOMPARE(locale.weekdays(), days);
}
-#endif // Q_OS_MAC
+#endif // Q_OS_DARWIN
#if defined(Q_OS_WIN)
#include <qt_windows.h>
@@ -2238,7 +2880,7 @@ void tst_QLocale::windowsDefaultLocale()
locale.toString(QDate(1974, 12, 1), QLocale::ShortFormat));
QCOMPARE(locale.toString(QDate(1974, 12, 1), QLocale::LongFormat),
QStringView(u"\u3021@\u3021\u3022@\u3021\u3029\u3027\u3024"));
- const QString expectedFormattedShortTime = QStringView(u"\u3021^\u3022").toString();
+ const QString expectedFormattedShortTime = QStringView(u"\u3021^\u3022^\u3023").toString();
QCOMPARE(locale.toString(QTime(1,2,3), QLocale::ShortFormat), expectedFormattedShortTime);
QCOMPARE(locale.toString(QTime(1,2,3), QLocale::NarrowFormat),
locale.toString(QTime(1,2,3), QLocale::ShortFormat));
@@ -2368,6 +3010,26 @@ void tst_QLocale::negativeNumbers()
i = locale.toInt(QLatin1String("-1000000"), &ok);
QVERIFY(ok);
QCOMPARE(i, -1000000);
+
+ // Several Arabic locales have an invisible script-marker before their signs:
+ const QLocale egypt(QLocale::Arabic, QLocale::Egypt);
+ QCOMPARE(egypt.toString(-403), u"\u061c-\u0664\u0660\u0663"_s);
+ i = egypt.toInt(u"\u061c-\u0664\u0660\u0663"_s, &ok);
+ QVERIFY(ok);
+ QCOMPARE(i, -403);
+ i = egypt.toInt(u"\u061c+\u0664\u0660\u0663"_s, &ok);
+ QVERIFY(ok);
+ QCOMPARE(i, 403);
+
+ // Likewise Farsi:
+ const QLocale farsi(QLocale::Persian, QLocale::Iran);
+ QCOMPARE(farsi.toString(-403), u"\u200e\u2212\u06f4\u06f0\u06f3"_s);
+ i = farsi.toInt(u"\u200e\u2212\u06f4\u06f0\u06f3"_s, &ok);
+ QVERIFY(ok);
+ QCOMPARE(i, -403);
+ i = farsi.toInt(u"\u200e+\u06f4\u06f0\u06f3"_s, &ok);
+ QVERIFY(ok);
+ QCOMPARE(i, 403);
}
#include <private/qlocale_p.h>
@@ -2385,9 +3047,9 @@ void tst_QLocale::testNames_data()
for (int i = 0; i < locale_data_count; ++i) {
const QLocaleData &item = locale_data[i];
const QByteArray lang =
- QLocale::languageToString(QLocale::Language(item.m_language_id)).toLatin1();
+ QLocale::languageToString(QLocale::Language(item.m_language_id)).toUtf8();
const QByteArray land =
- QLocale::territoryToString(QLocale::Territory(item.m_territory_id)).toLatin1();
+ QLocale::territoryToString(QLocale::Territory(item.m_territory_id)).toUtf8();
QTest::addRow("data_%d (%s/%s)", i, lang.constData(), land.constData())
<< QLocale::Language(item.m_language_id) << QLocale::Territory(item.m_territory_id);
@@ -2424,7 +3086,7 @@ void tst_QLocale::testNames()
if (language != QLocale::C) {
const int idx = name.indexOf(QLatin1Char('_'));
- QVERIFY(idx != -1);
+ QCOMPARE_NE(idx, -1);
const QString lang = name.left(idx);
QCOMPARE(QLocale(lang).language(), language);
@@ -2456,27 +3118,29 @@ void tst_QLocale::dayName_data()
QTest::newRow("ru_RU short")
<< QString("ru_RU") << QString::fromUtf8("\320\262\321\201") << 7 << QLocale::ShortFormat;
QTest::newRow("ru_RU narrow")
- << QString("ru_RU") << QString::fromUtf8("\320\262\321\201") << 7 << QLocale::NarrowFormat;
+ << QString("ru_RU") << u"\u0412"_s << 7 << QLocale::NarrowFormat;
+
+ QTest::newRow("ga_IE/Mon") << QString("ga_IE") << QString("Luan") << 1 << QLocale::ShortFormat;
+ QTest::newRow("ga_IE/Sun") << QString("ga_IE") << QString("Domh") << 7 << QLocale::ShortFormat;
+ QTest::newRow("el_GR/Tue")
+ << QString("el_GR") << QString::fromUtf8("\316\244\317\201\316\257")
+ << 2 << QLocale::ShortFormat;
+ QTest::newRow("el_GR/Thu")
+ << QString("el_GR") << QString::fromUtf8("\316\240\316\255\316\274")
+ << 4 << QLocale::ShortFormat;
+ QTest::newRow("el_GR/Sat")
+ << QString("el_GR") << QString::fromUtf8("\316\243\316\254\316\262")
+ << 6 << QLocale::ShortFormat;
}
void tst_QLocale::dayName()
{
QFETCH(QString, locale_name);
- QFETCH(QString, dayName);
QFETCH(int, day);
QFETCH(QLocale::FormatType, format);
QLocale l(locale_name);
- QCOMPARE(l.dayName(day, format), dayName);
-
- QLocale ir("ga_IE");
- QCOMPARE(ir.dayName(1, QLocale::ShortFormat), QLatin1String("Luan"));
- QCOMPARE(ir.dayName(7, QLocale::ShortFormat), QLatin1String("Domh"));
-
- QLocale gr("el_GR");
- QCOMPARE(gr.dayName(2, QLocale::ShortFormat), QString::fromUtf8("\316\244\317\201\316\257"));
- QCOMPARE(gr.dayName(4, QLocale::ShortFormat), QString::fromUtf8("\316\240\316\255\316\274"));
- QCOMPARE(gr.dayName(6, QLocale::ShortFormat), QString::fromUtf8("\316\243\316\254\316\262"));
+ QTEST(l.dayName(day, format), "dayName");
}
void tst_QLocale::standaloneDayName_data()
@@ -2515,12 +3179,11 @@ void tst_QLocale::standaloneDayName_data()
void tst_QLocale::standaloneDayName()
{
QFETCH(QString, locale_name);
- QFETCH(QString, dayName);
QFETCH(int, day);
QFETCH(QLocale::FormatType, format);
QLocale l(locale_name);
- QCOMPARE(l.standaloneDayName(day, format), dayName);
+ QTEST(l.standaloneDayName(day, format), "dayName");
}
void tst_QLocale::underflowOverflow()
@@ -2573,10 +3236,9 @@ void tst_QLocale::defaultNumberingSystem_data()
void tst_QLocale::defaultNumberingSystem()
{
- QFETCH(QString, expect);
QLatin1String name(QTest::currentDataTag());
QLocale locale(name);
- QCOMPARE(locale.toString(123), expect);
+ QTEST(locale.toString(123), "expect");
}
void tst_QLocale::ampm_data()
@@ -2593,17 +3255,16 @@ void tst_QLocale::ampm_data()
QTest::newRow("tr_TR") << QString::fromUtf8("\303\226\303\226")
<< QString::fromUtf8("\303\226\123");
QTest::newRow("id_ID") << QStringLiteral("AM") << QStringLiteral("PM");
- QTest::newRow("ta_LK") << QString::fromUtf8("முற்பகல்") << QString::fromUtf8("பிற்பகல்");
+ // CLDR v44 made Tamil's AM/PM inconsistent; AM was "முற்பகல்" before.
+ QTest::newRow("ta_LK") << QString::fromUtf8("AM") << QString::fromUtf8("பிற்பகல்");
}
void tst_QLocale::ampm()
{
- QFETCH(QString, morn);
- QFETCH(QString, even);
QLatin1String name(QTest::currentDataTag());
QLocale locale(name == QLatin1String("C") ? QLocale(QLocale::C) : QLocale(name));
- QCOMPARE(locale.amText(), morn);
- QCOMPARE(locale.pmText(), even);
+ QTEST(locale.amText(), "morn");
+ QTEST(locale.pmText(), "even");
}
void tst_QLocale::dateFormat()
@@ -2630,6 +3291,31 @@ void tst_QLocale::dateFormat()
const auto sys = QLocale::system(); // QTBUG-92018, ru_RU on MS
const QDate date(2021, 3, 17);
QCOMPARE(sys.toString(date, sys.dateFormat(QLocale::LongFormat)), sys.toString(date));
+
+ // Check that system locale can format a date with year < 1601 (MS cut-off):
+ QString old = sys.toString(QDate(1564, 2, 15), QLocale::LongFormat);
+ QVERIFY(!old.isEmpty());
+ QVERIFY2(old.contains(u"1564"), qPrintable(old + QLatin1String(" for locale ") + sys.name()));
+ old = sys.toString(QDate(1564, 2, 15), QLocale::ShortFormat);
+ QVERIFY(!old.isEmpty());
+ QVERIFY2(old.contains(u"64"), qPrintable(old + QLatin1String(" for locale ") + sys.name()));
+
+ // Including one with year % 100 < 12 (lest we substitute year for month or day)
+ old = sys.toString(QDate(1511, 11, 11), QLocale::LongFormat);
+ QVERIFY(!old.isEmpty());
+ QVERIFY2(old.contains(u"1511"), qPrintable(old + QLatin1String(" for locale ") + sys.name()));
+ old = sys.toString(QDate(1511, 11, 11), QLocale::ShortFormat);
+ QVERIFY(!old.isEmpty());
+ QVERIFY2(old.contains(u"11"), qPrintable(old + QLatin1String(" for locale ") + sys.name()));
+
+ // And, indeed, one for a negative year:
+ old = sys.toString(QDate(-1173, 5, 1), QLocale::LongFormat);
+ QVERIFY(!old.isEmpty());
+ qsizetype yearDigitStart = old.indexOf(u"1173");
+ QVERIFY2(yearDigitStart != -1, qPrintable(old + QLatin1String(" for locale ") + sys.name()));
+ QStringView before = QStringView(old).first(yearDigitStart);
+ QVERIFY2(before.endsWith(QChar('-')) || before.endsWith(QChar(0x2212)),
+ qPrintable(old + QLatin1String(" has no minus sign for locale ") + sys.name()));
}
void tst_QLocale::timeFormat()
@@ -2654,6 +3340,11 @@ void tst_QLocale::timeFormat()
const QLocale bra("pt_BR");
QCOMPARE(bra.timeFormat(QLocale::ShortFormat), QLatin1String("HH:mm"));
QCOMPARE(bra.timeFormat(QLocale::LongFormat), QLatin1String("HH:mm:ss t"));
+
+ // QTBUG-123872 - we kludge CLDR's B to Ap:
+ const QLocale tw("zh_TW");
+ QCOMPARE(tw.timeFormat(QLocale::ShortFormat), "Aph:mm"_L1);
+ QCOMPARE(tw.timeFormat(QLocale::LongFormat), "Aph:mm:ss [t]"_L1);
}
void tst_QLocale::dateTimeFormat()
@@ -2696,7 +3387,7 @@ void tst_QLocale::monthName()
QCOMPARE(ru.monthName(1, QLocale::NarrowFormat), QString::fromUtf8("\320\257"));
const auto sys = QLocale::system();
if (sys.language() == QLocale::Russian) // QTBUG-92018
- QVERIFY(sys.monthName(3) != sys.standaloneMonthName(3));
+ QCOMPARE_NE(sys.monthName(3), sys.standaloneMonthName(3));
const QLocale ir("ga_IE");
QCOMPARE(ir.monthName(1, QLocale::ShortFormat), QLatin1String("Ean"));
@@ -2737,6 +3428,140 @@ void tst_QLocale::standaloneMonthName()
QCOMPARE(ru.standaloneMonthName(1, QLocale::NarrowFormat), QString::fromUtf8("\xd0\xaf"));
}
+void tst_QLocale::languageToString_data()
+{
+ QTest::addColumn<QLocale::Language>("language");
+ QTest::addColumn<QString>("name");
+
+ // Prone to change at CLDR updates.
+ QTest::newRow("cu") << QLocale::Church << u"Church Slavic"_s;
+ QTest::newRow("dyo") << QLocale::JolaFonyi << u"Jola-Fonyi"_s;
+ QTest::newRow("ff") << QLocale::Fulah << u"Fula"_s;
+ QTest::newRow("gd") << QLocale::Gaelic << u"Scottish Gaelic"_s;
+ QTest::newRow("ht") << QLocale::Haitian << u"Haitian Creole"_s;
+ QTest::newRow("lu") << QLocale::LubaKatanga << u"Luba-Katanga"_s;
+ QTest::newRow("mgh") << QLocale::MakhuwaMeetto << u"Makhuwa-Meetto"_s;
+ QTest::newRow("mgo") << QLocale::Meta << u"Meta\u02bc"_s;
+ QTest::newRow("mi") << QLocale::Maori << u"M\u0101" "ori"_s;
+ QTest::newRow("nb") << QLocale::NorwegianBokmal << u"Norwegian Bokm\u00e5" "l"_s;
+ QTest::newRow("nqo") << QLocale::Nko << u"N\u2019" "Ko"_s;
+ QTest::newRow("quc") << QLocale::Kiche << u"K\u02bc" "iche\u02bc"_s;
+ QTest::newRow("sah") << QLocale::Sakha << u"Yakut"_s;
+ QTest::newRow("vo") << QLocale::Volapuk << u"Volap\u00fc" "k"_s;
+}
+
+void tst_QLocale::languageToString()
+{
+ QFETCH(const QLocale::Language, language);
+ QTEST(QLocale::languageToString(language), "name");
+}
+
+void tst_QLocale::scriptToString_data()
+{
+ QTest::addColumn<QLocale::Script>("script");
+ QTest::addColumn<QString>("name");
+
+ // Prone to change at CLDR updates.
+ QTest::newRow("Cans")
+ << QLocale::CanadianAboriginalScript << u"Unified Canadian Aboriginal Syllabics"_s;
+ QTest::newRow("Dupl") << QLocale::DuployanScript << u"Duployan shorthand"_s;
+ QTest::newRow("Egyp") << QLocale::EgyptianHieroglyphsScript << u"Egyptian hieroglyphs"_s;
+ QTest::newRow("Nkoo") << QLocale::NkoScript << u"N\u2019" "Ko"_s;
+ QTest::newRow("Phag") << QLocale::PhagsPaScript << u"Phags-pa"_s;
+ QTest::newRow("Rohg") << QLocale::HanifiScript << u"Hanifi Rohingya"_s;
+ QTest::newRow("Sgnw") << QLocale::SignWritingScript << u"SignWriting"_s;
+ QTest::newRow("Xsux") << QLocale::CuneiformScript << u"Sumero-Akkadian Cuneiform"_s;
+}
+
+void tst_QLocale::scriptToString()
+{
+ QFETCH(const QLocale::Script, script);
+ QTEST(QLocale::scriptToString(script), "name");
+}
+
+void tst_QLocale::territoryToString_data()
+{
+ QTest::addColumn<QLocale::Territory>("territory");
+ QTest::addColumn<QString>("name");
+ // Prone to change at CLDR updates.
+
+ QTest::newRow("AX") << QLocale::AlandIslands << u"\u00c5" "land Islands"_s;
+ QTest::newRow("AG") << QLocale::AntiguaAndBarbuda << u"Antigua & Barbuda"_s;
+ QTest::newRow("BA") << QLocale::BosniaAndHerzegovina << u"Bosnia & Herzegovina"_s;
+ QTest::newRow("BL") << QLocale::SaintBarthelemy << u"St. Barth\u00e9" "lemy"_s;
+ QTest::newRow("CC") << QLocale::CocosIslands << u"Cocos (Keeling) Islands"_s;
+ QTest::newRow("CD") << QLocale::CongoKinshasa << u"Congo - Kinshasa"_s;
+ QTest::newRow("CG") << QLocale::CongoBrazzaville << u"Congo - Brazzaville"_s;
+ QTest::newRow("CI") << QLocale::IvoryCoast << u"C\u00f4" "te d\u2019" "Ivoire"_s;
+ QTest::newRow("CW") << QLocale::Curacao << u"Cura\u00e7" "ao"_s;
+ QTest::newRow("EA") << QLocale::CeutaAndMelilla << u"Ceuta & Melilla"_s;
+ QTest::newRow("GS")
+ << QLocale::SouthGeorgiaAndSouthSandwichIslands
+ << u"South Georgia & South Sandwich Islands"_s;
+ QTest::newRow("GW") << QLocale::GuineaBissau << u"Guinea-Bissau"_s;
+ QTest::newRow("HM") << QLocale::HeardAndMcDonaldIslands << u"Heard & McDonald Islands"_s;
+ QTest::newRow("IM") << QLocale::IsleOfMan << u"Isle of Man"_s;
+ QTest::newRow("KN") << QLocale::SaintKittsAndNevis << u"St. Kitts & Nevis"_s;
+ QTest::newRow("LC") << QLocale::SaintLucia << u"St. Lucia"_s;
+ QTest::newRow("MF") << QLocale::SaintMartin << u"St. Martin"_s;
+ QTest::newRow("MK") << QLocale::Macedonia << u"North Macedonia"_s;
+ QTest::newRow("MM") << QLocale::Myanmar << u"Myanmar (Burma)"_s;
+ QTest::newRow("MO") << QLocale::Macao << u"Macao SAR China"_s;
+ QTest::newRow("PM") << QLocale::SaintPierreAndMiquelon << u"St. Pierre & Miquelon"_s;
+ QTest::newRow("PN") << QLocale::Pitcairn << u"Pitcairn Islands"_s;
+ QTest::newRow("RE") << QLocale::Reunion << u"R\u00e9" "union"_s;
+ QTest::newRow("SH") << QLocale::SaintHelena << u"St. Helena"_s;
+ QTest::newRow("SJ") << QLocale::SvalbardAndJanMayen << u"Svalbard & Jan Mayen"_s;
+ QTest::newRow("ST")
+ << QLocale::SaoTomeAndPrincipe << u"S\u00e3" "o Tom\u00e9" " & Pr\u00ed" "ncipe"_s;
+ QTest::newRow("TA") << QLocale::TristanDaCunha << u"Tristan da Cunha"_s;
+ QTest::newRow("TC") << QLocale::TurksAndCaicosIslands << u"Turks & Caicos Islands"_s;
+ QTest::newRow("TR") << QLocale::Turkey << u"T\u00fc" "rkiye"_s;
+ QTest::newRow("TT") << QLocale::TrinidadAndTobago << u"Trinidad & Tobago"_s;
+ QTest::newRow("UM") << QLocale::UnitedStatesOutlyingIslands << u"U.S. Outlying Islands"_s;
+ QTest::newRow("VC") << QLocale::SaintVincentAndGrenadines << u"St. Vincent & Grenadines"_s;
+ QTest::newRow("VI") << QLocale::UnitedStatesVirginIslands << u"U.S. Virgin Islands"_s;
+ QTest::newRow("WF") << QLocale::WallisAndFutuna << u"Wallis & Futuna"_s;
+ QTest::newRow("001") << QLocale::World << u"world"_s;
+}
+
+void tst_QLocale::territoryToString()
+{
+ QFETCH(const QLocale::Territory, territory);
+ QTEST(QLocale::territoryToString(territory), "name");
+}
+
+void tst_QLocale::endonym_data()
+{
+ QTest::addColumn<QLocale>("locale");
+ QTest::addColumn<QString>("language");
+ QTest::addColumn<QString>("territory");
+
+ QTest::newRow("en")
+ << QLocale(QLocale::English, QLocale::UnitedStates)
+ << u"American English"_s << u"United States"_s;
+ QTest::newRow("en_GB")
+ << QLocale(QLocale::English, QLocale::UnitedKingdom)
+ << u"British English"_s << u"United Kingdom"_s; // So inaccurate
+}
+
+void tst_QLocale::endonym()
+{
+ QFETCH(const QLocale, locale);
+
+ auto report = qScopeGuard([locale]() {
+ qDebug()
+ << "Failed for" << locale.name()
+ << "with language" << QLocale::languageToString(locale.language())
+ << "for territory" << QLocale::territoryToString(locale.territory())
+ << "in script" << QLocale::scriptToString(locale.script());
+ });
+
+ QTEST(locale.nativeLanguageName(), "language");
+ QTEST(locale.nativeTerritoryName(), "territory");
+ report.dismiss();
+}
+
void tst_QLocale::currency()
{
const QLocale c(QLocale::C);
@@ -2816,38 +3641,88 @@ void tst_QLocale::uiLanguages_data()
QTest::newRow("en_US")
<< QLocale("en_US")
- << QStringList{QString("en"), QString("en-US"), QString("en-Latn-US")};
+ << QStringList{QString("en-Latn-US"), QString("en-US"), QString("en")};
QTest::newRow("en_Latn_US")
<< QLocale("en_Latn_US") // Specifying the default script makes no difference
- << QStringList{QString("en"), QString("en-US"), QString("en-Latn-US")};
+ << QStringList{QString("en-Latn-US"), QString("en-US"), QString("en")};
QTest::newRow("en_GB")
<< QLocale("en_GB")
- << QStringList{QString("en-GB"), QString("en-Latn-GB")};
+ << QStringList{QString("en-Latn-GB"), QString("en-GB")};
QTest::newRow("en_Dsrt_US")
<< QLocale("en_Dsrt_US")
- << QStringList{QString("en-Dsrt"), QString("en-Dsrt-US")};
+ << QStringList{QString("en-Dsrt-US"), QString("en-Dsrt")};
QTest::newRow("ru_RU")
<< QLocale("ru_RU")
- << QStringList{QString("ru"), QString("ru-RU"), QString("ru-Cyrl-RU")};
+ << QStringList{QString("ru-Cyrl-RU"), QString("ru-RU"), QString("ru")};
QTest::newRow("zh_Hant")
<< QLocale("zh_Hant")
- << QStringList{QString("zh-TW"), QString("zh-Hant-TW")};
+ << QStringList{QString("zh-Hant-TW"), QString("zh-TW")};
QTest::newRow("zh_Hans_CN")
<< QLocale(QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China)
- << QStringList{QString("zh"), QString("zh-CN"), QString("zh-Hans-CN")};
+ << QStringList{QString("zh-Hans-CN"), QString("zh-CN"), QString("zh")};
+
+ // We presently map und (or any other unrecognized language) to C, ignoring
+ // what a sub-tag lookup would surely find us.
+ QTest::newRow("und_US") << QLocale("und_US") << QStringList{QString("C")};
+ QTest::newRow("und_Latn") << QLocale("und_Latn") << QStringList{QString("C")};
}
void tst_QLocale::uiLanguages()
{
+ // Compare mySystemLocale(), which tests the same for a custom system locale.
QFETCH(const QLocale, locale);
QFETCH(const QStringList, all);
- QCOMPARE(locale.uiLanguages(), all);
+ const auto expected = [all](QChar sep) {
+ QStringList adjusted;
+ for (QString name : all)
+ adjusted << name.replace(u'-', sep);
+ return adjusted;
+ };
+
+ {
+ // By default tags are joined with a dash:
+ const QStringList actual = locale.uiLanguages();
+ auto reporter = qScopeGuard([&actual]() {
+ qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1)));
+ });
+ QCOMPARE(actual, all);
+ reporter.dismiss();
+ }
+ {
+ // We also support joining with an underscore:
+ const QStringList actual = locale.uiLanguages(QLocale::TagSeparator::Underscore);
+ auto reporter = qScopeGuard([&actual]() {
+ qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1)));
+ });
+ QCOMPARE(actual, expected(u'_'));
+ reporter.dismiss();
+ }
+ {
+ // Or, in fact, any ASCII character:
+ const QStringList actual = locale.uiLanguages(QLocale::TagSeparator{'|'});
+ auto reporter = qScopeGuard([&actual]() {
+ qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1)));
+ });
+ QCOMPARE(actual, expected(u'|'));
+ reporter.dismiss();
+ }
+ {
+ // Non-ASCII separator (here, y-umlaut) is unsupported.
+ QTest::ignoreMessage(QtWarningMsg, "QLocale::uiLanguages(): "
+ "Using non-ASCII separator '\u00ff' (ff) is unsupported");
+ const QStringList actual = locale.uiLanguages(QLocale::TagSeparator{'\xff'});
+ auto reporter = qScopeGuard([&actual]() {
+ qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1)));
+ });
+ QCOMPARE(actual, QStringList{});
+ reporter.dismiss();
+ }
}
void tst_QLocale::weekendDays()
@@ -2908,8 +3783,7 @@ void tst_QLocale::measurementSystems_data()
void tst_QLocale::measurementSystems()
{
QFETCH(QLocale, locale);
- QFETCH(QLocale::MeasurementSystem, system);
- QCOMPARE(locale.measurementSystem(), system);
+ QTEST(locale.measurementSystem(), "system");
}
void tst_QLocale::QTBUG_26035_positivesign()
@@ -2943,6 +3817,7 @@ void tst_QLocale::textDirection_data()
case QLocale::Arabic:
case QLocale::Aramaic:
case QLocale::Avestan:
+ case QLocale::Baluchi:
case QLocale::CentralKurdish:
case QLocale::Divehi:
// case QLocale::Fulah:
@@ -2963,6 +3838,7 @@ void tst_QLocale::textDirection_data()
case QLocale::Sindhi:
case QLocale::SouthernKurdish:
case QLocale::Syriac:
+ case QLocale::Torwali:
case QLocale::Uighur:
case QLocale::Urdu:
case QLocale::WesternBalochi:
@@ -2974,7 +3850,7 @@ void tst_QLocale::textDirection_data()
default:
break;
}
- const QLatin1String testName = QLocalePrivate::languageToCode(QLocale::Language(language));
+ const QString testName = QLocale::languageToCode(QLocale::Language(language));
QTest::newRow(qPrintable(testName)) << language << int(QLocale::AnyScript) << rightToLeft;
}
QTest::newRow("pa_Arab") << int(QLocale::Punjabi) << int(QLocale::ArabicScript) << true;
@@ -2985,10 +3861,9 @@ void tst_QLocale::textDirection()
{
QFETCH(int, language);
QFETCH(int, script);
- QFETCH(bool, rightToLeft);
QLocale locale(QLocale::Language(language), QLocale::Script(script), QLocale::AnyTerritory);
- QCOMPARE(locale.textDirection() == Qt::RightToLeft, rightToLeft);
+ QTEST(locale.textDirection() == Qt::RightToLeft, "rightToLeft");
}
void tst_QLocale::formattedDataSize_data()
@@ -3093,8 +3968,8 @@ void tst_QLocale::formattedDataSize()
QFETCH(int, decimalPlaces);
QFETCH(QLocale::DataSizeFormats, units);
QFETCH(int, bytes);
- QFETCH(QString, output);
- QCOMPARE(QLocale(language).formattedDataSize(bytes, decimalPlaces, units), output);
+
+ QTEST(QLocale(language).formattedDataSize(bytes, decimalPlaces, units), "output");
}
void tst_QLocale::bcp47Name_data()
@@ -3122,20 +3997,51 @@ void tst_QLocale::bcp47Name_data()
void tst_QLocale::bcp47Name()
{
- QFETCH(QString, expect);
- QCOMPARE(QLocale(QLatin1String(QTest::currentDataTag())).bcp47Name(), expect);
+ QFETCH(const QString, expect);
+ const auto expected = [expect](QChar ch) {
+ // Kludge around QString::replace() not being const.
+ QString copy = expect;
+ return copy.replace(u'-', ch);
+ };
+
+ const auto locale = QLocale(QLatin1String(QTest::currentDataTag()));
+ QCOMPARE(locale.bcp47Name(), expect);
+ QCOMPARE(locale.bcp47Name(QLocale::TagSeparator::Underscore), expected(u'_'));
+ QCOMPARE(locale.bcp47Name(QLocale::TagSeparator{'|'}), expected(u'|'));
+ QTest::ignoreMessage(QtWarningMsg, "QLocale::bcp47Name(): "
+ "Using non-ASCII separator '\u00ff' (ff) is unsupported");
+ QCOMPARE(locale.bcp47Name(QLocale::TagSeparator{'\xff'}), QString());
}
+#ifndef QT_NO_SYSTEMLOCALE
+# ifdef QT_BUILD_INTERNAL
class MySystemLocale : public QSystemLocale
{
+ Q_DISABLE_COPY_MOVE(MySystemLocale)
public:
- MySystemLocale(const QString &locale) : m_name(locale), m_locale(locale)
+ MySystemLocale(const QString &locale)
+ : m_name(locale), m_id(QLocaleId::fromName(locale)), m_locale(locale)
{
}
- QVariant query(QueryType type, QVariant /*in*/) const override
+ QVariant query(QueryType type, QVariant &&/*in*/) const override
{
- return type == UILanguages ? QVariant(QStringList{m_name}) : QVariant();
+ switch (type) {
+ case UILanguages:
+ if (m_name == u"en-DE") // QTBUG-104930: simulate macOS's list not including m_name.
+ return QVariant(QStringList{QStringLiteral("en-GB"), QStringLiteral("de-DE")});
+ return QVariant(QStringList{m_name});
+ case LanguageId:
+ return m_id.language_id;
+ case TerritoryId:
+ return m_id.territory_id;
+ case ScriptId:
+ return m_id.script_id;
+
+ default:
+ break;
+ }
+ return QVariant();
}
QLocale fallbackLocale() const override
@@ -3145,10 +4051,11 @@ public:
private:
const QString m_name;
+ const QLocaleId m_id;
const QLocale m_locale;
};
-void tst_QLocale::systemLocale_data()
+void tst_QLocale::mySystemLocale_data()
{
// Test uses MySystemLocale, so is platform-independent.
QTest::addColumn<QString>("name");
@@ -3157,23 +4064,75 @@ void tst_QLocale::systemLocale_data()
QTest::addRow("catalan")
<< QString("ca") << QLocale::Catalan
- << QStringList{QStringLiteral("ca"), QStringLiteral("ca-ES"), QStringLiteral("ca-Latn-ES")};
+ << QStringList{QStringLiteral("ca"), QStringLiteral("ca-Latn-ES"), QStringLiteral("ca-ES")};
+ QTest::addRow("catalan-spain")
+ << QString("ca-ES") << QLocale::Catalan
+ << QStringList{QStringLiteral("ca-ES"), QStringLiteral("ca-Latn-ES"), QStringLiteral("ca")};
+ QTest::addRow("catalan-latin")
+ << QString("ca-Latn") << QLocale::Catalan
+ << QStringList{QStringLiteral("ca-Latn"), QStringLiteral("ca-Latn-ES"),
+ QStringLiteral("ca-ES"), QStringLiteral("ca")};
QTest::addRow("ukrainian")
<< QString("uk") << QLocale::Ukrainian
- << QStringList{QStringLiteral("uk"), QStringLiteral("uk-UA"), QStringLiteral("uk-Cyrl-UA")};
+ << QStringList{QStringLiteral("uk"), QStringLiteral("uk-Cyrl-UA"), QStringLiteral("uk-UA")};
+ QTest::addRow("english-germany")
+ << QString("en-DE") << QLocale::English
+ // First two were missed out before fix to QTBUG-104930:
+ << QStringList{QStringLiteral("en-DE"), QStringLiteral("en-Latn-DE"),
+ QStringLiteral("en-GB"), QStringLiteral("en-Latn-GB"),
+ QStringLiteral("de-DE"), QStringLiteral("de-Latn-DE"), QStringLiteral("de")};
QTest::addRow("german")
<< QString("de") << QLocale::German
- << QStringList{QStringLiteral("de"), QStringLiteral("de-DE"), QStringLiteral("de-Latn-DE")};
+ << QStringList{QStringLiteral("de"), QStringLiteral("de-Latn-DE"), QStringLiteral("de-DE")};
+ QTest::addRow("german-britain")
+ << QString("de-GB") << QLocale::German
+ << QStringList{QStringLiteral("de-GB"), QStringLiteral("de-Latn-GB")};
QTest::addRow("chinese-min")
<< QString("zh") << QLocale::Chinese
- << QStringList{QStringLiteral("zh"), QStringLiteral("zh-CN"), QStringLiteral("zh-Hans-CN")};
+ << QStringList{QStringLiteral("zh"), QStringLiteral("zh-Hans-CN"), QStringLiteral("zh-CN")};
QTest::addRow("chinese-full")
<< QString("zh-Hans-CN") << QLocale::Chinese
- << QStringList{QStringLiteral("zh-Hans-CN"), QStringLiteral("zh"), QStringLiteral("zh-CN")};
+ << QStringList{QStringLiteral("zh-Hans-CN"), QStringLiteral("zh-CN"), QStringLiteral("zh")};
+
+ // For C, it should preserve what the system gave us but only add "C", never anything more:
+ QTest::addRow("C") << QString("C") << QLocale::C << QStringList{QStringLiteral("C")};
+ QTest::addRow("C-Latn")
+ << QString("C-Latn") << QLocale::C
+ << QStringList{QStringLiteral("C-Latn"), QStringLiteral("C")};
+ QTest::addRow("C-US")
+ << QString("C-US") << QLocale::C
+ << QStringList{QStringLiteral("C-US"), QStringLiteral("C")};
+ QTest::addRow("C-Latn-US")
+ << QString("C-Latn-US") << QLocale::C
+ << QStringList{QStringLiteral("C-Latn-US"), QStringLiteral("C")};
+ QTest::addRow("C-Hans")
+ << QString("C-Hans") << QLocale::C
+ << QStringList{QStringLiteral("C-Hans"), QStringLiteral("C")};
+ QTest::addRow("C-CN")
+ << QString("C-CN") << QLocale::C
+ << QStringList{QStringLiteral("C-CN"), QStringLiteral("C")};
+ QTest::addRow("C-Hans-CN")
+ << QString("C-Hans-CN") << QLocale::C
+ << QStringList{QStringLiteral("C-Hans-CN"), QStringLiteral("C")};
+
+ QTest::newRow("und-US")
+ << QString("und-US") << QLocale::C
+ << QStringList{QStringLiteral("und-US"), QStringLiteral("C")};
+
+ QTest::newRow("und-Latn")
+ << QString("und-Latn") << QLocale::C
+ << QStringList{QStringLiteral("und-Latn"), QStringLiteral("C")};
+
+ // TODO: test actual system backends correctly handle locales with
+ // script-specificity (script listed first is the default, in CLDR v40):
+ // az_{Latn,Cyrl}_AZ, bs_{Latn,Cyrl}_BA, sr_{Cyrl,Latn}_{BA,RS,XK,UZ},
+ // sr_{Latn,Cyrl}_ME, ff_{Latn,Adlm}_{BF,CM,GH,GM,GN,GW,LR,MR,NE,NG,SL,SN},
+ // shi_{Tfng,Latn}_MA, vai_{Vaii,Latn}_LR, zh_{Hant,Hans}_{MO,HK}
}
-void tst_QLocale::systemLocale()
+void tst_QLocale::mySystemLocale()
{
+ // Compare uiLanguages(), which tests this for CLDR-derived locales.
QLocale originalLocale;
QLocale originalSystemLocale = QLocale::system();
@@ -3185,14 +4144,18 @@ void tst_QLocale::systemLocale()
MySystemLocale sLocale(name);
QCOMPARE(QLocale().language(), language);
QCOMPARE(QLocale::system().language(), language);
+ auto reporter = qScopeGuard([]() {
+ qDebug("\n\t%s", qPrintable(QLocale::system().uiLanguages().join(u"\n\t")));
+ });
QCOMPARE(QLocale::system().uiLanguages(), uiLanguages);
+ reporter.dismiss();
}
+ // Verify MySystemLocale tidy-up restored prior state:
QCOMPARE(QLocale(), originalLocale);
QCOMPARE(QLocale::system(), originalSystemLocale);
}
-
-#ifndef QT_NO_SYSTEMLOCALE
+# endif // QT_BUILD_INTERNAL
void tst_QLocale::systemLocaleDayAndMonthNames_data()
{
@@ -3221,10 +4184,10 @@ void tst_QLocale::systemLocaleDayAndMonthNames_data()
<< QByteArray("ru_RU") << QDate(2021, 8, 30) << QLocale::ShortFormat
<< QString("\u0430\u0432\u0433.") << QString("\u0430\u0432\u0433.")
<< QString("\u043f\u043d") << QString("\u043f\u043d");
- // А, А, пн, П
+ // А, А, П, П
QTest::newRow("ru_RU 30.08.2021 narrow")
<< QByteArray("ru_RU") << QDate(2021, 8, 30) << QLocale::NarrowFormat
- << QString("\u0410") << QString("\u0410") << QString("\u043f\u043d")
+ << QString("\u0410") << QString("\u0410") << QString("\u041f")
<< QString("\u041f");
#elif defined(Q_OS_DARWIN)
// августа, август, понедельник, понедельник
@@ -3314,10 +4277,6 @@ void tst_QLocale::systemLocaleDayAndMonthNames()
QFETCH(QByteArray, locale);
QFETCH(QDate, date);
QFETCH(QLocale::FormatType, format);
- QFETCH(QString, month);
- QFETCH(QString, standaloneMonth);
- QFETCH(QString, day);
- QFETCH(QString, standaloneDay);
locale += ".UTF-8"; // So we don't have to repeat it on every data row !
const TransientLocale tested(LC_ALL, locale.constData());
@@ -3326,21 +4285,105 @@ void tst_QLocale::systemLocaleDayAndMonthNames()
#if !QT_CONFIG(icu)
// setlocale() does not really change locale on Windows and macOS, we
// need to actually set the locale manually to run the test
- if (!locale.startsWith(sys.name().toLatin1()))
+ if (!locale.startsWith(sys.name().toUtf8()))
QSKIP(("Set locale to " + locale + " manually to run this test.").constData());
#endif
const int m = date.month();
- QCOMPARE(sys.monthName(m, format), month);
- QCOMPARE(sys.standaloneMonthName(m, format), standaloneMonth);
+ QTEST(sys.monthName(m, format), "month");
+ QTEST(sys.standaloneMonthName(m, format), "standaloneMonth");
const int d = date.dayOfWeek();
- QCOMPARE(sys.dayName(d, format), day);
- QCOMPARE(sys.standaloneDayName(d, format), standaloneDay);
+ QTEST(sys.dayName(d, format), "day");
+ QTEST(sys.standaloneDayName(d, format), "standaloneDay");
}
#endif // QT_NO_SYSTEMLOCALE
+void tst_QLocale::numberGrouping_data()
+{
+ QTest::addColumn<QLocale>("locale");
+ QTest::addColumn<int>("number");
+ QTest::addColumn<QString>("string");
+ // Number options set here are expected to be default, but set for the
+ // avoidance of uncertainty or susceptibility to changed defaults.
+
+ QLocale c(QLocale::C); // English-style, without separators.
+ c.setNumberOptions(c.numberOptions() | QLocale::OmitGroupSeparator);
+ QTest::newRow("c:1") << c << 1 << u"1"_s;
+ QTest::newRow("c:12") << c << 12 << u"12"_s;
+ QTest::newRow("c:123") << c << 123 << u"123"_s;
+ QTest::newRow("c:1234") << c << 1234 << u"1234"_s;
+ QTest::newRow("c:12345") << c << 12345 << u"12345"_s;
+ QTest::newRow("c:123456") << c << 123456 << u"123456"_s;
+ QTest::newRow("c:1234567") << c << 1234567 << u"1234567"_s;
+ QTest::newRow("c:12345678") << c << 12345678 << u"12345678"_s;
+ QTest::newRow("c:123456789") << c << 123456789 << u"123456789"_s;
+ QTest::newRow("c:1234567890") << c << 1234567890 << u"1234567890"_s;
+
+ QLocale en(QLocale::English); // English-style, with separators:
+ en.setNumberOptions(en.numberOptions() & ~QLocale::OmitGroupSeparator);
+ QTest::newRow("en:1") << en << 1 << u"1"_s;
+ QTest::newRow("en:12") << en << 12 << u"12"_s;
+ QTest::newRow("en:123") << en << 123 << u"123"_s;
+ QTest::newRow("en:1,234") << en << 1234 << u"1,234"_s;
+ QTest::newRow("en:12,345") << en << 12345 << u"12,345"_s;
+ QTest::newRow("en:123,456") << en << 123456 << u"123,456"_s;
+ QTest::newRow("en:1,234,567") << en << 1234567 << u"1,234,567"_s;
+ QTest::newRow("en:12,345,678") << en << 12345678 << u"12,345,678"_s;
+ QTest::newRow("en:123,456,789") << en << 123456789 << u"123,456,789"_s;
+ QTest::newRow("en:1,234,567,890") << en << 1234567890 << u"1,234,567,890"_s;
+
+ QLocale es(QLocale::Spanish); // Spanish-style, with separators
+ es.setNumberOptions(es.numberOptions() & ~QLocale::OmitGroupSeparator);
+ QTest::newRow("es:1") << es << 1 << u"1"_s;
+ QTest::newRow("es:12") << es << 12 << u"12"_s;
+ QTest::newRow("es:123") << es << 123 << u"123"_s;
+ // First split doesn't happen unless first group has at least two digits:
+ QTest::newRow("es:1234") << es << 1234 << u"1234"_s;
+ QTest::newRow("es:12.345") << es << 12345 << u"12.345"_s;
+ QTest::newRow("es:123.456") << es << 123456 << u"123.456"_s;
+ // Later splits aren't limited to two digits (QTBUG-115740):
+ QTest::newRow("es:1.234.567") << es << 1234567 << u"1.234.567"_s;
+ QTest::newRow("es:12.345.678") << es << 12345678 << u"12.345.678"_s;
+ QTest::newRow("es:123.456.789") << es << 123456789 << u"123.456.789"_s;
+ QTest::newRow("es:1.234.567.890") << es << 1234567890 << u"1.234.567.890"_s;
+
+ QLocale hi(QLocale::Hindi, QLocale::India);
+ hi.setNumberOptions(hi.numberOptions() & ~QLocale::OmitGroupSeparator);
+ QTest::newRow("hi:1") << hi << 1 << u"1"_s;
+ QTest::newRow("hi:12") << hi << 12 << u"12"_s;
+ QTest::newRow("hi:123") << hi << 123 << u"123"_s;
+ QTest::newRow("hi:1,234") << hi << 1234 << u"1,234"_s;
+ QTest::newRow("hi:12,345") << hi << 12345 << u"12,345"_s;
+ QTest::newRow("hi:1,23,456") << hi << 123456 << u"1,23,456"_s;
+ QTest::newRow("hi:12,34,567") << hi << 1234567 << u"12,34,567"_s;
+ QTest::newRow("hi:1,23,45,678") << hi << 12345678 << u"1,23,45,678"_s;
+ QTest::newRow("hi:12,34,56,789") << hi << 123456789 << u"12,34,56,789"_s;
+ QTest::newRow("hi:1,23,45,67,890") << hi << 1234567890 << u"1,23,45,67,890"_s;
+}
+
+void tst_QLocale::numberGrouping()
+{
+ QFETCH(const QLocale, locale);
+ QFETCH(const int, number);
+ QFETCH(const QString, string);
+
+ QCOMPARE(locale.toString(number), string);
+ QLocale sys = QLocale::system();
+ if (sys.language() == locale.language()
+ && sys.script() == locale.script()
+ && sys.territory() == locale.territory()) {
+ sys.setNumberOptions(locale.numberOptions());
+
+ QCOMPARE(sys.toString(number), string);
+ if (QLocale() == sys) { // This normally should be the case.
+ QCOMPARE(u"%L1"_s.arg(number), string);
+ QCOMPARE(u"%L1"_s.arg(double(number), 0, 'f', 0), string);
+ }
+ }
+}
+
void tst_QLocale::numberGroupingIndia()
{
const QLocale indian(QLocale::Hindi, QLocale::India);
@@ -3480,6 +4523,23 @@ void tst_QLocale::lcsToCode()
QCOMPARE(QLocale::languageToCode(QLocale::AnyLanguage), QString());
QCOMPARE(QLocale::languageToCode(QLocale::C), QString("C"));
QCOMPARE(QLocale::languageToCode(QLocale::English), QString("en"));
+ QCOMPARE(QLocale::languageToCode(QLocale::Albanian), u"sq"_s);
+ QCOMPARE(QLocale::languageToCode(QLocale::Albanian, QLocale::ISO639Part1), u"sq"_s);
+ QCOMPARE(QLocale::languageToCode(QLocale::Albanian, QLocale::ISO639Part2B), u"alb"_s);
+ QCOMPARE(QLocale::languageToCode(QLocale::Albanian, QLocale::ISO639Part2T), u"sqi"_s);
+ QCOMPARE(QLocale::languageToCode(QLocale::Albanian, QLocale::ISO639Part3), u"sqi"_s);
+
+ QCOMPARE(QLocale::languageToCode(QLocale::Taita), u"dav"_s);
+ QCOMPARE(QLocale::languageToCode(QLocale::Taita,
+ QLocale::ISO639Part1 | QLocale::ISO639Part2B
+ | QLocale::ISO639Part2T),
+ QString());
+ QCOMPARE(QLocale::languageToCode(QLocale::Taita, QLocale::ISO639Part3), u"dav"_s);
+ QCOMPARE(QLocale::languageToCode(QLocale::English, QLocale::LanguageCodeTypes {}), QString());
+
+ // Legacy codes can only be used to convert them to Language values, not other way around.
+ QCOMPARE(QLocale::languageToCode(QLocale::NorwegianBokmal, QLocale::LegacyLanguageCode),
+ QString());
QCOMPARE(QLocale::territoryToCode(QLocale::AnyTerritory), QString());
QCOMPARE(QLocale::territoryToCode(QLocale::UnitedStates), QString("US"));
@@ -3497,9 +4557,28 @@ void tst_QLocale::codeToLcs()
QCOMPARE(QLocale::codeToLanguage(QString("e")), QLocale::AnyLanguage);
QCOMPARE(QLocale::codeToLanguage(QString("en")), QLocale::English);
QCOMPARE(QLocale::codeToLanguage(QString("EN")), QLocale::English);
- QCOMPARE(QLocale::codeToLanguage(QString("eng")), QLocale::AnyLanguage);
+ QCOMPARE(QLocale::codeToLanguage(QString("eng")), QLocale::English);
QCOMPARE(QLocale::codeToLanguage(QString("ha")), QLocale::Hausa);
+ QCOMPARE(QLocale::codeToLanguage(QString("ha"), QLocale::ISO639Alpha3), QLocale::AnyLanguage);
QCOMPARE(QLocale::codeToLanguage(QString("haw")), QLocale::Hawaiian);
+ QCOMPARE(QLocale::codeToLanguage(QString("haw"), QLocale::ISO639Alpha2), QLocale::AnyLanguage);
+
+ QCOMPARE(QLocale::codeToLanguage(u"sq"), QLocale::Albanian);
+ QCOMPARE(QLocale::codeToLanguage(u"alb"), QLocale::Albanian);
+ QCOMPARE(QLocale::codeToLanguage(u"sqi"), QLocale::Albanian);
+ QCOMPARE(QLocale::codeToLanguage(u"sq", QLocale::ISO639Part1), QLocale::Albanian);
+ QCOMPARE(QLocale::codeToLanguage(u"sq", QLocale::ISO639Part3), QLocale::AnyLanguage);
+ QCOMPARE(QLocale::codeToLanguage(u"alb", QLocale::ISO639Part2B), QLocale::Albanian);
+ QCOMPARE(QLocale::codeToLanguage(u"alb", QLocale::ISO639Part2T | QLocale::ISO639Part3),
+ QLocale::AnyLanguage);
+ QCOMPARE(QLocale::codeToLanguage(u"sqi", QLocale::ISO639Part2T), QLocale::Albanian);
+ QCOMPARE(QLocale::codeToLanguage(u"sqi", QLocale::ISO639Part3), QLocale::Albanian);
+ QCOMPARE(QLocale::codeToLanguage(u"sqi", QLocale::ISO639Part1 | QLocale::ISO639Part2B),
+ QLocale::AnyLanguage);
+
+ // Legacy code
+ QCOMPARE(QLocale::codeToLanguage(u"no"), QLocale::NorwegianBokmal);
+ QCOMPARE(QLocale::codeToLanguage(u"no", QLocale::ISO639Part1), QLocale::AnyLanguage);
QCOMPARE(QLocale::codeToTerritory(QString()), QLocale::AnyTerritory);
QCOMPARE(QLocale::codeToTerritory(QString("ZZ")), QLocale::AnyTerritory);