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

source code of libcxx/test/std/containers/associative/multimap/multimap.modifiers/erase_key.pass.cpp