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