summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2022-06-09 12:07:15 +0100
committerJonathan Wakely <jwakely@redhat.com>2022-06-10 14:39:25 +0100
commitb370ed0bf93ecf0ff51d29e7fc132c433b2aa1be (patch)
treee34dad399c36da51e8135fbe7f03773feb3c769b /libstdc++-v3/testsuite
parent5940b4e59f8e198dbf7e8b733561ef72a9ba2cbc (diff)
libstdc++: Make std::hash<basic_string<>> allocator-agnostic (LWG 3705)
This new library issue was recently moved to Tentatively Ready by an LWG poll, so I'm making the change on trunk. As noted in PR libstc++/105907 the std::hash specializations for PMR strings were not treated as slow hashes by the unordered containers, so this change preserves that. The new specializations for custom allocators are also not treated as slow, for the same reason. For the versioned namespace (i.e. unstable ABI) we don't have to worry about that, so can enable hash code caching for all basic_string specializations. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (__hash_str_base): New class template. (hash<basic_string<C, char_traits<C>, A>>): Define partial specialization for each of the standard character types. (hash<string>, hash<wstring>, hash<u8string>, hash<u16string>) (hash<u32string>): Remove explicit specializations. * include/std/string (__hash_string_base): Remove class template. (hash<pmr::string>, hash<pmr::wstring>, hash<pmr::u8string>) (hash<pmr::u16string>, hash<pmr::u32string>): Remove explicit specializations. * testsuite/21_strings/basic_string/hash/hash.cc: Test with custom allocators. * testsuite/21_strings/basic_string/hash/hash_char8_t.cc: Likewise.
Diffstat (limited to 'libstdc++-v3/testsuite')
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/hash/hash.cc16
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/hash/hash_char8_t.cc12
2 files changed, 28 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/hash/hash.cc b/libstdc++-v3/testsuite/21_strings/basic_string/hash/hash.cc
index 4c16f0e6f40..7f3809926e5 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/hash/hash.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/hash/hash.cc
@@ -20,6 +20,7 @@
#include <string>
#include <memory_resource>
#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
// C++17 24.3.5 [basic.string.hash]
// If S is one of these string types, SV is the corresponding string view type,
@@ -54,9 +55,24 @@ test02()
#endif
}
+template<typename C>
+using String
+ = std::basic_string<C, std::char_traits<C>, __gnu_test::SimpleAllocator<C>>;
+
+void
+test03()
+{
+ // LWG 3705. Hashability shouldn't depend on basic_string's allocator
+ VERIFY( test(String<char>("a narrow string")) );
+ VERIFY( test(String<char16_t>(u"a utf-16 string")) );
+ VERIFY( test(String<char32_t>(U"a utf-32 string")) );
+ VERIFY( test(String<wchar_t>(L"a wide string")) );
+}
+
int
main()
{
test01();
test02();
+ test03();
}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/hash/hash_char8_t.cc b/libstdc++-v3/testsuite/21_strings/basic_string/hash/hash_char8_t.cc
index 217fe15c7fe..0ff98ad1caa 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/hash/hash_char8_t.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/hash/hash_char8_t.cc
@@ -21,6 +21,7 @@
#include <string>
#include <memory_resource>
#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
// C++2a N4810 21.3.5 [basic.string.hash]
// If S is one of these string types, SV is the corresponding string view type,
@@ -55,9 +56,20 @@ test02()
VERIFY( hash<std::string>()(native) == hash<std::u8string>()(utf8) );
}
+void
+test03()
+{
+ using Alloc = __gnu_test::SimpleAllocator<char8_t>;
+ using Stringu8 = std::basic_string<char8_t, std::char_traits<char8_t>, Alloc>;
+
+ // LWG 3705. Hashability shouldn't depend on basic_string's allocator
+ VERIFY( test(Stringu8(u8"a utf-8 string, with custom allocator")) );
+}
+
int
main()
{
test01();
test02();
+ test03();
}