comparison base/test/TestRealTime.h @ 1012:ee9f4477f65b

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