From 4fd5e900005f59337d3b22f3ec8ef6b182486d81 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sat, 2 Sep 2017 02:11:34 -0700 Subject: [PATCH] Fix constexpr_min after D5739715 Summary: [Folly] Fix `constexpr_min` after {D5739715} (facebook/folly@6d7c6d55f0f4b7b75607608ef9037db58083368f). Add new unit-tests for `constexpr_min` and `constexpr_max` to avoid silly mistakes like this in the future. It was defined recursively in terms of `constexpr_max` rather than, as intended, in terns of `constexpr_min`. HT Mandar12. Reviewed By: Orvid Differential Revision: D5761831 fbshipit-source-id: 2dad5833e05679232b3c529d33325a0205c2e4e4 --- folly/ConstexprMath.h | 2 +- folly/test/ConstexprMathTest.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/folly/ConstexprMath.h b/folly/ConstexprMath.h index 1650c02c..150acca3 100644 --- a/folly/ConstexprMath.h +++ b/folly/ConstexprMath.h @@ -41,7 +41,7 @@ constexpr T constexpr_min(T a) { } template constexpr T constexpr_min(T a, T b, Ts... ts) { - return b < a ? constexpr_max(b, ts...) : constexpr_max(a, ts...); + return b < a ? constexpr_min(b, ts...) : constexpr_min(a, ts...); } namespace detail { diff --git a/folly/test/ConstexprMathTest.cpp b/folly/test/ConstexprMathTest.cpp index 166175a3..73b6c90f 100644 --- a/folly/test/ConstexprMathTest.cpp +++ b/folly/test/ConstexprMathTest.cpp @@ -23,6 +23,24 @@ namespace { class ConstexprMathTest : public testing::Test {}; } +TEST_F(ConstexprMathTest, constexpr_min) { + constexpr auto x = uint16_t(3); + constexpr auto y = uint16_t(7); + constexpr auto z = uint16_t(4); + constexpr auto a = folly::constexpr_min(x, y, z); + EXPECT_EQ(3, a); + EXPECT_TRUE((std::is_same::value)); +} + +TEST_F(ConstexprMathTest, constexpr_max) { + constexpr auto x = uint16_t(3); + constexpr auto y = uint16_t(7); + constexpr auto z = uint16_t(4); + constexpr auto a = folly::constexpr_max(x, y, z); + EXPECT_EQ(7, a); + EXPECT_TRUE((std::is_same::value)); +} + TEST_F(ConstexprMathTest, constexpr_abs_unsigned) { constexpr auto v = uint32_t(17); constexpr auto a = folly::constexpr_abs(v); -- 2.34.1