Chromium Code Reviews| 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 800 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 811 | 811 |
| 812 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); | 812 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); |
| 813 EXPECT_TRUE(too_large.IsValid()); | 813 EXPECT_TRUE(too_large.IsValid()); |
| 814 too_large += d; | 814 too_large += d; |
| 815 EXPECT_FALSE(too_large.IsValid()); | 815 EXPECT_FALSE(too_large.IsValid()); |
| 816 too_large -= d; | 816 too_large -= d; |
| 817 EXPECT_FALSE(too_large.IsValid()); | 817 EXPECT_FALSE(too_large.IsValid()); |
| 818 too_large /= d; | 818 too_large /= d; |
| 819 EXPECT_FALSE(too_large.IsValid()); | 819 EXPECT_FALSE(too_large.IsValid()); |
| 820 } | 820 } |
| 821 | |
| 822 TEST(SafeNumerics, VariadicNumericOperations) { | |
| 823 auto a = base::CheckedAdd(1, 2UL, 3LL, 4).ValueOrDie(); | |
|
Tom Sepez
2016/11/23 16:52:06
Can we throw a checked numeric argument into each
jschuh
2016/11/23 19:18:00
Sounds good. I was going to add a simple wrapper t
jschuh
2016/11/23 21:49:22
Done.
| |
| 824 EXPECT_EQ(static_cast<decltype(a)>(10), a); | |
| 825 auto b = base::CheckedSub(20, 2UL, 4).ValueOrDie(); | |
| 826 EXPECT_EQ(static_cast<decltype(b)>(14.0), b); | |
| 827 auto c = base::CheckedMul(20, 1, 5, 3UL).ValueOrDie(); | |
| 828 EXPECT_EQ(static_cast<decltype(c)>(300.0), c); | |
| 829 auto d = base::CheckedDiv(20, 2.0, 5LL, -4).ValueOrDie(); | |
| 830 EXPECT_EQ(static_cast<decltype(d)>(-.5), d); | |
| 831 auto e = base::CheckedMod(20, 3).ValueOrDie(); | |
| 832 EXPECT_EQ(static_cast<decltype(e)>(2), e); | |
| 833 auto f = base::CheckedLsh(1, 2).ValueOrDie(); | |
| 834 EXPECT_EQ(static_cast<decltype(f)>(4), f); | |
| 835 auto g = base::CheckedRsh(4, 2).ValueOrDie(); | |
| 836 EXPECT_EQ(static_cast<decltype(g)>(1), g); | |
| 837 auto h = | |
| 838 base::CheckedRsh(base::CheckedAdd(1, 1, 1, 1), base::CheckedSub(4, 2)) | |
| 839 .ValueOrDie(); | |
| 840 EXPECT_EQ(static_cast<decltype(h)>(1), h); | |
| 841 } | |
| OLD | NEW |