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