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

Unified Diff: base/safe_numerics_unittest.cc

Issue 11886037: Add numeric_cast for checked integral narrowing casts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review fixes 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
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..7e4b02a4b61546adf57b28831dbc266c998c0ac9
--- /dev/null
+++ b/base/safe_numerics_unittest.cc
@@ -0,0 +1,77 @@
+// 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"
+
+namespace base {
+namespace internal {
+
+TEST(SafeNumerics, NumericCast) {
+ int small_positive = 1;
+ int small_negative = -1;
+ int large_positive = INT_MAX;
+ int large_negative = INT_MIN;
+
+ // Narrow signed destination.
+ EXPECT_TRUE(IsValidNumericCast<signed char>(small_positive));
+ EXPECT_TRUE(IsValidNumericCast<signed char>(small_negative));
+ EXPECT_FALSE(IsValidNumericCast<signed char>(large_positive));
+ EXPECT_FALSE(IsValidNumericCast<signed char>(large_negative));
+ EXPECT_TRUE(IsValidNumericCast<signed short>(small_positive));
+ EXPECT_TRUE(IsValidNumericCast<signed short>(small_negative));
+
+ // Narrow unsigned destination.
+ EXPECT_TRUE(IsValidNumericCast<unsigned char>(small_positive));
+ EXPECT_FALSE(IsValidNumericCast<unsigned char>(small_negative));
+ EXPECT_FALSE(IsValidNumericCast<unsigned char>(large_positive));
+ EXPECT_FALSE(IsValidNumericCast<unsigned char>(large_negative));
+ EXPECT_FALSE(IsValidNumericCast<unsigned short>(small_negative));
+ EXPECT_FALSE(IsValidNumericCast<unsigned short>(large_negative));
+
+ // Same width signed destination.
+ EXPECT_TRUE(IsValidNumericCast<signed int>(small_positive));
+ EXPECT_TRUE(IsValidNumericCast<signed int>(small_negative));
+ EXPECT_TRUE(IsValidNumericCast<signed int>(large_positive));
+ EXPECT_TRUE(IsValidNumericCast<signed int>(large_negative));
+
+ // Same width unsigned destination.
+ EXPECT_TRUE(IsValidNumericCast<unsigned int>(small_positive));
+ EXPECT_FALSE(IsValidNumericCast<unsigned int>(small_negative));
+ EXPECT_TRUE(IsValidNumericCast<unsigned int>(large_positive));
+ EXPECT_FALSE(IsValidNumericCast<unsigned int>(large_negative));
+
+ // Wider signed destination.
+ EXPECT_TRUE(IsValidNumericCast<long long>(small_positive));
+ EXPECT_TRUE(IsValidNumericCast<long long>(large_negative));
+ EXPECT_TRUE(IsValidNumericCast<long long>(small_positive));
+ EXPECT_TRUE(IsValidNumericCast<long long>(large_negative));
+
+ // Wider unsigned destination.
+ EXPECT_TRUE(IsValidNumericCast<unsigned long long>(small_positive));
+ EXPECT_FALSE(IsValidNumericCast<unsigned long long>(small_negative));
+ EXPECT_TRUE(IsValidNumericCast<unsigned long long>(large_positive));
+ EXPECT_FALSE(IsValidNumericCast<unsigned long long>(large_negative));
+
+ // Negative to size_t.
+ EXPECT_FALSE(IsValidNumericCast<size_t>(small_negative));
+ EXPECT_FALSE(IsValidNumericCast<size_t>(large_negative));
+
+ // Various edge cases.
+ EXPECT_TRUE(IsValidNumericCast<int>(static_cast<short>(SHRT_MIN)));
+ EXPECT_FALSE(
+ IsValidNumericCast<unsigned short>(static_cast<short>(SHRT_MIN)));
+ EXPECT_FALSE(IsValidNumericCast<unsigned short>(SHRT_MIN));
+
+ // 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);
+}
+
+} // namespace internal
+} // namespace base
« base/safe_numerics.h ('K') | « base/safe_numerics.h ('k') | base/safe_numerics_unittest.nc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698