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 | // <map> |
12 | |
13 | // class map |
14 | |
15 | // template <class... Args> |
16 | // pair<iterator, bool> emplace(Args&&... args); |
17 | |
18 | #include <map> |
19 | #include <cassert> |
20 | #include <tuple> |
21 | |
22 | #include "test_macros.h" |
23 | #include "../../../Emplaceable.h" |
24 | #include "DefaultOnly.h" |
25 | #include "min_allocator.h" |
26 | |
27 | int main(int, char**) { |
28 | { |
29 | typedef std::map<int, DefaultOnly> M; |
30 | typedef std::pair<M::iterator, bool> R; |
31 | M m; |
32 | assert(DefaultOnly::count == 0); |
33 | R r = m.emplace(); |
34 | assert(r.second); |
35 | assert(r.first == m.begin()); |
36 | assert(m.size() == 1); |
37 | assert(m.begin()->first == 0); |
38 | assert(m.begin()->second == DefaultOnly()); |
39 | assert(DefaultOnly::count == 1); |
40 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1), std::forward_as_tuple()); |
41 | assert(r.second); |
42 | assert(r.first == std::next(m.begin())); |
43 | assert(m.size() == 2); |
44 | assert(std::next(m.begin())->first == 1); |
45 | assert(std::next(m.begin())->second == DefaultOnly()); |
46 | assert(DefaultOnly::count == 2); |
47 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1), std::forward_as_tuple()); |
48 | assert(!r.second); |
49 | assert(r.first == std::next(m.begin())); |
50 | assert(m.size() == 2); |
51 | assert(std::next(m.begin())->first == 1); |
52 | assert(std::next(m.begin())->second == DefaultOnly()); |
53 | assert(DefaultOnly::count == 2); |
54 | } |
55 | assert(DefaultOnly::count == 0); |
56 | { |
57 | typedef std::map<int, Emplaceable> M; |
58 | typedef std::pair<M::iterator, bool> R; |
59 | M m; |
60 | R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 2), std::forward_as_tuple()); |
61 | assert(r.second); |
62 | assert(r.first == m.begin()); |
63 | assert(m.size() == 1); |
64 | assert(m.begin()->first == 2); |
65 | assert(m.begin()->second == Emplaceable()); |
66 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1), std::forward_as_tuple(args: 2, args: 3.5)); |
67 | assert(r.second); |
68 | assert(r.first == m.begin()); |
69 | assert(m.size() == 2); |
70 | assert(m.begin()->first == 1); |
71 | assert(m.begin()->second == Emplaceable(2, 3.5)); |
72 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1), std::forward_as_tuple(args: 2, args: 3.5)); |
73 | assert(!r.second); |
74 | assert(r.first == m.begin()); |
75 | assert(m.size() == 2); |
76 | assert(m.begin()->first == 1); |
77 | assert(m.begin()->second == Emplaceable(2, 3.5)); |
78 | } |
79 | { |
80 | typedef std::map<int, double> M; |
81 | typedef std::pair<M::iterator, bool> R; |
82 | M m; |
83 | R r = m.emplace(args: M::value_type(2, 3.5)); |
84 | assert(r.second); |
85 | assert(r.first == m.begin()); |
86 | assert(m.size() == 1); |
87 | assert(m.begin()->first == 2); |
88 | assert(m.begin()->second == 3.5); |
89 | } |
90 | { |
91 | typedef std::map<int, DefaultOnly, std::less<int>, min_allocator<std::pair<const int, DefaultOnly>>> M; |
92 | typedef std::pair<M::iterator, bool> R; |
93 | M m; |
94 | assert(DefaultOnly::count == 0); |
95 | R r = m.emplace(); |
96 | assert(r.second); |
97 | assert(r.first == m.begin()); |
98 | assert(m.size() == 1); |
99 | assert(m.begin()->first == 0); |
100 | assert(m.begin()->second == DefaultOnly()); |
101 | assert(DefaultOnly::count == 1); |
102 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1), std::forward_as_tuple()); |
103 | assert(r.second); |
104 | assert(r.first == std::next(m.begin())); |
105 | assert(m.size() == 2); |
106 | assert(std::next(m.begin())->first == 1); |
107 | assert(std::next(m.begin())->second == DefaultOnly()); |
108 | assert(DefaultOnly::count == 2); |
109 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1), std::forward_as_tuple()); |
110 | assert(!r.second); |
111 | assert(r.first == std::next(m.begin())); |
112 | assert(m.size() == 2); |
113 | assert(std::next(m.begin())->first == 1); |
114 | assert(std::next(m.begin())->second == DefaultOnly()); |
115 | assert(DefaultOnly::count == 2); |
116 | } |
117 | assert(DefaultOnly::count == 0); |
118 | { |
119 | typedef std::map<int, Emplaceable, std::less<int>, min_allocator<std::pair<const int, Emplaceable>>> M; |
120 | typedef std::pair<M::iterator, bool> R; |
121 | M m; |
122 | R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 2), std::forward_as_tuple()); |
123 | assert(r.second); |
124 | assert(r.first == m.begin()); |
125 | assert(m.size() == 1); |
126 | assert(m.begin()->first == 2); |
127 | assert(m.begin()->second == Emplaceable()); |
128 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1), std::forward_as_tuple(args: 2, args: 3.5)); |
129 | assert(r.second); |
130 | assert(r.first == m.begin()); |
131 | assert(m.size() == 2); |
132 | assert(m.begin()->first == 1); |
133 | assert(m.begin()->second == Emplaceable(2, 3.5)); |
134 | r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1), std::forward_as_tuple(args: 2, args: 3.5)); |
135 | assert(!r.second); |
136 | assert(r.first == m.begin()); |
137 | assert(m.size() == 2); |
138 | assert(m.begin()->first == 1); |
139 | assert(m.begin()->second == Emplaceable(2, 3.5)); |
140 | } |
141 | { |
142 | typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M; |
143 | typedef std::pair<M::iterator, bool> R; |
144 | M m; |
145 | R r = m.emplace(M::value_type(2, 3.5)); |
146 | assert(r.second); |
147 | assert(r.first == m.begin()); |
148 | assert(m.size() == 1); |
149 | assert(m.begin()->first == 2); |
150 | assert(m.begin()->second == 3.5); |
151 | } |
152 | |
153 | return 0; |
154 | } |
155 | |