1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 // Throwing bad_variant_access is supported starting in macosx10.13
13 // XFAIL: with_system_cxx_lib=macosx10.12 && !no-exceptions
14 // XFAIL: with_system_cxx_lib=macosx10.11 && !no-exceptions
15 // XFAIL: with_system_cxx_lib=macosx10.10 && !no-exceptions
16 // XFAIL: with_system_cxx_lib=macosx10.9 && !no-exceptions
17 
18 // <variant>
19 
20 // template <class... Types> struct hash<variant<Types...>>;
21 // template <> struct hash<monostate>;
22 
23 #include <cassert>
24 #include <type_traits>
25 #include <variant>
26 
27 #include "test_macros.h"
28 #include "variant_test_helpers.h"
29 #include "poisoned_hash_helper.h"
30 
31 #ifndef TEST_HAS_NO_EXCEPTIONS
32 namespace std {
33 template <> struct hash<::MakeEmptyT> {
34   size_t operator()(const ::MakeEmptyT &) const {
35     assert(false);
36     return 0;
37   }
38 };
39 }
40 #endif
41 
42 void test_hash_variant() {
43   {
44     using V = std::variant<int, long, int>;
45     using H = std::hash<V>;
46     const V v(std::in_place_index<0>, 42);
47     const V v_copy = v;
48     V v2(std::in_place_index<0>, 100);
49     const H h{};
50     assert(h(v) == h(v));
51     assert(h(v) != h(v2));
52     assert(h(v) == h(v_copy));
53     {
54       ASSERT_SAME_TYPE(decltype(h(v)), std::size_t);
55       static_assert(std::is_copy_constructible<H>::value, "");
56     }
57   }
58   {
59     using V = std::variant<std::monostate, int, long, const char *>;
60     using H = std::hash<V>;
61     const char *str = "hello";
62     const V v0;
63     const V v0_other;
64     const V v1(42);
65     const V v1_other(100);
66     V v2(100l);
67     V v2_other(999l);
68     V v3(str);
69     V v3_other("not hello");
70     const H h{};
71     assert(h(v0) == h(v0));
72     assert(h(v0) == h(v0_other));
73     assert(h(v1) == h(v1));
74     assert(h(v1) != h(v1_other));
75     assert(h(v2) == h(v2));
76     assert(h(v2) != h(v2_other));
77     assert(h(v3) == h(v3));
78     assert(h(v3) != h(v3_other));
79     assert(h(v0) != h(v1));
80     assert(h(v0) != h(v2));
81     assert(h(v0) != h(v3));
82     assert(h(v1) != h(v2));
83     assert(h(v1) != h(v3));
84     assert(h(v2) != h(v3));
85   }
86 #ifndef TEST_HAS_NO_EXCEPTIONS
87   {
88     using V = std::variant<int, MakeEmptyT>;
89     using H = std::hash<V>;
90     V v;
91     makeEmpty(v);
92     V v2;
93     makeEmpty(v2);
94     const H h{};
95     assert(h(v) == h(v2));
96   }
97 #endif
98 }
99 
100 void test_hash_monostate() {
101   using H = std::hash<std::monostate>;
102   const H h{};
103   std::monostate m1{};
104   const std::monostate m2{};
105   assert(h(m1) == h(m1));
106   assert(h(m2) == h(m2));
107   assert(h(m1) == h(m2));
108   {
109     ASSERT_SAME_TYPE(decltype(h(m1)), std::size_t);
110     ASSERT_NOEXCEPT(h(m1));
111     static_assert(std::is_copy_constructible<H>::value, "");
112   }
113   {
114     test_hash_enabled_for_type<std::monostate>();
115   }
116 }
117 
118 void test_hash_variant_duplicate_elements() {
119     // Test that the index of the alternative participates in the hash value.
120     using V = std::variant<std::monostate, std::monostate>;
121     using H = std::hash<V>;
122     H h{};
123     const V v1(std::in_place_index<0>);
124     const V v2(std::in_place_index<1>);
125     assert(h(v1) == h(v1));
126     assert(h(v2) == h(v2));
127     LIBCPP_ASSERT(h(v1) != h(v2));
128 }
129 
130 struct A {};
131 struct B {};
132 
133 namespace std {
134 
135 template <>
136 struct hash<B> {
137   size_t operator()(B const&) const {
138     return 0;
139   }
140 };
141 
142 }
143 
144 void test_hash_variant_enabled() {
145   {
146     test_hash_enabled_for_type<std::variant<int> >();
147     test_hash_enabled_for_type<std::variant<int*, long, double, const int> >();
148   }
149   {
150     test_hash_disabled_for_type<std::variant<int, A>>();
151     test_hash_disabled_for_type<std::variant<const A, void*>>();
152   }
153   {
154     test_hash_enabled_for_type<std::variant<int, B>>();
155     test_hash_enabled_for_type<std::variant<const B, int>>();
156   }
157 }
158 
159 int main(int, char**) {
160   test_hash_variant();
161   test_hash_variant_duplicate_elements();
162   test_hash_monostate();
163   test_hash_variant_enabled();
164 
165   return 0;
166 }
167