2 * Copyright 2012 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "folly/experimental/Bits.h"
19 #include <glog/logging.h>
20 #include <gtest/gtest.h>
22 using namespace folly;
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));
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));
40 Bits<uint8_t>::set(buf, 36);
41 Bits<uint8_t>::set(buf, 39);
42 EXPECT_EQ((1 << 7) | (1 << 4), buf[4]);
44 Bits<uint8_t>::clear(buf, 39);
45 EXPECT_EQ(1 << 4, buf[4]);
47 Bits<uint8_t>::set(buf, 40);
48 EXPECT_EQ(1 << 4, buf[4]);
51 EXPECT_EQ(2, Bits<uint8_t>::count(buf, buf + 256));
54 int main(int argc, char *argv[]) {
55 testing::InitGoogleTest(&argc, argv);
56 google::ParseCommandLineFlags(&argc, &argv, true);
57 return RUN_ALL_TESTS();