1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
13 // iterator erase(const_iterator first, const_iterator last);
18 #include "test_macros.h"
19 #include "min_allocator.h"
24 typedef std::map
<int, double> M
;
25 typedef std::pair
<int, double> P
;
26 typedef M::iterator I
;
38 M
m(ar
, ar
+ sizeof(ar
)/sizeof(ar
[0]));
39 assert(m
.size() == 8);
40 I i
= m
.erase(m
.cbegin(), m
.cbegin());
41 assert(m
.size() == 8);
42 assert(i
== m
.begin());
43 assert(m
.begin()->first
== 1);
44 assert(m
.begin()->second
== 1.5);
45 assert(std::next(m
.begin())->first
== 2);
46 assert(std::next(m
.begin())->second
== 2.5);
47 assert(std::next(m
.begin(), 2)->first
== 3);
48 assert(std::next(m
.begin(), 2)->second
== 3.5);
49 assert(std::next(m
.begin(), 3)->first
== 4);
50 assert(std::next(m
.begin(), 3)->second
== 4.5);
51 assert(std::next(m
.begin(), 4)->first
== 5);
52 assert(std::next(m
.begin(), 4)->second
== 5.5);
53 assert(std::next(m
.begin(), 5)->first
== 6);
54 assert(std::next(m
.begin(), 5)->second
== 6.5);
55 assert(std::next(m
.begin(), 6)->first
== 7);
56 assert(std::next(m
.begin(), 6)->second
== 7.5);
57 assert(std::next(m
.begin(), 7)->first
== 8);
58 assert(std::next(m
.begin(), 7)->second
== 8.5);
60 i
= m
.erase(m
.cbegin(), std::next(m
.cbegin(), 2));
61 assert(m
.size() == 6);
62 assert(i
== m
.begin());
63 assert(std::next(m
.begin(), 0)->first
== 3);
64 assert(std::next(m
.begin(), 0)->second
== 3.5);
65 assert(std::next(m
.begin(), 1)->first
== 4);
66 assert(std::next(m
.begin(), 1)->second
== 4.5);
67 assert(std::next(m
.begin(), 2)->first
== 5);
68 assert(std::next(m
.begin(), 2)->second
== 5.5);
69 assert(std::next(m
.begin(), 3)->first
== 6);
70 assert(std::next(m
.begin(), 3)->second
== 6.5);
71 assert(std::next(m
.begin(), 4)->first
== 7);
72 assert(std::next(m
.begin(), 4)->second
== 7.5);
73 assert(std::next(m
.begin(), 5)->first
== 8);
74 assert(std::next(m
.begin(), 5)->second
== 8.5);
76 i
= m
.erase(std::next(m
.cbegin(), 2), std::next(m
.cbegin(), 6));
77 assert(m
.size() == 2);
78 assert(i
== std::next(m
.begin(), 2));
79 assert(std::next(m
.begin(), 0)->first
== 3);
80 assert(std::next(m
.begin(), 0)->second
== 3.5);
81 assert(std::next(m
.begin(), 1)->first
== 4);
82 assert(std::next(m
.begin(), 1)->second
== 4.5);
84 i
= m
.erase(m
.cbegin(), m
.cend());
85 assert(m
.size() == 0);
86 assert(i
== m
.begin());
89 #if TEST_STD_VER >= 11
91 typedef std::map
<int, double, std::less
<int>, min_allocator
<std::pair
<const int, double>>> M
;
92 typedef std::pair
<int, double> P
;
93 typedef M::iterator I
;
105 M
m(ar
, ar
+ sizeof(ar
)/sizeof(ar
[0]));
106 assert(m
.size() == 8);
107 I i
= m
.erase(m
.cbegin(), m
.cbegin());
108 assert(m
.size() == 8);
109 assert(i
== m
.begin());
110 assert(m
.begin()->first
== 1);
111 assert(m
.begin()->second
== 1.5);
112 assert(std::next(m
.begin())->first
== 2);
113 assert(std::next(m
.begin())->second
== 2.5);
114 assert(std::next(m
.begin(), 2)->first
== 3);
115 assert(std::next(m
.begin(), 2)->second
== 3.5);
116 assert(std::next(m
.begin(), 3)->first
== 4);
117 assert(std::next(m
.begin(), 3)->second
== 4.5);
118 assert(std::next(m
.begin(), 4)->first
== 5);
119 assert(std::next(m
.begin(), 4)->second
== 5.5);
120 assert(std::next(m
.begin(), 5)->first
== 6);
121 assert(std::next(m
.begin(), 5)->second
== 6.5);
122 assert(std::next(m
.begin(), 6)->first
== 7);
123 assert(std::next(m
.begin(), 6)->second
== 7.5);
124 assert(std::next(m
.begin(), 7)->first
== 8);
125 assert(std::next(m
.begin(), 7)->second
== 8.5);
127 i
= m
.erase(m
.cbegin(), std::next(m
.cbegin(), 2));
128 assert(m
.size() == 6);
129 assert(i
== m
.begin());
130 assert(std::next(m
.begin(), 0)->first
== 3);
131 assert(std::next(m
.begin(), 0)->second
== 3.5);
132 assert(std::next(m
.begin(), 1)->first
== 4);
133 assert(std::next(m
.begin(), 1)->second
== 4.5);
134 assert(std::next(m
.begin(), 2)->first
== 5);
135 assert(std::next(m
.begin(), 2)->second
== 5.5);
136 assert(std::next(m
.begin(), 3)->first
== 6);
137 assert(std::next(m
.begin(), 3)->second
== 6.5);
138 assert(std::next(m
.begin(), 4)->first
== 7);
139 assert(std::next(m
.begin(), 4)->second
== 7.5);
140 assert(std::next(m
.begin(), 5)->first
== 8);
141 assert(std::next(m
.begin(), 5)->second
== 8.5);
143 i
= m
.erase(std::next(m
.cbegin(), 2), std::next(m
.cbegin(), 6));
144 assert(m
.size() == 2);
145 assert(i
== std::next(m
.begin(), 2));
146 assert(std::next(m
.begin(), 0)->first
== 3);
147 assert(std::next(m
.begin(), 0)->second
== 3.5);
148 assert(std::next(m
.begin(), 1)->first
== 4);
149 assert(std::next(m
.begin(), 1)->second
== 4.5);
151 i
= m
.erase(m
.cbegin(), m
.cend());
152 assert(m
.size() == 0);
153 assert(i
== m
.begin());
154 assert(i
== m
.end());