Chris@1012: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1012: Chris@1012: /* Chris@1012: Sonic Visualiser Chris@1012: An audio file viewer and annotation editor. Chris@1012: Centre for Digital Music, Queen Mary, University of London. Chris@1012: Chris@1012: This program is free software; you can redistribute it and/or Chris@1012: modify it under the terms of the GNU General Public License as Chris@1012: published by the Free Software Foundation; either version 2 of the Chris@1012: License, or (at your option) any later version. See the file Chris@1012: COPYING included with this distribution for more information. Chris@1012: */ Chris@1012: Chris@1012: #ifndef TEST_REALTIME_H Chris@1012: #define TEST_REALTIME_H Chris@1012: Chris@1012: #include "../RealTime.h" Chris@1012: Chris@1012: #include Chris@1012: #include Chris@1012: #include Chris@1012: Chris@1012: #include Chris@1012: Chris@1012: using namespace std; Chris@1012: Chris@1012: class TestRealTime : public QObject Chris@1012: { Chris@1012: Q_OBJECT Chris@1012: Chris@1012: private slots: Chris@1012: Chris@1012: #define ONE_MILLION 1000000 Chris@1012: #define ONE_BILLION 1000000000 Chris@1012: Chris@1012: void zero() Chris@1012: { Chris@1012: QCOMPARE(RealTime(0, 0), RealTime::zeroTime); Chris@1012: QCOMPARE(RealTime(0, 0).sec, 0); Chris@1012: QCOMPARE(RealTime(0, 0).nsec, 0); Chris@1012: QCOMPARE(RealTime(0, 0).msec(), 0); Chris@1012: QCOMPARE(RealTime(0, 0).usec(), 0); Chris@1012: } Chris@1012: Chris@1012: void ctor() Chris@1012: { Chris@1012: QCOMPARE(RealTime(0, 0), RealTime(0, 0)); Chris@1012: Chris@1012: // wraparounds Chris@1012: QCOMPARE(RealTime(0, ONE_BILLION/2), RealTime(1, -ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime(0, -ONE_BILLION/2), RealTime(-1, ONE_BILLION/2)); Chris@1012: Chris@1015: QCOMPARE(RealTime(1, ONE_BILLION), RealTime(2, 0)); Chris@1015: QCOMPARE(RealTime(1, -ONE_BILLION), RealTime(0, 0)); Chris@1015: QCOMPARE(RealTime(-1, ONE_BILLION), RealTime(0, 0)); Chris@1015: QCOMPARE(RealTime(-1, -ONE_BILLION), RealTime(-2, 0)); Chris@1015: Chris@1012: QCOMPARE(RealTime(2, -ONE_BILLION*2), RealTime(0, 0)); Chris@1012: QCOMPARE(RealTime(2, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2)); Chris@1012: Chris@1012: QCOMPARE(RealTime(-2, ONE_BILLION*2), RealTime(0, 0)); Chris@1012: QCOMPARE(RealTime(-2, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2)); Chris@1012: Chris@1012: QCOMPARE(RealTime(0, 1).sec, 0); Chris@1012: QCOMPARE(RealTime(0, 1).nsec, 1); Chris@1012: QCOMPARE(RealTime(0, -1).sec, 0); Chris@1012: QCOMPARE(RealTime(0, -1).nsec, -1); Chris@1012: QCOMPARE(RealTime(1, -1).sec, 0); Chris@1012: QCOMPARE(RealTime(1, -1).nsec, ONE_BILLION-1); Chris@1012: QCOMPARE(RealTime(-1, 1).sec, 0); Chris@1012: QCOMPARE(RealTime(-1, 1).nsec, -ONE_BILLION+1); Chris@1012: QCOMPARE(RealTime(-1, -1).sec, -1); Chris@1012: QCOMPARE(RealTime(-1, -1).nsec, -1); Chris@1015: Chris@1015: QCOMPARE(RealTime(2, -ONE_BILLION*2).sec, 0); Chris@1015: QCOMPARE(RealTime(2, -ONE_BILLION*2).nsec, 0); Chris@1015: QCOMPARE(RealTime(2, -ONE_BILLION/2).sec, 1); Chris@1015: QCOMPARE(RealTime(2, -ONE_BILLION/2).nsec, ONE_BILLION/2); Chris@1015: Chris@1015: QCOMPARE(RealTime(-2, ONE_BILLION*2).sec, 0); Chris@1015: QCOMPARE(RealTime(-2, ONE_BILLION*2).nsec, 0); Chris@1015: QCOMPARE(RealTime(-2, ONE_BILLION/2).sec, -1); Chris@1015: QCOMPARE(RealTime(-2, ONE_BILLION/2).nsec, -ONE_BILLION/2); Chris@1012: } Chris@1012: Chris@1012: void fromSeconds() Chris@1012: { Chris@1012: QCOMPARE(RealTime::fromSeconds(0), RealTime(0, 0)); Chris@1012: Chris@1012: QCOMPARE(RealTime::fromSeconds(0.5).sec, 0); Chris@1012: QCOMPARE(RealTime::fromSeconds(0.5).nsec, ONE_BILLION/2); Chris@1012: QCOMPARE(RealTime::fromSeconds(0.5).usec(), ONE_MILLION/2); Chris@1012: QCOMPARE(RealTime::fromSeconds(0.5).msec(), 500); Chris@1012: Chris@1012: QCOMPARE(RealTime::fromSeconds(0.5), RealTime(0, ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime::fromSeconds(1), RealTime(1, 0)); Chris@1012: QCOMPARE(RealTime::fromSeconds(1.5), RealTime(1, ONE_BILLION/2)); Chris@1012: Chris@1012: QCOMPARE(RealTime::fromSeconds(-0.5).sec, 0); Chris@1012: QCOMPARE(RealTime::fromSeconds(-0.5).nsec, -ONE_BILLION/2); Chris@1012: QCOMPARE(RealTime::fromSeconds(-0.5).usec(), -ONE_MILLION/2); Chris@1012: QCOMPARE(RealTime::fromSeconds(-0.5).msec(), -500); Chris@1012: Chris@1012: QCOMPARE(RealTime::fromSeconds(-1.5).sec, -1); Chris@1012: QCOMPARE(RealTime::fromSeconds(-1.5).nsec, -ONE_BILLION/2); Chris@1012: QCOMPARE(RealTime::fromSeconds(-1.5).usec(), -ONE_MILLION/2); Chris@1012: QCOMPARE(RealTime::fromSeconds(-1.5).msec(), -500); Chris@1012: Chris@1012: QCOMPARE(RealTime::fromSeconds(-0.5), RealTime(0, -ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime::fromSeconds(-1), RealTime(-1, 0)); Chris@1012: QCOMPARE(RealTime::fromSeconds(-1.5), RealTime(-1, -ONE_BILLION/2)); Chris@1012: } Chris@1012: Chris@1012: void fromMilliseconds() Chris@1012: { Chris@1012: QCOMPARE(RealTime::fromMilliseconds(0), RealTime(0, 0)); Chris@1012: QCOMPARE(RealTime::fromMilliseconds(500), RealTime(0, ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime::fromMilliseconds(1000), RealTime(1, 0)); Chris@1012: QCOMPARE(RealTime::fromMilliseconds(1500), RealTime(1, ONE_BILLION/2)); Chris@1012: Chris@1012: QCOMPARE(RealTime::fromMilliseconds(-0), RealTime(0, 0)); Chris@1012: QCOMPARE(RealTime::fromMilliseconds(-500), RealTime(0, -ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime::fromMilliseconds(-1000), RealTime(-1, 0)); Chris@1012: QCOMPARE(RealTime::fromMilliseconds(-1500), RealTime(-1, -ONE_BILLION/2)); Chris@1012: } Chris@1012: Chris@1012: void fromTimeval() Chris@1012: { Chris@1012: struct timeval tv; Chris@1012: Chris@1012: tv.tv_sec = 0; tv.tv_usec = 0; Chris@1012: QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, 0)); Chris@1012: tv.tv_sec = 0; tv.tv_usec = ONE_MILLION/2; Chris@1012: QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, ONE_BILLION/2)); Chris@1012: tv.tv_sec = 1; tv.tv_usec = 0; Chris@1012: QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, 0)); Chris@1012: tv.tv_sec = 1; tv.tv_usec = ONE_MILLION/2; Chris@1012: QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, ONE_BILLION/2)); Chris@1012: Chris@1012: tv.tv_sec = 0; tv.tv_usec = -ONE_MILLION/2; Chris@1012: QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, -ONE_BILLION/2)); Chris@1012: tv.tv_sec = -1; tv.tv_usec = 0; Chris@1012: QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, 0)); Chris@1012: tv.tv_sec = -1; tv.tv_usec = -ONE_MILLION/2; Chris@1012: QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, -ONE_BILLION/2)); Chris@1012: } Chris@1012: Chris@1012: void fromXsdDuration() Chris@1012: { Chris@1012: QCOMPARE(RealTime::fromXsdDuration("PT0"), RealTime::zeroTime); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("PT0S"), RealTime::zeroTime); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("PT10S"), RealTime(10, 0)); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("PT10.5S"), RealTime(10, ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("PT1.5S").sec, 1); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("PT1.5S").msec(), 500); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("-PT1.5S").sec, -1); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("-PT1.5S").msec(), -500); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("PT1M30.5S"), RealTime(90, ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime::fromXsdDuration("PT1H2M30.5S"), RealTime(3750, ONE_BILLION/2)); Chris@1012: } Chris@1012: Chris@1012: void toDouble() Chris@1012: { Chris@1012: QCOMPARE(RealTime(0, 0).toDouble(), 0.0); Chris@1012: QCOMPARE(RealTime(0, ONE_BILLION/2).toDouble(), 0.5); Chris@1012: QCOMPARE(RealTime(1, 0).toDouble(), 1.0); Chris@1012: QCOMPARE(RealTime(1, ONE_BILLION/2).toDouble(), 1.5); Chris@1012: Chris@1012: QCOMPARE(RealTime(0, -ONE_BILLION/2).toDouble(), -0.5); Chris@1012: QCOMPARE(RealTime(-1, 0).toDouble(), -1.0); Chris@1012: QCOMPARE(RealTime(-1, -ONE_BILLION/2).toDouble(), -1.5); Chris@1012: } Chris@1012: Chris@1012: void assign() Chris@1012: { Chris@1012: RealTime r; Chris@1012: r = RealTime(0, 0); Chris@1012: QCOMPARE(r, RealTime::zeroTime); Chris@1012: r = RealTime(0, ONE_BILLION/2); Chris@1012: QCOMPARE(r.toDouble(), 0.5); Chris@1012: r = RealTime(-1, -ONE_BILLION/2); Chris@1012: QCOMPARE(r.toDouble(), -1.5); Chris@1012: } Chris@1012: Chris@1012: void plus() Chris@1012: { Chris@1012: QCOMPARE(RealTime(0, 0) + RealTime(0, 0), RealTime(0, 0)); Chris@1012: Chris@1012: QCOMPARE(RealTime(0, 0) + RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(0, ONE_BILLION/2), RealTime(1, 0)); Chris@1012: QCOMPARE(RealTime(1, 0) + RealTime(0, ONE_BILLION/2), RealTime(1, ONE_BILLION/2)); Chris@1012: Chris@1012: QCOMPARE(RealTime(0, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(-1, 0)); Chris@1012: QCOMPARE(RealTime(-1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2)); Chris@1012: Chris@1012: QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(0, 0)); Chris@1012: QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2)); Chris@1012: Chris@1012: QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(-1, 0), RealTime(0, -ONE_BILLION/2)); Chris@1012: QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(1, 0), RealTime(0, ONE_BILLION/2)); Chris@1012: } Chris@1014: Chris@1014: void minus() Chris@1014: { Chris@1014: QCOMPARE(RealTime(0, 0) - RealTime(0, 0), RealTime(0, 0)); Chris@1014: Chris@1014: QCOMPARE(RealTime(0, 0) - RealTime(0, ONE_BILLION/2), RealTime(0, -ONE_BILLION/2)); Chris@1014: QCOMPARE(RealTime(0, ONE_BILLION/2) - RealTime(0, ONE_BILLION/2), RealTime(0, 0)); Chris@1014: QCOMPARE(RealTime(1, 0) - RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2)); Chris@1014: Chris@1014: QCOMPARE(RealTime(0, 0) - RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2)); Chris@1014: QCOMPARE(RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(0, 0)); Chris@1014: QCOMPARE(RealTime(-1, 0) - RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2)); Chris@1014: Chris@1014: QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2)); Chris@1014: QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(2, 0)); Chris@1014: QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(2, ONE_BILLION/2)); Chris@1014: Chris@1014: QCOMPARE(RealTime(0, ONE_BILLION/2) - RealTime(-1, 0), RealTime(1, ONE_BILLION/2)); Chris@1014: QCOMPARE(RealTime(0, -ONE_BILLION/2) - RealTime(1, 0), RealTime(-1, -ONE_BILLION/2)); Chris@1014: } Chris@1015: Chris@1015: void negate() Chris@1015: { Chris@1015: QCOMPARE(-RealTime(0, 0), RealTime(0, 0)); Chris@1015: QCOMPARE(-RealTime(1, 0), RealTime(-1, 0)); Chris@1015: QCOMPARE(-RealTime(1, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2)); Chris@1015: QCOMPARE(-RealTime(-1, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2)); Chris@1015: } Chris@1015: Chris@1015: void compare() Chris@1015: { Chris@1015: int sec, nsec; Chris@1015: for (sec = -2; sec <= 2; sec += 2) { Chris@1015: for (nsec = -1; nsec <= 1; nsec += 1) { Chris@1015: QCOMPARE(RealTime(sec, nsec) < RealTime(sec, nsec), false); Chris@1015: QCOMPARE(RealTime(sec, nsec) > RealTime(sec, nsec), false); Chris@1015: QCOMPARE(RealTime(sec, nsec) == RealTime(sec, nsec), true); Chris@1015: QCOMPARE(RealTime(sec, nsec) != RealTime(sec, nsec), false); Chris@1015: QCOMPARE(RealTime(sec, nsec) <= RealTime(sec, nsec), true); Chris@1015: QCOMPARE(RealTime(sec, nsec) >= RealTime(sec, nsec), true); Chris@1015: } Chris@1015: } Chris@1015: RealTime prev(-3, 0); Chris@1015: for (sec = -2; sec <= 2; sec += 2) { Chris@1015: for (nsec = -1; nsec <= 1; nsec += 1) { Chris@1015: Chris@1015: RealTime curr(sec, nsec); Chris@1015: Chris@1015: QCOMPARE(prev < curr, true); Chris@1015: QCOMPARE(prev > curr, false); Chris@1015: QCOMPARE(prev == curr, false); Chris@1015: QCOMPARE(prev != curr, true); Chris@1015: QCOMPARE(prev <= curr, true); Chris@1015: QCOMPARE(prev >= curr, false); Chris@1015: Chris@1015: QCOMPARE(curr < prev, false); Chris@1015: QCOMPARE(curr > prev, true); Chris@1015: QCOMPARE(curr == prev, false); Chris@1015: QCOMPARE(curr != prev, true); Chris@1015: QCOMPARE(curr <= prev, false); Chris@1015: QCOMPARE(curr >= prev, true); Chris@1015: Chris@1015: prev = curr; Chris@1015: } Chris@1015: } Chris@1015: } Chris@1015: Chris@1015: Chris@1015: Chris@1012: }; Chris@1012: Chris@1012: #endif