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