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 <cstddef> 23 #include <cstdint> 24 #include <limits> 25 #include <type_traits> 26 27 #include "test_macros.h" 28 29 template <class T> 30 void 31 test() 32 { 33 typedef std::hash<T> H; 34 #if TEST_STD_VER <= 17 35 static_assert((std::is_same<typename H::argument_type, T>::value), ""); 36 static_assert((std::is_same<typename H::result_type, std::size_t>::value), ""); 37 #endif 38 ASSERT_NOEXCEPT(H()(T())); 39 H h; 40 41 for (int i = 0; i <= 5; ++i) 42 { 43 T t(static_cast<T>(i)); 44 const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings 45 if (small) 46 { 47 const std::size_t result = h(t); 48 LIBCPP_ASSERT(result == static_cast<std::size_t>(t)); 49 ((void)result); // Prevent unused warning 50 } 51 } 52 } 53 54 int main(int, char**) 55 { 56 test<bool>(); 57 test<char>(); 58 test<signed char>(); 59 test<unsigned char>(); 60 test<char16_t>(); 61 test<char32_t>(); 62 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 63 test<wchar_t>(); 64 #endif 65 test<short>(); 66 test<unsigned short>(); 67 test<int>(); 68 test<unsigned int>(); 69 test<long>(); 70 test<unsigned long>(); 71 test<long long>(); 72 test<unsigned long long>(); 73 74 // LWG #2119 75 test<std::ptrdiff_t>(); 76 test<std::size_t>(); 77 78 test<std::int8_t>(); 79 test<std::int16_t>(); 80 test<std::int32_t>(); 81 test<std::int64_t>(); 82 83 test<std::int_fast8_t>(); 84 test<std::int_fast16_t>(); 85 test<std::int_fast32_t>(); 86 test<std::int_fast64_t>(); 87 88 test<std::int_least8_t>(); 89 test<std::int_least16_t>(); 90 test<std::int_least32_t>(); 91 test<std::int_least64_t>(); 92 93 test<std::intmax_t>(); 94 test<std::intptr_t>(); 95 96 test<std::uint8_t>(); 97 test<std::uint16_t>(); 98 test<std::uint32_t>(); 99 test<std::uint64_t>(); 100 101 test<std::uint_fast8_t>(); 102 test<std::uint_fast16_t>(); 103 test<std::uint_fast32_t>(); 104 test<std::uint_fast64_t>(); 105 106 test<std::uint_least8_t>(); 107 test<std::uint_least16_t>(); 108 test<std::uint_least32_t>(); 109 test<std::uint_least64_t>(); 110 111 test<std::uintmax_t>(); 112 test<std::uintptr_t>(); 113 114 #ifndef TEST_HAS_NO_INT128 115 test<__int128_t>(); 116 test<__uint128_t>(); 117 #endif 118 119 return 0; 120 } 121