Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 <limits> |
| 25 | |
| 26 | template <class T> |
| 27 | void |
| 28 | test() |
| 29 | { |
Marshall Clow | 674e07d | 2015-01-07 21:53:23 +0000 | [diff] [blame] | 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 | H h; |
| 34 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 35 | for (int i = 0; i <= 5; ++i) |
| 36 | { |
| 37 | T t(i); |
Howard Hinnant | 40c13d3 | 2011-12-05 00:08:45 +0000 | [diff] [blame] | 38 | if (sizeof(T) <= sizeof(std::size_t)) |
| 39 | assert(h(t) == t); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | int main() |
| 44 | { |
| 45 | test<bool>(); |
| 46 | test<char>(); |
| 47 | test<signed char>(); |
| 48 | test<unsigned char>(); |
| 49 | test<char16_t>(); |
| 50 | test<char32_t>(); |
| 51 | test<wchar_t>(); |
| 52 | test<short>(); |
| 53 | test<unsigned short>(); |
| 54 | test<int>(); |
| 55 | test<unsigned int>(); |
| 56 | test<long>(); |
| 57 | test<unsigned long>(); |
| 58 | test<long long>(); |
| 59 | test<unsigned long long>(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | } |