OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <limits> |
| 6 |
5 #include "src/base/bits.h" | 7 #include "src/base/bits.h" |
6 #include "src/base/macros.h" | 8 #include "src/base/macros.h" |
7 #include "testing/gtest-support.h" | 9 #include "testing/gtest-support.h" |
8 | 10 |
9 #ifdef DEBUG | 11 #ifdef DEBUG |
10 #define DISABLE_IN_RELEASE(Name) Name | 12 #define DISABLE_IN_RELEASE(Name) Name |
11 #else | 13 #else |
12 #define DISABLE_IN_RELEASE(Name) DISABLED_##Name | 14 #define DISABLE_IN_RELEASE(Name) DISABLED_##Name |
13 #endif | 15 #endif |
14 | 16 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 114 |
113 TEST(Bits, RotateRight64) { | 115 TEST(Bits, RotateRight64) { |
114 TRACED_FORRANGE(uint64_t, shift, 0, 63) { | 116 TRACED_FORRANGE(uint64_t, shift, 0, 63) { |
115 EXPECT_EQ(0u, RotateRight64(0u, shift)); | 117 EXPECT_EQ(0u, RotateRight64(0u, shift)); |
116 } | 118 } |
117 EXPECT_EQ(1u, RotateRight64(1, 0)); | 119 EXPECT_EQ(1u, RotateRight64(1, 0)); |
118 EXPECT_EQ(1u, RotateRight64(2, 1)); | 120 EXPECT_EQ(1u, RotateRight64(2, 1)); |
119 EXPECT_EQ(V8_UINT64_C(0x8000000000000000), RotateRight64(1, 1)); | 121 EXPECT_EQ(V8_UINT64_C(0x8000000000000000), RotateRight64(1, 1)); |
120 } | 122 } |
121 | 123 |
| 124 |
| 125 TEST(Bits, SignedAddOverflow32) { |
| 126 int32_t val = 0; |
| 127 EXPECT_FALSE(SignedAddOverflow32(0, 0, &val)); |
| 128 EXPECT_EQ(0, val); |
| 129 EXPECT_TRUE( |
| 130 SignedAddOverflow32(std::numeric_limits<int32_t>::max(), 1, &val)); |
| 131 EXPECT_EQ(std::numeric_limits<int32_t>::min(), val); |
| 132 EXPECT_TRUE( |
| 133 SignedAddOverflow32(std::numeric_limits<int32_t>::min(), -1, &val)); |
| 134 EXPECT_EQ(std::numeric_limits<int32_t>::max(), val); |
| 135 EXPECT_TRUE(SignedAddOverflow32(std::numeric_limits<int32_t>::max(), |
| 136 std::numeric_limits<int32_t>::max(), &val)); |
| 137 EXPECT_EQ(-2, val); |
| 138 TRACED_FORRANGE(int32_t, i, 1, 50) { |
| 139 TRACED_FORRANGE(int32_t, j, 1, i) { |
| 140 EXPECT_FALSE(SignedAddOverflow32(i, j, &val)); |
| 141 EXPECT_EQ(i + j, val); |
| 142 } |
| 143 } |
| 144 } |
| 145 |
| 146 |
| 147 TEST(Bits, SignedSubOverflow32) { |
| 148 int32_t val = 0; |
| 149 EXPECT_FALSE(SignedSubOverflow32(0, 0, &val)); |
| 150 EXPECT_EQ(0, val); |
| 151 EXPECT_TRUE( |
| 152 SignedSubOverflow32(std::numeric_limits<int32_t>::min(), 1, &val)); |
| 153 EXPECT_EQ(std::numeric_limits<int32_t>::max(), val); |
| 154 EXPECT_TRUE( |
| 155 SignedSubOverflow32(std::numeric_limits<int32_t>::max(), -1, &val)); |
| 156 EXPECT_EQ(std::numeric_limits<int32_t>::min(), val); |
| 157 TRACED_FORRANGE(int32_t, i, 1, 50) { |
| 158 TRACED_FORRANGE(int32_t, j, 1, i) { |
| 159 EXPECT_FALSE(SignedSubOverflow32(i, j, &val)); |
| 160 EXPECT_EQ(i - j, val); |
| 161 } |
| 162 } |
| 163 } |
| 164 |
122 } // namespace bits | 165 } // namespace bits |
123 } // namespace base | 166 } // namespace base |
124 } // namespace v8 | 167 } // namespace v8 |
OLD | NEW |