Repository navigation
Expand file tree
/
Copy pathbitmanip_tests.cpp
More file actions
60 lines (48 loc) · 1.6 KB
/
Copy pathbitmanip_tests.cpp
File metadata and controls
60 lines (48 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2019 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/setup_common.h>
#include <util/bitmanip.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(bitmanip_tests, BasicTestingSetup)
static void CheckBitCount(uint32_t value, uint32_t expected_count) {
// Count are rotation invariant.
for (int i = 0; i < 32; i++) {
BOOST_CHECK_EQUAL(countBits(value), expected_count);
value = (value << 1) | (value >> 31);
}
}
static uint32_t countBitsNaive(uint32_t value) {
uint32_t ret = 0;
while (value != 0) {
ret += (value & 0x01);
value >>= 1;
}
return ret;
}
#define COUNT 4096
BOOST_AUTO_TEST_CASE(bit_count) {
// Check various known values.
CheckBitCount(0, 0);
CheckBitCount(1, 1);
CheckBitCount(0xffffffff, 32);
CheckBitCount(0x01234567, 12);
CheckBitCount(0x12345678, 13);
CheckBitCount(0xfedcba98, 20);
CheckBitCount(0x5a55aaa5, 16);
CheckBitCount(0xdeadbeef, 24);
for (uint32_t i = 2; i != 0; i <<= 1) {
// Check two bit set for all combinations.
CheckBitCount(i | 0x01, 2);
}
// Check many small values against a naive implementation.
for (uint32_t v = 0; v <= 0xfff; v++) {
CheckBitCount(v, countBitsNaive(v));
}
// Check random values against a naive implementation.
for (int i = 0; i < COUNT; i++) {
uint32_t v = InsecureRand32();
CheckBitCount(v, countBitsNaive(v));
}
}
BOOST_AUTO_TEST_SUITE_END()