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 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // Not floating to integral and... | 343 // Not floating to integral and... |
344 (!(DstLimits::is_integer && SrcLimits::is_iec559) && | 344 (!(DstLimits::is_integer && SrcLimits::is_iec559) && |
345 // Same sign, same numeric, source is narrower or same. | 345 // Same sign, same numeric, source is narrower or same. |
346 ((SrcLimits::is_signed == DstLimits::is_signed && | 346 ((SrcLimits::is_signed == DstLimits::is_signed && |
347 sizeof(Dst) >= sizeof(Src)) || | 347 sizeof(Dst) >= sizeof(Src)) || |
348 // Or signed destination and source is smaller | 348 // Or signed destination and source is smaller |
349 (DstLimits::is_signed && sizeof(Dst) > sizeof(Src)))), | 349 (DstLimits::is_signed && sizeof(Dst) > sizeof(Src)))), |
350 "Comparison must be sign preserving and value preserving"); | 350 "Comparison must be sign preserving and value preserving"); |
351 | 351 |
352 const CheckedNumeric<Dst> checked_dst = SrcLimits::max(); | 352 const CheckedNumeric<Dst> checked_dst = SrcLimits::max(); |
353 ; | |
354 TEST_EXPECTED_VALIDITY(RANGE_VALID, checked_dst); | 353 TEST_EXPECTED_VALIDITY(RANGE_VALID, checked_dst); |
355 if (MaxExponent<Dst>::value > MaxExponent<Src>::value) { | 354 if (MaxExponent<Dst>::value > MaxExponent<Src>::value) { |
356 if (MaxExponent<Dst>::value >= MaxExponent<Src>::value * 2 - 1) { | 355 if (MaxExponent<Dst>::value >= MaxExponent<Src>::value * 2 - 1) { |
357 // At least twice larger type. | 356 // At least twice larger type. |
358 TEST_EXPECTED_VALIDITY(RANGE_VALID, SrcLimits::max() * checked_dst); | 357 TEST_EXPECTED_VALIDITY(RANGE_VALID, SrcLimits::max() * checked_dst); |
359 | 358 |
360 } else { // Larger, but not at least twice as large. | 359 } else { // Larger, but not at least twice as large. |
361 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, SrcLimits::max() * checked_dst); | 360 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, SrcLimits::max() * checked_dst); |
362 TEST_EXPECTED_VALIDITY(RANGE_VALID, checked_dst + 1); | 361 TEST_EXPECTED_VALIDITY(RANGE_VALID, checked_dst + 1); |
363 } | 362 } |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 | 776 |
778 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); | 777 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); |
779 EXPECT_TRUE(too_large.IsValid()); | 778 EXPECT_TRUE(too_large.IsValid()); |
780 too_large += d; | 779 too_large += d; |
781 EXPECT_FALSE(too_large.IsValid()); | 780 EXPECT_FALSE(too_large.IsValid()); |
782 too_large -= d; | 781 too_large -= d; |
783 EXPECT_FALSE(too_large.IsValid()); | 782 EXPECT_FALSE(too_large.IsValid()); |
784 too_large /= d; | 783 too_large /= d; |
785 EXPECT_FALSE(too_large.IsValid()); | 784 EXPECT_FALSE(too_large.IsValid()); |
786 } | 785 } |
OLD | NEW |