File: hash_global_namespace_test.cpp

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (103 lines) | stat: -rw-r--r-- 2,138 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

// Copyright 2006-2009 Daniel James.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// This test demonstrates an ADL bug in Borland 5.5 where ADL isn't performed
// in the global namespace.

#include "./config.hpp"

#include <boost/config.hpp>
#include <cstddef>

struct custom
{
    int value_;

    std::size_t hash() const
    {
        return value_ * 10;
    }

#if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
    friend std::size_t hash_value(custom const& x )
    {
        return x.hash();
    }
#endif

    custom(int x) : value_(x) {}
};

#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
namespace boost
{
    std::size_t hash_value(custom x)
    {
        return x.hash();
    }
}
#endif

#include "./config.hpp"

#ifdef TEST_EXTENSIONS
#  ifdef TEST_STD_INCLUDES
#    include <functional>
#  else
#    include <boost/functional/hash.hpp>
#  endif
#endif

#include <boost/detail/lightweight_test.hpp>

#ifdef TEST_EXTENSIONS

#include <vector>
#include <string>
#include <cctype>

void custom_tests()
{
    HASH_NAMESPACE::hash<custom> custom_hasher;
    BOOST_TEST(custom_hasher(10) == 100u);
    custom x(55);
    BOOST_TEST(custom_hasher(x) == 550u);

    {
        using namespace HASH_NAMESPACE;
        BOOST_TEST(custom_hasher(x) == hash_value(x));
    }

    std::vector<custom> custom_vector;
    custom_vector.push_back(5);
    custom_vector.push_back(25);
    custom_vector.push_back(35);

    std::size_t seed = 0;
    HASH_NAMESPACE::hash_combine(seed, custom(5));
    HASH_NAMESPACE::hash_combine(seed, custom(25));
    HASH_NAMESPACE::hash_combine(seed, custom(35));

    std::size_t seed2 = 0;
    HASH_NAMESPACE::hash_combine(seed2, 50u);
    HASH_NAMESPACE::hash_combine(seed2, 250u);
    HASH_NAMESPACE::hash_combine(seed2, 350u);

    BOOST_TEST(seed == HASH_NAMESPACE::hash_range(
        custom_vector.begin(), custom_vector.end()));
    BOOST_TEST(seed == seed2);
}

#endif // TEST_EXTENSIONS


int main()
{
#ifdef TEST_EXTENSIONS
    custom_tests();
#endif
    return boost::report_errors();
}