1053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
2053d81ceSMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6053d81ceSMarshall Clow //
7053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
8053d81ceSMarshall Clow 
94fc50236SJoe Loser // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
104fc50236SJoe Loser 
11053d81ceSMarshall Clow // <functional>
12053d81ceSMarshall Clow 
13053d81ceSMarshall Clow // template <class T>
14053d81ceSMarshall Clow // struct hash
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
test()32053d81ceSMarshall Clow test()
33053d81ceSMarshall Clow {
34f9127593SEric Fiselier     typedef std::hash<SV> H;
35*681cde7dSNikolas Klauser #if TEST_STD_VER <= 14
36f9127593SEric Fiselier     static_assert((std::is_same<typename H::argument_type, SV>::value), "" );
37053d81ceSMarshall Clow     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
38*681cde7dSNikolas Klauser #endif
39f9127593SEric Fiselier 
40f9127593SEric Fiselier     typedef typename SV::value_type char_type;
41f9127593SEric Fiselier     typedef std::basic_string<char_type> String;
42f9127593SEric Fiselier     typedef std::hash<String> SH;
437c803385SMarshall Clow     ASSERT_NOEXCEPT(H()(SV()));
44f9127593SEric Fiselier 
45053d81ceSMarshall Clow     char_type g1 [ 10 ];
46053d81ceSMarshall Clow     char_type g2 [ 10 ];
47053d81ceSMarshall Clow     for ( int i = 0; i < 10; ++i )
48f2e24f56SStephan T. Lavavej         g1[i] = g2[9-i] = static_cast<char_type>('0' + i);
49f9127593SEric Fiselier     H h;
50f9127593SEric Fiselier     SH sh;
51f9127593SEric Fiselier     SV s1(g1, 10);
52f9127593SEric Fiselier     String ss1(s1);
53f9127593SEric Fiselier     SV s2(g2, 10);
54f9127593SEric Fiselier     String ss2(s2);
55f9127593SEric Fiselier     assert(h(s1) == h(s1));
56053d81ceSMarshall Clow     assert(h(s1) != h(s2));
57f9127593SEric Fiselier     assert(sh(ss1) == h(s1));
58f9127593SEric Fiselier     assert(sh(ss2) == h(s2));
59053d81ceSMarshall Clow }
60053d81ceSMarshall Clow 
main(int,char **)612df59c50SJF Bastien int main(int, char**)
62053d81ceSMarshall Clow {
63053d81ceSMarshall Clow     test<std::string_view>();
647dad0bd6SMarshall Clow #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
657dad0bd6SMarshall Clow     test<std::u8string_view>();
667dad0bd6SMarshall Clow #endif
67053d81ceSMarshall Clow     test<std::u16string_view>();
68053d81ceSMarshall Clow     test<std::u32string_view>();
69f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
70053d81ceSMarshall Clow     test<std::wstring_view>();
71f4c1258dSLouis Dionne #endif
722df59c50SJF Bastien 
732df59c50SJF Bastien   return 0;
74053d81ceSMarshall Clow }
75