llvm-project
159 строк · 5.9 Кб
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// <map>
10
11// class map
12
13// iterator erase(const_iterator first, const_iterator last);
14
15#include <map>
16#include <cassert>
17
18#include "test_macros.h"
19#include "min_allocator.h"
20
21int main(int, char**)
22{
23{
24typedef std::map<int, double> M;
25typedef std::pair<int, double> P;
26typedef M::iterator I;
27P ar[] =
28{
29P(1, 1.5),
30P(2, 2.5),
31P(3, 3.5),
32P(4, 4.5),
33P(5, 5.5),
34P(6, 6.5),
35P(7, 7.5),
36P(8, 8.5),
37};
38M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
39assert(m.size() == 8);
40I i = m.erase(m.cbegin(), m.cbegin());
41assert(m.size() == 8);
42assert(i == m.begin());
43assert(m.begin()->first == 1);
44assert(m.begin()->second == 1.5);
45assert(std::next(m.begin())->first == 2);
46assert(std::next(m.begin())->second == 2.5);
47assert(std::next(m.begin(), 2)->first == 3);
48assert(std::next(m.begin(), 2)->second == 3.5);
49assert(std::next(m.begin(), 3)->first == 4);
50assert(std::next(m.begin(), 3)->second == 4.5);
51assert(std::next(m.begin(), 4)->first == 5);
52assert(std::next(m.begin(), 4)->second == 5.5);
53assert(std::next(m.begin(), 5)->first == 6);
54assert(std::next(m.begin(), 5)->second == 6.5);
55assert(std::next(m.begin(), 6)->first == 7);
56assert(std::next(m.begin(), 6)->second == 7.5);
57assert(std::next(m.begin(), 7)->first == 8);
58assert(std::next(m.begin(), 7)->second == 8.5);
59
60i = m.erase(m.cbegin(), std::next(m.cbegin(), 2));
61assert(m.size() == 6);
62assert(i == m.begin());
63assert(std::next(m.begin(), 0)->first == 3);
64assert(std::next(m.begin(), 0)->second == 3.5);
65assert(std::next(m.begin(), 1)->first == 4);
66assert(std::next(m.begin(), 1)->second == 4.5);
67assert(std::next(m.begin(), 2)->first == 5);
68assert(std::next(m.begin(), 2)->second == 5.5);
69assert(std::next(m.begin(), 3)->first == 6);
70assert(std::next(m.begin(), 3)->second == 6.5);
71assert(std::next(m.begin(), 4)->first == 7);
72assert(std::next(m.begin(), 4)->second == 7.5);
73assert(std::next(m.begin(), 5)->first == 8);
74assert(std::next(m.begin(), 5)->second == 8.5);
75
76i = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 6));
77assert(m.size() == 2);
78assert(i == std::next(m.begin(), 2));
79assert(std::next(m.begin(), 0)->first == 3);
80assert(std::next(m.begin(), 0)->second == 3.5);
81assert(std::next(m.begin(), 1)->first == 4);
82assert(std::next(m.begin(), 1)->second == 4.5);
83
84i = m.erase(m.cbegin(), m.cend());
85assert(m.size() == 0);
86assert(i == m.begin());
87assert(i == m.end());
88}
89#if TEST_STD_VER >= 11
90{
91typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
92typedef std::pair<int, double> P;
93typedef M::iterator I;
94P ar[] =
95{
96P(1, 1.5),
97P(2, 2.5),
98P(3, 3.5),
99P(4, 4.5),
100P(5, 5.5),
101P(6, 6.5),
102P(7, 7.5),
103P(8, 8.5),
104};
105M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
106assert(m.size() == 8);
107I i = m.erase(m.cbegin(), m.cbegin());
108assert(m.size() == 8);
109assert(i == m.begin());
110assert(m.begin()->first == 1);
111assert(m.begin()->second == 1.5);
112assert(std::next(m.begin())->first == 2);
113assert(std::next(m.begin())->second == 2.5);
114assert(std::next(m.begin(), 2)->first == 3);
115assert(std::next(m.begin(), 2)->second == 3.5);
116assert(std::next(m.begin(), 3)->first == 4);
117assert(std::next(m.begin(), 3)->second == 4.5);
118assert(std::next(m.begin(), 4)->first == 5);
119assert(std::next(m.begin(), 4)->second == 5.5);
120assert(std::next(m.begin(), 5)->first == 6);
121assert(std::next(m.begin(), 5)->second == 6.5);
122assert(std::next(m.begin(), 6)->first == 7);
123assert(std::next(m.begin(), 6)->second == 7.5);
124assert(std::next(m.begin(), 7)->first == 8);
125assert(std::next(m.begin(), 7)->second == 8.5);
126
127i = m.erase(m.cbegin(), std::next(m.cbegin(), 2));
128assert(m.size() == 6);
129assert(i == m.begin());
130assert(std::next(m.begin(), 0)->first == 3);
131assert(std::next(m.begin(), 0)->second == 3.5);
132assert(std::next(m.begin(), 1)->first == 4);
133assert(std::next(m.begin(), 1)->second == 4.5);
134assert(std::next(m.begin(), 2)->first == 5);
135assert(std::next(m.begin(), 2)->second == 5.5);
136assert(std::next(m.begin(), 3)->first == 6);
137assert(std::next(m.begin(), 3)->second == 6.5);
138assert(std::next(m.begin(), 4)->first == 7);
139assert(std::next(m.begin(), 4)->second == 7.5);
140assert(std::next(m.begin(), 5)->first == 8);
141assert(std::next(m.begin(), 5)->second == 8.5);
142
143i = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 6));
144assert(m.size() == 2);
145assert(i == std::next(m.begin(), 2));
146assert(std::next(m.begin(), 0)->first == 3);
147assert(std::next(m.begin(), 0)->second == 3.5);
148assert(std::next(m.begin(), 1)->first == 4);
149assert(std::next(m.begin(), 1)->second == 4.5);
150
151i = m.erase(m.cbegin(), m.cend());
152assert(m.size() == 0);
153assert(i == m.begin());
154assert(i == m.end());
155}
156#endif
157
158return 0;
159}
160