Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++98, c++03, c++11, c++14 |
| 10 | // <optional> |
| 11 | |
| 12 | // template <class T> struct hash<optional<T>>; |
| 13 | |
| 14 | #include <optional> |
| 15 | #include <string> |
| 16 | #include <memory> |
| 17 | #include <cassert> |
| 18 | |
Nico Weber | cc89063 | 2019-08-21 00:14:12 +0000 | [diff] [blame] | 19 | #include "poisoned_hash_helper.h" |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 20 | |
Marshall Clow | 7fc6a55 | 2019-05-31 18:35:30 +0000 | [diff] [blame] | 21 | #include "test_macros.h" |
| 22 | |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 23 | struct A {}; |
| 24 | struct B {}; |
| 25 | |
Michael Park | 41c4de4 | 2017-03-23 06:21:24 +0000 | [diff] [blame] | 26 | namespace std { |
| 27 | |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 28 | template <> |
Michael Park | 41c4de4 | 2017-03-23 06:21:24 +0000 | [diff] [blame] | 29 | struct hash<B> { |
Billy Robert O'Neal III | e52a34b | 2017-04-18 00:19:50 +0000 | [diff] [blame] | 30 | size_t operator()(B const&) TEST_NOEXCEPT_FALSE { return 0; } |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 31 | }; |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 32 | |
Michael Park | 41c4de4 | 2017-03-23 06:21:24 +0000 | [diff] [blame] | 33 | } |
| 34 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 35 | int main(int, char**) |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 36 | { |
| 37 | using std::optional; |
| 38 | const std::size_t nullopt_hash = |
| 39 | std::hash<optional<double>>{}(optional<double>{}); |
| 40 | |
Billy Robert O'Neal III | e52a34b | 2017-04-18 00:19:50 +0000 | [diff] [blame] | 41 | |
| 42 | { |
| 43 | optional<B> opt; |
| 44 | ASSERT_NOT_NOEXCEPT(std::hash<optional<B>>()(opt)); |
| 45 | ASSERT_NOT_NOEXCEPT(std::hash<optional<const B>>()(opt)); |
| 46 | } |
| 47 | |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 48 | { |
| 49 | typedef int T; |
| 50 | optional<T> opt; |
| 51 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 52 | opt = 2; |
| 53 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 54 | } |
| 55 | { |
| 56 | typedef std::string T; |
| 57 | optional<T> opt; |
| 58 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 59 | opt = std::string("123"); |
| 60 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 61 | } |
| 62 | { |
| 63 | typedef std::unique_ptr<int> T; |
| 64 | optional<T> opt; |
| 65 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 66 | opt = std::unique_ptr<int>(new int(3)); |
| 67 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 68 | } |
Eric Fiselier | f912759 | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 69 | { |
| 70 | test_hash_enabled_for_type<std::optional<int> >(); |
| 71 | test_hash_enabled_for_type<std::optional<int*> >(); |
| 72 | test_hash_enabled_for_type<std::optional<const int> >(); |
| 73 | test_hash_enabled_for_type<std::optional<int* const> >(); |
| 74 | |
| 75 | test_hash_disabled_for_type<std::optional<A>>(); |
| 76 | test_hash_disabled_for_type<std::optional<const A>>(); |
| 77 | |
| 78 | test_hash_enabled_for_type<std::optional<B>>(); |
| 79 | test_hash_enabled_for_type<std::optional<const B>>(); |
| 80 | } |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 81 | |
| 82 | return 0; |
Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 83 | } |