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

Unified Diff: base/numerics/safe_numerics_unittest.cc

Issue 2522073004: Add variadic helper functions for CheckedNumeric math operations (Closed)
Patch Set: test fixes Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« base/numerics/safe_math.h ('K') | « base/numerics/safe_math_impl.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/numerics/safe_numerics_unittest.cc
diff --git a/base/numerics/safe_numerics_unittest.cc b/base/numerics/safe_numerics_unittest.cc
index 430f7700b5d08e4c53d9cb90752e9f2172dd9455..8742002bc011f4f4b4d418cd98471c9ef9a132d5 100644
--- a/base/numerics/safe_numerics_unittest.cc
+++ b/base/numerics/safe_numerics_unittest.cc
@@ -818,3 +818,24 @@ TEST(SafeNumerics, CompoundNumericOperations) {
too_large /= d;
EXPECT_FALSE(too_large.IsValid());
}
+
+TEST(SafeNumerics, VariadicNumericOperations) {
+ 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.
+ EXPECT_EQ(static_cast<decltype(a)>(10), a);
+ auto b = base::CheckedSub(20, 2UL, 4).ValueOrDie();
+ EXPECT_EQ(static_cast<decltype(b)>(14.0), b);
+ auto c = base::CheckedMul(20, 1, 5, 3UL).ValueOrDie();
+ EXPECT_EQ(static_cast<decltype(c)>(300.0), c);
+ auto d = base::CheckedDiv(20, 2.0, 5LL, -4).ValueOrDie();
+ EXPECT_EQ(static_cast<decltype(d)>(-.5), d);
+ auto e = base::CheckedMod(20, 3).ValueOrDie();
+ EXPECT_EQ(static_cast<decltype(e)>(2), e);
+ auto f = base::CheckedLsh(1, 2).ValueOrDie();
+ EXPECT_EQ(static_cast<decltype(f)>(4), f);
+ auto g = base::CheckedRsh(4, 2).ValueOrDie();
+ EXPECT_EQ(static_cast<decltype(g)>(1), g);
+ auto h =
+ base::CheckedRsh(base::CheckedAdd(1, 1, 1, 1), base::CheckedSub(4, 2))
+ .ValueOrDie();
+ EXPECT_EQ(static_cast<decltype(h)>(1), h);
+}
« base/numerics/safe_math.h ('K') | « base/numerics/safe_math_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698