1 // boost cstdint.hpp test program ------------------------------------------//
3 // Copyright Beman Dawes 2000. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/integer for documentation.
11 // 11 Sep 01 Adapted to work with macros defined in native stdint.h (John Maddock)
12 // 12 Nov 00 Adapted to merged <boost/cstdint.hpp>
13 // 23 Sep 00 Added INTXX_C constant macro support + int64_t support (John Maddock).
14 // 28 Jun 00 Initial version
17 // There are two ways to test this: in version 1, we include cstdint.hpp as the first
18 // include, which means we get decide whether __STDC_CONSTANT_MACROS is defined.
19 // In version two we include stdint.h with __STDC_CONSTANT_MACROS *NOT* defined first,
20 // and check that we still end up with compatible definitions for the INT#_C macros.
25 #if defined(__GNUC__) && (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4))
26 // We can't suppress this warning on the command line as not all GCC versions support -Wno-type-limits :
27 #pragma GCC diagnostic ignored "-Wtype-limits"
30 #include <boost/config.hpp>
32 #ifdef BOOST_HAS_STDINT_H
34 # include <inttypes.h>
40 #include <boost/cstdint.hpp>
41 #include <boost/detail/lightweight_test.hpp>
44 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
46 // the following class is designed to verify
47 // that the various INTXX_C macros can be used
48 // in integral constant expressions:
50 struct integral_constant_checker
52 static const boost::int8_t int8
= INT8_C(-127);
53 static const boost::int_least8_t int_least8
= INT8_C(-127);
54 static const boost::int_fast8_t int_fast8
= INT8_C(-127);
56 static const boost::uint8_t uint8
= UINT8_C(255);
57 static const boost::uint_least8_t uint_least8
= UINT8_C(255);
58 static const boost::uint_fast8_t uint_fast8
= UINT8_C(255);
60 static const boost::int16_t int16
= INT16_C(-32767);
61 static const boost::int_least16_t int_least16
= INT16_C(-32767);
62 static const boost::int_fast16_t int_fast16
= INT16_C(-32767);
64 static const boost::uint16_t uint16
= UINT16_C(65535);
65 static const boost::uint_least16_t uint_least16
= UINT16_C(65535);
66 static const boost::uint_fast16_t uint_fast16
= UINT16_C(65535);
68 static const boost::int32_t int32
= INT32_C(-2147483647);
69 static const boost::int_least32_t int_least32
= INT32_C(-2147483647);
70 static const boost::int_fast32_t int_fast32
= INT32_C(-2147483647);
72 static const boost::uint32_t uint32
= UINT32_C(4294967295);
73 static const boost::uint_least32_t uint_least32
= UINT32_C(4294967295);
74 static const boost::uint_fast32_t uint_fast32
= UINT32_C(4294967295);
79 void integral_constant_checker::check()
81 BOOST_TEST( int8
== -127 );
82 BOOST_TEST( int_least8
== -127 );
83 BOOST_TEST( int_fast8
== -127 );
84 BOOST_TEST( uint8
== 255u );
85 BOOST_TEST( uint_least8
== 255u );
86 BOOST_TEST( uint_fast8
== 255u );
87 BOOST_TEST( int16
== -32767 );
88 BOOST_TEST( int_least16
== -32767 );
89 BOOST_TEST( int_fast16
== -32767 );
90 BOOST_TEST( uint16
== 65535u );
91 BOOST_TEST( uint_least16
== 65535u );
92 BOOST_TEST( uint_fast16
== 65535u );
93 BOOST_TEST( int32
== -2147483647 );
94 BOOST_TEST( int_least32
== -2147483647 );
95 BOOST_TEST( int_fast32
== -2147483647 );
96 BOOST_TEST( uint32
== 4294967295u );
97 BOOST_TEST( uint_least32
== 4294967295u );
98 BOOST_TEST( uint_fast32
== 4294967295u );
100 #endif // BOOST_NO_INCLASS_MEMBER_INITIALIZATION
103 // the following function simply verifies that the type
104 // of an integral constant is correctly defined:
106 #ifdef BOOST_BORLANDC
107 #pragma option -w-8008
108 #pragma option -w-8066
110 template <class T1
, class T2
>
111 void integral_constant_type_check(T1
, T2
)
114 // the types T1 and T2 may not be exactly
115 // the same type, but they should be the
116 // same size and signedness. We could use
117 // numeric_limits to verify this, but
118 // numeric_limits implementations currently
119 // vary too much, or are incomplete or missing.
121 T1 t1
= static_cast<T1
>(-1); // cast suppresses warnings
122 T2 t2
= static_cast<T2
>(-1); // ditto
123 #if defined(BOOST_HAS_STDINT_H)
124 // if we have a native stdint.h
125 // then the INTXX_C macros may define
126 // a type that's wider than required:
127 BOOST_TEST(sizeof(T1
) <= sizeof(T2
));
129 BOOST_TEST(sizeof(T1
) == sizeof(T2
));
130 BOOST_TEST(t1
== t2
);
132 #if defined(BOOST_HAS_STDINT_H)
133 // native headers are permitted to promote small
134 // unsigned types to type int:
135 if(sizeof(T1
) >= sizeof(int))
140 BOOST_TEST(!(t2
> 0));
143 BOOST_TEST(!(t2
> 0));
148 BOOST_TEST(!(t2
> 0));
153 int main(int, char*[])
155 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
156 integral_constant_checker::check();
159 // verify the types of the integral constants:
161 integral_constant_type_check(boost::int8_t(0), INT8_C(0));
162 integral_constant_type_check(boost::uint8_t(0), UINT8_C(0));
163 integral_constant_type_check(boost::int16_t(0), INT16_C(0));
164 integral_constant_type_check(boost::uint16_t(0), UINT16_C(0));
165 integral_constant_type_check(boost::int32_t(0), INT32_C(0));
166 integral_constant_type_check(boost::uint32_t(0), UINT32_C(0));
167 #ifndef BOOST_NO_INT64_T
168 integral_constant_type_check(boost::int64_t(0), INT64_C(0));
169 integral_constant_type_check(boost::uint64_t(0), UINT64_C(0));
172 boost::int8_t int8
= INT8_C(-127);
173 boost::int_least8_t int_least8
= INT8_C(-127);
174 boost::int_fast8_t int_fast8
= INT8_C(-127);
176 boost::uint8_t uint8
= UINT8_C(255);
177 boost::uint_least8_t uint_least8
= UINT8_C(255);
178 boost::uint_fast8_t uint_fast8
= UINT8_C(255);
180 boost::int16_t int16
= INT16_C(-32767);
181 boost::int_least16_t int_least16
= INT16_C(-32767);
182 boost::int_fast16_t int_fast16
= INT16_C(-32767);
184 boost::uint16_t uint16
= UINT16_C(65535);
185 boost::uint_least16_t uint_least16
= UINT16_C(65535);
186 boost::uint_fast16_t uint_fast16
= UINT16_C(65535);
188 boost::int32_t int32
= INT32_C(-2147483647);
189 boost::int_least32_t int_least32
= INT32_C(-2147483647);
190 boost::int_fast32_t int_fast32
= INT32_C(-2147483647);
192 boost::uint32_t uint32
= UINT32_C(4294967295);
193 boost::uint_least32_t uint_least32
= UINT32_C(4294967295);
194 boost::uint_fast32_t uint_fast32
= UINT32_C(4294967295);
196 #ifndef BOOST_NO_INT64_T
197 boost::int64_t int64
= INT64_C(-9223372036854775807);
198 boost::int_least64_t int_least64
= INT64_C(-9223372036854775807);
199 boost::int_fast64_t int_fast64
= INT64_C(-9223372036854775807);
201 boost::uint64_t uint64
= UINT64_C(18446744073709551615);
202 boost::uint_least64_t uint_least64
= UINT64_C(18446744073709551615);
203 boost::uint_fast64_t uint_fast64
= UINT64_C(18446744073709551615);
205 boost::intmax_t intmax
= INTMAX_C(-9223372036854775807);
206 boost::uintmax_t uintmax
= UINTMAX_C(18446744073709551615);
208 boost::intmax_t intmax
= INTMAX_C(-2147483647);
209 boost::uintmax_t uintmax
= UINTMAX_C(4294967295);
212 BOOST_TEST( int8
== -127 );
213 BOOST_TEST( int_least8
== -127 );
214 BOOST_TEST( int_fast8
== -127 );
215 BOOST_TEST( uint8
== 255u );
216 BOOST_TEST( uint_least8
== 255u );
217 BOOST_TEST( uint_fast8
== 255u );
218 BOOST_TEST( int16
== -32767 );
219 BOOST_TEST( int_least16
== -32767 );
220 BOOST_TEST( int_fast16
== -32767 );
221 BOOST_TEST( uint16
== 65535u );
222 BOOST_TEST( uint_least16
== 65535u );
223 BOOST_TEST( uint_fast16
== 65535u );
224 BOOST_TEST( int32
== -2147483647 );
225 BOOST_TEST( int_least32
== -2147483647 );
226 BOOST_TEST( int_fast32
== -2147483647 );
227 BOOST_TEST( uint32
== 4294967295u );
228 BOOST_TEST( uint_least32
== 4294967295u );
229 BOOST_TEST( uint_fast32
== 4294967295u );
231 #ifndef BOOST_NO_INT64_T
232 BOOST_TEST( int64
== INT64_C(-9223372036854775807) );
233 BOOST_TEST( int_least64
== INT64_C(-9223372036854775807) );
234 BOOST_TEST( int_fast64
== INT64_C(-9223372036854775807) );
235 BOOST_TEST( uint64
== UINT64_C(18446744073709551615) );
236 BOOST_TEST( uint_least64
== UINT64_C(18446744073709551615) );
237 BOOST_TEST( uint_fast64
== UINT64_C(18446744073709551615) );
238 BOOST_TEST( intmax
== INT64_C(-9223372036854775807) );
239 BOOST_TEST( uintmax
== UINT64_C(18446744073709551615) );
241 BOOST_TEST( intmax
== -2147483647 );
242 BOOST_TEST( uintmax
== 4294967295u );
247 return boost::report_errors();