llvm-project
108 строк · 3.0 Кб
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
26template <class Container, class Pair>
27void do_insert_rv_test()
28{
29typedef Container M;
30typedef Pair P;
31typedef std::pair<typename M::iterator, bool> R;
32M m;
33R r = m.insert(P(2, 2));
34assert(r.second);
35assert(r.first == m.begin());
36assert(m.size() == 1);
37assert(r.first->first == 2);
38assert(r.first->second == 2);
39
40r = m.insert(P(1, 1));
41assert(r.second);
42assert(r.first == m.begin());
43assert(m.size() == 2);
44assert(r.first->first == 1);
45assert(r.first->second == 1);
46
47r = m.insert(P(3, 3));
48assert(r.second);
49assert(r.first == std::prev(m.end()));
50assert(m.size() == 3);
51assert(r.first->first == 3);
52assert(r.first->second == 3);
53
54r = m.insert(P(3, 3));
55assert(!r.second);
56assert(r.first == std::prev(m.end()));
57assert(m.size() == 3);
58assert(r.first->first == 3);
59assert(r.first->second == 3);
60}
61
62int main(int, char**)
63{
64do_insert_rv_test<std::map<int, MoveOnly>, std::pair<int, MoveOnly>>();
65do_insert_rv_test<std::map<int, MoveOnly>, std::pair<const int, MoveOnly>>();
66
67{
68typedef std::map<int, MoveOnly, std::less<int>, min_allocator<std::pair<const int, MoveOnly>>> M;
69typedef std::pair<int, MoveOnly> P;
70typedef std::pair<const int, MoveOnly> CP;
71do_insert_rv_test<M, P>();
72do_insert_rv_test<M, CP>();
73}
74{
75typedef std::map<int, MoveOnly> M;
76typedef std::pair<M::iterator, bool> R;
77M m;
78R r = m.insert({2, MoveOnly(2)});
79assert(r.second);
80assert(r.first == m.begin());
81assert(m.size() == 1);
82assert(r.first->first == 2);
83assert(r.first->second == 2);
84
85r = m.insert({1, MoveOnly(1)});
86assert(r.second);
87assert(r.first == m.begin());
88assert(m.size() == 2);
89assert(r.first->first == 1);
90assert(r.first->second == 1);
91
92r = m.insert({3, MoveOnly(3)});
93assert(r.second);
94assert(r.first == std::prev(m.end()));
95assert(m.size() == 3);
96assert(r.first->first == 3);
97assert(r.first->second == 3);
98
99r = m.insert({3, MoveOnly(3)});
100assert(!r.second);
101assert(r.first == std::prev(m.end()));
102assert(m.size() == 3);
103assert(r.first->first == 3);
104assert(r.first->second == 3);
105}
106
107return 0;
108}
109