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 test()
33 {
34     typedef std::hash<SV> H;
35 #if TEST_STD_VER <= 14
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 #endif
39 
40     typedef typename SV::value_type char_type;
41     typedef std::basic_string<char_type> String;
42     typedef std::hash<String> SH;
43     ASSERT_NOEXCEPT(H()(SV()));
44 
45     char_type g1 [ 10 ];
46     char_type g2 [ 10 ];
47     for ( int i = 0; i < 10; ++i )
48         g1[i] = g2[9-i] = static_cast<char_type>('0' + i);
49     H h;
50     SH sh;
51     SV s1(g1, 10);
52     String ss1(s1);
53     SV s2(g2, 10);
54     String ss2(s2);
55     assert(h(s1) == h(s1));
56     assert(h(s1) != h(s2));
57     assert(sh(ss1) == h(s1));
58     assert(sh(ss2) == h(s2));
59 }
60 
main(int,char **)61 int main(int, char**)
62 {
63     test<std::string_view>();
64 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
65     test<std::u8string_view>();
66 #endif
67     test<std::u16string_view>();
68     test<std::u32string_view>();
69 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
70     test<std::wstring_view>();
71 #endif
72 
73   return 0;
74 }
75