annotate base/test/TestRealTime.h @ 1038:cc27f35aa75c cxx11

Introducing the signed 64-bit frame index type, and fixing build failures from inclusion of -Wconversion with -Werror. Not finished yet.
author Chris Cannam
date Tue, 03 Mar 2015 15:18:24 +0000
parents abe13fe10ed6
children a1cd5abcb38b
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@1015 54 QCOMPARE(RealTime(1, ONE_BILLION), RealTime(2, 0));
Chris@1015 55 QCOMPARE(RealTime(1, -ONE_BILLION), RealTime(0, 0));
Chris@1015 56 QCOMPARE(RealTime(-1, ONE_BILLION), RealTime(0, 0));
Chris@1015 57 QCOMPARE(RealTime(-1, -ONE_BILLION), RealTime(-2, 0));
Chris@1015 58
Chris@1012 59 QCOMPARE(RealTime(2, -ONE_BILLION*2), RealTime(0, 0));
Chris@1012 60 QCOMPARE(RealTime(2, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
Chris@1012 61
Chris@1012 62 QCOMPARE(RealTime(-2, ONE_BILLION*2), RealTime(0, 0));
Chris@1012 63 QCOMPARE(RealTime(-2, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2));
Chris@1012 64
Chris@1012 65 QCOMPARE(RealTime(0, 1).sec, 0);
Chris@1012 66 QCOMPARE(RealTime(0, 1).nsec, 1);
Chris@1012 67 QCOMPARE(RealTime(0, -1).sec, 0);
Chris@1012 68 QCOMPARE(RealTime(0, -1).nsec, -1);
Chris@1012 69 QCOMPARE(RealTime(1, -1).sec, 0);
Chris@1012 70 QCOMPARE(RealTime(1, -1).nsec, ONE_BILLION-1);
Chris@1012 71 QCOMPARE(RealTime(-1, 1).sec, 0);
Chris@1012 72 QCOMPARE(RealTime(-1, 1).nsec, -ONE_BILLION+1);
Chris@1012 73 QCOMPARE(RealTime(-1, -1).sec, -1);
Chris@1012 74 QCOMPARE(RealTime(-1, -1).nsec, -1);
Chris@1015 75
Chris@1015 76 QCOMPARE(RealTime(2, -ONE_BILLION*2).sec, 0);
Chris@1015 77 QCOMPARE(RealTime(2, -ONE_BILLION*2).nsec, 0);
Chris@1015 78 QCOMPARE(RealTime(2, -ONE_BILLION/2).sec, 1);
Chris@1015 79 QCOMPARE(RealTime(2, -ONE_BILLION/2).nsec, ONE_BILLION/2);
Chris@1015 80
Chris@1015 81 QCOMPARE(RealTime(-2, ONE_BILLION*2).sec, 0);
Chris@1015 82 QCOMPARE(RealTime(-2, ONE_BILLION*2).nsec, 0);
Chris@1015 83 QCOMPARE(RealTime(-2, ONE_BILLION/2).sec, -1);
Chris@1015 84 QCOMPARE(RealTime(-2, ONE_BILLION/2).nsec, -ONE_BILLION/2);
Chris@1012 85 }
Chris@1012 86
Chris@1012 87 void fromSeconds()
Chris@1012 88 {
Chris@1012 89 QCOMPARE(RealTime::fromSeconds(0), RealTime(0, 0));
Chris@1012 90
Chris@1012 91 QCOMPARE(RealTime::fromSeconds(0.5).sec, 0);
Chris@1012 92 QCOMPARE(RealTime::fromSeconds(0.5).nsec, ONE_BILLION/2);
Chris@1012 93 QCOMPARE(RealTime::fromSeconds(0.5).usec(), ONE_MILLION/2);
Chris@1012 94 QCOMPARE(RealTime::fromSeconds(0.5).msec(), 500);
Chris@1012 95
Chris@1012 96 QCOMPARE(RealTime::fromSeconds(0.5), RealTime(0, ONE_BILLION/2));
Chris@1012 97 QCOMPARE(RealTime::fromSeconds(1), RealTime(1, 0));
Chris@1012 98 QCOMPARE(RealTime::fromSeconds(1.5), RealTime(1, ONE_BILLION/2));
Chris@1012 99
Chris@1012 100 QCOMPARE(RealTime::fromSeconds(-0.5).sec, 0);
Chris@1012 101 QCOMPARE(RealTime::fromSeconds(-0.5).nsec, -ONE_BILLION/2);
Chris@1012 102 QCOMPARE(RealTime::fromSeconds(-0.5).usec(), -ONE_MILLION/2);
Chris@1012 103 QCOMPARE(RealTime::fromSeconds(-0.5).msec(), -500);
Chris@1012 104
Chris@1012 105 QCOMPARE(RealTime::fromSeconds(-1.5).sec, -1);
Chris@1012 106 QCOMPARE(RealTime::fromSeconds(-1.5).nsec, -ONE_BILLION/2);
Chris@1012 107 QCOMPARE(RealTime::fromSeconds(-1.5).usec(), -ONE_MILLION/2);
Chris@1012 108 QCOMPARE(RealTime::fromSeconds(-1.5).msec(), -500);
Chris@1012 109
Chris@1012 110 QCOMPARE(RealTime::fromSeconds(-0.5), RealTime(0, -ONE_BILLION/2));
Chris@1012 111 QCOMPARE(RealTime::fromSeconds(-1), RealTime(-1, 0));
Chris@1012 112 QCOMPARE(RealTime::fromSeconds(-1.5), RealTime(-1, -ONE_BILLION/2));
Chris@1012 113 }
Chris@1012 114
Chris@1012 115 void fromMilliseconds()
Chris@1012 116 {
Chris@1012 117 QCOMPARE(RealTime::fromMilliseconds(0), RealTime(0, 0));
Chris@1012 118 QCOMPARE(RealTime::fromMilliseconds(500), RealTime(0, ONE_BILLION/2));
Chris@1012 119 QCOMPARE(RealTime::fromMilliseconds(1000), RealTime(1, 0));
Chris@1012 120 QCOMPARE(RealTime::fromMilliseconds(1500), RealTime(1, ONE_BILLION/2));
Chris@1012 121
Chris@1012 122 QCOMPARE(RealTime::fromMilliseconds(-0), RealTime(0, 0));
Chris@1012 123 QCOMPARE(RealTime::fromMilliseconds(-500), RealTime(0, -ONE_BILLION/2));
Chris@1012 124 QCOMPARE(RealTime::fromMilliseconds(-1000), RealTime(-1, 0));
Chris@1012 125 QCOMPARE(RealTime::fromMilliseconds(-1500), RealTime(-1, -ONE_BILLION/2));
Chris@1012 126 }
Chris@1012 127
Chris@1012 128 void fromTimeval()
Chris@1012 129 {
Chris@1012 130 struct timeval tv;
Chris@1012 131
Chris@1012 132 tv.tv_sec = 0; tv.tv_usec = 0;
Chris@1012 133 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, 0));
Chris@1012 134 tv.tv_sec = 0; tv.tv_usec = ONE_MILLION/2;
Chris@1012 135 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, ONE_BILLION/2));
Chris@1012 136 tv.tv_sec = 1; tv.tv_usec = 0;
Chris@1012 137 QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, 0));
Chris@1012 138 tv.tv_sec = 1; tv.tv_usec = ONE_MILLION/2;
Chris@1012 139 QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, ONE_BILLION/2));
Chris@1012 140
Chris@1012 141 tv.tv_sec = 0; tv.tv_usec = -ONE_MILLION/2;
Chris@1012 142 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, -ONE_BILLION/2));
Chris@1012 143 tv.tv_sec = -1; tv.tv_usec = 0;
Chris@1012 144 QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, 0));
Chris@1012 145 tv.tv_sec = -1; tv.tv_usec = -ONE_MILLION/2;
Chris@1012 146 QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, -ONE_BILLION/2));
Chris@1012 147 }
Chris@1012 148
Chris@1012 149 void fromXsdDuration()
Chris@1012 150 {
Chris@1012 151 QCOMPARE(RealTime::fromXsdDuration("PT0"), RealTime::zeroTime);
Chris@1012 152 QCOMPARE(RealTime::fromXsdDuration("PT0S"), RealTime::zeroTime);
Chris@1012 153 QCOMPARE(RealTime::fromXsdDuration("PT10S"), RealTime(10, 0));
Chris@1012 154 QCOMPARE(RealTime::fromXsdDuration("PT10.5S"), RealTime(10, ONE_BILLION/2));
Chris@1012 155 QCOMPARE(RealTime::fromXsdDuration("PT1.5S").sec, 1);
Chris@1012 156 QCOMPARE(RealTime::fromXsdDuration("PT1.5S").msec(), 500);
Chris@1012 157 QCOMPARE(RealTime::fromXsdDuration("-PT1.5S").sec, -1);
Chris@1012 158 QCOMPARE(RealTime::fromXsdDuration("-PT1.5S").msec(), -500);
Chris@1012 159 QCOMPARE(RealTime::fromXsdDuration("PT1M30.5S"), RealTime(90, ONE_BILLION/2));
Chris@1012 160 QCOMPARE(RealTime::fromXsdDuration("PT1H2M30.5S"), RealTime(3750, ONE_BILLION/2));
Chris@1012 161 }
Chris@1012 162
Chris@1012 163 void toDouble()
Chris@1012 164 {
Chris@1012 165 QCOMPARE(RealTime(0, 0).toDouble(), 0.0);
Chris@1012 166 QCOMPARE(RealTime(0, ONE_BILLION/2).toDouble(), 0.5);
Chris@1012 167 QCOMPARE(RealTime(1, 0).toDouble(), 1.0);
Chris@1012 168 QCOMPARE(RealTime(1, ONE_BILLION/2).toDouble(), 1.5);
Chris@1012 169
Chris@1012 170 QCOMPARE(RealTime(0, -ONE_BILLION/2).toDouble(), -0.5);
Chris@1012 171 QCOMPARE(RealTime(-1, 0).toDouble(), -1.0);
Chris@1012 172 QCOMPARE(RealTime(-1, -ONE_BILLION/2).toDouble(), -1.5);
Chris@1012 173 }
Chris@1012 174
Chris@1012 175 void assign()
Chris@1012 176 {
Chris@1012 177 RealTime r;
Chris@1012 178 r = RealTime(0, 0);
Chris@1012 179 QCOMPARE(r, RealTime::zeroTime);
Chris@1012 180 r = RealTime(0, ONE_BILLION/2);
Chris@1012 181 QCOMPARE(r.toDouble(), 0.5);
Chris@1012 182 r = RealTime(-1, -ONE_BILLION/2);
Chris@1012 183 QCOMPARE(r.toDouble(), -1.5);
Chris@1012 184 }
Chris@1012 185
Chris@1012 186 void plus()
Chris@1012 187 {
Chris@1012 188 QCOMPARE(RealTime(0, 0) + RealTime(0, 0), RealTime(0, 0));
Chris@1012 189
Chris@1012 190 QCOMPARE(RealTime(0, 0) + RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
Chris@1012 191 QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(0, ONE_BILLION/2), RealTime(1, 0));
Chris@1012 192 QCOMPARE(RealTime(1, 0) + RealTime(0, ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
Chris@1012 193
Chris@1012 194 QCOMPARE(RealTime(0, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2));
Chris@1012 195 QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(-1, 0));
Chris@1012 196 QCOMPARE(RealTime(-1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2));
Chris@1012 197
Chris@1012 198 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
Chris@1012 199 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(0, 0));
Chris@1012 200 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 201
Chris@1012 202 QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(-1, 0), RealTime(0, -ONE_BILLION/2));
Chris@1012 203 QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(1, 0), RealTime(0, ONE_BILLION/2));
Chris@1012 204 }
Chris@1014 205
Chris@1014 206 void minus()
Chris@1014 207 {
Chris@1014 208 QCOMPARE(RealTime(0, 0) - RealTime(0, 0), RealTime(0, 0));
Chris@1014 209
Chris@1014 210 QCOMPARE(RealTime(0, 0) - RealTime(0, ONE_BILLION/2), RealTime(0, -ONE_BILLION/2));
Chris@1014 211 QCOMPARE(RealTime(0, ONE_BILLION/2) - RealTime(0, ONE_BILLION/2), RealTime(0, 0));
Chris@1014 212 QCOMPARE(RealTime(1, 0) - RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
Chris@1014 213
Chris@1014 214 QCOMPARE(RealTime(0, 0) - RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
Chris@1014 215 QCOMPARE(RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(0, 0));
Chris@1014 216 QCOMPARE(RealTime(-1, 0) - RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2));
Chris@1014 217
Chris@1014 218 QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
Chris@1014 219 QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(2, 0));
Chris@1014 220 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 221
Chris@1014 222 QCOMPARE(RealTime(0, ONE_BILLION/2) - RealTime(-1, 0), RealTime(1, ONE_BILLION/2));
Chris@1014 223 QCOMPARE(RealTime(0, -ONE_BILLION/2) - RealTime(1, 0), RealTime(-1, -ONE_BILLION/2));
Chris@1014 224 }
Chris@1015 225
Chris@1015 226 void negate()
Chris@1015 227 {
Chris@1015 228 QCOMPARE(-RealTime(0, 0), RealTime(0, 0));
Chris@1015 229 QCOMPARE(-RealTime(1, 0), RealTime(-1, 0));
Chris@1015 230 QCOMPARE(-RealTime(1, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2));
Chris@1015 231 QCOMPARE(-RealTime(-1, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
Chris@1015 232 }
Chris@1015 233
Chris@1015 234 void compare()
Chris@1015 235 {
Chris@1015 236 int sec, nsec;
Chris@1015 237 for (sec = -2; sec <= 2; sec += 2) {
Chris@1015 238 for (nsec = -1; nsec <= 1; nsec += 1) {
Chris@1015 239 QCOMPARE(RealTime(sec, nsec) < RealTime(sec, nsec), false);
Chris@1015 240 QCOMPARE(RealTime(sec, nsec) > RealTime(sec, nsec), false);
Chris@1015 241 QCOMPARE(RealTime(sec, nsec) == RealTime(sec, nsec), true);
Chris@1015 242 QCOMPARE(RealTime(sec, nsec) != RealTime(sec, nsec), false);
Chris@1015 243 QCOMPARE(RealTime(sec, nsec) <= RealTime(sec, nsec), true);
Chris@1015 244 QCOMPARE(RealTime(sec, nsec) >= RealTime(sec, nsec), true);
Chris@1015 245 }
Chris@1015 246 }
Chris@1015 247 RealTime prev(-3, 0);
Chris@1015 248 for (sec = -2; sec <= 2; sec += 2) {
Chris@1015 249 for (nsec = -1; nsec <= 1; nsec += 1) {
Chris@1015 250
Chris@1015 251 RealTime curr(sec, nsec);
Chris@1015 252
Chris@1015 253 QCOMPARE(prev < curr, true);
Chris@1015 254 QCOMPARE(prev > curr, false);
Chris@1015 255 QCOMPARE(prev == curr, false);
Chris@1015 256 QCOMPARE(prev != curr, true);
Chris@1015 257 QCOMPARE(prev <= curr, true);
Chris@1015 258 QCOMPARE(prev >= curr, false);
Chris@1015 259
Chris@1015 260 QCOMPARE(curr < prev, false);
Chris@1015 261 QCOMPARE(curr > prev, true);
Chris@1015 262 QCOMPARE(curr == prev, false);
Chris@1015 263 QCOMPARE(curr != prev, true);
Chris@1015 264 QCOMPARE(curr <= prev, false);
Chris@1015 265 QCOMPARE(curr >= prev, true);
Chris@1015 266
Chris@1015 267 prev = curr;
Chris@1015 268 }
Chris@1015 269 }
Chris@1015 270 }
Chris@1015 271
Chris@1028 272 void frame()
Chris@1028 273 {
Chris@1028 274 int frames[] = {
Chris@1028 275 0, 1, 2047, 2048, 6656, 32767, 32768, 44100, 44101, 999999999
Chris@1028 276 };
Chris@1028 277 int n = sizeof(frames)/sizeof(frames[0]);
Chris@1028 278
Chris@1028 279 int rates[] = {
Chris@1028 280 1, 2, 8000, 22050, 44100, 44101, 192000
Chris@1028 281 };
Chris@1028 282 int m = sizeof(rates)/sizeof(rates[0]);
Chris@1028 283
Chris@1028 284 for (int i = 0; i < n; ++i) {
Chris@1028 285 int frame = frames[i];
Chris@1028 286 for (int j = 0; j < m; ++j) {
Chris@1028 287 int rate = rates[j];
Chris@1028 288
Chris@1028 289 RealTime rt = RealTime::frame2RealTime(frame, rate);
Chris@1028 290 int conv = RealTime::realTime2Frame(rt, rate);
Chris@1028 291 QCOMPARE(frame, conv);
Chris@1028 292
Chris@1028 293 rt = RealTime::frame2RealTime(-frame, rate);
Chris@1028 294 conv = RealTime::realTime2Frame(rt, rate);
Chris@1028 295 QCOMPARE(-frame, conv);
Chris@1028 296 }
Chris@1028 297 }
Chris@1028 298 }
Chris@1012 299 };
Chris@1012 300
Chris@1012 301 #endif