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