• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011-2021 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 
7 #include "boost/core/lightweight_test.hpp"
8 
9 #include "boost/config.hpp"
10 #include "boost/variant.hpp"
11 #include "boost/functional/hash/hash.hpp"
12 
13 #if !defined(BOOST_NO_CXX11_HDR_UNORDERED_SET) && !defined(BOOST_VARIANT_DO_NOT_SPECIALIZE_STD_HASH)
14 
15 #include <unordered_set>
16 
test_std_hash()17 void test_std_hash() {
18     std::unordered_set<boost::variant<int, bool> > us;
19     us.insert(1);
20     us.insert(true);
21     BOOST_TEST(us.size() == 2);
22 }
23 
24 #else
test_std_hash()25 void test_std_hash() {}
26 #endif
27 
28 
run()29 void run() {
30     typedef boost::variant<bool, int, unsigned int, char> variant_type;
31     boost::hash<variant_type> hasher;
32 
33     variant_type bool_variant1 = true;
34     variant_type bool_variant2 = false;
35     variant_type int_variant = 1;
36     variant_type char_variant1 = '\1';
37     variant_type char_variant2 = '\2';
38     variant_type uint_variant = static_cast<unsigned int>(1);
39 
40     BOOST_TEST(hasher(bool_variant1) != hasher(bool_variant2));
41     BOOST_TEST(hasher(bool_variant1) == hasher(bool_variant1));
42     BOOST_TEST(hasher(int_variant) != hasher(uint_variant));
43     BOOST_TEST(hasher(char_variant1) != hasher(uint_variant));
44     BOOST_TEST(hasher(char_variant1) != hasher(char_variant2));
45     BOOST_TEST(hasher(char_variant1) == hasher(char_variant1));
46     BOOST_TEST(hasher(char_variant2) == hasher(char_variant2));
47 }
48 
main()49 int main() {
50     run();
51     test_std_hash();
52     return boost::report_errors();
53 }
54 
55