hash.pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <variant>
  11. // template <class... Types> struct hash<variant<Types...>>;
  12. // template <> struct hash<monostate>;
  13. #include <cassert>
  14. #include <type_traits>
  15. #include <variant>
  16. #include "test_macros.h"
  17. #include "variant_test_helpers.h"
  18. #include "poisoned_hash_helper.h"
  19. #ifndef TEST_HAS_NO_EXCEPTIONS
  20. namespace std {
  21. template <> struct hash<::MakeEmptyT> {
  22. size_t operator()(const ::MakeEmptyT &) const {
  23. assert(false);
  24. return 0;
  25. }
  26. };
  27. }
  28. #endif
  29. void test_hash_variant() {
  30. {
  31. using V = std::variant<int, long, int>;
  32. using H = std::hash<V>;
  33. const V v(std::in_place_index<0>, 42);
  34. const V v_copy = v;
  35. V v2(std::in_place_index<0>, 100);
  36. const H h{};
  37. assert(h(v) == h(v));
  38. assert(h(v) != h(v2));
  39. assert(h(v) == h(v_copy));
  40. {
  41. ASSERT_SAME_TYPE(decltype(h(v)), std::size_t);
  42. static_assert(std::is_copy_constructible<H>::value, "");
  43. }
  44. }
  45. {
  46. using V = std::variant<std::monostate, int, long, const char *>;
  47. using H = std::hash<V>;
  48. const char *str = "hello";
  49. const V v0;
  50. const V v0_other;
  51. const V v1(42);
  52. const V v1_other(100);
  53. V v2(100l);
  54. V v2_other(999l);
  55. V v3(str);
  56. V v3_other("not hello");
  57. const H h{};
  58. assert(h(v0) == h(v0));
  59. assert(h(v0) == h(v0_other));
  60. assert(h(v1) == h(v1));
  61. assert(h(v1) != h(v1_other));
  62. assert(h(v2) == h(v2));
  63. assert(h(v2) != h(v2_other));
  64. assert(h(v3) == h(v3));
  65. assert(h(v3) != h(v3_other));
  66. assert(h(v0) != h(v1));
  67. assert(h(v0) != h(v2));
  68. assert(h(v0) != h(v3));
  69. assert(h(v1) != h(v2));
  70. assert(h(v1) != h(v3));
  71. assert(h(v2) != h(v3));
  72. }
  73. #ifndef TEST_HAS_NO_EXCEPTIONS
  74. {
  75. using V = std::variant<int, MakeEmptyT>;
  76. using H = std::hash<V>;
  77. V v;
  78. makeEmpty(v);
  79. V v2;
  80. makeEmpty(v2);
  81. const H h{};
  82. assert(h(v) == h(v2));
  83. }
  84. #endif
  85. }
  86. void test_hash_monostate() {
  87. using H = std::hash<std::monostate>;
  88. const H h{};
  89. std::monostate m1{};
  90. const std::monostate m2{};
  91. assert(h(m1) == h(m1));
  92. assert(h(m2) == h(m2));
  93. assert(h(m1) == h(m2));
  94. {
  95. ASSERT_SAME_TYPE(decltype(h(m1)), std::size_t);
  96. ASSERT_NOEXCEPT(h(m1));
  97. static_assert(std::is_copy_constructible<H>::value, "");
  98. }
  99. {
  100. test_hash_enabled_for_type<std::monostate>();
  101. }
  102. }
  103. void test_hash_variant_duplicate_elements() {
  104. // Test that the index of the alternative participates in the hash value.
  105. using V = std::variant<std::monostate, std::monostate>;
  106. using H = std::hash<V>;
  107. H h{};
  108. const V v1(std::in_place_index<0>);
  109. const V v2(std::in_place_index<1>);
  110. assert(h(v1) == h(v1));
  111. assert(h(v2) == h(v2));
  112. LIBCPP_ASSERT(h(v1) != h(v2));
  113. }
  114. struct A {};
  115. struct B {};
  116. namespace std {
  117. template <>
  118. struct hash<B> {
  119. size_t operator()(B const&) const {
  120. return 0;
  121. }
  122. };
  123. }
  124. void test_hash_variant_enabled() {
  125. {
  126. test_hash_enabled_for_type<std::variant<int> >();
  127. test_hash_enabled_for_type<std::variant<int*, long, double, const int> >();
  128. }
  129. {
  130. test_hash_disabled_for_type<std::variant<int, A>>();
  131. test_hash_disabled_for_type<std::variant<const A, void*>>();
  132. }
  133. {
  134. test_hash_enabled_for_type<std::variant<int, B>>();
  135. test_hash_enabled_for_type<std::variant<const B, int>>();
  136. }
  137. }
  138. int main(int, char**) {
  139. test_hash_variant();
  140. test_hash_variant_duplicate_elements();
  141. test_hash_monostate();
  142. test_hash_variant_enabled();
  143. return 0;
  144. }