Commit 4fd5e900 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

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
parent 555494f0
...@@ -41,7 +41,7 @@ constexpr T constexpr_min(T a) { ...@@ -41,7 +41,7 @@ constexpr T constexpr_min(T a) {
} }
template <typename T, typename... Ts> template <typename T, typename... Ts>
constexpr T constexpr_min(T a, T b, Ts... ts) { 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 { namespace detail {
......
...@@ -23,6 +23,24 @@ namespace { ...@@ -23,6 +23,24 @@ namespace {
class ConstexprMathTest : public testing::Test {}; 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<const uint16_t, decltype(a)>::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<const uint16_t, decltype(a)>::value));
}
TEST_F(ConstexprMathTest, constexpr_abs_unsigned) { TEST_F(ConstexprMathTest, constexpr_abs_unsigned) {
constexpr auto v = uint32_t(17); constexpr auto v = uint32_t(17);
constexpr auto a = folly::constexpr_abs(v); constexpr auto a = folly::constexpr_abs(v);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment