comparison base/test/TestRealTime.h @ 1027:ce1077bd663a cxx11

Merge from default branch
author Chris Cannam
date Mon, 09 Feb 2015 10:31:07 +0000
parents 30afdc7c7b89
children abe13fe10ed6
comparison
equal deleted inserted replaced
1011:dbb7f0ab011e 1027:ce1077bd663a
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(1, ONE_BILLION), RealTime(2, 0));
55 QCOMPARE(RealTime(1, -ONE_BILLION), RealTime(0, 0));
56 QCOMPARE(RealTime(-1, ONE_BILLION), RealTime(0, 0));
57 QCOMPARE(RealTime(-1, -ONE_BILLION), RealTime(-2, 0));
58
59 QCOMPARE(RealTime(2, -ONE_BILLION*2), RealTime(0, 0));
60 QCOMPARE(RealTime(2, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
61
62 QCOMPARE(RealTime(-2, ONE_BILLION*2), RealTime(0, 0));
63 QCOMPARE(RealTime(-2, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2));
64
65 QCOMPARE(RealTime(0, 1).sec, 0);
66 QCOMPARE(RealTime(0, 1).nsec, 1);
67 QCOMPARE(RealTime(0, -1).sec, 0);
68 QCOMPARE(RealTime(0, -1).nsec, -1);
69 QCOMPARE(RealTime(1, -1).sec, 0);
70 QCOMPARE(RealTime(1, -1).nsec, ONE_BILLION-1);
71 QCOMPARE(RealTime(-1, 1).sec, 0);
72 QCOMPARE(RealTime(-1, 1).nsec, -ONE_BILLION+1);
73 QCOMPARE(RealTime(-1, -1).sec, -1);
74 QCOMPARE(RealTime(-1, -1).nsec, -1);
75
76 QCOMPARE(RealTime(2, -ONE_BILLION*2).sec, 0);
77 QCOMPARE(RealTime(2, -ONE_BILLION*2).nsec, 0);
78 QCOMPARE(RealTime(2, -ONE_BILLION/2).sec, 1);
79 QCOMPARE(RealTime(2, -ONE_BILLION/2).nsec, ONE_BILLION/2);
80
81 QCOMPARE(RealTime(-2, ONE_BILLION*2).sec, 0);
82 QCOMPARE(RealTime(-2, ONE_BILLION*2).nsec, 0);
83 QCOMPARE(RealTime(-2, ONE_BILLION/2).sec, -1);
84 QCOMPARE(RealTime(-2, ONE_BILLION/2).nsec, -ONE_BILLION/2);
85 }
86
87 void fromSeconds()
88 {
89 QCOMPARE(RealTime::fromSeconds(0), RealTime(0, 0));
90
91 QCOMPARE(RealTime::fromSeconds(0.5).sec, 0);
92 QCOMPARE(RealTime::fromSeconds(0.5).nsec, ONE_BILLION/2);
93 QCOMPARE(RealTime::fromSeconds(0.5).usec(), ONE_MILLION/2);
94 QCOMPARE(RealTime::fromSeconds(0.5).msec(), 500);
95
96 QCOMPARE(RealTime::fromSeconds(0.5), RealTime(0, ONE_BILLION/2));
97 QCOMPARE(RealTime::fromSeconds(1), RealTime(1, 0));
98 QCOMPARE(RealTime::fromSeconds(1.5), RealTime(1, ONE_BILLION/2));
99
100 QCOMPARE(RealTime::fromSeconds(-0.5).sec, 0);
101 QCOMPARE(RealTime::fromSeconds(-0.5).nsec, -ONE_BILLION/2);
102 QCOMPARE(RealTime::fromSeconds(-0.5).usec(), -ONE_MILLION/2);
103 QCOMPARE(RealTime::fromSeconds(-0.5).msec(), -500);
104
105 QCOMPARE(RealTime::fromSeconds(-1.5).sec, -1);
106 QCOMPARE(RealTime::fromSeconds(-1.5).nsec, -ONE_BILLION/2);
107 QCOMPARE(RealTime::fromSeconds(-1.5).usec(), -ONE_MILLION/2);
108 QCOMPARE(RealTime::fromSeconds(-1.5).msec(), -500);
109
110 QCOMPARE(RealTime::fromSeconds(-0.5), RealTime(0, -ONE_BILLION/2));
111 QCOMPARE(RealTime::fromSeconds(-1), RealTime(-1, 0));
112 QCOMPARE(RealTime::fromSeconds(-1.5), RealTime(-1, -ONE_BILLION/2));
113 }
114
115 void fromMilliseconds()
116 {
117 QCOMPARE(RealTime::fromMilliseconds(0), RealTime(0, 0));
118 QCOMPARE(RealTime::fromMilliseconds(500), RealTime(0, ONE_BILLION/2));
119 QCOMPARE(RealTime::fromMilliseconds(1000), RealTime(1, 0));
120 QCOMPARE(RealTime::fromMilliseconds(1500), RealTime(1, ONE_BILLION/2));
121
122 QCOMPARE(RealTime::fromMilliseconds(-0), RealTime(0, 0));
123 QCOMPARE(RealTime::fromMilliseconds(-500), RealTime(0, -ONE_BILLION/2));
124 QCOMPARE(RealTime::fromMilliseconds(-1000), RealTime(-1, 0));
125 QCOMPARE(RealTime::fromMilliseconds(-1500), RealTime(-1, -ONE_BILLION/2));
126 }
127
128 void fromTimeval()
129 {
130 struct timeval tv;
131
132 tv.tv_sec = 0; tv.tv_usec = 0;
133 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, 0));
134 tv.tv_sec = 0; tv.tv_usec = ONE_MILLION/2;
135 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, ONE_BILLION/2));
136 tv.tv_sec = 1; tv.tv_usec = 0;
137 QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, 0));
138 tv.tv_sec = 1; tv.tv_usec = ONE_MILLION/2;
139 QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, ONE_BILLION/2));
140
141 tv.tv_sec = 0; tv.tv_usec = -ONE_MILLION/2;
142 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, -ONE_BILLION/2));
143 tv.tv_sec = -1; tv.tv_usec = 0;
144 QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, 0));
145 tv.tv_sec = -1; tv.tv_usec = -ONE_MILLION/2;
146 QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, -ONE_BILLION/2));
147 }
148
149 void fromXsdDuration()
150 {
151 QCOMPARE(RealTime::fromXsdDuration("PT0"), RealTime::zeroTime);
152 QCOMPARE(RealTime::fromXsdDuration("PT0S"), RealTime::zeroTime);
153 QCOMPARE(RealTime::fromXsdDuration("PT10S"), RealTime(10, 0));
154 QCOMPARE(RealTime::fromXsdDuration("PT10.5S"), RealTime(10, ONE_BILLION/2));
155 QCOMPARE(RealTime::fromXsdDuration("PT1.5S").sec, 1);
156 QCOMPARE(RealTime::fromXsdDuration("PT1.5S").msec(), 500);
157 QCOMPARE(RealTime::fromXsdDuration("-PT1.5S").sec, -1);
158 QCOMPARE(RealTime::fromXsdDuration("-PT1.5S").msec(), -500);
159 QCOMPARE(RealTime::fromXsdDuration("PT1M30.5S"), RealTime(90, ONE_BILLION/2));
160 QCOMPARE(RealTime::fromXsdDuration("PT1H2M30.5S"), RealTime(3750, ONE_BILLION/2));
161 }
162
163 void toDouble()
164 {
165 QCOMPARE(RealTime(0, 0).toDouble(), 0.0);
166 QCOMPARE(RealTime(0, ONE_BILLION/2).toDouble(), 0.5);
167 QCOMPARE(RealTime(1, 0).toDouble(), 1.0);
168 QCOMPARE(RealTime(1, ONE_BILLION/2).toDouble(), 1.5);
169
170 QCOMPARE(RealTime(0, -ONE_BILLION/2).toDouble(), -0.5);
171 QCOMPARE(RealTime(-1, 0).toDouble(), -1.0);
172 QCOMPARE(RealTime(-1, -ONE_BILLION/2).toDouble(), -1.5);
173 }
174
175 void assign()
176 {
177 RealTime r;
178 r = RealTime(0, 0);
179 QCOMPARE(r, RealTime::zeroTime);
180 r = RealTime(0, ONE_BILLION/2);
181 QCOMPARE(r.toDouble(), 0.5);
182 r = RealTime(-1, -ONE_BILLION/2);
183 QCOMPARE(r.toDouble(), -1.5);
184 }
185
186 void plus()
187 {
188 QCOMPARE(RealTime(0, 0) + RealTime(0, 0), RealTime(0, 0));
189
190 QCOMPARE(RealTime(0, 0) + RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
191 QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(0, ONE_BILLION/2), RealTime(1, 0));
192 QCOMPARE(RealTime(1, 0) + RealTime(0, ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
193
194 QCOMPARE(RealTime(0, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2));
195 QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(-1, 0));
196 QCOMPARE(RealTime(-1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2));
197
198 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
199 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(0, 0));
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));
201
202 QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(-1, 0), RealTime(0, -ONE_BILLION/2));
203 QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(1, 0), RealTime(0, ONE_BILLION/2));
204 }
205
206 void minus()
207 {
208 QCOMPARE(RealTime(0, 0) - RealTime(0, 0), RealTime(0, 0));
209
210 QCOMPARE(RealTime(0, 0) - RealTime(0, ONE_BILLION/2), RealTime(0, -ONE_BILLION/2));
211 QCOMPARE(RealTime(0, ONE_BILLION/2) - RealTime(0, ONE_BILLION/2), RealTime(0, 0));
212 QCOMPARE(RealTime(1, 0) - RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
213
214 QCOMPARE(RealTime(0, 0) - RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2));
215 QCOMPARE(RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(0, 0));
216 QCOMPARE(RealTime(-1, 0) - RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2));
217
218 QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
219 QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(2, 0));
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));
221
222 QCOMPARE(RealTime(0, ONE_BILLION/2) - RealTime(-1, 0), RealTime(1, ONE_BILLION/2));
223 QCOMPARE(RealTime(0, -ONE_BILLION/2) - RealTime(1, 0), RealTime(-1, -ONE_BILLION/2));
224 }
225
226 void negate()
227 {
228 QCOMPARE(-RealTime(0, 0), RealTime(0, 0));
229 QCOMPARE(-RealTime(1, 0), RealTime(-1, 0));
230 QCOMPARE(-RealTime(1, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2));
231 QCOMPARE(-RealTime(-1, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2));
232 }
233
234 void compare()
235 {
236 int sec, nsec;
237 for (sec = -2; sec <= 2; sec += 2) {
238 for (nsec = -1; nsec <= 1; nsec += 1) {
239 QCOMPARE(RealTime(sec, nsec) < RealTime(sec, nsec), false);
240 QCOMPARE(RealTime(sec, nsec) > RealTime(sec, nsec), false);
241 QCOMPARE(RealTime(sec, nsec) == RealTime(sec, nsec), true);
242 QCOMPARE(RealTime(sec, nsec) != RealTime(sec, nsec), false);
243 QCOMPARE(RealTime(sec, nsec) <= RealTime(sec, nsec), true);
244 QCOMPARE(RealTime(sec, nsec) >= RealTime(sec, nsec), true);
245 }
246 }
247 RealTime prev(-3, 0);
248 for (sec = -2; sec <= 2; sec += 2) {
249 for (nsec = -1; nsec <= 1; nsec += 1) {
250
251 RealTime curr(sec, nsec);
252
253 QCOMPARE(prev < curr, true);
254 QCOMPARE(prev > curr, false);
255 QCOMPARE(prev == curr, false);
256 QCOMPARE(prev != curr, true);
257 QCOMPARE(prev <= curr, true);
258 QCOMPARE(prev >= curr, false);
259
260 QCOMPARE(curr < prev, false);
261 QCOMPARE(curr > prev, true);
262 QCOMPARE(curr == prev, false);
263 QCOMPARE(curr != prev, true);
264 QCOMPARE(curr <= prev, false);
265 QCOMPARE(curr >= prev, true);
266
267 prev = curr;
268 }
269 }
270 }
271
272
273
274 };
275
276 #endif