File indexing completed on 2024-04-28 07:42:12

0001 /*
0002     This file is part of the KDE Frameworks
0003 
0004     SPDX-FileCopyrightText: 2013 John Layt <jlayt@kde.org>
0005     SPDX-FileCopyrightText: 2010 Michael Leupold <lemma@confuego.org>
0006     SPDX-FileCopyrightText: 2009 Michael Pyne <mpyne@kde.org>
0007     SPDX-FileCopyrightText: 2008 Albert Astals Cid <aacid@kde.org>
0008 
0009     SPDX-License-Identifier: LGPL-2.0-or-later
0010 */
0011 
0012 #include "kformattest.h"
0013 
0014 #include <QTest>
0015 #include <QTimeZone>
0016 
0017 #include "kformat.h"
0018 
0019 void setupEnvironment()
0020 {
0021 #ifndef Q_OS_WIN
0022     // ignore translations
0023     qputenv("XDG_DATA_DIRS", "does-not-exist");
0024 #endif
0025 }
0026 Q_CONSTRUCTOR_FUNCTION(setupEnvironment)
0027 
0028 void KFormatTest::formatByteSize()
0029 {
0030     QLocale locale(QLocale::c());
0031     locale.setNumberOptions(QLocale::DefaultNumberOptions); // Qt >= 5.6 sets QLocale::OmitGroupSeparator for the C locale
0032     KFormat format(locale);
0033 
0034     QCOMPARE(format.formatByteSize(0), QStringLiteral("0 B"));
0035     QCOMPARE(format.formatByteSize(50), QStringLiteral("50 B"));
0036     QCOMPARE(format.formatByteSize(500), QStringLiteral("500 B"));
0037     QCOMPARE(format.formatByteSize(5000), QStringLiteral("4.9 KiB"));
0038     QCOMPARE(format.formatByteSize(50000), QStringLiteral("48.8 KiB"));
0039     QCOMPARE(format.formatByteSize(500000), QStringLiteral("488.3 KiB"));
0040     QCOMPARE(format.formatByteSize(5000000), QStringLiteral("4.8 MiB"));
0041     QCOMPARE(format.formatByteSize(50000000), QStringLiteral("47.7 MiB"));
0042     QCOMPARE(format.formatByteSize(500000000), QStringLiteral("476.8 MiB"));
0043 #if (defined(__WORDSIZE) && (__WORDSIZE == 64)) || defined(_LP64) || defined(__LP64__) || defined(__ILP64__)
0044     QCOMPARE(format.formatByteSize(5000000000), QStringLiteral("4.7 GiB"));
0045     QCOMPARE(format.formatByteSize(50000000000), QStringLiteral("46.6 GiB"));
0046     QCOMPARE(format.formatByteSize(500000000000), QStringLiteral("465.7 GiB"));
0047     QCOMPARE(format.formatByteSize(5000000000000), QStringLiteral("4.5 TiB"));
0048     QCOMPARE(format.formatByteSize(50000000000000), QStringLiteral("45.5 TiB"));
0049     QCOMPARE(format.formatByteSize(500000000000000), QStringLiteral("454.7 TiB"));
0050 #endif
0051 
0052     QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1.0 KiB"));
0053     QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1,023 B"));
0054     QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1.1 MiB")); // 1.2 metric
0055 
0056     QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1.0 KiB"));
0057     QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1,023 B"));
0058     QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1.1 MiB")); // 1.2 metric
0059 
0060     QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1.0 KB"));
0061     QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1,023 B"));
0062     QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1.1 MB"));
0063 
0064     QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1.0 KB"));
0065     QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1,023 B"));
0066     QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1.1 MB"));
0067 
0068     QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
0069     QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
0070     QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.2 MB"));
0071 
0072     QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.0 kB"));
0073     QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.0 kB"));
0074     QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.2 MB"));
0075 
0076     // Ensure all units are represented
0077     QCOMPARE(format.formatByteSize(2.0e9, 1, KFormat::MetricBinaryDialect), QStringLiteral("2.0 GB"));
0078     QCOMPARE(format.formatByteSize(3.2e12, 1, KFormat::MetricBinaryDialect), QStringLiteral("3.2 TB"));
0079     QCOMPARE(format.formatByteSize(4.1e15, 1, KFormat::MetricBinaryDialect), QStringLiteral("4.1 PB"));
0080     QCOMPARE(format.formatByteSize(6.7e18, 2, KFormat::MetricBinaryDialect), QStringLiteral("6.70 EB"));
0081     QCOMPARE(format.formatByteSize(5.6e20, 2, KFormat::MetricBinaryDialect), QStringLiteral("560.00 EB"));
0082     QCOMPARE(format.formatByteSize(2.3e22, 2, KFormat::MetricBinaryDialect), QStringLiteral("23.00 ZB"));
0083     QCOMPARE(format.formatByteSize(1.0e27, 1, KFormat::MetricBinaryDialect), QStringLiteral("1,000.0 YB"));
0084 
0085     // Spattering of specific units
0086     QCOMPARE(format.formatByteSize(823000, 3, KFormat::IECBinaryDialect, KFormat::UnitMegaByte), QStringLiteral("0.785 MiB"));
0087     QCOMPARE(format.formatByteSize(1234034.0, 4, KFormat::JEDECBinaryDialect, KFormat::UnitByte), QStringLiteral("1,234,034 B"));
0088 
0089     // Check examples from the documentation
0090     QCOMPARE(format.formatByteSize(1000, 1, KFormat::MetricBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 kB"));
0091     QCOMPARE(format.formatByteSize(1000, 1, KFormat::IECBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 KiB"));
0092     QCOMPARE(format.formatByteSize(1000, 1, KFormat::JEDECBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 KB"));
0093 }
0094 
0095 void KFormatTest::formatValue()
0096 {
0097     QLocale locale(QLocale::c());
0098     locale.setNumberOptions(QLocale::DefaultNumberOptions); // Qt >= 5.6 sets QLocale::OmitGroupSeparator for the C locale
0099     KFormat format(locale);
0100 
0101     // Check examples from the documentation
0102     QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
0103     QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::IECBinaryDialect), QStringLiteral("1.0 KiB"));
0104     QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::JEDECBinaryDialect), QStringLiteral("1.0 KB"));
0105 
0106     // Check examples from the documentation
0107     QCOMPARE(format.formatValue(1000, KFormat::Unit::Bit, 1, KFormat::UnitPrefix::Kilo, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kbit"));
0108     QCOMPARE(format.formatValue(1000, QStringLiteral("bit"), 1, KFormat::UnitPrefix::Kilo), QStringLiteral("1.0 kbit"));
0109     QCOMPARE(format.formatValue(1000, QStringLiteral("bit/s"), 1, KFormat::UnitPrefix::Kilo), QStringLiteral("1.0 kbit/s"));
0110 
0111     QCOMPARE(format.formatValue(100, QStringLiteral("bit/s")), QStringLiteral("100.0 bit/s"));
0112     QCOMPARE(format.formatValue(1000, QStringLiteral("bit/s")), QStringLiteral("1.0 kbit/s"));
0113     QCOMPARE(format.formatValue(10e3, QStringLiteral("bit/s")), QStringLiteral("10.0 kbit/s"));
0114     QCOMPARE(format.formatValue(10e6, QStringLiteral("bit/s")), QStringLiteral("10.0 Mbit/s"));
0115 
0116     QCOMPARE(format.formatValue(0.010, KFormat::Unit::Meter, 1, KFormat::UnitPrefix::Milli, KFormat::MetricBinaryDialect), QStringLiteral("10.0 mm"));
0117     QCOMPARE(format.formatValue(10.12e-6, KFormat::Unit::Meter, 2, KFormat::UnitPrefix::Micro, KFormat::MetricBinaryDialect), QString::fromUtf8("10.12 µm"));
0118     QCOMPARE(format.formatValue(10.55e-6, KFormat::Unit::Meter, 1, KFormat::UnitPrefix::AutoAdjust, KFormat::MetricBinaryDialect),
0119              QString::fromUtf8("10.6 µm"));
0120 }
0121 
0122 enum TimeConstants {
0123     MSecsInDay = 86400000,
0124     MSecsInHour = 3600000,
0125     MSecsInMinute = 60000,
0126     MSecsInSecond = 1000,
0127 };
0128 
0129 void KFormatTest::formatDuration()
0130 {
0131     KFormat format(QLocale::c());
0132 
0133     quint64 singleSecond = 3 * MSecsInSecond + 700;
0134     quint64 doubleSecond = 33 * MSecsInSecond + 700;
0135     quint64 singleMinute = 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
0136     quint64 doubleMinute = 38 * MSecsInMinute + 3 * MSecsInSecond + 700;
0137     quint64 singleHour = 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
0138     quint64 doubleHour = 15 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
0139     quint64 singleDay = 1 * MSecsInDay + 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
0140     quint64 doubleDay = 10 * MSecsInDay + 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
0141     quint64 roundingIssues = 2 * MSecsInHour + 59 * MSecsInMinute + 59 * MSecsInSecond + 900;
0142     quint64 largeValue = 9999999999;
0143 
0144     // Default format
0145     QCOMPARE(format.formatDuration(singleSecond), QStringLiteral("0:00:04"));
0146     QCOMPARE(format.formatDuration(doubleSecond), QStringLiteral("0:00:34"));
0147     QCOMPARE(format.formatDuration(singleMinute), QStringLiteral("0:08:04"));
0148     QCOMPARE(format.formatDuration(doubleMinute), QStringLiteral("0:38:04"));
0149     QCOMPARE(format.formatDuration(singleHour), QStringLiteral("5:08:04"));
0150     QCOMPARE(format.formatDuration(doubleHour), QStringLiteral("15:08:04"));
0151     QCOMPARE(format.formatDuration(singleDay), QStringLiteral("29:08:04"));
0152     QCOMPARE(format.formatDuration(doubleDay), QStringLiteral("245:08:04"));
0153     QCOMPARE(format.formatDuration(roundingIssues), QStringLiteral("3:00:00"));
0154     QCOMPARE(format.formatDuration(largeValue), QStringLiteral("2777:46:40"));
0155 
0156     // ShowMilliseconds format
0157     KFormat::DurationFormatOptions options = KFormat::ShowMilliseconds;
0158     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:00:03.700"));
0159     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:00:33.700"));
0160     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0:08:03.700"));
0161     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0:38:03.700"));
0162     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5:08:03.700"));
0163     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15:08:03.700"));
0164     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29:08:03.700"));
0165     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245:08:03.700"));
0166     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("2:59:59.900"));
0167     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777:46:39.999"));
0168 
0169     // HideSeconds format
0170     options = KFormat::HideSeconds;
0171     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:00"));
0172     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:01"));
0173     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0:08"));
0174     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0:38"));
0175     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5:08"));
0176     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15:08"));
0177     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29:08"));
0178     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245:08"));
0179     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3:00"));
0180     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777:47"));
0181 
0182     // FoldHours format
0183     options = KFormat::FoldHours;
0184     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:04"));
0185     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:34"));
0186     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8:04"));
0187     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38:04"));
0188     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308:04"));
0189     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908:04"));
0190     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748:04"));
0191     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708:04"));
0192     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("180:00"));
0193     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("166666:40"));
0194 
0195     // FoldHours ShowMilliseconds format
0196     options = KFormat::FoldHours;
0197     options = options | KFormat::ShowMilliseconds;
0198     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:03.700"));
0199     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:33.700"));
0200     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8:03.700"));
0201     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38:03.700"));
0202     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308:03.700"));
0203     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908:03.700"));
0204     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748:03.700"));
0205     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708:03.700"));
0206     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("179:59.900"));
0207     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("166666:39.999"));
0208 
0209     // InitialDuration format
0210     options = KFormat::InitialDuration;
0211     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m04s"));
0212     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h00m34s"));
0213     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m04s"));
0214     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m04s"));
0215     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m04s"));
0216     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m04s"));
0217     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m04s"));
0218     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m04s"));
0219     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3h00m00s"));
0220     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777h46m40s"));
0221 
0222     // InitialDuration and ShowMilliseconds format
0223     options = KFormat::InitialDuration;
0224     options = options | KFormat::ShowMilliseconds;
0225     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m03.700s"));
0226     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h00m33.700s"));
0227     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m03.700s"));
0228     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m03.700s"));
0229     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m03.700s"));
0230     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m03.700s"));
0231     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m03.700s"));
0232     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m03.700s"));
0233     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("2h59m59.900s"));
0234     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777h46m39.999s"));
0235 
0236     // InitialDuration and HideSeconds format
0237     options = KFormat::InitialDuration;
0238     options = options | KFormat::HideSeconds;
0239     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m"));
0240     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h01m"));
0241     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m"));
0242     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m"));
0243     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m"));
0244     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m"));
0245     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m"));
0246     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m"));
0247     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3h00m"));
0248     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777h47m"));
0249 
0250     // InitialDuration and FoldHours format
0251     options = KFormat::InitialDuration;
0252     options = options | KFormat::FoldHours;
0253     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0m04s"));
0254     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0m34s"));
0255     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8m04s"));
0256     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38m04s"));
0257     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308m04s"));
0258     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908m04s"));
0259     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748m04s"));
0260     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708m04s"));
0261     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("180m00s"));
0262     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("166666m40s"));
0263 
0264     // InitialDuration and FoldHours and ShowMilliseconds format
0265     options = KFormat::InitialDuration;
0266     options = options | KFormat::FoldHours | KFormat::ShowMilliseconds;
0267     QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0m03.700s"));
0268     QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0m33.700s"));
0269     QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8m03.700s"));
0270     QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38m03.700s"));
0271     QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308m03.700s"));
0272     QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908m03.700s"));
0273     QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748m03.700s"));
0274     QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708m03.700s"));
0275     QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("179m59.900s"));
0276     QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("166666m39.999s"));
0277 }
0278 
0279 void KFormatTest::formatDecimalDuration()
0280 {
0281     KFormat format(QLocale::c());
0282 
0283     QCOMPARE(format.formatDecimalDuration(10), QStringLiteral("10 millisecond(s)"));
0284     QCOMPARE(format.formatDecimalDuration(10, 3), QStringLiteral("10 millisecond(s)"));
0285     QCOMPARE(format.formatDecimalDuration(1 * MSecsInSecond + 10), QStringLiteral("1.01 seconds"));
0286     QCOMPARE(format.formatDecimalDuration(1 * MSecsInSecond + 1, 3), QStringLiteral("1.001 seconds"));
0287     QCOMPARE(format.formatDecimalDuration(1 * MSecsInMinute + 10 * MSecsInSecond), QStringLiteral("1.17 minutes"));
0288     QCOMPARE(format.formatDecimalDuration(1 * MSecsInMinute + 10 * MSecsInSecond, 3), QStringLiteral("1.167 minutes"));
0289     QCOMPARE(format.formatDecimalDuration(1 * MSecsInHour + 10 * MSecsInMinute), QStringLiteral("1.17 hours"));
0290     QCOMPARE(format.formatDecimalDuration(1 * MSecsInHour + 10 * MSecsInMinute, 3), QStringLiteral("1.167 hours"));
0291     QCOMPARE(format.formatDecimalDuration(1 * MSecsInDay + 10 * MSecsInHour), QStringLiteral("1.42 days"));
0292     QCOMPARE(format.formatDecimalDuration(1 * MSecsInDay + 10 * MSecsInHour, 3), QStringLiteral("1.417 days"));
0293 }
0294 
0295 void KFormatTest::formatSpelloutDuration()
0296 {
0297     KFormat format(QLocale::c());
0298 
0299     QCOMPARE(format.formatSpelloutDuration(1000), QStringLiteral("1 second(s)"));
0300     QCOMPARE(format.formatSpelloutDuration(5000), QStringLiteral("5 second(s)"));
0301     QCOMPARE(format.formatSpelloutDuration(60000), QStringLiteral("1 minute(s)"));
0302     QCOMPARE(format.formatSpelloutDuration(300000), QStringLiteral("5 minute(s)"));
0303     QCOMPARE(format.formatSpelloutDuration(3600000), QStringLiteral("1 hour(s)"));
0304     QCOMPARE(format.formatSpelloutDuration(18000000), QStringLiteral("5 hour(s)"));
0305     QCOMPARE(format.formatSpelloutDuration(75000), QStringLiteral("1 minute(s) and 15 second(s)"));
0306     // Problematic case #1 (there is a reference to this case on kformat.cpp)
0307     QCOMPARE(format.formatSpelloutDuration(119999), QStringLiteral("2 minute(s)"));
0308     // This case is strictly 2 hours, 15 minutes and 59 seconds. However, since the range is
0309     // pretty high between hours and seconds, formatSpelloutDuration always omits seconds when there
0310     // are hours in scene.
0311     QCOMPARE(format.formatSpelloutDuration(8159000), QStringLiteral("2 hour(s) and 15 minute(s)"));
0312     // This case is strictly 1 hour and 10 seconds. For the same reason, formatSpelloutDuration
0313     // detects that 10 seconds is just garbage compared to 1 hour, and omits it on the result.
0314     QCOMPARE(format.formatSpelloutDuration(3610000), QStringLiteral("1 hour(s)"));
0315 }
0316 
0317 void KFormatTest::formatRelativeDate()
0318 {
0319     KFormat format(QLocale::c());
0320 
0321     QDate testDate = QDate::currentDate();
0322 
0323     QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Today"));
0324     QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Today"));
0325     QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Today"));
0326 
0327     testDate = QDate::currentDate().addDays(1);
0328     QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Tomorrow"));
0329     QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Tomorrow"));
0330     QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Tomorrow"));
0331 
0332     testDate = QDate::currentDate().addDays(-1);
0333     QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Yesterday"));
0334     QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Yesterday"));
0335     QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Yesterday"));
0336 
0337     testDate = QDate::currentDate().addDays(2);
0338     QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("In two days"));
0339     QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("In two days"));
0340     QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("In two days"));
0341 
0342     testDate = QDate::currentDate().addDays(-2);
0343     QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Two days ago"));
0344     QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Two days ago"));
0345     QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Two days ago"));
0346 
0347     testDate = QDate::currentDate().addDays(-3);
0348     QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QLocale::c().toString(testDate, QLocale::LongFormat));
0349     QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QLocale::c().toString(testDate, QLocale::ShortFormat));
0350     QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QLocale::c().toString(testDate, QLocale::NarrowFormat));
0351 
0352     testDate = QDate::currentDate().addDays(3);
0353     QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QLocale::c().toString(testDate, QLocale::LongFormat));
0354     QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QLocale::c().toString(testDate, QLocale::ShortFormat));
0355     QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QLocale::c().toString(testDate, QLocale::NarrowFormat));
0356 
0357     testDate = QDate(); // invalid date
0358     QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Invalid date"));
0359 
0360     QDateTime now = QDateTime::currentDateTime();
0361 
0362     // An hour ago is **usually** today, except after midnight; just bump
0363     // to after 2am to make the "today" test work.
0364     if (now.time().hour() == 0) {
0365         now = now.addSecs(7201);
0366     }
0367 
0368     QDateTime testDateTime = now.addSecs(-3600);
0369     QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat),
0370              QStringLiteral("Today at %1").arg(testDateTime.toString(QStringLiteral("hh:mm:ss"))));
0371 
0372     // 1 second ago
0373     now = QDateTime::currentDateTime();
0374     testDateTime = now.addSecs(-1);
0375     QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("Just now"));
0376 
0377     // 5 minutes ago
0378     testDateTime = now.addSecs(-300);
0379     QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("5 minute(s) ago"));
0380 
0381     testDateTime = QDateTime(QDate::currentDate().addDays(8), QTime(3, 0, 0));
0382     QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::LongFormat),
0383              QStringLiteral("%1 at %2")
0384                  .arg(QLocale::c().toString(testDateTime.date(), QLocale::LongFormat), QLocale::c().toString(testDateTime.time(), QLocale::ShortFormat)));
0385 
0386     // 2021-10-03 07:33:57.000
0387     testDateTime = QDateTime::fromMSecsSinceEpoch(1633239237000, QTimeZone::utc());
0388     QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::LongFormat),
0389              QStringLiteral("%1 at %2")
0390                  .arg(QLocale::c().toString(testDateTime.date(), QLocale::LongFormat), QLocale::c().toString(testDateTime.time(), QLocale::ShortFormat)));
0391     QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat),
0392              QStringLiteral("%1 at %2")
0393                  .arg(QLocale::c().toString(testDateTime.date(), QLocale::ShortFormat), QLocale::c().toString(testDateTime.time(), QLocale::ShortFormat)));
0394 
0395     // With a different local for double check
0396     QLocale frenchLocal = QLocale::French;
0397     KFormat formatFrench(frenchLocal);
0398     QCOMPARE(formatFrench.formatRelativeDateTime(testDateTime, QLocale::LongFormat), QStringLiteral("Dimanche 3 octobre 2021 at 05:33"));
0399 }
0400 
0401 QTEST_MAIN(KFormatTest)
0402 
0403 #include "moc_kformattest.cpp"