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