| 1 | // Test for boost/core/bit.hpp (bit_ceil) |
|---|---|
| 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_bit_ceil( T x ) |
| 14 | { |
| 15 | if( !boost::core::has_single_bit( x ) ) |
| 16 | { |
| 17 | x = static_cast<T>( x >> 1 ); |
| 18 | } |
| 19 | |
| 20 | T y = boost::core::bit_ceil( x ); |
| 21 | |
| 22 | if( x == 0 ) |
| 23 | { |
| 24 | BOOST_TEST_EQ( y, 0 ); |
| 25 | } |
| 26 | else |
| 27 | { |
| 28 | BOOST_TEST( boost::core::has_single_bit( y ) ); |
| 29 | BOOST_TEST_GE( +y, +x ); |
| 30 | BOOST_TEST_LT( y >> 1, +x ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | int main() |
| 35 | { |
| 36 | { |
| 37 | test_bit_ceil( x: static_cast<unsigned char>( 0 ) ); |
| 38 | test_bit_ceil( x: static_cast<unsigned short>( 0 ) ); |
| 39 | test_bit_ceil( x: static_cast<unsigned int>( 0 ) ); |
| 40 | test_bit_ceil( x: static_cast<unsigned long>( 0 ) ); |
| 41 | test_bit_ceil( x: static_cast<boost::ulong_long_type>( 0 ) ); |
| 42 | } |
| 43 | |
| 44 | { |
| 45 | test_bit_ceil( x: static_cast<boost::uint8_t>( 0x80 ) ); |
| 46 | test_bit_ceil( x: static_cast<boost::uint16_t>( 0x8000 ) ); |
| 47 | test_bit_ceil( x: static_cast<boost::uint32_t>( 0x80000000 ) ); |
| 48 | test_bit_ceil( x: boost::uint64_t( 1 ) << 63 ); |
| 49 | } |
| 50 | |
| 51 | boost::detail::splitmix64 rng; |
| 52 | |
| 53 | for( int i = 0; i < 1000; ++i ) |
| 54 | { |
| 55 | boost::uint64_t x = rng(); |
| 56 | |
| 57 | test_bit_ceil( x: static_cast<unsigned char>( x ) ); |
| 58 | test_bit_ceil( x: static_cast<unsigned short>( x ) ); |
| 59 | test_bit_ceil( x: static_cast<unsigned int>( x ) ); |
| 60 | test_bit_ceil( x: static_cast<unsigned long>( x ) ); |
| 61 | test_bit_ceil( x: static_cast<boost::ulong_long_type>( x ) ); |
| 62 | } |
| 63 | |
| 64 | return boost::report_errors(); |
| 65 | } |
| 66 |
