blob: 0f74557b7300904545e94b2fd80cbd1254c32d54 [file] [log] [blame]
Eric Fiseliera9e65962016-10-12 07:46:20 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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 Fiseliera9e65962016-10-12 07:46:20 +00006//
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
Eric Fiselierf9127592017-01-21 00:02:12 +000019#include "poisoned_hash_helper.hpp"
20
21struct A {};
22struct B {};
23
Michael Park41c4de42017-03-23 06:21:24 +000024namespace std {
25
Eric Fiselierf9127592017-01-21 00:02:12 +000026template <>
Michael Park41c4de42017-03-23 06:21:24 +000027struct hash<B> {
Billy Robert O'Neal IIIe52a34b2017-04-18 00:19:50 +000028 size_t operator()(B const&) TEST_NOEXCEPT_FALSE { return 0; }
Eric Fiselierf9127592017-01-21 00:02:12 +000029};
Eric Fiseliera9e65962016-10-12 07:46:20 +000030
Michael Park41c4de42017-03-23 06:21:24 +000031}
32
Eric Fiseliera9e65962016-10-12 07:46:20 +000033int main()
34{
35 using std::optional;
36 const std::size_t nullopt_hash =
37 std::hash<optional<double>>{}(optional<double>{});
38
Billy Robert O'Neal IIIe52a34b2017-04-18 00:19:50 +000039
40 {
41 optional<B> opt;
42 ASSERT_NOT_NOEXCEPT(std::hash<optional<B>>()(opt));
43 ASSERT_NOT_NOEXCEPT(std::hash<optional<const B>>()(opt));
44 }
45
Eric Fiseliera9e65962016-10-12 07:46:20 +000046 {
47 typedef int T;
48 optional<T> opt;
49 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
50 opt = 2;
51 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
52 }
53 {
54 typedef std::string T;
55 optional<T> opt;
56 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
57 opt = std::string("123");
58 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
59 }
60 {
61 typedef std::unique_ptr<int> T;
62 optional<T> opt;
63 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
64 opt = std::unique_ptr<int>(new int(3));
65 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
66 }
Eric Fiselierf9127592017-01-21 00:02:12 +000067 {
68 test_hash_enabled_for_type<std::optional<int> >();
69 test_hash_enabled_for_type<std::optional<int*> >();
70 test_hash_enabled_for_type<std::optional<const int> >();
71 test_hash_enabled_for_type<std::optional<int* const> >();
72
73 test_hash_disabled_for_type<std::optional<A>>();
74 test_hash_disabled_for_type<std::optional<const A>>();
75
76 test_hash_enabled_for_type<std::optional<B>>();
77 test_hash_enabled_for_type<std::optional<const B>>();
78 }
Eric Fiseliera9e65962016-10-12 07:46:20 +000079}