| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14) |
| 10 | |
| 11 | // <functional> |
| 12 | |
| 13 | // template <class T> |
| 14 | // struct hash |
| 15 | // { |
| 16 | // size_t operator()(T val) const; |
| 17 | // }; |
| 18 | |
| 19 | // Not very portable |
| 20 | |
| 21 | #include <string_view> |
| 22 | #include <string> |
| 23 | #include <cassert> |
| 24 | #include <type_traits> |
| 25 | |
| 26 | #include "test_macros.h" |
| 27 | |
| 28 | using std::string_view; |
| 29 | |
| 30 | template <class SV> |
| 31 | void test() { |
| 32 | typedef std::hash<SV> H; |
| 33 | #if TEST_STD_VER <= 14 |
| 34 | static_assert((std::is_same<typename H::argument_type, SV>::value), "" ); |
| 35 | static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); |
| 36 | #endif |
| 37 | |
| 38 | typedef typename SV::value_type char_type; |
| 39 | typedef std::basic_string<char_type> String; |
| 40 | typedef std::hash<String> SH; |
| 41 | ASSERT_NOEXCEPT(H()(SV())); |
| 42 | |
| 43 | char_type g1[10]; |
| 44 | char_type g2[10]; |
| 45 | for (int i = 0; i < 10; ++i) |
| 46 | g1[i] = g2[9 - i] = static_cast<char_type>('0' + i); |
| 47 | H h; |
| 48 | SH sh; |
| 49 | SV s1(g1, 10); |
| 50 | String ss1(s1); |
| 51 | SV s2(g2, 10); |
| 52 | String ss2(s2); |
| 53 | assert(h(s1) == h(s1)); |
| 54 | assert(h(s1) != h(s2)); |
| 55 | assert(sh(ss1) == h(s1)); |
| 56 | assert(sh(ss2) == h(s2)); |
| 57 | } |
| 58 | |
| 59 | int main(int, char**) { |
| 60 | test<std::string_view>(); |
| 61 | #ifndef TEST_HAS_NO_CHAR8_T |
| 62 | test<std::u8string_view>(); |
| 63 | #endif |
| 64 | test<std::u16string_view>(); |
| 65 | test<std::u32string_view>(); |
| 66 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 67 | test<std::wstring_view>(); |
| 68 | #endif |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |