OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <gtest/gtest.h> |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/numeric_cast.h" |
| 10 |
| 11 TEST(NumericCast, Test) { |
| 12 int small_positive = 1; |
| 13 int small_negative = -1; |
| 14 int large_positive = INT_MAX; |
| 15 int large_negative = INT_MIN; |
| 16 |
| 17 // Narrow signed destination. |
| 18 EXPECT_TRUE(base::IsNumericCastableTo<signed char>(small_positive)); |
| 19 EXPECT_TRUE(base::IsNumericCastableTo<signed char>(small_negative)); |
| 20 EXPECT_FALSE(base::IsNumericCastableTo<signed char>(large_positive)); |
| 21 EXPECT_FALSE(base::IsNumericCastableTo<signed char>(large_negative)); |
| 22 |
| 23 // Narrow unsigned destination. |
| 24 EXPECT_TRUE(base::IsNumericCastableTo<unsigned char>(small_positive)); |
| 25 EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(small_negative)); |
| 26 EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(large_positive)); |
| 27 EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(large_negative)); |
| 28 |
| 29 // Same width signed destination. |
| 30 EXPECT_TRUE(base::IsNumericCastableTo<signed int>(small_positive)); |
| 31 EXPECT_TRUE(base::IsNumericCastableTo<signed int>(small_negative)); |
| 32 EXPECT_TRUE(base::IsNumericCastableTo<signed int>(large_positive)); |
| 33 EXPECT_TRUE(base::IsNumericCastableTo<signed int>(large_negative)); |
| 34 |
| 35 // Same width unsigned destination. |
| 36 EXPECT_TRUE(base::IsNumericCastableTo<unsigned int>(small_positive)); |
| 37 EXPECT_FALSE(base::IsNumericCastableTo<unsigned int>(small_negative)); |
| 38 EXPECT_TRUE(base::IsNumericCastableTo<unsigned int>(large_positive)); |
| 39 EXPECT_FALSE(base::IsNumericCastableTo<unsigned int>(large_negative)); |
| 40 |
| 41 // Wider signed destination. |
| 42 EXPECT_TRUE(base::IsNumericCastableTo<long long>(small_positive)); |
| 43 EXPECT_TRUE(base::IsNumericCastableTo<long long>(large_negative)); |
| 44 EXPECT_TRUE(base::IsNumericCastableTo<long long>(small_positive)); |
| 45 EXPECT_TRUE(base::IsNumericCastableTo<long long>(large_negative)); |
| 46 |
| 47 // Wider unsigned destination. |
| 48 EXPECT_TRUE(base::IsNumericCastableTo<unsigned long long>(small_positive)); |
| 49 EXPECT_FALSE(base::IsNumericCastableTo<unsigned long long>(small_negative)); |
| 50 EXPECT_TRUE(base::IsNumericCastableTo<unsigned long long>(large_positive)); |
| 51 EXPECT_FALSE(base::IsNumericCastableTo<unsigned long long>(large_negative)); |
| 52 |
| 53 // Negative to size_t. |
| 54 EXPECT_FALSE(base::IsNumericCastableTo<size_t>(small_negative)); |
| 55 EXPECT_FALSE(base::IsNumericCastableTo<size_t>(large_negative)); |
| 56 |
| 57 // These shouldn't compile. |
| 58 // EXPECT_TRUE(base::IsNumericCastableTo<float>(0.0)); |
| 59 // EXPECT_TRUE(base::IsNumericCastableTo<double>(0.0f)); |
| 60 // EXPECT_FALSE(base::IsNumericCastableTo<int>(DBL_MAX)); |
| 61 |
| 62 // Confirm that numeric_cast<> actually compiles. |
| 63 std::vector<int> v; |
| 64 unsigned int checked_size = base::numeric_cast<unsigned int>(v.size()); |
| 65 EXPECT_EQ(0, checked_size); |
| 66 } |
OLD | NEW |