llvm-project
185 строк · 6.5 Кб
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, c++11, c++14
10
11// <map>
12
13// class map
14
15// template <class... Args>
16// pair<iterator, bool> try_emplace(const key_type& k, Args&&... args); // C++17
17// template <class... Args>
18// pair<iterator, bool> try_emplace(key_type&& k, Args&&... args); // C++17
19// template <class... Args>
20// iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args); // C++17
21// template <class... Args>
22// iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args); // C++17
23
24#include <map>
25#include <cassert>
26#include <tuple>
27
28#include "test_macros.h"
29
30class Moveable
31{
32Moveable(const Moveable&);
33Moveable& operator=(const Moveable&);
34
35int int_;
36double double_;
37public:
38Moveable() : int_(0), double_(0) {}
39Moveable(int i, double d) : int_(i), double_(d) {}
40Moveable(Moveable&& x)
41: int_(x.int_), double_(x.double_)
42{x.int_ = -1; x.double_ = -1;}
43Moveable& operator=(Moveable&& x)
44{int_ = x.int_; x.int_ = -1;
45double_ = x.double_; x.double_ = -1;
46return *this;
47}
48
49bool operator==(const Moveable& x) const
50{return int_ == x.int_ && double_ == x.double_;}
51bool operator<(const Moveable& x) const
52{return int_ < x.int_ || (int_ == x.int_ && double_ < x.double_);}
53
54int get() const {return int_;}
55bool moved() const {return int_ == -1;}
56};
57
58
59int main(int, char**)
60{
61{ // pair<iterator, bool> try_emplace(const key_type& k, Args&&... args);
62typedef std::map<int, Moveable> M;
63typedef std::pair<M::iterator, bool> R;
64M m;
65R r;
66for (int i = 0; i < 20; i += 2)
67m.emplace (i, Moveable(i, (double) i));
68assert(m.size() == 10);
69
70Moveable mv1(3, 3.0);
71for (int i=0; i < 20; i += 2)
72{
73r = m.try_emplace(i, std::move(mv1));
74assert(m.size() == 10);
75assert(!r.second); // was not inserted
76assert(!mv1.moved()); // was not moved from
77assert(r.first->first == i); // key
78}
79
80r = m.try_emplace(-1, std::move(mv1));
81assert(m.size() == 11);
82assert(r.second); // was inserted
83assert(mv1.moved()); // was moved from
84assert(r.first->first == -1); // key
85assert(r.first->second.get() == 3); // value
86
87Moveable mv2(5, 3.0);
88r = m.try_emplace(5, std::move(mv2));
89assert(m.size() == 12);
90assert(r.second); // was inserted
91assert(mv2.moved()); // was moved from
92assert(r.first->first == 5); // key
93assert(r.first->second.get() == 5); // value
94
95Moveable mv3(-1, 3.0);
96r = m.try_emplace(117, std::move(mv2));
97assert(m.size() == 13);
98assert(r.second); // was inserted
99assert(mv2.moved()); // was moved from
100assert(r.first->first == 117); // key
101assert(r.first->second.get() == -1); // value
102}
103
104{ // pair<iterator, bool> try_emplace(key_type&& k, Args&&... args);
105typedef std::map<Moveable, Moveable> M;
106typedef std::pair<M::iterator, bool> R;
107M m;
108R r;
109for ( int i = 0; i < 20; i += 2 )
110m.emplace ( Moveable(i, (double) i), Moveable(i+1, (double) i+1));
111assert(m.size() == 10);
112
113Moveable mvkey1(2, 2.0);
114Moveable mv1(4, 4.0);
115r = m.try_emplace(std::move(mvkey1), std::move(mv1));
116assert(m.size() == 10);
117assert(!r.second); // was not inserted
118assert(!mv1.moved()); // was not moved from
119assert(!mvkey1.moved()); // was not moved from
120assert(r.first->first == mvkey1); // key
121
122Moveable mvkey2(3, 3.0);
123r = m.try_emplace(std::move(mvkey2), std::move(mv1));
124assert(m.size() == 11);
125assert(r.second); // was inserted
126assert(mv1.moved()); // was moved from
127assert(mvkey2.moved()); // was moved from
128assert(r.first->first.get() == 3); // key
129assert(r.first->second.get() == 4); // value
130}
131
132{ // iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args);
133typedef std::map<int, Moveable> M;
134M m;
135M::iterator r;
136for ( int i = 0; i < 20; i += 2 )
137m.try_emplace ( i, Moveable(i, (double) i));
138assert(m.size() == 10);
139M::const_iterator it = m.find(2);
140
141Moveable mv1(3, 3.0);
142for (int i=0; i < 20; i += 2)
143{
144r = m.try_emplace(it, i, std::move(mv1));
145assert(m.size() == 10);
146assert(!mv1.moved()); // was not moved from
147assert(r->first == i); // key
148assert(r->second.get() == i); // value
149}
150
151r = m.try_emplace(it, 3, std::move(mv1));
152assert(m.size() == 11);
153assert(mv1.moved()); // was moved from
154assert(r->first == 3); // key
155assert(r->second.get() == 3); // value
156}
157
158{ // iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args);
159typedef std::map<Moveable, Moveable> M;
160M m;
161M::iterator r;
162for ( int i = 0; i < 20; i += 2 )
163m.emplace ( Moveable(i, (double) i), Moveable(i+1, (double) i+1));
164assert(m.size() == 10);
165M::const_iterator it = std::next(m.cbegin());
166
167Moveable mvkey1(2, 2.0);
168Moveable mv1(4, 4.0);
169r = m.try_emplace(it, std::move(mvkey1), std::move(mv1));
170assert(m.size() == 10);
171assert(!mv1.moved()); // was not moved from
172assert(!mvkey1.moved()); // was not moved from
173assert(r->first == mvkey1); // key
174
175Moveable mvkey2(3, 3.0);
176r = m.try_emplace(it, std::move(mvkey2), std::move(mv1));
177assert(m.size() == 11);
178assert(mv1.moved()); // was moved from
179assert(mvkey2.moved()); // was moved from
180assert(r->first.get() == 3); // key
181assert(r->second.get() == 4); // value
182}
183
184return 0;
185}
186