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
10
11// <unordered_map>
12
13// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
14// class Alloc = allocator<pair<const Key, T>>>
15// class unordered_map
16
17// template <class P,
18// class = typename enable_if<is_convertible<P, value_type>::value>::type>
19// pair<iterator, bool> insert(P&& x);
20
21#include <unordered_map>
22#include <cassert>
23
24#include "test_macros.h"
25#include "MoveOnly.h"
26#include "min_allocator.h"
27
28int main(int, char**) {
29 {
30 typedef std::unordered_map<double, int> C;
31 typedef std::pair<C::iterator, bool> R;
32 typedef std::pair<double, short> P;
33 C c;
34 R r = c.insert(x: P(3.5, static_cast<short>(3)));
35 assert(r.second);
36 assert(c.size() == 1);
37 assert(r.first->first == 3.5);
38 assert(r.first->second == 3);
39
40 r = c.insert(x: P(3.5, static_cast<short>(4)));
41 assert(!r.second);
42 assert(c.size() == 1);
43 assert(r.first->first == 3.5);
44 assert(r.first->second == 3);
45
46 r = c.insert(x: P(4.5, static_cast<short>(4)));
47 assert(r.second);
48 assert(c.size() == 2);
49 assert(r.first->first == 4.5);
50 assert(r.first->second == 4);
51
52 r = c.insert(x: P(5.5, static_cast<short>(4)));
53 assert(r.second);
54 assert(c.size() == 3);
55 assert(r.first->first == 5.5);
56 assert(r.first->second == 4);
57 }
58 {
59 typedef std::unordered_map<MoveOnly, MoveOnly> C;
60 typedef std::pair<C::iterator, bool> R;
61 typedef std::pair<MoveOnly, MoveOnly> P;
62 C c;
63 R r = c.insert(P(3, 3));
64 assert(r.second);
65 assert(c.size() == 1);
66 assert(r.first->first == 3);
67 assert(r.first->second == 3);
68
69 r = c.insert(P(3, 4));
70 assert(!r.second);
71 assert(c.size() == 1);
72 assert(r.first->first == 3);
73 assert(r.first->second == 3);
74
75 r = c.insert(P(4, 4));
76 assert(r.second);
77 assert(c.size() == 2);
78 assert(r.first->first == 4);
79 assert(r.first->second == 4);
80
81 r = c.insert(P(5, 4));
82 assert(r.second);
83 assert(c.size() == 3);
84 assert(r.first->first == 5);
85 assert(r.first->second == 4);
86 }
87 {
88 typedef std::unordered_map<double,
89 int,
90 std::hash<double>,
91 std::equal_to<double>,
92 min_allocator<std::pair<const double, int>>>
93 C;
94 typedef std::pair<C::iterator, bool> R;
95 typedef std::pair<double, short> P;
96 C c;
97 R r = c.insert(P(3.5, static_cast<short>(3)));
98 assert(r.second);
99 assert(c.size() == 1);
100 assert(r.first->first == 3.5);
101 assert(r.first->second == 3);
102
103 r = c.insert(P(3.5, static_cast<short>(4)));
104 assert(!r.second);
105 assert(c.size() == 1);
106 assert(r.first->first == 3.5);
107 assert(r.first->second == 3);
108
109 r = c.insert(P(4.5, static_cast<short>(4)));
110 assert(r.second);
111 assert(c.size() == 2);
112 assert(r.first->first == 4.5);
113 assert(r.first->second == 4);
114
115 r = c.insert(P(5.5, static_cast<short>(4)));
116 assert(r.second);
117 assert(c.size() == 3);
118 assert(r.first->first == 5.5);
119 assert(r.first->second == 4);
120 }
121 {
122 typedef std::unordered_map<MoveOnly,
123 MoveOnly,
124 std::hash<MoveOnly>,
125 std::equal_to<MoveOnly>,
126 min_allocator<std::pair<const MoveOnly, MoveOnly>>>
127 C;
128 typedef std::pair<C::iterator, bool> R;
129 typedef std::pair<MoveOnly, MoveOnly> P;
130 C c;
131 R r = c.insert(P(3, 3));
132 assert(r.second);
133 assert(c.size() == 1);
134 assert(r.first->first == 3);
135 assert(r.first->second == 3);
136
137 r = c.insert(P(3, 4));
138 assert(!r.second);
139 assert(c.size() == 1);
140 assert(r.first->first == 3);
141 assert(r.first->second == 3);
142
143 r = c.insert(P(4, 4));
144 assert(r.second);
145 assert(c.size() == 2);
146 assert(r.first->first == 4);
147 assert(r.first->second == 4);
148
149 r = c.insert(P(5, 4));
150 assert(r.second);
151 assert(c.size() == 3);
152 assert(r.first->first == 5);
153 assert(r.first->second == 4);
154 }
155 {
156 typedef std::unordered_map<double, MoveOnly> C;
157 typedef std::pair<C::iterator, bool> R;
158 C c;
159 R r = c.insert({3.5, 3});
160 assert(r.second);
161 assert(c.size() == 1);
162 assert(r.first->first == 3.5);
163 assert(r.first->second == 3);
164
165 r = c.insert({3.5, 4});
166 assert(!r.second);
167 assert(c.size() == 1);
168 assert(r.first->first == 3.5);
169 assert(r.first->second == 3);
170
171 r = c.insert({4.5, 4});
172 assert(r.second);
173 assert(c.size() == 2);
174 assert(r.first->first == 4.5);
175 assert(r.first->second == 4);
176
177 r = c.insert({5.5, 4});
178 assert(r.second);
179 assert(c.size() == 3);
180 assert(r.first->first == 5.5);
181 assert(r.first->second == 4);
182 }
183
184 return 0;
185}
186

source code of libcxx/test/std/containers/unord/unord.map/unord.map.modifiers/insert_rvalue.pass.cpp