MediaTime.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2012 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 Computer, 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. #define _USE_MATH_DEFINES 1
  29. #include "config.h"
  30. #include <wtf/MediaTime.h>
  31. using namespace std;
  32. #if COMPILER(MSVC)
  33. // Work around Visual Studio 2008's lack of an INFINITY or NAN definition.
  34. #include <limits>
  35. #if !defined(INFINITY)
  36. #define INFINITY (numeric_limits<double>::infinity())
  37. #endif
  38. #if !defined(NAN)
  39. #define NAN (numeric_limits<double>::quiet_NaN())
  40. #endif
  41. #endif
  42. namespace WTF {
  43. std::ostream& operator<<(std::ostream& out, const MediaTime& val)
  44. {
  45. out << "{ ";
  46. if (val.isInvalid())
  47. out << "invalid";
  48. else if (val.isPositiveInfinite())
  49. out << "+infinite";
  50. else if (val.isNegativeInfinite())
  51. out << "-infinite";
  52. else
  53. out << "value: " << val.timeValue() << ", scale: " << val.timeScale();
  54. return out << " }";
  55. }
  56. }
  57. namespace TestWebKitAPI {
  58. TEST(WTF, MediaTime)
  59. {
  60. // Comparison Operators
  61. EXPECT_EQ(MediaTime::positiveInfiniteTime() > MediaTime::negativeInfiniteTime(), true);
  62. EXPECT_EQ(MediaTime::negativeInfiniteTime() < MediaTime::positiveInfiniteTime(), true);
  63. EXPECT_EQ(MediaTime::negativeInfiniteTime() == MediaTime::negativeInfiniteTime(), true);
  64. EXPECT_EQ(MediaTime::positiveInfiniteTime() == MediaTime::positiveInfiniteTime(), true);
  65. EXPECT_EQ(MediaTime::invalidTime() == MediaTime::invalidTime(), true);
  66. EXPECT_EQ(MediaTime::invalidTime() > MediaTime::negativeInfiniteTime(), true);
  67. EXPECT_EQ(MediaTime::invalidTime() > MediaTime::positiveInfiniteTime(), true);
  68. EXPECT_EQ(MediaTime::negativeInfiniteTime() < MediaTime::invalidTime(), true);
  69. EXPECT_EQ(MediaTime::positiveInfiniteTime() < MediaTime::invalidTime(), true);
  70. EXPECT_EQ(MediaTime::indefiniteTime() == MediaTime::indefiniteTime(), true);
  71. EXPECT_EQ(MediaTime::indefiniteTime() > MediaTime::negativeInfiniteTime(), true);
  72. EXPECT_EQ(MediaTime::indefiniteTime() < MediaTime::positiveInfiniteTime(), true);
  73. EXPECT_EQ(MediaTime::negativeInfiniteTime() < MediaTime::indefiniteTime(), true);
  74. EXPECT_EQ(MediaTime::positiveInfiniteTime() > MediaTime::indefiniteTime(), true);
  75. EXPECT_EQ(MediaTime(1, 1) < MediaTime::indefiniteTime(), true);
  76. EXPECT_EQ(MediaTime::indefiniteTime() > MediaTime(1, 1), true);
  77. EXPECT_EQ(MediaTime(1, 1) < MediaTime(2, 1), true);
  78. EXPECT_EQ(MediaTime(2, 1) > MediaTime(1, 1), true);
  79. EXPECT_EQ(MediaTime(2, 1) == MediaTime(2, 1), true);
  80. EXPECT_EQ(MediaTime(2, 1) == MediaTime(4, 2), true);
  81. // Addition Operators
  82. EXPECT_EQ(MediaTime::positiveInfiniteTime() + MediaTime::positiveInfiniteTime(), MediaTime::positiveInfiniteTime());
  83. EXPECT_EQ(MediaTime::negativeInfiniteTime() + MediaTime::negativeInfiniteTime(), MediaTime::negativeInfiniteTime());
  84. EXPECT_EQ(MediaTime::positiveInfiniteTime() + MediaTime::negativeInfiniteTime(), MediaTime::invalidTime());
  85. EXPECT_EQ(MediaTime::negativeInfiniteTime() + MediaTime::positiveInfiniteTime(), MediaTime::invalidTime());
  86. EXPECT_EQ(MediaTime::invalidTime() + MediaTime::positiveInfiniteTime(), MediaTime::invalidTime());
  87. EXPECT_EQ(MediaTime::invalidTime() + MediaTime::negativeInfiniteTime(), MediaTime::invalidTime());
  88. EXPECT_EQ(MediaTime::invalidTime() + MediaTime::invalidTime(), MediaTime::invalidTime());
  89. EXPECT_EQ(MediaTime::invalidTime() + MediaTime(1, 1), MediaTime::invalidTime());
  90. EXPECT_EQ(MediaTime::indefiniteTime() + MediaTime::positiveInfiniteTime(), MediaTime::indefiniteTime());
  91. EXPECT_EQ(MediaTime::indefiniteTime() + MediaTime::negativeInfiniteTime(), MediaTime::indefiniteTime());
  92. EXPECT_EQ(MediaTime::indefiniteTime() + MediaTime::indefiniteTime(), MediaTime::indefiniteTime());
  93. EXPECT_EQ(MediaTime::indefiniteTime() + MediaTime(1, 1), MediaTime::indefiniteTime());
  94. EXPECT_EQ(MediaTime(1, 1) + MediaTime(1, 1), MediaTime(2, 1));
  95. EXPECT_EQ(MediaTime(1, 2) + MediaTime(1, 3), MediaTime(5, 6));
  96. EXPECT_EQ(MediaTime(1, numeric_limits<int32_t>::max()-1) + MediaTime(1, numeric_limits<int32_t>::max()-2), MediaTime(2, numeric_limits<int32_t>::max()));
  97. // Subtraction Operators
  98. EXPECT_EQ(MediaTime::positiveInfiniteTime() - MediaTime::positiveInfiniteTime(), MediaTime::invalidTime());
  99. EXPECT_EQ(MediaTime::negativeInfiniteTime() - MediaTime::negativeInfiniteTime(), MediaTime::invalidTime());
  100. EXPECT_EQ(MediaTime::positiveInfiniteTime() - MediaTime::negativeInfiniteTime(), MediaTime::positiveInfiniteTime());
  101. EXPECT_EQ(MediaTime::negativeInfiniteTime() - MediaTime::positiveInfiniteTime(), MediaTime::negativeInfiniteTime());
  102. EXPECT_EQ(MediaTime::invalidTime() - MediaTime::positiveInfiniteTime(), MediaTime::invalidTime());
  103. EXPECT_EQ(MediaTime::invalidTime() - MediaTime::negativeInfiniteTime(), MediaTime::invalidTime());
  104. EXPECT_EQ(MediaTime::invalidTime() - MediaTime::invalidTime(), MediaTime::invalidTime());
  105. EXPECT_EQ(MediaTime::invalidTime() - MediaTime(1, 1), MediaTime::invalidTime());
  106. EXPECT_EQ(MediaTime::indefiniteTime() - MediaTime::positiveInfiniteTime(), MediaTime::indefiniteTime());
  107. EXPECT_EQ(MediaTime::indefiniteTime() - MediaTime::negativeInfiniteTime(), MediaTime::indefiniteTime());
  108. EXPECT_EQ(MediaTime::indefiniteTime() - MediaTime::indefiniteTime(), MediaTime::indefiniteTime());
  109. EXPECT_EQ(MediaTime::indefiniteTime() - MediaTime(1, 1), MediaTime::indefiniteTime());
  110. EXPECT_EQ(MediaTime(3, 1) - MediaTime(2, 1), MediaTime(1, 1));
  111. EXPECT_EQ(MediaTime(1, 2) - MediaTime(1, 3), MediaTime(1, 6));
  112. EXPECT_EQ(MediaTime(2, numeric_limits<int32_t>::max()-1) - MediaTime(1, numeric_limits<int32_t>::max()-2), MediaTime(1, numeric_limits<int32_t>::max()));
  113. // Constants
  114. EXPECT_EQ(MediaTime::zeroTime(), MediaTime(0, 1));
  115. EXPECT_EQ(MediaTime::invalidTime(), MediaTime(-1, 1, 0));
  116. EXPECT_EQ(MediaTime::positiveInfiniteTime(), MediaTime(0, 1, MediaTime::PositiveInfinite));
  117. EXPECT_EQ(MediaTime::negativeInfiniteTime(), MediaTime(0, 1, MediaTime::NegativeInfinite));
  118. EXPECT_EQ(MediaTime::indefiniteTime(), MediaTime(0, 1, MediaTime::Indefinite));
  119. // Absolute Functions
  120. EXPECT_EQ(abs(MediaTime::positiveInfiniteTime()), MediaTime::positiveInfiniteTime());
  121. EXPECT_EQ(abs(MediaTime::negativeInfiniteTime()), MediaTime::positiveInfiniteTime());
  122. EXPECT_EQ(abs(MediaTime::invalidTime()), MediaTime::invalidTime());
  123. EXPECT_EQ(abs(MediaTime(1, 1)), MediaTime(1, 1));
  124. EXPECT_EQ(abs(MediaTime(-1, 1)), MediaTime(1, 1));
  125. EXPECT_EQ(abs(MediaTime(-1, -1)), MediaTime(-1, -1));
  126. EXPECT_EQ(abs(MediaTime(1, -1)), MediaTime(-1, -1));
  127. // Floating Point Functions
  128. EXPECT_EQ(MediaTime::createWithFloat(1.0f), MediaTime(1, 1));
  129. EXPECT_EQ(MediaTime::createWithFloat(1.5f), MediaTime(3, 2));
  130. EXPECT_EQ(MediaTime::createWithDouble(1.0), MediaTime(1, 1));
  131. EXPECT_EQ(MediaTime::createWithDouble(1.5), MediaTime(3, 2));
  132. EXPECT_EQ(MediaTime(1, 1).toFloat(), 1.0f);
  133. EXPECT_EQ(MediaTime(3, 2).toFloat(), 1.5f);
  134. EXPECT_EQ(MediaTime(1, 1).toDouble(), 1.0);
  135. EXPECT_EQ(MediaTime(3, 2).toDouble(), 1.5);
  136. EXPECT_EQ(MediaTime(1, 1 << 16).toFloat(), 1 / pow(2.0f, 16.0f));
  137. EXPECT_EQ(MediaTime(1, 1 << 30).toDouble(), 1 / pow(2.0, 30.0));
  138. EXPECT_EQ(MediaTime::createWithDouble(M_PI, 1 << 30), MediaTime(3373259426U, 1 << 30));
  139. EXPECT_EQ(MediaTime::createWithFloat(INFINITY), MediaTime::positiveInfiniteTime());
  140. EXPECT_EQ(MediaTime::createWithFloat(-INFINITY), MediaTime::negativeInfiniteTime());
  141. EXPECT_EQ(MediaTime::createWithFloat(NAN), MediaTime::invalidTime());
  142. EXPECT_EQ(MediaTime::createWithDouble(INFINITY), MediaTime::positiveInfiniteTime());
  143. EXPECT_EQ(MediaTime::createWithDouble(-INFINITY), MediaTime::negativeInfiniteTime());
  144. EXPECT_EQ(MediaTime::createWithDouble(NAN), MediaTime::invalidTime());
  145. // Overflow Behavior
  146. EXPECT_EQ(MediaTime::createWithFloat(pow(2.0f, 64.0f)), MediaTime::positiveInfiniteTime());
  147. EXPECT_EQ(MediaTime::createWithFloat(-pow(2.0f, 64.0f)), MediaTime::negativeInfiniteTime());
  148. EXPECT_EQ(MediaTime::createWithFloat(pow(2.0f, 63.0f), 2).timeScale(), 1);
  149. EXPECT_EQ(MediaTime::createWithFloat(pow(2.0f, 63.0f), 3).timeScale(), 1);
  150. EXPECT_EQ(MediaTime::createWithDouble(pow(2.0, 64.0)), MediaTime::positiveInfiniteTime());
  151. EXPECT_EQ(MediaTime::createWithDouble(-pow(2.0, 64.0)), MediaTime::negativeInfiniteTime());
  152. EXPECT_EQ(MediaTime::createWithDouble(pow(2.0, 63.0), 2).timeScale(), 1);
  153. EXPECT_EQ(MediaTime::createWithDouble(pow(2.0, 63.0), 3).timeScale(), 1);
  154. EXPECT_EQ((MediaTime(numeric_limits<int64_t>::max(), 2) + MediaTime(numeric_limits<int64_t>::max(), 2)).timeScale(), 1);
  155. EXPECT_EQ((MediaTime(numeric_limits<int64_t>::min(), 2) - MediaTime(numeric_limits<int64_t>::max(), 2)).timeScale(), 1);
  156. EXPECT_EQ(MediaTime(numeric_limits<int64_t>::max(), 1) + MediaTime(numeric_limits<int64_t>::max(), 1), MediaTime::positiveInfiniteTime());
  157. EXPECT_EQ(MediaTime(numeric_limits<int64_t>::min(), 1) + MediaTime(numeric_limits<int64_t>::min(), 1), MediaTime::negativeInfiniteTime());
  158. EXPECT_EQ(MediaTime(numeric_limits<int64_t>::min(), 1) - MediaTime(numeric_limits<int64_t>::max(), 1), MediaTime::negativeInfiniteTime());
  159. EXPECT_EQ(MediaTime(numeric_limits<int64_t>::max(), 1) - MediaTime(numeric_limits<int64_t>::min(), 1), MediaTime::positiveInfiniteTime());
  160. }
  161. }