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 // <map>
11 
12 // class map
13 
14 // template <class... Args>
15 //   pair<iterator, bool> emplace(Args&&... args);
16 
17 #include <map>
18 #include <cassert>
19 #include <tuple>
20 
21 #include "../../../Emplaceable.h"
22 #include "DefaultOnly.h"
23 #include "min_allocator.h"
24 
main()25 int main()
26 {
27 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
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(1),
41                                                 std::forward_as_tuple());
42         assert(r.second);
43         assert(r.first == next(m.begin()));
44         assert(m.size() == 2);
45         assert(next(m.begin())->first == 1);
46         assert(next(m.begin())->second == DefaultOnly());
47         assert(DefaultOnly::count == 2);
48         r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),
49                                                 std::forward_as_tuple());
50         assert(!r.second);
51         assert(r.first == next(m.begin()));
52         assert(m.size() == 2);
53         assert(next(m.begin())->first == 1);
54         assert(next(m.begin())->second == DefaultOnly());
55         assert(DefaultOnly::count == 2);
56     }
57     assert(DefaultOnly::count == 0);
58     {
59         typedef std::map<int, Emplaceable> M;
60         typedef std::pair<M::iterator, bool> R;
61         M m;
62         R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(2),
63                                                   std::forward_as_tuple());
64         assert(r.second);
65         assert(r.first == m.begin());
66         assert(m.size() == 1);
67         assert(m.begin()->first == 2);
68         assert(m.begin()->second == Emplaceable());
69         r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),
70                                                 std::forward_as_tuple(2, 3.5));
71         assert(r.second);
72         assert(r.first == m.begin());
73         assert(m.size() == 2);
74         assert(m.begin()->first == 1);
75         assert(m.begin()->second == Emplaceable(2, 3.5));
76         r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),
77                                                 std::forward_as_tuple(2, 3.5));
78         assert(!r.second);
79         assert(r.first == m.begin());
80         assert(m.size() == 2);
81         assert(m.begin()->first == 1);
82         assert(m.begin()->second == Emplaceable(2, 3.5));
83     }
84     {
85         typedef std::map<int, double> M;
86         typedef std::pair<M::iterator, bool> R;
87         M m;
88         R r = m.emplace(M::value_type(2, 3.5));
89         assert(r.second);
90         assert(r.first == m.begin());
91         assert(m.size() == 1);
92         assert(m.begin()->first == 2);
93         assert(m.begin()->second == 3.5);
94     }
95 #if __cplusplus >= 201103L
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 #endif
164 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
165 }
166