• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 template <class T>
28 void
test()29 test()
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     H h;
35 
36     for (int i = 0; i <= 5; ++i)
37     {
38         T t(static_cast<T>(i));
39         const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
40         if (small)
41         {
42             const std::size_t result = h(t);
43             LIBCPP_ASSERT(result == static_cast<size_t>(t));
44             ((void)result); // Prevent unused warning
45         }
46     }
47 }
48 
main()49 int main()
50 {
51     test<bool>();
52     test<char>();
53     test<signed char>();
54     test<unsigned char>();
55     test<char16_t>();
56     test<char32_t>();
57     test<wchar_t>();
58     test<short>();
59     test<unsigned short>();
60     test<int>();
61     test<unsigned int>();
62     test<long>();
63     test<unsigned long>();
64     test<long long>();
65     test<unsigned long long>();
66 
67 //	LWG #2119
68     test<std::ptrdiff_t>();
69     test<size_t>();
70 
71 	test<int8_t>();
72 	test<int16_t>();
73 	test<int32_t>();
74 	test<int64_t>();
75 
76 	test<int_fast8_t>();
77 	test<int_fast16_t>();
78 	test<int_fast32_t>();
79 	test<int_fast64_t>();
80 
81 	test<int_least8_t>();
82 	test<int_least16_t>();
83 	test<int_least32_t>();
84 	test<int_least64_t>();
85 
86     test<intmax_t>();
87     test<intptr_t>();
88 
89 	test<uint8_t>();
90 	test<uint16_t>();
91 	test<uint32_t>();
92 	test<uint64_t>();
93 
94 	test<uint_fast8_t>();
95 	test<uint_fast16_t>();
96 	test<uint_fast32_t>();
97 	test<uint_fast64_t>();
98 
99 	test<uint_least8_t>();
100 	test<uint_least16_t>();
101 	test<uint_least32_t>();
102 	test<uint_least64_t>();
103 
104     test<uintmax_t>();
105     test<uintptr_t>();
106 
107 #ifndef _LIBCPP_HAS_NO_INT128
108     test<__int128_t>();
109     test<__uint128_t>();
110 #endif
111 }
112