Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3833)

Unified Diff: base/numeric_cast_unittest.cc

Issue 11881047: Add numeric_cast for checked integral narrowing casts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/numeric_cast.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/numeric_cast_unittest.cc
===================================================================
--- base/numeric_cast_unittest.cc (revision 0)
+++ base/numeric_cast_unittest.cc (revision 0)
@@ -0,0 +1,66 @@
+// 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/numeric_cast.h"
+
+TEST(NumericCast, Test) {
+ 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));
+
+ // 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));
+
+ // 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));
+
+ // These shouldn't compile.
+ // EXPECT_TRUE(base::IsNumericCastableTo<float>(0.0));
+ // 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);
+}
« no previous file with comments | « base/numeric_cast.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698