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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 namespace base {
12 namespace internal {
13
14 TEST(SafeNumerics, NumericCast) {
15 int small_positive = 1;
16 int small_negative = -1;
17 int large_positive = INT_MAX;
18 int large_negative = INT_MIN;
19
20 // Narrow signed destination.
21 EXPECT_TRUE(IsValidNumericCast<signed char>(small_positive));
22 EXPECT_TRUE(IsValidNumericCast<signed char>(small_negative));
23 EXPECT_FALSE(IsValidNumericCast<signed char>(large_positive));
24 EXPECT_FALSE(IsValidNumericCast<signed char>(large_negative));
25 EXPECT_TRUE(IsValidNumericCast<signed short>(small_positive));
26 EXPECT_TRUE(IsValidNumericCast<signed short>(small_negative));
27
28 // Narrow unsigned destination.
29 EXPECT_TRUE(IsValidNumericCast<unsigned char>(small_positive));
30 EXPECT_FALSE(IsValidNumericCast<unsigned char>(small_negative));
31 EXPECT_FALSE(IsValidNumericCast<unsigned char>(large_positive));
32 EXPECT_FALSE(IsValidNumericCast<unsigned char>(large_negative));
33 EXPECT_FALSE(IsValidNumericCast<unsigned short>(small_negative));
34 EXPECT_FALSE(IsValidNumericCast<unsigned short>(large_negative));
35
36 // Same width signed destination.
37 EXPECT_TRUE(IsValidNumericCast<signed int>(small_positive));
38 EXPECT_TRUE(IsValidNumericCast<signed int>(small_negative));
39 EXPECT_TRUE(IsValidNumericCast<signed int>(large_positive));
40 EXPECT_TRUE(IsValidNumericCast<signed int>(large_negative));
41
42 // Same width unsigned destination.
43 EXPECT_TRUE(IsValidNumericCast<unsigned int>(small_positive));
44 EXPECT_FALSE(IsValidNumericCast<unsigned int>(small_negative));
45 EXPECT_TRUE(IsValidNumericCast<unsigned int>(large_positive));
46 EXPECT_FALSE(IsValidNumericCast<unsigned int>(large_negative));
47
48 // Wider signed destination.
49 EXPECT_TRUE(IsValidNumericCast<long long>(small_positive));
50 EXPECT_TRUE(IsValidNumericCast<long long>(large_negative));
51 EXPECT_TRUE(IsValidNumericCast<long long>(small_positive));
52 EXPECT_TRUE(IsValidNumericCast<long long>(large_negative));
53
54 // Wider unsigned destination.
55 EXPECT_TRUE(IsValidNumericCast<unsigned long long>(small_positive));
56 EXPECT_FALSE(IsValidNumericCast<unsigned long long>(small_negative));
57 EXPECT_TRUE(IsValidNumericCast<unsigned long long>(large_positive));
58 EXPECT_FALSE(IsValidNumericCast<unsigned long long>(large_negative));
59
60 // Negative to size_t.
61 EXPECT_FALSE(IsValidNumericCast<size_t>(small_negative));
62 EXPECT_FALSE(IsValidNumericCast<size_t>(large_negative));
63
64 // Various edge cases.
65 EXPECT_TRUE(IsValidNumericCast<int>(static_cast<short>(SHRT_MIN)));
66 EXPECT_FALSE(
67 IsValidNumericCast<unsigned short>(static_cast<short>(SHRT_MIN)));
68 EXPECT_FALSE(IsValidNumericCast<unsigned short>(SHRT_MIN));
69
70 // Confirm that numeric_cast<> actually compiles.
71 std::vector<int> v;
72 unsigned int checked_size = base::numeric_cast<unsigned int>(v.size());
73 EXPECT_EQ(0, checked_size);
74 }
75
76 } // namespace internal
77 } // namespace base
OLDNEW
« 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