diff options
Diffstat (limited to 'tests/auto/corelib/text/qlocale')
8 files changed, 2440 insertions, 849 deletions
diff --git a/tests/auto/corelib/text/qlocale/CMakeLists.txt b/tests/auto/corelib/text/qlocale/CMakeLists.txt new file mode 100644 index 0000000000..a91dd44ea5 --- /dev/null +++ b/tests/auto/corelib/text/qlocale/CMakeLists.txt @@ -0,0 +1,13 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qlocale LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +add_subdirectory(test) +if(QT_FEATURE_jalalicalendar) + add_subdirectory(syslocaleapp) +endif() diff --git a/tests/auto/corelib/text/qlocale/qlocale.pro b/tests/auto/corelib/text/qlocale/qlocale.pro deleted file mode 100644 index 5161200260..0000000000 --- a/tests/auto/corelib/text/qlocale/qlocale.pro +++ /dev/null @@ -1,4 +0,0 @@ -TEMPLATE = subdirs - -SUBDIRS += test -!winrt: SUBDIRS+=syslocaleapp diff --git a/tests/auto/corelib/text/qlocale/syslocaleapp/CMakeLists.txt b/tests/auto/corelib/text/qlocale/syslocaleapp/CMakeLists.txt new file mode 100644 index 0000000000..0a5bce6183 --- /dev/null +++ b/tests/auto/corelib/text/qlocale/syslocaleapp/CMakeLists.txt @@ -0,0 +1,12 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## syslocaleapp Binary: +##################################################################### + +qt_internal_add_executable(syslocaleapp + OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/" + SOURCES + syslocaleapp.cpp +) diff --git a/tests/auto/corelib/text/qlocale/syslocaleapp/syslocaleapp.cpp b/tests/auto/corelib/text/qlocale/syslocaleapp/syslocaleapp.cpp index 1e1bcc8c5f..44c6ce3aa6 100644 --- a/tests/auto/corelib/text/qlocale/syslocaleapp/syslocaleapp.cpp +++ b/tests/auto/corelib/text/qlocale/syslocaleapp/syslocaleapp.cpp @@ -1,40 +1,21 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QLocale> +#include <QCalendar> #include <QCoreApplication> #include <QTextStream> int main(int argc, char** argv) { QCoreApplication app(argc, argv); - QLocale l; + // Setting a default locale should not mess up the system one. + QLocale::setDefault(QLocale::Persian); + QLocale l = QLocale::system(); + // A non-Roman calendar will use CLDR data instead of system data, so needs + // to have got the right locale index to look that up. + QCalendar cal = QCalendar(QCalendar::System::Jalali); QTextStream str(stdout); - str << l.name(); + str << l.name() << ' ' << cal.standaloneMonthName(l, 2); return 0; } diff --git a/tests/auto/corelib/text/qlocale/syslocaleapp/syslocaleapp.pro b/tests/auto/corelib/text/qlocale/syslocaleapp/syslocaleapp.pro deleted file mode 100644 index 3e283c05a4..0000000000 --- a/tests/auto/corelib/text/qlocale/syslocaleapp/syslocaleapp.pro +++ /dev/null @@ -1,7 +0,0 @@ -SOURCES += syslocaleapp.cpp -DESTDIR = ./ - -CONFIG += cmdline - -QT = core - diff --git a/tests/auto/corelib/text/qlocale/test/CMakeLists.txt b/tests/auto/corelib/text/qlocale/test/CMakeLists.txt new file mode 100644 index 0000000000..fc3e1488cd --- /dev/null +++ b/tests/auto/corelib/text/qlocale/test/CMakeLists.txt @@ -0,0 +1,33 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qlocale Test: +##################################################################### + +qt_internal_add_test(tst_qlocale + OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../" + SOURCES + ../tst_qlocale.cpp + LIBRARIES + Qt::CorePrivate + Qt::TestPrivate +) + +## Scopes: +##################################################################### + +qt_internal_extend_target(tst_qlocale CONDITION embedded + LIBRARIES + Qt::Gui +) + +qt_internal_extend_target(tst_qlocale CONDITION NOT QT_FEATURE_doubleconversion AND NOT QT_FEATURE_system_doubleconversion + DEFINES + QT_NO_DOUBLECONVERSION +) + +## Depends on ../syslocaleapp +if(QT_FEATURE_process) + add_dependencies(tst_qlocale syslocaleapp) +endif() diff --git a/tests/auto/corelib/text/qlocale/test/test.pro b/tests/auto/corelib/text/qlocale/test/test.pro deleted file mode 100644 index f7243e99a7..0000000000 --- a/tests/auto/corelib/text/qlocale/test/test.pro +++ /dev/null @@ -1,19 +0,0 @@ -CONFIG += console testcase -QT = core testlib core-private -embedded: QT += gui -SOURCES = ../tst_qlocale.cpp - -!qtConfig(doubleconversion):!qtConfig(system-doubleconversion) { - DEFINES += QT_NO_DOUBLECONVERSION -} - -TARGET = ../tst_qlocale -win32 { - CONFIG(debug, debug|release) { - TARGET = ../../debug/tst_qlocale - } else { - TARGET = ../../release/tst_qlocale - } -} - -!android:!winrt: TEST_HELPER_INSTALLS = ../syslocaleapp/syslocaleapp diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 7b2d92f9cb..eb2d73b9b2 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -1,63 +1,35 @@ -/**************************************************************************** -** -** Copyright (C) 2019 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$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> -#include <math.h> -#include <qdebug.h> -#include <qdir.h> -#include <qfileinfo.h> -#include <QScopedArrayPointer> -#include <qtextcodec.h> -#include <qdatetime.h> +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QTest> +#include <QtTest/private/qcomparisontesthelper_p.h> +#include <QLocale> + +#include <QDateTime> +#include <QDebug> +#include <QDir> +#include <QFileInfo> +#include <qnumeric.h> #if QT_CONFIG(process) -# include <qprocess.h> +# include <QProcess> #endif -#include <float.h> -#include <locale.h> +#include <QScopedArrayPointer> +#include <QTimeZone> -#include <qlocale.h> #include <private/qlocale_p.h> #include <private/qlocale_tools_p.h> -#include <qnumeric.h> - -#if defined(Q_OS_LINUX) && !defined(__UCLIBC__) -# define QT_USE_FENV -#endif +#include "../../../../shared/localechange.h" -#ifdef QT_USE_FENV -# include <fenv.h> -#endif +#include <float.h> +#include <math.h> +#include <fenv.h> -#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 @@ -69,19 +41,22 @@ public: private slots: void initTestCase(); - void cleanupTestCase(); -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) + void compareCompiles(); +#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(); @@ -93,9 +68,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(); @@ -107,6 +84,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(); @@ -121,6 +107,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(); @@ -128,6 +123,7 @@ private slots: void ampm(); void currency(); void quoteString(); + void uiLanguages_data(); void uiLanguages(); void weekendDays(); void listPatterns(); @@ -144,10 +140,23 @@ 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(); - void IndianNumberGrouping(); + void lcsToCode(); + void codeToLcs(); // *** ORDER-DEPENDENCY *** (This Is Bad.) // Test order is determined by order of declaration here: *all* tests that @@ -163,45 +172,45 @@ 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(); - class TransientLocale - { - const int m_category; - const char *const m_prior; - public: - TransientLocale(int category, const char *locale) - : m_category(category), m_prior(setlocale(category, locale)) {} - ~TransientLocale() { setlocale(m_category, m_prior); } - }; + 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::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile<QLocale>(); } 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 +218,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,293 +227,349 @@ void tst_QLocale::initTestCase() #endif // QT_CONFIG(process) } -void tst_QLocale::cleanupTestCase() -{} +void tst_QLocale::ctor_data() +{ + 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 +} void tst_QLocale::ctor() { - QLocale default_locale = QLocale::system(); - QLocale::Language default_lang = default_locale.language(); - QLocale::Country default_country = default_locale.country(); - - qDebug("Default: %s/%s", QLocale::languageToString(default_lang).toLatin1().constData(), - QLocale::countryToString(default_country).toLatin1().constData()); + QFETCH(const QLocale::Language, reqLang); + QFETCH(const QLocale::Script, reqText); + QFETCH(const QLocale::Territory, reqLand); { - QLocale l; - QVERIFY(l.language() == default_lang); - QVERIFY(l.country() == default_country); + const QLocale l(reqLang, reqText, reqLand); + 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) \ - { \ - QLocale l(QLocale::req_lang, QLocale::req_script, QLocale::req_country); \ - QCOMPARE((int)l.language(), (int)exp_lang); \ - QCOMPARE((int)l.script(), (int)exp_script); \ - QCOMPARE((int)l.country(), (int)exp_country); \ + 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"); } +} - // Exact matches - TEST_CTOR(Chinese, SimplifiedHanScript, China, - QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China); - TEST_CTOR(Chinese, TraditionalHanScript, Taiwan, - QLocale::Chinese, QLocale::TraditionalHanScript, QLocale::Taiwan); - TEST_CTOR(Chinese, TraditionalHanScript, HongKong, - QLocale::Chinese, QLocale::TraditionalHanScript, QLocale::HongKong); - - // Best match for AnyCountry - TEST_CTOR(Chinese, SimplifiedHanScript, AnyCountry, - QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China); - TEST_CTOR(Chinese, TraditionalHanScript, AnyCountry, - QLocale::Chinese, QLocale::TraditionalHanScript, QLocale::Taiwan); - - // Best match for AnyScript (and change country to supported one, if necessary) - TEST_CTOR(Chinese, AnyScript, China, - QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China); - TEST_CTOR(Chinese, AnyScript, Taiwan, - QLocale::Chinese, QLocale::TraditionalHanScript, QLocale::Taiwan); - TEST_CTOR(Chinese, AnyScript, HongKong, - QLocale::Chinese, QLocale::TraditionalHanScript, QLocale::HongKong); - TEST_CTOR(Chinese, AnyScript, UnitedStates, - QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China); - - // Fully-specified not found; find best alternate country - TEST_CTOR(Chinese, SimplifiedHanScript, Taiwan, - QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China); - TEST_CTOR(Chinese, SimplifiedHanScript, UnitedStates, - QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China); - TEST_CTOR(Chinese, TraditionalHanScript, China, - QLocale::Chinese, QLocale::TraditionalHanScript, QLocale::Taiwan); - TEST_CTOR(Chinese, TraditionalHanScript, UnitedStates, - QLocale::Chinese, QLocale::TraditionalHanScript, QLocale::Taiwan); - - // Fully-specified not found; find best alternate script - TEST_CTOR(Chinese, LatinScript, China, - QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China); - TEST_CTOR(Chinese, LatinScript, Taiwan, - QLocale::Chinese, QLocale::TraditionalHanScript, QLocale::Taiwan); - - // Fully-specified not found; find best alternate country and script - TEST_CTOR(Chinese, LatinScript, UnitedStates, - QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::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() { QLocale default_locale = QLocale::system(); QLocale::Language default_lang = default_locale.language(); - QLocale::Country default_country = default_locale.country(); + QLocale::Territory default_country = default_locale.territory(); - qDebug("Default: %s/%s", QLocale::languageToString(default_lang).toLatin1().constData(), - QLocale::countryToString(default_country).toLatin1().constData()); + qDebug("Default: %s/%s", QLocale::languageToString(default_lang).toUtf8().constData(), + QLocale::territoryToString(default_country).toUtf8().constData()); { - QLocale l(QLocale::C, QLocale::AnyCountry); - QCOMPARE(l.language(), QLocale::C); - QCOMPARE(l.country(), QLocale::AnyCountry); + QLocale l; + QCOMPARE(l.language(), default_lang); + QCOMPARE(l.territory(), default_country); } -#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.country(), (int)exp_country); \ + { + QLocale l(QLocale::C, QLocale::AnyTerritory); + QCOMPARE(l.language(), QLocale::C); + QCOMPARE(l.territory(), QLocale::AnyTerritory); } - TEST_CTOR(AnyLanguage, AnyCountry, default_lang, default_country) - TEST_CTOR(C, AnyCountry, QLocale::C, QLocale::AnyCountry) - TEST_CTOR(Aymara, AnyCountry, default_lang, default_country) - TEST_CTOR(Aymara, France, default_lang, default_country) +#define CHECK_DEFAULT(lang, terr) \ + do { \ + const QLocale l; \ + QCOMPARE(l.language(), lang); \ + QCOMPARE(l.territory(), terr); \ + } while (false) - TEST_CTOR(English, AnyCountry, 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::AnyCountry) +#define TEST_CTOR(req_lang, req_country, exp_lang, 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(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(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.country() == 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, AnyCountry, QLocale::C, QLocale::AnyCountry) - TEST_CTOR(C, France, QLocale::C, QLocale::AnyCountry) - TEST_CTOR(Aymara, AnyCountry, 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.country() == 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, AnyCountry, QLocale::C, QLocale::AnyCountry) - TEST_CTOR(C, France, QLocale::C, QLocale::AnyCountry) + TEST_CTOR(C, AnyTerritory, QLocale::C, QLocale::AnyTerritory); + TEST_CTOR(C, France, QLocale::C, QLocale::AnyTerritory); QLocale::setDefault(QLocale(QLocale::Aymara, QLocale::France)); - - { - QLocale l; - QVERIFY(l.language() == QLocale::English); - QVERIFY(l.country() == QLocale::UnitedKingdom); - } - - TEST_CTOR(Aymara, AnyCountry, QLocale::English, QLocale::UnitedKingdom) - TEST_CTOR(Aymara, France, QLocale::English, QLocale::UnitedKingdom) - - TEST_CTOR(English, AnyCountry, 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, AnyCountry, QLocale::C, QLocale::AnyCountry) - TEST_CTOR(C, France, QLocale::C, QLocale::AnyCountry) - - QLocale::setDefault(QLocale(QLocale::Aymara, QLocale::AnyCountry)); - - { - QLocale l; - QVERIFY(l.language() == QLocale::English); - QVERIFY(l.country() == QLocale::UnitedKingdom); - } - - TEST_CTOR(Aymara, AnyCountry, QLocale::English, QLocale::UnitedKingdom) - TEST_CTOR(Aymara, France, QLocale::English, QLocale::UnitedKingdom) - - TEST_CTOR(English, AnyCountry, 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, AnyCountry, QLocale::C, QLocale::AnyCountry) - TEST_CTOR(C, France, QLocale::C, QLocale::AnyCountry) - - TEST_CTOR(Arabic, AnyCountry, QLocale::Arabic, QLocale::Egypt) - TEST_CTOR(Dutch, AnyCountry, QLocale::Dutch, QLocale::Netherlands) - TEST_CTOR(German, AnyCountry, QLocale::German, QLocale::Germany) - TEST_CTOR(Greek, AnyCountry, QLocale::Greek, QLocale::Greece) - TEST_CTOR(Malay, AnyCountry, QLocale::Malay, QLocale::Malaysia) - TEST_CTOR(Persian, AnyCountry, QLocale::Persian, QLocale::Iran) - TEST_CTOR(Portuguese, AnyCountry, QLocale::Portuguese, QLocale::Brazil) - TEST_CTOR(Serbian, AnyCountry, QLocale::Serbian, QLocale::Serbia) - TEST_CTOR(Somali, AnyCountry, QLocale::Somali, QLocale::Somalia) - TEST_CTOR(Spanish, AnyCountry, QLocale::Spanish, QLocale::Spain) - TEST_CTOR(Swedish, AnyCountry, QLocale::Swedish, QLocale::Sweden) - TEST_CTOR(Uzbek, AnyCountry, 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); + + QLocale::setDefault(QLocale(QLocale::Aymara, QLocale::AnyTerritory)); + 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.country() == QLocale::exp_country, \ - QString("requested: \"" + QString(req_lc) + "\", got: " \ - + QLocale::languageToString(l.language()) \ - + QLatin1Char('/') \ - + QLocale::countryToString(l.country())).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, AnyCountry) - TEST_CTOR("bla", C, AnyCountry) - TEST_CTOR("zz", C, AnyCountry) - TEST_CTOR("zz_zz", C, AnyCountry) - TEST_CTOR("zz...", C, AnyCountry) - TEST_CTOR("", C, AnyCountry) - TEST_CTOR("en/", C, AnyCountry) - TEST_CTOR(empty, C, AnyCountry) - 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) - - QVERIFY(QLocale::Norwegian == QLocale::NorwegianBokmal); - TEST_CTOR("no", Norwegian, Norway) - TEST_CTOR("nb", Norwegian, Norway) - TEST_CTOR("nn", NorwegianNynorsk, Norway) - TEST_CTOR("no_NO", Norwegian, Norway) - TEST_CTOR("nb_NO", Norwegian, 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.country() == QLocale::exp_country, \ - QString("requested: \"" + QString(req_lc) + "\", got: " \ - + QLocale::languageToString(l.language()) \ - + QLatin1Char('/') + QLocale::scriptToString(l.script()) \ - + QLatin1Char('/') + QLocale::countryToString(l.country())).toLatin1().constData()); \ - } - - 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) + 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("ru", Russian, CyrillicScript, RussianFederation); + TEST_CTOR("ru_Cyrl", Russian, CyrillicScript, RussianFederation); #undef TEST_CTOR +#undef CHECK_DEFAULT } #if QT_CONFIG(process) static inline bool runSysApp(const QString &binary, + const QStringList &args, const QStringList &env, QString *output, QString *errorMessage) @@ -512,7 +578,7 @@ static inline bool runSysApp(const QString &binary, errorMessage->clear(); QProcess process; process.setEnvironment(env); - process.start(binary); + process.start(binary, args); process.closeWriteChannel(); if (!process.waitForStarted()) { *errorMessage = QLatin1String("Cannot start '") + binary @@ -535,8 +601,12 @@ static inline bool runSysAppTest(const QString &binary, QString *errorMessage) { QString output; + QStringList args; +#ifdef Q_OS_MACOS + args << "-AppleLocale" << requestedLocale; +#endif baseEnv.append(QStringLiteral("LANG=") + requestedLocale); - if (!runSysApp(binary, baseEnv, &output, errorMessage)) + if (!runSysApp(binary, args, baseEnv, &output, errorMessage)) return false; if (output.isEmpty()) { @@ -554,13 +624,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"); @@ -572,53 +642,62 @@ 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 // Get default locale. QString defaultLoc; QString errorMessage; - if (runSysApp(m_sysapp, cleanEnv, &defaultLoc, &errorMessage)) { -#define ADD_CTOR_TEST(give) QTest::newRow(give) << defaultLoc; + if (runSysApp(m_sysapp, QStringList(), cleanEnv, &defaultLoc, &errorMessage)) { +#if defined(Q_OS_MACOS) + QString localeForInvalidLocale = "C Ordibehesht"; +#else + QString localeForInvalidLocale = defaultLoc; +#endif +#define ADD_CTOR_TEST(give) QTest::newRow(give) << localeForInvalidLocale; ADD_CTOR_TEST("en/"); ADD_CTOR_TEST("asdfghj"); ADD_CTOR_TEST("123456"); @@ -629,7 +708,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()); @@ -648,58 +727,40 @@ void tst_QLocale::emptyCtor() void tst_QLocale::legacyNames() { - QVERIFY(QLocale::Norwegian == QLocale::NorwegianBokmal); QLocale::setDefault(QLocale(QLocale::C)); -#define TEST_CTOR(req_lang, req_country, exp_lang, exp_country) \ - { \ - QLocale l(QLocale::req_lang, QLocale::req_country); \ - QCOMPARE((int)l.language(), (int)QLocale::exp_lang); \ - QCOMPARE((int)l.country(), (int)QLocale::exp_country); \ - } - - TEST_CTOR(Moldavian, Moldova, Romanian, Moldova) - TEST_CTOR(Norwegian, AnyCountry, Norwegian, Norway) - TEST_CTOR(SerboCroatian, Montenegro, Serbian, Montenegro) - TEST_CTOR(Tagalog, AnyCountry, Filipino, Philippines) - -#undef TEST_CTOR - #define TEST_CTOR(req_lc, exp_lang, exp_country) \ - { \ - QLocale l(req_lc); \ - QVERIFY2(l.language() == QLocale::exp_lang \ - && l.country() == QLocale::exp_country, \ - QString("requested: \"" + QString(req_lc) + "\", got: " \ - + QLocale::languageToString(l.language()) \ - + QLatin1Char('/') \ - + QLocale::countryToString(l.country())).toLatin1().constData()); \ - } - - TEST_CTOR("mo_MD", Romanian, Moldova) - TEST_CTOR("no", Norwegian, Norway) - TEST_CTOR("sh_ME", Serbian, Montenegro) - TEST_CTOR("tl", Filipino, Philippines) - TEST_CTOR("iw", Hebrew, Israel) - TEST_CTOR("in", Indonesian, Indonesia) + 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); #undef TEST_CTOR } void tst_QLocale::consistentC() { const QLocale c(QLocale::C); - QCOMPARE(c, QLocale::c()); - QCOMPARE(c, QLocale(QLocale::C, QLocale::AnyScript, QLocale::AnyCountry)); + QT_TEST_EQUALITY_OPS(c, QLocale::c(), true); + QT_TEST_EQUALITY_OPS(c, QLocale(QLocale::C, QLocale::AnyScript, QLocale::AnyTerritory), true); QVERIFY(QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, - QLocale::AnyCountry).contains(c)); + QLocale::AnyTerritory).contains(c)); } void tst_QLocale::matchingLocales() { const QLocale c(QLocale::C); const QLocale ru_RU(QLocale::Russian, QLocale::Russia); + QT_TEST_EQUALITY_OPS(c, ru_RU, false); - QList<QLocale> locales = QLocale::matchingLocales(QLocale::C, QLocale::AnyScript, QLocale::AnyCountry); + QList<QLocale> locales = QLocale::matchingLocales(QLocale::C, QLocale::AnyScript, QLocale::AnyTerritory); QCOMPARE(locales.size(), 1); QVERIFY(locales.contains(c)); @@ -707,12 +768,12 @@ void tst_QLocale::matchingLocales() QCOMPARE(locales.size(), 1); QVERIFY(locales.contains(ru_RU)); - locales = QLocale::matchingLocales(QLocale::Russian, QLocale::AnyScript, QLocale::AnyCountry); + locales = QLocale::matchingLocales(QLocale::Russian, QLocale::AnyScript, QLocale::AnyTerritory); QVERIFY(!locales.isEmpty()); QVERIFY(!locales.contains(c)); QVERIFY(locales.contains(ru_RU)); - locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::CyrillicScript, QLocale::AnyCountry); + locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::CyrillicScript, QLocale::AnyTerritory); QVERIFY(!locales.isEmpty()); QVERIFY(!locales.contains(c)); QVERIFY(locales.contains(ru_RU)); @@ -721,19 +782,25 @@ void tst_QLocale::matchingLocales() QVERIFY(!locales.isEmpty()); QVERIFY(!locales.contains(c)); QVERIFY(locales.contains(ru_RU)); + + // Regression check for assertion failure when no locales match: + locales = QLocale::matchingLocales(QLocale::Abkhazian, QLocale::AnyScript, QLocale::AnyTerritory); + // Empty in CLDR v39, but don't require that. + QVERIFY(!locales.contains(c)); + QVERIFY(!locales.contains(ru_RU)); } void tst_QLocale::unixLocaleName_data() { QTest::addColumn<QLocale::Language>("lang"); - QTest::addColumn<QLocale::Country>("land"); + QTest::addColumn<QLocale::Territory>("land"); QTest::addColumn<QString>("expect"); #define ADDROW(nom, lang, land, name) \ QTest::newRow(nom) << QLocale::lang << QLocale::land << QStringLiteral(name) - ADDROW("C_any", C, AnyCountry, "C"); - ADDROW("en_any", English, AnyCountry, "en_US"); + ADDROW("C_any", C, AnyTerritory, "C"); + ADDROW("en_any", English, AnyTerritory, "en_US"); ADDROW("en_GB", English, UnitedKingdom, "en_GB"); ADDROW("ay_GB", Aymara, UnitedKingdom, "C"); #undef ADDROW @@ -741,14 +808,24 @@ void tst_QLocale::unixLocaleName_data() void tst_QLocale::unixLocaleName() { - QFETCH(QLocale::Language, lang); - QFETCH(QLocale::Country, 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() @@ -873,6 +950,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() @@ -899,6 +1014,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() @@ -909,7 +1031,7 @@ void tst_QLocale::stringToDouble() QFETCH(QString, num_str); QFETCH(bool, good); QFETCH(double, num); - QStringRef num_strRef = num_str.leftRef(-1); + QStringView num_strRef{ num_str }; QLocale locale(locale_name); QCOMPARE(locale.name(), locale_name); @@ -920,7 +1042,7 @@ void tst_QLocale::stringToDouble() { // Make sure result is independent of locale: - TransientLocale ignoreme(LC_ALL, "ar_SA"); + TransientLocale ignoreme(LC_ALL, "ar_SA.UTF-8"); QCOMPARE(locale.toDouble(num_str, &ok), d); QCOMPARE(ok, good); } @@ -930,7 +1052,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); } } @@ -941,7 +1063,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 @@ -985,6 +1107,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() @@ -995,19 +1124,33 @@ void tst_QLocale::stringToFloat() QFETCH(QString, num_str); QFETCH(bool, good); QFETCH(double, num); - QStringRef num_strRef = num_str.leftRef(-1); + QStringView num_strRef{ num_str }; float fnum = num; 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"); + TransientLocale ignoreme(LC_ALL, "ar_SA.UTF-8"); QCOMPARE(locale.toFloat(num_str, &ok), f); QCOMPARE(ok, good); } @@ -1017,7 +1160,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); } } @@ -1028,7 +1171,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 @@ -1036,14 +1179,60 @@ void tst_QLocale::stringToFloat() void tst_QLocale::doubleToString_data() { - QTest::addColumn<QString>("locale_name"); - QTest::addColumn<QString>("num_str"); + QTest::addColumn<QString>("localeName"); + QTest::addColumn<QString>("numStr"); QTest::addColumn<double>("num"); QTest::addColumn<char>("mode"); QTest::addColumn<int>("precision"); 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; @@ -1060,8 +1249,8 @@ void tst_QLocale::doubleToString_data() QTest::newRow("de_DE 3,4 f 1") << QString("de_DE") << QString("3,4") << 3.4 << 'f' << 1; QTest::newRow("de_DE 3,4 f -") << QString("de_DE") << QString("3,4") << 3.4 << 'f' << shortest; - QTest::newRow("de_DE 3,4 e 1") << QString("de_DE") << QString("3,4e+00") << 3.4 << 'e' << 1; - QTest::newRow("de_DE 3,4 e -") << QString("de_DE") << QString("3,4e+00") << 3.4 << 'e' << shortest; + QTest::newRow("de_DE 3,4 e 1") << QString("de_DE") << QString("3,4E+00") << 3.4 << 'e' << 1; + QTest::newRow("de_DE 3,4 e -") << QString("de_DE") << QString("3,4E+00") << 3.4 << 'e' << shortest; QTest::newRow("de_DE 3,4 g 2") << QString("de_DE") << QString("3,4") << 3.4 << 'g' << 2; QTest::newRow("de_DE 3,4 g -") << QString("de_DE") << QString("3,4") << 3.4 << 'g' << shortest; @@ -1081,10 +1270,29 @@ void tst_QLocale::doubleToString_data() QTest::newRow("de_DE 0,035003945 f 9") << QString("de_DE") << QString("0,035003945") << 0.035003945 << 'f' << 9; QTest::newRow("de_DE 0,035003945 f -") << QString("de_DE") << QString("0,035003945") << 0.035003945 << 'f' << shortest; - QTest::newRow("de_DE 0,035003945 e 7") << QString("de_DE") << QString("3,5003945e-02") << 0.035003945 << 'e' << 7; - QTest::newRow("de_DE 0,035003945 e -") << QString("de_DE") << QString("3,5003945e-02") << 0.035003945 << 'e' << shortest; + QTest::newRow("de_DE 0,035003945 e 7") << QString("de_DE") << QString("3,5003945E-02") << 0.035003945 << 'e' << 7; + QTest::newRow("de_DE 0,035003945 e -") << QString("de_DE") << QString("3,5003945E-02") << 0.035003945 << 'e' << shortest; QTest::newRow("de_DE 0,035003945 g 8") << QString("de_DE") << QString("0,035003945") << 0.035003945 << 'g' << 8; QTest::newRow("de_DE 0,035003945 g -") << QString("de_DE") << QString("0,035003945") << 0.035003945 << 'g' << shortest; + // Check 'f/F' iff (adjusted) precision > exponent >= -4: + QTest::newRow("de_DE 12345 g 4") << QString("de_DE") << QString("1,235E+04") << 12345. << 'g' << 4; + QTest::newRow("de_DE 1e7 g 8") << QString("de_DE") << QString("10.000.000") << 1e7 << 'g' << 8; + QTest::newRow("de_DE 1e8 g 8") << QString("de_DE") << QString("1E+08") << 1e8 << 'g' << 8; + QTest::newRow("de_DE 10.0 g 1") << QString("de_DE") << QString("1E+01") << 10.0 << 'g' << 1; + QTest::newRow("de_DE 10.0 g 0") << QString("de_DE") << QString("1E+01") << 10.0 << 'g' << 0; + QTest::newRow("de_DE 1.0 g 0") << QString("de_DE") << QString("1") << 1.0 << 'g' << 0; + QTest::newRow("de_DE 0.0001 g 0") << QString("de_DE") << QString("0,0001") << 0.0001 << 'g' << 0; + QTest::newRow("de_DE 0.00001 g 0") << QString("de_DE") << QString("1E-05") << 0.00001 << 'g' << 0; + // Check transition to exponent form: + 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 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; QTest::newRow("C 0.000003945 f 12") << QString("C") << QString("0.000003945000") << 0.000003945 << 'f' << 12; QTest::newRow("C 0.000003945 f 6") << QString("C") << QString("0.000004") << 0.000003945 << 'f' << 6; @@ -1092,6 +1300,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; @@ -1102,16 +1323,33 @@ void tst_QLocale::doubleToString_data() QTest::newRow("de_DE 0,000003945 f 9") << QString("de_DE") << QString("0,000003945") << 0.000003945 << 'f' << 9; QTest::newRow("de_DE 0,000003945 f -") << QString("de_DE") << QString("0,000003945") << 0.000003945 << 'f' << shortest; - QTest::newRow("de_DE 0,000003945 e 3") << QString("de_DE") << QString("3,945e-06") << 0.000003945 << 'e' << 3; - QTest::newRow("de_DE 0,000003945 e -") << QString("de_DE") << QString("3,945e-06") << 0.000003945 << 'e' << shortest; - QTest::newRow("de_DE 0,000003945 g 4") << QString("de_DE") << QString("3,945e-06") << 0.000003945 << 'g' << 4; - QTest::newRow("de_DE 0,000003945 g -") << QString("de_DE") << QString("3,945e-06") << 0.000003945 << 'g' << shortest; + QTest::newRow("de_DE 0,000003945 e 3") << QString("de_DE") << QString("3,945E-06") << 0.000003945 << 'e' << 3; + QTest::newRow("de_DE 0,000003945 e -") << QString("de_DE") << QString("3,945E-06") << 0.000003945 << 'e' << shortest; + QTest::newRow("de_DE 0,000003945 g 4") << QString("de_DE") << QString("3,945E-06") << 0.000003945 << 'g' << 4; + QTest::newRow("de_DE 0,000003945 g -") << QString("de_DE") << QString("3,945E-06") << 0.000003945 << 'g' << shortest; QTest::newRow("C 12456789012 f 3") << QString("C") << QString("12456789012.000") << 12456789012.0 << 'f' << 3; QTest::newRow("C 12456789012 e 13") << QString("C") << QString("1.2456789012000e+10") << 12456789012.0 << 'e' << 13; QTest::newRow("C 12456789012 e 7") << QString("C") << QString("1.2456789e+10") << 12456789012.0 << 'e' << 7; QTest::newRow("C 12456789012 g 14") << QString("C") << QString("12456789012") << 12456789012.0 << 'g' << 14; QTest::newRow("C 12456789012 g 8") << QString("C") << QString("1.2456789e+10") << 12456789012.0 << 'g' << 8; + // Check 'f/F' iff (adjusted) precision > exponent >= -4: + QTest::newRow("C 12345 g 4") << QString("C") << QString("1.235e+04") << 12345. << 'g' << 4; + QTest::newRow("C 1e7 g 8") << QString("C") << QString("10000000") << 1e7 << 'g' << 8; + QTest::newRow("C 1e8 g 8") << QString("C") << QString("1e+08") << 1e8 << 'g' << 8; + QTest::newRow("C 10.0 g 1") << QString("C") << QString("1e+01") << 10.0 << 'g' << 1; + QTest::newRow("C 10.0 g 0") << QString("C") << QString("1e+01") << 10.0 << 'g' << 0; + QTest::newRow("C 1.0 g 0") << QString("C") << QString("1") << 1.0 << 'g' << 0; + QTest::newRow("C 0.0001 g 0") << QString("C") << QString("0.0001") << 0.0001 << 'g' << 0; + QTest::newRow("C 0.00001 g 0") << QString("C") << QString("1e-05") << 0.00001 << 'g' << 0; + // Check transition to exponent form: + QTest::newRow("C 1245678900000 g -") << QString("C") << QString("1245678900000") << 1245678900000.0 << 'g' << shortest; + QTest::newRow("C 12456789100000 g -") << QString("C") << QString("12456789100000") << 12456789100000.0 << 'g' << shortest; + QTest::newRow("C 12456789000000 g -") << QString("C") << QString("1.2456789e+13") << 12456789000000.0 << 'g' << shortest; + QTest::newRow("C 1200000 g -") << QString("C") << QString("1200000") << 12e5 << 'g' << shortest; + QTest::newRow("C 12000000 g -") << QString("C") << QString("1.2e+07") << 12e6 << 'g' << shortest; + QTest::newRow("C 10000 g -") << QString("C") << QString("10000") << 1e4 << 'g' << shortest; + QTest::newRow("C 100000 g -") << QString("C") << QString("1e+05") << 1e5 << 'g' << shortest; QTest::newRow("C 12456789012 f 0") << QString("C") << QString("12456789012") << 12456789012.0 << 'f' << 0; QTest::newRow("C 12456789012 f -") << QString("C") << QString("12456789012") << 12456789012.0 << 'f' << shortest; @@ -1122,16 +1360,16 @@ void tst_QLocale::doubleToString_data() QTest::newRow("de_DE 12456789012 f 0") << QString("de_DE") << QString("12.456.789.012") << 12456789012.0 << 'f' << 0; QTest::newRow("de_DE 12456789012 f -") << QString("de_DE") << QString("12.456.789.012") << 12456789012.0 << 'f' << shortest; - QTest::newRow("de_DE 12456789012 e 10") << QString("de_DE") << QString("1,2456789012e+10") << 12456789012.0 << 'e' << 10; - QTest::newRow("de_DE 12456789012 e -") << QString("de_DE") << QString("1,2456789012e+10") << 12456789012.0 << 'e' << shortest; + QTest::newRow("de_DE 12456789012 e 10") << QString("de_DE") << QString("1,2456789012E+10") << 12456789012.0 << 'e' << 10; + QTest::newRow("de_DE 12456789012 e -") << QString("de_DE") << QString("1,2456789012E+10") << 12456789012.0 << 'e' << shortest; QTest::newRow("de_DE 12456789012 g 11") << QString("de_DE") << QString("12.456.789.012") << 12456789012.0 << 'g' << 11; QTest::newRow("de_DE 12456789012 g -") << QString("de_DE") << QString("12.456.789.012") << 12456789012.0 << 'g' << shortest; } void tst_QLocale::doubleToString() { - QFETCH(QString, locale_name); - QFETCH(QString, num_str); + QFETCH(QString, localeName); + QFETCH(QString, numStr); QFETCH(double, num); QFETCH(char, mode); QFETCH(int, precision); @@ -1141,11 +1379,12 @@ void tst_QLocale::doubleToString() QSKIP("'Shortest' double conversion is not that short without libdouble-conversion"); #endif - const QLocale locale(locale_name); - QCOMPARE(locale.toString(num, mode, precision), num_str); + const QLocale locale(localeName); + QCOMPARE(locale.toString(num, mode, precision), numStr); - TransientLocale ignoreme(LC_ALL, "de_DE"); - QCOMPARE(locale.toString(num, mode, precision), num_str); + // System locale is irrelevant here: + TransientLocale ignoreme(LC_ALL, "de_DE.UTF-8"); + QCOMPARE(locale.toString(num, mode, precision), numStr); } void tst_QLocale::strtod_data() @@ -1175,6 +1414,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; @@ -1206,6 +1453,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; @@ -1229,8 +1488,8 @@ void tst_QLocale::strtod() QFETCH(int, processed); QFETCH(bool, ok); - QByteArray numData = num_str.toLatin1(); - const char *end = 0; + QByteArray numData = num_str.toUtf8(); + const char *end = nullptr; bool actualOk = false; double result = qstrtod(numData.constData(), &end, &actualOk); @@ -1238,9 +1497,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); @@ -1254,8 +1513,8 @@ void tst_QLocale::strtod() QCOMPARE(actualOk, ok); } - // and QStringRef, but we can limit the length without allocating memory - QStringRef num_strref(&num_str, 0, processed); + // and QStringView, but we can limit the length without allocating memory + QStringView num_strref = QStringView{ num_str }.mid(0, processed); actualOk = false; QCOMPARE(QLocale::c().toDouble(num_strref, &actualOk), num); QCOMPARE(actualOk, ok); @@ -1283,6 +1542,9 @@ void tst_QLocale::long_long_conversion_data() QTest::newRow("C 12345,67") << QString("C") << "12345,67" << false << (qlonglong) 0; QTest::newRow("C 123456,7") << QString("C") << "123456,7" << false << (qlonglong) 0; QTest::newRow("C 1,234,567") << QString("C") << "1,234,567" << true << (qlonglong) 1234567; + using LL = std::numeric_limits<qlonglong>; + QTest::newRow("C LLONG_MIN") << QString("C") << QString::number(LL::min()) << true << LL::min(); + QTest::newRow("C LLONG_MAX") << QString("C") << QString::number(LL::max()) << true << LL::max(); QTest::newRow("de_DE 1") << QString("de_DE") << "1" << true << (qlonglong) 1; QTest::newRow("de_DE 1.") << QString("de_DE") << "1." << false << (qlonglong) 0; @@ -1320,7 +1582,7 @@ void tst_QLocale::long_long_conversion() QFETCH(QString, num_str); QFETCH(bool, good); QFETCH(qlonglong, num); - QStringRef num_strRef = num_str.leftRef(-1); + QStringView num_strRef{ num_str }; QLocale locale(locale_name); QCOMPARE(locale.name(), locale_name); @@ -1360,51 +1622,114 @@ 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 + // TODO: QTBUG-95460 -- could support localized forms of inf/NaN + const QLocale c(QLocale::C); - QVERIFY(qIsInf(-neginf)); - QVERIFY(!qIsNaN(-neginf)); - QVERIFY(!qIsFinite(-neginf)); + QT_TEST_EQUALITY_OPS(QLocale(), QLocale(QLocale::C), false); + QT_TEST_EQUALITY_OPS(QLocale(), QLocale(), true); + QT_TEST_EQUALITY_OPS(QLocale(QLocale::C), c, true); + + 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 - 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); @@ -1412,23 +1737,37 @@ 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 } +void tst_QLocale::negativeZero_data() +{ + QTest::addColumn<QLocale::Language>("language"); + QTest::addColumn<QLocale::Script>("script"); + QTest::addColumn<QLocale::Territory>("territory"); + QTest::addColumn<QStringView>("expect"); + + QTest::newRow("C") + << QLocale::C << QLocale::AnyScript << QLocale::AnyTerritory + << QStringView(u"0"); + QTest::newRow("Arabic") + << QLocale::Arabic << QLocale::ArabicScript << QLocale::AnyTerritory + << QStringView(u"\u0660"); + QTest::newRow("Chakma") + << QLocale::Chakma << QLocale::ChakmaScript << QLocale::AnyTerritory + << QStringView(u"\xD804\xDD36"); // A surrogate pair. +} + void tst_QLocale::negativeZero() { - double negativeZero( 0.0 ); // Initialise to zero. - uchar *ptr = (uchar *)&negativeZero; - ptr[QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 7] = 0x80; - QString s = QString::number(negativeZero); - QCOMPARE(s, QString("0")); + QFETCH(QLocale::Language, language); + QFETCH(QLocale::Script, script); + QFETCH(QLocale::Territory, territory); + QFETCH(QStringView, expect); + QLocale locale(language, script, territory); + QCOMPARE(locale.toString(std::copysign(0.0, -1.0)), expect); } void tst_QLocale::dayOfWeek_data() @@ -1490,6 +1829,11 @@ void tst_QLocale::formatDate_data() << QDate(1974, 12, 1) << "hh:mm:ss.zzz ap d'd'dd/M/yy" << "hh:mm:ss.zzz ap 1d01/12/74"; QTest::newRow("dd MMMM yyyy") << QDate(1, 1, 1) << "dd MMMM yyyy" << "01 January 0001"; + + // Test unicode handling. + QTest::newRow("unicode in format string") << QDate(1, 1, 1) + << u8"🔴😤🌼😫dd☀😥🤤👉💃MM💛🙂💓🤩yyyy😴" + << u8"🔴😤🌼😫01☀😥🤤👉💃01💛🙂💓🤩0001😴"; } void tst_QLocale::formatDate() @@ -1506,55 +1850,93 @@ void tst_QLocale::formatDate() void tst_QLocale::formatTime_data() { QTest::addColumn<QTime>("time"); + QTest::addColumn<QString>("locale"); QTest::addColumn<QString>("format"); QTest::addColumn<QString>("result"); - QTest::newRow("1") << QTime(1, 2, 3) << "h:m:s" << "1:2:3"; - QTest::newRow("3") << QTime(1, 2, 3) << "H:m:s" << "1:2:3"; - QTest::newRow("4") << QTime(1, 2, 3) << "hh:mm:ss" << "01:02:03"; - QTest::newRow("5") << QTime(1, 2, 3) << "HH:mm:ss" << "01:02:03"; - QTest::newRow("6") << QTime(1, 2, 3) << "hhh:mmm:sss" << "011:022:033"; - - QTest::newRow("8") << QTime(14, 2, 3) << "h:m:s" << "14:2:3"; - QTest::newRow("9") << QTime(14, 2, 3) << "H:m:s" << "14:2:3"; - QTest::newRow("10") << QTime(14, 2, 3) << "hh:mm:ss" << "14:02:03"; - QTest::newRow("11") << QTime(14, 2, 3) << "HH:mm:ss" << "14:02:03"; - QTest::newRow("12") << QTime(14, 2, 3) << "hhh:mmm:sss" << "1414:022:033"; - - QTest::newRow("14") << QTime(14, 2, 3) << "h:m:s ap" << "2:2:3 pm"; - QTest::newRow("15") << QTime(14, 2, 3) << "H:m:s AP" << "14:2:3 PM"; - QTest::newRow("16") << QTime(14, 2, 3) << "hh:mm:ss aap" << "02:02:03 pmpm"; - QTest::newRow("17") << QTime(14, 2, 3) << "HH:mm:ss AP aa" << "14:02:03 PM pmpm"; - - QTest::newRow("18") << QTime(1, 2, 3) << "h:m:s ap" << "1:2:3 am"; - QTest::newRow("19") << QTime(1, 2, 3) << "H:m:s AP" << "1:2:3 AM"; - - QTest::newRow("20") << QTime(1, 2, 3) << "foo" << "foo"; - QTest::newRow("21") << QTime(1, 2, 3) << "'" << ""; - QTest::newRow("22") << QTime(1, 2, 3) << "''" << "'"; - QTest::newRow("23") << QTime(1, 2, 3) << "'''" << "'"; - QTest::newRow("24") << QTime(1, 2, 3) << "\"" << "\""; - QTest::newRow("25") << QTime(1, 2, 3) << "\"\"" << "\"\""; - QTest::newRow("26") << QTime(1, 2, 3) << "\"H\"" << "\"1\""; - QTest::newRow("27") << QTime(1, 2, 3) << "'\"H\"'" << "\"H\""; - - QTest::newRow("28") << QTime(1, 2, 3, 456) << "H:m:s.z" << "1:2:3.456"; - QTest::newRow("29") << QTime(1, 2, 3, 456) << "H:m:s.zz" << "1:2:3.456456"; - QTest::newRow("30") << QTime(1, 2, 3, 456) << "H:m:s.zzz" << "1:2:3.456"; - QTest::newRow("31") << QTime(1, 2, 3, 400) << "H:m:s.z" << "1:2:3.4"; - QTest::newRow("32") << QTime(1, 2, 3, 4) << "H:m:s.z" << "1:2:3.004"; - QTest::newRow("33") << QTime(1, 2, 3, 4) << "H:m:s.zzz" << "1:2:3.004"; - QTest::newRow("34") << QTime() << "H:m:s.zzz" << ""; - QTest::newRow("35") << QTime(1, 2, 3, 4) << "dd MM yyyy H:m:s.zzz" << "dd MM yyyy 1:2:3.004"; + QTest::newRow("C-h:m:s-am") << QTime(1, 2, 3) << "C" << "h:m:s" << "1:2:3"; + QTest::newRow("C-H:m:s-am") << QTime(1, 2, 3) << "C" << "H:m:s" << "1:2:3"; + QTest::newRow("C-hh:mm:ss-am") << QTime(1, 2, 3) << "C" << "hh:mm:ss" << "01:02:03"; + QTest::newRow("C-HH:mm:ss-am") << QTime(1, 2, 3) << "C" << "HH:mm:ss" << "01:02:03"; + QTest::newRow("C-hhh:mmm:sss-am") << QTime(1, 2, 3) << "C" << "hhh:mmm:sss" << "011:022:033"; + + QTest::newRow("C-h:m:s-pm") << QTime(14, 2, 3) << "C" << "h:m:s" << "14:2:3"; + QTest::newRow("C-H:m:s-pm") << QTime(14, 2, 3) << "C" << "H:m:s" << "14:2:3"; + QTest::newRow("C-hh:mm:ss-pm") << QTime(14, 2, 3) << "C" << "hh:mm:ss" << "14:02:03"; + QTest::newRow("C-HH:mm:ss-pm") << QTime(14, 2, 3) << "C" << "HH:mm:ss" << "14:02:03"; + QTest::newRow("C-hhh:mmm:sss-pm") << QTime(14, 2, 3) << "C" << "hhh:mmm:sss" << "1414:022:033"; + + QTest::newRow("C-h:m:s+ap-pm") << QTime(14, 2, 3) << "C" << "h:m:s ap" << "2:2:3 pm"; + QTest::newRow("C-H:m:s+AP-pm") << QTime(14, 2, 3) << "C" << "H:m:s AP" << "14:2:3 PM"; + QTest::newRow("C-hh:mm:ss+aap-pm") + << QTime(14, 2, 3) << "C" << "hh:mm:ss aap" << "02:02:03 pmpm"; + QTest::newRow("C-HH:mm:ss+AP+aa-pm") + << QTime(14, 2, 3) << "C" << "HH:mm:ss AP aa" << "14:02:03 PM pmpm"; + + QTest::newRow("C-h:m:s+ap-am") << QTime(1, 2, 3) << "C" << "h:m:s ap" << "1:2:3 am"; + QTest::newRow("C-H:m:s+AP-am") << QTime(1, 2, 3) << "C" << "H:m:s AP" << "1:2:3 AM"; + + QTest::newRow("C-foo") << QTime(1, 2, 3) << "C" << "foo" << "foo"; + QTest::newRow("C-quote") << QTime(1, 2, 3) << "C" << "'" << ""; + QTest::newRow("C-quote*2") << QTime(1, 2, 3) << "C" << "''" << "'"; + QTest::newRow("C-quote*3") << QTime(1, 2, 3) << "C" << "'''" << "'"; + QTest::newRow("C-dquote") << QTime(1, 2, 3) << "C" << "\"" << "\""; + QTest::newRow("C-dquote*2") << QTime(1, 2, 3) << "C" << "\"\"" << "\"\""; + QTest::newRow("C-dquote-H") << QTime(1, 2, 3) << "C" << "\"H\"" << "\"1\""; + 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.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"; + QTest::newRow("C-H:m:s.z=004") << QTime(1, 2, 3, 4) << "C" << "H:m:s.z" << "1:2:3.004"; + QTest::newRow("C-H:m:s.zzz=004") << QTime(1, 2, 3, 4) << "C" << "H:m:s.zzz" << "1:2:3.004"; + + QTest::newRow("C-invalid") << QTime() << "C" << "H:m:s.zzz" << ""; + QTest::newRow("C-date-time") + << QTime(1, 2, 3, 4) << "C" << "dd MM yyyy H:m:s.zzz" << "dd MM yyyy 1:2:3.004"; + + // Test unicode handling. + QTest::newRow("C-emoji") + << QTime(17, 22, 05, 18) << "C" << u8"m📌ss📢H.zzz" << u8"22📌05📢17.018"; + + // Test-cases related to QTBUG-95790 (case of localized am/pm indicators): + QTest::newRow("en_US-h:m:s+ap-pm") + << QTime(14, 2, 3) << "en_US" << "h:m:s ap" << "2:2:3 pm"; + QTest::newRow("en_US-H:m:s+AP-pm") + << QTime(14, 2, 3) << "en_US" << "H:m:s AP" << "14:2:3 PM"; + QTest::newRow("en_US-H:m:s+Ap-pm") + << QTime(14, 2, 3) << "en_US" << "H:m:s Ap" << "14:2:3 PM"; + QTest::newRow("en_US-h:m:s+ap-am") + << QTime(1, 2, 3) << "en_US" << "h:m:s ap" << "1:2:3 am"; + QTest::newRow("en_US-H:m:s+AP-am") + << QTime(1, 2, 3) << "en_US" << "H:m:s AP" << "1:2:3 AM"; + QTest::newRow("en_US-H:m:s+aP-am") + << QTime(1, 2, 3) << "en_US" << "H:m:s aP" << "1:2:3 AM"; + + QTest::newRow("cs_CZ-h:m:s+ap-pm") + << QTime(14, 2, 3) << "cs_CZ" << "h:m:s ap" << "2:2:3 odp."; + QTest::newRow("cs_CZ-h:m:s+AP-pm") + << QTime(14, 2, 3) << "cs_CZ" << "h:m:s AP" << "2:2:3 ODP."; + QTest::newRow("cs_CZ-h:m:s+Ap-pm") + << QTime(14, 2, 3) << "cs_CZ" << "h:m:s Ap" << "2:2:3 odp."; + QTest::newRow("cs_CZ-h:m:s+ap-am") + << QTime(1, 2, 3) << "cs_CZ" << "h:m:s ap" << "1:2:3 dop."; + QTest::newRow("cs_CZ-h:m:s+AP-am") + << QTime(1, 2, 3) << "cs_CZ" << "h:m:s AP" << "1:2:3 DOP."; + QTest::newRow("cs_CZ-h:m:s+aP-am") + << QTime(1, 2, 3) << "cs_CZ" << "h:m:s aP" << "1:2:3 dop."; } void tst_QLocale::formatTime() { - QFETCH(QTime, time); - QFETCH(QString, format); - QFETCH(QString, result); + QFETCH(const QTime, time); + QFETCH(const QString, locale); + QFETCH(const QString, format); + QFETCH(const QString, result); - QLocale l(QLocale::C); + QLocale l(locale); QCOMPARE(l.toString(time, format), result); QCOMPARE(l.toString(time, QStringView(format)), result); } @@ -1668,7 +2050,7 @@ void tst_QLocale::formatDateTime_data() << QString("31-apAP12-1999 23:59:59.999"); QTest::newRow("datetime3") << "en_US" << testLongHour << QString("Apdd-MM-yyyy hh:mm:ss.zzz") - << QString("PMp31-12-1999 11:59:59.999"); + << QString("PM31-12-1999 11:59:59.999"); QTest::newRow("datetime4") << "en_US" << testLongHour << QString("'ap'apdd-MM-yyyy 'AP'hh:mm:ss.zzz") << QString("appm31-12-1999 AP11:59:59.999"); @@ -1707,6 +2089,11 @@ void tst_QLocale::formatDateTime_data() << QString("MMM") << QString("Dec"); QTest::newRow("empty") << "en_US" << testZeroHour << QString("") << QString(""); + + // Test unicode handling. + QTest::newRow("emoji in format string") + << "en_US" << QDateTime(QDate(1980, 2, 1), QTime(17, 12)) + << QString(u8"💖yyyy💖MM💖dd hh💖mm") << u8"💖1980💖02💖01 17💖12"; } void tst_QLocale::formatDateTime() @@ -1725,64 +2112,61 @@ 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)"); } -#ifdef Q_OS_ANDROID // Only reports (general) zones as offsets (QTBUG-68837) - const QString cet(QStringLiteral("GMT+01:00")); - const QString cest(QStringLiteral("GMT+02:00")); -#elif defined Q_OS_DARWIN - const QString cet(QStringLiteral("GMT+1")); - const QString cest(QStringLiteral("GMT+2")); -#else - const QString cet(QStringLiteral("CET")); - const QString cest(QStringLiteral("CEST")); -#endif +#if QT_CONFIG(timezone) + const QTimeZone berlin("Europe/Berlin"); + const QDateTime jan(QDate(2010, 1, 1).startOfDay(berlin)); + const QDateTime jul(QDate(2010, 7, 1).startOfDay(berlin)); - QDateTime dt6(QDate(2013, 1, 1), QTime(0, 0, 0), QTimeZone("Europe/Berlin")); -#ifdef Q_OS_WIN - QEXPECT_FAIL("", "QTimeZone windows backend only returns long name", Continue); + QCOMPARE(enUS.toString(jan, "t"), berlin.displayName(jan, QTimeZone::ShortName, enUS)); + QCOMPARE(enUS.toString(jul, "t"), berlin.displayName(jul, QTimeZone::ShortName, enUS)); #endif - QCOMPARE(enUS.toString(dt6, "t"), cet); - QDateTime dt7(QDate(2013, 6, 1), QTime(0, 0, 0), QTimeZone("Europe/Berlin")); -#ifdef Q_OS_WIN - QEXPECT_FAIL("", "QTimeZone windows backend only returns long name", Continue); + // 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 - QCOMPARE(enUS.toString(dt7, "t"), cest); + zone = now.timeZoneAbbreviation(); + QCOMPARE(enUS.toString(now, "t"), zone); - // Current datetime should return current abbreviation - QCOMPARE(enUS.toString(QDateTime::currentDateTime(), "t"), - QDateTime::currentDateTime().timeZoneAbbreviation()); - - // 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() @@ -1791,65 +2175,139 @@ void tst_QLocale::toDateTime_data() QTest::addColumn<QDateTime>("result"); QTest::addColumn<QString>("format"); QTest::addColumn<QString>("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"; + << "d/M/yyyy hh:h:mm" << "1/12/1974 05:5:14" << true; QTest::newRow("2C") << "C" << QDateTime(QDate(1974, 12, 1), QTime(15, 0, 0)) - << "d/M/yyyyy h" << "1/12/1974y 15"; + << "d/M/yyyyy h" << "1/12/1974y 15" << false; QTest::newRow("4C") << "C" << QDateTime(QDate(1974, 1, 1), QTime(0, 0, 0, 1)) - << "d/M/yyyy zzz" << "1/1/1974 001"; + << "d/M/yyyy zzz" << "1/1/1974 001" << true; QTest::newRow("5C") << "C" << QDateTime(QDate(1974, 1, 1), QTime(0, 0, 0, 1)) - << "dd/MM/yyy z" << "01/01/74y 001"; - QTest::newRow("5Cbis") << "C" << QDateTime(QDate(1974, 1, 1), QTime(0, 0, 0, 100)) - << "dd/MM/yyy z" << "01/01/74y 1"; + << "dd/MM/yyy z" << "01/01/74y 001" << false; + QTest::newRow("6C") << "C" << QDateTime(QDate(1974, 1, 1), QTime(0, 0, 0, 100)) + << "dd/MM/yyy z" << "01/01/74y 1" << false; QTest::newRow("8C") << "C" << QDateTime(QDate(1974, 12, 2), QTime(0, 0, 13)) - << "ddddd/MMMMM/yy ss" << "Monday2/December12/74 13"; + << "ddddd/MMMMM/yy ss" << "Monday2/December12/74 13" << true; QTest::newRow("9C") << "C" << QDateTime(QDate(1974, 12, 1), QTime(0, 0, 13)) - << "'dddd'/MMMM/yy s" << "dddd/December/74 13"; + << "'dddd'/MMMM/yy s" << "dddd/December/74 13" << false; QTest::newRow("10C") << "C" << QDateTime(QDate(1974, 12, 1), QTime(0, 4, 0)) - << "d'dd'd/MMMM/yyy m'm'mm" << "1dd1/December/74y 4m04"; + << "d'dd'd/MMMM/yyy m'm'mm" << "1dd1/December/74y 4m04" << false; QTest::newRow("11C") << "C" << QDateTime(QDate(1974, 12, 1), QTime(0, 0, 3)) - << "d'dd'd/MMM'M'/yysss" << "1dd1/DecM/74033"; + << "d'dd'd/MMM'M'/yysss" << "1dd1/DecM/74033" << false; QTest::newRow("12C") << "C" << QDateTime(QDate(1974, 12, 1), QTime(15, 0, 0)) - << "d'd'dd/M/yyh" << "1d01/12/7415"; + << "d'd'dd/M/yyh" << "1d01/12/7415" << false; // Unpadded value for fixed-width field is wrong: - QTest::newRow("bad-day-C") << "C" << QDateTime() << "dd-MMM-yy" << "4-Jun-11"; - QTest::newRow("bad-month-C") << "C" << QDateTime() << "d-MM-yy" << "4-6-11"; - QTest::newRow("bad-year-C") << "C" << QDateTime() << "d-MMM-yyyy" << "4-Jun-11"; - QTest::newRow("bad-hour-C") << "C" << QDateTime() << "d-MMM-yy hh:m" << "4-Jun-11 1:2"; - QTest::newRow("bad-min-C") << "C" << QDateTime() << "d-MMM-yy h:mm" << "4-Jun-11 1:2"; - QTest::newRow("bad-sec-C") << "C" << QDateTime() << "d-MMM-yy h:m:ss" << "4-Jun-11 1:2:3"; + QTest::newRow("bad-day-C") << "C" << QDateTime() << "dd-MMM-yy" << "4-Jun-11" << true; + QTest::newRow("bad-month-C") << "C" << QDateTime() << "d-MM-yy" << "4-6-11" << true; + QTest::newRow("bad-year-C") << "C" << QDateTime() << "d-MMM-yyyy" << "4-Jun-11" << true; + QTest::newRow("bad-hour-C") << "C" << QDateTime() << "d-MMM-yy hh:m" << "4-Jun-11 1:2" << true; + QTest::newRow("bad-min-C") << "C" << QDateTime() << "d-MMM-yy h:mm" << "4-Jun-11 1:2" << true; + QTest::newRow("bad-sec-C") + << "C" << QDateTime() << "d-MMM-yy h:m:ss" << "4-Jun-11 1:2:3" << true; QTest::newRow("bad-milli-C") - << "C" << QDateTime() << "d-MMM-yy h:m:s.zzz" << "4-Jun-11 1:2:3.4"; + << "C" << QDateTime() << "d-MMM-yy h:m:s.zzz" << "4-Jun-11 1:2:3.4" << true; QTest::newRow("ok-C") << "C" << QDateTime(QDate(1911, 6, 4), QTime(1, 2, 3, 400)) - << "d-MMM-yy h:m:s.z" << "4-Jun-11 1:2:3.4"; + << "d-MMM-yy h:m:s.z" << "4-Jun-11 1:2:3.4" << true; QTest::newRow("1no_NO") << "no_NO" << QDateTime(QDate(1974, 12, 1), QTime(5, 14, 0)) - << "d/M/yyyy hh:h:mm" << "1/12/1974 05:5:14"; + << "d/M/yyyy hh:h:mm" << "1/12/1974 05:5:14" << true; QTest::newRow("2no_NO") << "no_NO" << QDateTime(QDate(1974, 12, 1), QTime(15, 0, 0)) - << "d/M/yyyyy h" << "1/12/1974y 15"; + << "d/M/yyyyy h" << "1/12/1974y 15" << false; QTest::newRow("4no_NO") << "no_NO" << QDateTime(QDate(1974, 1, 1), QTime(0, 0, 0)) - << "d/M/yyyy zzz" << "1/1/1974 000"; + << "d/M/yyyy zzz" << "1/1/1974 000" << true; QTest::newRow("5no_NO") << "no_NO" << QDateTime(QDate(1974, 1, 1), QTime(0, 0, 0)) - << "dd/MM/yyy z" << "01/01/74y 0"; + << "dd/MM/yyy z" << "01/01/74y 0" << false; QTest::newRow("8no_NO") << "no_NO" << QDateTime(QDate(1974, 12, 2), QTime(0, 0, 13)) - << "ddddd/MMMMM/yy ss" << "mandag2/desember12/74 13"; + << "ddddd/MMMMM/yy ss" << "mandag2/desember12/74 13" << true; QTest::newRow("9no_NO") << "no_NO" << QDateTime(QDate(1974, 12, 1), QTime(0, 0, 13)) - << "'dddd'/MMMM/yy s" << "dddd/desember/74 13"; + << "'dddd'/MMMM/yy s" << "dddd/desember/74 13" << false; QTest::newRow("10no_NO") << "no_NO" << QDateTime(QDate(1974, 12, 1), QTime(0, 4, 0)) - << "d'dd'd/MMMM/yyy m'm'mm" << "1dd1/desember/74y 4m04"; + << "d'dd'd/MMMM/yyy m'm'mm" << "1dd1/desember/74y 4m04" << false; QTest::newRow("11no_NO") << "no_NO" << QDateTime(QDate(1974, 12, 1), QTime(0, 0, 3)) - << "d'dd'd/MMM'M'/yysss" << "1dd1/des.M/74033"; + << "d'dd'd/MMM'M'/yysss" << "1dd1/des.M/74033" << false; QTest::newRow("12no_NO") << "no_NO" << QDateTime(QDate(1974, 12, 1), QTime(15, 0, 0)) - << "d'd'dd/M/yyh" << "1d01/12/7415"; + << "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"; + << "ddd, dd MMM yyyy hh:mm:ss 'GMT'" << "Thu, 01 Nov 2007 18:08:30 GMT" << false; QTest::newRow("longFormat") << "en_US" << QDateTime(QDate(2009, 1, 5), QTime(11, 48, 32)) - << "dddd, MMMM d, yyyy h:mm:ss AP " << "Monday, January 5, 2009 11:48:32 AM "; + << "dddd, MMMM d, yyyy h:mm:ss AP " << "Monday, January 5, 2009 11:48:32 AM " << true; + + // Parsing am/pm indicators case-insensitively: + QTest::newRow("am-cs_CZ") + << "cs_CZ" << QDateTime(QDate(1945, 8, 6), QTime(8, 15, 44, 400)) + << "yyyy-MM-dd hh:mm:ss.z aP" << "1945-08-06 08:15:44.4 dOp." << true; + QTest::newRow("pm-cs_CZ") + << "cs_CZ" << QDateTime(QDate(1945, 8, 15), QTime(12, 0)) + << "yyyy-MM-dd hh:mm aP" << "1945-08-15 12:00 OdP." << true; + + const QDateTime dt(QDate(2017, 02, 25), QTime(17, 21, 25)); + // These formats correspond to the locale formats, with the timezone removed. + // We hardcode them in case an update to the locale DB changes them. + + QTest::newRow("C:long") << "C" << dt << "dddd, d MMMM yyyy HH:mm:ss" + << "Saturday, 25 February 2017 17:21:25" << true; + QTest::newRow("C:short") + << "C" << dt << "d MMM yyyy HH:mm:ss" << "25 Feb 2017 17:21:25" << true; + QTest::newRow("C:narrow") + << "C" << dt << "d MMM yyyy HH:mm:ss" << "25 Feb 2017 17:21:25" << true; + + // Test the same again with unicode and emoji. + QTest::newRow("C:long with emoji") << "C" << dt << u8"dddd, d💪MMMM yyyy HH:mm:ss" + << u8"Saturday, 25💪February 2017 17:21:25" << true; + QTest::newRow("C:short with emoji") + << "C" << dt << u8"d MMM yyyy HH📞mm📞ss" << u8"25 Feb 2017 17📞21📞25" << true; + QTest::newRow("C:narrow with emoji") + << "C" << dt << u8"🇬🇧d MMM yyyy HH:mm:ss🇬🇧" << u8"🇬🇧25 Feb 2017 17:21:25🇬🇧" << true; + + QTest::newRow("fr:long") << "fr" << dt << "dddd d MMMM yyyy HH:mm:ss" + << "Samedi 25 février 2017 17:21:25" << true; + QTest::newRow("fr:short") + << "fr" << dt.addSecs(-25) << "dd/MM/yyyy HH:mm" << "25/02/2017 17:21" << true; + + // In Turkish, the word for Friday ("Cuma") is a prefix for the word for + // Saturday ("Cumartesi") + QTest::newRow("tr:long") + << "tr" << dt << "d MMMM yyyy dddd HH:mm:ss" << "25 Şubat 2017 Cumartesi 17:21:25" << true; + QTest::newRow("tr:long2") << "tr" << dt.addDays(-1) << "d MMMM yyyy dddd HH:mm:ss" + << "24 Şubat 2017 Cuma 17:21:25" << true; + QTest::newRow("tr:mashed") + << "tr" << dt << "d MMMMyyyy ddddHH:mm:ss" << "25 Şubat2017 Cumartesi17:21:25" << true; + QTest::newRow("tr:mashed2") << "tr" << dt.addDays(-1) << "d MMMMyyyy ddddHH:mm:ss" + << "24 Şubat2017 Cuma17:21:25" << true; + QTest::newRow("tr:short") + << "tr" << dt.addSecs(-25) << "d.MM.yyyy HH:mm" << "25.02.2017 17:21" << true; + + QTest::newRow("ccp:short") + << "ccp" << dt << "dd/M/yy h:mm AP" + // "𑄸𑄻/𑄸/𑄷𑄽 𑄻:𑄸𑄷 PM" + << QString::fromUcs4(U"\U00011138\U0001113b/\U00011138/\U00011137\U0001113d \U0001113b:" + U"\U00011138\U00011137 PM") << true; + QTest::newRow("ccp:long") + << "ccp" << dt << "dddd, d MMMM, yyyy h:mm:ss AP" + // "𑄥𑄧𑄚𑄨𑄝𑄢𑄴, 𑄸𑄻 𑄜𑄬𑄛𑄴𑄝𑄳𑄢𑄪𑄠𑄢𑄨, 𑄸𑄶𑄷𑄽 𑄻:𑄸𑄷:𑄸𑄻 PM" + << 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 " + U"\U0001113b:\U00011138\U00011137:\U00011138\U0001113b PM") << true; } void tst_QLocale::toDateTime() @@ -1858,25 +2316,384 @@ void tst_QLocale::toDateTime() QFETCH(QDateTime, result); QFETCH(QString, format); QFETCH(QString, string); + QFETCH(bool, clean); QLocale l(localeName); + 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(l.toDateTime(string, format), result); + if (clean) { + QCOMPARE(l.toDateTime(string.toLower(), format), result); + QCOMPARE(l.toDateTime(string.toUpper(), format), result); + } + if (l.dateTimeFormat(QLocale::LongFormat) == format) QCOMPARE(l.toDateTime(string, QLocale::LongFormat), result); + if (l.dateTimeFormat(QLocale::ShortFormat) == format) + 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". -QString systemLocaleFormatNumber(const QString &numberString) +QString systemLocaleFormatNumber(QString &&numberString) { QLocale locale = QLocale::system(); - QString numberStringCopy = numberString; - return numberStringCopy.replace(QChar(','), QChar('G')) - .replace(QChar('.'), QChar('D')) - .replace(QChar('G'), locale.groupSeparator()) - .replace(QChar('D'), locale.decimalPoint()); + QString numberStringMunged = + numberString.replace(QChar(','), QChar('G')).replace(QChar('.'), QChar('D')); + if (locale.numberOptions() & QLocale::OmitGroupSeparator) + numberStringMunged.remove(QLatin1Char('G')); + else + numberStringMunged.replace(QChar('G'), locale.groupSeparator()); + return numberStringMunged.replace(QChar('D'), locale.decimalPoint()); } void tst_QLocale::macDefaultLocale() @@ -1898,13 +2715,14 @@ void tst_QLocale::macDefaultLocale() // On OS X the decimal point and group separator are configurable // independently of the locale. Verify that they have one of the // allowed values and are not the same. - QVERIFY(locale.decimalPoint() == QChar('.') || locale.decimalPoint() == QChar(',')); - QVERIFY(locale.groupSeparator() == QChar(',') - || locale.groupSeparator() == QChar('.') - || locale.groupSeparator() == QChar('\xA0') // no-breaking space - || locale.groupSeparator() == QChar('\'') - || locale.groupSeparator() == QChar()); - QVERIFY(locale.decimalPoint() != locale.groupSeparator()); + QVERIFY(locale.decimalPoint() == QStringView(u".") + || locale.decimalPoint() == QStringView(u",")); + QVERIFY(locale.groupSeparator() == QStringView(u",") + || locale.groupSeparator() == QStringView(u".") + || locale.groupSeparator() == QStringView(u"\xA0") // no-breaking space + || locale.groupSeparator() == QStringView(u"'") + || locale.groupSeparator().isEmpty()); + 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"))); @@ -1964,9 +2782,9 @@ void tst_QLocale::macDefaultLocale() QCOMPARE(locale.weekdays(), days); } -#endif // Q_OS_MAC +#endif // Q_OS_DARWIN -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined(Q_OS_WIN) #include <qt_windows.h> static QString getWinLocaleInfo(LCTYPE type) @@ -1989,7 +2807,7 @@ static QString getWinLocaleInfo(LCTYPE type) return QString::fromWCharArray(buf.data()); } -static void setWinLocaleInfo(LCTYPE type, const QString &value) +static void setWinLocaleInfo(LCTYPE type, QStringView value) { LCID id = GetThreadLocale(); SetLocaleInfo(id, type, reinterpret_cast<const wchar_t*>(value.utf16())); @@ -2008,7 +2826,10 @@ public: m_thousand = getWinLocaleInfo(LOCALE_STHOUSAND); m_sdate = getWinLocaleInfo(LOCALE_SSHORTDATE); m_ldate = getWinLocaleInfo(LOCALE_SLONGDATE); - m_time = getWinLocaleInfo(LOCALE_SSHORTTIME); + m_stime = getWinLocaleInfo(LOCALE_SSHORTTIME); + m_ltime = getWinLocaleInfo(LOCALE_STIMEFORMAT); + m_digits = getWinLocaleInfo(LOCALE_SNATIVEDIGITS); + m_subst = getWinLocaleInfo(LOCALE_IDIGITSUBSTITUTION); } ~RestoreLocaleHelper() @@ -2018,64 +2839,77 @@ public: setWinLocaleInfo(LOCALE_STHOUSAND, m_thousand); setWinLocaleInfo(LOCALE_SSHORTDATE, m_sdate); setWinLocaleInfo(LOCALE_SLONGDATE, m_ldate); - setWinLocaleInfo(LOCALE_SSHORTTIME, m_time); + setWinLocaleInfo(LOCALE_SSHORTTIME, m_stime); + setWinLocaleInfo(LOCALE_STIMEFORMAT, m_ltime); + setWinLocaleInfo(LOCALE_SNATIVEDIGITS, m_digits); + setWinLocaleInfo(LOCALE_IDIGITSUBSTITUTION, m_subst); - QSystemLocale dummy; // to provoke a refresh of the system locale + QTestLocaleChange::resetSystemLocale(); } - QString m_decimal, m_thousand, m_sdate, m_ldate, m_time; + QString m_decimal, m_thousand, m_sdate, m_ldate, m_stime, m_ltime, m_digits, m_subst; }; void tst_QLocale::windowsDefaultLocale() { RestoreLocaleHelper systemLocale; // set weird system defaults and make sure we're using them - setWinLocaleInfo(LOCALE_SDECIMAL, QLatin1String("@")); - setWinLocaleInfo(LOCALE_STHOUSAND, QLatin1String("?")); + setWinLocaleInfo(LOCALE_SDECIMAL, u"@"); + setWinLocaleInfo(LOCALE_STHOUSAND, u"?"); const QString shortDateFormat = QStringLiteral("d*M*yyyy"); setWinLocaleInfo(LOCALE_SSHORTDATE, shortDateFormat); const QString longDateFormat = QStringLiteral("d@M@yyyy"); setWinLocaleInfo(LOCALE_SLONGDATE, longDateFormat); const QString shortTimeFormat = QStringLiteral("h^m^s"); setWinLocaleInfo(LOCALE_SSHORTTIME, shortTimeFormat); - - QSystemLocale dummy; // to provoke a refresh of the system locale - QLocale locale = QLocale::system(); - - // make sure we are seeing the system's format strings - QCOMPARE(locale.decimalPoint(), QChar('@')); - QCOMPARE(locale.groupSeparator(), QChar('?')); + const QString longTimeFormat = QStringLiteral("HH%mm%ss"); + setWinLocaleInfo(LOCALE_STIMEFORMAT, longTimeFormat); + // Suzhou numerals (QTBUG-85409): + const QStringView digits = u"\u3007\u3021\u3022\u3023\u3024\u3025\u3026\u3027\u3028\u3029"; + setWinLocaleInfo(LOCALE_SNATIVEDIGITS, digits); + setWinLocaleInfo(LOCALE_IDIGITSUBSTITUTION, u"2"); + // NB: when adding to the system things being set, be sure to update RestoreLocaleHelper, too. + + QLocale locale = QTestLocaleChange::resetSystemLocale(); + + // Make sure we are seeing the system's format strings + QCOMPARE(locale.zeroDigit(), QStringView(u"\u3007")); + QCOMPARE(locale.decimalPoint(), QStringView(u"@")); + QCOMPARE(locale.groupSeparator(), QStringView(u"?")); QCOMPARE(locale.dateFormat(QLocale::ShortFormat), shortDateFormat); QCOMPARE(locale.dateFormat(QLocale::LongFormat), longDateFormat); QCOMPARE(locale.timeFormat(QLocale::ShortFormat), shortTimeFormat); QCOMPARE(locale.dateTimeFormat(QLocale::ShortFormat), shortDateFormat + QLatin1Char(' ') + shortTimeFormat); const QString expectedLongDateTimeFormat - = longDateFormat + QLatin1Char(' ') + QStringLiteral("h:mm:ss AP"); + = longDateFormat + QLatin1Char(' ') + longTimeFormat; QCOMPARE(locale.dateTimeFormat(QLocale::LongFormat), expectedLongDateTimeFormat); // make sure we are using the system to parse them - QCOMPARE(locale.toString(1234.56), QString("1?234@56")); - QCOMPARE(locale.toString(QDate(1974, 12, 1), QLocale::ShortFormat), QString("1*12*1974")); + QCOMPARE(locale.toString(1234.56), QStringView(u"\u3021?\u3022\u3023\u3024@\u3025\u3026")); + QCOMPARE(locale.toString(QDate(1974, 12, 1), QLocale::ShortFormat), + QStringView(u"\u3021*\u3021\u3022*\u3021\u3029\u3027\u3024")); QCOMPARE(locale.toString(QDate(1974, 12, 1), QLocale::NarrowFormat), locale.toString(QDate(1974, 12, 1), QLocale::ShortFormat)); - QCOMPARE(locale.toString(QDate(1974, 12, 1), QLocale::LongFormat), QString("1@12@1974")); - const QString expectedFormattedShortTimeSeconds = QStringLiteral("1^2^3"); - const QString expectedFormattedShortTime = QStringLiteral("1^2"); + 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^\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)); - const QString expectedFormattedLongTime = QStringLiteral("1:02:03 AM"); + const QString expectedFormattedLongTime + = QStringView(u"\u3007\u3021%\u3007\u3022%\u3007\u3023").toString(); QCOMPARE(locale.toString(QTime(1,2,3), QLocale::LongFormat), expectedFormattedLongTime); QCOMPARE(locale.toString(QDateTime(QDate(1974, 12, 1), QTime(1,2,3)), QLocale::ShortFormat), - QStringLiteral("1*12*1974 ") + expectedFormattedShortTime); + QStringView(u"\u3021*\u3021\u3022*\u3021\u3029\u3027\u3024 ").toString() + + expectedFormattedShortTime); QCOMPARE(locale.toString(QDateTime(QDate(1974, 12, 1), QTime(1,2,3)), QLocale::NarrowFormat), locale.toString(QDateTime(QDate(1974, 12, 1), QTime(1,2,3)), QLocale::ShortFormat)); QCOMPARE(locale.toString(QDateTime(QDate(1974, 12, 1), QTime(1,2,3)), QLocale::LongFormat), - QStringLiteral("1@12@1974 ") + expectedFormattedLongTime); - QCOMPARE(locale.toString(QTime(1,2,3), QLocale::LongFormat), expectedFormattedLongTime); + QStringView(u"\u3021@\u3021\u3022@\u3021\u3029\u3027\u3024 ").toString() + + expectedFormattedLongTime); } -#endif // Q_OS_WIN but !Q_OS_WINRT +#endif // Q_OS_WIN void tst_QLocale::numberOptions() { @@ -2145,6 +2979,7 @@ void tst_QLocale::numberOptions() QVERIFY(ok); locale.toDouble(QString("12.400"), &ok); QVERIFY(!ok); + QT_TEST_EQUALITY_OPS(locale, locale2, false); } void tst_QLocale::negativeNumbers() @@ -2189,6 +3024,27 @@ 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); + QT_TEST_EQUALITY_OPS(egypt, farsi, false); } #include <private/qlocale_p.h> @@ -2198,58 +3054,59 @@ static const int locale_data_count = sizeof(locale_data)/sizeof(locale_data[0]); void tst_QLocale::testNames_data() { - QTest::addColumn<int>("language"); - QTest::addColumn<int>("country"); + QTest::addColumn<QLocale::Language>("language"); + QTest::addColumn<QLocale::Territory>("country"); QLocale::setDefault(QLocale(QLocale::C)); // Ensures predictable fall-backs 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)).toUtf8(); + const QByteArray land = + QLocale::territoryToString(QLocale::Territory(item.m_territory_id)).toUtf8(); - const QString testName = QLatin1String("data_") + QString::number(i) + QLatin1String(" (") - + QLocale::languageToString((QLocale::Language)item.m_language_id) - + QLatin1Char('/') + QLocale::countryToString((QLocale::Country)item.m_country_id) - + QLatin1Char(')'); - QTest::newRow(testName.toLatin1().constData()) << (int)item.m_language_id << (int)item.m_country_id; + QTest::addRow("data_%d (%s/%s)", i, lang.constData(), land.constData()) + << QLocale::Language(item.m_language_id) << QLocale::Territory(item.m_territory_id); } } void tst_QLocale::testNames() { - QFETCH(int, language); - QFETCH(int, country); + QFETCH(QLocale::Language, language); + QFETCH(const QLocale::Territory, country); - QLocale l1((QLocale::Language)language, (QLocale::Country)country); - if (language == QLocale::AnyLanguage && country == QLocale::AnyCountry) + const QLocale l1(language, country); + if (language == QLocale::AnyLanguage && country == QLocale::AnyTerritory) language = QLocale::C; - QCOMPARE((int)l1.language(), language); - QCOMPARE((int)l1.country(), country); + QCOMPARE(l1.language(), language); + QCOMPARE(l1.territory(), country); - QString name = l1.name(); + const QString name = l1.name(); - QLocale l2(name); - QCOMPARE((int)l2.language(), language); - QCOMPARE((int)l2.country(), country); + const QLocale l2(name); + QCOMPARE(l2.language(), language); + QCOMPARE(l2.territory(), country); QCOMPARE(l2.name(), name); - QLocale l3(name + QLatin1String("@foo")); - QCOMPARE((int)l3.language(), language); - QCOMPARE((int)l3.country(), country); + const QLocale l3(name + QLatin1String("@foo")); + QCOMPARE(l3.language(), language); + QCOMPARE(l3.territory(), country); QCOMPARE(l3.name(), name); - QLocale l4(name + QLatin1String(".foo")); - QCOMPARE((int)l4.language(), language); - QCOMPARE((int)l4.country(), country); + const QLocale l4(name + QLatin1String(".foo")); + QCOMPARE(l4.language(), language); + QCOMPARE(l4.territory(), country); QCOMPARE(l4.name(), name); if (language != QLocale::C) { - int idx = name.indexOf(QLatin1Char('_')); - QVERIFY(idx != -1); - QString lang = name.left(idx); + const int idx = name.indexOf(QLatin1Char('_')); + QCOMPARE_NE(idx, -1); + const QString lang = name.left(idx); - QCOMPARE((int)QLocale(lang).language(), language); - QCOMPARE((int)QLocale(lang + QLatin1String("@foo")).language(), language); - QCOMPARE((int)QLocale(lang + QLatin1String(".foo")).language(), language); + QCOMPARE(QLocale(lang).language(), language); + QCOMPARE(QLocale(lang + QLatin1String("@foo")).language(), language); + QCOMPARE(QLocale(lang + QLatin1String(".foo")).language(), language); } } @@ -2276,27 +3133,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() @@ -2335,12 +3194,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() @@ -2393,10 +3251,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() @@ -2413,17 +3270,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() @@ -2446,6 +3302,40 @@ void tst_QLocale::dateFormat() const QLocale ir("ga_IE"); QCOMPARE(ir.dateFormat(QLocale::ShortFormat), QLatin1String("dd/MM/yyyy")); + + QT_TEST_EQUALITY_OPS(c, no, false); + QT_TEST_EQUALITY_OPS(ca, ja, false); + QT_TEST_EQUALITY_OPS(ca, ir, false); + QT_TEST_EQUALITY_OPS(ir, ja, false); + + 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() @@ -2457,19 +3347,29 @@ void tst_QLocale::timeFormat() const QLocale no("no_NO"); QCOMPARE(no.timeFormat(QLocale::NarrowFormat), QLatin1String("HH:mm")); QCOMPARE(no.timeFormat(QLocale::ShortFormat), QLatin1String("HH:mm")); - QCOMPARE(no.timeFormat(QLocale::LongFormat), QLatin1String("HH:mm:ss t")); + QCOMPARE(no.timeFormat(QLocale::LongFormat), "HH:mm:ss tttt"_L1); const QLocale id("id_ID"); QCOMPARE(id.timeFormat(QLocale::ShortFormat), QLatin1String("HH.mm")); - QCOMPARE(id.timeFormat(QLocale::LongFormat), QLatin1String("HH.mm.ss t")); + QCOMPARE(id.timeFormat(QLocale::LongFormat), "HH.mm.ss tttt"_L1); const QLocale cat("ca_ES"); QCOMPARE(cat.timeFormat(QLocale::ShortFormat), QLatin1String("H:mm")); - QCOMPARE(cat.timeFormat(QLocale::LongFormat), QLatin1String("H:mm:ss t")); + QCOMPARE(cat.timeFormat(QLocale::LongFormat), "H:mm:ss (tttt)"_L1); const QLocale bra("pt_BR"); QCOMPARE(bra.timeFormat(QLocale::ShortFormat), QLatin1String("HH:mm")); - QCOMPARE(bra.timeFormat(QLocale::LongFormat), QLatin1String("HH:mm:ss t")); + QCOMPARE(bra.timeFormat(QLocale::LongFormat), "HH:mm:ss tttt"_L1); + + // 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 [tttt]"_L1); + + QT_TEST_EQUALITY_OPS(c, no, false); + QT_TEST_EQUALITY_OPS(id, no, false); + QT_TEST_EQUALITY_OPS(c, cat, false); + QT_TEST_EQUALITY_OPS(bra, no, false); } void tst_QLocale::dateTimeFormat() @@ -2481,7 +3381,9 @@ void tst_QLocale::dateTimeFormat() const QLocale no("no_NO"); QCOMPARE(no.dateTimeFormat(QLocale::NarrowFormat), QLatin1String("dd.MM.yyyy HH:mm")); QCOMPARE(no.dateTimeFormat(QLocale::ShortFormat), QLatin1String("dd.MM.yyyy HH:mm")); - QCOMPARE(no.dateTimeFormat(QLocale::LongFormat), QLatin1String("dddd d. MMMM yyyy HH:mm:ss t")); + QCOMPARE(no.dateTimeFormat(QLocale::LongFormat), "dddd d. MMMM yyyy HH:mm:ss tttt"_L1); + + QT_TEST_EQUALITY_OPS(c, no, false); } void tst_QLocale::monthName() @@ -2504,18 +3406,21 @@ void tst_QLocale::monthName() // 'de' locale doesn't have narrow month name QCOMPARE(de.monthName(12, QLocale::NarrowFormat), QLatin1String("D")); - QLocale ru("ru_RU"); + const QLocale ru("ru_RU"); QCOMPARE(ru.monthName(1, QLocale::LongFormat), QString::fromUtf8("\321\217\320\275\320\262\320\260\321\200\321\217")); QCOMPARE(ru.monthName(1, QLocale::ShortFormat), QString::fromUtf8("\321\217\320\275\320\262\56")); QCOMPARE(ru.monthName(1, QLocale::NarrowFormat), QString::fromUtf8("\320\257")); + const auto sys = QLocale::system(); + if (sys.language() == QLocale::Russian) // QTBUG-92018 + QCOMPARE_NE(sys.monthName(3), sys.standaloneMonthName(3)); - QLocale ir("ga_IE"); + const QLocale ir("ga_IE"); QCOMPARE(ir.monthName(1, QLocale::ShortFormat), QLatin1String("Ean")); QCOMPARE(ir.monthName(12, QLocale::ShortFormat), QLatin1String("Noll")); - QLocale cz("cs_CZ"); + const QLocale cz("cs_CZ"); QCOMPARE(cz.monthName(1, QLocale::ShortFormat), QLatin1String("led")); QCOMPARE(cz.monthName(12, QLocale::ShortFormat), QLatin1String("pro")); } @@ -2550,6 +3455,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); @@ -2563,12 +3602,12 @@ void tst_QLocale::currency() const QLocale en_US("en_US"); QCOMPARE(en_US.toCurrencyString(qulonglong(1234)), QString("$1,234")); - QCOMPARE(en_US.toCurrencyString(qlonglong(-1234)), QString("$-1,234")); + QCOMPARE(en_US.toCurrencyString(qlonglong(-1234)), QString("($1,234)")); QCOMPARE(en_US.toCurrencyString(double(1234.56)), QString("$1,234.56")); - QCOMPARE(en_US.toCurrencyString(double(-1234.56)), QString("$-1,234.56")); - QCOMPARE(en_US.toCurrencyString(double(-1234.5678)), QString("$-1,234.57")); - QCOMPARE(en_US.toCurrencyString(double(-1234.5678), NULL, 4), QString("$-1,234.5678")); - QCOMPARE(en_US.toCurrencyString(double(-1234.56), NULL, 4), QString("$-1,234.5600")); + QCOMPARE(en_US.toCurrencyString(double(-1234.56)), QString("($1,234.56)")); + QCOMPARE(en_US.toCurrencyString(double(-1234.5678)), QString("($1,234.57)")); + QCOMPARE(en_US.toCurrencyString(double(-1234.5678), NULL, 4), QString("($1,234.5678)")); + QCOMPARE(en_US.toCurrencyString(double(-1234.56), NULL, 4), QString("($1,234.5600)")); const QLocale ru_RU("ru_RU"); QCOMPARE(ru_RU.toCurrencyString(qulonglong(1234)), @@ -2599,9 +3638,12 @@ void tst_QLocale::currency() const QLocale es_CR(QLocale::Spanish, QLocale::CostaRica); QCOMPARE(es_CR.toCurrencyString(double(1565.25)), QString::fromUtf8("\xE2\x82\xA1" "1\xC2\xA0" "565,25")); + QCOMPARE(es_CR.toCurrencyString(double(12565.25)), + QString::fromUtf8("\xE2\x82\xA1" "12\xC2\xA0" "565,25")); const QLocale system = QLocale::system(); QVERIFY(system.toCurrencyString(1, QLatin1String("FOO")).contains(QLatin1String("FOO"))); + QT_TEST_EQUALITY_OPS(system, es_CR, false); } void tst_QLocale::quoteString() @@ -2616,47 +3658,100 @@ void tst_QLocale::quoteString() QCOMPARE(de_CH.quoteString(someText), QString::fromUtf8("\xe2\x80\x9e" "text" "\xe2\x80\x9c")); QCOMPARE(de_CH.quoteString(someText, QLocale::AlternateQuotation), QString::fromUtf8("\xe2\x80\x9a" "text" "\xe2\x80\x98")); - + QT_TEST_EQUALITY_OPS(de_CH, c, false); } -void tst_QLocale::uiLanguages() +void tst_QLocale::uiLanguages_data() { - const QLocale c(QLocale::C); - QCOMPARE(c.uiLanguages().size(), 1); - QCOMPARE(c.uiLanguages().at(0), QLatin1String("C")); + QTest::addColumn<QLocale>("locale"); + QTest::addColumn<QStringList>("all"); - const QLocale en_US("en_US"); - QCOMPARE(en_US.uiLanguages().size(), 3); - QCOMPARE(en_US.uiLanguages().at(0), QLatin1String("en")); - QCOMPARE(en_US.uiLanguages().at(1), QLatin1String("en-US")); - QCOMPARE(en_US.uiLanguages().at(2), QLatin1String("en-Latn-US")); - - const QLocale en_Latn_US("en_Latn_US"); - QCOMPARE(en_Latn_US.uiLanguages().size(), 3); - QCOMPARE(en_Latn_US.uiLanguages().at(0), QLatin1String("en")); - QCOMPARE(en_Latn_US.uiLanguages().at(1), QLatin1String("en-US")); - QCOMPARE(en_Latn_US.uiLanguages().at(2), QLatin1String("en-Latn-US")); - - const QLocale en_GB("en_GB"); - QCOMPARE(en_GB.uiLanguages().size(), 2); - QCOMPARE(en_GB.uiLanguages().at(0), QLatin1String("en-GB")); - QCOMPARE(en_GB.uiLanguages().at(1), QLatin1String("en-Latn-GB")); - - const QLocale en_Dsrt_US("en_Dsrt_US"); - QCOMPARE(en_Dsrt_US.uiLanguages().size(), 2); - QCOMPARE(en_Dsrt_US.uiLanguages().at(0), QLatin1String("en-Dsrt")); - QCOMPARE(en_Dsrt_US.uiLanguages().at(1), QLatin1String("en-Dsrt-US")); + QTest::newRow("C") << QLocale::c() << QStringList{QString("C")}; - const QLocale ru_RU("ru_RU"); - QCOMPARE(ru_RU.uiLanguages().size(), 3); - QCOMPARE(ru_RU.uiLanguages().at(0), QLatin1String("ru")); - QCOMPARE(ru_RU.uiLanguages().at(1), QLatin1String("ru-RU")); - QCOMPARE(ru_RU.uiLanguages().at(2), QLatin1String("ru-Cyrl-RU")); + QTest::newRow("en_US") + << QLocale("en_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-Latn-US"), QString("en-US"), QString("en")}; + + QTest::newRow("en_GB") + << QLocale("en_GB") + << QStringList{QString("en-Latn-GB"), QString("en-GB")}; + + QTest::newRow("en_Dsrt_US") + << QLocale("en_Dsrt_US") + << QStringList{QString("en-Dsrt-US"), QString("en-Dsrt")}; + + QTest::newRow("ru_RU") + << QLocale("ru_RU") + << QStringList{QString("ru-Cyrl-RU"), QString("ru-RU"), QString("ru")}; + + QTest::newRow("zh_Hant") + << QLocale("zh_Hant") + << QStringList{QString("zh-Hant-TW"), QString("zh-TW")}; + + QTest::newRow("zh_Hans_CN") + << QLocale(QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China) + << 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); + const auto expected = [all](QChar sep) { + QStringList adjusted; + for (QString name : all) + adjusted << name.replace(u'-', sep); + return adjusted; + }; - const QLocale zh_Hant("zh_Hant"); - QCOMPARE(zh_Hant.uiLanguages().size(), 2); - QCOMPARE(zh_Hant.uiLanguages().at(0), QLatin1String("zh-TW")); - QCOMPARE(zh_Hant.uiLanguages().at(1), QLatin1String("zh-Hant-TW")); + { + // 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() @@ -2717,8 +3812,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() @@ -2749,12 +3843,11 @@ void tst_QLocale::textDirection_data() switch (language) { // based on likelySubtags for RTL scripts case QLocale::AncientGreek: - case QLocale::AncientNorthArabian: case QLocale::Arabic: case QLocale::Aramaic: case QLocale::Avestan: + case QLocale::Baluchi: case QLocale::CentralKurdish: - case QLocale::ClassicalMandaic: case QLocale::Divehi: // case QLocale::Fulah: // case QLocale::Hausa: @@ -2762,26 +3855,19 @@ void tst_QLocale::textDirection_data() // case QLocale::Hungarian: case QLocale::Kashmiri: // case QLocale::Kurdish: - case QLocale::Lydian: case QLocale::Mandingo: - case QLocale::ManichaeanMiddlePersian: case QLocale::Mazanderani: case QLocale::Mende: - case QLocale::Meroitic: case QLocale::Nko: case QLocale::NorthernLuri: - case QLocale::OldTurkish: case QLocale::Pahlavi: - case QLocale::Parthian: case QLocale::Pashto: case QLocale::Persian: case QLocale::Phoenician: - case QLocale::PrakritLanguage: - case QLocale::Sabaean: - case QLocale::Samaritan: case QLocale::Sindhi: case QLocale::SouthernKurdish: case QLocale::Syriac: + case QLocale::Torwali: case QLocale::Uighur: case QLocale::Urdu: case QLocale::WesternBalochi: @@ -2793,7 +3879,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; @@ -2804,10 +3890,9 @@ void tst_QLocale::textDirection() { QFETCH(int, language); QFETCH(int, script); - QFETCH(bool, rightToLeft); - QLocale locale(QLocale::Language(language), QLocale::Script(script), QLocale::AnyCountry); - QCOMPARE(locale.textDirection() == Qt::RightToLeft, rightToLeft); + QLocale locale(QLocale::Language(language), QLocale::Script(script), QLocale::AnyTerritory); + QTEST(locale.textDirection() == Qt::RightToLeft, "rightToLeft"); } void tst_QLocale::formattedDataSize_data() @@ -2912,8 +3997,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() @@ -2941,108 +4026,605 @@ 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()); + QT_TEST_EQUALITY_OPS(locale, QLocale(QLatin1String(QTest::currentDataTag())), true); } +#ifndef QT_NO_SYSTEMLOCALE +# ifdef QT_BUILD_INTERNAL class MySystemLocale : public QSystemLocale { + Q_DISABLE_COPY_MOVE(MySystemLocale) public: - MySystemLocale(const QLocale &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 { + 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 fallbackUiLocale() const override + QLocale fallbackLocale() const override { return m_locale; } 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"); QTest::addColumn<QLocale::Language>("language"); - QTest::addRow("catalan") << QString("ca") << QLocale::Catalan; - QTest::addRow("ukrainian") << QString("uk") << QLocale::Ukrainian; - QTest::addRow("german") << QString("de") << QLocale::German; + QTest::addColumn<QStringList>("uiLanguages"); + + QTest::addRow("catalan") + << QString("ca") << QLocale::Catalan + << 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-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-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-Hans-CN"), QStringLiteral("zh-CN")}; + QTest::addRow("chinese-full") + << QString("zh-Hans-CN") << QLocale::Chinese + << 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(); QFETCH(QString, name); QFETCH(QLocale::Language, language); + QFETCH(QStringList, uiLanguages); { 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); + QCOMPARE(QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore), + uiLanguages.replaceInStrings(u"-", u"_")); + reporter.dismiss(); } - QCOMPARE(QLocale(), originalLocale); - QCOMPARE(QLocale::system(), originalSystemLocale); + // Verify MySystemLocale tidy-up restored prior state: + QT_TEST_EQUALITY_OPS(QLocale(), originalLocale, true); + QT_TEST_EQUALITY_OPS(QLocale::system(), originalSystemLocale, true); } +# endif // QT_BUILD_INTERNAL -void tst_QLocale::IndianNumberGrouping() +void tst_QLocale::systemLocaleDayAndMonthNames_data() { - QLocale indian(QLocale::Hindi, QLocale::India); + QTest::addColumn<QByteArray>("locale"); + QTest::addColumn<QDate>("date"); + QTest::addColumn<QLocale::FormatType>("format"); + QTest::addColumn<QString>("month"); + QTest::addColumn<QString>("standaloneMonth"); + QTest::addColumn<QString>("day"); + QTest::addColumn<QString>("standaloneDay"); + + // en_US and de_DE locale outputs for ICU and macOS are similar. + // ru_RU are different. + // Windows has its own representation for all of the locales + +#if QT_CONFIG(icu) + // августа, август, понедельник, понедельник + QTest::newRow("ru_RU 30.08.2021 long") + << QByteArray("ru_RU") << QDate(2021, 8, 30) << QLocale::LongFormat + << QString("\u0430\u0432\u0433\u0443\u0441\u0442\u0430") + << QString("\u0430\u0432\u0433\u0443\u0441\u0442") + << QString("\u043f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a") + << QString("\u043f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a"); + // авг., авг., пн, пн + QTest::newRow("ru_RU 30.08.2021 short") + << 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("\u041f") + << QString("\u041f"); +#elif defined(Q_OS_DARWIN) + // августа, август, понедельник, понедельник + QTest::newRow("ru_RU 30.08.2021 long") + << QByteArray("ru_RU") << QDate(2021, 8, 30) << QLocale::LongFormat + << QString("\u0430\u0432\u0433\u0443\u0441\u0442\u0430") + << QString("\u0430\u0432\u0433\u0443\u0441\u0442") + << QString("\u043f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a") + << QString("\u043f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a"); + // авг., авг., Пн, Пн + QTest::newRow("ru_RU 30.08.2021 short") + << QByteArray("ru_RU") << QDate(2021, 8, 30) << QLocale::ShortFormat + << QString("\u0430\u0432\u0433.") << QString("\u0430\u0432\u0433.") + << QString("\u041f\u043d") << QString("\u041f\u043d"); + // А, А, Пн, П + QTest::newRow("ru_RU 30.08.2021 narrow") + << QByteArray("ru_RU") << QDate(2021, 8, 30) << QLocale::NarrowFormat + << QString("\u0410") << QString("\u0410") << QString("\u041f\u043d") + << QString("\u041f"); +#endif - qint8 int8 = 100; - QString strResult8("100"); - QCOMPARE(indian.toString(int8), strResult8); - QCOMPARE(indian.toShort(strResult8), short(int8)); +#if QT_CONFIG(icu) || defined(Q_OS_DARWIN) + QTest::newRow("en_US 30.08.2021 long") + << QByteArray("en_US") << QDate(2021, 8, 30) << QLocale::LongFormat + << "August" << "August" << "Monday" << "Monday"; + QTest::newRow("en_US 30.08.2021 short") + << QByteArray("en_US") << QDate(2021, 8, 30) << QLocale::ShortFormat + << "Aug" << "Aug" << "Mon" << "Mon"; + QTest::newRow("en_US 30.08.2021 narrow") + << QByteArray("en_US") << QDate(2021, 8, 30) << QLocale::NarrowFormat + << "A" << "A" << "M" << "M"; + + QTest::newRow("de_DE 30.08.2021 long") + << QByteArray("de_DE") << QDate(2021, 8, 30) << QLocale::LongFormat + << "August" << "August" << "Montag" << "Montag"; + QTest::newRow("de_DE 30.08.2021 short") + << QByteArray("de_DE") << QDate(2021, 8, 30) << QLocale::ShortFormat + << "Aug." << "Aug" << "Mo." << "Mo"; + QTest::newRow("de_DE 30.08.2021 narrow") + << QByteArray("de_DE") << QDate(2021, 8, 30) << QLocale::NarrowFormat + << "A" << "A" << "M" << "M"; +#elif defined(Q_OS_WIN) + // августа, Август, понедельник, понедельник + QTest::newRow("ru_RU 30.08.2021 long") + << QByteArray("ru_RU") << QDate(2021, 8, 30) << QLocale::LongFormat + << QString("\u0430\u0432\u0433\u0443\u0441\u0442\u0430") + << QString("\u0410\u0432\u0433\u0443\u0441\u0442") + << QString("\u043f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a") + << QString("\u043f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a"); + // авг, авг, Пн, пн + QTest::newRow("ru_RU 30.08.2021 short") + << QByteArray("ru_RU") << QDate(2021, 8, 30) << QLocale::ShortFormat + << QString("\u0430\u0432\u0433") << QString("\u0430\u0432\u0433") + << QString("\u041f\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("\u041f\u043d") + << QString("\u041f"); + + QTest::newRow("en_US 30.08.2021 long") + << QByteArray("en_US") << QDate(2021, 8, 30) << QLocale::LongFormat + << "August" << "August" << "Monday" << "Monday"; + QTest::newRow("en_US 30.08.2021 short") + << QByteArray("en_US") << QDate(2021, 8, 30) << QLocale::ShortFormat + << "Aug" << "Aug" << "Mon" << "Mon"; + QTest::newRow("en_US 30.08.2021 narrow") + << QByteArray("en_US") << QDate(2021, 8, 30) << QLocale::NarrowFormat + << "A" << "A" << "Mo" << "M"; + + QTest::newRow("de_DE 30.08.2021 long") + << QByteArray("de_DE") << QDate(2021, 8, 30) << QLocale::LongFormat + << "August" << "August" << "Montag" << "Montag"; + QTest::newRow("de_DE 30.08.2021 short") + << QByteArray("de_DE") << QDate(2021, 8, 30) << QLocale::ShortFormat + << "Aug" << "Aug" << "Mo" << "Mo"; + QTest::newRow("de_DE 30.08.2021 narrow") + << QByteArray("de_DE") << QDate(2021, 8, 30) << QLocale::NarrowFormat + << "A" << "A" << "Mo" << "M"; +#else + QSKIP("This test can't run on this OS"); +#endif +} - quint8 uint8 = 100; - QCOMPARE(indian.toString(uint8), strResult8); - QCOMPARE(indian.toShort(strResult8), short(uint8)); +void tst_QLocale::systemLocaleDayAndMonthNames() +{ + QFETCH(QByteArray, locale); + QFETCH(QDate, date); + QFETCH(QLocale::FormatType, format); + locale += ".UTF-8"; // So we don't have to repeat it on every data row ! + + const TransientLocale tested(LC_ALL, locale.constData()); + + QLocale sys = QLocale::system(); +#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().toUtf8())) + QSKIP(("Set locale to " + locale + " manually to run this test.").constData()); +#endif + + const int m = date.month(); + QTEST(sys.monthName(m, format), "month"); + QTEST(sys.standaloneMonthName(m, format), "standaloneMonth"); + + const int d = date.dayOfWeek(); + QTEST(sys.dayName(d, format), "day"); + QTEST(sys.standaloneDayName(d, format), "standaloneDay"); +} - // Boundary case 1000 for short and ushort - short shortInt = 1000; - QString strResult16("1,000"); - QCOMPARE(indian.toString(shortInt), strResult16); - QCOMPARE(indian.toShort(strResult16), shortInt); +#endif // QT_NO_SYSTEMLOCALE - ushort uShortInt = 1000; - QCOMPARE(indian.toString(uShortInt), strResult16); - QCOMPARE(indian.toUShort(strResult16), uShortInt); +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; +} - shortInt = 10000; - strResult16 = "10,000"; - QCOMPARE(indian.toString(shortInt), strResult16); - QCOMPARE(indian.toShort(strResult16), shortInt); +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); + } + } +} - uShortInt = 10000; - QCOMPARE(indian.toString(uShortInt), strResult16); - QCOMPARE(indian.toUShort(strResult16), uShortInt); +void tst_QLocale::numberGroupingIndia() +{ + const QLocale indian(QLocale::Hindi, QLocale::India); - int intInt = 1000000000; - QString strResult32("1,00,00,00,000"); - QCOMPARE(indian.toString(intInt), strResult32); - QCOMPARE(indian.toInt(strResult32), intInt); + // A 7-bit value (fits in signed 8-bit): + const QString strResult8("120"); + const qint8 int8 = 120; + QCOMPARE(indian.toString(int8), strResult8); + QCOMPARE(indian.toShort(strResult8), short(int8)); - uint uIntInt = 1000000000; - QCOMPARE(indian.toString(uIntInt), strResult32); - QCOMPARE(indian.toUInt(strResult32), uIntInt); + const quint8 uint8 = 120u; + QCOMPARE(indian.toString(uint8), strResult8); + QCOMPARE(indian.toShort(strResult8), short(uint8)); - QString strResult64("10,00,00,00,00,00,00,00,000"); - qint64 int64 = Q_INT64_C(1000000000000000000); + // Boundary case for needing a first separator: + const QString strResultSep("3,210"); + const short shortSep = 3210; + QCOMPARE(indian.toString(shortSep), strResultSep); + QCOMPARE(indian.toShort(strResultSep), shortSep); + + const ushort uShortSep = 3210u; + QCOMPARE(indian.toString(uShortSep), strResultSep); + QCOMPARE(indian.toUShort(strResultSep), uShortSep); + + // 15-bit: + const QString strResult16("24,310"); + const short short16 = 24310; + QCOMPARE(indian.toString(short16), strResult16); + QCOMPARE(indian.toShort(strResult16), short16); + + const ushort uShort16 = 24310u; + QCOMPARE(indian.toString(uShort16), strResult16); + QCOMPARE(indian.toUShort(strResult16), uShort16); + + // 31-bit + const QString strResult32("2,03,04,05,010"); + const int integer32 = 2030405010; + QCOMPARE(indian.toString(integer32), strResult32); + QCOMPARE(indian.toInt(strResult32), integer32); + + const uint uInteger32 = 2030405010u; + QCOMPARE(indian.toString(uInteger32), strResult32); + QCOMPARE(indian.toUInt(strResult32), uInteger32); + + // 63-bit: + const QString strResult64("60,05,00,40,03,00,20,01,000"); + const qint64 int64 = Q_INT64_C(6005004003002001000); QCOMPARE(indian.toString(int64), strResult64); QCOMPARE(indian.toLongLong(strResult64), int64); - quint64 uint64 = Q_UINT64_C(1000000000000000000); + const quint64 uint64 = Q_UINT64_C(6005004003002001000); QCOMPARE(indian.toString(uint64), strResult64); QCOMPARE(indian.toULongLong(strResult64), uint64); } +void tst_QLocale::numberFormatChakma() +{ + const QLocale chakma(QLocale::Chakma, QLocale::ChakmaScript, QLocale::Bangladesh); + const uint zeroVal = 0x11136; // Unicode's representation of Chakma zero + const QChar data[] = { + QChar::highSurrogate(zeroVal), QChar::lowSurrogate(zeroVal), + QChar::highSurrogate(zeroVal + 1), QChar::lowSurrogate(zeroVal + 1), + QChar::highSurrogate(zeroVal + 2), QChar::lowSurrogate(zeroVal + 2), + QChar::highSurrogate(zeroVal + 3), QChar::lowSurrogate(zeroVal + 3), + QChar::highSurrogate(zeroVal + 4), QChar::lowSurrogate(zeroVal + 4), + QChar::highSurrogate(zeroVal + 5), QChar::lowSurrogate(zeroVal + 5), + QChar::highSurrogate(zeroVal + 6), QChar::lowSurrogate(zeroVal + 6), + }; + const QChar separator(QLatin1Char(',')); + const QString + zero = QString::fromRawData(data, 2), + one = QString::fromRawData(data + 2, 2), + two = QString::fromRawData(data + 4, 2), + three = QString::fromRawData(data + 6, 2), + four = QString::fromRawData(data + 8, 2), + five = QString::fromRawData(data + 10, 2), + six = QString::fromRawData(data + 12, 2); + + // A 7-bit value (fits in signed 8-bit): + const QString strResult8 = one + two + zero; + const qint8 int8 = 120; + QCOMPARE(chakma.toString(int8), strResult8); + QCOMPARE(chakma.toShort(strResult8), short(int8)); + + const quint8 uint8 = 120; + QCOMPARE(chakma.toString(uint8), strResult8); + QCOMPARE(chakma.toShort(strResult8), short(uint8)); + + // Boundary case for needing a first separator: + const QString strResultSep = three + separator + two + one + zero; + const short shortSep = 3210; + QCOMPARE(chakma.toString(shortSep), strResultSep); + QCOMPARE(chakma.toShort(strResultSep), shortSep); + + const ushort uShortSep = 3210u; + QCOMPARE(chakma.toString(uShortSep), strResultSep); + QCOMPARE(chakma.toUShort(strResultSep), uShortSep); + + // Fifteen-bit value: + const QString strResult16 = two + four + separator + three + one + zero; + const short short16 = 24310; + QCOMPARE(chakma.toString(short16), strResult16); + QCOMPARE(chakma.toShort(strResult16), short16); + + const ushort uShort16 = 24310u; + QCOMPARE(chakma.toString(uShort16), strResult16); + QCOMPARE(chakma.toUShort(strResult16), uShort16); + + // 31-bit + const QString strResult32 = + two + separator + zero + three + separator + zero + four + + separator + zero + five + separator + zero + one + zero; + const int integer32 = 2030405010; + QCOMPARE(chakma.toString(integer32), strResult32); + QCOMPARE(chakma.toInt(strResult32), integer32); + + const uint uInteger32 = 2030405010u; + QCOMPARE(chakma.toString(uInteger32), strResult32); + QCOMPARE(chakma.toUInt(strResult32), uInteger32); + + // 63-bit: + const QString strResult64 = + six + zero + separator + zero + five + separator + zero + zero + separator + + four + zero + separator + zero + three + separator + zero + zero + separator + + two + zero + separator + zero + one + separator + zero + zero + zero; + const qint64 int64 = Q_INT64_C(6005004003002001000); + QCOMPARE(chakma.toString(int64), strResult64); + QCOMPARE(chakma.toLongLong(strResult64), int64); + + const quint64 uint64 = Q_UINT64_C(6005004003002001000); + QCOMPARE(chakma.toString(uint64), strResult64); + QCOMPARE(chakma.toULongLong(strResult64), uint64); +} + +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")); + QCOMPARE(QLocale::territoryToCode(QLocale::EuropeanUnion), QString("EU")); + + QCOMPARE(QLocale::scriptToCode(QLocale::AnyScript), QString()); + QCOMPARE(QLocale::scriptToCode(QLocale::SimplifiedHanScript), QString("Hans")); +} + +void tst_QLocale::codeToLcs() +{ + QCOMPARE(QLocale::codeToLanguage(QString()), QLocale::AnyLanguage); + QCOMPARE(QLocale::codeToLanguage(QString(" ")), QLocale::AnyLanguage); + QCOMPARE(QLocale::codeToLanguage(QString("und")), QLocale::AnyLanguage); + 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::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); + QCOMPARE(QLocale::codeToTerritory(QString("US")), QLocale::UnitedStates); + QCOMPARE(QLocale::codeToTerritory(QString("us")), QLocale::UnitedStates); + QCOMPARE(QLocale::codeToTerritory(QString("USA")), QLocale::AnyTerritory); + QCOMPARE(QLocale::codeToTerritory(QString("EU")), QLocale::EuropeanUnion); + QCOMPARE(QLocale::codeToTerritory(QString("001")), QLocale::World); + QCOMPARE(QLocale::codeToTerritory(QString("150")), QLocale::Europe); + + QCOMPARE(QLocale::codeToScript(QString()), QLocale::AnyScript); + QCOMPARE(QLocale::codeToScript(QString("Zzzz")), QLocale::AnyScript); + QCOMPARE(QLocale::codeToScript(QString("Hans")), QLocale::SimplifiedHanScript); +} + QTEST_MAIN(tst_QLocale) #include "tst_qlocale.moc" |