1 // boost cstdint.hpp test program ------------------------------------------//
2
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)
6
7
8 // See http://www.boost.org/libs/integer for documentation.
9
10 // Revision History
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
15
16 //
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.
21 //
22 // This is version 1.
23 //
24
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"
28 #endif
29
30 #include <boost/cstdint.hpp>
31 #include <boost/detail/lightweight_test.hpp>
32 #include <iostream>
33
34 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
35 //
36 // the following class is designed to verify
37 // that the various INTXX_C macros can be used
38 // in integral constant expressions:
39 //
40 struct integral_constant_checker
41 {
42 static const boost::int8_t int8 = INT8_C(-127);
43 static const boost::int_least8_t int_least8 = INT8_C(-127);
44 static const boost::int_fast8_t int_fast8 = INT8_C(-127);
45
46 static const boost::uint8_t uint8 = UINT8_C(255);
47 static const boost::uint_least8_t uint_least8 = UINT8_C(255);
48 static const boost::uint_fast8_t uint_fast8 = UINT8_C(255);
49
50 static const boost::int16_t int16 = INT16_C(-32767);
51 static const boost::int_least16_t int_least16 = INT16_C(-32767);
52 static const boost::int_fast16_t int_fast16 = INT16_C(-32767);
53
54 static const boost::uint16_t uint16 = UINT16_C(65535);
55 static const boost::uint_least16_t uint_least16 = UINT16_C(65535);
56 static const boost::uint_fast16_t uint_fast16 = UINT16_C(65535);
57
58 static const boost::int32_t int32 = INT32_C(-2147483647);
59 static const boost::int_least32_t int_least32 = INT32_C(-2147483647);
60 static const boost::int_fast32_t int_fast32 = INT32_C(-2147483647);
61
62 static const boost::uint32_t uint32 = UINT32_C(4294967295);
63 static const boost::uint_least32_t uint_least32 = UINT32_C(4294967295);
64 static const boost::uint_fast32_t uint_fast32 = UINT32_C(4294967295);
65
66 static void check();
67 };
68
check()69 void integral_constant_checker::check()
70 {
71 BOOST_TEST( int8 == -127 );
72 BOOST_TEST( int_least8 == -127 );
73 BOOST_TEST( int_fast8 == -127 );
74 BOOST_TEST( uint8 == 255u );
75 BOOST_TEST( uint_least8 == 255u );
76 BOOST_TEST( uint_fast8 == 255u );
77 BOOST_TEST( int16 == -32767 );
78 BOOST_TEST( int_least16 == -32767 );
79 BOOST_TEST( int_fast16 == -32767 );
80 BOOST_TEST( uint16 == 65535u );
81 BOOST_TEST( uint_least16 == 65535u );
82 BOOST_TEST( uint_fast16 == 65535u );
83 BOOST_TEST( int32 == -2147483647 );
84 BOOST_TEST( int_least32 == -2147483647 );
85 BOOST_TEST( int_fast32 == -2147483647 );
86 BOOST_TEST( uint32 == 4294967295u );
87 BOOST_TEST( uint_least32 == 4294967295u );
88 BOOST_TEST( uint_fast32 == 4294967295u );
89 }
90 #endif // BOOST_NO_INCLASS_MEMBER_INITIALIZATION
91
92 //
93 // the following function simply verifies that the type
94 // of an integral constant is correctly defined:
95 //
96 #ifdef BOOST_BORLANDC
97 #pragma option -w-8008
98 #pragma option -w-8066
99 #endif
100 template <class T1, class T2>
integral_constant_type_check(T1,T2)101 void integral_constant_type_check(T1, T2)
102 {
103 //
104 // the types T1 and T2 may not be exactly
105 // the same type, but they should be the
106 // same size and signedness. We could use
107 // numeric_limits to verify this, but
108 // numeric_limits implementations currently
109 // vary too much, or are incomplete or missing.
110 //
111 T1 t1 = static_cast<T1>(-1); // cast suppresses warnings
112 T2 t2 = static_cast<T2>(-1); // ditto
113 #if defined(BOOST_HAS_STDINT_H)
114 // if we have a native stdint.h
115 // then the INTXX_C macros may define
116 // a type that's wider than required:
117 BOOST_TEST(sizeof(T1) <= sizeof(T2));
118 #else
119 BOOST_TEST(sizeof(T1) == sizeof(T2));
120 BOOST_TEST(t1 == t2);
121 #endif
122 #if defined(BOOST_HAS_STDINT_H)
123 // native headers are permitted to promote small
124 // unsigned types to type int:
125 if(sizeof(T1) >= sizeof(int))
126 {
127 if(t1 > 0)
128 BOOST_TEST(t2 > 0);
129 else
130 BOOST_TEST(!(t2 > 0));
131 }
132 else if(t1 < 0)
133 BOOST_TEST(!(t2 > 0));
134 #else
135 if(t1 > 0)
136 BOOST_TEST(t2 > 0);
137 else
138 BOOST_TEST(!(t2 > 0));
139 #endif
140 }
141
142
main(int,char * [])143 int main(int, char*[])
144 {
145 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
146 integral_constant_checker::check();
147 #endif
148 //
149 // verify the types of the integral constants:
150 //
151 integral_constant_type_check(boost::int8_t(0), INT8_C(0));
152 integral_constant_type_check(boost::uint8_t(0), UINT8_C(0));
153 integral_constant_type_check(boost::int16_t(0), INT16_C(0));
154 integral_constant_type_check(boost::uint16_t(0), UINT16_C(0));
155 integral_constant_type_check(boost::int32_t(0), INT32_C(0));
156 integral_constant_type_check(boost::uint32_t(0), UINT32_C(0));
157 #ifndef BOOST_NO_INT64_T
158 integral_constant_type_check(boost::int64_t(0), INT64_C(0));
159 integral_constant_type_check(boost::uint64_t(0), UINT64_C(0));
160 #endif
161 //
162 boost::int8_t int8 = INT8_C(-127);
163 boost::int_least8_t int_least8 = INT8_C(-127);
164 boost::int_fast8_t int_fast8 = INT8_C(-127);
165
166 boost::uint8_t uint8 = UINT8_C(255);
167 boost::uint_least8_t uint_least8 = UINT8_C(255);
168 boost::uint_fast8_t uint_fast8 = UINT8_C(255);
169
170 boost::int16_t int16 = INT16_C(-32767);
171 boost::int_least16_t int_least16 = INT16_C(-32767);
172 boost::int_fast16_t int_fast16 = INT16_C(-32767);
173
174 boost::uint16_t uint16 = UINT16_C(65535);
175 boost::uint_least16_t uint_least16 = UINT16_C(65535);
176 boost::uint_fast16_t uint_fast16 = UINT16_C(65535);
177
178 boost::int32_t int32 = INT32_C(-2147483647);
179 boost::int_least32_t int_least32 = INT32_C(-2147483647);
180 boost::int_fast32_t int_fast32 = INT32_C(-2147483647);
181
182 boost::uint32_t uint32 = UINT32_C(4294967295);
183 boost::uint_least32_t uint_least32 = UINT32_C(4294967295);
184 boost::uint_fast32_t uint_fast32 = UINT32_C(4294967295);
185
186 #ifndef BOOST_NO_INT64_T
187 boost::int64_t int64 = INT64_C(-9223372036854775807);
188 boost::int_least64_t int_least64 = INT64_C(-9223372036854775807);
189 boost::int_fast64_t int_fast64 = INT64_C(-9223372036854775807);
190
191 boost::uint64_t uint64 = UINT64_C(18446744073709551615);
192 boost::uint_least64_t uint_least64 = UINT64_C(18446744073709551615);
193 boost::uint_fast64_t uint_fast64 = UINT64_C(18446744073709551615);
194
195 boost::intmax_t intmax = INTMAX_C(-9223372036854775807);
196 boost::uintmax_t uintmax = UINTMAX_C(18446744073709551615);
197 #else
198 boost::intmax_t intmax = INTMAX_C(-2147483647);
199 boost::uintmax_t uintmax = UINTMAX_C(4294967295);
200 #endif
201
202 BOOST_TEST( int8 == -127 );
203 BOOST_TEST( int_least8 == -127 );
204 BOOST_TEST( int_fast8 == -127 );
205 BOOST_TEST( uint8 == 255u );
206 BOOST_TEST( uint_least8 == 255u );
207 BOOST_TEST( uint_fast8 == 255u );
208 BOOST_TEST( int16 == -32767 );
209 BOOST_TEST( int_least16 == -32767 );
210 BOOST_TEST( int_fast16 == -32767 );
211 BOOST_TEST( uint16 == 65535u );
212 BOOST_TEST( uint_least16 == 65535u );
213 BOOST_TEST( uint_fast16 == 65535u );
214 BOOST_TEST( int32 == -2147483647 );
215 BOOST_TEST( int_least32 == -2147483647 );
216 BOOST_TEST( int_fast32 == -2147483647 );
217 BOOST_TEST( uint32 == 4294967295u );
218 BOOST_TEST( uint_least32 == 4294967295u );
219 BOOST_TEST( uint_fast32 == 4294967295u );
220
221 #ifndef BOOST_NO_INT64_T
222 BOOST_TEST( int64 == INT64_C(-9223372036854775807) );
223 BOOST_TEST( int_least64 == INT64_C(-9223372036854775807) );
224 BOOST_TEST( int_fast64 == INT64_C(-9223372036854775807) );
225 BOOST_TEST( uint64 == UINT64_C(18446744073709551615) );
226 BOOST_TEST( uint_least64 == UINT64_C(18446744073709551615) );
227 BOOST_TEST( uint_fast64 == UINT64_C(18446744073709551615) );
228 BOOST_TEST( intmax == INT64_C(-9223372036854775807) );
229 BOOST_TEST( uintmax == UINT64_C(18446744073709551615) );
230 #else
231 BOOST_TEST( intmax == -2147483647 );
232 BOOST_TEST( uintmax == 4294967295u );
233 #endif
234
235
236 std::cout << "OK\n";
237 return boost::report_errors();
238 }
239