1053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
2053d81ceSMarshall Clow //
3053d81ceSMarshall Clow //                     The LLVM Compiler Infrastructure
4053d81ceSMarshall Clow //
5053d81ceSMarshall Clow // This file is dual licensed under the MIT and the University of Illinois Open
6053d81ceSMarshall Clow // Source Licenses. See LICENSE.TXT for details.
7053d81ceSMarshall Clow //
8053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
9053d81ceSMarshall Clow 
10053d81ceSMarshall Clow // <functional>
11053d81ceSMarshall Clow 
12053d81ceSMarshall Clow // template <class T>
13053d81ceSMarshall Clow // struct hash
14053d81ceSMarshall Clow //     : public unary_function<T, size_t>
15053d81ceSMarshall Clow // {
16053d81ceSMarshall Clow //     size_t operator()(T val) const;
17053d81ceSMarshall Clow // };
18053d81ceSMarshall Clow 
19053d81ceSMarshall Clow // Not very portable
20053d81ceSMarshall Clow 
21053d81ceSMarshall Clow #include <string_view>
22f9127593SEric Fiselier #include <string>
23053d81ceSMarshall Clow #include <cassert>
24053d81ceSMarshall Clow #include <type_traits>
25053d81ceSMarshall Clow 
267c803385SMarshall Clow #include "test_macros.h"
277c803385SMarshall Clow 
28053d81ceSMarshall Clow using std::string_view;
29053d81ceSMarshall Clow 
30f9127593SEric Fiselier template <class SV>
31053d81ceSMarshall Clow void
32053d81ceSMarshall Clow test()
33053d81ceSMarshall Clow {
34f9127593SEric Fiselier     typedef std::hash<SV> H;
35f9127593SEric Fiselier     static_assert((std::is_same<typename H::argument_type, SV>::value), "" );
36053d81ceSMarshall Clow     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
37f9127593SEric Fiselier 
38f9127593SEric Fiselier     typedef typename SV::value_type char_type;
39f9127593SEric Fiselier     typedef std::basic_string<char_type> String;
40f9127593SEric Fiselier     typedef std::hash<String> SH;
417c803385SMarshall Clow     ASSERT_NOEXCEPT(H()(SV()));
42f9127593SEric Fiselier 
43053d81ceSMarshall Clow     char_type g1 [ 10 ];
44053d81ceSMarshall Clow     char_type g2 [ 10 ];
45053d81ceSMarshall Clow     for ( int i = 0; i < 10; ++i )
46f2e24f56SStephan T. Lavavej         g1[i] = g2[9-i] = static_cast<char_type>('0' + i);
47f9127593SEric Fiselier     H h;
48f9127593SEric Fiselier     SH sh;
49f9127593SEric Fiselier     SV s1(g1, 10);
50f9127593SEric Fiselier     String ss1(s1);
51f9127593SEric Fiselier     SV s2(g2, 10);
52f9127593SEric Fiselier     String ss2(s2);
53f9127593SEric Fiselier     assert(h(s1) == h(s1));
54053d81ceSMarshall Clow     assert(h(s1) != h(s2));
55f9127593SEric Fiselier     assert(sh(ss1) == h(s1));
56f9127593SEric Fiselier     assert(sh(ss2) == h(s2));
57053d81ceSMarshall Clow }
58053d81ceSMarshall Clow 
59053d81ceSMarshall Clow int main()
60053d81ceSMarshall Clow {
61053d81ceSMarshall Clow     test<std::string_view>();
62*7dad0bd6SMarshall Clow #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
63*7dad0bd6SMarshall Clow     test<std::u8string_view>();
64*7dad0bd6SMarshall Clow #endif
65053d81ceSMarshall Clow #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
66053d81ceSMarshall Clow     test<std::u16string_view>();
67053d81ceSMarshall Clow     test<std::u32string_view>();
68053d81ceSMarshall Clow #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
69053d81ceSMarshall Clow     test<std::wstring_view>();
70053d81ceSMarshall Clow }
71