hash_optional_test.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2018 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. #ifndef BOOST_HASH_TEST_STD_INCLUDES
  6. # include <boost/container_hash/hash.hpp>
  7. #endif
  8. #include <boost/config.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #if BOOST_HASH_HAS_OPTIONAL
  11. #include <optional>
  12. #include <string>
  13. void test_optional_int()
  14. {
  15. std::optional<int> x1a;
  16. std::optional<int> x1b;
  17. std::optional<int> x2a(10);
  18. std::optional<int> x2b(x2a);
  19. std::optional<int> x3(20);
  20. boost::hash<std::optional<int> > hasher;
  21. BOOST_TEST(hasher(x1a) == hasher(x1a));
  22. BOOST_TEST(hasher(x1a) == hasher(x1b));
  23. BOOST_TEST(hasher(x1a) != hasher(x2a));
  24. BOOST_TEST(hasher(x1a) != hasher(x3));
  25. BOOST_TEST(hasher(x2a) == hasher(x2a));
  26. BOOST_TEST(hasher(x2b) == hasher(x2b));
  27. BOOST_TEST(hasher(x2a) != hasher(x3));
  28. BOOST_TEST(hasher(x3) == hasher(x3));
  29. }
  30. void test_optional_string()
  31. {
  32. std::optional<std::string> x1a;
  33. std::optional<std::string> x1b;
  34. std::optional<std::string> x2a("10");
  35. std::optional<std::string> x2b(x2a);
  36. std::optional<std::string> x3("20");
  37. boost::hash<std::optional<std::string> > hasher;
  38. BOOST_TEST(hasher(x1a) == hasher(x1a));
  39. BOOST_TEST(hasher(x1a) == hasher(x1b));
  40. BOOST_TEST(hasher(x1a) != hasher(x2a));
  41. BOOST_TEST(hasher(x1a) != hasher(x3));
  42. BOOST_TEST(hasher(x2a) == hasher(x2a));
  43. BOOST_TEST(hasher(x2b) == hasher(x2b));
  44. BOOST_TEST(hasher(x2a) != hasher(x3));
  45. BOOST_TEST(hasher(x3) == hasher(x3));
  46. }
  47. #endif
  48. int main()
  49. {
  50. #if BOOST_HASH_HAS_OPTIONAL
  51. test_optional_int();
  52. test_optional_string();
  53. #else
  54. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "<optional> not available." << std::endl;
  55. #endif
  56. return boost::report_errors();
  57. }