#include #include #include #include #include #include Q_DECLARE_METATYPE(Seconds); DateTimeRange computeZoom(QDateTime start, QDateTime stop, double zoom) { double start_epoch = start.toMSecsSinceEpoch(); double stop_epoch = stop.toMSecsSinceEpoch(); auto delta = stop_epoch - start_epoch; auto dt_ms = (zoom-1.)*(double(delta))/2.; return DateTimeRange{(start_epoch - dt_ms)/1000., (stop_epoch + dt_ms)/1000.}; } class TestDateTimeRange: public QObject { Q_OBJECT private slots: void testRangeDelta_data() { QTest::addColumn("tstart"); QTest::addColumn("tend"); QTest::addColumn("expected"); auto now = QDateTime::currentDateTime(); auto yesterday = QDateTime::currentDateTime().addDays(-1); QTest::newRow("No delta") << now << now << 0.; QTest::newRow("One day delta") << yesterday << now << 60.*60.*24.; QTest::newRow("Minus one day delta") << now << yesterday << -60.*60.*24.; } void testRangeDelta() { QFETCH(QDateTime,tstart); QFETCH(QDateTime,tend); QFETCH(double,expected); auto range = DateTimeRange::fromDateTime(tstart, tend); double delta = range.delta(); // Since it is built from QDateTime don't expect better resolution QVERIFY((delta-expected) <= 0.002); } void testRangeShift_data() { QTest::addColumn("initial"); QTest::addColumn>("shift"); QTest::addColumn("expected"); auto now = QDateTime::currentDateTime(); auto yestd = QDateTime::currentDateTime().addDays(-1); auto range = DateTimeRange::fromDateTime(yestd, now); QTest::newRow("No shift") << range << Seconds{0.} << range; QTest::newRow("One milisecond left") << range << Seconds{-.001} << DateTimeRange::fromDateTime(yestd.addMSecs(-1.), now.addMSecs(-1.)); QTest::newRow("One milisecond right") << range << Seconds{.001} << DateTimeRange::fromDateTime(yestd.addMSecs(1.), now.addMSecs(1.)); QTest::newRow("One second left") << range << Seconds{-1.} << DateTimeRange::fromDateTime(yestd.addSecs(-1.), now.addSecs(-1.)); QTest::newRow("One second right") << range << Seconds{1.} << DateTimeRange::fromDateTime(yestd.addSecs(1.), now.addSecs(1.)); QTest::newRow("One year left") << range << Seconds{-365.*24.*60.*60.} << DateTimeRange::fromDateTime(yestd.addYears(-1.), now.addYears(-1.)); QTest::newRow("One year right") << range << Seconds{365.*24.*60.*60.} << DateTimeRange::fromDateTime(yestd.addYears(1.), now.addYears(1.)); } void testRangeShift() { QFETCH(DateTimeRange,initial); QFETCH(Seconds,shift); QFETCH(DateTimeRange,expected); QCOMPARE(initial+shift, expected); } void testRangeZoom_data() { QTest::addColumn("initial"); QTest::addColumn("zoom"); QTest::addColumn("expected"); auto now = QDateTime::currentDateTime(); auto yestd = QDateTime::currentDateTime().addDays(-1); auto range = DateTimeRange::fromDateTime(yestd, now); QTest::newRow("No zoom") << range << 1. << range; QTest::newRow("Zoom IN 0.001") << range << 1.001 << computeZoom(yestd, now, 1.001); QTest::newRow("Zoom OUT 0.001") << range << 0.999 << computeZoom(yestd, now, 0.999); } void testRangeZoom() { QFETCH(DateTimeRange,initial); QFETCH(double,zoom); QFETCH(DateTimeRange,expected); QCOMPARE(initial*zoom, expected); } }; QTEST_MAIN(TestDateTimeRange) #include "TestDateTimeRange.moc"