Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[folly.git] / folly / experimental / test / BitsTest.cpp
1 /*
2  * Copyright 2012 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/experimental/Bits.h"
18
19 #include <glog/logging.h>
20 #include <gtest/gtest.h>
21
22 using namespace folly;
23
24 TEST(Bits, Simple) {
25   EXPECT_EQ(0, Bits<uint8_t>::blockCount(0));
26   EXPECT_EQ(1, Bits<uint8_t>::blockCount(1));
27   EXPECT_EQ(1, Bits<uint8_t>::blockCount(8));
28   EXPECT_EQ(2, Bits<uint8_t>::blockCount(9));
29   EXPECT_EQ(256, Bits<uint8_t>::blockCount(2048));
30   EXPECT_EQ(257, Bits<uint8_t>::blockCount(2049));
31
32   EXPECT_EQ(4, Bits<uint8_t>::blockIndex(39));
33   EXPECT_EQ(7, Bits<uint8_t>::bitOffset(39));
34   EXPECT_EQ(5, Bits<uint8_t>::blockIndex(40));
35   EXPECT_EQ(0, Bits<uint8_t>::bitOffset(40));
36
37   uint8_t buf[256];
38   memset(buf, 0, 256);
39
40   Bits<uint8_t>::set(buf, 36);
41   Bits<uint8_t>::set(buf, 39);
42   EXPECT_EQ((1 << 7) | (1 << 4), buf[4]);
43   EXPECT_EQ(0, buf[5]);
44   Bits<uint8_t>::clear(buf, 39);
45   EXPECT_EQ(1 << 4, buf[4]);
46   EXPECT_EQ(0, buf[5]);
47   Bits<uint8_t>::set(buf, 40);
48   EXPECT_EQ(1 << 4, buf[4]);
49   EXPECT_EQ(1, buf[5]);
50
51   EXPECT_EQ(2, Bits<uint8_t>::count(buf, buf + 256));
52 }
53
54 int main(int argc, char *argv[]) {
55   testing::InitGoogleTest(&argc, argv);
56   google::ParseCommandLineFlags(&argc, &argv, true);
57   return RUN_ALL_TESTS();
58 }
59