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