cstdint_test2.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // boost cstdint.hpp test program ------------------------------------------//
  2. // Copyright Beman Dawes 2000. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/integer for documentation.
  6. // Revision History
  7. // 11 Sep 01 Adapted to work with macros defined in native stdint.h (John Maddock)
  8. // 12 Nov 00 Adapted to merged <boost/cstdint.hpp>
  9. // 23 Sep 00 Added INTXX_C constant macro support + int64_t support (John Maddock).
  10. // 28 Jun 00 Initial version
  11. //
  12. // There are two ways to test this: in version 1, we include cstdint.hpp as the first
  13. // include, which means we get decide whether __STDC_CONSTANT_MACROS is defined.
  14. // In version two we include stdint.h with __STDC_CONSTANT_MACROS *NOT* defined first,
  15. // and check that we still end up with compatible definitions for the INT#_C macros.
  16. //
  17. // This is version 2.
  18. //
  19. #if defined(__GNUC__) && (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4))
  20. // We can't suppress this warning on the command line as not all GCC versions support -Wno-type-limits :
  21. #pragma GCC diagnostic ignored "-Wtype-limits"
  22. #endif
  23. #include <boost/config.hpp>
  24. #ifdef BOOST_HAS_STDINT_H
  25. #ifdef __hpux
  26. # include <inttypes.h>
  27. #else
  28. # include <stdint.h>
  29. #endif
  30. #endif
  31. #include <boost/cstdint.hpp>
  32. #include <boost/detail/lightweight_test.hpp>
  33. #include <iostream>
  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. static const boost::uint8_t uint8 = UINT8_C(255);
  46. static const boost::uint_least8_t uint_least8 = UINT8_C(255);
  47. static const boost::uint_fast8_t uint_fast8 = UINT8_C(255);
  48. static const boost::int16_t int16 = INT16_C(-32767);
  49. static const boost::int_least16_t int_least16 = INT16_C(-32767);
  50. static const boost::int_fast16_t int_fast16 = INT16_C(-32767);
  51. static const boost::uint16_t uint16 = UINT16_C(65535);
  52. static const boost::uint_least16_t uint_least16 = UINT16_C(65535);
  53. static const boost::uint_fast16_t uint_fast16 = UINT16_C(65535);
  54. static const boost::int32_t int32 = INT32_C(-2147483647);
  55. static const boost::int_least32_t int_least32 = INT32_C(-2147483647);
  56. static const boost::int_fast32_t int_fast32 = INT32_C(-2147483647);
  57. static const boost::uint32_t uint32 = UINT32_C(4294967295);
  58. static const boost::uint_least32_t uint_least32 = UINT32_C(4294967295);
  59. static const boost::uint_fast32_t uint_fast32 = UINT32_C(4294967295);
  60. static void check();
  61. };
  62. void integral_constant_checker::check()
  63. {
  64. BOOST_TEST( int8 == -127 );
  65. BOOST_TEST( int_least8 == -127 );
  66. BOOST_TEST( int_fast8 == -127 );
  67. BOOST_TEST( uint8 == 255u );
  68. BOOST_TEST( uint_least8 == 255u );
  69. BOOST_TEST( uint_fast8 == 255u );
  70. BOOST_TEST( int16 == -32767 );
  71. BOOST_TEST( int_least16 == -32767 );
  72. BOOST_TEST( int_fast16 == -32767 );
  73. BOOST_TEST( uint16 == 65535u );
  74. BOOST_TEST( uint_least16 == 65535u );
  75. BOOST_TEST( uint_fast16 == 65535u );
  76. BOOST_TEST( int32 == -2147483647 );
  77. BOOST_TEST( int_least32 == -2147483647 );
  78. BOOST_TEST( int_fast32 == -2147483647 );
  79. BOOST_TEST( uint32 == 4294967295u );
  80. BOOST_TEST( uint_least32 == 4294967295u );
  81. BOOST_TEST( uint_fast32 == 4294967295u );
  82. }
  83. #endif // BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  84. //
  85. // the following function simply verifies that the type
  86. // of an integral constant is correctly defined:
  87. //
  88. #ifdef __BORLANDC__
  89. #pragma option -w-8008
  90. #pragma option -w-8066
  91. #endif
  92. template <class T1, class T2>
  93. void integral_constant_type_check(T1, T2)
  94. {
  95. //
  96. // the types T1 and T2 may not be exactly
  97. // the same type, but they should be the
  98. // same size and signedness. We could use
  99. // numeric_limits to verify this, but
  100. // numeric_limits implementations currently
  101. // vary too much, or are incomplete or missing.
  102. //
  103. T1 t1 = static_cast<T1>(-1); // cast suppresses warnings
  104. T2 t2 = static_cast<T2>(-1); // ditto
  105. #if defined(BOOST_HAS_STDINT_H)
  106. // if we have a native stdint.h
  107. // then the INTXX_C macros may define
  108. // a type that's wider than required:
  109. BOOST_TEST(sizeof(T1) <= sizeof(T2));
  110. #else
  111. BOOST_TEST(sizeof(T1) == sizeof(T2));
  112. BOOST_TEST(t1 == t2);
  113. #endif
  114. #if defined(BOOST_HAS_STDINT_H)
  115. // native headers are permitted to promote small
  116. // unsigned types to type int:
  117. if(sizeof(T1) >= sizeof(int))
  118. {
  119. if(t1 > 0)
  120. BOOST_TEST(t2 > 0);
  121. else
  122. BOOST_TEST(!(t2 > 0));
  123. }
  124. else if(t1 < 0)
  125. BOOST_TEST(!(t2 > 0));
  126. #else
  127. if(t1 > 0)
  128. BOOST_TEST(t2 > 0);
  129. else
  130. BOOST_TEST(!(t2 > 0));
  131. #endif
  132. }
  133. int main(int, char*[])
  134. {
  135. #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  136. integral_constant_checker::check();
  137. #endif
  138. //
  139. // verify the types of the integral constants:
  140. //
  141. integral_constant_type_check(boost::int8_t(0), INT8_C(0));
  142. integral_constant_type_check(boost::uint8_t(0), UINT8_C(0));
  143. integral_constant_type_check(boost::int16_t(0), INT16_C(0));
  144. integral_constant_type_check(boost::uint16_t(0), UINT16_C(0));
  145. integral_constant_type_check(boost::int32_t(0), INT32_C(0));
  146. integral_constant_type_check(boost::uint32_t(0), UINT32_C(0));
  147. #ifndef BOOST_NO_INT64_T
  148. integral_constant_type_check(boost::int64_t(0), INT64_C(0));
  149. integral_constant_type_check(boost::uint64_t(0), UINT64_C(0));
  150. #endif
  151. //
  152. boost::int8_t int8 = INT8_C(-127);
  153. boost::int_least8_t int_least8 = INT8_C(-127);
  154. boost::int_fast8_t int_fast8 = INT8_C(-127);
  155. boost::uint8_t uint8 = UINT8_C(255);
  156. boost::uint_least8_t uint_least8 = UINT8_C(255);
  157. boost::uint_fast8_t uint_fast8 = UINT8_C(255);
  158. boost::int16_t int16 = INT16_C(-32767);
  159. boost::int_least16_t int_least16 = INT16_C(-32767);
  160. boost::int_fast16_t int_fast16 = INT16_C(-32767);
  161. boost::uint16_t uint16 = UINT16_C(65535);
  162. boost::uint_least16_t uint_least16 = UINT16_C(65535);
  163. boost::uint_fast16_t uint_fast16 = UINT16_C(65535);
  164. boost::int32_t int32 = INT32_C(-2147483647);
  165. boost::int_least32_t int_least32 = INT32_C(-2147483647);
  166. boost::int_fast32_t int_fast32 = INT32_C(-2147483647);
  167. boost::uint32_t uint32 = UINT32_C(4294967295);
  168. boost::uint_least32_t uint_least32 = UINT32_C(4294967295);
  169. boost::uint_fast32_t uint_fast32 = UINT32_C(4294967295);
  170. #ifndef BOOST_NO_INT64_T
  171. boost::int64_t int64 = INT64_C(-9223372036854775807);
  172. boost::int_least64_t int_least64 = INT64_C(-9223372036854775807);
  173. boost::int_fast64_t int_fast64 = INT64_C(-9223372036854775807);
  174. boost::uint64_t uint64 = UINT64_C(18446744073709551615);
  175. boost::uint_least64_t uint_least64 = UINT64_C(18446744073709551615);
  176. boost::uint_fast64_t uint_fast64 = UINT64_C(18446744073709551615);
  177. boost::intmax_t intmax = INTMAX_C(-9223372036854775807);
  178. boost::uintmax_t uintmax = UINTMAX_C(18446744073709551615);
  179. #else
  180. boost::intmax_t intmax = INTMAX_C(-2147483647);
  181. boost::uintmax_t uintmax = UINTMAX_C(4294967295);
  182. #endif
  183. BOOST_TEST( int8 == -127 );
  184. BOOST_TEST( int_least8 == -127 );
  185. BOOST_TEST( int_fast8 == -127 );
  186. BOOST_TEST( uint8 == 255u );
  187. BOOST_TEST( uint_least8 == 255u );
  188. BOOST_TEST( uint_fast8 == 255u );
  189. BOOST_TEST( int16 == -32767 );
  190. BOOST_TEST( int_least16 == -32767 );
  191. BOOST_TEST( int_fast16 == -32767 );
  192. BOOST_TEST( uint16 == 65535u );
  193. BOOST_TEST( uint_least16 == 65535u );
  194. BOOST_TEST( uint_fast16 == 65535u );
  195. BOOST_TEST( int32 == -2147483647 );
  196. BOOST_TEST( int_least32 == -2147483647 );
  197. BOOST_TEST( int_fast32 == -2147483647 );
  198. BOOST_TEST( uint32 == 4294967295u );
  199. BOOST_TEST( uint_least32 == 4294967295u );
  200. BOOST_TEST( uint_fast32 == 4294967295u );
  201. #ifndef BOOST_NO_INT64_T
  202. BOOST_TEST( int64 == INT64_C(-9223372036854775807) );
  203. BOOST_TEST( int_least64 == INT64_C(-9223372036854775807) );
  204. BOOST_TEST( int_fast64 == INT64_C(-9223372036854775807) );
  205. BOOST_TEST( uint64 == UINT64_C(18446744073709551615) );
  206. BOOST_TEST( uint_least64 == UINT64_C(18446744073709551615) );
  207. BOOST_TEST( uint_fast64 == UINT64_C(18446744073709551615) );
  208. BOOST_TEST( intmax == INT64_C(-9223372036854775807) );
  209. BOOST_TEST( uintmax == UINT64_C(18446744073709551615) );
  210. #else
  211. BOOST_TEST( intmax == -2147483647 );
  212. BOOST_TEST( uintmax == 4294967295u );
  213. #endif
  214. std::cout << "OK\n";
  215. return boost::report_errors();
  216. }