1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <functional>
11
12// template <class T>
13// struct hash
14//     : public unary_function<T, size_t>
15// {
16//     size_t operator()(T val) const;
17// };
18
19#include <functional>
20#include <cassert>
21#include <type_traits>
22#include <cstddef>
23#include <limits>
24
25#include "test_macros.h"
26
27template <class T>
28void
29test()
30{
31    typedef std::hash<T> H;
32    static_assert((std::is_same<typename H::argument_type, T>::value), "" );
33    static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
34    ASSERT_NOEXCEPT(H()(T()));
35    H h;
36
37    for (int i = 0; i <= 5; ++i)
38    {
39        T t(static_cast<T>(i));
40        const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
41        if (small)
42        {
43            const std::size_t result = h(t);
44            LIBCPP_ASSERT(result == static_cast<size_t>(t));
45            ((void)result); // Prevent unused warning
46        }
47    }
48}
49
50int main()
51{
52    test<bool>();
53    test<char>();
54    test<signed char>();
55    test<unsigned char>();
56    test<char16_t>();
57    test<char32_t>();
58    test<wchar_t>();
59    test<short>();
60    test<unsigned short>();
61    test<int>();
62    test<unsigned int>();
63    test<long>();
64    test<unsigned long>();
65    test<long long>();
66    test<unsigned long long>();
67
68//  LWG #2119
69    test<std::ptrdiff_t>();
70    test<size_t>();
71
72    test<int8_t>();
73    test<int16_t>();
74    test<int32_t>();
75    test<int64_t>();
76
77    test<int_fast8_t>();
78    test<int_fast16_t>();
79    test<int_fast32_t>();
80    test<int_fast64_t>();
81
82    test<int_least8_t>();
83    test<int_least16_t>();
84    test<int_least32_t>();
85    test<int_least64_t>();
86
87    test<intmax_t>();
88    test<intptr_t>();
89
90    test<uint8_t>();
91    test<uint16_t>();
92    test<uint32_t>();
93    test<uint64_t>();
94
95    test<uint_fast8_t>();
96    test<uint_fast16_t>();
97    test<uint_fast32_t>();
98    test<uint_fast64_t>();
99
100    test<uint_least8_t>();
101    test<uint_least16_t>();
102    test<uint_least32_t>();
103    test<uint_least64_t>();
104
105    test<uintmax_t>();
106    test<uintptr_t>();
107
108#ifndef _LIBCPP_HAS_NO_INT128
109    test<__int128_t>();
110    test<__uint128_t>();
111#endif
112}
113