CheckedArithmeticOperations.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2011 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. * 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/CheckedArithmetic.h>
  27. namespace TestWebKitAPI {
  28. #define CheckedArithmeticTest(type, coerceLiteral, MixedSignednessTest) \
  29. TEST(WTF, Checked_##type) \
  30. { \
  31. Checked<type, RecordOverflow> value; \
  32. EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \
  33. EXPECT_EQ(std::numeric_limits<type>::max(), (value + std::numeric_limits<type>::max()).unsafeGet()); \
  34. EXPECT_EQ(std::numeric_limits<type>::max(), (std::numeric_limits<type>::max() + value).unsafeGet()); \
  35. EXPECT_EQ(std::numeric_limits<type>::min(), (value + std::numeric_limits<type>::min()).unsafeGet()); \
  36. EXPECT_EQ(std::numeric_limits<type>::min(), (std::numeric_limits<type>::min() + value).unsafeGet()); \
  37. EXPECT_EQ(coerceLiteral(0), (value * coerceLiteral(0)).unsafeGet()); \
  38. EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) * value).unsafeGet()); \
  39. EXPECT_EQ(coerceLiteral(0), (value * value).unsafeGet()); \
  40. EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(0)).unsafeGet()); \
  41. EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) - value).unsafeGet()); \
  42. EXPECT_EQ(coerceLiteral(0), (value - value).unsafeGet()); \
  43. EXPECT_EQ(coerceLiteral(0), (value++).unsafeGet()); \
  44. EXPECT_EQ(coerceLiteral(1), (value--).unsafeGet()); \
  45. EXPECT_EQ(coerceLiteral(1), (++value).unsafeGet()); \
  46. EXPECT_EQ(coerceLiteral(0), (--value).unsafeGet()); \
  47. EXPECT_EQ(coerceLiteral(10), (value += coerceLiteral(10)).unsafeGet()); \
  48. EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
  49. EXPECT_EQ(coerceLiteral(100), (value *= coerceLiteral(10)).unsafeGet()); \
  50. EXPECT_EQ(coerceLiteral(100), value.unsafeGet()); \
  51. EXPECT_EQ(coerceLiteral(0), (value -= coerceLiteral(100)).unsafeGet()); \
  52. EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \
  53. value = 10; \
  54. EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
  55. EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(10)).unsafeGet()); \
  56. EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
  57. value = std::numeric_limits<type>::min(); \
  58. EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - coerceLiteral(1))).hasOverflowed()); \
  59. EXPECT_EQ(true, !((value--).hasOverflowed())); \
  60. EXPECT_EQ(true, value.hasOverflowed()); \
  61. value = std::numeric_limits<type>::max(); \
  62. EXPECT_EQ(true, !value.hasOverflowed()); \
  63. EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + coerceLiteral(1))).hasOverflowed()); \
  64. EXPECT_EQ(true, !(value++).hasOverflowed()); \
  65. EXPECT_EQ(true, value.hasOverflowed()); \
  66. value = std::numeric_limits<type>::max(); \
  67. EXPECT_EQ(true, (value += coerceLiteral(1)).hasOverflowed()); \
  68. EXPECT_EQ(true, value.hasOverflowed()); \
  69. value = 10; \
  70. type _value = 0; \
  71. EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(0)).safeGet(_value)); \
  72. _value = 0; \
  73. EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(0) * value).safeGet(_value)); \
  74. _value = 0; \
  75. EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
  76. _value = 0; \
  77. EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \
  78. value = 0; \
  79. _value = 0; \
  80. EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
  81. _value = 0; \
  82. EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \
  83. value = 1; \
  84. _value = 0; \
  85. EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
  86. _value = 0; \
  87. EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \
  88. _value = 0; \
  89. value = 0; \
  90. EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
  91. _value = 0; \
  92. EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)0).safeGet(_value)); \
  93. _value = 0; \
  94. value = 1; \
  95. EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
  96. _value = 0; \
  97. EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)1).safeGet(_value)); \
  98. _value = 0; \
  99. value = 2; \
  100. EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
  101. _value = 0; \
  102. EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)2).safeGet(_value)); \
  103. value = 10; \
  104. EXPECT_EQ(true, (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).hasOverflowed()); \
  105. MixedSignednessTest(EXPECT_EQ(coerceLiteral(0), (value + -10).unsafeGet())); \
  106. MixedSignednessTest(EXPECT_EQ(0U, (value - 10U).unsafeGet())); \
  107. MixedSignednessTest(EXPECT_EQ(coerceLiteral(0), (-10 + value).unsafeGet())); \
  108. MixedSignednessTest(EXPECT_EQ(0U, (10U - value).unsafeGet())); \
  109. value = std::numeric_limits<type>::min(); \
  110. MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - 1)).hasOverflowed())); \
  111. MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \
  112. MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
  113. value = std::numeric_limits<type>::max(); \
  114. MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \
  115. MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1)).hasOverflowed())); \
  116. MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \
  117. MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
  118. value = std::numeric_limits<type>::max(); \
  119. MixedSignednessTest(EXPECT_EQ(true, (value += 1).hasOverflowed())); \
  120. MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
  121. value = std::numeric_limits<type>::min(); \
  122. MixedSignednessTest(EXPECT_EQ(true, (value - 1U).hasOverflowed())); \
  123. MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \
  124. MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
  125. value = std::numeric_limits<type>::max(); \
  126. MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \
  127. MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1U)).hasOverflowed())); \
  128. MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \
  129. MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
  130. value = std::numeric_limits<type>::max(); \
  131. MixedSignednessTest(EXPECT_EQ(true, (value += 1U).hasOverflowed())); \
  132. MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
  133. }
  134. #define CoerceLiteralToUnsigned(x) x##U
  135. #define CoerceLiteralNop(x) x
  136. #define AllowMixedSignednessTest(x) x
  137. #define IgnoreMixedSignednessTest(x)
  138. CheckedArithmeticTest(int8_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
  139. CheckedArithmeticTest(int16_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
  140. CheckedArithmeticTest(int32_t, CoerceLiteralNop, AllowMixedSignednessTest)
  141. CheckedArithmeticTest(uint32_t, CoerceLiteralToUnsigned, AllowMixedSignednessTest)
  142. CheckedArithmeticTest(int64_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
  143. CheckedArithmeticTest(uint64_t, CoerceLiteralToUnsigned, IgnoreMixedSignednessTest)
  144. } // namespace TestWebKitAPI