node_handle.pass.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. #include <unordered_set>
  10. #include <unordered_map>
  11. #include <set>
  12. #include <map>
  13. #include "test_macros.h"
  14. #include "min_allocator.h"
  15. using namespace std;
  16. // [container.node.overview] Table 83.
  17. template <class K, class T, class C1, class C2, class H1, class H2, class E1, class E2, class A_set, class A_map>
  18. struct node_compatibility_table
  19. {
  20. static constexpr bool value =
  21. is_same_v<typename map<K, T, C1, A_map>::node_type, typename map<K, T, C2, A_map>::node_type> &&
  22. is_same_v<typename map<K, T, C1, A_map>::node_type, typename multimap<K, T, C2, A_map>::node_type> &&
  23. is_same_v<typename set<K, C1, A_set>::node_type, typename set<K, C2, A_set>::node_type> &&
  24. is_same_v<typename set<K, C1, A_set>::node_type, typename multiset<K, C2, A_set>::node_type> &&
  25. is_same_v<typename unordered_map<K, T, H1, E1, A_map>::node_type, typename unordered_map<K, T, H2, E2, A_map>::node_type> &&
  26. is_same_v<typename unordered_map<K, T, H1, E1, A_map>::node_type, typename unordered_multimap<K, T, H2, E2, A_map>::node_type> &&
  27. is_same_v<typename unordered_set<K, H1, E1, A_set>::node_type, typename unordered_set<K, H2, E2, A_set>::node_type> &&
  28. is_same_v<typename unordered_set<K, H1, E1, A_set>::node_type, typename unordered_multiset<K, H2, E2, A_set>::node_type>;
  29. };
  30. template <class T> struct my_hash
  31. {
  32. using argument_type = T;
  33. using result_type = size_t;
  34. my_hash() = default;
  35. size_t operator()(const T&) const {return 0;}
  36. };
  37. template <class T> struct my_compare
  38. {
  39. my_compare() = default;
  40. bool operator()(const T&, const T&) const {return true;}
  41. };
  42. template <class T> struct my_equal
  43. {
  44. my_equal() = default;
  45. bool operator()(const T&, const T&) const {return true;}
  46. };
  47. struct Static
  48. {
  49. Static() = default;
  50. Static(const Static&) = delete;
  51. Static(Static&&) = delete;
  52. Static& operator=(const Static&) = delete;
  53. Static& operator=(Static&&) = delete;
  54. };
  55. namespace std
  56. {
  57. template <> struct hash<Static>
  58. {
  59. using argument_type = Static;
  60. using result_type = size_t;
  61. hash() = default;
  62. size_t operator()(const Static&) const;
  63. };
  64. }
  65. static_assert(node_compatibility_table<
  66. int, int, std::less<int>, std::less<int>, std::hash<int>,
  67. std::hash<int>, std::equal_to<int>, std::equal_to<int>,
  68. std::allocator<int>,
  69. std::allocator<std::pair<const int, int>>>::value,
  70. "");
  71. static_assert(
  72. node_compatibility_table<int, int, std::less<int>, my_compare<int>,
  73. std::hash<int>, my_hash<int>, std::equal_to<int>,
  74. my_equal<int>, allocator<int>,
  75. allocator<std::pair<const int, int>>>::value,
  76. "");
  77. static_assert(node_compatibility_table<
  78. Static, int, my_compare<Static>, std::less<Static>,
  79. my_hash<Static>, std::hash<Static>, my_equal<Static>,
  80. std::equal_to<Static>, min_allocator<Static>,
  81. min_allocator<std::pair<const Static, int>>>::value,
  82. "");
  83. template <class Container>
  84. void test_node_handle_operations()
  85. {
  86. Container c;
  87. typename Container::node_type nt1, nt2 = c.extract(c.emplace().first);
  88. assert(nt2.get_allocator() == c.get_allocator());
  89. assert(!nt2.empty());
  90. assert(nt1.empty());
  91. std::swap(nt1, nt2);
  92. assert(nt1.get_allocator() == c.get_allocator());
  93. assert(nt2.empty());
  94. }
  95. template <class Container>
  96. void test_node_handle_operations_multi()
  97. {
  98. Container c;
  99. typename Container::node_type nt1, nt2 = c.extract(c.emplace());
  100. assert(nt2.get_allocator() == c.get_allocator());
  101. assert(!nt2.empty());
  102. assert(nt1.empty());
  103. std::swap(nt1, nt2);
  104. assert(nt1.get_allocator() == c.get_allocator());
  105. assert(nt2.empty());
  106. }
  107. template <class> void test_typedef() {}
  108. template <class Container>
  109. void test_insert_return_type()
  110. {
  111. test_typedef<typename Container::insert_return_type>();
  112. }
  113. int main(int, char**)
  114. {
  115. test_node_handle_operations<std::map<int, int>>();
  116. test_node_handle_operations_multi<std::multimap<int, int>>();
  117. test_node_handle_operations<std::set<int>>();
  118. test_node_handle_operations_multi<std::multiset<int>>();
  119. test_node_handle_operations<std::unordered_map<int, int>>();
  120. test_node_handle_operations_multi<std::unordered_multimap<int, int>>();
  121. test_node_handle_operations<std::unordered_set<int>>();
  122. test_node_handle_operations_multi<std::unordered_multiset<int>>();
  123. test_insert_return_type<std::map<int, int>>();
  124. test_insert_return_type<std::set<int>>();
  125. test_insert_return_type<std::unordered_map<int, int>>();
  126. test_insert_return_type<std::unordered_set<int>>();
  127. return 0;
  128. }