Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/functional/hash/test/hash_custom_test.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.1 KB
Line 
1
2//  Copyright Daniel James 2005-2006. Use, modification, and distribution are
3//  subject to 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 <boost/config.hpp>
7#include <cstddef>
8
9namespace test
10{
11    struct custom
12    {
13        int value_;
14
15        std::size_t hash() const
16        {
17            return value_ * 10;
18        }
19
20#if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
21        friend std::size_t hash_value(custom const& x )
22        {
23            return x.hash();
24        }
25#endif
26
27        custom(int x) : value_(x) {}
28    };
29}
30
31#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
32namespace boost
33{
34    std::size_t hash_value(test::custom x)
35    {
36        return x.hash();
37    }
38}
39#endif
40
41#include "./config.hpp"
42
43#ifdef TEST_EXTENSIONS
44#  ifdef TEST_STD_INCLUDES
45#    include <functional>
46#  else
47#    include <boost/functional/hash.hpp>
48#  endif
49#endif
50
51#include <boost/detail/lightweight_test.hpp>
52
53#ifdef TEST_EXTENSIONS
54
55#include <vector>
56#include <string>
57#include <cctype>
58
59void custom_tests()
60{
61    HASH_NAMESPACE::hash<test::custom> custom_hasher;
62    BOOST_TEST(custom_hasher(10) == 100u);
63    test::custom x(55);
64    BOOST_TEST(custom_hasher(x) == 550u);
65
66    {
67        using namespace HASH_NAMESPACE;
68        BOOST_TEST(custom_hasher(x) == hash_value(x));
69    }
70
71    std::vector<test::custom> custom_vector;
72    custom_vector.push_back(5);
73    custom_vector.push_back(25);
74    custom_vector.push_back(35);
75
76    std::size_t seed = 0;
77    HASH_NAMESPACE::hash_combine(seed, test::custom(5));
78    HASH_NAMESPACE::hash_combine(seed, test::custom(25));
79    HASH_NAMESPACE::hash_combine(seed, test::custom(35));
80
81    std::size_t seed2 = 0;
82    HASH_NAMESPACE::hash_combine(seed2, 50u);
83    HASH_NAMESPACE::hash_combine(seed2, 250u);
84    HASH_NAMESPACE::hash_combine(seed2, 350u);
85
86    BOOST_TEST(seed ==
87            HASH_NAMESPACE::hash_range(custom_vector.begin(), custom_vector.end()));
88    BOOST_TEST(seed == seed2);
89}
90
91#endif // TEST_EXTENSIONS
92
93int main()
94{
95#ifdef TEST_EXTENSIONS
96    custom_tests();
97#endif
98    return boost::report_errors();
99}
Note: See TracBrowser for help on using the repository browser.