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

Side by Side Diff: base/numerics/safe_numerics_unittest.cc

Issue 1346753002: Implement CheckedNumeric::UnsignedAbs() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: gcc build fix Created 5 years, 3 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
« no previous file with comments | « base/numerics/safe_math_impl.h ('k') | no next file » | 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 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) 5 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS)
6 #include <mmintrin.h> 6 #include <mmintrin.h>
7 #endif 7 #endif
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 10 matching lines...) Expand all
21 using base::IsValueInRangeForNumericType; 21 using base::IsValueInRangeForNumericType;
22 using base::SizeT; 22 using base::SizeT;
23 using base::StrictNumeric; 23 using base::StrictNumeric;
24 using base::saturated_cast; 24 using base::saturated_cast;
25 using base::strict_cast; 25 using base::strict_cast;
26 using base::internal::MaxExponent; 26 using base::internal::MaxExponent;
27 using base::internal::RANGE_VALID; 27 using base::internal::RANGE_VALID;
28 using base::internal::RANGE_INVALID; 28 using base::internal::RANGE_INVALID;
29 using base::internal::RANGE_OVERFLOW; 29 using base::internal::RANGE_OVERFLOW;
30 using base::internal::RANGE_UNDERFLOW; 30 using base::internal::RANGE_UNDERFLOW;
31 using base::internal::SignedIntegerForSize;
31 using base::enable_if; 32 using base::enable_if;
32 33
33 // These tests deliberately cause arithmetic overflows. If the compiler is 34 // These tests deliberately cause arithmetic overflows. If the compiler is
34 // aggressive enough, it can const fold these overflows. Disable warnings about 35 // aggressive enough, it can const fold these overflows. Disable warnings about
35 // overflows for const expressions. 36 // overflows for const expressions.
36 #if defined(OS_WIN) 37 #if defined(OS_WIN)
37 #pragma warning(disable:4756) 38 #pragma warning(disable:4756)
38 #endif 39 #endif
39 40
40 // This is a helper function for finding the maximum value in Src that can be 41 // This is a helper function for finding the maximum value in Src that can be
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 typedef numeric_limits<Dst> DstLimits; 137 typedef numeric_limits<Dst> DstLimits;
137 TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min())); 138 TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min()));
138 TEST_EXPECTED_VALIDITY(RANGE_VALID, 139 TEST_EXPECTED_VALIDITY(RANGE_VALID,
139 CheckedNumeric<Dst>(DstLimits::min()).Abs()); 140 CheckedNumeric<Dst>(DstLimits::min()).Abs());
140 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, 141 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW,
141 CheckedNumeric<Dst>(DstLimits::min()) + -1); 142 CheckedNumeric<Dst>(DstLimits::min()) + -1);
142 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, 143 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW,
143 CheckedNumeric<Dst>(DstLimits::min()) - 1); 144 CheckedNumeric<Dst>(DstLimits::min()) - 1);
144 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) * 2); 145 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) * 2);
145 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) / 2); 146 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) / 2);
147 TEST_EXPECTED_VALIDITY(RANGE_VALID,
148 CheckedNumeric<Dst>(DstLimits::min()).UnsignedAbs());
149 TEST_EXPECTED_VALIDITY(
150 RANGE_VALID,
151 CheckedNumeric<typename SignedIntegerForSize<Dst>::type>(
152 std::numeric_limits<typename SignedIntegerForSize<Dst>::type>::min())
153 .UnsignedAbs());
146 154
147 // Modulus is legal only for integers. 155 // Modulus is legal only for integers.
148 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() % 1); 156 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() % 1);
149 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); 157 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1);
150 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) % 2); 158 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) % 2);
151 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2); 159 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2);
152 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); 160 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2);
153 // Test all the different modulus combinations. 161 // Test all the different modulus combinations.
154 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); 162 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1));
155 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); 163 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1));
(...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 IsValueInRangeForNumericType<int64_t>(UINT64_C(0x8000000000000000))); 720 IsValueInRangeForNumericType<int64_t>(UINT64_C(0x8000000000000000)));
713 EXPECT_FALSE( 721 EXPECT_FALSE(
714 IsValueInRangeForNumericType<int64_t>(UINT64_C(0xffffffffffffffff))); 722 IsValueInRangeForNumericType<int64_t>(UINT64_C(0xffffffffffffffff)));
715 EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( 723 EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(
716 std::numeric_limits<int32_t>::min())); 724 std::numeric_limits<int32_t>::min()));
717 EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( 725 EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(
718 implicit_cast<int64_t>(std::numeric_limits<int32_t>::min()))); 726 implicit_cast<int64_t>(std::numeric_limits<int32_t>::min())));
719 EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( 727 EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(
720 std::numeric_limits<int64_t>::min())); 728 std::numeric_limits<int64_t>::min()));
721 } 729 }
OLDNEW
« no previous file with comments | « base/numerics/safe_math_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698