Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | 412dbeb | 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 | 3e51952 | 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 <string> |
| 22 | #include <cassert> |
| 23 | #include <type_traits> |
| 24 | |
Marshall Clow | 7c80338 | 2017-03-23 02:40:28 +0000 | [diff] [blame] | 25 | #include "test_macros.h" |
| 26 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | template <class T> |
| 28 | void |
| 29 | test() |
| 30 | { |
| 31 | typedef std::hash<T> H; |
Marshall Clow | d95510e | 2015-01-07 21:53:23 +0000 | [diff] [blame] | 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), "" ); |
Marshall Clow | 7c80338 | 2017-03-23 02:40:28 +0000 | [diff] [blame] | 34 | ASSERT_NOEXCEPT(H()(T())); |
| 35 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 36 | H h; |
| 37 | std::string g1 = "1234567890"; |
| 38 | std::string g2 = "1234567891"; |
| 39 | T s1(g1.begin(), g1.end()); |
| 40 | T s2(g2.begin(), g2.end()); |
| 41 | assert(h(s1) != h(s2)); |
| 42 | } |
| 43 | |
| 44 | int main() |
| 45 | { |
| 46 | test<std::string>(); |
Marshall Clow | 7dad0bd | 2018-12-11 04:35:44 +0000 | [diff] [blame^] | 47 | #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L |
| 48 | test<std::u8string>(); |
| 49 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 50 | #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS |
| 51 | test<std::u16string>(); |
| 52 | test<std::u32string>(); |
Howard Hinnant | bf2897c | 2010-08-22 00:47:54 +0000 | [diff] [blame] | 53 | #endif // _LIBCPP_HAS_NO_UNICODE_CHARS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | test<std::wstring>(); |
| 55 | } |