hash_complex_test.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2005-2009 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "./config.hpp"
  5. #if !defined(BOOST_HASH_TEST_EXTENSIONS)
  6. int main() {}
  7. #else
  8. #ifdef BOOST_HASH_TEST_STD_INCLUDES
  9. # include <functional>
  10. #else
  11. # include <boost/container_hash/hash.hpp>
  12. #endif
  13. #include <boost/core/lightweight_test.hpp>
  14. #if defined(BOOST_MSVC)
  15. #pragma warning(disable:4244) // conversion from 'unsigned long' to
  16. // 'unsigned short', possible loss of data
  17. #pragma warning(disable:4245) // conversion from 'int' to
  18. // 'const unsigned short',
  19. // signed/unsigned mismatch
  20. #pragma warning(disable:4305) // truncation from 'double' to
  21. // 'const std::complex<float>::_Ty'
  22. #pragma warning(disable:4309) // truncation of constant value
  23. #pragma warning(disable:4512) // assignment operator could not be generated
  24. #if BOOST_MSVC < 1400
  25. #pragma warning(disable:4267) // conversion from 'size_t' to 'unsigned int',
  26. // possible loss of data
  27. #endif
  28. #endif
  29. #if defined(__GNUC__) && !defined(BOOST_INTEL_CXX_VERSION)
  30. #pragma GCC diagnostic ignored "-Wfloat-equal"
  31. #endif
  32. #include <complex>
  33. #include <sstream>
  34. #include <boost/limits.hpp>
  35. template <class T>
  36. void generic_complex_tests(std::complex<T> v)
  37. {
  38. BOOST_HASH_TEST_NAMESPACE::hash<std::complex<T> > complex_hasher;
  39. BOOST_TEST(complex_hasher(v) == complex_hasher(v));
  40. BOOST_HASH_TEST_NAMESPACE::hash<T> real_hasher;
  41. T real = v.real();
  42. T imag = v.imag();
  43. BOOST_TEST(real_hasher(real) == complex_hasher(std::complex<T>(real)));
  44. if(imag != 0 && real_hasher(real) == complex_hasher(v)) {
  45. std::ostringstream os;
  46. os<<"real_hasher("<<real<<") == complex_hasher("
  47. <<v.real()<<" + "<<v.imag()<<"i) == "
  48. <<real_hasher(real)<<" (This might not be a bug).";
  49. BOOST_ERROR(os.str().c_str());
  50. }
  51. }
  52. template <class Float>
  53. void complex_float_tests(Float*)
  54. {
  55. typedef std::complex<Float> complex;
  56. generic_complex_tests(complex(0,0));
  57. generic_complex_tests(complex(0.5,0));
  58. generic_complex_tests(complex(25,0));
  59. generic_complex_tests(complex(25,0));
  60. generic_complex_tests(complex(static_cast<Float>(-67.5324535),static_cast<Float>(56.23578678)));
  61. }
  62. template <class Integer>
  63. void complex_integral_tests(Integer*)
  64. {
  65. typedef std::complex<Integer> complex;
  66. generic_complex_tests(complex(0,0));
  67. generic_complex_tests(complex(15342,124));
  68. generic_complex_tests(complex(25,54356));
  69. generic_complex_tests(complex(5325,2346));
  70. generic_complex_tests(complex(Integer(-243897),Integer(-49923874)));
  71. generic_complex_tests(complex(Integer(-543),Integer(763)));
  72. }
  73. int main()
  74. {
  75. // I've comments out the short and unsigned short tests
  76. // as they cause warnings and don't really test
  77. // anything that the other tests already deal with.
  78. complex_float_tests((float*) 0);
  79. complex_float_tests((double*) 0);
  80. complex_float_tests((long double*) 0);
  81. //complex_integral_tests((short*) 0);
  82. complex_integral_tests((int*) 0);
  83. complex_integral_tests((long*) 0);
  84. //complex_integral_tests((unsigned short*) 0);
  85. complex_integral_tests((unsigned int*) 0);
  86. complex_integral_tests((unsigned long*) 0);
  87. return boost::report_errors();
  88. }
  89. #endif // BOOST_HASH_TEST_EXTENSIONS