annotate base/test/TestRealTime.h @ 1013:6370575a812c

Merge
author Chris Cannam
date Mon, 17 Nov 2014 17:10:29 +0000
parents ee9f4477f65b
children ace22dccde8f
rev   line source
Chris@1012 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1012 2
Chris@1012 3 /*
Chris@1012 4 Sonic Visualiser
Chris@1012 5 An audio file viewer and annotation editor.
Chris@1012 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1012 7
Chris@1012 8 This program is free software; you can redistribute it and/or
Chris@1012 9 modify it under the terms of the GNU General Public License as
Chris@1012 10 published by the Free Software Foundation; either version 2 of the
Chris@1012 11 License, or (at your option) any later version. See the file
Chris@1012 12 COPYING included with this distribution for more information.
Chris@1012 13 */
Chris@1012 14
Chris@1012 15 #ifndef TEST_REALTIME_H
Chris@1012 16 #define TEST_REALTIME_H
Chris@1012 17
Chris@1012 18 #include "../RealTime.h"
Chris@1012 19
Chris@1012 20 #include <QObject>
Chris@1012 21 #include <QtTest>
Chris@1012 22 #include <QDir>
Chris@1012 23
Chris@1012 24 #include <iostream>
Chris@1012 25
Chris@1012 26 using namespace std;
Chris@1012 27
Chris@1012 28 class TestRealTime : public QObject
Chris@1012 29 {
Chris@1012 30 Q_OBJECT
Chris@1012 31
Chris@1012 32 private slots:
Chris@1012 33
Chris@1012 34 #define ONE_MILLION 1000000
Chris@1012 35 #define ONE_BILLION 1000000000
Chris@1012 36
Chris@1012 37 void zero()
Chris@1012 38 {
Chris@1012 39 QCOMPARE(RealTime(0, 0), RealTime::zeroTime);
Chris@1012 40 QCOMPARE(RealTime(0, 0).sec, 0);
Chris@1012 41 QCOMPARE(RealTime(0, 0).nsec, 0);
Chris@1012 42 QCOMPARE(RealTime(0, 0).msec(), 0);
Chris@1012 43 QCOMPARE(RealTime(0, 0).usec(), 0);
Chris@1012 44 }
Chris@1012 45
Chris@1012 46 void ctor()
Chris@1012 47 {
Chris@1012 48 QCOMPARE(RealTime(0, 0), RealTime(0, 0));
Chris@1012 49
Chris@1012 50 // wraparounds
Chris@1012 51 QCOMPARE(RealTime(0, ONE_BILLION/2), RealTime(1, -ONE_BILLION/2));
Chris@1012 52 QCOMPARE(RealTime(0, -ONE_BILLION/2), RealTime(-1, ONE_BILLION/2));
Chris@1012 53
Chris@1012 54 QCOMPARE(RealTime(2, -ONE_BILLION*2), RealTime(0, 0));
Chris@1012 55 QCOMPARE(RealTime(2, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
Chris@1012 56
Chris@1012 57 QCOMPARE(RealTime(-2, ONE_BILLION*2), RealTime(0, 0));
Chris@1012 58 QCOMPARE(RealTime(-2, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2));
Chris@1012 59
Chris@1012 60 QCOMPARE(RealTime(0, 1).sec, 0);
Chris@1012 61 QCOMPARE(RealTime(0, 1).nsec, 1);
Chris@1012 62 QCOMPARE(RealTime(0, -1).sec, 0);
Chris@1012 63 QCOMPARE(RealTime(0, -1).nsec, -1);
Chris@1012 64 QCOMPARE(RealTime(1, -1).sec, 0);
Chris@1012 65 QCOMPARE(RealTime(1, -1).nsec, ONE_BILLION-1);
Chris@1012 66 QCOMPARE(RealTime(-1, 1).sec, 0);
Chris@1012 67 QCOMPARE(RealTime(-1, 1).nsec, -ONE_BILLION+1);
Chris@1012 68 QCOMPARE(RealTime(-1, -1).sec, -1);
Chris@1012 69 QCOMPARE(RealTime(-1, -1).nsec, -1);
Chris@1012 70 }
Chris@1012 71
Chris@1012 72 void fromSeconds()
Chris@1012 73 {
Chris@1012 74 QCOMPARE(RealTime::fromSeconds(0), RealTime(0, 0));
Chris@1012 75
Chris@1012 76 QCOMPARE(RealTime::fromSeconds(0.5).sec, 0);
Chris@1012 77 QCOMPARE(RealTime::fromSeconds(0.5).nsec, ONE_BILLION/2);
Chris@1012 78 QCOMPARE(RealTime::fromSeconds(0.5).usec(), ONE_MILLION/2);
Chris@1012 79 QCOMPARE(RealTime::fromSeconds(0.5).msec(), 500);
Chris@1012 80
Chris@1012 81 QCOMPARE(RealTime::fromSeconds(0.5), RealTime(0, ONE_BILLION/2));
Chris@1012 82 QCOMPARE(RealTime::fromSeconds(1), RealTime(1, 0));
Chris@1012 83 QCOMPARE(RealTime::fromSeconds(1.5), RealTime(1, ONE_BILLION/2));
Chris@1012 84
Chris@1012 85 QCOMPARE(RealTime::fromSeconds(-0.5).sec, 0);
Chris@1012 86 QCOMPARE(RealTime::fromSeconds(-0.5).nsec, -ONE_BILLION/2);
Chris@1012 87 QCOMPARE(RealTime::fromSeconds(-0.5).usec(), -ONE_MILLION/2);
Chris@1012 88 QCOMPARE(RealTime::fromSeconds(-0.5).msec(), -500);
Chris@1012 89
Chris@1012 90 QCOMPARE(RealTime::fromSeconds(-1.5).sec, -1);
Chris@1012 91 QCOMPARE(RealTime::fromSeconds(-1.5).nsec, -ONE_BILLION/2);
Chris@1012 92 QCOMPARE(RealTime::fromSeconds(-1.5).usec(), -ONE_MILLION/2);
Chris@1012 93 QCOMPARE(RealTime::fromSeconds(-1.5).msec(), -500);
Chris@1012 94
Chris@1012 95 QCOMPARE(RealTime::fromSeconds(-0.5), RealTime(0, -ONE_BILLION/2));
Chris@1012 96 QCOMPARE(RealTime::fromSeconds(-1), RealTime(-1, 0));
Chris@1012 97 QCOMPARE(RealTime::fromSeconds(-1.5), RealTime(-1, -ONE_BILLION/2));
Chris@1012 98 }
Chris@1012 99
Chris@1012 100 void fromMilliseconds()
Chris@1012 101 {
Chris@1012 102 QCOMPARE(RealTime::fromMilliseconds(0), RealTime(0, 0));
Chris@1012 103 QCOMPARE(RealTime::fromMilliseconds(500), RealTime(0, ONE_BILLION/2));
Chris@1012 104 QCOMPARE(RealTime::fromMilliseconds(1000), RealTime(1, 0));
Chris@1012 105 QCOMPARE(RealTime::fromMilliseconds(1500), RealTime(1, ONE_BILLION/2));
Chris@1012 106
Chris@1012 107 QCOMPARE(RealTime::fromMilliseconds(-0), RealTime(0, 0));
Chris@1012 108 QCOMPARE(RealTime::fromMilliseconds(-500), RealTime(0, -ONE_BILLION/2));
Chris@1012 109 QCOMPARE(RealTime::fromMilliseconds(-1000), RealTime(-1, 0));
Chris@1012 110 QCOMPARE(RealTime::fromMilliseconds(-1500), RealTime(-1, -ONE_BILLION/2));
Chris@1012 111 }
Chris@1012 112
Chris@1012 113 void fromTimeval()
Chris@1012 114 {
Chris@1012 115 struct timeval tv;
Chris@1012 116
Chris@1012 117 tv.tv_sec = 0; tv.tv_usec = 0;
Chris@1012 118 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, 0));
Chris@1012 119 tv.tv_sec = 0; tv.tv_usec = ONE_MILLION/2;
Chris@1012 120 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, ONE_BILLION/2));
Chris@1012 121 tv.tv_sec = 1; tv.tv_usec = 0;
Chris@1012 122 QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, 0));
Chris@1012 123 tv.tv_sec = 1; tv.tv_usec = ONE_MILLION/2;
Chris@1012 124 QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, ONE_BILLION/2));
Chris@1012 125
Chris@1012 126 tv.tv_sec = 0; tv.tv_usec = -ONE_MILLION/2;
Chris@1012 127 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, -ONE_BILLION/2));
Chris@1012 128 tv.tv_sec = -1; tv.tv_usec = 0;
Chris@1012 129 QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, 0));
Chris@1012 130 tv.tv_sec = -1; tv.tv_usec = -ONE_MILLION/2;
Chris@1012 131 QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, -ONE_BILLION/2));
Chris@1012 132 }
Chris@1012 133
Chris@1012 134 void fromXsdDuration()
Chris@1012 135 {
Chris@1012 136 QCOMPARE(RealTime::fromXsdDuration("PT0"), RealTime::zeroTime);
Chris@1012 137 QCOMPARE(RealTime::fromXsdDuration("PT0S"), RealTime::zeroTime);
Chris@1012 138 QCOMPARE(RealTime::fromXsdDuration("PT10S"), RealTime(10, 0));
Chris@1012 139 QCOMPARE(RealTime::fromXsdDuration("PT10.5S"), RealTime(10, ONE_BILLION/2));
Chris@1012 140 QCOMPARE(RealTime::fromXsdDuration("PT1.5S").sec, 1);
Chris@1012 141 QCOMPARE(RealTime::fromXsdDuration("PT1.5S").msec(), 500);
Chris@1012 142 QCOMPARE(RealTime::fromXsdDuration("-PT1.5S").sec, -1);
Chris@1012 143 QCOMPARE(RealTime::fromXsdDuration("-PT1.5S").msec(), -500);
Chris@1012 144 QCOMPARE(RealTime::fromXsdDuration("PT1M30.5S"), RealTime(90, ONE_BILLION/2));
Chris@1012 145 QCOMPARE(RealTime::fromXsdDuration("PT1H2M30.5S"), RealTime(3750, ONE_BILLION/2));
Chris@1012 146 }
Chris@1012 147
Chris@1012 148 void toDouble()
Chris@1012 149 {
Chris@1012 150 QCOMPARE(RealTime(0, 0).toDouble(), 0.0);
Chris@1012 151 QCOMPARE(RealTime(0, ONE_BILLION/2).toDouble(), 0.5);
Chris@1012 152 QCOMPARE(RealTime(1, 0).toDouble(), 1.0);
Chris@1012 153 QCOMPARE(RealTime(1, ONE_BILLION/2).toDouble(), 1.5);
Chris@1012 154
Chris@1012 155 QCOMPARE(RealTime(0, -ONE_BILLION/2).toDouble(), -0.5);
Chris@1012 156 QCOMPARE(RealTime(-1, 0).toDouble(), -1.0);
Chris@1012 157 QCOMPARE(RealTime(-1, -ONE_BILLION/2).toDouble(), -1.5);
Chris@1012 158 }
Chris@1012 159
Chris@1012 160 void assign()
Chris@1012 161 {
Chris@1012 162 RealTime r;
Chris@1012 163 r = RealTime(0, 0);
Chris@1012 164 QCOMPARE(r, RealTime::zeroTime);
Chris@1012 165 r = RealTime(0, ONE_BILLION/2);
Chris@1012 166 QCOMPARE(r.toDouble(), 0.5);
Chris@1012 167 r = RealTime(-1, -ONE_BILLION/2);
Chris@1012 168 QCOMPARE(r.toDouble(), -1.5);
Chris@1012 169 }
Chris@1012 170
Chris@1012 171 void plus()
Chris@1012 172 {
Chris@1012 173 QCOMPARE(RealTime(0, 0) + RealTime(0, 0), RealTime(0, 0));
Chris@1012 174
Chris@1012 175 QCOMPARE(RealTime(0, 0) + RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
Chris@1012 176 QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(0, ONE_BILLION/2), RealTime(1, 0));
Chris@1012 177 QCOMPARE(RealTime(1, 0) + RealTime(0, ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
Chris@1012 178
Chris@1012 179 QCOMPARE(RealTime(0, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2));
Chris@1012 180 QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(-1, 0));
Chris@1012 181 QCOMPARE(RealTime(-1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2));
Chris@1012 182
Chris@1012 183 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
Chris@1012 184 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(0, 0));
Chris@1012 185 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 186
Chris@1012 187 QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(-1, 0), RealTime(0, -ONE_BILLION/2));
Chris@1012 188 QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(1, 0), RealTime(0, ONE_BILLION/2));
Chris@1012 189 }
Chris@1012 190 };
Chris@1012 191
Chris@1012 192 #endif