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

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

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 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') | base/observer_list.h » ('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 #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>
11 11
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/numerics/safe_conversions.h" 13 #include "base/numerics/safe_conversions.h"
14 #include "base/numerics/safe_math.h" 14 #include "base/numerics/safe_math.h"
15 #include "base/template_util.h" 15 #include "base/template_util.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 using std::numeric_limits; 18 using std::numeric_limits;
19 using base::CheckedNumeric; 19 using base::CheckedNumeric;
20 using base::checked_cast; 20 using base::checked_cast;
21 using base::SizeT;
22 using base::StrictNumeric;
21 using base::saturated_cast; 23 using base::saturated_cast;
24 using base::strict_cast;
22 using base::internal::MaxExponent; 25 using base::internal::MaxExponent;
23 using base::internal::RANGE_VALID; 26 using base::internal::RANGE_VALID;
24 using base::internal::RANGE_INVALID; 27 using base::internal::RANGE_INVALID;
25 using base::internal::RANGE_OVERFLOW; 28 using base::internal::RANGE_OVERFLOW;
26 using base::internal::RANGE_UNDERFLOW; 29 using base::internal::RANGE_UNDERFLOW;
27 using base::enable_if; 30 using base::enable_if;
28 31
29 // These tests deliberately cause arithmetic overflows. If the compiler is 32 // These tests deliberately cause arithmetic overflows. If the compiler is
30 // aggressive enough, it can const fold these overflows. Disable warnings about 33 // aggressive enough, it can const fold these overflows. Disable warnings about
31 // overflows for const expressions. 34 // overflows for const expressions.
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 TEST_EXPECTED_VALUE(-1, (CheckedNumeric<Dst>() - 1)); 235 TEST_EXPECTED_VALUE(-1, (CheckedNumeric<Dst>() - 1));
233 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(1) - 1)); 236 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(1) - 1));
234 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) - 1)); 237 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) - 1));
235 TEST_EXPECTED_VALIDITY(RANGE_VALID, 238 TEST_EXPECTED_VALIDITY(RANGE_VALID,
236 CheckedNumeric<Dst>(DstLimits::max()) - 1); 239 CheckedNumeric<Dst>(DstLimits::max()) - 1);
237 240
238 // Generic multiplication. 241 // Generic multiplication.
239 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>() * 1)); 242 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>() * 1));
240 TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>(1) * 1)); 243 TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>(1) * 1));
241 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) * 2)); 244 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) * 2));
245 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * 0));
246 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) * 0));
247 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * -1));
242 TEST_EXPECTED_VALIDITY( 248 TEST_EXPECTED_VALIDITY(
243 RANGE_OVERFLOW, CheckedNumeric<Dst>(DstLimits::max()) * DstLimits::max()); 249 RANGE_OVERFLOW, CheckedNumeric<Dst>(DstLimits::max()) * DstLimits::max());
244 250
245 // Generic division. 251 // Generic division.
246 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() / 1); 252 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() / 1);
247 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1); 253 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1);
248 TEST_EXPECTED_VALUE(DstLimits::min() / 2, 254 TEST_EXPECTED_VALUE(DstLimits::min() / 2,
249 CheckedNumeric<Dst>(DstLimits::min()) / 2); 255 CheckedNumeric<Dst>(DstLimits::min()) / 2);
250 TEST_EXPECTED_VALUE(DstLimits::max() / 2, 256 TEST_EXPECTED_VALUE(DstLimits::max() / 2,
251 CheckedNumeric<Dst>(DstLimits::max()) / 2); 257 CheckedNumeric<Dst>(DstLimits::max()) / 2);
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 // Since that's intentional, we need to shut this warning off. 555 // Since that's intentional, we need to shut this warning off.
550 #if defined(COMPILER_MSVC) 556 #if defined(COMPILER_MSVC)
551 #pragma warning(disable : 4756) 557 #pragma warning(disable : 4756)
552 #endif 558 #endif
553 559
554 int small_positive = 1; 560 int small_positive = 1;
555 int small_negative = -1; 561 int small_negative = -1;
556 double double_small = 1.0; 562 double double_small = 1.0;
557 double double_large = numeric_limits<double>::max(); 563 double double_large = numeric_limits<double>::max();
558 double double_infinity = numeric_limits<float>::infinity(); 564 double double_infinity = numeric_limits<float>::infinity();
565 double double_large_int = numeric_limits<int>::max();
566 double double_small_int = numeric_limits<int>::min();
559 567
560 // Just test that the cast compiles, since the other tests cover logic. 568 // Just test that the casts compile, since the other tests cover logic.
561 EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0))); 569 EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0)));
570 EXPECT_EQ(0, strict_cast<int>(static_cast<char>(0)));
571 EXPECT_EQ(0, strict_cast<int>(static_cast<unsigned char>(0)));
572 EXPECT_EQ(0U, strict_cast<unsigned>(static_cast<unsigned char>(0)));
573 EXPECT_EQ(1ULL, static_cast<uint64_t>(StrictNumeric<size_t>(1U)));
574 EXPECT_EQ(1ULL, static_cast<uint64_t>(SizeT(1U)));
575 EXPECT_EQ(1U, static_cast<size_t>(StrictNumeric<unsigned>(1U)));
576
577 EXPECT_TRUE(CheckedNumeric<uint64_t>(StrictNumeric<unsigned>(1U)).IsValid());
578 EXPECT_TRUE(CheckedNumeric<int>(StrictNumeric<unsigned>(1U)).IsValid());
579 EXPECT_FALSE(CheckedNumeric<unsigned>(StrictNumeric<int>(-1)).IsValid());
580
581 // These casts and coercions will fail to compile:
582 // EXPECT_EQ(0, strict_cast<int>(static_cast<size_t>(0)));
583 // EXPECT_EQ(0, strict_cast<size_t>(static_cast<int>(0)));
584 // EXPECT_EQ(1ULL, StrictNumeric<size_t>(1));
585 // EXPECT_EQ(1, StrictNumeric<size_t>(1U));
562 586
563 // Test various saturation corner cases. 587 // Test various saturation corner cases.
564 EXPECT_EQ(saturated_cast<int>(small_negative), 588 EXPECT_EQ(saturated_cast<int>(small_negative),
565 static_cast<int>(small_negative)); 589 static_cast<int>(small_negative));
566 EXPECT_EQ(saturated_cast<int>(small_positive), 590 EXPECT_EQ(saturated_cast<int>(small_positive),
567 static_cast<int>(small_positive)); 591 static_cast<int>(small_positive));
568 EXPECT_EQ(saturated_cast<unsigned>(small_negative), 592 EXPECT_EQ(saturated_cast<unsigned>(small_negative),
569 static_cast<unsigned>(0)); 593 static_cast<unsigned>(0));
570 EXPECT_EQ(saturated_cast<int>(double_small), 594 EXPECT_EQ(saturated_cast<int>(double_small),
571 static_cast<int>(double_small)); 595 static_cast<int>(double_small));
572 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); 596 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max());
573 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); 597 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity);
574 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); 598 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity);
599 EXPECT_EQ(numeric_limits<int>::min(), saturated_cast<int>(double_small_int));
600 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int));
575 } 601 }
576 602
OLDNEW
« no previous file with comments | « base/numerics/safe_math_impl.h ('k') | base/observer_list.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698