]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/core/test/has_single_bit_test.cpp
1 // Test for boost/core/bit.hpp (has_single_bit)
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_single_bit( T x
)
15 BOOST_TEST_EQ( boost::core::has_single_bit( x
), boost::core::popcount( x
) == 1 );
23 BOOST_TEST_EQ( boost::core::has_single_bit( x
), false );
27 BOOST_TEST_EQ( boost::core::has_single_bit( x
), true );
31 for( int i
= 1; i
< 8; ++i
, x
<<= 1 )
33 BOOST_TEST_EQ( boost::core::has_single_bit( x
), true );
34 BOOST_TEST_EQ( boost::core::has_single_bit( static_cast<boost::uint8_t>( x
| ( x
>> 1 ) ) ), false );
35 BOOST_TEST_EQ( boost::core::has_single_bit( static_cast<boost::uint8_t>( x
| ( x
>> 1 ) | ( x
>> 2 ) ) ), false );
40 boost::uint16_t x
= 0;
42 BOOST_TEST_EQ( boost::core::has_single_bit( x
), false );
46 BOOST_TEST_EQ( boost::core::has_single_bit( x
), true );
50 for( int i
= 1; i
< 16; ++i
, x
<<= 1 )
52 BOOST_TEST_EQ( boost::core::has_single_bit( x
), true );
53 BOOST_TEST_EQ( boost::core::has_single_bit( static_cast<boost::uint16_t>( x
| ( x
>> 1 ) ) ), false );
54 BOOST_TEST_EQ( boost::core::has_single_bit( static_cast<boost::uint16_t>( x
| ( x
>> 1 ) | ( x
>> 2 ) ) ), false );
59 boost::uint32_t x
= 0;
61 BOOST_TEST_EQ( boost::core::has_single_bit( x
), false );
65 BOOST_TEST_EQ( boost::core::has_single_bit( x
), true );
69 for( int i
= 1; i
< 32; ++i
, x
<<= 1 )
71 BOOST_TEST_EQ( boost::core::has_single_bit( x
), true );
72 BOOST_TEST_EQ( boost::core::has_single_bit( static_cast<boost::uint32_t>( x
| ( x
>> 1 ) ) ), false );
73 BOOST_TEST_EQ( boost::core::has_single_bit( static_cast<boost::uint32_t>( x
| ( x
>> 1 ) | ( x
>> 2 ) ) ), false );
78 boost::uint64_t x
= 0;
80 BOOST_TEST_EQ( boost::core::has_single_bit( x
), false );
84 BOOST_TEST_EQ( boost::core::has_single_bit( x
), true );
88 for( int i
= 1; i
< 64; ++i
, x
<<= 1 )
90 BOOST_TEST_EQ( boost::core::has_single_bit( x
), true );
91 BOOST_TEST_EQ( boost::core::has_single_bit( static_cast<boost::uint64_t>( x
| ( x
>> 1 ) ) ), false );
92 BOOST_TEST_EQ( boost::core::has_single_bit( static_cast<boost::uint64_t>( x
| ( x
>> 1 ) | ( x
>> 2 ) ) ), false );
96 boost::detail::splitmix64 rng
;
98 for( int i
= 0; i
< 1000; ++i
)
100 boost::uint64_t x
= rng();
102 test_single_bit( static_cast<unsigned char>( x
) );
103 test_single_bit( static_cast<unsigned short>( x
) );
104 test_single_bit( static_cast<unsigned int>( x
) );
105 test_single_bit( static_cast<unsigned long>( x
) );
106 test_single_bit( static_cast<boost::ulong_long_type
>( x
) );
109 return boost::report_errors();