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