Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / optional / optional.hash / hash.pass.cpp
blobcb23e03e7ca708168d70009194d6edb01e6ac7c5
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
10 // <optional>
12 // template <class T> struct hash<optional<T>>;
14 #include <optional>
15 #include <string>
16 #include <memory>
17 #include <cassert>
19 #include "poisoned_hash_helper.h"
21 #include "test_macros.h"
23 struct A {};
24 struct B {};
26 namespace std {
28 template <>
29 struct hash<B> {
30 std::size_t operator()(B const&) noexcept(false) { return 0; }
35 int main(int, char**)
37 using std::optional;
38 const std::size_t nullopt_hash =
39 std::hash<optional<double>>{}(optional<double>{});
43 optional<B> opt;
44 ASSERT_NOT_NOEXCEPT(std::hash<optional<B>>()(opt));
45 ASSERT_NOT_NOEXCEPT(std::hash<optional<const B>>()(opt));
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));
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));
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));
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> >();
75 test_hash_disabled_for_type<std::optional<A>>();
76 test_hash_disabled_for_type<std::optional<const A>>();
78 test_hash_enabled_for_type<std::optional<B>>();
79 test_hash_enabled_for_type<std::optional<const B>>();
82 return 0;