1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03
11
12 // <map>
13
14 // class map
15
16 // template <class... Args>
17 // pair<iterator, bool> emplace(Args&&... args);
18
19 #include <map>
20 #include <cassert>
21 #include <tuple>
22
23 #include "../../../Emplaceable.h"
24 #include "DefaultOnly.h"
25 #include "min_allocator.h"
26
main()27 int main()
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(1),
42 std::forward_as_tuple());
43 assert(r.second);
44 assert(r.first == next(m.begin()));
45 assert(m.size() == 2);
46 assert(next(m.begin())->first == 1);
47 assert(next(m.begin())->second == DefaultOnly());
48 assert(DefaultOnly::count == 2);
49 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),
50 std::forward_as_tuple());
51 assert(!r.second);
52 assert(r.first == next(m.begin()));
53 assert(m.size() == 2);
54 assert(next(m.begin())->first == 1);
55 assert(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(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(1),
71 std::forward_as_tuple(2, 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(1),
78 std::forward_as_tuple(2, 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(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(1),
109 std::forward_as_tuple());
110 assert(r.second);
111 assert(r.first == next(m.begin()));
112 assert(m.size() == 2);
113 assert(next(m.begin())->first == 1);
114 assert(next(m.begin())->second == DefaultOnly());
115 assert(DefaultOnly::count == 2);
116 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),
117 std::forward_as_tuple());
118 assert(!r.second);
119 assert(r.first == next(m.begin()));
120 assert(m.size() == 2);
121 assert(next(m.begin())->first == 1);
122 assert(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(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(1),
138 std::forward_as_tuple(2, 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(1),
145 std::forward_as_tuple(2, 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