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 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 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 test<wchar_t>(); 62 test<short>(); 63 test<unsigned short>(); 64 test<int>(); 65 test<unsigned int>(); 66 test<long>(); 67 test<unsigned long>(); 68 test<long long>(); 69 test<unsigned long long>(); 70 71 // LWG #2119 72 test<std::ptrdiff_t>(); 73 test<size_t>(); 74 75 test<int8_t>(); 76 test<int16_t>(); 77 test<int32_t>(); 78 test<int64_t>(); 79 80 test<int_fast8_t>(); 81 test<int_fast16_t>(); 82 test<int_fast32_t>(); 83 test<int_fast64_t>(); 84 85 test<int_least8_t>(); 86 test<int_least16_t>(); 87 test<int_least32_t>(); 88 test<int_least64_t>(); 89 90 test<intmax_t>(); 91 test<intptr_t>(); 92 93 test<uint8_t>(); 94 test<uint16_t>(); 95 test<uint32_t>(); 96 test<uint64_t>(); 97 98 test<uint_fast8_t>(); 99 test<uint_fast16_t>(); 100 test<uint_fast32_t>(); 101 test<uint_fast64_t>(); 102 103 test<uint_least8_t>(); 104 test<uint_least16_t>(); 105 test<uint_least32_t>(); 106 test<uint_least64_t>(); 107 108 test<uintmax_t>(); 109 test<uintptr_t>(); 110 111 #ifndef _LIBCPP_HAS_NO_INT128 112 test<__int128_t>(); 113 test<__uint128_t>(); 114 #endif 115 116 return 0; 117 } 118