blob: 91d63cc42171d77d734f10a5e9db9aff353845c0 [file] [log] [blame]
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00006//
7//===----------------------------------------------------------------------===//
8
Louis Dionne31cbe0f2020-06-01 10:38:23 -04009// UNSUPPORTED: c++03, c++11, c++14
Erik Pilkingtonb0386a52018-08-01 01:33:38 +000010
11#include <unordered_set>
12#include <unordered_map>
13#include <set>
14#include <map>
Marshall Clow7fc6a552019-05-31 18:35:30 +000015#include "test_macros.h"
Erik Pilkingtonb0386a52018-08-01 01:33:38 +000016#include "min_allocator.h"
17
18using namespace std;
19
20// [container.node.overview] Table 83.
21template <class K, class T, class C1, class C2, class H1, class H2, class E1, class E2, class A_set, class A_map>
22struct 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
35template <class T> struct my_hash
36{
37 using argument_type = T;
38 using result_type = size_t;
39 my_hash() = default;
40 size_t operator()(const T&) const {return 0;}
41};
42
43template <class T> struct my_compare
44{
45 my_compare() = default;
46 bool operator()(const T&, const T&) const {return true;}
47};
48
49template <class T> struct my_equal
50{
51 my_equal() = default;
52 bool operator()(const T&, const T&) const {return true;}
53};
54
55struct 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
64namespace std
65{
66template <> 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
75static_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
82static_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
89static_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
96template <class Container>
97void 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
110template <class Container>
111void 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
Billy Robert O'Neal III77e898c2018-08-08 04:24:47 +0000124template <class> void test_typedef() {}
125
Erik Pilkingtonb0386a52018-08-01 01:33:38 +0000126template <class Container>
127void test_insert_return_type()
128{
Billy Robert O'Neal III77e898c2018-08-08 04:24:47 +0000129 test_typedef<typename Container::insert_return_type>();
Erik Pilkingtonb0386a52018-08-01 01:33:38 +0000130}
131
JF Bastien2df59c52019-02-04 20:31:13 +0000132int main(int, char**)
Erik Pilkingtonb0386a52018-08-01 01:33:38 +0000133{
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>>();
JF Bastien2df59c52019-02-04 20:31:13 +0000147
148 return 0;
Erik Pilkingtonb0386a52018-08-01 01:33:38 +0000149}