hash_std_smart_ptr_test.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2012 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. #ifdef BOOST_HASH_TEST_STD_INCLUDES
  6. # include <functional>
  7. #else
  8. # include <boost/container_hash/hash.hpp>
  9. #endif
  10. #include <boost/core/lightweight_test.hpp>
  11. #include "./compile_time.hpp"
  12. #if defined(BOOST_HASH_TEST_EXTENSIONS) && !defined(BOOST_NO_CXX11_SMART_PTR)
  13. #define TEST_SMART_PTRS
  14. #include <memory>
  15. #endif
  16. #ifdef TEST_SMART_PTRS
  17. void shared_ptr_tests()
  18. {
  19. std::shared_ptr<int> x;
  20. compile_time_tests(&x);
  21. BOOST_HASH_TEST_NAMESPACE::hash<std::shared_ptr<int> > x1;
  22. BOOST_HASH_TEST_NAMESPACE::hash<std::shared_ptr<int> > x2;
  23. std::shared_ptr<int> ptr1(new int(10));
  24. std::shared_ptr<int> ptr2;
  25. BOOST_TEST(x1(x) == x2(ptr2));
  26. BOOST_TEST(x1(x) != x2(ptr1));
  27. ptr2.reset(new int(10));
  28. BOOST_TEST(x1(ptr1) == x2(ptr1));
  29. BOOST_TEST(x1(ptr1) != x2(ptr2));
  30. ptr2 = ptr1;
  31. BOOST_TEST(x1(ptr1) == x2(ptr2));
  32. #if defined(BOOST_HASH_TEST_EXTENSIONS)
  33. BOOST_TEST(x1(x) == BOOST_HASH_TEST_NAMESPACE::hash_value(x));
  34. BOOST_TEST(x1(ptr1) == BOOST_HASH_TEST_NAMESPACE::hash_value(ptr2));
  35. #endif
  36. }
  37. void unique_ptr_tests()
  38. {
  39. std::unique_ptr<int> x;
  40. compile_time_tests(&x);
  41. BOOST_HASH_TEST_NAMESPACE::hash<std::unique_ptr<int> > x1;
  42. BOOST_HASH_TEST_NAMESPACE::hash<std::unique_ptr<int> > x2;
  43. std::unique_ptr<int> ptr1(new int(10));
  44. std::unique_ptr<int> ptr2;
  45. BOOST_TEST(x1(x) == x2(ptr2));
  46. BOOST_TEST(x1(x) != x2(ptr1));
  47. ptr2.reset(new int(10));
  48. BOOST_TEST(x1(ptr1) == x2(ptr1));
  49. BOOST_TEST(x1(ptr1) != x2(ptr2));
  50. #if defined(BOOST_HASH_TEST_EXTENSIONS)
  51. BOOST_TEST(x1(x) == BOOST_HASH_TEST_NAMESPACE::hash_value(x));
  52. #endif
  53. }
  54. #endif
  55. int main()
  56. {
  57. #ifdef TEST_SMART_PTRS
  58. shared_ptr_tests();
  59. unique_ptr_tests();
  60. #endif
  61. return boost::report_errors();
  62. }