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