cstdint_test.cpp 8.6 KB

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