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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 #include <type_traits> | 9 #include <type_traits> |
10 | 10 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 #define TEST_EXPECTED_VALIDITY(expected, actual) \ | 66 #define TEST_EXPECTED_VALIDITY(expected, actual) \ |
67 EXPECT_EQ(expected, CheckedNumeric<Dst>(actual).IsValid()) \ | 67 EXPECT_EQ(expected, CheckedNumeric<Dst>(actual).IsValid()) \ |
68 << "Result test: Value " << +(actual).ValueUnsafe() << " as " << dst \ | 68 << "Result test: Value " << +(actual).ValueUnsafe() << " as " << dst \ |
69 << " on line " << line; | 69 << " on line " << line; |
70 | 70 |
71 #define TEST_EXPECTED_SUCCESS(actual) TEST_EXPECTED_VALIDITY(true, actual) | 71 #define TEST_EXPECTED_SUCCESS(actual) TEST_EXPECTED_VALIDITY(true, actual) |
72 #define TEST_EXPECTED_FAILURE(actual) TEST_EXPECTED_VALIDITY(false, actual) | 72 #define TEST_EXPECTED_FAILURE(actual) TEST_EXPECTED_VALIDITY(false, actual) |
73 | 73 |
74 #define TEST_EXPECTED_VALUE(expected, actual) \ | 74 #define TEST_EXPECTED_VALUE(expected, actual) \ |
75 EXPECT_EQ(static_cast<Dst>(expected), \ | 75 EXPECT_EQ(static_cast<Dst>(expected), \ |
76 CheckedNumeric<Dst>(actual).ValueUnsafe()) \ | 76 CheckedNumeric<Dst>(actual).ValueOrDie()) \ |
77 << "Result test: Value " << +((actual).ValueUnsafe()) << " as " << dst \ | 77 << "Result test: Value " << +((actual).ValueUnsafe()) << " as " << dst \ |
78 << " on line " << line; | 78 << " on line " << line; |
79 | 79 |
80 // Signed integer arithmetic. | 80 // Signed integer arithmetic. |
81 template <typename Dst> | 81 template <typename Dst> |
82 static void TestSpecializedArithmetic( | 82 static void TestSpecializedArithmetic( |
83 const char* dst, | 83 const char* dst, |
84 int line, | 84 int line, |
85 typename std::enable_if<numeric_limits<Dst>::is_integer && | 85 typename std::enable_if<numeric_limits<Dst>::is_integer && |
86 numeric_limits<Dst>::is_signed, | 86 numeric_limits<Dst>::is_signed, |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 | 237 |
238 // Generic absolute value. | 238 // Generic absolute value. |
239 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>().Abs()); | 239 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>().Abs()); |
240 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1).Abs()); | 240 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1).Abs()); |
241 TEST_EXPECTED_VALUE(DstLimits::max(), | 241 TEST_EXPECTED_VALUE(DstLimits::max(), |
242 CheckedNumeric<Dst>(DstLimits::max()).Abs()); | 242 CheckedNumeric<Dst>(DstLimits::max()).Abs()); |
243 | 243 |
244 // Generic addition. | 244 // Generic addition. |
245 TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>() + 1)); | 245 TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>() + 1)); |
246 TEST_EXPECTED_VALUE(2, (CheckedNumeric<Dst>(1) + 1)); | 246 TEST_EXPECTED_VALUE(2, (CheckedNumeric<Dst>(1) + 1)); |
247 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) + 1)); | 247 if (numeric_limits<Dst>::is_signed) |
| 248 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) + 1)); |
248 TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::min()) + 1); | 249 TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::min()) + 1); |
249 TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) + | 250 TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) + |
250 DstLimits::max()); | 251 DstLimits::max()); |
251 | 252 |
252 // Generic subtraction. | 253 // Generic subtraction. |
253 TEST_EXPECTED_VALUE(-1, (CheckedNumeric<Dst>() - 1)); | |
254 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(1) - 1)); | 254 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(1) - 1)); |
255 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) - 1)); | |
256 TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::max()) - 1); | 255 TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::max()) - 1); |
| 256 if (numeric_limits<Dst>::is_signed) { |
| 257 TEST_EXPECTED_VALUE(-1, (CheckedNumeric<Dst>() - 1)); |
| 258 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) - 1)); |
| 259 } |
257 | 260 |
258 // Generic multiplication. | 261 // Generic multiplication. |
259 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>() * 1)); | 262 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>() * 1)); |
260 TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>(1) * 1)); | 263 TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>(1) * 1)); |
261 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * 0)); | 264 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * 0)); |
262 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) * 0)); | 265 if (numeric_limits<Dst>::is_signed) { |
263 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * -1)); | 266 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) * 0)); |
| 267 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * -1)); |
| 268 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) * 2)); |
| 269 } |
264 TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) * | 270 TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) * |
265 DstLimits::max()); | 271 DstLimits::max()); |
266 if (DstLimits::is_signed) { | |
267 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) * 2)); | |
268 } | |
269 | 272 |
270 // Generic division. | 273 // Generic division. |
271 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() / 1); | 274 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() / 1); |
272 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1); | 275 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1); |
273 TEST_EXPECTED_VALUE(DstLimits::min() / 2, | 276 TEST_EXPECTED_VALUE(DstLimits::min() / 2, |
274 CheckedNumeric<Dst>(DstLimits::min()) / 2); | 277 CheckedNumeric<Dst>(DstLimits::min()) / 2); |
275 TEST_EXPECTED_VALUE(DstLimits::max() / 2, | 278 TEST_EXPECTED_VALUE(DstLimits::max() / 2, |
276 CheckedNumeric<Dst>(DstLimits::max()) / 2); | 279 CheckedNumeric<Dst>(DstLimits::max()) / 2); |
277 | 280 |
278 TestSpecializedArithmetic<Dst>(dst, line); | 281 TestSpecializedArithmetic<Dst>(dst, line); |
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 | 762 |
760 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); | 763 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); |
761 EXPECT_TRUE(too_large.IsValid()); | 764 EXPECT_TRUE(too_large.IsValid()); |
762 too_large += d; | 765 too_large += d; |
763 EXPECT_FALSE(too_large.IsValid()); | 766 EXPECT_FALSE(too_large.IsValid()); |
764 too_large -= d; | 767 too_large -= d; |
765 EXPECT_FALSE(too_large.IsValid()); | 768 EXPECT_FALSE(too_large.IsValid()); |
766 too_large /= d; | 769 too_large /= d; |
767 EXPECT_FALSE(too_large.IsValid()); | 770 EXPECT_FALSE(too_large.IsValid()); |
768 } | 771 } |
OLD | NEW |