Files
boost_integer/test/integer_mask_test.cpp

73 lines
2.7 KiB
C++
Raw Normal View History

2001-12-07 13:19:38 +00:00
// boost integer_mask.hpp test program -------------------------------------//
2004-07-30 04:46:56 +00:00
// (C) Copyright Daryle Walker 2001.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
2001-12-07 13:19:38 +00:00
// See http://www.boost.org for most recent version including documentation.
// Revision History
// 27 Jul 2008 Changed tests to use the unit-test system (Daryle Walker)
2001-12-07 13:19:38 +00:00
// 23 Sep 2001 Initial version (Daryle Walker)
#define BOOST_TEST_MODULE "Integer mask tests"
#include <boost/test/unit_test.hpp> // unit testing framework
2001-12-07 13:19:38 +00:00
#include <boost/cstdint.hpp> // for boost::uintmax_t
2001-12-07 13:19:38 +00:00
#include <boost/integer/integer_mask.hpp> // for boost::high_bit_mask_t, etc.
#include <boost/mpl/assert.hpp> // for BOOST_MPL_ASSERT_RELATION
#include <boost/mpl/range_c.hpp> // for boost::mpl::range_c
2001-12-07 13:19:38 +00:00
#include <cstddef> // for std::size_t
2001-12-07 13:19:38 +00:00
// Custom types/templates, helper functions, and objects
namespace
{
// A big one
boost::uintmax_t const one = 1u;
// List the ranges of template parameters tests (ranges are half-open)
typedef boost::mpl::range_c<std::size_t, 0u, 32u> high_bit_offsets;
typedef boost::mpl::range_c<std::size_t, 1u, 33u> low_bit_lengths;
} // unnamed namespace
2001-12-07 13:19:38 +00:00
// Check the various integer-valued bit-masks
BOOST_AUTO_TEST_SUITE( integer_mask_tests )
2001-12-07 13:19:38 +00:00
// Check the bit-masks of one offset bit
BOOST_AUTO_TEST_CASE_TEMPLATE( high_bit_mask_test, T, high_bit_offsets )
{
BOOST_MPL_ASSERT_RELATION( boost::high_bit_mask_t<T::value>::high_bit, ==,
(one << T::value) );
BOOST_MPL_ASSERT_RELATION( boost::high_bit_mask_t<T::value>::high_bit_fast,
==, (one << T::value) );
}
2001-12-07 13:19:38 +00:00
// Check the bit-masks of a block of low-valued bits
BOOST_AUTO_TEST_CASE_TEMPLATE( low_bits_mask_test, T, low_bit_lengths )
2001-12-07 13:19:38 +00:00
{
// One can express (2^x - 1) in two ways
// 1. (1 << x) - 1
// 2. (1 << (x-1)) | ((1 << (x-1)) - 1)
// Since unsigneds have modulo arithmetic, [1] gives the right answer even
// when x is the number of bits in the register. However, that last case
// gives warnings about the sole bit flowing past the register. Applying
// distributive property backwards gives [2], which works without overflow.
BOOST_MPL_ASSERT_RELATION( boost::low_bits_mask_t<T::value>::sig_bits, ==,
(one << ( T::value - 1u )) | (( one << (T::value - 1u) ) - 1u) );
BOOST_MPL_ASSERT_RELATION( boost::low_bits_mask_t<T::value>::sig_bits_fast,
==, (one << ( T::value - 1u )) | (( one << (T::value - 1u) ) - 1u) );
2001-12-07 13:19:38 +00:00
}
BOOST_AUTO_TEST_SUITE_END()
// Verification of bugs and their fixes
BOOST_AUTO_TEST_SUITE( bug_fix_tests )
BOOST_AUTO_TEST_SUITE_END()