Use the GTest portability headers
[folly.git] / folly / test / BitsTest.cpp
1 /*
2  * Copyright 2016 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 // @author Tudor Bosman (tudorb@fb.com)
18
19 #include <folly/Bits.h>
20
21 #include <folly/portability/GTest.h>
22
23 using namespace folly;
24
25 // Test constexpr-ness.
26 #if !defined(__clang__) && !defined(_MSC_VER)
27 static_assert(findFirstSet(2u) == 2, "findFirstSet");
28 static_assert(findLastSet(2u) == 2, "findLastSet");
29 static_assert(nextPowTwo(2u) == 2, "nextPowTwo");
30 #endif
31
32 #ifndef __clang__
33 static_assert(isPowTwo(2u), "isPowTwo");
34 #endif
35
36 namespace {
37
38 template <class INT>
39 void testFFS() {
40   EXPECT_EQ(0, findFirstSet(static_cast<INT>(0)));
41   size_t bits = std::numeric_limits<
42     typename std::make_unsigned<INT>::type>::digits;
43   for (size_t i = 0; i < bits; i++) {
44     INT v = (static_cast<INT>(1) << (bits - 1)) |
45             (static_cast<INT>(1) << i);
46     EXPECT_EQ(i+1, findFirstSet(v));
47   }
48 }
49
50 template <class INT>
51 void testFLS() {
52   typedef typename std::make_unsigned<INT>::type UINT;
53   EXPECT_EQ(0, findLastSet(static_cast<INT>(0)));
54   size_t bits = std::numeric_limits<UINT>::digits;
55   for (size_t i = 0; i < bits; i++) {
56     INT v1 = static_cast<UINT>(1) << i;
57     EXPECT_EQ(i + 1, findLastSet(v1));
58
59     INT v2 = (static_cast<UINT>(1) << i) - 1;
60     EXPECT_EQ(i, findLastSet(v2));
61   }
62 }
63
64 }  // namespace
65
66 TEST(Bits, FindFirstSet) {
67   testFFS<char>();
68   testFFS<signed char>();
69   testFFS<unsigned char>();
70   testFFS<short>();
71   testFFS<unsigned short>();
72   testFFS<int>();
73   testFFS<unsigned int>();
74   testFFS<long>();
75   testFFS<unsigned long>();
76   testFFS<long long>();
77   testFFS<unsigned long long>();
78 }
79
80 TEST(Bits, FindLastSet) {
81   testFLS<char>();
82   testFLS<signed char>();
83   testFLS<unsigned char>();
84   testFLS<short>();
85   testFLS<unsigned short>();
86   testFLS<int>();
87   testFLS<unsigned int>();
88   testFLS<long>();
89   testFLS<unsigned long>();
90   testFLS<long long>();
91   testFLS<unsigned long long>();
92 }
93
94 TEST(Bits, nextPowTwoClz) {
95   EXPECT_EQ(1, nextPowTwo(0u));
96   EXPECT_EQ(1, nextPowTwo(1u));
97   EXPECT_EQ(2, nextPowTwo(2u));
98   EXPECT_EQ(4, nextPowTwo(3u));
99   EXPECT_EQ(4, nextPowTwo(4u));
100   EXPECT_EQ(8, nextPowTwo(5u));
101   EXPECT_EQ(8, nextPowTwo(6u));
102   EXPECT_EQ(8, nextPowTwo(7u));
103   EXPECT_EQ(8, nextPowTwo(8u));
104   EXPECT_EQ(16, nextPowTwo(9u));
105   EXPECT_EQ(16, nextPowTwo(13u));
106   EXPECT_EQ(16, nextPowTwo(16u));
107   EXPECT_EQ(512, nextPowTwo(510u));
108   EXPECT_EQ(512, nextPowTwo(511u));
109   EXPECT_EQ(512, nextPowTwo(512u));
110   EXPECT_EQ(1024, nextPowTwo(513u));
111   EXPECT_EQ(1024, nextPowTwo(777u));
112   EXPECT_EQ(1ul << 31, nextPowTwo((1ul << 31) - 1));
113   EXPECT_EQ(1ull << 32, nextPowTwo((1ull << 32) - 1));
114   EXPECT_EQ(1ull << 63, nextPowTwo((1ull << 62) + 1));
115 }
116
117 TEST(Bits, isPowTwo) {
118   EXPECT_FALSE(isPowTwo(0u));
119   EXPECT_TRUE(isPowTwo(1ul));
120   EXPECT_TRUE(isPowTwo(2ull));
121   EXPECT_FALSE(isPowTwo(3ul));
122   EXPECT_TRUE(isPowTwo(4ul));
123   EXPECT_FALSE(isPowTwo(5ul));
124   EXPECT_TRUE(isPowTwo(8ul));
125   EXPECT_FALSE(isPowTwo(15u));
126   EXPECT_TRUE(isPowTwo(16u));
127   EXPECT_FALSE(isPowTwo(17u));
128   EXPECT_FALSE(isPowTwo(511ul));
129   EXPECT_TRUE(isPowTwo(512ul));
130   EXPECT_FALSE(isPowTwo(513ul));
131   EXPECT_FALSE(isPowTwo((1ul<<31) - 1));
132   EXPECT_TRUE(isPowTwo(1ul<<31));
133   EXPECT_FALSE(isPowTwo((1ul<<31) + 1));
134   EXPECT_FALSE(isPowTwo((1ull<<63) - 1));
135   EXPECT_TRUE(isPowTwo(1ull<<63));
136   EXPECT_FALSE(isPowTwo((1ull<<63) + 1));
137 }
138
139 TEST(Bits, popcount) {
140   EXPECT_EQ(0, popcount(0U));
141   EXPECT_EQ(1, popcount(1U));
142   EXPECT_EQ(32, popcount(uint32_t(-1)));
143   EXPECT_EQ(64, popcount(uint64_t(-1)));
144 }