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