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

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

Issue 2535603002: Add CheckedNumeric::AssignIfValid() method (Closed)
Patch Set: Created 4 years 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
« base/numerics/safe_math.h ('K') | « 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 #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 746 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 static_assert( 757 static_assert(
758 std::is_same<float, decltype(double_max.ValueFloating<float>())>::value, 758 std::is_same<float, decltype(double_max.ValueFloating<float>())>::value,
759 "ValueFloating returning incorrect type."); 759 "ValueFloating returning incorrect type.");
760 EXPECT_FALSE(int8_min.template IsValid<uint8_t>()); 760 EXPECT_FALSE(int8_min.template IsValid<uint8_t>());
761 EXPECT_TRUE(int8_max.template IsValid<uint8_t>()); 761 EXPECT_TRUE(int8_max.template IsValid<uint8_t>());
762 EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::min()), 762 EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::min()),
763 int8_min.template ValueOrDie<int>()); 763 int8_min.template ValueOrDie<int>());
764 EXPECT_TRUE(int8_max.template IsValid<uint32_t>()); 764 EXPECT_TRUE(int8_max.template IsValid<uint32_t>());
765 EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::max()), 765 EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::max()),
766 int8_max.template ValueOrDie<int>()); 766 int8_max.template ValueOrDie<int>());
767 uint8_t uint8_dest = 0;
768 int16_t int16_dest = 0;
769 double double_dest = 0;
770 EXPECT_TRUE(int8_max.AssignIfValid(&uint8_dest));
771 EXPECT_EQ(static_cast<uint8_t>(numeric_limits<int8_t>::max()), uint8_dest);
772 EXPECT_FALSE(int8_min.AssignIfValid(&uint8_dest));
773 EXPECT_TRUE(int8_max.AssignIfValid(&int16_dest));
774 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::max()), int16_dest);
775 EXPECT_TRUE(int8_min.AssignIfValid(&int16_dest));
776 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::min()), int16_dest);
777 EXPECT_FALSE(double_max.AssignIfValid(&uint8_dest));
778 EXPECT_FALSE(double_max.AssignIfValid(&int16_dest));
779 EXPECT_TRUE(double_max.AssignIfValid(&double_dest));
780 EXPECT_EQ(numeric_limits<double>::max(), double_dest);
767 } 781 }
768 782
769 TEST(SafeNumerics, SaturatedCastChecks) { 783 TEST(SafeNumerics, SaturatedCastChecks) {
770 float not_a_number = std::numeric_limits<float>::infinity() - 784 float not_a_number = std::numeric_limits<float>::infinity() -
771 std::numeric_limits<float>::infinity(); 785 std::numeric_limits<float>::infinity();
772 EXPECT_TRUE(std::isnan(not_a_number)); 786 EXPECT_TRUE(std::isnan(not_a_number));
773 EXPECT_DEATH_IF_SUPPORTED( 787 EXPECT_DEATH_IF_SUPPORTED(
774 (saturated_cast<int, base::CheckOnFailure>(not_a_number)), 788 (saturated_cast<int, base::CheckOnFailure>(not_a_number)),
775 ""); 789 "");
776 } 790 }
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
885 EXPECT_EQ(static_cast<decltype(d)>(-.5), d); 899 EXPECT_EQ(static_cast<decltype(d)>(-.5), d);
886 auto e = CheckMod(CheckNum(20), 3).ValueOrDie(); 900 auto e = CheckMod(CheckNum(20), 3).ValueOrDie();
887 EXPECT_EQ(static_cast<decltype(e)>(2), e); 901 EXPECT_EQ(static_cast<decltype(e)>(2), e);
888 auto f = CheckLsh(1, CheckNum(2)).ValueOrDie(); 902 auto f = CheckLsh(1, CheckNum(2)).ValueOrDie();
889 EXPECT_EQ(static_cast<decltype(f)>(4), f); 903 EXPECT_EQ(static_cast<decltype(f)>(4), f);
890 auto g = CheckRsh(4, CheckNum(2)).ValueOrDie(); 904 auto g = CheckRsh(4, CheckNum(2)).ValueOrDie();
891 EXPECT_EQ(static_cast<decltype(g)>(1), g); 905 EXPECT_EQ(static_cast<decltype(g)>(1), g);
892 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); 906 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie();
893 EXPECT_EQ(static_cast<decltype(h)>(1), h); 907 EXPECT_EQ(static_cast<decltype(h)>(1), h);
894 } 908 }
OLDNEW
« base/numerics/safe_math.h ('K') | « base/numerics/safe_math.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698