Added support for exact width integer type to int_t and uint_t

Updated docs to match.
Fixes #1225.

[SVN r57941]
This commit is contained in:
John Maddock
2009-11-26 11:08:27 +00:00
parent ca84baa55d
commit ac155d3843
9 changed files with 338 additions and 338 deletions

View File

@ -26,7 +26,7 @@
<a name="boost_integer.history"></a><a class="link" href="history.html" title="History"> History</a> <a name="boost_integer.history"></a><a class="link" href="history.html" title="History"> History</a>
</h2></div></div></div> </h2></div></div></div>
<a name="boost_integer.history.1_42_0"></a><h5> <a name="boost_integer.history.1_42_0"></a><h5>
<a name="id677364"></a> <a name="id785558"></a>
<a class="link" href="history.html#boost_integer.history.1_42_0">1.42.0</a> <a class="link" href="history.html#boost_integer.history.1_42_0">1.42.0</a>
</h5> </h5>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul type="disc">
@ -55,7 +55,7 @@
</li> </li>
</ul></div> </ul></div>
<a name="boost_integer.history.1_32_0"></a><h5> <a name="boost_integer.history.1_32_0"></a><h5>
<a name="id677466"></a> <a name="id785660"></a>
<a class="link" href="history.html#boost_integer.history.1_32_0">1.32.0</a> <a class="link" href="history.html#boost_integer.history.1_32_0">1.32.0</a>
</h5> </h5>
<div class="itemizedlist"><ul type="disc"><li> <div class="itemizedlist"><ul type="disc"><li>

View File

@ -61,6 +61,8 @@
</span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">int</span> <span class="identifier">Bits</span><span class="special">&gt;</span> </span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">int</span> <span class="identifier">Bits</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">int_t</span> <span class="keyword">struct</span> <span class="identifier">int_t</span>
<span class="special">{</span> <span class="special">{</span>
<span class="comment">/* Member exact may or may not be defined depending upon Bits */</span>
<span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">exact</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span>
<span class="special">};</span> <span class="special">};</span>
@ -69,6 +71,8 @@
</span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">int</span> <span class="identifier">Bits</span><span class="special">&gt;</span> </span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">int</span> <span class="identifier">Bits</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">uint_t</span> <span class="keyword">struct</span> <span class="identifier">uint_t</span>
<span class="special">{</span> <span class="special">{</span>
<span class="comment">/* Member exact may or may not be defined depending upon Bits */</span>
<span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">exact</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span>
<span class="special">};</span> <span class="special">};</span>
@ -136,7 +140,7 @@
<code class="literal">fast</code>. The following table describes each template's criteria. <code class="literal">fast</code>. The following table describes each template's criteria.
</p> </p>
<div class="table"> <div class="table">
<a name="id673121"></a><p class="title"><b>Table<EFBFBD>1.<2E>Criteria for the Sized Type Class Templates</b></p> <a name="id781298"></a><p class="title"><b>Table<EFBFBD>1.<2E>Criteria for the Sized Type Class Templates</b></p>
<div class="table-contents"><table class="table" summary="Criteria for the Sized Type Class Templates"> <div class="table-contents"><table class="table" summary="Criteria for the Sized Type Class Templates">
<colgroup> <colgroup>
<col> <col>
@ -166,7 +170,9 @@
The smallest built-in signed integral type with at least <span class="emphasis"><em>N</em></span> The smallest built-in signed integral type with at least <span class="emphasis"><em>N</em></span>
bits, including the sign bit. The parameter should be a positive number. bits, including the sign bit. The parameter should be a positive number.
A compile-time error results if the parameter is larger than the number A compile-time error results if the parameter is larger than the number
of bits in the largest integer type. of bits in the largest integer type. Note that the member <span class="emphasis"><em>exact</em></span>
is defined <span class="bold"><strong>only</strong></span> if there is a type
with exactly N bits.
</p> </p>
</td> </td>
</tr> </tr>
@ -181,7 +187,9 @@
The smallest built-in unsigned integral type with at least <span class="emphasis"><em>N</em></span> The smallest built-in unsigned integral type with at least <span class="emphasis"><em>N</em></span>
bits. The parameter should be a positive number. A compile-time error bits. The parameter should be a positive number. A compile-time error
results if the parameter is larger than the number of bits in the largest results if the parameter is larger than the number of bits in the largest
integer type. integer type. Note that the member <span class="emphasis"><em>exact</em></span> is defined
<span class="bold"><strong>only</strong></span> if there is a type with exactly
N bits.
</p> </p>
</td> </td>
</tr> </tr>

View File

@ -105,7 +105,7 @@
of <code class="literal">high_bit_mask_t</code>. of <code class="literal">high_bit_mask_t</code>.
</p> </p>
<div class="table"> <div class="table">
<a name="id674116"></a><p class="title"><b>Table<EFBFBD>2.<2E>Members of the `boost::high_bit_mask_t` Class Template</b></p> <a name="id782311"></a><p class="title"><b>Table<EFBFBD>2.<2E>Members of the `boost::high_bit_mask_t` Class Template</b></p>
<div class="table-contents"><table class="table" summary="Members of the `boost::high_bit_mask_t` Class Template"> <div class="table-contents"><table class="table" summary="Members of the `boost::high_bit_mask_t` Class Template">
<colgroup> <colgroup>
<col> <col>
@ -205,7 +205,7 @@
the members of an instantiation of <code class="literal">low_bits_mask_t</code>. the members of an instantiation of <code class="literal">low_bits_mask_t</code>.
</p> </p>
<div class="table"> <div class="table">
<a name="id674318"></a><p class="title"><b>Table<EFBFBD>3.<2E>Members of the [^boost::low_bits_mask_t] Class Template</b></p> <a name="id782512"></a><p class="title"><b>Table<EFBFBD>3.<2E>Members of the [^boost::low_bits_mask_t] Class Template</b></p>
<div class="table-contents"><table class="table" summary="Members of the [^boost::low_bits_mask_t] Class Template"> <div class="table-contents"><table class="table" summary="Members of the [^boost::low_bits_mask_t] Class Template">
<colgroup> <colgroup>
<col> <col>

View File

@ -40,7 +40,7 @@
<div><p class="copyright">Copyright <20> 2001 -2009 Beman Dawes, Daryle Walker, Gennaro Prota, <div><p class="copyright">Copyright <20> 2001 -2009 Beman Dawes, Daryle Walker, Gennaro Prota,
John Maddock</p></div> John Maddock</p></div>
<div><div class="legalnotice"> <div><div class="legalnotice">
<a name="id662860"></a><p> <a name="id770995"></a><p>
Distributed under the Boost Software License, Version 1.0. (See accompanying Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p> </p>
@ -229,7 +229,7 @@
</div> </div>
</div> </div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: November 25, 2009 at 12:02:32 GMT</small></p></td> <td align="left"><p><small>Last revised: November 26, 2009 at 11:06:43 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td> <td align="right"><div class="copyright-footer"></div></td>
</tr></table> </tr></table>
<hr> <hr>

View File

@ -282,6 +282,8 @@ This facility is particularly useful for solving generic programming problems.
template<int Bits> template<int Bits>
struct int_t struct int_t
{ {
/* Member exact may or may not be defined depending upon Bits */
typedef ``['implementation-defined-type]`` exact;
typedef ``['implementation-defined-type]`` least; typedef ``['implementation-defined-type]`` least;
typedef int_fast_t<least>::fast fast; typedef int_fast_t<least>::fast fast;
}; };
@ -290,6 +292,8 @@ This facility is particularly useful for solving generic programming problems.
template<int Bits> template<int Bits>
struct uint_t struct uint_t
{ {
/* Member exact may or may not be defined depending upon Bits */
typedef ``['implementation-defined-type]`` exact;
typedef ``['implementation-defined-type]`` least; typedef ``['implementation-defined-type]`` least;
typedef int_fast_t<least>::fast fast; typedef int_fast_t<least>::fast fast;
}; };
@ -351,13 +355,16 @@ The following table describes each template's criteria.
[[^boost::int_t<N>]] [[^boost::int_t<N>]]
[The smallest built-in signed integral type with at least /N/ bits, including the sign bit. [The smallest built-in signed integral type with at least /N/ bits, including the sign bit.
The parameter should be a positive number. A compile-time error results if the parameter is The parameter should be a positive number. A compile-time error results if the parameter is
larger than the number of bits in the largest integer type.] larger than the number of bits in the largest integer type. Note that the member /exact/ is defined
[*only] if there is a type with exactly N bits.]
] ]
[ [
[[^boost::uint_t<N>]] [[^boost::uint_t<N>]]
[The smallest built-in unsigned integral type with at least /N/ bits. [The smallest built-in unsigned integral type with at least /N/ bits.
The parameter should be a positive number. A compile-time error results if the The parameter should be a positive number. A compile-time error results if the
parameter is larger than the number of bits in the largest integer type.] parameter is larger than the number of bits in the largest integer type.
Note that the member /exact/ is defined
[*only] if there is a type with exactly N bits.]
] ]
[ [
[[^boost::int_max_value_t<V>]] [[^boost::int_max_value_t<V>]]

View File

@ -21,6 +21,18 @@
#include <boost/limits.hpp> // for ::std::numeric_limits #include <boost/limits.hpp> // for ::std::numeric_limits
#include <boost/cstdint.hpp> // for boost::int64_t and BOOST_NO_INTEGRAL_INT64_T #include <boost/cstdint.hpp> // for boost::int64_t and BOOST_NO_INTEGRAL_INT64_T
//
// We simply cannot include this header on gcc without getting copious warnings of the kind:
//
// boost/integer.hpp:77:30: warning: use of C99 long long integer constant
//
// And yet there is no other reasonable implementation, so we declare this a system header
// to suppress these warnings.
//
#if defined(__GNUC__) && (__GNUC__ >= 4)
#pragma GCC system_header
#endif
namespace boost namespace boost
{ {
@ -54,13 +66,42 @@ namespace boost
template<> struct int_least_helper<9> { typedef unsigned short least; }; template<> struct int_least_helper<9> { typedef unsigned short least; };
template<> struct int_least_helper<10> { typedef unsigned char least; }; template<> struct int_least_helper<10> { typedef unsigned char least; };
template <int Bits>
struct exact_signed_base_helper{};
template <int Bits>
struct exact_unsigned_base_helper{};
template <> struct exact_signed_base_helper<sizeof(signed char)* CHAR_BIT> { typedef signed char exact; };
template <> struct exact_unsigned_base_helper<sizeof(unsigned char)* CHAR_BIT> { typedef unsigned char exact; };
#if USHRT_MAX != UCHAR_MAX
template <> struct exact_signed_base_helper<sizeof(short)* CHAR_BIT> { typedef short exact; };
template <> struct exact_unsigned_base_helper<sizeof(unsigned short)* CHAR_BIT> { typedef unsigned short exact; };
#endif
#if UINT_MAX != USHRT_MAX
template <> struct exact_signed_base_helper<sizeof(int)* CHAR_BIT> { typedef int exact; };
template <> struct exact_unsigned_base_helper<sizeof(unsigned int)* CHAR_BIT> { typedef unsigned int exact; };
#endif
#if ULONG_MAX != UINT_MAX
template <> struct exact_signed_base_helper<sizeof(long)* CHAR_BIT> { typedef long exact; };
template <> struct exact_unsigned_base_helper<sizeof(unsigned long)* CHAR_BIT> { typedef unsigned long exact; };
#endif
#if defined(BOOST_HAS_LONG_LONG) &&\
((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\
(defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\
(defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\
(defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX)))
template <> struct exact_signed_base_helper<sizeof(long long)* CHAR_BIT> { typedef long long exact; };
template <> struct exact_unsigned_base_helper<sizeof(unsigned long long)* CHAR_BIT> { typedef unsigned long long exact; };
#endif
} // namespace detail } // namespace detail
// integer templates specifying number of bits ---------------------------// // integer templates specifying number of bits ---------------------------//
// signed // signed
template< int Bits > // bits (including sign) required template< int Bits > // bits (including sign) required
struct int_t struct int_t : public detail::exact_signed_base_helper<Bits>
{ {
typedef typename detail::int_least_helper typedef typename detail::int_least_helper
< <
@ -79,7 +120,7 @@ namespace boost
// unsigned // unsigned
template< int Bits > // bits required template< int Bits > // bits required
struct uint_t struct uint_t : public detail::exact_unsigned_base_helper<Bits>
{ {
typedef typename detail::int_least_helper typedef typename detail::int_least_helper
< <

View File

@ -5,11 +5,11 @@
import testing ; import testing ;
test-suite integer test-suite integer
: [ run cstdint_test.cpp ] : [ run cstdint_test.cpp /boost/test//minimal ]
[ run integer_test.cpp
/boost/test//minimal ]
[ run integer_traits_test.cpp [ run integer_traits_test.cpp
/boost/test//boost_test_exec_monitor ] /boost/test//boost_test_exec_monitor ]
[ run integer_test.cpp
/boost/test//minimal ]
[ run integer_mask_test.cpp [ run integer_mask_test.cpp
/boost/test//minimal ] /boost/test//minimal ]
[ run static_log2_test.cpp [ run static_log2_test.cpp

View File

@ -13,17 +13,10 @@
// 23 Sep 00 Added INTXX_C constant macro support + int64_t support (John Maddock). // 23 Sep 00 Added INTXX_C constant macro support + int64_t support (John Maddock).
// 28 Jun 00 Initial version // 28 Jun 00 Initial version
#define __STDC_CONSTANT_MACROS #define __STDC_CONSTANT_MACROS
#include <cassert>
#include <iostream> #include <iostream>
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
#include <boost/test/minimal.hpp>
#ifdef NDEBUG
int main()
{
std::cout << "This test makes no sense with NDEBUG defined.\n";
return 0;
}
#else
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
// //
@ -62,24 +55,24 @@ struct integral_constant_checker
void integral_constant_checker::check() void integral_constant_checker::check()
{ {
assert( int8 == -127 ); BOOST_CHECK( int8 == -127 );
assert( int_least8 == -127 ); BOOST_CHECK( int_least8 == -127 );
assert( int_fast8 == -127 ); BOOST_CHECK( int_fast8 == -127 );
assert( uint8 == 255u ); BOOST_CHECK( uint8 == 255u );
assert( uint_least8 == 255u ); BOOST_CHECK( uint_least8 == 255u );
assert( uint_fast8 == 255u ); BOOST_CHECK( uint_fast8 == 255u );
assert( int16 == -32767 ); BOOST_CHECK( int16 == -32767 );
assert( int_least16 == -32767 ); BOOST_CHECK( int_least16 == -32767 );
assert( int_fast16 == -32767 ); BOOST_CHECK( int_fast16 == -32767 );
assert( uint16 == 65535u ); BOOST_CHECK( uint16 == 65535u );
assert( uint_least16 == 65535u ); BOOST_CHECK( uint_least16 == 65535u );
assert( uint_fast16 == 65535u ); BOOST_CHECK( uint_fast16 == 65535u );
assert( int32 == -2147483647 ); BOOST_CHECK( int32 == -2147483647 );
assert( int_least32 == -2147483647 ); BOOST_CHECK( int_least32 == -2147483647 );
assert( int_fast32 == -2147483647 ); BOOST_CHECK( int_fast32 == -2147483647 );
assert( uint32 == 4294967295u ); BOOST_CHECK( uint32 == 4294967295u );
assert( uint_least32 == 4294967295u ); BOOST_CHECK( uint_least32 == 4294967295u );
assert( uint_fast32 == 4294967295u ); BOOST_CHECK( uint_fast32 == 4294967295u );
} }
#endif // BOOST_NO_INCLASS_MEMBER_INITIALIZATION #endif // BOOST_NO_INCLASS_MEMBER_INITIALIZATION
@ -108,10 +101,10 @@ void integral_constant_type_check(T1, T2)
// if we have a native stdint.h // if we have a native stdint.h
// then the INTXX_C macros may define // then the INTXX_C macros may define
// a type that's wider than required: // a type that's wider than required:
assert(sizeof(T1) <= sizeof(T2)); BOOST_CHECK(sizeof(T1) <= sizeof(T2));
#else #else
assert(sizeof(T1) == sizeof(T2)); BOOST_CHECK(sizeof(T1) == sizeof(T2));
assert(t1 == t2); BOOST_CHECK(t1 == t2);
#endif #endif
#if defined(BOOST_HAS_STDINT_H) #if defined(BOOST_HAS_STDINT_H)
// native headers are permitted to promote small // native headers are permitted to promote small
@ -119,22 +112,22 @@ void integral_constant_type_check(T1, T2)
if(sizeof(T1) >= sizeof(int)) if(sizeof(T1) >= sizeof(int))
{ {
if(t1 > 0) if(t1 > 0)
assert(t2 > 0); BOOST_CHECK(t2 > 0);
else else
assert(!(t2 > 0)); BOOST_CHECK(!(t2 > 0));
} }
else if(t1 < 0) else if(t1 < 0)
assert(!(t2 > 0)); BOOST_CHECK(!(t2 > 0));
#else #else
if(t1 > 0) if(t1 > 0)
assert(t2 > 0); BOOST_CHECK(t2 > 0);
else else
assert(!(t2 > 0)); BOOST_CHECK(!(t2 > 0));
#endif #endif
} }
int main() int test_main(int, char*[])
{ {
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
integral_constant_checker::check(); integral_constant_checker::check();
@ -193,41 +186,40 @@ int main()
boost::uintmax_t uintmax = UINTMAX_C(4294967295); boost::uintmax_t uintmax = UINTMAX_C(4294967295);
#endif #endif
assert( int8 == -127 ); BOOST_CHECK( int8 == -127 );
assert( int_least8 == -127 ); BOOST_CHECK( int_least8 == -127 );
assert( int_fast8 == -127 ); BOOST_CHECK( int_fast8 == -127 );
assert( uint8 == 255u ); BOOST_CHECK( uint8 == 255u );
assert( uint_least8 == 255u ); BOOST_CHECK( uint_least8 == 255u );
assert( uint_fast8 == 255u ); BOOST_CHECK( uint_fast8 == 255u );
assert( int16 == -32767 ); BOOST_CHECK( int16 == -32767 );
assert( int_least16 == -32767 ); BOOST_CHECK( int_least16 == -32767 );
assert( int_fast16 == -32767 ); BOOST_CHECK( int_fast16 == -32767 );
assert( uint16 == 65535u ); BOOST_CHECK( uint16 == 65535u );
assert( uint_least16 == 65535u ); BOOST_CHECK( uint_least16 == 65535u );
assert( uint_fast16 == 65535u ); BOOST_CHECK( uint_fast16 == 65535u );
assert( int32 == -2147483647 ); BOOST_CHECK( int32 == -2147483647 );
assert( int_least32 == -2147483647 ); BOOST_CHECK( int_least32 == -2147483647 );
assert( int_fast32 == -2147483647 ); BOOST_CHECK( int_fast32 == -2147483647 );
assert( uint32 == 4294967295u ); BOOST_CHECK( uint32 == 4294967295u );
assert( uint_least32 == 4294967295u ); BOOST_CHECK( uint_least32 == 4294967295u );
assert( uint_fast32 == 4294967295u ); BOOST_CHECK( uint_fast32 == 4294967295u );
#ifndef BOOST_NO_INT64_T #ifndef BOOST_NO_INT64_T
assert( int64 == INT64_C(-9223372036854775807) ); BOOST_CHECK( int64 == INT64_C(-9223372036854775807) );
assert( int_least64 == INT64_C(-9223372036854775807) ); BOOST_CHECK( int_least64 == INT64_C(-9223372036854775807) );
assert( int_fast64 == INT64_C(-9223372036854775807) ); BOOST_CHECK( int_fast64 == INT64_C(-9223372036854775807) );
assert( uint64 == UINT64_C(18446744073709551615) ); BOOST_CHECK( uint64 == UINT64_C(18446744073709551615) );
assert( uint_least64 == UINT64_C(18446744073709551615) ); BOOST_CHECK( uint_least64 == UINT64_C(18446744073709551615) );
assert( uint_fast64 == UINT64_C(18446744073709551615) ); BOOST_CHECK( uint_fast64 == UINT64_C(18446744073709551615) );
assert( intmax == INT64_C(-9223372036854775807) ); BOOST_CHECK( intmax == INT64_C(-9223372036854775807) );
assert( uintmax == UINT64_C(18446744073709551615) ); BOOST_CHECK( uintmax == UINT64_C(18446744073709551615) );
#else #else
assert( intmax == -2147483647 ); BOOST_CHECK( intmax == -2147483647 );
assert( uintmax == 4294967295u ); BOOST_CHECK( uintmax == 4294967295u );
#endif #endif
std::cout << "OK\n"; std::cout << "OK\n";
return 0; return 0;
} }
#endif

View File

@ -1,6 +1,9 @@
// boost integer.hpp test program ------------------------------------------// // boost integer.hpp test program ------------------------------------------//
// Copyright Beman Dawes 1999. Distributed under the Boost // Copyright Beman Dawes 1999.
// Copyright Daryle Walker 2001.
// Copyright John Maddock 2009.
// Distributed under the Boost
// Software License, Version 1.0. (See accompanying file // Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@ -13,10 +16,8 @@
// 31 Aug 99 Initial version // 31 Aug 99 Initial version
#include <boost/test/minimal.hpp> // for main, BOOST_CHECK #include <boost/test/minimal.hpp> // for main, BOOST_CHECK
#include <boost/config.hpp> // for BOOST_NO_USING_TEMPLATE
#include <boost/cstdlib.hpp> // for boost::exit_success
#include <boost/integer.hpp> // for boost::int_t, boost::uint_t #include <boost/integer.hpp> // for boost::int_t, boost::uint_t
#include <boost/type_traits/is_same.hpp>
#include <climits> // for ULONG_MAX, LONG_MAX, LONG_MIN #include <climits> // for ULONG_MAX, LONG_MAX, LONG_MIN
#include <iostream> // for std::cout (std::endl indirectly) #include <iostream> // for std::cout (std::endl indirectly)
@ -25,271 +26,222 @@
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
#pragma warning(disable:4127) // conditional expression is constant #pragma warning(disable:4127) // conditional expression is constant
#endif #endif
//
// Keep track of error count, so we can print out detailed
// Control if the names of the types for each version // info only if we need it:
// of the integer templates will be printed. //
#ifndef CONTROL_SHOW_TYPES unsigned last_error_count = 0;
#define CONTROL_SHOW_TYPES 0 //
// Helpers to print out the name of a type,
// we use these as typeid(X).name() doesn't always
// return a human readable string:
//
template <class T> const char* get_name_of_type(T){ return typeid(T).name(); }
const char* get_name_of_type(signed char){ return "signed char"; }
const char* get_name_of_type(unsigned char){ return "unsigned char"; }
const char* get_name_of_type(short){ return "short"; }
const char* get_name_of_type(unsigned short){ return "unsigned short"; }
const char* get_name_of_type(int){ return "int"; }
const char* get_name_of_type(unsigned int){ return "unsigned int"; }
const char* get_name_of_type(long){ return "long"; }
const char* get_name_of_type(unsigned long){ return "unsigned long"; }
#ifdef BOOST_HAS_LONG_LONG
const char* get_name_of_type(long long){ return "long long"; }
const char* get_name_of_type(unsigned long long){ return "unsigned long long"; }
#endif #endif
template <int Bits>
// If specializations have not already been done, then we can confirm void do_test_exact(boost::mpl::true_ const&)
// the effects of the "fast" types by making a specialization.
namespace boost
{ {
template < > // Test the ::exact member:
struct int_fast_t< short > typedef typename boost::int_t<Bits>::exact int_exact;
{ typedef typename boost::uint_t<Bits>::exact uint_exact;
typedef long fast; typedef typename boost::int_t<Bits>::least least_int;
}; typedef typename boost::uint_t<Bits>::least least_uint;
BOOST_CHECK((boost::is_same<int_exact, least_int>::value));
BOOST_CHECK((boost::is_same<uint_exact, least_uint>::value));
}
template <int Bits>
void do_test_exact(boost::mpl::false_ const&)
{
// Nothing to do, type does not have an ::extact member.
} }
template <int Bits>
// Show the types of an integer template version void do_test_bits()
#if CONTROL_SHOW_TYPES {
#define SHOW_TYPE(Template, Number, Type) ::std::cout << "Type \"" \ //
#Template "<" #Number ">::" #Type "\" is \"" << typeid(Template < \ // Recurse to next smallest number of bits:
Number > :: Type).name() << ".\"\n" //
#else do_test_bits<Bits - 1>();
#define SHOW_TYPE(Template, Number, Type) //
// Test exact types if we have them:
//
do_test_exact<Bits>(
boost::mpl::bool_<
(sizeof(unsigned char) * CHAR_BIT == Bits)
|| (sizeof(unsigned short) * CHAR_BIT == Bits)
|| (sizeof(unsigned int) * CHAR_BIT == Bits)
|| (sizeof(unsigned long) * CHAR_BIT == Bits)
#ifdef BOOST_HAS_LONG_LONG
|| (sizeof(unsigned long long) * CHAR_BIT == Bits)
#endif #endif
>());
//
// We need to check all aspects of the members of int_t<Bits> and uint_t<Bits>:
//
typedef typename boost::int_t<Bits>::least least_int;
typedef typename boost::int_t<Bits>::least fast_int;
typedef typename boost::uint_t<Bits>::least least_uint;
typedef typename boost::uint_t<Bits>::fast fast_uint;
#define SHOW_TYPES(Template, Type) SHOW_TYPE(Template, 32, Type); \ if(std::numeric_limits<least_int>::is_specialized)
SHOW_TYPE(Template, 31, Type); SHOW_TYPE(Template, 30, Type); \ {
SHOW_TYPE(Template, 29, Type); SHOW_TYPE(Template, 28, Type); \ BOOST_CHECK(std::numeric_limits<least_int>::digits + 1 >= Bits);
SHOW_TYPE(Template, 27, Type); SHOW_TYPE(Template, 26, Type); \ }
SHOW_TYPE(Template, 25, Type); SHOW_TYPE(Template, 24, Type); \ if(std::numeric_limits<least_uint>::is_specialized)
SHOW_TYPE(Template, 23, Type); SHOW_TYPE(Template, 22, Type); \ {
SHOW_TYPE(Template, 21, Type); SHOW_TYPE(Template, 20, Type); \ BOOST_CHECK(std::numeric_limits<least_uint>::digits >= Bits);
SHOW_TYPE(Template, 19, Type); SHOW_TYPE(Template, 18, Type); \ }
SHOW_TYPE(Template, 17, Type); SHOW_TYPE(Template, 16, Type); \ BOOST_CHECK(sizeof(least_int) * CHAR_BIT >= Bits);
SHOW_TYPE(Template, 15, Type); SHOW_TYPE(Template, 14, Type); \ BOOST_CHECK(sizeof(least_uint) * CHAR_BIT >= Bits);
SHOW_TYPE(Template, 13, Type); SHOW_TYPE(Template, 12, Type); \ BOOST_CHECK(sizeof(fast_int) >= sizeof(least_int));
SHOW_TYPE(Template, 11, Type); SHOW_TYPE(Template, 10, Type); \ BOOST_CHECK(sizeof(fast_uint) >= sizeof(least_uint));
SHOW_TYPE(Template, 9, Type); SHOW_TYPE(Template, 8, Type); \ //
SHOW_TYPE(Template, 7, Type); SHOW_TYPE(Template, 6, Type); \ // There should be no type smaller than least_* that also has enough bits:
SHOW_TYPE(Template, 5, Type); SHOW_TYPE(Template, 4, Type); \ //
SHOW_TYPE(Template, 3, Type); SHOW_TYPE(Template, 2, Type); \ if(!boost::is_same<signed char, least_int>::value)
SHOW_TYPE(Template, 1, Type); SHOW_TYPE(Template, 0, Type) {
BOOST_CHECK(std::numeric_limits<signed char>::digits < Bits);
if(!boost::is_same<short, least_int>::value)
{
BOOST_CHECK(std::numeric_limits<short>::digits < Bits);
if(!boost::is_same<int, least_int>::value)
{
BOOST_CHECK(std::numeric_limits<int>::digits < Bits);
if(!boost::is_same<long, least_int>::value)
{
BOOST_CHECK(std::numeric_limits<long>::digits < Bits);
}
}
}
}
// And again, but unsigned:
if(!boost::is_same<unsigned char, least_uint>::value)
{
BOOST_CHECK(std::numeric_limits<unsigned char>::digits < Bits);
if(!boost::is_same<unsigned short, least_uint>::value)
{
BOOST_CHECK(std::numeric_limits<unsigned short>::digits < Bits);
if(!boost::is_same<unsigned int, least_uint>::value)
{
BOOST_CHECK(std::numeric_limits<unsigned int>::digits < Bits);
if(!boost::is_same<unsigned long, least_uint>::value)
{
BOOST_CHECK(std::numeric_limits<unsigned long>::digits < Bits);
}
}
}
}
#define SHOW_SHIFTED_TYPE(Template, Number, Type) SHOW_TYPE(Template, (1UL << Number), Type) if(boost::minimal_test::errors_counter() != last_error_count)
{
#define SHOW_SHIFTED_TYPES(Template, Type) SHOW_SHIFTED_TYPE(Template, 30, Type); \ last_error_count = boost::minimal_test::errors_counter();
SHOW_SHIFTED_TYPE(Template, 29, Type); SHOW_SHIFTED_TYPE(Template, 28, Type); \ std::cout << "Errors occured while testing with bit count = " << Bits << std::endl;
SHOW_SHIFTED_TYPE(Template, 27, Type); SHOW_SHIFTED_TYPE(Template, 26, Type); \ std::cout << "Type int_t<" << Bits << ">::least was " << get_name_of_type(least_int(0)) << std::endl;
SHOW_SHIFTED_TYPE(Template, 25, Type); SHOW_SHIFTED_TYPE(Template, 24, Type); \ std::cout << "Type int_t<" << Bits << ">::fast was " << get_name_of_type(fast_int(0)) << std::endl;
SHOW_SHIFTED_TYPE(Template, 23, Type); SHOW_SHIFTED_TYPE(Template, 22, Type); \ std::cout << "Type uint_t<" << Bits << ">::least was " << get_name_of_type(least_uint(0)) << std::endl;
SHOW_SHIFTED_TYPE(Template, 21, Type); SHOW_SHIFTED_TYPE(Template, 20, Type); \ std::cout << "Type uint_t<" << Bits << ">::fast was " << get_name_of_type(fast_uint(0)) << std::endl;
SHOW_SHIFTED_TYPE(Template, 19, Type); SHOW_SHIFTED_TYPE(Template, 18, Type); \ }
SHOW_SHIFTED_TYPE(Template, 17, Type); SHOW_SHIFTED_TYPE(Template, 16, Type); \ }
SHOW_SHIFTED_TYPE(Template, 15, Type); SHOW_SHIFTED_TYPE(Template, 14, Type); \ template <>
SHOW_SHIFTED_TYPE(Template, 13, Type); SHOW_SHIFTED_TYPE(Template, 12, Type); \ void do_test_bits<-1>()
SHOW_SHIFTED_TYPE(Template, 11, Type); SHOW_SHIFTED_TYPE(Template, 10, Type); \ {
SHOW_SHIFTED_TYPE(Template, 9, Type); SHOW_SHIFTED_TYPE(Template, 8, Type); \ // Nothing to do here!!
SHOW_SHIFTED_TYPE(Template, 7, Type); SHOW_SHIFTED_TYPE(Template, 6, Type); \ }
SHOW_SHIFTED_TYPE(Template, 5, Type); SHOW_SHIFTED_TYPE(Template, 4, Type); \
SHOW_SHIFTED_TYPE(Template, 3, Type); SHOW_SHIFTED_TYPE(Template, 2, Type); \
SHOW_SHIFTED_TYPE(Template, 1, Type); SHOW_SHIFTED_TYPE(Template, 0, Type)
#define SHOW_POS_SHIFTED_TYPE(Template, Number, Type) SHOW_TYPE(Template, +(1L << Number), Type)
#define SHOW_POS_SHIFTED_TYPES(Template, Type) SHOW_POS_SHIFTED_TYPE(Template, 30, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 29, Type); SHOW_POS_SHIFTED_TYPE(Template, 28, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 27, Type); SHOW_POS_SHIFTED_TYPE(Template, 26, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 25, Type); SHOW_POS_SHIFTED_TYPE(Template, 24, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 23, Type); SHOW_POS_SHIFTED_TYPE(Template, 22, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 21, Type); SHOW_POS_SHIFTED_TYPE(Template, 20, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 19, Type); SHOW_POS_SHIFTED_TYPE(Template, 18, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 17, Type); SHOW_POS_SHIFTED_TYPE(Template, 16, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 15, Type); SHOW_POS_SHIFTED_TYPE(Template, 14, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 13, Type); SHOW_POS_SHIFTED_TYPE(Template, 12, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 11, Type); SHOW_POS_SHIFTED_TYPE(Template, 10, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 9, Type); SHOW_POS_SHIFTED_TYPE(Template, 8, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 7, Type); SHOW_POS_SHIFTED_TYPE(Template, 6, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 5, Type); SHOW_POS_SHIFTED_TYPE(Template, 4, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 3, Type); SHOW_POS_SHIFTED_TYPE(Template, 2, Type); \
SHOW_POS_SHIFTED_TYPE(Template, 1, Type); SHOW_POS_SHIFTED_TYPE(Template, 0, Type)
#define SHOW_NEG_SHIFTED_TYPE(Template, Number, Type) SHOW_TYPE(Template, -(1L << Number), Type)
#define SHOW_NEG_SHIFTED_TYPES(Template, Type) SHOW_NEG_SHIFTED_TYPE(Template, 30, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 29, Type); SHOW_NEG_SHIFTED_TYPE(Template, 28, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 27, Type); SHOW_NEG_SHIFTED_TYPE(Template, 26, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 25, Type); SHOW_NEG_SHIFTED_TYPE(Template, 24, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 23, Type); SHOW_NEG_SHIFTED_TYPE(Template, 22, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 21, Type); SHOW_NEG_SHIFTED_TYPE(Template, 20, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 19, Type); SHOW_NEG_SHIFTED_TYPE(Template, 18, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 17, Type); SHOW_NEG_SHIFTED_TYPE(Template, 16, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 15, Type); SHOW_NEG_SHIFTED_TYPE(Template, 14, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 13, Type); SHOW_NEG_SHIFTED_TYPE(Template, 12, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 11, Type); SHOW_NEG_SHIFTED_TYPE(Template, 10, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 9, Type); SHOW_NEG_SHIFTED_TYPE(Template, 8, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 7, Type); SHOW_NEG_SHIFTED_TYPE(Template, 6, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 5, Type); SHOW_NEG_SHIFTED_TYPE(Template, 4, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 3, Type); SHOW_NEG_SHIFTED_TYPE(Template, 2, Type); \
SHOW_NEG_SHIFTED_TYPE(Template, 1, Type); SHOW_NEG_SHIFTED_TYPE(Template, 0, Type)
// Test if a constant can fit within a certain type
#define PRIVATE_FIT_TEST(Template, Number, Type, Value) BOOST_CHECK( Template < Number > :: Type ( Value ) == Value )
#if (ULONG_MAX > 0xFFFFFFFFL) || !defined(BOOST_NO_INTEGRAL_INT64_T)
#define PRIVATE_FIT_TESTS(Template, Type, ValType, InitVal) do { ValType v = InitVal ; \
PRIVATE_FIT_TEST(Template, 64, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 63, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 62, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 61, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 60, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 59, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 58, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 57, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 56, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 55, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 54, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 53, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 52, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 51, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 50, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 49, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 48, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 47, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 46, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 45, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 44, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 43, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 42, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 41, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 40, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 39, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 38, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 37, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 36, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 35, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 34, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 33, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 32, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 31, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 30, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 29, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 28, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 27, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 26, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 25, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 24, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 23, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 22, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 21, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 20, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 19, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 18, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 17, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 16, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 15, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 14, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 13, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 12, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 11, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 10, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 9, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 8, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 7, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 6, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 5, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 4, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 3, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 2, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 1, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 0, Type, v); } while ( false )
#else
#define PRIVATE_FIT_TESTS(Template, Type, ValType, InitVal) do { ValType v = InitVal ; \
PRIVATE_FIT_TEST(Template, 32, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 31, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 30, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 29, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 28, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 27, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 26, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 25, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 24, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 23, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 22, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 21, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 20, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 19, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 18, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 17, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 16, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 15, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 14, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 13, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 12, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 11, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 10, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 9, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 8, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 7, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 6, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 5, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 4, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 3, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 2, Type, v); v >>= 1; \
PRIVATE_FIT_TEST(Template, 1, Type, v); v >>= 1; PRIVATE_FIT_TEST(Template, 0, Type, v); } while ( false )
#endif
#define PRIVATE_SHIFTED_FIT_TEST(Template, Number, Type, Value) BOOST_CHECK( Template < (ULONG_MAX >> Number) > :: Type ( Value ) == Value )
#define PRIVATE_SHIFTED_FIT_TESTS(Template, Type, ValType, InitVal) do { ValType v = InitVal ; \
PRIVATE_SHIFTED_FIT_TEST(Template, 0, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 1, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 2, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 3, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 4, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 5, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 6, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 7, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 8, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 9, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 10, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 11, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 12, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 13, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 14, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 15, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 16, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 17, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 18, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 19, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 20, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 21, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 22, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 23, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 24, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 25, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 26, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 27, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 28, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 29, Type, v); v >>= 1; \
PRIVATE_SHIFTED_FIT_TEST(Template, 30, Type, v); v >>= 1; PRIVATE_SHIFTED_FIT_TEST(Template, 31, Type, v); } while ( false )
#define PRIVATE_POS_SHIFTED_FIT_TEST(Template, Number, Type, Value) BOOST_CHECK( Template < (LONG_MAX >> Number) > :: Type ( Value ) == Value )
#define PRIVATE_POS_FIT_TESTS(Template, Type, ValType, InitVal) do { ValType v = InitVal ; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 0, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 1, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 2, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 3, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 4, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 5, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 6, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 7, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 8, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 9, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 10, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 11, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 12, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 13, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 14, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 15, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 16, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 17, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 18, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 19, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 20, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 21, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 22, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 23, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 24, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 25, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 26, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 27, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 28, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 29, Type, v); v >>= 1; \
PRIVATE_POS_SHIFTED_FIT_TEST(Template, 30, Type, v); v >>= 1; PRIVATE_POS_SHIFTED_FIT_TEST(Template, 31, Type, v); } while ( false )
#define PRIVATE_NEG_SHIFTED_FIT_TEST(Template, Number, Type, Value) BOOST_CHECK( Template < (LONG_MIN >> Number) > :: Type ( Value ) == Value )
#define PRIVATE_NEG_FIT_TESTS(Template, Type, ValType, InitVal) do { ValType v = InitVal ; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 0, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 1, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 2, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 3, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 4, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 5, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 6, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 7, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 8, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 9, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 10, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 11, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 12, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 13, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 14, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 15, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 16, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 17, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 18, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 19, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 20, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 21, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 22, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 23, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 24, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 25, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 26, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 27, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 28, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 29, Type, v); v >>= 1; \
PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 30, Type, v); v >>= 1; PRIVATE_NEG_SHIFTED_FIT_TEST(Template, 31, Type, v); } while ( false )
template <class Traits, class Expected>
void test_min_max_type(Expected val)
{
typedef typename Traits::least least_type;
typedef typename Traits::fast fast_type;
BOOST_CHECK((boost::is_same<least_type, Expected>::value));
BOOST_CHECK(sizeof(fast_type) >= sizeof(least_type));
BOOST_CHECK((std::numeric_limits<least_type>::min)() <= val);
BOOST_CHECK((std::numeric_limits<least_type>::max)() >= val);
}
// Test program // Test program
int int test_main(int, char*[])
test_main
(
int,
char*[]
)
{ {
#ifndef BOOST_NO_USING_TEMPLATE // Test int_t and unint_t first:
using boost::int_t; if(std::numeric_limits<boost::intmax_t>::is_specialized)
using boost::uint_t; do_test_bits<std::numeric_limits<boost::uintmax_t>::digits>();
using boost::int_max_value_t; else
using boost::int_min_value_t; do_test_bits<std::numeric_limits<long>::digits>();
using boost::uint_value_t;
#else
using namespace boost;
#endif
SHOW_TYPES( int_t, least ); //
SHOW_TYPES( int_t, fast ); // Test min and max value types:
SHOW_TYPES( uint_t, least ); //
SHOW_TYPES( uint_t, fast ); test_min_max_type<boost::int_max_value_t<SCHAR_MAX>, signed char>(SCHAR_MAX);
SHOW_POS_SHIFTED_TYPES( int_max_value_t, least ); test_min_max_type<boost::int_min_value_t<SCHAR_MIN>, signed char>(SCHAR_MIN);
SHOW_POS_SHIFTED_TYPES( int_max_value_t, fast ); test_min_max_type<boost::uint_value_t<UCHAR_MAX>, unsigned char>(UCHAR_MAX);
SHOW_NEG_SHIFTED_TYPES( int_min_value_t, least ); #if(USHRT_MAX != UCHAR_MAX)
SHOW_NEG_SHIFTED_TYPES( int_min_value_t, fast ); test_min_max_type<boost::int_max_value_t<SCHAR_MAX+1>, short>(SCHAR_MAX+1);
SHOW_SHIFTED_TYPES( uint_value_t, least ); test_min_max_type<boost::int_min_value_t<SCHAR_MIN-1>, short>(SCHAR_MIN-1);
SHOW_SHIFTED_TYPES( uint_value_t, fast ); test_min_max_type<boost::uint_value_t<UCHAR_MAX+1>, unsigned short>(UCHAR_MAX+1);
test_min_max_type<boost::int_max_value_t<SHRT_MAX>, short>(SHRT_MAX);
PRIVATE_FIT_TESTS( int_t, least, long, LONG_MAX ); test_min_max_type<boost::int_min_value_t<SHRT_MIN>, short>(SHRT_MIN);
PRIVATE_FIT_TESTS( int_t, fast, long, LONG_MAX ); test_min_max_type<boost::uint_value_t<USHRT_MAX>, unsigned short>(USHRT_MAX);
PRIVATE_FIT_TESTS( uint_t, least, unsigned long, ULONG_MAX ); #endif
PRIVATE_FIT_TESTS( uint_t, fast, unsigned long, ULONG_MAX ); #if(UINT_MAX != USHRT_MAX)
PRIVATE_POS_FIT_TESTS( int_max_value_t, least, long, LONG_MAX ); test_min_max_type<boost::int_max_value_t<SHRT_MAX+1>, int>(SHRT_MAX+1);
PRIVATE_POS_FIT_TESTS( int_max_value_t, fast, long, LONG_MAX ); test_min_max_type<boost::int_min_value_t<SHRT_MIN-1>, int>(SHRT_MIN-1);
PRIVATE_NEG_FIT_TESTS( int_min_value_t, least, long, LONG_MIN ); test_min_max_type<boost::uint_value_t<USHRT_MAX+1>, unsigned int>(USHRT_MAX+1);
PRIVATE_NEG_FIT_TESTS( int_min_value_t, fast, long, LONG_MIN ); test_min_max_type<boost::int_max_value_t<INT_MAX>, int>(INT_MAX);
PRIVATE_SHIFTED_FIT_TESTS( uint_value_t, least, unsigned long, ULONG_MAX ); test_min_max_type<boost::int_min_value_t<INT_MIN>, int>(INT_MIN);
PRIVATE_SHIFTED_FIT_TESTS( uint_value_t, fast, unsigned long, ULONG_MAX ); test_min_max_type<boost::uint_value_t<UINT_MAX>, unsigned int>(UINT_MAX);
#endif
return boost::exit_success; #if(ULONG_MAX != UINT_MAX)
test_min_max_type<boost::int_max_value_t<INT_MAX+1L>, long>(INT_MAX+1L);
test_min_max_type<boost::int_min_value_t<INT_MIN-1L>, long>(INT_MIN-1L);
test_min_max_type<boost::uint_value_t<UINT_MAX+1uL>, unsigned long>(UINT_MAX+1uL);
test_min_max_type<boost::int_max_value_t<LONG_MAX>, long>(LONG_MAX);
test_min_max_type<boost::int_min_value_t<LONG_MIN>, long>(LONG_MIN);
test_min_max_type<boost::uint_value_t<ULONG_MAX>, unsigned long>(ULONG_MAX);
#endif
#if defined(BOOST_HAS_LONG_LONG) && (defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX))
test_min_max_type<boost::int_max_value_t<LONG_MAX+1LL>, long long>(LONG_MAX+1LL);
test_min_max_type<boost::int_min_value_t<LONG_MIN-1LL>, long long>(LONG_MIN-1LL);
test_min_max_type<boost::uint_value_t<ULONG_MAX+1uLL>, unsigned long long>(ULONG_MAX+1uLL);
test_min_max_type<boost::int_max_value_t<LLONG_MAX>, long long>(LLONG_MAX);
test_min_max_type<boost::int_min_value_t<LLONG_MIN>, long long>(LLONG_MIN);
test_min_max_type<boost::uint_value_t<ULLONG_MAX>, unsigned long long>(ULLONG_MAX);
#endif
#if defined(BOOST_HAS_LONG_LONG) && (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX))
test_min_max_type<boost::int_max_value_t<LONG_MAX+1LL>, long long>(LONG_MAX+1LL);
test_min_max_type<boost::int_min_value_t<LONG_MIN-1LL>, long long>(LONG_MIN-1LL);
test_min_max_type<boost::uint_value_t<ULONG_MAX+1uLL>, unsigned long long>(ULONG_MAX+1uLL);
test_min_max_type<boost::int_max_value_t<LONG_LONG_MAX>, long long>(LONG_LONG_MAX);
test_min_max_type<boost::int_min_value_t<LONG_LONG_MIN>, long long>(LONG_LONG_MIN);
test_min_max_type<boost::uint_value_t<ULONG_LONG_MAX>, unsigned long long>(ULONG_LONG_MAX);
#endif
#if defined(BOOST_HAS_LONG_LONG) && (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX))
test_min_max_type<boost::int_max_value_t<LONG_MAX+1LL>, long long>(LONG_MAX+1LL);
test_min_max_type<boost::int_min_value_t<LONG_MIN-1LL>, long long>(LONG_MIN-1LL);
test_min_max_type<boost::uint_value_t<ULONG_MAX+1uLL>, unsigned long long>(ULONG_MAX+1uLL);
test_min_max_type<boost::int_max_value_t<LONGLONG_MAX>, long long>(LONGLONG_MAX);
test_min_max_type<boost::int_min_value_t<LONGLONG_MIN>, long long>(LONGLONG_MAX);
test_min_max_type<boost::uint_value_t<ULONGLONG_MAX>, unsigned long long>(ULONGLONG_MAX);
#endif
#if defined(BOOST_HAS_LONG_LONG) && (defined(_ULLONG_MAX) && defined(_LLONG_MIN) && (_ULLONG_MAX != ULONG_MAX))
test_min_max_type<boost::int_max_value_t<LONG_MAX+1LL>, long long>(LONG_MAX+1LL);
test_min_max_type<boost::int_min_value_t<LONG_MIN-1LL>, long long>(LONG_MIN-1LL);
test_min_max_type<boost::uint_value_t<ULONG_MAX+1uLL>, unsigned long long>(ULONG_MAX+1uLL);
test_min_max_type<boost::int_max_value_t<_LLONG_MAX>, long long>(_LLONG_MAX);
test_min_max_type<boost::int_min_value_t<_LLONG_MIN>, long long>(_LLONG_MIN);
test_min_max_type<boost::uint_value_t<_ULLONG_MAX>, unsigned long long>(_ULLONG_MAX);
#endif
return 0;
} }