blob: 8f94d6be6531732fe3dd8bfc0ee32a49620219e1 [file] [log] [blame]
Eric Fiseliera9e65962016-10-12 07:46:20 +00001//===----------------------------------------------------------------------===//
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 Fiselierf9127592017-01-21 00:02:12 +000020#include "poisoned_hash_helper.hpp"
21
22struct A {};
23struct B {};
24
25template <>
26struct std::hash<B> {
27 size_t operator()(B const&) { return 0; }
28};
Eric Fiseliera9e65962016-10-12 07:46:20 +000029
30int main()
31{
32 using std::optional;
33 const std::size_t nullopt_hash =
34 std::hash<optional<double>>{}(optional<double>{});
35
36 {
37 typedef int T;
38 optional<T> opt;
Marshall Clow7c803382017-03-23 02:40:28 +000039 ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt));
Eric Fiseliera9e65962016-10-12 07:46:20 +000040 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
41 opt = 2;
42 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
43 }
44 {
45 typedef std::string T;
46 optional<T> opt;
Marshall Clow7c803382017-03-23 02:40:28 +000047 ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt));
Eric Fiseliera9e65962016-10-12 07:46:20 +000048 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
49 opt = std::string("123");
50 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
51 }
52 {
53 typedef std::unique_ptr<int> T;
54 optional<T> opt;
Marshall Clow7c803382017-03-23 02:40:28 +000055 ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt));
Eric Fiseliera9e65962016-10-12 07:46:20 +000056 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
57 opt = std::unique_ptr<int>(new int(3));
58 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
59 }
Eric Fiselierf9127592017-01-21 00:02:12 +000060 {
61 test_hash_enabled_for_type<std::optional<int> >();
62 test_hash_enabled_for_type<std::optional<int*> >();
63 test_hash_enabled_for_type<std::optional<const int> >();
64 test_hash_enabled_for_type<std::optional<int* const> >();
65
66 test_hash_disabled_for_type<std::optional<A>>();
67 test_hash_disabled_for_type<std::optional<const A>>();
68
69 test_hash_enabled_for_type<std::optional<B>>();
70 test_hash_enabled_for_type<std::optional<const B>>();
71 }
Eric Fiseliera9e65962016-10-12 07:46:20 +000072}