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