1 2 // Copyright 2018 Daniel James 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #include "./config.hpp" 7 8 #ifndef BOOST_HASH_TEST_STD_INCLUDES 9 # include <boost/container_hash/hash.hpp> 10 #endif 11 #include <boost/config.hpp> 12 #include <boost/core/lightweight_test.hpp> 13 14 #if BOOST_HASH_HAS_VARIANT 15 16 #include <variant> 17 #include <string> 18 test_monostate()19void test_monostate() 20 { 21 std::monostate x1; 22 std::monostate x2; 23 24 boost::hash<std::monostate> hasher; 25 26 BOOST_TEST(hasher(x1) == hasher(x2)); 27 } 28 test_variant_int()29void test_variant_int() 30 { 31 std::variant<std::monostate, int> x1a; 32 std::variant<std::monostate, int> x1b; 33 std::variant<std::monostate, int> x2a(10); 34 std::variant<std::monostate, int> x2b(x2a); 35 std::variant<std::monostate, int> x3(20); 36 37 boost::hash<std::variant<std::monostate, int> > hasher; 38 39 BOOST_TEST(hasher(x1a) == hasher(x1a)); 40 BOOST_TEST(hasher(x1a) == hasher(x1b)); 41 BOOST_TEST(hasher(x1a) != hasher(x2a)); 42 BOOST_TEST(hasher(x1a) != hasher(x3)); 43 BOOST_TEST(hasher(x2a) == hasher(x2a)); 44 BOOST_TEST(hasher(x2b) == hasher(x2b)); 45 BOOST_TEST(hasher(x2a) != hasher(x3)); 46 BOOST_TEST(hasher(x3) == hasher(x3)); 47 } 48 49 struct custom1 { 50 int value; hash_value(custom1 v)51 friend std::size_t hash_value(custom1 v) { return boost::hash_value(v.value); } 52 }; 53 54 struct custom2 { 55 int value; hash_value(custom2 v)56 friend std::size_t hash_value(custom2 v) { return boost::hash_value(v.value); } 57 }; 58 test_variant_unique_types()59void test_variant_unique_types() 60 { 61 custom1 x11 = { 0 }; 62 custom1 x12 = { 1 }; 63 custom2 x21 = { 0 }; 64 custom2 x22 = { 1 }; 65 66 boost::hash<custom1> hasher1; 67 boost::hash<custom2> hasher2; 68 69 BOOST_TEST(hasher1(x11) == hasher2(x21)); 70 BOOST_TEST(hasher1(x11) != hasher2(x22)); 71 BOOST_TEST(hasher1(x12) != hasher2(x21)); 72 BOOST_TEST(hasher1(x12) == hasher2(x22)); 73 74 typedef std::variant<custom1, custom2> variant_type; 75 76 variant_type y11(x11); 77 variant_type y12(x12); 78 variant_type y21(x21); 79 variant_type y22(x22); 80 81 boost::hash<variant_type> hasher; 82 83 BOOST_TEST(hasher(y11) != hasher(y21)); 84 BOOST_TEST(hasher(y11) != hasher(y22)); 85 BOOST_TEST(hasher(y12) != hasher(y21)); 86 BOOST_TEST(hasher(y12) != hasher(y22)); 87 } 88 89 #endif 90 main()91int main() 92 { 93 #if BOOST_HASH_HAS_VARIANT 94 test_variant_int(); 95 test_variant_unique_types(); 96 #else 97 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "<variant> not available." << std::endl; 98 #endif 99 return boost::report_errors(); 100 } 101