9d8457030749c99d0fecfefbb0cb744708adce34
[platform/upstream/boost.git] / libs / functional / hash / test / hash_friend_test.cpp
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
18         std::size_t hash() const
19         {
20             return value_ * 10;
21         }
22
23 #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
24         friend std::size_t hash_value(custom const& x)
25         {
26             return x.hash();
27         }
28 #endif
29
30         custom(int x) : value_(x) {}
31     };
32 }
33
34 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
35 namespace boost
36 {
37     template <class T>
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 TEST_EXTENSIONS
48 #  ifdef TEST_STD_INCLUDES
49 #    include <functional>
50 #  else
51 #    include <boost/functional/hash.hpp>
52 #  endif
53 #endif
54
55 #include <boost/detail/lightweight_test.hpp>
56
57 #ifdef TEST_EXTENSIONS
58
59 #include <vector>
60 #include <string>
61 #include <cctype>
62
63 void custom_tests()
64 {
65     HASH_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 HASH_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     HASH_NAMESPACE::hash_combine(seed, test::custom<int>(5));
82     HASH_NAMESPACE::hash_combine(seed, test::custom<int>(25));
83     HASH_NAMESPACE::hash_combine(seed, test::custom<int>(35));
84
85     std::size_t seed2 = 0;
86     HASH_NAMESPACE::hash_combine(seed2, 50u);
87     HASH_NAMESPACE::hash_combine(seed2, 250u);
88     HASH_NAMESPACE::hash_combine(seed2, 350u);
89
90     BOOST_TEST(seed == HASH_NAMESPACE::hash_range(
91         custom_vector.begin(), custom_vector.end()));
92     BOOST_TEST(seed == seed2);
93 }
94
95 #endif // TEST_EXTENSIONS
96
97 int main()
98 {
99 #ifdef TEST_EXTENSIONS
100     custom_tests();
101 #endif
102     return boost::report_errors();
103 }