MathExtras.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2012 Intel Corporation
  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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include <wtf/MathExtras.h>
  27. namespace TestWebKitAPI {
  28. TEST(WTF, Lrint)
  29. {
  30. EXPECT_EQ(lrint(-7.5), -8);
  31. EXPECT_EQ(lrint(-8.5), -8);
  32. EXPECT_EQ(lrint(-0.5), 0);
  33. EXPECT_EQ(lrint(0.5), 0);
  34. EXPECT_EQ(lrint(-0.5), 0);
  35. EXPECT_EQ(lrint(1.3), 1);
  36. EXPECT_EQ(lrint(1.7), 2);
  37. EXPECT_EQ(lrint(0), 0);
  38. EXPECT_EQ(lrint(-0), 0);
  39. if (sizeof(long int) == 8) {
  40. // Largest double number with 0.5 precision and one halfway rounding case below.
  41. EXPECT_EQ(lrint(pow(2.0, 52) - 0.5), pow(2.0, 52));
  42. EXPECT_EQ(lrint(pow(2.0, 52) - 1.5), pow(2.0, 52) - 2);
  43. // Smallest double number with 0.5 precision and one halfway rounding case above.
  44. EXPECT_EQ(lrint(-pow(2.0, 52) + 0.5), -pow(2.0, 52));
  45. EXPECT_EQ(lrint(-pow(2.0, 52) + 1.5), -pow(2.0, 52) + 2);
  46. }
  47. }
  48. TEST(WTF, clampToIntLong)
  49. {
  50. if (sizeof(long) == sizeof(int))
  51. return;
  52. long maxInt = std::numeric_limits<int>::max();
  53. long minInt = std::numeric_limits<int>::min();
  54. long overflowInt = maxInt + 1;
  55. long underflowInt = minInt - 1;
  56. EXPECT_GT(overflowInt, maxInt);
  57. EXPECT_LT(underflowInt, minInt);
  58. EXPECT_EQ(clampTo<int>(maxInt), maxInt);
  59. EXPECT_EQ(clampTo<int>(minInt), minInt);
  60. EXPECT_EQ(clampTo<int>(overflowInt), maxInt);
  61. EXPECT_EQ(clampTo<int>(underflowInt), minInt);
  62. }
  63. TEST(WTF, clampToIntLongLong)
  64. {
  65. long long maxInt = std::numeric_limits<int>::max();
  66. long long minInt = std::numeric_limits<int>::min();
  67. long long overflowInt = maxInt + 1;
  68. long long underflowInt = minInt - 1;
  69. EXPECT_GT(overflowInt, maxInt);
  70. EXPECT_LT(underflowInt, minInt);
  71. EXPECT_EQ(clampTo<int>(maxInt), maxInt);
  72. EXPECT_EQ(clampTo<int>(minInt), minInt);
  73. EXPECT_EQ(clampTo<int>(overflowInt), maxInt);
  74. EXPECT_EQ(clampTo<int>(underflowInt), minInt);
  75. }
  76. TEST(WTF, clampToIntegerFloat)
  77. {
  78. // This test is inaccurate as floats will round the min / max integer
  79. // due to the narrow mantissa. However it will properly checks within
  80. // (close to the extreme) and outside the integer range.
  81. float maxInt = std::numeric_limits<int>::max();
  82. float minInt = std::numeric_limits<int>::min();
  83. float overflowInt = maxInt * 1.1;
  84. float underflowInt = minInt * 1.1;
  85. EXPECT_GT(overflowInt, maxInt);
  86. EXPECT_LT(underflowInt, minInt);
  87. // If maxInt == 2^31 - 1 (ie on I32 architecture), the closest float used to represent it is 2^31.
  88. EXPECT_NEAR(clampToInteger(maxInt), maxInt, 1);
  89. EXPECT_EQ(clampToInteger(minInt), minInt);
  90. EXPECT_NEAR(clampToInteger(overflowInt), maxInt, 1);
  91. EXPECT_EQ(clampToInteger(underflowInt), minInt);
  92. }
  93. TEST(WTF, clampToIntegerDouble)
  94. {
  95. double maxInt = std::numeric_limits<int>::max();
  96. double minInt = std::numeric_limits<int>::min();
  97. double overflowInt = maxInt + 1;
  98. double underflowInt = minInt - 1;
  99. EXPECT_GT(overflowInt, maxInt);
  100. EXPECT_LT(underflowInt, minInt);
  101. EXPECT_EQ(clampToInteger(maxInt), maxInt);
  102. EXPECT_EQ(clampToInteger(minInt), minInt);
  103. EXPECT_EQ(clampToInteger(overflowInt), maxInt);
  104. EXPECT_EQ(clampToInteger(underflowInt), minInt);
  105. }
  106. TEST(WTF, clampToFloat)
  107. {
  108. double maxFloat = std::numeric_limits<float>::max();
  109. double minFloat = -maxFloat;
  110. double overflowFloat = maxFloat * 1.1;
  111. double underflowFloat = minFloat * 1.1;
  112. EXPECT_GT(overflowFloat, maxFloat);
  113. EXPECT_LT(underflowFloat, minFloat);
  114. EXPECT_EQ(clampToFloat(maxFloat), maxFloat);
  115. EXPECT_EQ(clampToFloat(minFloat), minFloat);
  116. EXPECT_EQ(clampToFloat(overflowFloat), maxFloat);
  117. EXPECT_EQ(clampToFloat(underflowFloat), minFloat);
  118. EXPECT_EQ(clampToFloat(std::numeric_limits<float>::infinity()), maxFloat);
  119. EXPECT_EQ(clampToFloat(-std::numeric_limits<float>::infinity()), minFloat);
  120. }
  121. TEST(WTF, clampToUnsignedLong)
  122. {
  123. if (sizeof(unsigned long) == sizeof(unsigned))
  124. return;
  125. unsigned long maxUnsigned = std::numeric_limits<unsigned>::max();
  126. unsigned long overflowUnsigned = maxUnsigned + 1;
  127. EXPECT_GT(overflowUnsigned, maxUnsigned);
  128. EXPECT_EQ(clampTo<unsigned>(maxUnsigned), maxUnsigned);
  129. EXPECT_EQ(clampTo<unsigned>(overflowUnsigned), maxUnsigned);
  130. EXPECT_EQ(clampTo<unsigned>(-1), 0u);
  131. }
  132. TEST(WTF, clampToUnsignedLongLong)
  133. {
  134. unsigned long long maxUnsigned = std::numeric_limits<unsigned>::max();
  135. unsigned long long overflowUnsigned = maxUnsigned + 1;
  136. EXPECT_GT(overflowUnsigned, maxUnsigned);
  137. EXPECT_EQ(clampTo<unsigned>(maxUnsigned), maxUnsigned);
  138. EXPECT_EQ(clampTo<unsigned>(overflowUnsigned), maxUnsigned);
  139. EXPECT_EQ(clampTo<unsigned>(-1), 0u);
  140. }
  141. } // namespace TestWebKitAPI