1// Test for boost/core/bit.hpp (countr_zero, countr_one)
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
13template<class T> void test_countr( T x )
14{
15 x |= 1;
16
17 for( int i = 0; i <= std::numeric_limits<T>::digits; ++i, x = static_cast<T>( x << 1 ) )
18 {
19 BOOST_TEST_EQ( boost::core::countr_zero( x ), i );
20 BOOST_TEST_EQ( boost::core::countr_one( static_cast<T>( ~x ) ), i );
21 }
22}
23
24int main()
25{
26 test_countr( x: static_cast<unsigned char>( 0 ) );
27 test_countr( x: static_cast<unsigned short>( 0 ) );
28 test_countr( x: static_cast<unsigned int>( 0 ) );
29 test_countr( x: static_cast<unsigned long>( 0 ) );
30 test_countr( x: static_cast<boost::ulong_long_type>( 0 ) );
31
32 boost::detail::splitmix64 rng;
33
34 for( int i = 0; i < 1000; ++i )
35 {
36 boost::uint64_t x = rng();
37
38 test_countr( x: static_cast<unsigned char>( x ) );
39 test_countr( x: static_cast<unsigned short>( x ) );
40 test_countr( x: static_cast<unsigned int>( x ) );
41 test_countr( x: static_cast<unsigned long>( x ) );
42 test_countr( x: static_cast<boost::ulong_long_type>( x ) );
43 }
44
45 return boost::report_errors();
46}
47

source code of boost/libs/core/test/bit_countr_test.cpp