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 // <functional> 10 11 // template <class T> 12 // struct hash 13 // : public unary_function<T, size_t> 14 // { 15 // size_t operator()(T val) const; 16 // }; 17 18 #include <functional> 19 #include <cassert> 20 #include <type_traits> 21 #include <cstddef> 22 #include <limits> 23 24 #include "test_macros.h" 25 26 template <class T> 27 void test()28test() 29 { 30 typedef std::hash<T> H; 31 static_assert((std::is_same<typename H::argument_type, T>::value), "" ); 32 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); 33 ASSERT_NOEXCEPT(H()(T())); 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(int,char **)49int main(int, char**) 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 return 0; 113 } 114