• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright 2005-2009 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 #include <boost/config.hpp>
8 #include <cstddef>
9 
10 namespace test
11 {
12     struct custom
13     {
14         int value_;
15 
hashtest::custom16         std::size_t hash() const
17         {
18             return static_cast<std::size_t>(value_ * 10);
19         }
20 
21 #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
hash_value(custom const & x)22         friend std::size_t hash_value(custom const& x )
23         {
24             return x.hash();
25         }
26 #endif
27 
customtest::custom28         custom(int x) : value_(x) {}
29     };
30 }
31 
32 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
33 namespace boost
34 {
hash_value(test::custom x)35     std::size_t hash_value(test::custom x)
36     {
37         return x.hash();
38     }
39 }
40 #endif
41 
42 #include "./config.hpp"
43 
44 #ifdef BOOST_HASH_TEST_EXTENSIONS
45 #  ifdef BOOST_HASH_TEST_STD_INCLUDES
46 #    include <functional>
47 #  else
48 #    include <boost/container_hash/hash.hpp>
49 #  endif
50 #endif
51 
52 #include <boost/core/lightweight_test.hpp>
53 
54 #ifdef BOOST_HASH_TEST_EXTENSIONS
55 
56 #include <vector>
57 #include <string>
58 #include <cctype>
59 
custom_tests()60 void custom_tests()
61 {
62     BOOST_HASH_TEST_NAMESPACE::hash<test::custom> custom_hasher;
63     BOOST_TEST(custom_hasher(10) == 100u);
64     test::custom x(55);
65     BOOST_TEST(custom_hasher(x) == 550u);
66 
67     {
68         using namespace BOOST_HASH_TEST_NAMESPACE;
69         BOOST_TEST(custom_hasher(x) == hash_value(x));
70     }
71 
72     std::vector<test::custom> custom_vector;
73     custom_vector.push_back(5);
74     custom_vector.push_back(25);
75     custom_vector.push_back(35);
76 
77     std::size_t seed = 0;
78     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed, test::custom(5));
79     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed, test::custom(25));
80     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed, test::custom(35));
81 
82     std::size_t seed2 = 0;
83     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed2, 50u);
84     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed2, 250u);
85     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed2, 350u);
86 
87     BOOST_TEST(seed == BOOST_HASH_TEST_NAMESPACE::hash_range(
88         custom_vector.begin(), custom_vector.end()));
89     BOOST_TEST(seed == seed2);
90 }
91 
92 #endif // BOOST_HASH_TEST_EXTENSIONS
93 
main()94 int main()
95 {
96 #ifdef BOOST_HASH_TEST_EXTENSIONS
97     custom_tests();
98 #endif
99     return boost::report_errors();
100 }
101