| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <type_traits> | 9 #include <type_traits> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 13 #include "base/numerics/safe_math.h" | 13 #include "base/numerics/safe_math.h" |
| 14 #include "base/test/gtest_util.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 18 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 18 #include <mmintrin.h> | 19 #include <mmintrin.h> |
| 19 #endif | 20 #endif |
| 20 | 21 |
| 21 using std::numeric_limits; | 22 using std::numeric_limits; |
| 22 using base::CheckedNumeric; | 23 using base::CheckedNumeric; |
| 23 using base::checked_cast; | 24 using base::checked_cast; |
| (...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); | 644 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
| 644 EXPECT_EQ(numeric_limits<int>::min(), saturated_cast<int>(double_small_int)); | 645 EXPECT_EQ(numeric_limits<int>::min(), saturated_cast<int>(double_small_int)); |
| 645 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); | 646 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); |
| 646 | 647 |
| 647 float not_a_number = std::numeric_limits<float>::infinity() - | 648 float not_a_number = std::numeric_limits<float>::infinity() - |
| 648 std::numeric_limits<float>::infinity(); | 649 std::numeric_limits<float>::infinity(); |
| 649 EXPECT_TRUE(std::isnan(not_a_number)); | 650 EXPECT_TRUE(std::isnan(not_a_number)); |
| 650 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); | 651 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); |
| 651 } | 652 } |
| 652 | 653 |
| 653 #if GTEST_HAS_DEATH_TEST | |
| 654 | |
| 655 TEST(SafeNumerics, SaturatedCastChecks) { | 654 TEST(SafeNumerics, SaturatedCastChecks) { |
| 656 float not_a_number = std::numeric_limits<float>::infinity() - | 655 float not_a_number = std::numeric_limits<float>::infinity() - |
| 657 std::numeric_limits<float>::infinity(); | 656 std::numeric_limits<float>::infinity(); |
| 658 EXPECT_TRUE(std::isnan(not_a_number)); | 657 EXPECT_TRUE(std::isnan(not_a_number)); |
| 659 EXPECT_DEATH((saturated_cast<int, base::SaturatedCastNaNBehaviorCheck>( | 658 EXPECT_DCHECK_DEATH( |
| 660 not_a_number)), ""); | 659 (saturated_cast<int, base::SaturatedCastNaNBehaviorCheck>(not_a_number)), |
| 660 ""); |
| 661 } | 661 } |
| 662 | 662 |
| 663 #endif // GTEST_HAS_DEATH_TEST | |
| 664 | |
| 665 TEST(SafeNumerics, IsValueInRangeForNumericType) { | 663 TEST(SafeNumerics, IsValueInRangeForNumericType) { |
| 666 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); | 664 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); |
| 667 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); | 665 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); |
| 668 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); | 666 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); |
| 669 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); | 667 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); |
| 670 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); | 668 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); |
| 671 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); | 669 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); |
| 672 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); | 670 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); |
| 673 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000001))); | 671 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000001))); |
| 674 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( | 672 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 | 751 |
| 754 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); | 752 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); |
| 755 EXPECT_TRUE(too_large.IsValid()); | 753 EXPECT_TRUE(too_large.IsValid()); |
| 756 too_large += d; | 754 too_large += d; |
| 757 EXPECT_FALSE(too_large.IsValid()); | 755 EXPECT_FALSE(too_large.IsValid()); |
| 758 too_large -= d; | 756 too_large -= d; |
| 759 EXPECT_FALSE(too_large.IsValid()); | 757 EXPECT_FALSE(too_large.IsValid()); |
| 760 too_large /= d; | 758 too_large /= d; |
| 761 EXPECT_FALSE(too_large.IsValid()); | 759 EXPECT_FALSE(too_large.IsValid()); |
| 762 } | 760 } |
| OLD | NEW |