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
27int main(int, char**)
28{
29 {
30 typedef std::map<int, DefaultOnly> M;
31 typedef std::pair<M::iterator, bool> R;
32 M m;
33 assert(DefaultOnly::count == 0);
34 R r = m.emplace();
35 assert(r.second);
36 assert(r.first == m.begin());
37 assert(m.size() == 1);
38 assert(m.begin()->first == 0);
39 assert(m.begin()->second == DefaultOnly());
40 assert(DefaultOnly::count == 1);
41 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1),
42 std::forward_as_tuple());
43 assert(r.second);
44 assert(r.first == std::next(m.begin()));
45 assert(m.size() == 2);
46 assert(std::next(m.begin())->first == 1);
47 assert(std::next(m.begin())->second == DefaultOnly());
48 assert(DefaultOnly::count == 2);
49 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1),
50 std::forward_as_tuple());
51 assert(!r.second);
52 assert(r.first == std::next(m.begin()));
53 assert(m.size() == 2);
54 assert(std::next(m.begin())->first == 1);
55 assert(std::next(m.begin())->second == DefaultOnly());
56 assert(DefaultOnly::count == 2);
57 }
58 assert(DefaultOnly::count == 0);
59 {
60 typedef std::map<int, Emplaceable> M;
61 typedef std::pair<M::iterator, bool> R;
62 M m;
63 R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 2),
64 std::forward_as_tuple());
65 assert(r.second);
66 assert(r.first == m.begin());
67 assert(m.size() == 1);
68 assert(m.begin()->first == 2);
69 assert(m.begin()->second == Emplaceable());
70 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1),
71 std::forward_as_tuple(args: 2, args: 3.5));
72 assert(r.second);
73 assert(r.first == m.begin());
74 assert(m.size() == 2);
75 assert(m.begin()->first == 1);
76 assert(m.begin()->second == Emplaceable(2, 3.5));
77 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1),
78 std::forward_as_tuple(args: 2, args: 3.5));
79 assert(!r.second);
80 assert(r.first == m.begin());
81 assert(m.size() == 2);
82 assert(m.begin()->first == 1);
83 assert(m.begin()->second == Emplaceable(2, 3.5));
84 }
85 {
86 typedef std::map<int, double> M;
87 typedef std::pair<M::iterator, bool> R;
88 M m;
89 R r = m.emplace(args: M::value_type(2, 3.5));
90 assert(r.second);
91 assert(r.first == m.begin());
92 assert(m.size() == 1);
93 assert(m.begin()->first == 2);
94 assert(m.begin()->second == 3.5);
95 }
96 {
97 typedef std::map<int, DefaultOnly, std::less<int>, min_allocator<std::pair<const int, DefaultOnly>>> M;
98 typedef std::pair<M::iterator, bool> R;
99 M m;
100 assert(DefaultOnly::count == 0);
101 R r = m.emplace();
102 assert(r.second);
103 assert(r.first == m.begin());
104 assert(m.size() == 1);
105 assert(m.begin()->first == 0);
106 assert(m.begin()->second == DefaultOnly());
107 assert(DefaultOnly::count == 1);
108 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1),
109 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 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1),
117 std::forward_as_tuple());
118 assert(!r.second);
119 assert(r.first == std::next(m.begin()));
120 assert(m.size() == 2);
121 assert(std::next(m.begin())->first == 1);
122 assert(std::next(m.begin())->second == DefaultOnly());
123 assert(DefaultOnly::count == 2);
124 }
125 assert(DefaultOnly::count == 0);
126 {
127 typedef std::map<int, Emplaceable, std::less<int>, min_allocator<std::pair<const int, Emplaceable>>> M;
128 typedef std::pair<M::iterator, bool> R;
129 M m;
130 R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 2),
131 std::forward_as_tuple());
132 assert(r.second);
133 assert(r.first == m.begin());
134 assert(m.size() == 1);
135 assert(m.begin()->first == 2);
136 assert(m.begin()->second == Emplaceable());
137 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1),
138 std::forward_as_tuple(args: 2, args: 3.5));
139 assert(r.second);
140 assert(r.first == m.begin());
141 assert(m.size() == 2);
142 assert(m.begin()->first == 1);
143 assert(m.begin()->second == Emplaceable(2, 3.5));
144 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(args: 1),
145 std::forward_as_tuple(args: 2, args: 3.5));
146 assert(!r.second);
147 assert(r.first == m.begin());
148 assert(m.size() == 2);
149 assert(m.begin()->first == 1);
150 assert(m.begin()->second == Emplaceable(2, 3.5));
151 }
152 {
153 typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
154 typedef std::pair<M::iterator, bool> R;
155 M m;
156 R r = m.emplace(M::value_type(2, 3.5));
157 assert(r.second);
158 assert(r.first == m.begin());
159 assert(m.size() == 1);
160 assert(m.begin()->first == 2);
161 assert(m.begin()->second == 3.5);
162 }
163
164 return 0;
165}
166

source code of libcxx/test/std/containers/associative/map/map.modifiers/emplace.pass.cpp