Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 |
| 11 | // <optional> |
| 12 | |
| 13 | // template <class T> struct hash<optional<T>>; |
| 14 | |
| 15 | #include <optional> |
| 16 | #include <string> |
| 17 | #include <memory> |
| 18 | #include <cassert> |
| 19 | |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 20 | #include "poisoned_hash_helper.hpp" |
| 21 | |
| 22 | struct A {}; |
| 23 | struct B {}; |
| 24 | |
Michael Park | 41c4de4 | 2017-03-23 06:21:24 +0000 | [diff] [blame] | 25 | namespace std { |
| 26 | |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 27 | template <> |
Michael Park | 41c4de4 | 2017-03-23 06:21:24 +0000 | [diff] [blame] | 28 | struct hash<B> { |
Billy Robert O'Neal III | 8ac1fa1 | 2017-04-06 23:50:33 +0000 | [diff] [blame] | 29 | size_t operator()(B const&) noexcept(false) { return 0; } |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 30 | }; |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 31 | |
Michael Park | 41c4de4 | 2017-03-23 06:21:24 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 34 | int main() |
| 35 | { |
| 36 | using std::optional; |
| 37 | const std::size_t nullopt_hash = |
| 38 | std::hash<optional<double>>{}(optional<double>{}); |
| 39 | |
| 40 | { |
| 41 | typedef int T; |
| 42 | optional<T> opt; |
Akira Hatanaka | 6055ae8 | 2017-04-07 05:55:28 +0000 | [diff] [blame] | 43 | ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt)); |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 44 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 45 | opt = 2; |
| 46 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 47 | } |
| 48 | { |
| 49 | typedef std::string T; |
| 50 | optional<T> opt; |
Akira Hatanaka | 6055ae8 | 2017-04-07 05:55:28 +0000 | [diff] [blame] | 51 | ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt)); |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 52 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 53 | opt = std::string("123"); |
| 54 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 55 | } |
| 56 | { |
| 57 | typedef std::unique_ptr<int> T; |
| 58 | optional<T> opt; |
Akira Hatanaka | 6055ae8 | 2017-04-07 05:55:28 +0000 | [diff] [blame] | 59 | ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt)); |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 60 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 61 | opt = std::unique_ptr<int>(new int(3)); |
| 62 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 63 | } |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 64 | { |
| 65 | test_hash_enabled_for_type<std::optional<int> >(); |
| 66 | test_hash_enabled_for_type<std::optional<int*> >(); |
| 67 | test_hash_enabled_for_type<std::optional<const int> >(); |
| 68 | test_hash_enabled_for_type<std::optional<int* const> >(); |
| 69 | |
| 70 | test_hash_disabled_for_type<std::optional<A>>(); |
| 71 | test_hash_disabled_for_type<std::optional<const A>>(); |
| 72 | |
| 73 | test_hash_enabled_for_type<std::optional<B>>(); |
| 74 | test_hash_enabled_for_type<std::optional<const B>>(); |
| 75 | } |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 76 | } |