Chromium Code Reviews| 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/safe_numerics.h" | |
| 10 | |
| 11 TEST(SafeNumerics, NumericCast) { | |
|
darin (slow to review)
2013/01/15 18:53:29
nit: put this TEST inside the base namespace so th
scottmg
2013/01/15 19:30:17
Done.
| |
| 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 EXPECT_TRUE(base::IsNumericCastableTo<signed short>(small_positive)); | |
| 23 EXPECT_TRUE(base::IsNumericCastableTo<signed short>(small_negative)); | |
| 24 | |
| 25 // Narrow unsigned destination. | |
| 26 EXPECT_TRUE(base::IsNumericCastableTo<unsigned char>(small_positive)); | |
| 27 EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(small_negative)); | |
| 28 EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(large_positive)); | |
| 29 EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(large_negative)); | |
| 30 EXPECT_FALSE(base::IsNumericCastableTo<unsigned short>(small_negative)); | |
| 31 EXPECT_FALSE(base::IsNumericCastableTo<unsigned short>(large_negative)); | |
| 32 | |
| 33 // Same width signed destination. | |
| 34 EXPECT_TRUE(base::IsNumericCastableTo<signed int>(small_positive)); | |
| 35 EXPECT_TRUE(base::IsNumericCastableTo<signed int>(small_negative)); | |
| 36 EXPECT_TRUE(base::IsNumericCastableTo<signed int>(large_positive)); | |
| 37 EXPECT_TRUE(base::IsNumericCastableTo<signed int>(large_negative)); | |
| 38 | |
| 39 // Same width unsigned destination. | |
| 40 EXPECT_TRUE(base::IsNumericCastableTo<unsigned int>(small_positive)); | |
| 41 EXPECT_FALSE(base::IsNumericCastableTo<unsigned int>(small_negative)); | |
| 42 EXPECT_TRUE(base::IsNumericCastableTo<unsigned int>(large_positive)); | |
| 43 EXPECT_FALSE(base::IsNumericCastableTo<unsigned int>(large_negative)); | |
| 44 | |
| 45 // Wider signed destination. | |
| 46 EXPECT_TRUE(base::IsNumericCastableTo<long long>(small_positive)); | |
| 47 EXPECT_TRUE(base::IsNumericCastableTo<long long>(large_negative)); | |
| 48 EXPECT_TRUE(base::IsNumericCastableTo<long long>(small_positive)); | |
| 49 EXPECT_TRUE(base::IsNumericCastableTo<long long>(large_negative)); | |
| 50 | |
| 51 // Wider unsigned destination. | |
| 52 EXPECT_TRUE(base::IsNumericCastableTo<unsigned long long>(small_positive)); | |
| 53 EXPECT_FALSE(base::IsNumericCastableTo<unsigned long long>(small_negative)); | |
| 54 EXPECT_TRUE(base::IsNumericCastableTo<unsigned long long>(large_positive)); | |
| 55 EXPECT_FALSE(base::IsNumericCastableTo<unsigned long long>(large_negative)); | |
| 56 | |
| 57 // Negative to size_t. | |
| 58 EXPECT_FALSE(base::IsNumericCastableTo<size_t>(small_negative)); | |
| 59 EXPECT_FALSE(base::IsNumericCastableTo<size_t>(large_negative)); | |
| 60 | |
| 61 // Various edge cases. | |
| 62 EXPECT_TRUE(base::IsNumericCastableTo<int>(static_cast<short>(SHRT_MIN))); | |
| 63 EXPECT_FALSE( | |
| 64 base::IsNumericCastableTo<unsigned short>(static_cast<short>(SHRT_MIN))); | |
| 65 EXPECT_FALSE(base::IsNumericCastableTo<unsigned short>(SHRT_MIN)); | |
| 66 | |
| 67 // These shouldn't compile. | |
| 68 // EXPECT_TRUE(base::IsNumericCastableTo<float>(0.0)); | |
|
darin (slow to review)
2013/01/15 18:53:29
I think there is support for testing that certain
scottmg
2013/01/15 19:30:17
Looks like they've been disabled for a while unfor
| |
| 69 // EXPECT_TRUE(base::IsNumericCastableTo<double>(0.0f)); | |
| 70 // EXPECT_FALSE(base::IsNumericCastableTo<int>(DBL_MAX)); | |
| 71 | |
| 72 // Confirm that numeric_cast<> actually compiles. | |
| 73 std::vector<int> v; | |
| 74 unsigned int checked_size = base::numeric_cast<unsigned int>(v.size()); | |
| 75 EXPECT_EQ(0, checked_size); | |
| 76 } | |
| OLD | NEW |