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 
9053d81ceSMarshall Clow // <functional>
10053d81ceSMarshall Clow 
11053d81ceSMarshall Clow // template <class T>
12053d81ceSMarshall Clow // struct hash
13053d81ceSMarshall Clow //     : public unary_function<T, size_t>
14053d81ceSMarshall Clow // {
15053d81ceSMarshall Clow //     size_t operator()(T val) const;
16053d81ceSMarshall Clow // };
17053d81ceSMarshall Clow 
18053d81ceSMarshall Clow // Not very portable
19053d81ceSMarshall Clow 
20053d81ceSMarshall Clow #include <string_view>
21f9127593SEric Fiselier #include <string>
22053d81ceSMarshall Clow #include <cassert>
23053d81ceSMarshall Clow #include <type_traits>
24053d81ceSMarshall Clow 
257c803385SMarshall Clow #include "test_macros.h"
267c803385SMarshall Clow 
27053d81ceSMarshall Clow using std::string_view;
28053d81ceSMarshall Clow 
29f9127593SEric Fiselier template <class SV>
30053d81ceSMarshall Clow void
31053d81ceSMarshall Clow test()
32053d81ceSMarshall Clow {
33f9127593SEric Fiselier     typedef std::hash<SV> H;
34f9127593SEric Fiselier     static_assert((std::is_same<typename H::argument_type, SV>::value), "" );
35053d81ceSMarshall Clow     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
36f9127593SEric Fiselier 
37f9127593SEric Fiselier     typedef typename SV::value_type char_type;
38f9127593SEric Fiselier     typedef std::basic_string<char_type> String;
39f9127593SEric Fiselier     typedef std::hash<String> SH;
407c803385SMarshall Clow     ASSERT_NOEXCEPT(H()(SV()));
41f9127593SEric Fiselier 
42053d81ceSMarshall Clow     char_type g1 [ 10 ];
43053d81ceSMarshall Clow     char_type g2 [ 10 ];
44053d81ceSMarshall Clow     for ( int i = 0; i < 10; ++i )
45f2e24f56SStephan T. Lavavej         g1[i] = g2[9-i] = static_cast<char_type>('0' + i);
46f9127593SEric Fiselier     H h;
47f9127593SEric Fiselier     SH sh;
48f9127593SEric Fiselier     SV s1(g1, 10);
49f9127593SEric Fiselier     String ss1(s1);
50f9127593SEric Fiselier     SV s2(g2, 10);
51f9127593SEric Fiselier     String ss2(s2);
52f9127593SEric Fiselier     assert(h(s1) == h(s1));
53053d81ceSMarshall Clow     assert(h(s1) != h(s2));
54f9127593SEric Fiselier     assert(sh(ss1) == h(s1));
55f9127593SEric Fiselier     assert(sh(ss2) == h(s2));
56053d81ceSMarshall Clow }
57053d81ceSMarshall Clow 
58*2df59c50SJF Bastien int main(int, char**)
59053d81ceSMarshall Clow {
60053d81ceSMarshall Clow     test<std::string_view>();
617dad0bd6SMarshall Clow #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
627dad0bd6SMarshall Clow     test<std::u8string_view>();
637dad0bd6SMarshall Clow #endif
64053d81ceSMarshall Clow #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
65053d81ceSMarshall Clow     test<std::u16string_view>();
66053d81ceSMarshall Clow     test<std::u32string_view>();
67053d81ceSMarshall Clow #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
68053d81ceSMarshall Clow     test<std::wstring_view>();
69*2df59c50SJF Bastien 
70*2df59c50SJF Bastien   return 0;
71053d81ceSMarshall Clow }
72