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