]>
Commit | Line | Data |
---|---|---|
83ffe9cd | 1 | // Copyright (C) 2019-2023 Free Software Foundation, Inc. |
0cb78ef4 JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
d8c446a1 | 18 | // { dg-do run { target c++20 } } |
0cb78ef4 JW |
19 | |
20 | #include <string> | |
21 | #include <memory_resource> | |
22 | #include <testsuite_hooks.h> | |
b370ed0b | 23 | #include <testsuite_allocator.h> |
0cb78ef4 JW |
24 | |
25 | // C++2a N4810 21.3.5 [basic.string.hash] | |
26 | // If S is one of these string types, SV is the corresponding string view type, | |
27 | // and s is an object of type S, then hash<S>()(s) == hash<SV>()(SV(s)). | |
28 | ||
29 | template<typename S> | |
30 | bool | |
31 | test(const S& s) | |
32 | { | |
33 | using std::hash; | |
34 | using SV = std::basic_string_view<typename S::value_type>; | |
35 | return hash<S>()(s) == hash<SV>()(SV(s)); | |
36 | } | |
37 | ||
38 | void | |
39 | test01() | |
40 | { | |
41 | VERIFY( test(std::string("a narrow string")) ); | |
0cb78ef4 | 42 | VERIFY( test(std::u8string(u8"a utf-8 string")) ); |
53e69273 JW |
43 | #if _GLIBCXX_USE_CXX11_ABI |
44 | VERIFY( test(std::pmr::string("a narrow string, but with PMR!")) ); | |
0cb78ef4 | 45 | VERIFY( test(std::pmr::u8string(u8"a utf-8 string, but with PMR!")) ); |
53e69273 | 46 | #endif |
0cb78ef4 JW |
47 | } |
48 | ||
49 | void | |
50 | test02() | |
51 | { | |
52 | using std::hash; | |
53 | std::string native("a string, a string, my stringdom for a string"); | |
54 | std::u8string utf8(u8"a string, a string, my stringdom for a string"); | |
55 | VERIFY( hash<std::string>()(native) == hash<std::u8string>()(utf8) ); | |
56 | } | |
57 | ||
b370ed0b JW |
58 | void |
59 | test03() | |
60 | { | |
61 | using Alloc = __gnu_test::SimpleAllocator<char8_t>; | |
62 | using Stringu8 = std::basic_string<char8_t, std::char_traits<char8_t>, Alloc>; | |
63 | ||
64 | // LWG 3705. Hashability shouldn't depend on basic_string's allocator | |
65 | VERIFY( test(Stringu8(u8"a utf-8 string, with custom allocator")) ); | |
66 | } | |
67 | ||
0cb78ef4 JW |
68 | int |
69 | main() | |
70 | { | |
71 | test01(); | |
72 | test02(); | |
b370ed0b | 73 | test03(); |
0cb78ef4 | 74 | } |