diff options
author | Eric Fiselier <eric@efcs.ca> | 2017-01-21 00:02:12 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2017-01-21 00:02:12 +0000 |
commit | f9127593a9fd0ef585fed52f838d89c825b8cd72 (patch) | |
tree | 62a21ebb1691065d901bb9bb1294b3a2bd56021f /libcxx/test/std | |
parent | 59e1df524f41d3c2ba1de62f4abea1582cc7332e (diff) | |
download | bcm5719-llvm-f9127593a9fd0ef585fed52f838d89c825b8cd72.tar.gz bcm5719-llvm-f9127593a9fd0ef585fed52f838d89c825b8cd72.zip |
Implement P0513R0 - "Poisoning the Hash"
Summary:
Exactly what the title says.
This patch also adds a `std::hash<nullptr_t>` specialization in C++17, but it was not added by this paper and I can't find the actual paper that adds it.
See http://wg21.link/P0513R0 for more info.
If there are no comments in the next couple of days I'll commit this
Reviewers: mclow.lists, K-ballo, EricWF
Reviewed By: EricWF
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D28938
llvm-svn: 292684
Diffstat (limited to 'libcxx/test/std')
18 files changed, 450 insertions, 10 deletions
diff --git a/libcxx/test/std/containers/sequences/vector.bool/enabled_hash.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/enabled_hash.pass.cpp new file mode 100644 index 00000000000..f8a8dbd8d6d --- /dev/null +++ b/libcxx/test/std/containers/sequences/vector.bool/enabled_hash.pass.cpp @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <vector> + +// Test that <vector> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <vector> + +#include "poisoned_hash_helper.hpp" +#include "min_allocator.h" + +int main() { + test_library_hash_specializations_available(); + { + test_hash_enabled_for_type<std::vector<bool> >(); + test_hash_enabled_for_type<std::vector<bool, min_allocator<bool>>>(); + } +} diff --git a/libcxx/test/std/diagnostics/syserr/syserr.hash/enabled_hash.pass.cpp b/libcxx/test/std/diagnostics/syserr/syserr.hash/enabled_hash.pass.cpp new file mode 100644 index 00000000000..f1b46057591 --- /dev/null +++ b/libcxx/test/std/diagnostics/syserr/syserr.hash/enabled_hash.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <system_error> + +// Test that <system_error> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <system_error> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); + { + test_hash_enabled_for_type<std::error_code>(); + test_hash_enabled_for_type<std::error_condition>(); + } +} diff --git a/libcxx/test/std/strings/basic.string.hash/enabled_hashes.pass.cpp b/libcxx/test/std/strings/basic.string.hash/enabled_hashes.pass.cpp new file mode 100644 index 00000000000..01f01218999 --- /dev/null +++ b/libcxx/test/std/strings/basic.string.hash/enabled_hashes.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <string> + +// Test that <string> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <string> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); + { + test_hash_enabled_for_type<std::string>(); + test_hash_enabled_for_type<std::wstring>(); +#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS + test_hash_enabled_for_type<std::u16string>(); + test_hash_enabled_for_type<std::u32string>(); +#endif + } +} diff --git a/libcxx/test/std/strings/string.view/string.view.hash/enabled_hashes.pass.cpp b/libcxx/test/std/strings/string.view/string.view.hash/enabled_hashes.pass.cpp new file mode 100644 index 00000000000..2e9ebcb4c03 --- /dev/null +++ b/libcxx/test/std/strings/string.view/string.view.hash/enabled_hashes.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <string_view> + +// Test that <string_view> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <string_view> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); + { + test_hash_enabled_for_type<std::string_view>(); + test_hash_enabled_for_type<std::wstring_view>(); +#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS + test_hash_enabled_for_type<std::u16string_view>(); + test_hash_enabled_for_type<std::u32string_view>(); +#endif + } +} diff --git a/libcxx/test/std/strings/string.view/string.view.hash/string_view.pass.cpp b/libcxx/test/std/strings/string.view/string.view.hash/string_view.pass.cpp index 63099e2c886..d0766482551 100644 --- a/libcxx/test/std/strings/string.view/string.view.hash/string_view.pass.cpp +++ b/libcxx/test/std/strings/string.view/string.view.hash/string_view.pass.cpp @@ -19,29 +19,38 @@ // Not very portable #include <string_view> +#include <string> #include <cassert> #include <type_traits> using std::string_view; -template <class T> +template <class SV> void test() { - typedef std::hash<T> H; - static_assert((std::is_same<typename H::argument_type, T>::value), "" ); + typedef std::hash<SV> H; + static_assert((std::is_same<typename H::argument_type, SV>::value), "" ); static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); - H h; -// std::string g1 = "1234567890"; -// std::string g2 = "1234567891"; - typedef typename T::value_type char_type; + + typedef typename SV::value_type char_type; + typedef std::basic_string<char_type> String; + typedef std::hash<String> SH; + char_type g1 [ 10 ]; char_type g2 [ 10 ]; for ( int i = 0; i < 10; ++i ) g1[i] = g2[9-i] = static_cast<char_type>('0' + i); - T s1(g1, 10); - T s2(g2, 10); + H h; + SH sh; + SV s1(g1, 10); + String ss1(s1); + SV s2(g2, 10); + String ss2(s2); + assert(h(s1) == h(s1)); assert(h(s1) != h(s2)); + assert(sh(ss1) == h(s1)); + assert(sh(ss2) == h(s2)); } int main() diff --git a/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/enabled_hashes.pass.cpp b/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/enabled_hashes.pass.cpp new file mode 100644 index 00000000000..9799467c452 --- /dev/null +++ b/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/enabled_hashes.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: libcpp-has-no-threads +// UNSUPPORTED: c++98, c++03 + +// <thread> + +// Test that <thread> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <thread> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); + { + test_hash_enabled_for_type<std::thread::id>(); + } +} diff --git a/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/thread_id.pass.cpp b/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/thread_id.pass.cpp index 106c69e2e4a..f7d4deb8f98 100644 --- a/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/thread_id.pass.cpp +++ b/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/thread_id.pass.cpp @@ -9,7 +9,7 @@ // // UNSUPPORTED: libcpp-has-no-threads -// <functional> +// <thread> // template <class T> // struct hash diff --git a/libcxx/test/std/utilities/function.objects/unord.hash/enabled_hashes.pass.cpp b/libcxx/test/std/utilities/function.objects/unord.hash/enabled_hashes.pass.cpp new file mode 100644 index 00000000000..775247fc80b --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/unord.hash/enabled_hashes.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <functional> + +// Test that <functional> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <functional> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/enabled_hash.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/enabled_hash.pass.cpp new file mode 100644 index 00000000000..e9237c534fb --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/enabled_hash.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <memory> + +// Test that <memory> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <memory> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_shared_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_shared_ptr.pass.cpp index 990cb58722b..5fba1fc0460 100644 --- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_shared_ptr.pass.cpp +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_shared_ptr.pass.cpp @@ -20,11 +20,25 @@ #include <memory> #include <cassert> +#if TEST_STD_VER >= 11 +#include "poisoned_hash_helper.hpp" + +struct A {}; +#endif + int main() { + { int* ptr = new int; std::shared_ptr<int> p(ptr); std::hash<std::shared_ptr<int> > f; std::size_t h = f(p); assert(h == std::hash<int*>()(ptr)); + } +#if TEST_STD_VER >= 11 + { + test_hash_enabled_for_type<std::shared_ptr<int>>(); + test_hash_enabled_for_type<std::shared_ptr<A>>(); + } +#endif } diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp index 5cd4ab1f83d..f989a017348 100644 --- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp @@ -20,11 +20,76 @@ #include <memory> #include <cassert> +#include "test_macros.h" + +#if TEST_STD_VER >= 11 +#include "poisoned_hash_helper.hpp" +#include "deleter_types.h" +#include "min_allocator.h" + +template <class ValueT, class Del> +void test_enabled_with_deleter() { + using UPtr = std::unique_ptr<ValueT, Del>; + using pointer = typename UPtr::pointer; + using RawDel = typename std::decay<Del>::type; + RawDel d(1); + UPtr p(nullptr, std::forward<Del>(d)); + test_hash_enabled_for_type<UPtr>(p); + test_hash_enabled_for_type<pointer>(); +} + +template <class ValueT, class Del> +void test_disabled_with_deleter() { + using UPtr = std::unique_ptr<ValueT, Del>; + using pointer = typename UPtr::pointer; + test_hash_disabled_for_type<UPtr>(); + test_hash_disabled_for_type<pointer>(); +} + +template <class T> +struct std::hash<min_pointer<T, std::integral_constant<size_t, 1>>> { + size_t operator()(min_pointer<T, std::integral_constant<size_t, 1>> p) const { + if (!p) return 0; + return std::hash<T*>{}(std::addressof(*p)); + } +}; + +struct A {}; + +#endif // TEST_STD_VER >= 11 + int main() { + { int* ptr = new int; std::unique_ptr<int> p(ptr); std::hash<std::unique_ptr<int> > f; std::size_t h = f(p); assert(h == std::hash<int*>()(ptr)); + } +#if TEST_STD_VER >= 11 + { + test_enabled_with_deleter<int, Deleter<int>>(); + test_enabled_with_deleter<int[], Deleter<int[]>>(); + test_enabled_with_deleter<int, CopyDeleter<int>>(); + test_enabled_with_deleter<int, CopyDeleter<int[]>>(); + test_enabled_with_deleter<int, NCDeleter<int>&>(); + test_enabled_with_deleter<int[], NCDeleter<int[]>&>(); + test_enabled_with_deleter<int, NCConstDeleter<int> const&>(); + test_enabled_with_deleter<int[], NCConstDeleter<int[]> const&>(); + } + { + test_enabled_with_deleter<int, PointerDeleter<int, 1>>(); + test_enabled_with_deleter<int[], PointerDeleter<int[], 1>>(); + test_enabled_with_deleter<A, PointerDeleter<A, 1>>(); + test_enabled_with_deleter<A[], PointerDeleter<A[], 1>>(); + +#if TEST_STD_VER > 14 + test_disabled_with_deleter<int, PointerDeleter<int, 0>>(); + test_disabled_with_deleter<int[], PointerDeleter<int[], 0>>(); + test_disabled_with_deleter<A, PointerDeleter<A, 0>>(); + test_disabled_with_deleter<A[], PointerDeleter<A[], 0>>(); +#endif + } +#endif } diff --git a/libcxx/test/std/utilities/optional/optional.hash/enabled_hash.pass.cpp b/libcxx/test/std/utilities/optional/optional.hash/enabled_hash.pass.cpp new file mode 100644 index 00000000000..e54a4ced841 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.hash/enabled_hash.pass.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// <optional> + +// Test that <optional> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <optional> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); + { + + } +} diff --git a/libcxx/test/std/utilities/optional/optional.hash/hash.pass.cpp b/libcxx/test/std/utilities/optional/optional.hash/hash.pass.cpp index dfdd07ddf45..ea89dc3bd92 100644 --- a/libcxx/test/std/utilities/optional/optional.hash/hash.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.hash/hash.pass.cpp @@ -17,6 +17,15 @@ #include <memory> #include <cassert> +#include "poisoned_hash_helper.hpp" + +struct A {}; +struct B {}; + +template <> +struct std::hash<B> { + size_t operator()(B const&) { return 0; } +}; int main() { @@ -45,4 +54,16 @@ int main() opt = std::unique_ptr<int>(new int(3)); assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); } + { + test_hash_enabled_for_type<std::optional<int> >(); + test_hash_enabled_for_type<std::optional<int*> >(); + test_hash_enabled_for_type<std::optional<const int> >(); + test_hash_enabled_for_type<std::optional<int* const> >(); + + test_hash_disabled_for_type<std::optional<A>>(); + test_hash_disabled_for_type<std::optional<const A>>(); + + test_hash_enabled_for_type<std::optional<B>>(); + test_hash_enabled_for_type<std::optional<const B>>(); + } } diff --git a/libcxx/test/std/utilities/template.bitset/bitset.hash/enabled_hash.pass.cpp b/libcxx/test/std/utilities/template.bitset/bitset.hash/enabled_hash.pass.cpp new file mode 100644 index 00000000000..1d8bff41ae5 --- /dev/null +++ b/libcxx/test/std/utilities/template.bitset/bitset.hash/enabled_hash.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <bitset> + +// Test that <bitset> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <bitset> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); + { + test_hash_enabled_for_type<std::bitset<0> >(); + test_hash_enabled_for_type<std::bitset<1> >(); + test_hash_enabled_for_type<std::bitset<1024> >(); + test_hash_enabled_for_type<std::bitset<100000> >(); + } +} diff --git a/libcxx/test/std/utilities/type.index/type.index.hash/enabled_hash.pass.cpp b/libcxx/test/std/utilities/type.index/type.index.hash/enabled_hash.pass.cpp new file mode 100644 index 00000000000..26bada38fdd --- /dev/null +++ b/libcxx/test/std/utilities/type.index/type.index.hash/enabled_hash.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <typeindex> + +// Test that <typeindex> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <typeindex> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); +} diff --git a/libcxx/test/std/utilities/type.index/type.index.synopsis/hash_type_index.pass.cpp b/libcxx/test/std/utilities/type.index/type.index.synopsis/hash_type_index.pass.cpp index 6d353f1d305..0fe020bf3d4 100644 --- a/libcxx/test/std/utilities/type.index/type.index.synopsis/hash_type_index.pass.cpp +++ b/libcxx/test/std/utilities/type.index/type.index.synopsis/hash_type_index.pass.cpp @@ -18,9 +18,19 @@ #include <typeindex> #include <type_traits> +#include "test_macros.h" +#if TEST_STD_VER >= 11 +#include "poisoned_hash_helper.hpp" +#endif + int main() { + { typedef std::hash<std::type_index> H; static_assert((std::is_same<typename H::argument_type, std::type_index>::value), "" ); static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); + } + { + test_hash_enabled_for_type<std::type_index>(std::type_index(typeid(int))); + } } diff --git a/libcxx/test/std/utilities/variant/variant.hash/enabled_hash.pass.cpp b/libcxx/test/std/utilities/variant/variant.hash/enabled_hash.pass.cpp new file mode 100644 index 00000000000..826dcbae6b9 --- /dev/null +++ b/libcxx/test/std/utilities/variant/variant.hash/enabled_hash.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// <variant> + +// Test that <variant> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <variant> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); +} diff --git a/libcxx/test/std/utilities/variant/variant.hash/hash.pass.cpp b/libcxx/test/std/utilities/variant/variant.hash/hash.pass.cpp index d807a7c7e2e..96ef967f6c1 100644 --- a/libcxx/test/std/utilities/variant/variant.hash/hash.pass.cpp +++ b/libcxx/test/std/utilities/variant/variant.hash/hash.pass.cpp @@ -21,6 +21,7 @@ #include "test_macros.h" #include "variant_test_helpers.hpp" +#include "poisoned_hash_helper.hpp" #ifndef TEST_HAS_NO_EXCEPTIONS namespace std { @@ -103,6 +104,9 @@ void test_hash_monostate() { ASSERT_SAME_TYPE(decltype(h(m1)), std::size_t); static_assert(std::is_copy_constructible<H>::value, ""); } + { + test_hash_enabled_for_type<std::monostate>(); + } } void test_hash_variant_duplicate_elements() { @@ -117,8 +121,34 @@ void test_hash_variant_duplicate_elements() { LIBCPP_ASSERT(h(v1) != h(v2)); } +struct A {}; +struct B {}; + +template <> +struct std::hash<B> { + size_t operator()(B const&) const { + return 0; + } +}; + +void test_hash_variant_enabled() { + { + test_hash_enabled_for_type<std::variant<int> >(); + test_hash_enabled_for_type<std::variant<int*, long, double, const int> >(); + } + { + test_hash_disabled_for_type<std::variant<int, A>>(); + test_hash_disabled_for_type<std::variant<const A, void*>>(); + } + { + test_hash_enabled_for_type<std::variant<int, B>>(); + test_hash_enabled_for_type<std::variant<const B, int>>(); + } +} + int main() { test_hash_variant(); test_hash_variant_duplicate_elements(); test_hash_monostate(); + test_hash_variant_enabled(); } |