| 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 "base/safe_numerics.h" | 5 #include "base/numerics/safe_conversions.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 namespace internal { | 15 namespace internal { |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 #pragma warning(disable : 4756) | 251 #pragma warning(disable : 4756) |
| 252 #endif | 252 #endif |
| 253 | 253 |
| 254 int small_positive = 1; | 254 int small_positive = 1; |
| 255 int small_negative = -1; | 255 int small_negative = -1; |
| 256 double double_small = 1.0; | 256 double double_small = 1.0; |
| 257 double double_large = std::numeric_limits<double>::max(); | 257 double double_large = std::numeric_limits<double>::max(); |
| 258 double double_infinity = std::numeric_limits<float>::infinity(); | 258 double double_infinity = std::numeric_limits<float>::infinity(); |
| 259 | 259 |
| 260 // Just test that the cast compiles, since the other tests cover logic. | 260 // Just test that the cast compiles, since the other tests cover logic. |
| 261 EXPECT_EQ(0, base::checked_numeric_cast<int>(static_cast<size_t>(0))); | 261 EXPECT_EQ(0, base::checked_cast<int>(static_cast<size_t>(0))); |
| 262 | 262 |
| 263 // Test various saturation corner cases. | 263 // Test various saturation corner cases. |
| 264 EXPECT_EQ(saturated_cast<int>(small_negative), | 264 EXPECT_EQ(saturated_cast<int>(small_negative), |
| 265 static_cast<int>(small_negative)); | 265 static_cast<int>(small_negative)); |
| 266 EXPECT_EQ(saturated_cast<int>(small_positive), | 266 EXPECT_EQ(saturated_cast<int>(small_positive), |
| 267 static_cast<int>(small_positive)); | 267 static_cast<int>(small_positive)); |
| 268 EXPECT_EQ(saturated_cast<unsigned>(small_negative), | 268 EXPECT_EQ(saturated_cast<unsigned>(small_negative), |
| 269 static_cast<unsigned>(0)); | 269 static_cast<unsigned>(0)); |
| 270 EXPECT_EQ(saturated_cast<int>(double_small), | 270 EXPECT_EQ(saturated_cast<int>(double_small), |
| 271 static_cast<int>(double_small)); | 271 static_cast<int>(double_small)); |
| 272 EXPECT_EQ(saturated_cast<int>(double_large), | 272 EXPECT_EQ(saturated_cast<int>(double_large), |
| 273 std::numeric_limits<int>::max()); | 273 std::numeric_limits<int>::max()); |
| 274 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); | 274 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); |
| 275 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); | 275 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
| 276 } | 276 } |
| 277 | 277 |
| 278 } // namespace internal | 278 } // namespace internal |
| 279 } // namespace base | 279 } // namespace base |
| OLD | NEW |