1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
#include <cxxtest/TestSuite.h>
#include "audio/timestamp.h"
#include <limits.h>
class TimestampTestSuite : public CxxTest::TestSuite
{
public:
void test_diff_add_frames() {
const Audio::Timestamp a(10000, 1000);
const Audio::Timestamp b(10001, 1000);
const Audio::Timestamp c(10002, 1000);
TS_ASSERT_EQUALS(a.frameDiff(b), -1);
TS_ASSERT_EQUALS(b.frameDiff(a), 1);
TS_ASSERT_EQUALS(c.frameDiff(a), 2);
TS_ASSERT_EQUALS(b.addFrames(2000).frameDiff(a), 2001);
TS_ASSERT_EQUALS(a.frameDiff(b), -1);
TS_ASSERT_EQUALS(b.frameDiff(a), 1);
TS_ASSERT_EQUALS(c.frameDiff(a), 2);
TS_ASSERT_EQUALS(b.addFrames(2000).frameDiff(a.addFrames(-1000)), 3001);
TS_ASSERT_EQUALS(a.frameDiff(b), -1);
TS_ASSERT_EQUALS(b.frameDiff(a), 1);
TS_ASSERT_EQUALS(c.frameDiff(a), 2);
}
void test_diff_add_msecs() {
Audio::Timestamp ts0(3, 22050);
Audio::Timestamp ts1(0, 22050);
Audio::Timestamp ts2(0, 22050);
TS_ASSERT_EQUALS(ts0.msecs(), 3);
TS_ASSERT_EQUALS(ts0.totalNumberOfFrames(), 3 * 22050 / 1000);
TS_ASSERT_EQUALS(ts0.numberOfFrames(), 3 * 22050 / 1000);
ts1 = ts1.addFrames(53248);
TS_ASSERT_EQUALS(ts1.secs(), 2);
TS_ASSERT_EQUALS(ts1.msecs(), 53248 * 1000 / 22050);
TS_ASSERT_EQUALS(ts1.totalNumberOfFrames(), 53248);
TS_ASSERT_EQUALS(ts1.numberOfFrames(), 53248 - 2 * 22050);
ts1 = ts1.addMsecs(47);
TS_ASSERT_EQUALS(ts1.secs(), 2);
TS_ASSERT_EQUALS(ts1.msecs(), 2414+47);
TS_ASSERT_EQUALS(ts1.totalNumberOfFrames(), 47*22050 / 1000 + 53248);
TS_ASSERT_EQUALS(ts1.numberOfFrames(), 47*22050 / 1000 + 53248 - 2 * 22050);
ts2 = ts2.addMsecs(47);
TS_ASSERT_EQUALS(ts2.secs(), 0);
TS_ASSERT_EQUALS(ts2.msecs(), 47);
TS_ASSERT_EQUALS(ts2.totalNumberOfFrames(), 47*22050 / 1000);
TS_ASSERT_EQUALS(ts2.numberOfFrames(), 47*22050 / 1000);
ts2 = ts2.addFrames(53248);
TS_ASSERT_EQUALS(ts2.secs(), 2);
TS_ASSERT_EQUALS(ts2.msecs(), 2414+47);
TS_ASSERT_EQUALS(ts2.totalNumberOfFrames(), 47*22050 / 1000 + 53248);
TS_ASSERT_EQUALS(ts2.numberOfFrames(), 47*22050 / 1000 + 53248 - 2 * 22050);
}
void test_ticks() {
const Audio::Timestamp a(1234, 60);
const Audio::Timestamp b(5678, 60);
TS_ASSERT_EQUALS(a.msecs(), 1234);
TS_ASSERT_EQUALS(b.msecs(), 5678);
TS_ASSERT_EQUALS(a.secs(), 1);
TS_ASSERT_EQUALS(b.secs(), 5);
TS_ASSERT_EQUALS(a.msecsDiff(b), 1234 - 5678);
TS_ASSERT_EQUALS(b.msecsDiff(a), 5678 - 1234);
TS_ASSERT_EQUALS(a.frameDiff(b), (1234 - 5678) * 60 / 1000);
TS_ASSERT_EQUALS(b.frameDiff(a), (5678 - 1234) * 60 / 1000);
TS_ASSERT_EQUALS(a.addFrames(1).msecs(), (1234 + 1000 * 1/60));
TS_ASSERT_EQUALS(a.addFrames(59).msecs(), (1234 + 1000 * 59/60));
TS_ASSERT_EQUALS(a.addFrames(60).msecs(), (1234 + 1000 * 60/60));
// As soon as we go back even by only one frame, the msec value
// has to drop by at least one.
TS_ASSERT_EQUALS(a.addFrames(-1).msecs(), (1234 - 1000 * 1/60 - 1));
TS_ASSERT_EQUALS(a.addFrames(-59).msecs(), (1234 - 1000 * 59/60 - 1));
TS_ASSERT_EQUALS(a.addFrames(-60).msecs(), (1234 - 1000 * 60/60));
}
void test_more_add_diff() {
const Audio::Timestamp c(10002, 1000);
for (int i = -10000; i < 10000; i++) {
int v = c.addFrames(i).frameDiff(c);
TS_ASSERT_EQUALS(v, i);
}
}
void test_negate() {
const Audio::Timestamp a = Audio::Timestamp(0, 60).addFrames(13);
const Audio::Timestamp b = -a;
TS_ASSERT_EQUALS(a.msecs() + 1, -b.msecs());
TS_ASSERT_EQUALS(a.totalNumberOfFrames(), -b.totalNumberOfFrames());
TS_ASSERT_EQUALS(a.numberOfFrames(), 60 - b.numberOfFrames());
}
void test_add_sub() {
const Audio::Timestamp a = Audio::Timestamp(0, 60).addFrames(13);
const Audio::Timestamp b = -a;
const Audio::Timestamp c = Audio::Timestamp(0, 60).addFrames(20);
TS_ASSERT_EQUALS((a+a).secs(), 0);
TS_ASSERT_EQUALS((a+a).numberOfFrames(), 2*13);
TS_ASSERT_EQUALS((a+b).secs(), 0);
TS_ASSERT_EQUALS((a+b).numberOfFrames(), 0);
TS_ASSERT_EQUALS((a+c).secs(), 0);
TS_ASSERT_EQUALS((a+c).numberOfFrames(), 13+20);
TS_ASSERT_EQUALS((a-a).secs(), 0);
TS_ASSERT_EQUALS((a-a).numberOfFrames(), 0);
TS_ASSERT_EQUALS((a-b).secs(), 0);
TS_ASSERT_EQUALS((a-b).numberOfFrames(), 2*13);
TS_ASSERT_EQUALS((a-c).secs(), -1);
TS_ASSERT_EQUALS((a-c).numberOfFrames(), 60 + (13 - 20));
}
void test_diff_with_conversion() {
const Audio::Timestamp a = Audio::Timestamp(10, 1000).addFrames(20);
const Audio::Timestamp b = Audio::Timestamp(10, 1000/5).addFrames(20/5);
const Audio::Timestamp c = Audio::Timestamp(10, 1000*2).addFrames(20*2);
TS_ASSERT_EQUALS(a.frameDiff(a), 0);
TS_ASSERT_EQUALS(a.frameDiff(b), 0);
TS_ASSERT_EQUALS(a.frameDiff(c), 0);
TS_ASSERT_EQUALS(b.frameDiff(a), 0);
TS_ASSERT_EQUALS(b.frameDiff(b), 0);
TS_ASSERT_EQUALS(b.frameDiff(c), 0);
TS_ASSERT_EQUALS(c.frameDiff(a), 0);
TS_ASSERT_EQUALS(c.frameDiff(b), 0);
TS_ASSERT_EQUALS(c.frameDiff(c), 0);
}
void test_convert() {
const Audio::Timestamp a = Audio::Timestamp(10, 1000).addFrames(20);
const Audio::Timestamp b = Audio::Timestamp(10, 1000/5).addFrames(20/5);
const Audio::Timestamp c = Audio::Timestamp(10, 1000*2).addFrames(20*2);
TS_ASSERT_EQUALS(a.convertToFramerate(1000/5), b);
TS_ASSERT_EQUALS(a.convertToFramerate(1000*2), c);
}
void test_equals() {
const Audio::Timestamp a = Audio::Timestamp(500, 1000);
Audio::Timestamp b = Audio::Timestamp(0, 1000);
Audio::Timestamp c = Audio::Timestamp(0, 100);
TS_ASSERT_EQUALS(a, Audio::Timestamp(0, 500, 1000));
TS_ASSERT(a != b);
TS_ASSERT(a != c);
TS_ASSERT(b == c);
b = b.addFrames(500);
c = c.addFrames(50);
TS_ASSERT(a == b);
TS_ASSERT(a == c);
TS_ASSERT(b == c);
}
void test_compare() {
const Audio::Timestamp a = Audio::Timestamp(60, 1000);
Audio::Timestamp b = Audio::Timestamp(60, 60);
Audio::Timestamp c = Audio::Timestamp(60, 44100);
TS_ASSERT(a <= b);
TS_ASSERT(b <= c);
TS_ASSERT(a <= c);
TS_ASSERT(b >= a);
TS_ASSERT(c >= b);
TS_ASSERT(c >= a);
b = b.addFrames(60 / 12);
c = c.addFrames(44100 / 10);
TS_ASSERT(a < b);
TS_ASSERT(b < c);
TS_ASSERT(a < c);
TS_ASSERT(b > a);
TS_ASSERT(c > b);
TS_ASSERT(c > a);
TS_ASSERT(a <= b);
TS_ASSERT(b <= c);
TS_ASSERT(a <= c);
TS_ASSERT(b >= a);
TS_ASSERT(c >= b);
TS_ASSERT(c >= a);
}
void test_framerate() {
const Audio::Timestamp a = Audio::Timestamp(500, 1000);
const Audio::Timestamp b = Audio::Timestamp(500, 67);
const Audio::Timestamp c = Audio::Timestamp(500, 100);
const Audio::Timestamp d = Audio::Timestamp(500, 44100);
TS_ASSERT_EQUALS(a.framerate(), (uint)1000);
TS_ASSERT_EQUALS(b.framerate(), (uint)67);
TS_ASSERT_EQUALS(c.framerate(), (uint)100);
TS_ASSERT_EQUALS(d.framerate(), (uint)44100);
}
void test_direct_query() {
const Audio::Timestamp a = Audio::Timestamp(0, 22050);
const Audio::Timestamp b = a.addFrames(11025);
const Audio::Timestamp c = Audio::Timestamp(1500, 22050);
TS_ASSERT_EQUALS(a.secs(), 0);
TS_ASSERT_EQUALS(a.msecs(), 0);
TS_ASSERT_EQUALS(a.numberOfFrames(), 0);
TS_ASSERT_EQUALS(a.totalNumberOfFrames(), 0);
TS_ASSERT_EQUALS(b.secs(), 0);
TS_ASSERT_EQUALS(b.msecs(), 500);
TS_ASSERT_EQUALS(b.numberOfFrames(), 11025);
TS_ASSERT_EQUALS(b.totalNumberOfFrames(), 11025);
TS_ASSERT_EQUALS(c.secs(), 1);
TS_ASSERT_EQUALS(c.msecs(), 1500);
TS_ASSERT_EQUALS(c.numberOfFrames(), 11025);
TS_ASSERT_EQUALS(c.totalNumberOfFrames(), 33075);
}
void test_no_overflow() {
// The constructor should not overflow and give incoherent values
const Audio::Timestamp a = Audio::Timestamp(0, UINT_MAX, 1000);
int secs = UINT_MAX / 1000;
int frames = UINT_MAX % 1000;
TS_ASSERT_EQUALS(a.secs(), secs);
TS_ASSERT_EQUALS(a.numberOfFrames(), frames);
}
};
|