| 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 defined(BOOST_HAS_STDINT_H) |
|---|
| 117 | // native headers are permitted to promote small |
|---|
| 118 | // unsigned types to type int: |
|---|
| 119 | if(sizeof(T1) >= sizeof(int)) |
|---|
| 120 | { |
|---|
| 121 | if(t1 > 0) |
|---|
| 122 | assert(t2 > 0); |
|---|
| 123 | else |
|---|
| 124 | assert(!(t2 > 0)); |
|---|
| 125 | } |
|---|
| 126 | else if(t1 < 0) |
|---|
| 127 | assert(!(t2 > 0)); |
|---|
| 128 | #else |
|---|
| 129 | if(t1 > 0) |
|---|
| 130 | assert(t2 > 0); |
|---|
| 131 | else |
|---|
| 132 | assert(!(t2 > 0)); |
|---|
| 133 | #endif |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | int main() |
|---|
| 138 | { |
|---|
| 139 | #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION |
|---|
| 140 | integral_constant_checker::check(); |
|---|
| 141 | #endif |
|---|
| 142 | // |
|---|
| 143 | // verify the types of the integral constants: |
|---|
| 144 | // |
|---|
| 145 | integral_constant_type_check(boost::int8_t(0), INT8_C(0)); |
|---|
| 146 | integral_constant_type_check(boost::uint8_t(0), UINT8_C(0)); |
|---|
| 147 | integral_constant_type_check(boost::int16_t(0), INT16_C(0)); |
|---|
| 148 | integral_constant_type_check(boost::uint16_t(0), UINT16_C(0)); |
|---|
| 149 | integral_constant_type_check(boost::int32_t(0), INT32_C(0)); |
|---|
| 150 | integral_constant_type_check(boost::uint32_t(0), UINT32_C(0)); |
|---|
| 151 | #ifndef BOOST_NO_INT64_T |
|---|
| 152 | integral_constant_type_check(boost::int64_t(0), INT64_C(0)); |
|---|
| 153 | integral_constant_type_check(boost::uint64_t(0), UINT64_C(0)); |
|---|
| 154 | #endif |
|---|
| 155 | // |
|---|
| 156 | boost::int8_t int8 = INT8_C(-127); |
|---|
| 157 | boost::int_least8_t int_least8 = INT8_C(-127); |
|---|
| 158 | boost::int_fast8_t int_fast8 = INT8_C(-127); |
|---|
| 159 | |
|---|
| 160 | boost::uint8_t uint8 = UINT8_C(255); |
|---|
| 161 | boost::uint_least8_t uint_least8 = UINT8_C(255); |
|---|
| 162 | boost::uint_fast8_t uint_fast8 = UINT8_C(255); |
|---|
| 163 | |
|---|
| 164 | boost::int16_t int16 = INT16_C(-32767); |
|---|
| 165 | boost::int_least16_t int_least16 = INT16_C(-32767); |
|---|
| 166 | boost::int_fast16_t int_fast16 = INT16_C(-32767); |
|---|
| 167 | |
|---|
| 168 | boost::uint16_t uint16 = UINT16_C(65535); |
|---|
| 169 | boost::uint_least16_t uint_least16 = UINT16_C(65535); |
|---|
| 170 | boost::uint_fast16_t uint_fast16 = UINT16_C(65535); |
|---|
| 171 | |
|---|
| 172 | boost::int32_t int32 = INT32_C(-2147483647); |
|---|
| 173 | boost::int_least32_t int_least32 = INT32_C(-2147483647); |
|---|
| 174 | boost::int_fast32_t int_fast32 = INT32_C(-2147483647); |
|---|
| 175 | |
|---|
| 176 | boost::uint32_t uint32 = UINT32_C(4294967295); |
|---|
| 177 | boost::uint_least32_t uint_least32 = UINT32_C(4294967295); |
|---|
| 178 | boost::uint_fast32_t uint_fast32 = UINT32_C(4294967295); |
|---|
| 179 | |
|---|
| 180 | #ifndef BOOST_NO_INT64_T |
|---|
| 181 | boost::int64_t int64 = INT64_C(-9223372036854775807); |
|---|
| 182 | boost::int_least64_t int_least64 = INT64_C(-9223372036854775807); |
|---|
| 183 | boost::int_fast64_t int_fast64 = INT64_C(-9223372036854775807); |
|---|
| 184 | |
|---|
| 185 | boost::uint64_t uint64 = UINT64_C(18446744073709551615); |
|---|
| 186 | boost::uint_least64_t uint_least64 = UINT64_C(18446744073709551615); |
|---|
| 187 | boost::uint_fast64_t uint_fast64 = UINT64_C(18446744073709551615); |
|---|
| 188 | |
|---|
| 189 | boost::intmax_t intmax = INTMAX_C(-9223372036854775807); |
|---|
| 190 | boost::uintmax_t uintmax = UINTMAX_C(18446744073709551615); |
|---|
| 191 | #else |
|---|
| 192 | boost::intmax_t intmax = INTMAX_C(-2147483647); |
|---|
| 193 | boost::uintmax_t uintmax = UINTMAX_C(4294967295); |
|---|
| 194 | #endif |
|---|
| 195 | |
|---|
| 196 | assert( int8 == -127 ); |
|---|
| 197 | assert( int_least8 == -127 ); |
|---|
| 198 | assert( int_fast8 == -127 ); |
|---|
| 199 | assert( uint8 == 255u ); |
|---|
| 200 | assert( uint_least8 == 255u ); |
|---|
| 201 | assert( uint_fast8 == 255u ); |
|---|
| 202 | assert( int16 == -32767 ); |
|---|
| 203 | assert( int_least16 == -32767 ); |
|---|
| 204 | assert( int_fast16 == -32767 ); |
|---|
| 205 | assert( uint16 == 65535u ); |
|---|
| 206 | assert( uint_least16 == 65535u ); |
|---|
| 207 | assert( uint_fast16 == 65535u ); |
|---|
| 208 | assert( int32 == -2147483647 ); |
|---|
| 209 | assert( int_least32 == -2147483647 ); |
|---|
| 210 | assert( int_fast32 == -2147483647 ); |
|---|
| 211 | assert( uint32 == 4294967295u ); |
|---|
| 212 | assert( uint_least32 == 4294967295u ); |
|---|
| 213 | assert( uint_fast32 == 4294967295u ); |
|---|
| 214 | |
|---|
| 215 | #ifndef BOOST_NO_INT64_T |
|---|
| 216 | assert( int64 == INT64_C(-9223372036854775807) ); |
|---|
| 217 | assert( int_least64 == INT64_C(-9223372036854775807) ); |
|---|
| 218 | assert( int_fast64 == INT64_C(-9223372036854775807) ); |
|---|
| 219 | assert( uint64 == UINT64_C(18446744073709551615) ); |
|---|
| 220 | assert( uint_least64 == UINT64_C(18446744073709551615) ); |
|---|
| 221 | assert( uint_fast64 == UINT64_C(18446744073709551615) ); |
|---|
| 222 | assert( intmax == INT64_C(-9223372036854775807) ); |
|---|
| 223 | assert( uintmax == UINT64_C(18446744073709551615) ); |
|---|
| 224 | #else |
|---|
| 225 | assert( intmax == -2147483647 ); |
|---|
| 226 | assert( uintmax == 4294967295u ); |
|---|
| 227 | #endif |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | std::cout << "OK\n"; |
|---|
| 231 | return 0; |
|---|
| 232 | } |
|---|
| 233 | #endif |
|---|