• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright 2020 Peter Dimov.
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 
6 #if defined(_MSC_VER)
7 # pragma warning( disable: 4244 ) // conversion from int to float, possible loss of data
8 #endif
9 
10 #include <boost/variant2/variant.hpp>
11 #include <boost/core/lightweight_test.hpp>
12 #include <boost/core/lightweight_test_trait.hpp>
13 #include <boost/container_hash/hash.hpp>
14 #include <boost/config/workaround.hpp>
15 
16 using namespace boost::variant2;
17 
test()18 template<template<class...> class Hash, class T1, class T2, class T3> void test()
19 {
20     variant<T1, T2, T3> v1( in_place_index_t<0>{} );
21     std::size_t h1 = Hash<decltype(v1)>()( v1 );
22 
23     variant<T1, T2, T3> v2( in_place_index_t<1>{} );
24     std::size_t h2 = Hash<decltype(v2)>()( v2 );
25 
26     variant<T1, T2, T3> v3( in_place_index_t<2>{} );
27     std::size_t h3 = Hash<decltype(v3)>()( v3 );
28 
29     BOOST_TEST_NE( h1, h2 );
30     BOOST_TEST_NE( h1, h3 );
31     BOOST_TEST_NE( h2, h3 );
32 }
33 
test2()34 template<template<class...> class Hash, class T> void test2()
35 {
36     variant<T> v1( 0 );
37     std::size_t h1 = Hash<decltype(v1)>()( v1 );
38 
39     variant<T> v2( 1 );
40     std::size_t h2 = Hash<decltype(v2)>()( v2 );
41 
42     variant<T> v3( 2 );
43     std::size_t h3 = Hash<decltype(v3)>()( v3 );
44 
45     BOOST_TEST_NE( h1, h2 );
46     BOOST_TEST_NE( h1, h3 );
47     BOOST_TEST_NE( h2, h3 );
48 }
49 
50 struct X {};
51 
main()52 int main()
53 {
54     test<std::hash, monostate, monostate, monostate>();
55     test<std::hash, int, int, float>();
56 
57     test<boost::hash, monostate, monostate, monostate>();
58     test<boost::hash, int, int, float>();
59 
60     test2<std::hash, int>();
61     test2<std::hash, float>();
62 
63     test2<boost::hash, int>();
64     test2<boost::hash, float>();
65 
66 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1910) && ( !defined(_LIBCPP_STD_VER) || _LIBCPP_STD_VER > 11 )
67 
68     BOOST_TEST_TRAIT_FALSE(( detail::is_hash_enabled<X> ));
69 
70 #endif
71 
72     return boost::report_errors();
73 }
74