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 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 5 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
6 #include <mmintrin.h> | 6 #include <mmintrin.h> |
7 #endif | 7 #endif |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
14 #include "base/numerics/safe_math.h" | 14 #include "base/numerics/safe_math.h" |
15 #include "base/template_util.h" | 15 #include "base/template_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 using std::numeric_limits; | 18 using std::numeric_limits; |
19 using base::CheckedNumeric; | 19 using base::CheckedNumeric; |
20 using base::checked_cast; | 20 using base::checked_cast; |
| 21 using base::SSizeT; |
| 22 using base::StrictNumeric; |
21 using base::saturated_cast; | 23 using base::saturated_cast; |
| 24 using base::strict_cast; |
22 using base::internal::MaxExponent; | 25 using base::internal::MaxExponent; |
23 using base::internal::RANGE_VALID; | 26 using base::internal::RANGE_VALID; |
24 using base::internal::RANGE_INVALID; | 27 using base::internal::RANGE_INVALID; |
25 using base::internal::RANGE_OVERFLOW; | 28 using base::internal::RANGE_OVERFLOW; |
26 using base::internal::RANGE_UNDERFLOW; | 29 using base::internal::RANGE_UNDERFLOW; |
27 using base::enable_if; | 30 using base::enable_if; |
28 | 31 |
29 // These tests deliberately cause arithmetic overflows. If the compiler is | 32 // These tests deliberately cause arithmetic overflows. If the compiler is |
30 // aggressive enough, it can const fold these overflows. Disable warnings about | 33 // aggressive enough, it can const fold these overflows. Disable warnings about |
31 // overflows for const expressions. | 34 // overflows for const expressions. |
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 #if defined(COMPILER_MSVC) | 556 #if defined(COMPILER_MSVC) |
554 #pragma warning(disable : 4756) | 557 #pragma warning(disable : 4756) |
555 #endif | 558 #endif |
556 | 559 |
557 int small_positive = 1; | 560 int small_positive = 1; |
558 int small_negative = -1; | 561 int small_negative = -1; |
559 double double_small = 1.0; | 562 double double_small = 1.0; |
560 double double_large = numeric_limits<double>::max(); | 563 double double_large = numeric_limits<double>::max(); |
561 double double_infinity = numeric_limits<float>::infinity(); | 564 double double_infinity = numeric_limits<float>::infinity(); |
562 | 565 |
563 // Just test that the cast compiles, since the other tests cover logic. | 566 // Just test that the casts compile, since the other tests cover logic. |
564 EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0))); | 567 EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0))); |
| 568 EXPECT_EQ(0, strict_cast<int>(static_cast<char>(0))); |
| 569 EXPECT_EQ(0, strict_cast<int>(static_cast<unsigned char>(0))); |
| 570 EXPECT_EQ(0U, strict_cast<unsigned>(static_cast<unsigned char>(0))); |
| 571 EXPECT_EQ(1ULL, static_cast<uint64_t>(StrictNumeric<size_t>(1U))); |
| 572 EXPECT_EQ(1ULL, static_cast<uint64_t>(SSizeT(1U))); |
| 573 EXPECT_EQ(1U, static_cast<size_t>(StrictNumeric<unsigned>(1U))); |
| 574 |
| 575 // These casts and coercions will fail to compile: |
| 576 // EXPECT_EQ(0, strict_cast<int>(static_cast<size_t>(0))); |
| 577 // EXPECT_EQ(0, strict_cast<size_t>(static_cast<int>(0))); |
| 578 // EXPECT_EQ(1ULL, StrictNumeric<size_t>(1)); |
| 579 // EXPECT_EQ(1, StrictNumeric<size_t>(1U)); |
565 | 580 |
566 // Test various saturation corner cases. | 581 // Test various saturation corner cases. |
567 EXPECT_EQ(saturated_cast<int>(small_negative), | 582 EXPECT_EQ(saturated_cast<int>(small_negative), |
568 static_cast<int>(small_negative)); | 583 static_cast<int>(small_negative)); |
569 EXPECT_EQ(saturated_cast<int>(small_positive), | 584 EXPECT_EQ(saturated_cast<int>(small_positive), |
570 static_cast<int>(small_positive)); | 585 static_cast<int>(small_positive)); |
571 EXPECT_EQ(saturated_cast<unsigned>(small_negative), | 586 EXPECT_EQ(saturated_cast<unsigned>(small_negative), |
572 static_cast<unsigned>(0)); | 587 static_cast<unsigned>(0)); |
573 EXPECT_EQ(saturated_cast<int>(double_small), | 588 EXPECT_EQ(saturated_cast<int>(double_small), |
574 static_cast<int>(double_small)); | 589 static_cast<int>(double_small)); |
575 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); | 590 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); |
576 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); | 591 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); |
577 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); | 592 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
578 } | 593 } |
579 | 594 |
OLD | NEW |