2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/ConstexprMath.h>
19 #include <folly/portability/GTest.h>
23 class ConstexprMathTest : public testing::Test {};
26 TEST_F(ConstexprMathTest, constexpr_min) {
27 constexpr auto x = uint16_t(3);
28 constexpr auto y = uint16_t(7);
29 constexpr auto z = uint16_t(4);
30 constexpr auto a = folly::constexpr_min(x, y, z);
32 EXPECT_TRUE((std::is_same<const uint16_t, decltype(a)>::value));
35 TEST_F(ConstexprMathTest, constexpr_max) {
36 constexpr auto x = uint16_t(3);
37 constexpr auto y = uint16_t(7);
38 constexpr auto z = uint16_t(4);
39 constexpr auto a = folly::constexpr_max(x, y, z);
41 EXPECT_TRUE((std::is_same<const uint16_t, decltype(a)>::value));
44 TEST_F(ConstexprMathTest, constexpr_abs_unsigned) {
45 constexpr auto v = uint32_t(17);
46 constexpr auto a = folly::constexpr_abs(v);
48 EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
51 TEST_F(ConstexprMathTest, constexpr_abs_signed_positive) {
52 constexpr auto v = int32_t(17);
53 constexpr auto a = folly::constexpr_abs(v);
55 EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
58 TEST_F(ConstexprMathTest, constexpr_abs_signed_negative) {
59 constexpr auto v = int32_t(-17);
60 constexpr auto a = folly::constexpr_abs(v);
62 EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
65 TEST_F(ConstexprMathTest, constexpr_abs_float_positive) {
66 constexpr auto v = 17.5f;
67 constexpr auto a = folly::constexpr_abs(v);
69 EXPECT_TRUE((std::is_same<const float, decltype(a)>::value));
72 TEST_F(ConstexprMathTest, constexpr_abs_float_negative) {
73 constexpr auto v = -17.5f;
74 constexpr auto a = folly::constexpr_abs(v);
76 EXPECT_TRUE((std::is_same<const float, decltype(a)>::value));
79 TEST_F(ConstexprMathTest, constexpr_abs_double_positive) {
80 constexpr auto v = 17.5;
81 constexpr auto a = folly::constexpr_abs(v);
83 EXPECT_TRUE((std::is_same<const double, decltype(a)>::value));
86 TEST_F(ConstexprMathTest, constexpr_abs_double_negative) {
87 constexpr auto v = -17.5;
88 constexpr auto a = folly::constexpr_abs(v);
90 EXPECT_TRUE((std::is_same<const double, decltype(a)>::value));
93 TEST_F(ConstexprMathTest, constexpr_log2_1) {
94 constexpr auto v = 1ull;
95 constexpr auto a = folly::constexpr_log2(v);
97 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
100 TEST_F(ConstexprMathTest, constexpr_log2_2) {
101 constexpr auto v = 2ull;
102 constexpr auto a = folly::constexpr_log2(v);
104 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
107 TEST_F(ConstexprMathTest, constexpr_log2_64) {
108 constexpr auto v = 64ull;
109 constexpr auto a = folly::constexpr_log2(v);
111 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));