From 3b14a3677d539e045db3e07fb094b2674f82a52a Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 28 Dec 2020 23:59:58 +0200 Subject: [PATCH] Add has_single_bit_test --- test/Jamfile.v2 | 1 + test/has_single_bit_test.cpp | 110 +++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 test/has_single_bit_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index a624eee..376cc52 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -227,6 +227,7 @@ run bit_rotate_test.cpp ; run bit_countr_test.cpp ; run bit_countl_test.cpp ; run bit_width_test.cpp ; +run has_single_bit_test.cpp ; use-project /boost/core/swap : ./swap ; build-project ./swap ; diff --git a/test/has_single_bit_test.cpp b/test/has_single_bit_test.cpp new file mode 100644 index 0000000..317d769 --- /dev/null +++ b/test/has_single_bit_test.cpp @@ -0,0 +1,110 @@ +// Test for boost/core/bit.hpp (has_single_bit) +// +// 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_single_bit( T x ) +{ + // BOOST_TEST_EQ( boost::core::has_single_bit( x ), boost::core::popcount( x ) == 1 ); +} + +int main() +{ + { + boost::uint8_t x = 0; + + BOOST_TEST_EQ( boost::core::has_single_bit( x ), false ); + + x = 1; + + BOOST_TEST_EQ( boost::core::has_single_bit( x ), true ); + + x = 2; + + for( int i = 1; i < 8; ++i, x <<= 1 ) + { + BOOST_TEST_EQ( boost::core::has_single_bit( x ), true ); + BOOST_TEST_EQ( boost::core::has_single_bit( static_cast( x | ( x >> 1 ) ) ), false ); + BOOST_TEST_EQ( boost::core::has_single_bit( static_cast( x | ( x >> 1 ) | ( x >> 2 ) ) ), false ); + } + } + + { + boost::uint16_t x = 0; + + BOOST_TEST_EQ( boost::core::has_single_bit( x ), false ); + + x = 1; + + BOOST_TEST_EQ( boost::core::has_single_bit( x ), true ); + + x = 2; + + for( int i = 1; i < 16; ++i, x <<= 1 ) + { + BOOST_TEST_EQ( boost::core::has_single_bit( x ), true ); + BOOST_TEST_EQ( boost::core::has_single_bit( static_cast( x | ( x >> 1 ) ) ), false ); + BOOST_TEST_EQ( boost::core::has_single_bit( static_cast( x | ( x >> 1 ) | ( x >> 2 ) ) ), false ); + } + } + + { + boost::uint32_t x = 0; + + BOOST_TEST_EQ( boost::core::has_single_bit( x ), false ); + + x = 1; + + BOOST_TEST_EQ( boost::core::has_single_bit( x ), true ); + + x = 2; + + for( int i = 1; i < 32; ++i, x <<= 1 ) + { + BOOST_TEST_EQ( boost::core::has_single_bit( x ), true ); + BOOST_TEST_EQ( boost::core::has_single_bit( static_cast( x | ( x >> 1 ) ) ), false ); + BOOST_TEST_EQ( boost::core::has_single_bit( static_cast( x | ( x >> 1 ) | ( x >> 2 ) ) ), false ); + } + } + + { + boost::uint64_t x = 0; + + BOOST_TEST_EQ( boost::core::has_single_bit( x ), false ); + + x = 1; + + BOOST_TEST_EQ( boost::core::has_single_bit( x ), true ); + + x = 2; + + for( int i = 1; i < 64; ++i, x <<= 1 ) + { + BOOST_TEST_EQ( boost::core::has_single_bit( x ), true ); + BOOST_TEST_EQ( boost::core::has_single_bit( static_cast( x | ( x >> 1 ) ) ), false ); + BOOST_TEST_EQ( boost::core::has_single_bit( static_cast( x | ( x >> 1 ) | ( x >> 2 ) ) ), false ); + } + } + + boost::detail::splitmix64 rng; + + for( int i = 0; i < 1000; ++i ) + { + boost::uint64_t x = rng(); + + test_single_bit( static_cast( x ) ); + test_single_bit( static_cast( x ) ); + test_single_bit( static_cast( x ) ); + test_single_bit( static_cast( x ) ); + test_single_bit( static_cast( x ) ); + } + + return boost::report_errors(); +}