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
28template <class T>
29void
30test()
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<std::size_t>(t));
48 ((void)result); // Prevent unused warning
49 }
50 }
51}
52
53int 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<std::size_t>();
76
77 test<std::int8_t>();
78 test<std::int16_t>();
79 test<std::int32_t>();
80 test<std::int64_t>();
81
82 test<std::int_fast8_t>();
83 test<std::int_fast16_t>();
84 test<std::int_fast32_t>();
85 test<std::int_fast64_t>();
86
87 test<std::int_least8_t>();
88 test<std::int_least16_t>();
89 test<std::int_least32_t>();
90 test<std::int_least64_t>();
91
92 test<std::intmax_t>();
93 test<std::intptr_t>();
94
95 test<std::uint8_t>();
96 test<std::uint16_t>();
97 test<std::uint32_t>();
98 test<std::uint64_t>();
99
100 test<std::uint_fast8_t>();
101 test<std::uint_fast16_t>();
102 test<std::uint_fast32_t>();
103 test<std::uint_fast64_t>();
104
105 test<std::uint_least8_t>();
106 test<std::uint_least16_t>();
107 test<std::uint_least32_t>();
108 test<std::uint_least64_t>();
109
110 test<std::uintmax_t>();
111 test<std::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

source code of libcxx/test/std/utilities/function.objects/unord.hash/integral.pass.cpp