constexpr_pow
[folly.git] / folly / test / ConstexprMathTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/ConstexprMath.h>
18
19 #include <folly/portability/GTest.h>
20
21 namespace {
22
23 class ConstexprMathTest : public testing::Test {};
24 } // namespace
25
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);
31   EXPECT_EQ(3, a);
32   EXPECT_TRUE((std::is_same<const uint16_t, decltype(a)>::value));
33 }
34
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);
40   EXPECT_EQ(7, a);
41   EXPECT_TRUE((std::is_same<const uint16_t, decltype(a)>::value));
42 }
43
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);
50   EXPECT_EQ(3, x);
51   EXPECT_EQ(5, y);
52   EXPECT_EQ(7, z);
53   EXPECT_TRUE((std::is_same<const uint16_t, decltype(y)>::value));
54 }
55
56 TEST_F(ConstexprMathTest, constexpr_abs_unsigned) {
57   constexpr auto v = uint32_t(17);
58   constexpr auto a = folly::constexpr_abs(v);
59   EXPECT_EQ(17, a);
60   EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
61 }
62
63 TEST_F(ConstexprMathTest, constexpr_abs_signed_positive) {
64   constexpr auto v = int32_t(17);
65   constexpr auto a = folly::constexpr_abs(v);
66   EXPECT_EQ(17, a);
67   EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
68 }
69
70 TEST_F(ConstexprMathTest, constexpr_abs_signed_negative) {
71   constexpr auto v = int32_t(-17);
72   constexpr auto a = folly::constexpr_abs(v);
73   EXPECT_EQ(17, a);
74   EXPECT_TRUE((std::is_same<const uint32_t, decltype(a)>::value));
75 }
76
77 TEST_F(ConstexprMathTest, constexpr_abs_float_positive) {
78   constexpr auto v = 17.5f;
79   constexpr auto a = folly::constexpr_abs(v);
80   EXPECT_EQ(17.5, a);
81   EXPECT_TRUE((std::is_same<const float, decltype(a)>::value));
82 }
83
84 TEST_F(ConstexprMathTest, constexpr_abs_float_negative) {
85   constexpr auto v = -17.5f;
86   constexpr auto a = folly::constexpr_abs(v);
87   EXPECT_EQ(17.5, a);
88   EXPECT_TRUE((std::is_same<const float, decltype(a)>::value));
89 }
90
91 TEST_F(ConstexprMathTest, constexpr_abs_double_positive) {
92   constexpr auto v = 17.5;
93   constexpr auto a = folly::constexpr_abs(v);
94   EXPECT_EQ(17.5, a);
95   EXPECT_TRUE((std::is_same<const double, decltype(a)>::value));
96 }
97
98 TEST_F(ConstexprMathTest, constexpr_abs_double_negative) {
99   constexpr auto v = -17.5;
100   constexpr auto a = folly::constexpr_abs(v);
101   EXPECT_EQ(17.5, a);
102   EXPECT_TRUE((std::is_same<const double, decltype(a)>::value));
103 }
104
105 TEST_F(ConstexprMathTest, constexpr_log2_1) {
106   constexpr auto v = 1ull;
107   constexpr auto a = folly::constexpr_log2(v);
108   EXPECT_EQ(0ull, a);
109   EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
110 }
111
112 TEST_F(ConstexprMathTest, constexpr_log2_2) {
113   constexpr auto v = 2ull;
114   constexpr auto a = folly::constexpr_log2(v);
115   EXPECT_EQ(1ull, a);
116   EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
117 }
118
119 TEST_F(ConstexprMathTest, constexpr_log2_64) {
120   constexpr auto v = 64ull;
121   constexpr auto a = folly::constexpr_log2(v);
122   EXPECT_EQ(6ull, a);
123   EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
124 }
125
126 TEST_F(ConstexprMathTest, constexpr_log2_ceil_1) {
127   constexpr auto v = 1ull;
128   constexpr auto a = folly::constexpr_log2_ceil(v);
129   EXPECT_EQ(0ull, a);
130   EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
131 }
132
133 TEST_F(ConstexprMathTest, constexpr_log2_ceil_2) {
134   constexpr auto v = 2ull;
135   constexpr auto a = folly::constexpr_log2_ceil(v);
136   EXPECT_EQ(1ull, a);
137   EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
138 }
139
140 TEST_F(ConstexprMathTest, constexpr_log2_ceil_3) {
141   constexpr auto v = 3ull;
142   constexpr auto a = folly::constexpr_log2_ceil(v);
143   EXPECT_EQ(2ull, a);
144   EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
145 }
146
147 TEST_F(ConstexprMathTest, constexpr_log2_ceil_63) {
148   constexpr auto v = 63ull;
149   constexpr auto a = folly::constexpr_log2_ceil(v);
150   EXPECT_EQ(6ull, a);
151   EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
152 }
153
154 TEST_F(ConstexprMathTest, constexpr_log2_ceil_64) {
155   constexpr auto v = 64ull;
156   constexpr auto a = folly::constexpr_log2_ceil(v);
157   EXPECT_EQ(6ull, a);
158   EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
159 }
160
161 TEST_F(ConstexprMathTest, constexpr_ceil) {
162   {
163     constexpr auto roundable = 20ull;
164     constexpr auto round = 6ull;
165     constexpr auto rounded = folly::constexpr_ceil(roundable, round);
166     EXPECT_EQ(24ull, rounded);
167   }
168   {
169     constexpr auto roundable = -20ll;
170     constexpr auto round = 6ll;
171     constexpr auto rounded = folly::constexpr_ceil(roundable, round);
172     EXPECT_EQ(-18ll, rounded);
173   }
174   {
175     constexpr auto roundable = -20ll;
176     constexpr auto round = 0ll;
177     constexpr auto rounded = folly::constexpr_ceil(roundable, round);
178     EXPECT_EQ(-20ll, rounded);
179   }
180 }
181
182 TEST_F(ConstexprMathTest, constexpr_pow) {
183   {
184     constexpr auto a = folly::constexpr_pow(uint64_t(0), 15);
185     EXPECT_EQ(0, a);
186   }
187   {
188     constexpr auto a = folly::constexpr_pow(uint64_t(15), 0);
189     EXPECT_EQ(1, a);
190   }
191   {
192     constexpr auto a = folly::constexpr_pow(uint64_t(2), 6);
193     EXPECT_EQ(64, a);
194   }
195 }