]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/core/test/bit_width_test.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / core / test / bit_width_test.cpp
1 // Test for boost/core/bit.hpp (bit_width)
2 //
3 // Copyright 2020 Peter Dimov
4 // Distributed under the Boost Software License, Version 1.0.
5 // https://www.boost.org/LICENSE_1_0.txt
6
7 #include <boost/core/bit.hpp>
8 #include <boost/core/lightweight_test.hpp>
9 #include <boost/core/detail/splitmix64.hpp>
10 #include <boost/cstdint.hpp>
11 #include <limits>
12
13 template<class T> void test_width( T x )
14 {
15 BOOST_TEST_EQ( boost::core::bit_width( x ), std::numeric_limits<T>::digits - boost::core::countl_zero( x ) );
16 }
17
18 int main()
19 {
20 {
21 boost::uint8_t x = 0;
22
23 BOOST_TEST_EQ( boost::core::bit_width( x ), 0 );
24
25 x = 1;
26
27 for( int i = 0; i < 8; ++i, x <<= 1 )
28 {
29 BOOST_TEST_EQ( boost::core::bit_width( x ), i+1 );
30 BOOST_TEST_EQ( boost::core::bit_width( static_cast<boost::uint8_t>( x | ( x >> 1 ) ) ), i+1 );
31 BOOST_TEST_EQ( boost::core::bit_width( static_cast<boost::uint8_t>( x | ( x >> 2 ) ) ), i+1 );
32 }
33 }
34
35 {
36 boost::uint16_t x = 0;
37
38 BOOST_TEST_EQ( boost::core::bit_width( x ), 0 );
39
40 x = 1;
41
42 for( int i = 0; i < 16; ++i, x <<= 1 )
43 {
44 BOOST_TEST_EQ( boost::core::bit_width( x ), i+1 );
45 BOOST_TEST_EQ( boost::core::bit_width( static_cast<boost::uint16_t>( x | ( x >> 1 ) ) ), i+1 );
46 BOOST_TEST_EQ( boost::core::bit_width( static_cast<boost::uint16_t>( x | ( x >> 2 ) ) ), i+1 );
47 }
48 }
49
50 {
51 boost::uint32_t x = 0;
52
53 BOOST_TEST_EQ( boost::core::bit_width( x ), 0 );
54
55 x = 1;
56
57 for( int i = 0; i < 32; ++i, x <<= 1 )
58 {
59 BOOST_TEST_EQ( boost::core::bit_width( x ), i+1 );
60 BOOST_TEST_EQ( boost::core::bit_width( static_cast<boost::uint32_t>( x | ( x >> 1 ) ) ), i+1 );
61 BOOST_TEST_EQ( boost::core::bit_width( static_cast<boost::uint32_t>( x | ( x >> 2 ) ) ), i+1 );
62 }
63 }
64
65 {
66 boost::uint64_t x = 0;
67
68 BOOST_TEST_EQ( boost::core::bit_width( x ), 0 );
69
70 x = 1;
71
72 for( int i = 0; i < 64; ++i, x <<= 1 )
73 {
74 BOOST_TEST_EQ( boost::core::bit_width( x ), i+1 );
75 BOOST_TEST_EQ( boost::core::bit_width( static_cast<boost::uint64_t>( x | ( x >> 1 ) ) ), i+1 );
76 BOOST_TEST_EQ( boost::core::bit_width( static_cast<boost::uint64_t>( x | ( x >> 2 ) ) ), i+1 );
77 }
78 }
79
80 boost::detail::splitmix64 rng;
81
82 for( int i = 0; i < 1000; ++i )
83 {
84 boost::uint64_t x = rng();
85
86 test_width( static_cast<unsigned char>( x ) );
87 test_width( static_cast<unsigned short>( x ) );
88 test_width( static_cast<unsigned int>( x ) );
89 test_width( static_cast<unsigned long>( x ) );
90 test_width( static_cast<boost::ulong_long_type>( x ) );
91 }
92
93 return boost::report_errors();
94 }