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_clamp) {
45 constexpr auto lo = uint16_t(3);
46 constexpr auto hi = uint16_t(7);
47 constexpr auto x = folly::constexpr_clamp(uint16_t(2), lo, hi);
48 constexpr auto y = folly::constexpr_clamp(uint16_t(5), lo, hi);
49 constexpr auto z = folly::constexpr_clamp(uint16_t(8), lo, hi);
53 EXPECT_TRUE((std::is_same<const uint16_t, decltype(y)>::value));
56 TEST_F(ConstexprMathTest, constexpr_abs_unsigned) {
57 constexpr auto v = uint32_t(17);
58 constexpr auto a = folly::constexpr_abs(v);
60 EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
63 TEST_F(ConstexprMathTest, constexpr_abs_signed_positive) {
64 constexpr auto v = int32_t(17);
65 constexpr auto a = folly::constexpr_abs(v);
67 EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
70 TEST_F(ConstexprMathTest, constexpr_abs_signed_negative) {
71 constexpr auto v = int32_t(-17);
72 constexpr auto a = folly::constexpr_abs(v);
74 EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
77 TEST_F(ConstexprMathTest, constexpr_abs_float_positive) {
78 constexpr auto v = 17.5f;
79 constexpr auto a = folly::constexpr_abs(v);
81 EXPECT_TRUE((std::is_same<const float, decltype(a)>::value));
84 TEST_F(ConstexprMathTest, constexpr_abs_float_negative) {
85 constexpr auto v = -17.5f;
86 constexpr auto a = folly::constexpr_abs(v);
88 EXPECT_TRUE((std::is_same<const float, decltype(a)>::value));
91 TEST_F(ConstexprMathTest, constexpr_abs_double_positive) {
92 constexpr auto v = 17.5;
93 constexpr auto a = folly::constexpr_abs(v);
95 EXPECT_TRUE((std::is_same<const double, decltype(a)>::value));
98 TEST_F(ConstexprMathTest, constexpr_abs_double_negative) {
99 constexpr auto v = -17.5;
100 constexpr auto a = folly::constexpr_abs(v);
102 EXPECT_TRUE((std::is_same<const double, decltype(a)>::value));
105 TEST_F(ConstexprMathTest, constexpr_log2_1) {
106 constexpr auto v = 1ull;
107 constexpr auto a = folly::constexpr_log2(v);
109 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
112 TEST_F(ConstexprMathTest, constexpr_log2_2) {
113 constexpr auto v = 2ull;
114 constexpr auto a = folly::constexpr_log2(v);
116 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
119 TEST_F(ConstexprMathTest, constexpr_log2_64) {
120 constexpr auto v = 64ull;
121 constexpr auto a = folly::constexpr_log2(v);
123 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));