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

Side by Side Diff: base/safe_numerics_unittest.cc

Issue 131063002: Expand support in safe_numeric.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: final: this time it's for real Created 6 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
« no previous file with comments | « base/safe_numerics_impl.h ('k') | base/safe_numerics_unittest.nc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <gtest/gtest.h>
6
7 #include <sstream>
8 #include <vector>
9
10 #include "base/safe_numerics.h" 5 #include "base/safe_numerics.h"
11 6
7 #include <stdint.h>
8
9 #include <limits>
10
11 #include "base/compiler_specific.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
12 namespace base { 14 namespace base {
13 namespace internal { 15 namespace internal {
14 16
15 // This is far (far, far) too slow to run normally, but if you're refactoring 17 // Enumerates the five different conversions types we need to test.
16 // it might be useful. 18 enum NumericConversionType {
17 // #define RUN_EXHAUSTIVE_TEST 19 SIGN_PRESERVING_VALUE_PRESERVING,
18 20 SIGN_PRESERVING_NARROW,
19 #ifdef RUN_EXHAUSTIVE_TEST 21 SIGN_TO_UNSIGN_WIDEN_OR_EQUAL,
20 22 SIGN_TO_UNSIGN_NARROW,
21 template <class From, class To> void ExhaustiveCheckFromTo() { 23 UNSIGN_TO_SIGN_NARROW_OR_EQUAL,
22 fprintf(stderr, "."); 24 };
23 From i = std::numeric_limits<From>::min(); 25
24 for (;;) { 26 // Template covering the different conversion tests.
25 std::ostringstream str_from, str_to; 27 template <typename Dst, typename Src, NumericConversionType conversion>
26 str_from << i; 28 struct TestNumericConversion {};
27 To to = static_cast<To>(i); 29
28 str_to << to; 30 // EXPECT_EQ wrapper providing specific detail on test failures.
29 bool strings_equal = str_from.str() == str_to.str(); 31 #define TEST_EXPECTED_RANGE(expected, actual) \
30 EXPECT_EQ(IsValidNumericCast<To>(i), strings_equal); 32 EXPECT_EQ(expected, RangeCheck<Dst>(actual)) << \
31 fprintf(stderr, "\r%s vs %s\x1B[K", 33 "Conversion test: " << src << " value " << actual << \
32 str_from.str().c_str(), str_to.str().c_str()); 34 " to " << dst << " on line " << line;
33 ++i; 35
34 // If we wrap, then we've tested everything. 36 template <typename Dst, typename Src>
35 if (i == std::numeric_limits<From>::min()) 37 struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING> {
36 break; 38 static void Test(const char *dst, const char *src, int line) {
37 } 39 typedef std::numeric_limits<Src> SrcLimits;
38 } 40 typedef std::numeric_limits<Dst> DstLimits;
39 41 // Integral to floating.
40 template <class From> void ExhaustiveCheckFrom() { 42 COMPILE_ASSERT((DstLimits::is_iec559 && SrcLimits::is_integer) ||
41 ExhaustiveCheckFromTo<From, short>(); 43 // Not floating to integral and...
42 ExhaustiveCheckFromTo<From, unsigned short>(); 44 (!(DstLimits::is_integer && SrcLimits::is_iec559) &&
43 ExhaustiveCheckFromTo<From, int>(); 45 // Same sign, same numeric, source is narrower or same.
44 ExhaustiveCheckFromTo<From, unsigned int>(); 46 ((SrcLimits::is_signed == DstLimits::is_signed &&
45 ExhaustiveCheckFromTo<From, long long>(); 47 sizeof(Dst) >= sizeof(Src)) ||
46 ExhaustiveCheckFromTo<From, unsigned long long>(); 48 // Or signed destination and source is smaller
47 ExhaustiveCheckFromTo<From, size_t>(); 49 (DstLimits::is_signed && sizeof(Dst) > sizeof(Src)))),
48 fprintf(stderr, "\n"); 50 comparison_must_be_sign_preserving_and_value_preserving);
49 } 51
50 52 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::max());
53 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1));
54 if (SrcLimits::is_iec559) {
55 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::max() * static_cast<Src>(-1));
56 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::infinity());
57 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::infinity() * -1);
58 TEST_EXPECTED_RANGE(TYPE_INVALID, SrcLimits::quiet_NaN());
59 } else if (std::numeric_limits<Src>::is_signed) {
60 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(-1));
61 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::min());
62 }
63 }
64 };
65
66 template <typename Dst, typename Src>
67 struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_NARROW> {
68 static void Test(const char *dst, const char *src, int line) {
69 typedef std::numeric_limits<Src> SrcLimits;
70 typedef std::numeric_limits<Dst> DstLimits;
71 COMPILE_ASSERT(SrcLimits::is_signed == DstLimits::is_signed,
72 destination_and_source_sign_must_be_the_same);
73 COMPILE_ASSERT(sizeof(Dst) < sizeof(Src) ||
74 (DstLimits::is_integer && SrcLimits::is_iec559),
75 destination_must_be_narrower_than_source);
76
77 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::max());
78 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1));
79 if (SrcLimits::is_iec559) {
80 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::max() * -1);
81 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(-1));
82 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::infinity());
83 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::infinity() * -1);
84 TEST_EXPECTED_RANGE(TYPE_INVALID, SrcLimits::quiet_NaN());
85 } else if (SrcLimits::is_signed) {
86 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::min());
87 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(-1));
88 } else {
89 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::min());
90 }
91 }
92 };
93
94 template <typename Dst, typename Src>
95 struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL> {
96 static void Test(const char *dst, const char *src, int line) {
97 typedef std::numeric_limits<Src> SrcLimits;
98 typedef std::numeric_limits<Dst> DstLimits;
99 COMPILE_ASSERT(sizeof(Dst) >= sizeof(Src),
100 destination_must_be_equal_or_wider_than_source);
101 COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed);
102 COMPILE_ASSERT(!DstLimits::is_signed, destination_must_be_unsigned);
103
104 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::min());
105 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::max());
106 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1));
107 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, static_cast<Src>(-1));
108 }
109 };
110
111 template <typename Dst, typename Src>
112 struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_NARROW> {
113 static void Test(const char *dst, const char *src, int line) {
114 typedef std::numeric_limits<Src> SrcLimits;
115 typedef std::numeric_limits<Dst> DstLimits;
116 COMPILE_ASSERT((DstLimits::is_integer && SrcLimits::is_iec559) ||
117 (sizeof(Dst) < sizeof(Src)),
118 destination_must_be_narrower_than_source);
119 COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed);
120 COMPILE_ASSERT(!DstLimits::is_signed, destination_must_be_unsigned);
121
122 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::max());
123 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1));
124 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, static_cast<Src>(-1));
125 if (SrcLimits::is_iec559) {
126 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::max() * -1);
127 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::infinity());
128 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::infinity() * -1);
129 TEST_EXPECTED_RANGE(TYPE_INVALID, SrcLimits::quiet_NaN());
130 } else {
131 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::min());
132 }
133 }
134 };
135
136 template <typename Dst, typename Src>
137 struct TestNumericConversion<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL> {
138 static void Test(const char *dst, const char *src, int line) {
139 typedef std::numeric_limits<Src> SrcLimits;
140 typedef std::numeric_limits<Dst> DstLimits;
141 COMPILE_ASSERT(sizeof(Dst) <= sizeof(Src),
142 destination_must_be_narrower_or_equal_to_source);
143 COMPILE_ASSERT(!SrcLimits::is_signed, source_must_be_unsigned);
144 COMPILE_ASSERT(DstLimits::is_signed, destination_must_be_signed);
145
146 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::min());
147 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::max());
148 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1));
149 }
150 };
151
152 // Helper macro to wrap displaying the conversion types and line numbers
153 #define TEST_NUMERIC_CONVERSION(d, s, t) \
154 TestNumericConversion<d, s, t>::Test(#d, #s, __LINE__)
155
156 TEST(SafeNumerics, IntMinConversions) {
157 TEST_NUMERIC_CONVERSION(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING);
158 TEST_NUMERIC_CONVERSION(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
159
160 TEST_NUMERIC_CONVERSION(int8_t, int, SIGN_PRESERVING_NARROW);
161 TEST_NUMERIC_CONVERSION(uint8_t, unsigned int, SIGN_PRESERVING_NARROW);
162 TEST_NUMERIC_CONVERSION(int8_t, float, SIGN_PRESERVING_NARROW);
163
164 TEST_NUMERIC_CONVERSION(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
165
166 TEST_NUMERIC_CONVERSION(uint8_t, int, SIGN_TO_UNSIGN_NARROW);
167 TEST_NUMERIC_CONVERSION(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW);
168 TEST_NUMERIC_CONVERSION(uint8_t, float, SIGN_TO_UNSIGN_NARROW);
169
170 TEST_NUMERIC_CONVERSION(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
171 TEST_NUMERIC_CONVERSION(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
172 }
173
174 TEST(SafeNumerics, IntConversions) {
175 TEST_NUMERIC_CONVERSION(int, int, SIGN_PRESERVING_VALUE_PRESERVING);
176 TEST_NUMERIC_CONVERSION(unsigned int, unsigned int,
177 SIGN_PRESERVING_VALUE_PRESERVING);
178 TEST_NUMERIC_CONVERSION(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING);
179 TEST_NUMERIC_CONVERSION(unsigned int, uint8_t,
180 SIGN_PRESERVING_VALUE_PRESERVING);
181 TEST_NUMERIC_CONVERSION(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
182
183 TEST_NUMERIC_CONVERSION(int, intmax_t, SIGN_PRESERVING_NARROW);
184 TEST_NUMERIC_CONVERSION(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW);
185 TEST_NUMERIC_CONVERSION(int, float, SIGN_PRESERVING_NARROW);
186 TEST_NUMERIC_CONVERSION(int, double, SIGN_PRESERVING_NARROW);
187
188 TEST_NUMERIC_CONVERSION(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
189 TEST_NUMERIC_CONVERSION(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
190
191 TEST_NUMERIC_CONVERSION(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW);
192 TEST_NUMERIC_CONVERSION(unsigned int, float, SIGN_TO_UNSIGN_NARROW);
193 TEST_NUMERIC_CONVERSION(unsigned int, double, SIGN_TO_UNSIGN_NARROW);
194
195 TEST_NUMERIC_CONVERSION(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
196 TEST_NUMERIC_CONVERSION(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
197 }
198
199 TEST(SafeNumerics, IntMaxConversions) {
200 TEST_NUMERIC_CONVERSION(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
201 TEST_NUMERIC_CONVERSION(uintmax_t, uintmax_t,
202 SIGN_PRESERVING_VALUE_PRESERVING);
203 TEST_NUMERIC_CONVERSION(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING);
204 TEST_NUMERIC_CONVERSION(uintmax_t, unsigned int,
205 SIGN_PRESERVING_VALUE_PRESERVING);
206 TEST_NUMERIC_CONVERSION(intmax_t, unsigned int,
207 SIGN_PRESERVING_VALUE_PRESERVING);
208 TEST_NUMERIC_CONVERSION(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
209
210 TEST_NUMERIC_CONVERSION(intmax_t, float, SIGN_PRESERVING_NARROW);
211 TEST_NUMERIC_CONVERSION(intmax_t, double, SIGN_PRESERVING_NARROW);
212
213 TEST_NUMERIC_CONVERSION(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
214 TEST_NUMERIC_CONVERSION(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
215
216 TEST_NUMERIC_CONVERSION(uintmax_t, float, SIGN_TO_UNSIGN_NARROW);
217 TEST_NUMERIC_CONVERSION(uintmax_t, double, SIGN_TO_UNSIGN_NARROW);
218
219 TEST_NUMERIC_CONVERSION(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
220 }
221
222 TEST(SafeNumerics, FloatConversions) {
223 TEST_NUMERIC_CONVERSION(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
224 TEST_NUMERIC_CONVERSION(float, uintmax_t,
225 SIGN_PRESERVING_VALUE_PRESERVING);
226 TEST_NUMERIC_CONVERSION(float, int, SIGN_PRESERVING_VALUE_PRESERVING);
227 TEST_NUMERIC_CONVERSION(float, unsigned int,
228 SIGN_PRESERVING_VALUE_PRESERVING);
229
230 TEST_NUMERIC_CONVERSION(float, double, SIGN_PRESERVING_NARROW);
231 }
232
233 TEST(SafeNumerics, DoubleConversions) {
234 TEST_NUMERIC_CONVERSION(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
235 TEST_NUMERIC_CONVERSION(double, uintmax_t,
236 SIGN_PRESERVING_VALUE_PRESERVING);
237 TEST_NUMERIC_CONVERSION(double, int, SIGN_PRESERVING_VALUE_PRESERVING);
238 TEST_NUMERIC_CONVERSION(double, unsigned int,
239 SIGN_PRESERVING_VALUE_PRESERVING);
240 }
241
242 TEST(SafeNumerics, SizeTConversions) {
243 TEST_NUMERIC_CONVERSION(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
244 TEST_NUMERIC_CONVERSION(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
245 }
246
247 TEST(SafeNumerics, CastTests) {
248 // MSVC catches and warns that we're forcing saturation in these tests.
249 // Since that's intentional, we need to shut this warning off.
250 #if defined(COMPILER_MSVC)
251 #pragma warning(disable : 4756)
51 #endif 252 #endif
52 253
53
54 TEST(SafeNumerics, NumericCast) {
55 int small_positive = 1; 254 int small_positive = 1;
56 int small_negative = -1; 255 int small_negative = -1;
57 int large_positive = INT_MAX; 256 double double_small = 1.0;
58 int large_negative = INT_MIN; 257 double double_large = std::numeric_limits<double>::max();
59 size_t size_t_small = 1; 258 double double_infinity = std::numeric_limits<float>::infinity();
60 size_t size_t_large = UINT_MAX; 259
61 260 // Just test that the cast compiles, since the other tests cover logic.
62 // Narrow signed destination. 261 EXPECT_EQ(0, base::checked_numeric_cast<int>(static_cast<size_t>(0)));
63 EXPECT_TRUE(IsValidNumericCast<signed char>(small_positive)); 262
64 EXPECT_TRUE(IsValidNumericCast<signed char>(small_negative)); 263 // Test various saturation corner cases.
65 EXPECT_FALSE(IsValidNumericCast<signed char>(large_positive)); 264 EXPECT_EQ(saturated_cast<int>(small_negative),
66 EXPECT_FALSE(IsValidNumericCast<signed char>(large_negative)); 265 static_cast<int>(small_negative));
67 EXPECT_TRUE(IsValidNumericCast<signed short>(small_positive)); 266 EXPECT_EQ(saturated_cast<int>(small_positive),
68 EXPECT_TRUE(IsValidNumericCast<signed short>(small_negative)); 267 static_cast<int>(small_positive));
69 268 EXPECT_EQ(saturated_cast<unsigned>(small_negative),
70 // Narrow unsigned destination. 269 static_cast<unsigned>(0));
71 EXPECT_TRUE(IsValidNumericCast<unsigned char>(small_positive)); 270 EXPECT_EQ(saturated_cast<int>(double_small),
72 EXPECT_FALSE(IsValidNumericCast<unsigned char>(small_negative)); 271 static_cast<int>(double_small));
73 EXPECT_FALSE(IsValidNumericCast<unsigned char>(large_positive)); 272 EXPECT_EQ(saturated_cast<int>(double_large),
74 EXPECT_FALSE(IsValidNumericCast<unsigned char>(large_negative)); 273 std::numeric_limits<int>::max());
75 EXPECT_FALSE(IsValidNumericCast<unsigned short>(small_negative)); 274 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity);
76 EXPECT_FALSE(IsValidNumericCast<unsigned short>(large_negative)); 275 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity);
77
78 // Same width signed destination.
79 EXPECT_TRUE(IsValidNumericCast<signed int>(small_positive));
80 EXPECT_TRUE(IsValidNumericCast<signed int>(small_negative));
81 EXPECT_TRUE(IsValidNumericCast<signed int>(large_positive));
82 EXPECT_TRUE(IsValidNumericCast<signed int>(large_negative));
83
84 // Same width unsigned destination.
85 EXPECT_TRUE(IsValidNumericCast<unsigned int>(small_positive));
86 EXPECT_FALSE(IsValidNumericCast<unsigned int>(small_negative));
87 EXPECT_TRUE(IsValidNumericCast<unsigned int>(large_positive));
88 EXPECT_FALSE(IsValidNumericCast<unsigned int>(large_negative));
89
90 // Wider signed destination.
91 EXPECT_TRUE(IsValidNumericCast<long long>(small_positive));
92 EXPECT_TRUE(IsValidNumericCast<long long>(large_negative));
93 EXPECT_TRUE(IsValidNumericCast<long long>(small_positive));
94 EXPECT_TRUE(IsValidNumericCast<long long>(large_negative));
95
96 // Wider unsigned destination.
97 EXPECT_TRUE(IsValidNumericCast<unsigned long long>(small_positive));
98 EXPECT_FALSE(IsValidNumericCast<unsigned long long>(small_negative));
99 EXPECT_TRUE(IsValidNumericCast<unsigned long long>(large_positive));
100 EXPECT_FALSE(IsValidNumericCast<unsigned long long>(large_negative));
101
102 // Negative to size_t.
103 EXPECT_FALSE(IsValidNumericCast<size_t>(small_negative));
104 EXPECT_FALSE(IsValidNumericCast<size_t>(large_negative));
105
106 // From unsigned.
107 // Small.
108 EXPECT_TRUE(IsValidNumericCast<signed char>(size_t_small));
109 EXPECT_TRUE(IsValidNumericCast<unsigned char>(size_t_small));
110 EXPECT_TRUE(IsValidNumericCast<short>(size_t_small));
111 EXPECT_TRUE(IsValidNumericCast<unsigned short>(size_t_small));
112 EXPECT_TRUE(IsValidNumericCast<int>(size_t_small));
113 EXPECT_TRUE(IsValidNumericCast<unsigned int>(size_t_small));
114 EXPECT_TRUE(IsValidNumericCast<long long>(size_t_small));
115 EXPECT_TRUE(IsValidNumericCast<unsigned long long>(size_t_small));
116
117 // Large.
118 EXPECT_FALSE(IsValidNumericCast<signed char>(size_t_large));
119 EXPECT_FALSE(IsValidNumericCast<unsigned char>(size_t_large));
120 EXPECT_FALSE(IsValidNumericCast<short>(size_t_large));
121 EXPECT_FALSE(IsValidNumericCast<unsigned short>(size_t_large));
122 EXPECT_FALSE(IsValidNumericCast<int>(size_t_large));
123 EXPECT_TRUE(IsValidNumericCast<unsigned int>(size_t_large));
124 EXPECT_TRUE(IsValidNumericCast<long long>(size_t_large));
125 EXPECT_TRUE(IsValidNumericCast<unsigned long long>(size_t_large));
126
127 // Various edge cases.
128 EXPECT_TRUE(IsValidNumericCast<int>(static_cast<short>(SHRT_MIN)));
129 EXPECT_FALSE(
130 IsValidNumericCast<unsigned short>(static_cast<short>(SHRT_MIN)));
131 EXPECT_FALSE(IsValidNumericCast<unsigned short>(SHRT_MIN));
132
133 // Confirm that checked_numeric_cast<> actually compiles.
134 std::vector<int> v;
135 unsigned int checked_size =
136 base::checked_numeric_cast<unsigned int>(v.size());
137 EXPECT_EQ(0u, checked_size);
138
139 #ifdef RUN_EXHAUSTIVE_TEST
140 ExhaustiveCheckFrom<short>();
141 ExhaustiveCheckFrom<unsigned short>();
142 ExhaustiveCheckFrom<int>();
143 ExhaustiveCheckFrom<unsigned int>();
144 ExhaustiveCheckFrom<long long>();
145 ExhaustiveCheckFrom<unsigned long long>();
146 ExhaustiveCheckFrom<size_t>();
147 #endif
148 } 276 }
149 277
150 } // namespace internal 278 } // namespace internal
151 } // namespace base 279 } // namespace base
OLDNEW
« no previous file with comments | « base/safe_numerics_impl.h ('k') | base/safe_numerics_unittest.nc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698