2 * Copyright 2017-present 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));
126 TEST_F(ConstexprMathTest, constexpr_log2_ceil_1) {
127 constexpr auto v = 1ull;
128 constexpr auto a = folly::constexpr_log2_ceil(v);
130 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
133 TEST_F(ConstexprMathTest, constexpr_log2_ceil_2) {
134 constexpr auto v = 2ull;
135 constexpr auto a = folly::constexpr_log2_ceil(v);
137 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
140 TEST_F(ConstexprMathTest, constexpr_log2_ceil_3) {
141 constexpr auto v = 3ull;
142 constexpr auto a = folly::constexpr_log2_ceil(v);
144 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
147 TEST_F(ConstexprMathTest, constexpr_log2_ceil_63) {
148 constexpr auto v = 63ull;
149 constexpr auto a = folly::constexpr_log2_ceil(v);
151 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
154 TEST_F(ConstexprMathTest, constexpr_log2_ceil_64) {
155 constexpr auto v = 64ull;
156 constexpr auto a = folly::constexpr_log2_ceil(v);
158 EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
161 TEST_F(ConstexprMathTest, constexpr_ceil) {
163 constexpr auto roundable = 20ull;
164 constexpr auto round = 6ull;
165 constexpr auto rounded = folly::constexpr_ceil(roundable, round);
166 EXPECT_EQ(24ull, rounded);
169 constexpr auto roundable = -20ll;
170 constexpr auto round = 6ll;
171 constexpr auto rounded = folly::constexpr_ceil(roundable, round);
172 EXPECT_EQ(-18ll, rounded);
175 constexpr auto roundable = -20ll;
176 constexpr auto round = 0ll;
177 constexpr auto rounded = folly::constexpr_ceil(roundable, round);
178 EXPECT_EQ(-20ll, rounded);
182 TEST_F(ConstexprMathTest, constexpr_pow) {
184 constexpr auto a = folly::constexpr_pow(uint64_t(0), 15);
188 constexpr auto a = folly::constexpr_pow(uint64_t(15), 0);
192 constexpr auto a = folly::constexpr_pow(uint64_t(2), 6);