• 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 // This test demonstrates an ADL bug in Borland 5.5 where ADL isn't performed
7 // in the global namespace.
8 
9 #include "./config.hpp"
10 
11 #include <boost/config.hpp>
12 #include <cstddef>
13 
14 struct custom
15 {
16     int value_;
17 
hashcustom18     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 
customcustom30     custom(int x) : value_(x) {}
31 };
32 
33 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
34 namespace boost
35 {
hash_value(custom x)36     std::size_t hash_value(custom x)
37     {
38         return x.hash();
39     }
40 }
41 #endif
42 
43 #include "./config.hpp"
44 
45 #ifdef BOOST_HASH_TEST_EXTENSIONS
46 #  ifdef BOOST_HASH_TEST_STD_INCLUDES
47 #    include <functional>
48 #  else
49 #    include <boost/container_hash/hash.hpp>
50 #  endif
51 #endif
52 
53 #include <boost/core/lightweight_test.hpp>
54 
55 #ifdef BOOST_HASH_TEST_EXTENSIONS
56 
57 #include <vector>
58 #include <string>
59 #include <cctype>
60 
custom_tests()61 void custom_tests()
62 {
63     BOOST_HASH_TEST_NAMESPACE::hash<custom> custom_hasher;
64     BOOST_TEST(custom_hasher(10) == 100u);
65     custom x(55);
66     BOOST_TEST(custom_hasher(x) == 550u);
67 
68     {
69         using namespace BOOST_HASH_TEST_NAMESPACE;
70         BOOST_TEST(custom_hasher(x) == hash_value(x));
71     }
72 
73     std::vector<custom> custom_vector;
74     custom_vector.push_back(5);
75     custom_vector.push_back(25);
76     custom_vector.push_back(35);
77 
78     std::size_t seed = 0;
79     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed, custom(5));
80     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed, custom(25));
81     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed, custom(35));
82 
83     std::size_t seed2 = 0;
84     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed2, 50u);
85     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed2, 250u);
86     BOOST_HASH_TEST_NAMESPACE::hash_combine(seed2, 350u);
87 
88     BOOST_TEST(seed == BOOST_HASH_TEST_NAMESPACE::hash_range(
89         custom_vector.begin(), custom_vector.end()));
90     BOOST_TEST(seed == seed2);
91 }
92 
93 #endif // BOOST_HASH_TEST_EXTENSIONS
94 
95 
main()96 int main()
97 {
98 #ifdef BOOST_HASH_TEST_EXTENSIONS
99     custom_tests();
100 #endif
101     return boost::report_errors();
102 }
103 
104