File indexing completed on 2024-04-28 16:49:46
0001 /* 0002 SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include <QLocale> 0008 #include <QTest> 0009 0010 #include <unistd.h> 0011 0012 #include "Formatter.h" 0013 #include "Unit.h" 0014 0015 #define QSL QStringLiteral 0016 0017 class FormatterTest : public QObject 0018 { 0019 Q_OBJECT 0020 private Q_SLOTS: 0021 void initTestCase() 0022 { 0023 // Ensure we use a known locale for the test. 0024 QLocale::setDefault(QLocale{QLocale::English, QLocale::UnitedStates}); 0025 } 0026 0027 void testDouble_data() 0028 { 0029 QTest::addColumn<double>("input"); 0030 QTest::addColumn<KSysGuard::Unit>("unit"); 0031 QTest::addColumn<QString>("output"); 0032 0033 QTest::newRow("1.0, B") << 1.0 << KSysGuard::UnitByte << QSL("1.0 B"); 0034 QTest::newRow("1.0, KiB") << 1.0 << KSysGuard::UnitKiloByte << QSL("1.0 KiB"); 0035 QTest::newRow("1.0, KiB/s") << 1.0 << KSysGuard::UnitKiloByteRate << QSL("1.0 KiB/s"); 0036 QTest::newRow("1.0, %") << 1.0 << KSysGuard::UnitPercent << QSL("1.0%"); 0037 0038 QTest::newRow("0.213, B") << 0.213 << KSysGuard::UnitByte << QString::number(0.2) + QSL(" B"); 0039 QTest::newRow("5.647, KiB") << 5.647 << KSysGuard::UnitKiloByte << QString::number(5.6) + QSL(" KiB"); 0040 QTest::newRow("99.99, KiB/s") << 99.99 << KSysGuard::UnitKiloByteRate << QString::number(100.0, 'f', 1) + QSL(" KiB/s"); 0041 QTest::newRow("0.2567, %") << 0.2567 << KSysGuard::UnitPercent << QString::number(0.3) + QSL("%"); 0042 } 0043 0044 void testDouble() 0045 { 0046 QFETCH(double, input); 0047 QFETCH(KSysGuard::Unit, unit); 0048 QFETCH(QString, output); 0049 0050 auto formatted = KSysGuard::Formatter::formatValue(input, unit); 0051 QCOMPARE(formatted, output); 0052 } 0053 0054 void testFormatTime_data() 0055 { 0056 QTest::addColumn<int>("input"); 0057 QTest::addColumn<QString>("output"); 0058 QTest::newRow("1 s") << 1 << QSL("0:00:01"); 0059 QTest::newRow("10 s") << 10 << QSL("0:00:10"); 0060 QTest::newRow("1 m") << 60 << QSL("0:01:00"); 0061 QTest::newRow("10m") << 60 * 10 << QSL("0:10:00"); 0062 QTest::newRow("1h") << 60 * 60 << QSL("1:00:00"); 0063 QTest::newRow("1h 1 m 1s") << (60 * 60) + 60 + 1 << QSL("1:01:01"); 0064 QTest::newRow("25h 0m 0s") << (25 * 3600) << QSL("25:00:00"); 0065 } 0066 0067 void testFormatTime() 0068 { 0069 QFETCH(int, input); 0070 QFETCH(QString, output); 0071 auto formatted = KSysGuard::Formatter::formatValue(input, KSysGuard::UnitTime); 0072 QCOMPARE(formatted, output); 0073 } 0074 0075 void testFormatBootTimestamp_data() 0076 { 0077 QTest::addColumn<int>("input"); 0078 // This uses a list because we are comparing translated strings and those may 0079 // end up slightly different in some cases. 0080 QTest::addColumn<QStringList>("output"); 0081 QTest::newRow("1s ago") << 1 << QStringList{QSL("Just now")}; 0082 QTest::newRow("10s ago") << 10 << QStringList{QSL("Just now")}; 0083 QTest::newRow("1m ago") << 60 << QStringList{QSL("Just now")}; 0084 QTest::newRow("10m ago") << 600 << QStringList{QSL("10 minute(s) ago"), QSL("10 minutes ago")}; 0085 QTest::newRow("59m59s ago") << 3559 << QStringList{QSL("59 minute(s) ago"), QSL("59 minutes ago")}; 0086 } 0087 0088 void testFormatBootTimestamp() 0089 { 0090 QFETCH(int, input); 0091 QFETCH(QStringList, output); 0092 0093 timespec tp; 0094 #ifdef Q_OS_LINUX 0095 clock_gettime(CLOCK_BOOTTIME, &tp); 0096 #else 0097 clock_gettime(CLOCK_MONOTONIC, &tp); 0098 #endif 0099 0100 const auto ticks = sysconf(_SC_CLK_TCK); 0101 0102 const QDateTime systemBootTime = QDateTime::currentDateTime().addSecs(-tp.tv_sec); 0103 auto elapsed = QDateTime::currentDateTime().addSecs(-input); 0104 auto formatted = KSysGuard::Formatter::formatValue(systemBootTime.secsTo(elapsed) * ticks, KSysGuard::UnitBootTimestamp); 0105 QVERIFY(output.contains(formatted)); 0106 } 0107 0108 void testPrefixRollover_data() 0109 { 0110 QTest::addColumn<int>("input"); 0111 QTest::addColumn<KSysGuard::Unit>("unit"); 0112 QTest::addColumn<QString>("output"); 0113 0114 // Must not roll over to "1.3 W". 0115 QTest::newRow("kilovolt") << 1337 << KSysGuard::UnitVolt << QSL("1,337 V"); 0116 QTest::newRow("megavolt") << 1337000 << KSysGuard::UnitVolt << QSL("1,337,000 V"); 0117 // Must not roll over to "1.3 Wh". 0118 QTest::newRow("kilowatt") << 1337 << KSysGuard::UnitWatt << QSL("1,337 W"); 0119 QTest::newRow("megawatt") << 1337000 << KSysGuard::UnitWatt << QSL("1,337,000 W"); 0120 // Must not roll over to "1.3 V". 0121 QTest::newRow("kilowatthour") << 1337 << KSysGuard::UnitWattHour << QSL("1,337 Wh"); 0122 QTest::newRow("megawatthour") << 1337000 << KSysGuard::UnitWattHour << QSL("1,337,000 Wh"); 0123 // Must not roll over to having no unit. 0124 QTest::newRow("kiloampere") << 1337 << KSysGuard::UnitAmpere << QSL("1,337 A"); 0125 QTest::newRow("megaampere") << 1337000 << KSysGuard::UnitAmpere << QSL("1,337,000 A"); 0126 } 0127 0128 void testPrefixRollover() 0129 { 0130 QFETCH(int, input); 0131 QFETCH(KSysGuard::Unit, unit); 0132 QFETCH(QString, output); 0133 0134 auto formatted = KSysGuard::Formatter::formatValue(input, unit); 0135 QCOMPARE(formatted, output); 0136 } 0137 }; 0138 0139 QTEST_MAIN(FormatterTest); 0140 0141 #include "formattertest.moc"