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