From 26991f0c756bfde745bbd2156545065423515673 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 28 Dec 2020 23:52:39 +0200 Subject: [PATCH] Add bit_width_test --- test/Jamfile.v2 | 1 + test/bit_width_test.cpp | 94 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 test/bit_width_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 62942c4..a624eee 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -226,6 +226,7 @@ run bit_cast_test.cpp ; run bit_rotate_test.cpp ; run bit_countr_test.cpp ; run bit_countl_test.cpp ; +run bit_width_test.cpp ; use-project /boost/core/swap : ./swap ; build-project ./swap ; diff --git a/test/bit_width_test.cpp b/test/bit_width_test.cpp new file mode 100644 index 0000000..1447b90 --- /dev/null +++ b/test/bit_width_test.cpp @@ -0,0 +1,94 @@ +// Test for boost/core/bit.hpp (bit_width) +// +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +template void test_width( T x ) +{ + BOOST_TEST_EQ( boost::core::bit_width( x ), std::numeric_limits::digits - boost::core::countl_zero( x ) ); +} + +int main() +{ + { + boost::uint8_t x = 0; + + BOOST_TEST_EQ( boost::core::bit_width( x ), 0 ); + + x = 1; + + for( int i = 0; i < 8; ++i, x <<= 1 ) + { + BOOST_TEST_EQ( boost::core::bit_width( x ), i+1 ); + BOOST_TEST_EQ( boost::core::bit_width( static_cast( x | ( x >> 1 ) ) ), i+1 ); + BOOST_TEST_EQ( boost::core::bit_width( static_cast( x | ( x >> 2 ) ) ), i+1 ); + } + } + + { + boost::uint16_t x = 0; + + BOOST_TEST_EQ( boost::core::bit_width( x ), 0 ); + + x = 1; + + for( int i = 0; i < 16; ++i, x <<= 1 ) + { + BOOST_TEST_EQ( boost::core::bit_width( x ), i+1 ); + BOOST_TEST_EQ( boost::core::bit_width( static_cast( x | ( x >> 1 ) ) ), i+1 ); + BOOST_TEST_EQ( boost::core::bit_width( static_cast( x | ( x >> 2 ) ) ), i+1 ); + } + } + + { + boost::uint32_t x = 0; + + BOOST_TEST_EQ( boost::core::bit_width( x ), 0 ); + + x = 1; + + for( int i = 0; i < 32; ++i, x <<= 1 ) + { + BOOST_TEST_EQ( boost::core::bit_width( x ), i+1 ); + BOOST_TEST_EQ( boost::core::bit_width( static_cast( x | ( x >> 1 ) ) ), i+1 ); + BOOST_TEST_EQ( boost::core::bit_width( static_cast( x | ( x >> 2 ) ) ), i+1 ); + } + } + + { + boost::uint64_t x = 0; + + BOOST_TEST_EQ( boost::core::bit_width( x ), 0 ); + + x = 1; + + for( int i = 0; i < 64; ++i, x <<= 1 ) + { + BOOST_TEST_EQ( boost::core::bit_width( x ), i+1 ); + BOOST_TEST_EQ( boost::core::bit_width( static_cast( x | ( x >> 1 ) ) ), i+1 ); + BOOST_TEST_EQ( boost::core::bit_width( static_cast( x | ( x >> 2 ) ) ), i+1 ); + } + } + + boost::detail::splitmix64 rng; + + for( int i = 0; i < 1000; ++i ) + { + boost::uint64_t x = rng(); + + test_width( static_cast( x ) ); + test_width( static_cast( x ) ); + test_width( static_cast( x ) ); + test_width( static_cast( x ) ); + test_width( static_cast( x ) ); + } + + return boost::report_errors(); +}