]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/core/test/bit_width_test.cpp
1 // Test for boost/core/bit.hpp (bit_width)
3 // Copyright 2020 Peter Dimov
4 // Distributed under the Boost Software License, Version 1.0.
5 // https://www.boost.org/LICENSE_1_0.txt
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>
13 template<class T
> void test_width( T x
)
15 BOOST_TEST_EQ( boost::core::bit_width( x
), std::numeric_limits
<T
>::digits
- boost::core::countl_zero( x
) );
23 BOOST_TEST_EQ( boost::core::bit_width( x
), 0 );
27 for( int i
= 0; i
< 8; ++i
, x
<<= 1 )
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 );
36 boost::uint16_t x
= 0;
38 BOOST_TEST_EQ( boost::core::bit_width( x
), 0 );
42 for( int i
= 0; i
< 16; ++i
, x
<<= 1 )
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 );
51 boost::uint32_t x
= 0;
53 BOOST_TEST_EQ( boost::core::bit_width( x
), 0 );
57 for( int i
= 0; i
< 32; ++i
, x
<<= 1 )
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 );
66 boost::uint64_t x
= 0;
68 BOOST_TEST_EQ( boost::core::bit_width( x
), 0 );
72 for( int i
= 0; i
< 64; ++i
, x
<<= 1 )
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 );
80 boost::detail::splitmix64 rng
;
82 for( int i
= 0; i
< 1000; ++i
)
84 boost::uint64_t x
= rng();
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
) );
93 return boost::report_errors();