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