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;
operator ()my_hash38 std::size_t operator()(const T&) const {return 0;}
39 };
40
41 template <class T> struct my_compare
42 {
43 my_compare() = default;
operator ()my_compare44 bool operator()(const T&, const T&) const {return true;}
45 };
46
47 template <class T> struct my_equal
48 {
49 my_equal() = default;
operator ()my_equal50 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 namespace std
63 {
64 template <> struct hash<Static>
65 {
66 using argument_type = Static;
67 using result_type = std::size_t;
68 hash() = default;
69 std::size_t operator()(const Static&) const;
70 };
71 }
72
73 static_assert(node_compatibility_table<
74 int, int, std::less<int>, std::less<int>, std::hash<int>,
75 std::hash<int>, std::equal_to<int>, std::equal_to<int>,
76 std::allocator<int>,
77 std::allocator<std::pair<const int, int>>>::value,
78 "");
79
80 static_assert(
81 node_compatibility_table<int, int, std::less<int>, my_compare<int>,
82 std::hash<int>, my_hash<int>, std::equal_to<int>,
83 my_equal<int>, std::allocator<int>,
84 std::allocator<std::pair<const int, int>>>::value,
85 "");
86
87 static_assert(node_compatibility_table<
88 Static, int, my_compare<Static>, std::less<Static>,
89 my_hash<Static>, std::hash<Static>, my_equal<Static>,
90 std::equal_to<Static>, min_allocator<Static>,
91 min_allocator<std::pair<const Static, int>>>::value,
92 "");
93
94 template <class Container>
test_node_handle_operations()95 void test_node_handle_operations()
96 {
97 Container c;
98
99 typename Container::node_type nt1, nt2 = c.extract(c.emplace().first);
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
108 template <class Container>
test_node_handle_operations_multi()109 void test_node_handle_operations_multi()
110 {
111 Container c;
112
113 typename Container::node_type nt1, nt2 = c.extract(c.emplace());
114 assert(nt2.get_allocator() == c.get_allocator());
115 assert(!nt2.empty());
116 assert(nt1.empty());
117 std::swap(nt1, nt2);
118 assert(nt1.get_allocator() == c.get_allocator());
119 assert(nt2.empty());
120 }
121
test_typedef()122 template <class> void test_typedef() {}
123
124 template <class Container>
test_insert_return_type()125 void test_insert_return_type()
126 {
127 test_typedef<typename Container::insert_return_type>();
128 }
129
main(int,char **)130 int main(int, char**)
131 {
132 test_node_handle_operations<std::map<int, int>>();
133 test_node_handle_operations_multi<std::multimap<int, int>>();
134 test_node_handle_operations<std::set<int>>();
135 test_node_handle_operations_multi<std::multiset<int>>();
136 test_node_handle_operations<std::unordered_map<int, int>>();
137 test_node_handle_operations_multi<std::unordered_multimap<int, int>>();
138 test_node_handle_operations<std::unordered_set<int>>();
139 test_node_handle_operations_multi<std::unordered_multiset<int>>();
140
141 test_insert_return_type<std::map<int, int>>();
142 test_insert_return_type<std::set<int>>();
143 test_insert_return_type<std::unordered_map<int, int>>();
144 test_insert_return_type<std::unordered_set<int>>();
145
146 return 0;
147 }
148