proxygen
BitsTest.cpp File Reference
#include <cmath>
#include <folly/experimental/Bits.h>
#include <glog/logging.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

template<class T >
void runSimpleTest8 ()
 
 TEST (Bits, Simple8)
 
 TEST (Bits, SimpleUnaligned8)
 
template<class T >
void runSimpleTest64 ()
 
 TEST (Bits, Simple64)
 
 TEST (Bits, SimpleUnaligned64)
 
template<class T >
void runMultiBitTest8 ()
 
 TEST (Bits, MultiBit8)
 
 TEST (Bits, MultiBitUnaligned8)
 
template<class T >
void runSignedMultiBitTest8 ()
 
 TEST (Bits, SignedMultiBit8)
 
template<class T , class R = T>
void runMultiBitTest64 ()
 
 TEST (Bits, MultiBit64)
 
 TEST (Bits, MultiBitSigned64)
 
 TEST (Bits, MultiBitUnaligned64)
 
 TEST (Bits, Boundaries)
 
template<size_t N>
void accSize (size_t &w)
 
template<size_t N, typename T , bool NEG, bool aligned>
void testSetLoop (size_t &w, size_t bufSize, uint8_t *buf)
 
template<size_t N, typename T , bool NEG, bool aligned>
void testGetLoop (size_t &r, size_t bufSize, uint8_t *buf)
 
template<bool aligned>
void testConcatenation ()
 
 TEST (Bits, ConcatenationUnalignedUnsigned)
 
 TEST (Bits, ConcatenationAligned)
 
int main (int argc, char *argv[])
 

Function Documentation

template<size_t N>
void accSize ( size_t &  w)

Definition at line 263 of file BitsTest.cpp.

References s.

263  {
264  for (size_t s = 0; s <= N; ++s) {
265  w += s;
266  }
267 }
static set< string > s
int main ( int  argc,
char *  argv[] 
)

Definition at line 374 of file BitsTest.cpp.

References testing::InitGoogleTest(), and RUN_ALL_TESTS().

374  {
376  gflags::ParseCommandLineFlags(&argc, &argv, true);
377  return RUN_ALL_TESTS();
378 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
char ** argv
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
template<class T , class R = T>
void runMultiBitTest64 ( )

Definition at line 182 of file BitsTest.cpp.

References EXPECT_EQ, deadlock::load(), folly::Bits< T, Traits >::set(), and folly::T.

182  {
184  T buf[] = {0x123456789abcdef0, 0x13579bdf2468ace0};
185 
186  EXPECT_EQ(0x123456789abcdef0, load(Bits<T>::get(buf, 0, 64)));
187  EXPECT_EQ(0xf0, load(Bits<T>::get(buf, 0, 8)));
188  EXPECT_EQ(0x89abcdef, load(Bits<T>::get(buf, 4, 32)));
189  EXPECT_EQ(0x189abcdef, load(Bits<T>::get(buf, 4, 33)));
190 
191  Bits<T>::set(buf, 4, 31, 0x55555555);
192  EXPECT_EQ(0xd5555555, load(Bits<T>::get(buf, 4, 32)));
193  EXPECT_EQ(0x1d5555555, load(Bits<T>::get(buf, 4, 33)));
194  EXPECT_EQ(0xd55555550, load(Bits<T>::get(buf, 0, 36)));
195 
196  Bits<T>::set(buf, 0, 64, 0x23456789abcdef01);
197  EXPECT_EQ(0x23456789abcdef01, load(Bits<T>::get(buf, 0, 64)));
198 }
#define T(v)
Definition: http_parser.c:233
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
def load()
Definition: deadlock.py:441
template<class T >
void runMultiBitTest8 ( )

Definition at line 114 of file BitsTest.cpp.

References EXPECT_EQ, deadlock::load(), folly::Bits< T, Traits >::set(), and folly::T.

114  {
116  T buf[] = {0x12, 0x34, 0x56, 0x78};
117 
118  EXPECT_EQ(0x02, load(Bits<T>::get(buf, 0, 4)));
119  EXPECT_EQ(0x1a, load(Bits<T>::get(buf, 9, 5)));
120  EXPECT_EQ(0xb1, load(Bits<T>::get(buf, 13, 8)));
121 
122  Bits<T>::set(buf, 0, 4, 0x0b);
123  EXPECT_EQ(0x1b, load(buf[0]));
124  EXPECT_EQ(0x34, load(buf[1]));
125  EXPECT_EQ(0x56, load(buf[2]));
126  EXPECT_EQ(0x78, load(buf[3]));
127 
128  Bits<T>::set(buf, 9, 5, 0x0e);
129  EXPECT_EQ(0x1b, load(buf[0]));
130  EXPECT_EQ(0x1c, load(buf[1]));
131  EXPECT_EQ(0x56, load(buf[2]));
132  EXPECT_EQ(0x78, load(buf[3]));
133 
134  Bits<T>::set(buf, 13, 8, 0xaa);
135  EXPECT_EQ(0x1b, load(buf[0]));
136  EXPECT_EQ(0x5c, load(buf[1]));
137  EXPECT_EQ(0x55, load(buf[2]));
138  EXPECT_EQ(0x78, load(buf[3]));
139 }
#define T(v)
Definition: http_parser.c:233
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
def load()
Definition: deadlock.py:441
template<class T >
void runSignedMultiBitTest8 ( )

Definition at line 150 of file BitsTest.cpp.

References EXPECT_EQ, deadlock::load(), folly::Bits< T, Traits >::set(), and folly::T.

150  {
152  T buf[] = {0x12, 0x34, 0x56, 0x78};
153 
154  EXPECT_EQ(0x02, load(Bits<T>::get(buf, 0, 4)));
155  EXPECT_EQ(0x1a - 32, load(Bits<T>::get(buf, 9, 5)));
156  EXPECT_EQ(0xb1 - 256, load(Bits<T>::get(buf, 13, 8)));
157 
158  Bits<T>::set(buf, 0, 4, 0x0b - 0x10);
159  EXPECT_EQ(0x1b, load(buf[0]));
160  EXPECT_EQ(0x34, load(buf[1]));
161  EXPECT_EQ(0x56, load(buf[2]));
162  EXPECT_EQ(0x78, load(buf[3]));
163 
164  Bits<T>::set(buf, 9, 5, 0x0e);
165  EXPECT_EQ(0x1b, load(buf[0]));
166  EXPECT_EQ(0x1c, load(buf[1]));
167  EXPECT_EQ(0x56, load(buf[2]));
168  EXPECT_EQ(0x78, load(buf[3]));
169 
170  Bits<T>::set(buf, 13, 8, 0xaa - 0x100);
171  EXPECT_EQ(0x1b, load(buf[0]));
172  EXPECT_EQ(0x5c, load(buf[1]));
173  EXPECT_EQ(0x55, load(buf[2]));
174  EXPECT_EQ(0x78, load(buf[3]));
175 }
#define T(v)
Definition: http_parser.c:233
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
def load()
Definition: deadlock.py:441
template<class T >
void runSimpleTest64 ( )

Definition at line 69 of file BitsTest.cpp.

References folly::Bits< T, Traits >::clear(), EXPECT_EQ, deadlock::load(), folly::Bits< T, Traits >::set(), folly::T, and uint64_t.

69  {
71 
78  EXPECT_EQ(32, Bits<T>::blockCount(2048));
79  EXPECT_EQ(33, Bits<T>::blockCount(2049));
80 
84  EXPECT_EQ(63, Bits<T>::bitOffset(319));
87 
88  T buf[256];
89  std::fill(buf, buf + 256, T(0));
90 
91  Bits<T>::set(buf, 300);
92  Bits<T>::set(buf, 319);
93  EXPECT_EQ((uint64_t(1) << 44) | (uint64_t(1) << 63), load(buf[4]));
94  EXPECT_EQ(0, load(buf[5]));
95  Bits<T>::clear(buf, 319);
96  EXPECT_EQ(uint64_t(1) << 44, load(buf[4]));
97  EXPECT_EQ(0, load(buf[5]));
98  Bits<T>::set(buf, 320);
99  EXPECT_EQ(uint64_t(1) << 44, load(buf[4]));
100  EXPECT_EQ(1, load(buf[5]));
101 
102  EXPECT_EQ(2, Bits<T>::count(buf, buf + 256));
103 }
#define T(v)
Definition: http_parser.c:233
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
def load()
Definition: deadlock.py:441
template<class T >
void runSimpleTest8 ( )

Definition at line 28 of file BitsTest.cpp.

References folly::Bits< T, Traits >::clear(), EXPECT_EQ, deadlock::load(), folly::Bits< T, Traits >::set(), and folly::T.

28  {
30 
35  EXPECT_EQ(256, Bits<T>::blockCount(2048));
36  EXPECT_EQ(257, Bits<T>::blockCount(2049));
37 
42 
43  T buf[256];
44  std::fill(buf, buf + 256, T(0));
45 
46  Bits<T>::set(buf, 36);
47  Bits<T>::set(buf, 39);
48  EXPECT_EQ((1 << 7) | (1 << 4), load(buf[4]));
49  EXPECT_EQ(0, load(buf[5]));
50  Bits<T>::clear(buf, 39);
51  EXPECT_EQ(1 << 4, load(buf[4]));
52  EXPECT_EQ(0, load(buf[5]));
53  Bits<T>::set(buf, 40);
54  EXPECT_EQ(1 << 4, load(buf[4]));
55  EXPECT_EQ(1, load(buf[5]));
56 
57  EXPECT_EQ(2, Bits<T>::count(buf, buf + 256));
58 }
#define T(v)
Definition: http_parser.c:233
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
def load()
Definition: deadlock.py:441
TEST ( Bits  ,
Simple8   
)

Definition at line 60 of file BitsTest.cpp.

60  {
61  runSimpleTest8<uint8_t>();
62 }
TEST ( Bits  ,
SimpleUnaligned8   
)

Definition at line 64 of file BitsTest.cpp.

64  {
65  runSimpleTest8<Unaligned<uint8_t>>();
66 }
TEST ( Bits  ,
Simple64   
)

Definition at line 105 of file BitsTest.cpp.

105  {
106  runSimpleTest64<uint64_t>();
107 }
TEST ( Bits  ,
SimpleUnaligned64   
)

Definition at line 109 of file BitsTest.cpp.

109  {
110  runSimpleTest64<Unaligned<uint64_t>>();
111 }
TEST ( Bits  ,
MultiBit8   
)

Definition at line 141 of file BitsTest.cpp.

141  {
142  runMultiBitTest8<uint8_t>();
143 }
TEST ( Bits  ,
MultiBitUnaligned8   
)

Definition at line 145 of file BitsTest.cpp.

145  {
146  runMultiBitTest8<Unaligned<uint8_t>>();
147 }
TEST ( Bits  ,
SignedMultiBit8   
)

Definition at line 177 of file BitsTest.cpp.

177  {
178  runSignedMultiBitTest8<int8_t>();
179 }
TEST ( Bits  ,
MultiBit64   
)

Definition at line 200 of file BitsTest.cpp.

200  {
201  runMultiBitTest64<uint64_t>();
202 }
TEST ( Bits  ,
MultiBitSigned64   
)

Definition at line 204 of file BitsTest.cpp.

204  {
205  // runMultiBitTest64<int64_t>();
206 }
TEST ( Bits  ,
MultiBitUnaligned64   
)
TEST ( Bits  ,
Boundaries   
)

Definition at line 251 of file BitsTest.cpp.

References EXPECT_EQ, int32_t, folly::size(), uint8_t, and folly::value().

251  {
252  uint8_t buf[20];
253  for (size_t offset = 0; offset <= 64; ++offset) {
254  for (size_t size = 0; size <= 32; ++size) {
255  int32_t value = testValue<int32_t>(size);
256  testSet<true>(buf, offset, size, value);
257  EXPECT_EQ(value, (testGet<true, int32_t>(buf, offset, size)));
258  }
259  }
260 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
static const char *const value
Definition: Conv.cpp:50
TEST ( Bits  ,
ConcatenationUnalignedUnsigned   
)

Definition at line 366 of file BitsTest.cpp.

366  {
367  testConcatenation<false>();
368 }
TEST ( Bits  ,
ConcatenationAligned   
)

Definition at line 370 of file BitsTest.cpp.

370  {
371  testConcatenation<true>();
372 }
template<bool aligned>
void testConcatenation ( )

Definition at line 289 of file BitsTest.cpp.

References buffer(), and uint8_t.

289  {
290  // concatenate fields of length 1, 2, 3, ... 64, all unsigned, storing 2/3s
291  // the maximum value in each.
292 
293  // calculate how much buffer size we need
294  size_t bufSize = 0;
295  {
296  size_t w = 0;
297  // Unsigned
298  accSize<8>(w);
299  accSize<16>(w);
300  accSize<32>(w);
301  accSize<64>(w);
302 
303  // Signed NEG=false
304  accSize<7>(w);
305  accSize<15>(w);
306  accSize<31>(w);
307  accSize<63>(w);
308 
309  // Signed NEG=true
310  accSize<7>(w);
311  accSize<15>(w);
312  accSize<31>(w);
313  accSize<63>(w);
314 
315  bufSize = w;
316  }
317  // bits->bytes, rounding up
318  bufSize = (bufSize + 7) / 8;
319  // round up to next multiple of 8
320  bufSize = (bufSize + 7) / 8 * 8;
321  std::vector<uint8_t> buffer(bufSize);
322  uint8_t* buf = buffer.data();
323  {
324  size_t w = 0;
325  // Unsigned
326  testSetLoop<8, uint8_t, false, aligned>(w, bufSize, buf);
327  testSetLoop<16, uint16_t, false, aligned>(w, bufSize, buf);
328  testSetLoop<32, uint32_t, false, aligned>(w, bufSize, buf);
329  testSetLoop<64, uint64_t, false, aligned>(w, bufSize, buf);
330 
331  // Signed NEG=false
332  testSetLoop<7, int8_t, false, aligned>(w, bufSize, buf);
333  testSetLoop<15, int16_t, false, aligned>(w, bufSize, buf);
334  testSetLoop<31, int32_t, false, aligned>(w, bufSize, buf);
335  testSetLoop<63, int64_t, false, aligned>(w, bufSize, buf);
336 
337  // Signed NEG=true
338  testSetLoop<7, int8_t, true, aligned>(w, bufSize, buf);
339  testSetLoop<15, int16_t, true, aligned>(w, bufSize, buf);
340  testSetLoop<31, int32_t, true, aligned>(w, bufSize, buf);
341  testSetLoop<63, int64_t, true, aligned>(w, bufSize, buf);
342  }
343 
344  {
345  size_t r = 0;
346  // Unsigned
347  testGetLoop<8, uint8_t, false, aligned>(r, bufSize, buf);
348  testGetLoop<16, uint16_t, false, aligned>(r, bufSize, buf);
349  testGetLoop<32, uint32_t, false, aligned>(r, bufSize, buf);
350  testGetLoop<64, uint64_t, false, aligned>(r, bufSize, buf);
351 
352  // Signed NEG=false
353  testGetLoop<7, int8_t, false, aligned>(r, bufSize, buf);
354  testGetLoop<15, int16_t, false, aligned>(r, bufSize, buf);
355  testGetLoop<31, int32_t, false, aligned>(r, bufSize, buf);
356  testGetLoop<63, int64_t, false, aligned>(r, bufSize, buf);
357 
358  // Signed NEG=true
359  testGetLoop<7, int8_t, true, aligned>(r, bufSize, buf);
360  testGetLoop<15, int16_t, true, aligned>(r, bufSize, buf);
361  testGetLoop<31, int32_t, true, aligned>(r, bufSize, buf);
362  testGetLoop<63, int64_t, true, aligned>(r, bufSize, buf);
363  }
364 }
std::vector< uint8_t > buffer(kBufferSize+16)
template<size_t N, typename T , bool NEG, bool aligned>
void testGetLoop ( size_t &  r,
size_t  bufSize,
uint8_t buf 
)

Definition at line 280 of file BitsTest.cpp.

References EXPECT_EQ, and s.

280  {
281  for (size_t s = 0; s <= N; ++s) {
282  CHECK_LE(s + r, 8 * bufSize);
283  EXPECT_EQ((testValue<T, NEG>(s)), (testGet<aligned, T>(buf, r, s))) << s;
284  r += s;
285  }
286 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static set< string > s
template<size_t N, typename T , bool NEG, bool aligned>
void testSetLoop ( size_t &  w,
size_t  bufSize,
uint8_t buf 
)

Definition at line 270 of file BitsTest.cpp.

References EXPECT_EQ, and s.

270  {
271  for (size_t s = 0; s <= N; ++s) {
272  CHECK_LE(s + w, 8 * bufSize) << s << ' ' << w << ' ' << bufSize;
273  testSet<aligned>(buf, w, s, testValue<T, NEG>(s));
274  EXPECT_EQ((testValue<T, NEG>(s)), (testGet<aligned, T>(buf, w, s))) << s;
275  w += s;
276  }
277 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static set< string > s