hash_variant_test.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2011-2019 Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include "boost/core/lightweight_test.hpp"
  7. #include "boost/config.hpp"
  8. #include "boost/variant.hpp"
  9. #include "boost/functional/hash/hash.hpp"
  10. #if !defined(BOOST_NO_CXX11_HDR_UNORDERED_SET) && !defined(BOOST_VARIANT_DO_NOT_SPECIALIZE_STD_HASH)
  11. #include <unordered_set>
  12. void test_std_hash() {
  13. std::unordered_set<boost::variant<int, bool> > us;
  14. us.insert(1);
  15. us.insert(true);
  16. BOOST_TEST(us.size() == 2);
  17. }
  18. #else
  19. void test_std_hash() {}
  20. #endif
  21. void run() {
  22. typedef boost::variant<bool, int, unsigned int, char> variant_type;
  23. boost::hash<variant_type> hasher;
  24. variant_type bool_variant1 = true;
  25. variant_type bool_variant2 = false;
  26. variant_type int_variant = 1;
  27. variant_type char_variant1 = '\1';
  28. variant_type char_variant2 = '\2';
  29. variant_type uint_variant = static_cast<unsigned int>(1);
  30. BOOST_TEST(hasher(bool_variant1) != hasher(bool_variant2));
  31. BOOST_TEST(hasher(bool_variant1) == hasher(bool_variant1));
  32. BOOST_TEST(hasher(int_variant) != hasher(uint_variant));
  33. BOOST_TEST(hasher(char_variant1) != hasher(uint_variant));
  34. BOOST_TEST(hasher(char_variant1) != hasher(char_variant2));
  35. BOOST_TEST(hasher(char_variant1) == hasher(char_variant1));
  36. BOOST_TEST(hasher(char_variant2) == hasher(char_variant2));
  37. }
  38. int main() {
  39. run();
  40. test_std_hash();
  41. return boost::report_errors();
  42. }