proxygen
BitsTest.cpp File Reference
#include <algorithm>
#include <random>
#include <vector>
#include <folly/Random.h>
#include <folly/lang/Bits.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST (Bits, FindFirstSet)
 
 TEST (Bits, FindLastSet)
 
 TEST (Bits, nextPowTwoClz)
 
 TEST (Bits, prevPowTwoClz)
 
 TEST (Bits, isPowTwo)
 
 TEST (Bits, popcount)
 
 TEST (Bits, Endian_swap_uint)
 
 TEST (Bits, Endian_swap_real)
 
uint64_t reverse_simple (uint64_t v)
 
 TEST (Bits, BitReverse)
 
 TEST (Bits, PartialLoadUnaligned)
 

Function Documentation

uint64_t reverse_simple ( uint64_t  v)

Definition at line 191 of file BitsTest.cpp.

References i, and uint64_t.

Referenced by TEST().

191  {
192  uint64_t r = 0;
193 
194  for (int i = 0; i < 64; i++) {
195  r <<= 1;
196  r |= v & 1;
197  v >>= 1;
198  }
199  return r;
200 }
TEST ( Bits  ,
FindFirstSet   
)

Definition at line 70 of file BitsTest.cpp.

70  {
71  testFFS<char>();
72  testFFS<signed char>();
73  testFFS<unsigned char>();
74  testFFS<short>();
75  testFFS<unsigned short>();
76  testFFS<int>();
77  testFFS<unsigned int>();
78  testFFS<long>();
79  testFFS<unsigned long>();
80  testFFS<long long>();
81  testFFS<unsigned long long>();
82 }
TEST ( Bits  ,
FindLastSet   
)

Definition at line 84 of file BitsTest.cpp.

84  {
85  testFLS<char>();
86  testFLS<signed char>();
87  testFLS<unsigned char>();
88  testFLS<short>();
89  testFLS<unsigned short>();
90  testFLS<int>();
91  testFLS<unsigned int>();
92  testFLS<long>();
93  testFLS<unsigned long>();
94  testFLS<long long>();
95  testFLS<unsigned long long>();
96 }
TEST ( Bits  ,
nextPowTwoClz   
)

Definition at line 98 of file BitsTest.cpp.

References EXPECT_EQ, and folly::nextPowTwo().

98  {
99  EXPECT_EQ(1, nextPowTwo(0u));
100  EXPECT_EQ(1, nextPowTwo(1u));
101  EXPECT_EQ(2, nextPowTwo(2u));
102  EXPECT_EQ(4, nextPowTwo(3u));
103  EXPECT_EQ(4, nextPowTwo(4u));
104  EXPECT_EQ(8, nextPowTwo(5u));
105  EXPECT_EQ(8, nextPowTwo(6u));
106  EXPECT_EQ(8, nextPowTwo(7u));
107  EXPECT_EQ(8, nextPowTwo(8u));
108  EXPECT_EQ(16, nextPowTwo(9u));
109  EXPECT_EQ(16, nextPowTwo(13u));
110  EXPECT_EQ(16, nextPowTwo(16u));
111  EXPECT_EQ(512, nextPowTwo(510u));
112  EXPECT_EQ(512, nextPowTwo(511u));
113  EXPECT_EQ(512, nextPowTwo(512u));
114  EXPECT_EQ(1024, nextPowTwo(513u));
115  EXPECT_EQ(1024, nextPowTwo(777u));
116  EXPECT_EQ(1ul << 31, nextPowTwo((1ul << 31) - 1));
117  EXPECT_EQ(1ull << 32, nextPowTwo((1ull << 32) - 1));
118  EXPECT_EQ(1ull << 63, nextPowTwo((1ull << 62) + 1));
119 }
constexpr T nextPowTwo(T const v)
Definition: Bits.h:149
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Bits  ,
prevPowTwoClz   
)

Definition at line 121 of file BitsTest.cpp.

References EXPECT_EQ, and folly::prevPowTwo().

121  {
122  EXPECT_EQ(0, prevPowTwo(0u));
123  EXPECT_EQ(1, prevPowTwo(1u));
124  EXPECT_EQ(2, prevPowTwo(2u));
125  EXPECT_EQ(2, prevPowTwo(3u));
126  EXPECT_EQ(4, prevPowTwo(4u));
127  EXPECT_EQ(4, prevPowTwo(5u));
128  EXPECT_EQ(4, prevPowTwo(6u));
129  EXPECT_EQ(4, prevPowTwo(7u));
130  EXPECT_EQ(8, prevPowTwo(8u));
131  EXPECT_EQ(8, prevPowTwo(9u));
132  EXPECT_EQ(8, prevPowTwo(13u));
133  EXPECT_EQ(16, prevPowTwo(16u));
134  EXPECT_EQ(256, prevPowTwo(510u));
135  EXPECT_EQ(256, prevPowTwo(511u));
136  EXPECT_EQ(512, prevPowTwo(512u));
137  EXPECT_EQ(512, prevPowTwo(513u));
138  EXPECT_EQ(512, prevPowTwo(777u));
139  EXPECT_EQ(1ul << 30, prevPowTwo((1ul << 31) - 1));
140  EXPECT_EQ(1ull << 31, prevPowTwo((1ull << 32) - 1));
141  EXPECT_EQ(1ull << 62, prevPowTwo((1ull << 62) + 1));
142 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr T prevPowTwo(T const v)
Definition: Bits.h:155
TEST ( Bits  ,
isPowTwo   
)

Definition at line 144 of file BitsTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, and folly::isPowTwo().

144  {
145  EXPECT_FALSE(isPowTwo(0u));
146  EXPECT_TRUE(isPowTwo(1ul));
147  EXPECT_TRUE(isPowTwo(2ull));
148  EXPECT_FALSE(isPowTwo(3ul));
149  EXPECT_TRUE(isPowTwo(4ul));
150  EXPECT_FALSE(isPowTwo(5ul));
151  EXPECT_TRUE(isPowTwo(8ul));
152  EXPECT_FALSE(isPowTwo(15u));
153  EXPECT_TRUE(isPowTwo(16u));
154  EXPECT_FALSE(isPowTwo(17u));
155  EXPECT_FALSE(isPowTwo(511ul));
156  EXPECT_TRUE(isPowTwo(512ul));
157  EXPECT_FALSE(isPowTwo(513ul));
158  EXPECT_FALSE(isPowTwo((1ul << 31) - 1));
159  EXPECT_TRUE(isPowTwo(1ul << 31));
160  EXPECT_FALSE(isPowTwo((1ul << 31) + 1));
161  EXPECT_FALSE(isPowTwo((1ull << 63) - 1));
162  EXPECT_TRUE(isPowTwo(1ull << 63));
163  EXPECT_FALSE(isPowTwo((1ull << 63) + 1));
164 }
constexpr bool isPowTwo(T const v)
Definition: Bits.h:161
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Bits  ,
popcount   
)

Definition at line 166 of file BitsTest.cpp.

References EXPECT_EQ, folly::popcount(), uint32_t, and uint64_t.

166  {
167  EXPECT_EQ(0, popcount(0U));
168  EXPECT_EQ(1, popcount(1U));
169  EXPECT_EQ(32, popcount(uint32_t(-1)));
170  EXPECT_EQ(64, popcount(uint64_t(-1)));
171 }
constexpr unsigned int popcount(T const v)
Definition: Bits.h:130
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Bits  ,
Endian_swap_uint   
)

Definition at line 173 of file BitsTest.cpp.

References EXPECT_EQ, folly::Endian::swap(), uint16_t, uint32_t, uint64_t, and uint8_t.

173  {
174  EXPECT_EQ(uint8_t(0xda), Endian::swap(uint8_t(0xda)));
175  EXPECT_EQ(uint16_t(0x4175), Endian::swap(uint16_t(0x7541)));
176  EXPECT_EQ(uint32_t(0x42efb918), Endian::swap(uint32_t(0x18b9ef42)));
177  EXPECT_EQ(
178  uint64_t(0xa244f5e862c71d8a), Endian::swap(uint64_t(0x8a1dc762e8f544a2)));
179 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
TEST ( Bits  ,
Endian_swap_real   
)

Definition at line 181 of file BitsTest.cpp.

References EXPECT_EQ, EXPECT_NE, f, rng, and folly::Endian::swap().

181  {
182  std::mt19937_64 rng;
183  auto f = std::uniform_real_distribution<float>()(rng);
186  auto d = std::uniform_real_distribution<double>()(rng);
187  EXPECT_NE(d, Endian::swap(d));
189 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
auto rng
Definition: CollectTest.cpp:31
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
TEST ( Bits  ,
BitReverse   
)

Definition at line 202 of file BitsTest.cpp.

References b, folly::bitReverse(), EXPECT_EQ, i, folly::Random::rand32(), folly::Random::rand64(), reverse_simple(), uint32_t, and uint64_t.

202  {
203  EXPECT_EQ(folly::bitReverse<uint8_t>(0), 0);
204  EXPECT_EQ(folly::bitReverse<uint8_t>(1), 128);
205  for (int i = 0; i < 100; i++) {
210  }
211 }
char b
T bitReverse(T n)
Definition: Bits.h:369
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint64_t reverse_simple(uint64_t v)
Definition: BitsTest.cpp:191
static uint32_t rand32()
Definition: Random.h:213
static uint64_t rand64()
Definition: Random.h:263
TEST ( Bits  ,
PartialLoadUnaligned   
)

Definition at line 213 of file BitsTest.cpp.

References EXPECT_EQ, folly::Random::rand32(), and uint64_t.

213  {
214  std::vector<char> buf(128);
215  std::generate(
216  buf.begin(), buf.end(), [] { return folly::Random::rand32(255); });
217  for (size_t l = 0; l < 8; ++l) {
218  for (size_t pos = 0; pos <= buf.size() - l; ++pos) {
219  auto p = buf.data() + pos;
220  auto x = folly::partialLoadUnaligned<uint64_t>(p, l);
221 
222  uint64_t expected = 0;
223  memcpy(&expected, p, l);
224 
225  EXPECT_EQ(x, expected);
226 
227  if (l < 4) {
228  auto x32 = folly::partialLoadUnaligned<uint32_t>(p, l);
229  EXPECT_EQ(x32, static_cast<uint32_t>(expected));
230  }
231  }
232  }
233 }
Definition: InvokeTest.cpp:58
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static uint32_t rand32()
Definition: Random.h:213