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 // pair<iterator, bool> insert( value_type&& v);  // C++17 and later
17 // template <class P>
18 //   pair<iterator, bool> insert(P&& p);
19 
20 #include <map>
21 #include <cassert>
22 
23 #include "MoveOnly.h"
24 #include "min_allocator.h"
25 #include "test_macros.h"
26 
27 template <class Container, class Pair>
do_insert_rv_test()28 void do_insert_rv_test()
29 {
30     typedef Container M;
31     typedef Pair P;
32     typedef std::pair<typename M::iterator, bool> R;
33     M m;
34     R r = m.insert(P(2, 2));
35     assert(r.second);
36     assert(r.first == m.begin());
37     assert(m.size() == 1);
38     assert(r.first->first == 2);
39     assert(r.first->second == 2);
40 
41     r = m.insert(P(1, 1));
42     assert(r.second);
43     assert(r.first == m.begin());
44     assert(m.size() == 2);
45     assert(r.first->first == 1);
46     assert(r.first->second == 1);
47 
48     r = m.insert(P(3, 3));
49     assert(r.second);
50     assert(r.first == prev(m.end()));
51     assert(m.size() == 3);
52     assert(r.first->first == 3);
53     assert(r.first->second == 3);
54 
55     r = m.insert(P(3, 3));
56     assert(!r.second);
57     assert(r.first == prev(m.end()));
58     assert(m.size() == 3);
59     assert(r.first->first == 3);
60     assert(r.first->second == 3);
61 }
62 
main()63 int main()
64 {
65     do_insert_rv_test<std::map<int, MoveOnly>, std::pair<int, MoveOnly>>();
66     do_insert_rv_test<std::map<int, MoveOnly>, std::pair<const int, MoveOnly>>();
67 
68     {
69         typedef std::map<int, MoveOnly, std::less<int>, min_allocator<std::pair<const int, MoveOnly>>> M;
70         typedef std::pair<int, MoveOnly> P;
71         typedef std::pair<const int, MoveOnly> CP;
72         do_insert_rv_test<M, P>();
73         do_insert_rv_test<M, CP>();
74     }
75     {
76         typedef std::map<int, MoveOnly> M;
77         typedef std::pair<M::iterator, bool> R;
78         M m;
79         R r = m.insert({2, MoveOnly(2)});
80         assert(r.second);
81         assert(r.first == m.begin());
82         assert(m.size() == 1);
83         assert(r.first->first == 2);
84         assert(r.first->second == 2);
85 
86         r = m.insert({1, MoveOnly(1)});
87         assert(r.second);
88         assert(r.first == m.begin());
89         assert(m.size() == 2);
90         assert(r.first->first == 1);
91         assert(r.first->second == 1);
92 
93         r = m.insert({3, MoveOnly(3)});
94         assert(r.second);
95         assert(r.first == prev(m.end()));
96         assert(m.size() == 3);
97         assert(r.first->first == 3);
98         assert(r.first->second == 3);
99 
100         r = m.insert({3, MoveOnly(3)});
101         assert(!r.second);
102         assert(r.first == prev(m.end()));
103         assert(m.size() == 3);
104         assert(r.first->first == 3);
105         assert(r.first->second == 3);
106     }
107 }
108