integer_traits_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* boost integer_traits.hpp tests
  2. *
  3. * Copyright Jens Maurer 2000
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * $Id$
  9. *
  10. * Revision history
  11. * 2000-02-22 Small improvements by Beman Dawes
  12. * 2000-06-27 Rework for better MSVC and BCC co-operation
  13. */
  14. #include <iostream>
  15. #include <boost/integer_traits.hpp>
  16. // use int64_t instead of long long for better portability
  17. #include <boost/cstdint.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. /*
  20. * General portability note:
  21. * MSVC mis-compiles explicit function template instantiations.
  22. * For example, f<A>() and f<B>() are both compiled to call f<A>().
  23. * BCC is unable to implicitly convert a "const char *" to a std::string
  24. * when using explicit function template instantiations.
  25. *
  26. * Therefore, avoid explicit function template instantiations.
  27. */
  28. #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
  29. template<typename T> inline T make_char_numeric_for_streaming(T x) { return x; }
  30. namespace fix{
  31. inline int make_char_numeric_for_streaming(char c) { return c; }
  32. inline int make_char_numeric_for_streaming(signed char c) { return c; }
  33. inline int make_char_numeric_for_streaming(unsigned char c) { return c; }
  34. }
  35. using namespace fix;
  36. #else
  37. template<typename T> inline T make_char_numeric_for_streaming(T x) { return x; }
  38. inline int make_char_numeric_for_streaming(char c) { return c; }
  39. inline int make_char_numeric_for_streaming(signed char c) { return c; }
  40. inline int make_char_numeric_for_streaming(unsigned char c) { return c; }
  41. #endif
  42. template<class T>
  43. void runtest(const char * type, T)
  44. {
  45. typedef boost::integer_traits<T> traits;
  46. std::cout << "Checking " << type
  47. << "; min is " << make_char_numeric_for_streaming((traits::min)())
  48. << ", max is " << make_char_numeric_for_streaming((traits::max)())
  49. << std::endl;
  50. BOOST_TEST(traits::is_specialized);
  51. #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1200)
  52. // MSVC++ 6.0 issues a LNK1179 error (duplicate comdat) when the compiler
  53. // generates different symbol names with a very long common prefix:
  54. // the dummy "&& true" disambiguates between the symbols generated by this
  55. // BOOST_TEST instantiation and the preceding one.
  56. BOOST_TEST(traits::is_integer && true);
  57. #else
  58. BOOST_TEST(traits::is_integer);
  59. #endif
  60. BOOST_TEST(traits::is_integral == true);
  61. BOOST_TEST(traits::const_min == (traits::min)());
  62. BOOST_TEST(traits::const_max == (traits::max)());
  63. }
  64. int main(int, char*[])
  65. {
  66. runtest("bool", bool());
  67. runtest("char", char());
  68. typedef signed char signed_char;
  69. runtest("signed char", signed_char());
  70. typedef unsigned char unsigned_char;
  71. runtest("unsigned char", unsigned_char());
  72. runtest("wchar_t", wchar_t());
  73. runtest("short", short());
  74. typedef unsigned short unsigned_short;
  75. runtest("unsigned short", unsigned_short());
  76. runtest("int", int());
  77. typedef unsigned int unsigned_int;
  78. runtest("unsigned int", unsigned_int());
  79. runtest("long", long());
  80. typedef unsigned long unsigned_long;
  81. runtest("unsigned long", unsigned_long());
  82. #ifndef BOOST_NO_INTEGRAL_INT64_T
  83. //
  84. // MS/Borland compilers can't support 64-bit member constants
  85. // BeOS doesn't have specialisations for long long in SGI's <limits> header.
  86. runtest("int64_t (possibly long long)", boost::int64_t());
  87. runtest("uint64_t (possibly unsigned long long)", boost::uint64_t());
  88. #else
  89. std::cout << "Skipped int64_t and uint64_t" << std::endl;
  90. #endif
  91. // Some compilers don't pay attention to std:3.6.1/5 and issue a
  92. // warning here if "return 0;" is omitted.
  93. return boost::report_errors();
  94. }