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> | 5 #include <limits> |
6 | 6 |
7 #include "src/base/bits.h" | 7 #include "src/base/bits.h" |
8 #include "src/base/macros.h" | 8 #include "src/base/macros.h" |
9 #include "testing/gtest-support.h" | 9 #include "testing/gtest-support.h" |
10 | 10 |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 EXPECT_EQ(0, SignedMod32(i, 0)); | 248 EXPECT_EQ(0, SignedMod32(i, 0)); |
249 TRACED_FORRANGE(int32_t, j, 1, i) { | 249 TRACED_FORRANGE(int32_t, j, 1, i) { |
250 EXPECT_EQ(0, SignedMod32(j, j)); | 250 EXPECT_EQ(0, SignedMod32(j, j)); |
251 EXPECT_EQ(i % j, SignedMod32(i, j)); | 251 EXPECT_EQ(i % j, SignedMod32(i, j)); |
252 EXPECT_EQ(i % j, SignedMod32(i, -j)); | 252 EXPECT_EQ(i % j, SignedMod32(i, -j)); |
253 } | 253 } |
254 } | 254 } |
255 } | 255 } |
256 | 256 |
257 | 257 |
| 258 TEST(Bits, UnsignedAddOverflow32) { |
| 259 uint32_t val = 0; |
| 260 EXPECT_FALSE(UnsignedAddOverflow32(0, 0, &val)); |
| 261 EXPECT_EQ(0u, val); |
| 262 EXPECT_TRUE( |
| 263 UnsignedAddOverflow32(std::numeric_limits<uint32_t>::max(), 1u, &val)); |
| 264 EXPECT_EQ(std::numeric_limits<uint32_t>::min(), val); |
| 265 EXPECT_TRUE(UnsignedAddOverflow32(std::numeric_limits<uint32_t>::max(), |
| 266 std::numeric_limits<uint32_t>::max(), |
| 267 &val)); |
| 268 TRACED_FORRANGE(uint32_t, i, 1, 50) { |
| 269 TRACED_FORRANGE(uint32_t, j, 1, i) { |
| 270 EXPECT_FALSE(UnsignedAddOverflow32(i, j, &val)); |
| 271 EXPECT_EQ(i + j, val); |
| 272 } |
| 273 } |
| 274 } |
| 275 |
| 276 |
258 TEST(Bits, UnsignedDiv32) { | 277 TEST(Bits, UnsignedDiv32) { |
259 TRACED_FORRANGE(uint32_t, i, 0, 50) { | 278 TRACED_FORRANGE(uint32_t, i, 0, 50) { |
260 EXPECT_EQ(0u, UnsignedDiv32(i, 0)); | 279 EXPECT_EQ(0u, UnsignedDiv32(i, 0)); |
261 TRACED_FORRANGE(uint32_t, j, i + 1, 100) { | 280 TRACED_FORRANGE(uint32_t, j, i + 1, 100) { |
262 EXPECT_EQ(1u, UnsignedDiv32(j, j)); | 281 EXPECT_EQ(1u, UnsignedDiv32(j, j)); |
263 EXPECT_EQ(i / j, UnsignedDiv32(i, j)); | 282 EXPECT_EQ(i / j, UnsignedDiv32(i, j)); |
264 } | 283 } |
265 } | 284 } |
266 } | 285 } |
267 | 286 |
268 | 287 |
269 TEST(Bits, UnsignedMod32) { | 288 TEST(Bits, UnsignedMod32) { |
270 TRACED_FORRANGE(uint32_t, i, 0, 50) { | 289 TRACED_FORRANGE(uint32_t, i, 0, 50) { |
271 EXPECT_EQ(0u, UnsignedMod32(i, 0)); | 290 EXPECT_EQ(0u, UnsignedMod32(i, 0)); |
272 TRACED_FORRANGE(uint32_t, j, i + 1, 100) { | 291 TRACED_FORRANGE(uint32_t, j, i + 1, 100) { |
273 EXPECT_EQ(0u, UnsignedMod32(j, j)); | 292 EXPECT_EQ(0u, UnsignedMod32(j, j)); |
274 EXPECT_EQ(i % j, UnsignedMod32(i, j)); | 293 EXPECT_EQ(i % j, UnsignedMod32(i, j)); |
275 } | 294 } |
276 } | 295 } |
277 } | 296 } |
278 | 297 |
279 } // namespace bits | 298 } // namespace bits |
280 } // namespace base | 299 } // namespace base |
281 } // namespace v8 | 300 } // namespace v8 |
OLD | NEW |