File indexing completed on 2024-04-28 05:50:04
0001 /* 0002 * SPDX-License-Identifier: GPL-3.0-or-later 0003 * SPDX-FileCopyrightText: 2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> 0004 */ 0005 #include "model/accounts.h" 0006 0007 #include <QDateTime> 0008 #include <QObject> 0009 #include <QTest> 0010 0011 #include <functional> 0012 0013 class MillisecondsLeftForTokenTest: public QObject 0014 { 0015 Q_OBJECT 0016 private Q_SLOTS: 0017 void testMillisecondsLeftForToken(void); 0018 void testMillisecondsLeftForToken_data(void); 0019 }; 0020 0021 static void define_test_case(const char *testCase, const QDateTime &epoch, uint timeStep, qint64 now, qint64 left) 0022 { 0023 QTest::newRow(qPrintable(QLatin1String(testCase))) << epoch << timeStep << now << left; 0024 } 0025 0026 void MillisecondsLeftForTokenTest::testMillisecondsLeftForToken(void) 0027 { 0028 QFETCH(QDateTime, epoch); 0029 QFETCH(uint, timeStep); 0030 QFETCH(qint64, now); 0031 0032 const std::function<qint64(void)> clock([now](void) -> qint64 0033 { 0034 return now; 0035 }); 0036 0037 QTEST(model::millisecondsLeftForToken(epoch, timeStep, clock), "left"); 0038 } 0039 0040 void MillisecondsLeftForTokenTest::testMillisecondsLeftForToken_data(void) 0041 { 0042 QTest::addColumn<QDateTime>("epoch"); 0043 QTest::addColumn<uint>("timeStep"); 0044 QTest::addColumn<qint64>("now"); 0045 QTest::addColumn<qint64>("left"); 0046 0047 define_test_case("at the epoch itself", QDateTime::fromMSecsSinceEpoch(0), 30U, 0LL, 30LL * 1000LL); 0048 0049 const QString withLeftToGo(QLatin1String("with %1ms left to go")); 0050 for (qint64 left = 1000LL; left > 0; --left) { 0051 QTest::newRow(qPrintable(withLeftToGo.arg(left))) << QDateTime::fromMSecsSinceEpoch(0) << 1U << 1000LL - left << left; 0052 } 0053 0054 define_test_case("at the start of 'next' token", QDateTime::fromMSecsSinceEpoch(0), 30U, 30LL * 1000LL, 30LL * 1000LL); 0055 define_test_case("using a non-standard epoch (in the past)", QDateTime::fromMSecsSinceEpoch(15LL * 1000LL), 30U, 30LL * 1000LL, 15LL * 1000LL); 0056 0057 define_test_case("using a non-standard epoch (in the future)", QDateTime::fromMSecsSinceEpoch(25LL * 1000LL), 30U, 0LL, 25LL * 1000LL); 0058 } 0059 0060 QTEST_APPLESS_MAIN(MillisecondsLeftForTokenTest) 0061 0062 #include "milliseconds-left-for-token.moc"