Index: base/safe_numerics_unittest.cc |
diff --git a/base/safe_numerics_unittest.cc b/base/safe_numerics_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6e97e1578e12c91ecf88f8403300ded57ac27740 |
--- /dev/null |
+++ b/base/safe_numerics_unittest.cc |
@@ -0,0 +1,76 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <gtest/gtest.h> |
+ |
+#include <vector> |
+ |
+#include "base/safe_numerics.h" |
+ |
+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.
|
+ int small_positive = 1; |
+ int small_negative = -1; |
+ int large_positive = INT_MAX; |
+ int large_negative = INT_MIN; |
+ |
+ // Narrow signed destination. |
+ EXPECT_TRUE(base::IsNumericCastableTo<signed char>(small_positive)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<signed char>(small_negative)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<signed char>(large_positive)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<signed char>(large_negative)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<signed short>(small_positive)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<signed short>(small_negative)); |
+ |
+ // Narrow unsigned destination. |
+ EXPECT_TRUE(base::IsNumericCastableTo<unsigned char>(small_positive)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(small_negative)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(large_positive)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(large_negative)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned short>(small_negative)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned short>(large_negative)); |
+ |
+ // Same width signed destination. |
+ EXPECT_TRUE(base::IsNumericCastableTo<signed int>(small_positive)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<signed int>(small_negative)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<signed int>(large_positive)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<signed int>(large_negative)); |
+ |
+ // Same width unsigned destination. |
+ EXPECT_TRUE(base::IsNumericCastableTo<unsigned int>(small_positive)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned int>(small_negative)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<unsigned int>(large_positive)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned int>(large_negative)); |
+ |
+ // Wider signed destination. |
+ EXPECT_TRUE(base::IsNumericCastableTo<long long>(small_positive)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<long long>(large_negative)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<long long>(small_positive)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<long long>(large_negative)); |
+ |
+ // Wider unsigned destination. |
+ EXPECT_TRUE(base::IsNumericCastableTo<unsigned long long>(small_positive)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned long long>(small_negative)); |
+ EXPECT_TRUE(base::IsNumericCastableTo<unsigned long long>(large_positive)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned long long>(large_negative)); |
+ |
+ // Negative to size_t. |
+ EXPECT_FALSE(base::IsNumericCastableTo<size_t>(small_negative)); |
+ EXPECT_FALSE(base::IsNumericCastableTo<size_t>(large_negative)); |
+ |
+ // Various edge cases. |
+ EXPECT_TRUE(base::IsNumericCastableTo<int>(static_cast<short>(SHRT_MIN))); |
+ EXPECT_FALSE( |
+ base::IsNumericCastableTo<unsigned short>(static_cast<short>(SHRT_MIN))); |
+ EXPECT_FALSE(base::IsNumericCastableTo<unsigned short>(SHRT_MIN)); |
+ |
+ // These shouldn't compile. |
+ // 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
|
+ // EXPECT_TRUE(base::IsNumericCastableTo<double>(0.0f)); |
+ // EXPECT_FALSE(base::IsNumericCastableTo<int>(DBL_MAX)); |
+ |
+ // Confirm that numeric_cast<> actually compiles. |
+ std::vector<int> v; |
+ unsigned int checked_size = base::numeric_cast<unsigned int>(v.size()); |
+ EXPECT_EQ(0, checked_size); |
+} |