1/*
2 * Copyright (C) 2012-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30
31#include <limits>
32#include <wtf/MathExtras.h>
33#include <wtf/MediaTime.h>
34
35using namespace std;
36
37namespace WTF {
38
39std::ostream& operator<<(std::ostream& out, const MediaTime& val)
40{
41 out << "{ ";
42 if (val.isInvalid())
43 out << "invalid";
44 else if (val.isPositiveInfinite())
45 out << "+infinite";
46 else if (val.isNegativeInfinite())
47 out << "-infinite";
48 else if (val.hasDoubleValue())
49 out << "double: " << val.toDouble();
50 else
51 out << "value: " << val.timeValue() << ", scale: " << val.timeScale();
52 return out << " }";
53}
54
55}
56
57namespace TestWebKitAPI {
58
59TEST(WTF, MediaTime)
60{
61 // Comparison Operators
62 EXPECT_EQ(MediaTime::positiveInfiniteTime() > MediaTime::negativeInfiniteTime(), true);
63 EXPECT_EQ(MediaTime::negativeInfiniteTime() < MediaTime::positiveInfiniteTime(), true);
64 EXPECT_EQ(MediaTime::negativeInfiniteTime() == MediaTime::negativeInfiniteTime(), true);
65 EXPECT_EQ(MediaTime::positiveInfiniteTime() == MediaTime::positiveInfiniteTime(), true);
66 EXPECT_EQ(MediaTime::positiveInfiniteTime() != MediaTime::negativeInfiniteTime(), true);
67 EXPECT_EQ(MediaTime::invalidTime() == MediaTime::invalidTime(), true);
68 EXPECT_EQ(MediaTime::invalidTime() != MediaTime::invalidTime(), false);
69 EXPECT_EQ(MediaTime::invalidTime() != MediaTime::zeroTime(), true);
70 EXPECT_EQ(MediaTime::invalidTime() > MediaTime::negativeInfiniteTime(), true);
71 EXPECT_EQ(MediaTime::invalidTime() > MediaTime::positiveInfiniteTime(), true);
72 EXPECT_EQ(MediaTime::negativeInfiniteTime() < MediaTime::invalidTime(), true);
73 EXPECT_EQ(MediaTime::positiveInfiniteTime() < MediaTime::invalidTime(), true);
74 EXPECT_EQ(MediaTime::indefiniteTime() == MediaTime::indefiniteTime(), true);
75 EXPECT_EQ(MediaTime::indefiniteTime() != MediaTime::indefiniteTime(), false);
76 EXPECT_EQ(MediaTime::indefiniteTime() != MediaTime::zeroTime(), true);
77 EXPECT_EQ(MediaTime::indefiniteTime() > MediaTime::negativeInfiniteTime(), true);
78 EXPECT_EQ(MediaTime::indefiniteTime() < MediaTime::positiveInfiniteTime(), true);
79 EXPECT_EQ(MediaTime::negativeInfiniteTime() < MediaTime::indefiniteTime(), true);
80 EXPECT_EQ(MediaTime::positiveInfiniteTime() > MediaTime::indefiniteTime(), true);
81 EXPECT_EQ(MediaTime(1, 1) < MediaTime::indefiniteTime(), true);
82 EXPECT_EQ(MediaTime::indefiniteTime() > MediaTime(1, 1), true);
83 EXPECT_EQ(MediaTime(1, 1) < MediaTime(2, 1), true);
84 EXPECT_EQ(MediaTime(2, 1) > MediaTime(1, 1), true);
85 EXPECT_EQ(MediaTime(1, 1) != MediaTime(2, 1), true);
86 EXPECT_EQ(MediaTime(2, 1) == MediaTime(2, 1), true);
87 EXPECT_EQ(MediaTime(2, 1) == MediaTime(4, 2), true);
88 EXPECT_EQ(MediaTime(-2, 1) < MediaTime(-1, 1), true);
89 EXPECT_EQ(MediaTime(-2, 1) <= MediaTime(-1, 1), true);
90 EXPECT_EQ(MediaTime(-1, 1) < MediaTime(-2, 1), false);
91 EXPECT_EQ(MediaTime(-1, 1) < MediaTime(2, 1), true);
92 EXPECT_EQ(MediaTime(1, 1) > MediaTime(-2, 1), true);
93 EXPECT_EQ(MediaTime(-8, 10) >= MediaTime(-1, 1), true);
94 EXPECT_TRUE((bool)MediaTime(1, 1));
95 EXPECT_TRUE(!MediaTime(0, 1));
96 EXPECT_TRUE(!MediaTime::invalidTime());
97 EXPECT_TRUE(!MediaTime::createWithDouble(0.0, 1));
98 EXPECT_FALSE(!MediaTime(1, 1));
99 EXPECT_FALSE((bool)MediaTime::invalidTime());
100
101 // Addition Operators
102 EXPECT_EQ(MediaTime::positiveInfiniteTime() + MediaTime::positiveInfiniteTime(), MediaTime::positiveInfiniteTime());
103 EXPECT_EQ(MediaTime::negativeInfiniteTime() + MediaTime::negativeInfiniteTime(), MediaTime::negativeInfiniteTime());
104 EXPECT_EQ(MediaTime::positiveInfiniteTime() + MediaTime::negativeInfiniteTime(), MediaTime::invalidTime());
105 EXPECT_EQ(MediaTime::negativeInfiniteTime() + MediaTime::positiveInfiniteTime(), MediaTime::invalidTime());
106 EXPECT_EQ(MediaTime::positiveInfiniteTime() + MediaTime(1, 1), MediaTime::positiveInfiniteTime());
107 EXPECT_EQ(MediaTime(1, 1) + MediaTime::positiveInfiniteTime(), MediaTime::positiveInfiniteTime());
108 EXPECT_EQ(MediaTime::negativeInfiniteTime() + MediaTime(1, 1), MediaTime::negativeInfiniteTime());
109 EXPECT_EQ(MediaTime(1, 1) + MediaTime::negativeInfiniteTime(), MediaTime::negativeInfiniteTime());
110 EXPECT_EQ(MediaTime::invalidTime() + MediaTime::positiveInfiniteTime(), MediaTime::invalidTime());
111 EXPECT_EQ(MediaTime::invalidTime() + MediaTime::negativeInfiniteTime(), MediaTime::invalidTime());
112 EXPECT_EQ(MediaTime::invalidTime() + MediaTime::invalidTime(), MediaTime::invalidTime());
113 EXPECT_EQ(MediaTime::invalidTime() + MediaTime(1, 1), MediaTime::invalidTime());
114 EXPECT_EQ(MediaTime::indefiniteTime() + MediaTime::positiveInfiniteTime(), MediaTime::indefiniteTime());
115 EXPECT_EQ(MediaTime::indefiniteTime() + MediaTime::negativeInfiniteTime(), MediaTime::indefiniteTime());
116 EXPECT_EQ(MediaTime::indefiniteTime() + MediaTime::indefiniteTime(), MediaTime::indefiniteTime());
117 EXPECT_EQ(MediaTime::indefiniteTime() + MediaTime(1, 1), MediaTime::indefiniteTime());
118 EXPECT_EQ(MediaTime(1, 1) + MediaTime(1, 1), MediaTime(2, 1));
119 EXPECT_EQ(MediaTime(1, 2) + MediaTime(1, 3), MediaTime(5, 6));
120 EXPECT_EQ(MediaTime(1, MediaTime::MaximumTimeScale - 1) + MediaTime(1, MediaTime::MaximumTimeScale - 2), MediaTime(2, MediaTime::MaximumTimeScale));
121
122 // Subtraction Operators
123 EXPECT_EQ(MediaTime::positiveInfiniteTime() - MediaTime::positiveInfiniteTime(), MediaTime::invalidTime());
124 EXPECT_EQ(MediaTime::negativeInfiniteTime() - MediaTime::negativeInfiniteTime(), MediaTime::invalidTime());
125 EXPECT_EQ(MediaTime::positiveInfiniteTime() - MediaTime::negativeInfiniteTime(), MediaTime::positiveInfiniteTime());
126 EXPECT_EQ(MediaTime::negativeInfiniteTime() - MediaTime::positiveInfiniteTime(), MediaTime::negativeInfiniteTime());
127 EXPECT_EQ(MediaTime::positiveInfiniteTime() - MediaTime(1, 1), MediaTime::positiveInfiniteTime());
128 EXPECT_EQ(MediaTime(1, 1) - MediaTime::positiveInfiniteTime(), MediaTime::negativeInfiniteTime());
129 EXPECT_EQ(MediaTime::negativeInfiniteTime() - MediaTime(1, 1), MediaTime::negativeInfiniteTime());
130 EXPECT_EQ(MediaTime(1, 1) - MediaTime::negativeInfiniteTime(), MediaTime::positiveInfiniteTime());
131 EXPECT_EQ(MediaTime::invalidTime() - MediaTime::positiveInfiniteTime(), MediaTime::invalidTime());
132 EXPECT_EQ(MediaTime::invalidTime() - MediaTime::negativeInfiniteTime(), MediaTime::invalidTime());
133 EXPECT_EQ(MediaTime::invalidTime() - MediaTime::invalidTime(), MediaTime::invalidTime());
134 EXPECT_EQ(MediaTime::invalidTime() - MediaTime(1, 1), MediaTime::invalidTime());
135 EXPECT_EQ(MediaTime::indefiniteTime() - MediaTime::positiveInfiniteTime(), MediaTime::indefiniteTime());
136 EXPECT_EQ(MediaTime::indefiniteTime() - MediaTime::negativeInfiniteTime(), MediaTime::indefiniteTime());
137 EXPECT_EQ(MediaTime::indefiniteTime() - MediaTime::indefiniteTime(), MediaTime::indefiniteTime());
138 EXPECT_EQ(MediaTime::indefiniteTime() - MediaTime(1, 1), MediaTime::indefiniteTime());
139 EXPECT_EQ(MediaTime(3, 1) - MediaTime(2, 1), MediaTime(1, 1));
140 EXPECT_EQ(MediaTime(1, 2) - MediaTime(1, 3), MediaTime(1, 6));
141 EXPECT_EQ(MediaTime(2, MediaTime::MaximumTimeScale - 1) - MediaTime(1, MediaTime::MaximumTimeScale - 2), MediaTime(1, MediaTime::MaximumTimeScale));
142
143 // Multiplication Operators
144 EXPECT_EQ(MediaTime::positiveInfiniteTime(), MediaTime::positiveInfiniteTime() * 2);
145 EXPECT_EQ(MediaTime::negativeInfiniteTime(), MediaTime::negativeInfiniteTime() * 2);
146 EXPECT_EQ(MediaTime::negativeInfiniteTime(), MediaTime::positiveInfiniteTime() * -2);
147 EXPECT_EQ(MediaTime::positiveInfiniteTime(), MediaTime::negativeInfiniteTime() * -2);
148 EXPECT_EQ(MediaTime::invalidTime(), MediaTime::invalidTime() * 2);
149 EXPECT_EQ(MediaTime::invalidTime(), MediaTime::invalidTime() * -2);
150 EXPECT_EQ(MediaTime::indefiniteTime(), MediaTime::indefiniteTime() * 2);
151 EXPECT_EQ(MediaTime::indefiniteTime(), MediaTime::indefiniteTime() * -2);
152 EXPECT_EQ(MediaTime(6, 1), MediaTime(3, 1) * 2);
153 EXPECT_EQ(MediaTime(0, 1), MediaTime(0, 1) * 2);
154 EXPECT_EQ(MediaTime(int64_t(1) << 60, 1), MediaTime(int64_t(1) << 60, 2) * 2);
155 EXPECT_EQ(MediaTime::positiveInfiniteTime(), MediaTime(numeric_limits<int64_t>::max(), 1) * 2);
156 EXPECT_EQ(MediaTime::positiveInfiniteTime(), MediaTime(numeric_limits<int64_t>::min(), 1) * -2);
157 EXPECT_EQ(MediaTime::negativeInfiniteTime(), MediaTime(numeric_limits<int64_t>::max(), 1) * -2);
158 EXPECT_EQ(MediaTime::negativeInfiniteTime(), MediaTime(numeric_limits<int64_t>::min(), 1) * 2);
159
160 // Constants
161 EXPECT_EQ(MediaTime::zeroTime(), MediaTime(0, 1));
162 EXPECT_EQ(MediaTime::invalidTime(), MediaTime(-1, 1, 0));
163 EXPECT_EQ(MediaTime::positiveInfiniteTime(), MediaTime(0, 1, MediaTime::PositiveInfinite));
164 EXPECT_EQ(MediaTime::negativeInfiniteTime(), MediaTime(0, 1, MediaTime::NegativeInfinite));
165 EXPECT_EQ(MediaTime::indefiniteTime(), MediaTime(0, 1, MediaTime::Indefinite));
166
167 // Absolute Functions
168 EXPECT_EQ(abs(MediaTime::positiveInfiniteTime()), MediaTime::positiveInfiniteTime());
169 EXPECT_EQ(abs(MediaTime::negativeInfiniteTime()), MediaTime::positiveInfiniteTime());
170 EXPECT_EQ(abs(MediaTime::invalidTime()), MediaTime::invalidTime());
171 EXPECT_EQ(abs(MediaTime(1, 1)), MediaTime(1, 1));
172 EXPECT_EQ(abs(MediaTime(-1, 1)), MediaTime(1, 1));
173
174 // Floating Point Functions
175 EXPECT_EQ(MediaTime::createWithFloat(1.0f), MediaTime(1, 1));
176 EXPECT_EQ(MediaTime::createWithFloat(1.5f), MediaTime(3, 2));
177 EXPECT_EQ(MediaTime::createWithDouble(1.0), MediaTime(1, 1));
178 EXPECT_EQ(MediaTime::createWithDouble(1.5), MediaTime(3, 2));
179 EXPECT_EQ(MediaTime(1, 1).toFloat(), 1.0f);
180 EXPECT_EQ(MediaTime(3, 2).toFloat(), 1.5f);
181 EXPECT_EQ(MediaTime(1, 1).toDouble(), 1.0);
182 EXPECT_EQ(MediaTime(3, 2).toDouble(), 1.5);
183 EXPECT_EQ(MediaTime(1, 1 << 16).toFloat(), 1 / pow(2.0f, 16.0f));
184 EXPECT_EQ(MediaTime(1, 1 << 30).toDouble(), 1 / pow(2.0, 30.0));
185 EXPECT_EQ(MediaTime::createWithDouble(piDouble, 1 << 30), MediaTime(3373259426U, 1 << 30));
186
187 EXPECT_EQ(MediaTime::createWithFloat(std::numeric_limits<float>::infinity()), MediaTime::positiveInfiniteTime());
188 EXPECT_EQ(MediaTime::createWithFloat(-std::numeric_limits<float>::infinity()), MediaTime::negativeInfiniteTime());
189 EXPECT_EQ(MediaTime::createWithFloat(std::numeric_limits<float>::quiet_NaN()), MediaTime::invalidTime());
190
191 EXPECT_EQ(MediaTime::createWithDouble(std::numeric_limits<double>::infinity()), MediaTime::positiveInfiniteTime());
192 EXPECT_EQ(MediaTime::createWithDouble(-std::numeric_limits<double>::infinity()), MediaTime::negativeInfiniteTime());
193 EXPECT_EQ(MediaTime::createWithDouble(std::numeric_limits<double>::quiet_NaN()), MediaTime::invalidTime());
194
195 // Floating Point Round Trip
196 EXPECT_EQ(10.0123456789f, MediaTime::createWithFloat(10.0123456789f).toFloat());
197 EXPECT_EQ(10.0123456789, MediaTime::createWithDouble(10.0123456789).toDouble());
198 EXPECT_EQ(MediaTime(1, 3), MediaTime::createWithDouble(MediaTime(1, 3).toDouble()));
199
200 // Floating Point Math
201 EXPECT_EQ(1.5 + 3.3, (MediaTime::createWithDouble(1.5) + MediaTime::createWithDouble(3.3)).toDouble());
202 EXPECT_EQ(1.5 - 3.3, (MediaTime::createWithDouble(1.5) - MediaTime::createWithDouble(3.3)).toDouble());
203 EXPECT_EQ(-3.3, (-MediaTime::createWithDouble(3.3)).toDouble());
204 EXPECT_EQ(3.3 * 2, (MediaTime::createWithDouble(3.3) * 2).toDouble());
205
206 EXPECT_EQ((MediaTime::createWithDouble(0.5)+MediaTime::createWithDouble(1e303)).toDouble(), 0.5f+1e303);
207 EXPECT_EQ((MediaTime::createWithDouble(0.5, 1U)+MediaTime::createWithDouble(1e303)).toDouble(), 0.5f+1e303);
208 EXPECT_EQ((MediaTime(1, 2)+MediaTime::createWithDouble(1e303)).toDouble(), 0.5f+1e303);
209 EXPECT_EQ((2*MediaTime::createWithDouble(1e303)).toDouble(), 2*1e303);
210
211 // Floating Point and non-Floating Point math
212 EXPECT_EQ(2.0, (MediaTime::createWithDouble(1.5) + MediaTime(1, 2)).toDouble());
213 EXPECT_EQ(1.0, (MediaTime::createWithDouble(1.5) - MediaTime(1, 2)).toDouble());
214
215 // Overflow Behavior
216 EXPECT_EQ(MediaTime::createWithFloat(pow(2.0f, 64.0f), 1U), MediaTime::positiveInfiniteTime());
217 EXPECT_EQ(MediaTime::createWithFloat(-pow(2.0f, 64.0f), 1U), MediaTime::negativeInfiniteTime());
218 EXPECT_EQ(MediaTime::createWithFloat(pow(2.0f, 64.0f)).toFloat(), pow(2.0f, 64.0f));
219 EXPECT_EQ(MediaTime::createWithFloat(-pow(2.0f, 64.0f)).toFloat(), -pow(2.0f, 64.0f));
220 EXPECT_EQ(MediaTime::createWithFloat(pow(2.0f, 63.0f), 2).timeScale(), 1U);
221 EXPECT_EQ(MediaTime::createWithFloat(pow(2.0f, 63.0f), 3).timeScale(), 1U);
222 EXPECT_EQ(MediaTime::createWithDouble(pow(2.0, 64.0), 1U), MediaTime::positiveInfiniteTime());
223 EXPECT_EQ(MediaTime::createWithDouble(-pow(2.0, 64.0), 1U), MediaTime::negativeInfiniteTime());
224 EXPECT_EQ(MediaTime::createWithDouble(pow(2.0f, 64.0f)).toDouble(), pow(2.0f, 64.0f));
225 EXPECT_EQ(MediaTime::createWithDouble(-pow(2.0f, 64.0f)).toDouble(), -pow(2.0f, 64.0f));
226 EXPECT_EQ(MediaTime::createWithDouble(pow(2.0, 63.0), 2).timeScale(), 1U);
227 EXPECT_EQ(MediaTime::createWithDouble(pow(2.0, 63.0), 3).timeScale(), 1U);
228 EXPECT_EQ((MediaTime(numeric_limits<int64_t>::max(), 2) + MediaTime(numeric_limits<int64_t>::max(), 2)).timeScale(), 1U);
229 EXPECT_EQ((MediaTime(numeric_limits<int64_t>::min(), 2) - MediaTime(numeric_limits<int64_t>::max(), 2)).timeScale(), 1U);
230 EXPECT_EQ(MediaTime(numeric_limits<int64_t>::max(), 1) + MediaTime(numeric_limits<int64_t>::max(), 1), MediaTime::positiveInfiniteTime());
231 EXPECT_EQ(MediaTime(numeric_limits<int64_t>::min(), 1) + MediaTime(numeric_limits<int64_t>::min(), 1), MediaTime::negativeInfiniteTime());
232 EXPECT_EQ(MediaTime(numeric_limits<int64_t>::min(), 1) - MediaTime(numeric_limits<int64_t>::max(), 1), MediaTime::negativeInfiniteTime());
233 EXPECT_EQ(MediaTime(numeric_limits<int64_t>::max(), 1) - MediaTime(numeric_limits<int64_t>::min(), 1), MediaTime::positiveInfiniteTime());
234 EXPECT_EQ(MediaTime::createWithDouble(numeric_limits<double>::max()) + MediaTime::createWithDouble(numeric_limits<double>::max()), MediaTime::positiveInfiniteTime());
235 EXPECT_EQ(MediaTime::createWithDouble(numeric_limits<double>::lowest()) + MediaTime::createWithDouble(numeric_limits<double>::lowest()), MediaTime::negativeInfiniteTime());
236 EXPECT_EQ(MediaTime::createWithDouble(numeric_limits<double>::lowest()) - MediaTime::createWithDouble(numeric_limits<double>::max()), MediaTime::negativeInfiniteTime());
237 EXPECT_EQ(MediaTime::createWithDouble(numeric_limits<double>::max()) - MediaTime::createWithDouble(numeric_limits<double>::lowest()), MediaTime::positiveInfiniteTime());
238
239 // Rounding
240 EXPECT_EQ(MediaTime(1, 1).toTimeScale(2).timeValue(), 2);
241 EXPECT_EQ(MediaTime(1, 3).toTimeScale(6).timeValue(), 2);
242 EXPECT_EQ(MediaTime(2, 2).toTimeScale(1).timeValue(), 1);
243 EXPECT_EQ(MediaTime(2, 6).toTimeScale(3).timeValue(), 1);
244 EXPECT_EQ(MediaTime(1, 2).toTimeScale(1).hasBeenRounded(), true);
245 EXPECT_EQ(MediaTime(1, 2).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), 1);
246 EXPECT_EQ(MediaTime(51, 100).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), 1);
247 EXPECT_EQ(MediaTime(49, 100).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), 0);
248 EXPECT_EQ(MediaTime(3, 2).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), 2);
249 EXPECT_EQ(MediaTime(151, 100).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), 2);
250 EXPECT_EQ(MediaTime(149, 100).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), 1);
251 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), -1);
252 EXPECT_EQ(MediaTime(-51, 100).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), -1);
253 EXPECT_EQ(MediaTime(-49, 100).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), 0);
254 EXPECT_EQ(MediaTime(-3, 2).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), -2);
255 EXPECT_EQ(MediaTime(-151, 100).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), -2);
256 EXPECT_EQ(MediaTime(-149, 100).toTimeScale(1, MediaTime::RoundingFlags::HalfAwayFromZero).timeValue(), -1);
257 EXPECT_EQ(MediaTime(1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), 0);
258 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), 0);
259 EXPECT_EQ(MediaTime(3, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), 1);
260 EXPECT_EQ(MediaTime(151, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), 1);
261 EXPECT_EQ(MediaTime(149, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), 1);
262 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), 0);
263 EXPECT_EQ(MediaTime(-51, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), 0);
264 EXPECT_EQ(MediaTime(-49, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), 0);
265 EXPECT_EQ(MediaTime(-3, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), -1);
266 EXPECT_EQ(MediaTime(-151, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), -1);
267 EXPECT_EQ(MediaTime(-149, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardZero).timeValue(), -1);
268 EXPECT_EQ(MediaTime(1, 2).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), 1);
269 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), -1);
270 EXPECT_EQ(MediaTime(3, 2).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), 2);
271 EXPECT_EQ(MediaTime(151, 100).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), 2);
272 EXPECT_EQ(MediaTime(149, 100).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), 2);
273 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), -1);
274 EXPECT_EQ(MediaTime(-51, 100).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), -1);
275 EXPECT_EQ(MediaTime(-49, 100).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), -1);
276 EXPECT_EQ(MediaTime(-3, 2).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), -2);
277 EXPECT_EQ(MediaTime(-151, 100).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), -2);
278 EXPECT_EQ(MediaTime(-149, 100).toTimeScale(1, MediaTime::RoundingFlags::AwayFromZero).timeValue(), -2);
279 EXPECT_EQ(MediaTime(1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), 1);
280 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), 0);
281 EXPECT_EQ(MediaTime(3, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), 2);
282 EXPECT_EQ(MediaTime(151, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), 2);
283 EXPECT_EQ(MediaTime(149, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), 2);
284 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), 0);
285 EXPECT_EQ(MediaTime(-51, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), 0);
286 EXPECT_EQ(MediaTime(-49, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), 0);
287 EXPECT_EQ(MediaTime(-3, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), -1);
288 EXPECT_EQ(MediaTime(-151, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), -1);
289 EXPECT_EQ(MediaTime(-149, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardPositiveInfinity).timeValue(), -1);
290 EXPECT_EQ(MediaTime(1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), 0);
291 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), -1);
292 EXPECT_EQ(MediaTime(3, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), 1);
293 EXPECT_EQ(MediaTime(151, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), 1);
294 EXPECT_EQ(MediaTime(149, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), 1);
295 EXPECT_EQ(MediaTime(-1, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), -1);
296 EXPECT_EQ(MediaTime(-51, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), -1);
297 EXPECT_EQ(MediaTime(-49, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), -1);
298 EXPECT_EQ(MediaTime(-3, 2).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), -2);
299 EXPECT_EQ(MediaTime(-151, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), -2);
300 EXPECT_EQ(MediaTime(-149, 100).toTimeScale(1, MediaTime::RoundingFlags::TowardNegativeInfinity).timeValue(), -2);
301 EXPECT_EQ(MediaTime(numeric_limits<int64_t>::max(), 1).toTimeScale(2), MediaTime::positiveInfiniteTime());
302 EXPECT_EQ(MediaTime(numeric_limits<int64_t>::min(), 1).toTimeScale(2), MediaTime::negativeInfiniteTime());
303 int64_t maxInt32 = numeric_limits<int32_t>::max();
304 EXPECT_EQ(MediaTime(maxInt32 * 2, 1).toTimeScale(2).timeValue(), maxInt32 * 4);
305 int64_t bigInt = 1LL << 62;
306 EXPECT_EQ(MediaTime(bigInt, 1U << 31).toTimeScale(1U << 29).timeValue(), bigInt / 4);
307 EXPECT_EQ(MediaTime(bigInt + 1, 1U << 31).toTimeScale(1U << 29, MediaTime::RoundingFlags::TowardZero).timeValue(), bigInt / 4);
308 EXPECT_EQ(MediaTime(bigInt + 1, 1U << 31).toTimeScale(1U << 29).hasBeenRounded(), true);
309 EXPECT_EQ(MediaTime(bigInt - 2, MediaTime::MaximumTimeScale).toTimeScale(MediaTime::MaximumTimeScale - 1).hasBeenRounded(), true);
310 EXPECT_EQ(MediaTime(bigInt, 1).toTimeScale(MediaTime::MaximumTimeScale), MediaTime::positiveInfiniteTime());
311 EXPECT_EQ(MediaTime(-bigInt, 1).toTimeScale(MediaTime::MaximumTimeScale), MediaTime::negativeInfiniteTime());
312
313 // Non-zero timescale
314 EXPECT_EQ(MediaTime(102, 0), MediaTime::positiveInfiniteTime());
315 EXPECT_EQ(MediaTime(-102, 0), MediaTime::negativeInfiniteTime());
316 EXPECT_EQ(MediaTime::createWithDouble(99, 0), MediaTime::positiveInfiniteTime());
317 EXPECT_EQ(MediaTime::createWithDouble(-99, 0), MediaTime::negativeInfiniteTime());
318 EXPECT_EQ(MediaTime::createWithDouble(99).toTimeScale(0), MediaTime::positiveInfiniteTime());
319 EXPECT_EQ(MediaTime::createWithDouble(-99).toTimeScale(0), MediaTime::negativeInfiniteTime());
320 EXPECT_EQ(MediaTime::createWithFloat(909, 0), MediaTime::positiveInfiniteTime());
321 EXPECT_EQ(MediaTime::createWithFloat(-909, 0), MediaTime::negativeInfiniteTime());
322 EXPECT_EQ(MediaTime::createWithFloat(999).toTimeScale(0), MediaTime::positiveInfiniteTime());
323 EXPECT_EQ(MediaTime::createWithFloat(-999).toTimeScale(0), MediaTime::negativeInfiniteTime());
324}
325
326}
327
328