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