]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/core/test/has_single_bit_test.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / core / test / has_single_bit_test.cpp
1 // Test for boost/core/bit.hpp (has_single_bit)
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_single_bit( T x )
14 {
15 BOOST_TEST_EQ( boost::core::has_single_bit( x ), boost::core::popcount( x ) == 1 );
16 }
17
18 int main()
19 {
20 {
21 boost::uint8_t x = 0;
22
23 BOOST_TEST_EQ( boost::core::has_single_bit( x ), false );
24
25 x = 1;
26
27 BOOST_TEST_EQ( boost::core::has_single_bit( x ), true );
28
29 x = 2;
30
31 for( int i = 1; i < 8; ++i, x <<= 1 )
32 {
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 );
36 }
37 }
38
39 {
40 boost::uint16_t x = 0;
41
42 BOOST_TEST_EQ( boost::core::has_single_bit( x ), false );
43
44 x = 1;
45
46 BOOST_TEST_EQ( boost::core::has_single_bit( x ), true );
47
48 x = 2;
49
50 for( int i = 1; i < 16; ++i, x <<= 1 )
51 {
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 );
55 }
56 }
57
58 {
59 boost::uint32_t x = 0;
60
61 BOOST_TEST_EQ( boost::core::has_single_bit( x ), false );
62
63 x = 1;
64
65 BOOST_TEST_EQ( boost::core::has_single_bit( x ), true );
66
67 x = 2;
68
69 for( int i = 1; i < 32; ++i, x <<= 1 )
70 {
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 );
74 }
75 }
76
77 {
78 boost::uint64_t x = 0;
79
80 BOOST_TEST_EQ( boost::core::has_single_bit( x ), false );
81
82 x = 1;
83
84 BOOST_TEST_EQ( boost::core::has_single_bit( x ), true );
85
86 x = 2;
87
88 for( int i = 1; i < 64; ++i, x <<= 1 )
89 {
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 );
93 }
94 }
95
96 boost::detail::splitmix64 rng;
97
98 for( int i = 0; i < 1000; ++i )
99 {
100 boost::uint64_t x = rng();
101
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 ) );
107 }
108
109 return boost::report_errors();
110 }