File indexing completed on 2024-04-28 15:20:08

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