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