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

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

Issue 1154543003: Add strictly checked numeric types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CheckedNumeric ctor and SizeT rename Created 5 years, 7 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.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>
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 521 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 #if defined(COMPILER_MSVC) 556 #if defined(COMPILER_MSVC)
554 #pragma warning(disable : 4756) 557 #pragma warning(disable : 4756)
555 #endif 558 #endif
556 559
557 int small_positive = 1; 560 int small_positive = 1;
558 int small_negative = -1; 561 int small_negative = -1;
559 double double_small = 1.0; 562 double double_small = 1.0;
560 double double_large = numeric_limits<double>::max(); 563 double double_large = numeric_limits<double>::max();
561 double double_infinity = numeric_limits<float>::infinity(); 564 double double_infinity = numeric_limits<float>::infinity();
562 565
563 // Just test that the cast compiles, since the other tests cover logic. 566 // Just test that the casts compile, since the other tests cover logic.
564 EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0))); 567 EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0)));
568 EXPECT_EQ(0, strict_cast<int>(static_cast<char>(0)));
569 EXPECT_EQ(0, strict_cast<int>(static_cast<unsigned char>(0)));
570 EXPECT_EQ(0U, strict_cast<unsigned>(static_cast<unsigned char>(0)));
571 EXPECT_EQ(1ULL, static_cast<uint64_t>(StrictNumeric<size_t>(1U)));
572 EXPECT_EQ(1ULL, static_cast<uint64_t>(SizeT(1U)));
573 EXPECT_EQ(1U, static_cast<size_t>(StrictNumeric<unsigned>(1U)));
574
575 EXPECT_TRUE(CheckedNumeric<uint64_t>(StrictNumeric<unsigned>(1U)).IsValid());
576 EXPECT_TRUE(CheckedNumeric<int>(StrictNumeric<unsigned>(1U)).IsValid());
577 EXPECT_FALSE(CheckedNumeric<unsigned>(StrictNumeric<int>(-1)).IsValid());
578
579 // These casts and coercions will fail to compile:
580 // EXPECT_EQ(0, strict_cast<int>(static_cast<size_t>(0)));
581 // EXPECT_EQ(0, strict_cast<size_t>(static_cast<int>(0)));
582 // EXPECT_EQ(1ULL, StrictNumeric<size_t>(1));
583 // EXPECT_EQ(1, StrictNumeric<size_t>(1U));
565 584
566 // Test various saturation corner cases. 585 // Test various saturation corner cases.
567 EXPECT_EQ(saturated_cast<int>(small_negative), 586 EXPECT_EQ(saturated_cast<int>(small_negative),
568 static_cast<int>(small_negative)); 587 static_cast<int>(small_negative));
569 EXPECT_EQ(saturated_cast<int>(small_positive), 588 EXPECT_EQ(saturated_cast<int>(small_positive),
570 static_cast<int>(small_positive)); 589 static_cast<int>(small_positive));
571 EXPECT_EQ(saturated_cast<unsigned>(small_negative), 590 EXPECT_EQ(saturated_cast<unsigned>(small_negative),
572 static_cast<unsigned>(0)); 591 static_cast<unsigned>(0));
573 EXPECT_EQ(saturated_cast<int>(double_small), 592 EXPECT_EQ(saturated_cast<int>(double_small),
574 static_cast<int>(double_small)); 593 static_cast<int>(double_small));
575 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); 594 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max());
576 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); 595 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity);
577 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); 596 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity);
578 } 597 }
579 598
OLDNEW
« no previous file with comments | « base/numerics/safe_math.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698