llvm-project
266 строк · 7.5 Кб
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 begin();
14// const_iterator begin() const;
15// iterator end();
16// const_iterator end() const;
17//
18// reverse_iterator rbegin();
19// const_reverse_iterator rbegin() const;
20// reverse_iterator rend();
21// const_reverse_iterator rend() const;
22//
23// const_iterator cbegin() const;
24// const_iterator cend() const;
25// const_reverse_iterator crbegin() const;
26// const_reverse_iterator crend() const;
27
28#include <map>
29#include <cassert>
30#include <cstddef>
31
32#include "test_macros.h"
33#include "min_allocator.h"
34
35int main(int, char**)
36{
37{
38typedef std::pair<const int, double> V;
39V ar[] =
40{
41V(1, 1),
42V(1, 1.5),
43V(1, 2),
44V(2, 1),
45V(2, 1.5),
46V(2, 2),
47V(3, 1),
48V(3, 1.5),
49V(3, 2),
50V(4, 1),
51V(4, 1.5),
52V(4, 2),
53V(5, 1),
54V(5, 1.5),
55V(5, 2),
56V(6, 1),
57V(6, 1.5),
58V(6, 2),
59V(7, 1),
60V(7, 1.5),
61V(7, 2),
62V(8, 1),
63V(8, 1.5),
64V(8, 2)
65};
66std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
67assert(static_cast<std::size_t>(std::distance(m.begin(), m.end())) == m.size());
68assert(static_cast<std::size_t>(std::distance(m.rbegin(), m.rend())) == m.size());
69std::map<int, double>::iterator i;
70i = m.begin();
71std::map<int, double>::const_iterator k = i;
72assert(i == k);
73for (int j = 1; static_cast<std::size_t>(j) <= m.size(); ++j, ++i)
74{
75assert(i->first == j);
76assert(i->second == 1);
77i->second = 2.5;
78assert(i->second == 2.5);
79}
80assert(i == m.end());
81for (int j = m.size(); j >= 1; --j)
82{
83--i;
84assert(i->first == j);
85assert(i->second == 2.5);
86i->second = 1;
87assert(i->second == 1);
88}
89assert(i == m.begin());
90}
91{
92typedef std::pair<const int, double> V;
93V ar[] =
94{
95V(1, 1),
96V(1, 1.5),
97V(1, 2),
98V(2, 1),
99V(2, 1.5),
100V(2, 2),
101V(3, 1),
102V(3, 1.5),
103V(3, 2),
104V(4, 1),
105V(4, 1.5),
106V(4, 2),
107V(5, 1),
108V(5, 1.5),
109V(5, 2),
110V(6, 1),
111V(6, 1.5),
112V(6, 2),
113V(7, 1),
114V(7, 1.5),
115V(7, 2),
116V(8, 1),
117V(8, 1.5),
118V(8, 2)
119};
120const std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
121assert(static_cast<std::size_t>(std::distance(m.begin(), m.end())) == m.size());
122assert(static_cast<std::size_t>(std::distance(m.cbegin(), m.cend())) == m.size());
123assert(static_cast<std::size_t>(std::distance(m.rbegin(), m.rend())) == m.size());
124assert(static_cast<std::size_t>(std::distance(m.crbegin(), m.crend())) == m.size());
125std::map<int, double>::const_iterator i;
126i = m.begin();
127for (int j = 1; static_cast<std::size_t>(j) <= m.size(); ++j, ++i)
128{
129assert(i->first == j);
130assert(i->second == 1);
131}
132assert(i == m.end());
133for (int j = m.size(); j >= 1; --j)
134{
135--i;
136assert(i->first == j);
137assert(i->second == 1);
138}
139assert(i == m.begin());
140}
141#if TEST_STD_VER >= 11
142{
143typedef std::pair<const int, double> V;
144V ar[] =
145{
146V(1, 1),
147V(1, 1.5),
148V(1, 2),
149V(2, 1),
150V(2, 1.5),
151V(2, 2),
152V(3, 1),
153V(3, 1.5),
154V(3, 2),
155V(4, 1),
156V(4, 1.5),
157V(4, 2),
158V(5, 1),
159V(5, 1.5),
160V(5, 2),
161V(6, 1),
162V(6, 1.5),
163V(6, 2),
164V(7, 1),
165V(7, 1.5),
166V(7, 2),
167V(8, 1),
168V(8, 1.5),
169V(8, 2)
170};
171std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
172assert(static_cast<std::size_t>(std::distance(m.begin(), m.end())) == m.size());
173assert(static_cast<std::size_t>(std::distance(m.rbegin(), m.rend())) == m.size());
174std::map<int, double, std::less<int>, min_allocator<V>>::iterator i;
175i = m.begin();
176std::map<int, double, std::less<int>, min_allocator<V>>::const_iterator k = i;
177assert(i == k);
178for (int j = 1; static_cast<std::size_t>(j) <= m.size(); ++j, ++i)
179{
180assert(i->first == j);
181assert(i->second == 1);
182i->second = 2.5;
183assert(i->second == 2.5);
184}
185assert(i == m.end());
186for (int j = m.size(); j >= 1; --j)
187{
188--i;
189assert(i->first == j);
190assert(i->second == 2.5);
191i->second = 1;
192assert(i->second == 1);
193}
194assert(i == m.begin());
195}
196{
197typedef std::pair<const int, double> V;
198V ar[] =
199{
200V(1, 1),
201V(1, 1.5),
202V(1, 2),
203V(2, 1),
204V(2, 1.5),
205V(2, 2),
206V(3, 1),
207V(3, 1.5),
208V(3, 2),
209V(4, 1),
210V(4, 1.5),
211V(4, 2),
212V(5, 1),
213V(5, 1.5),
214V(5, 2),
215V(6, 1),
216V(6, 1.5),
217V(6, 2),
218V(7, 1),
219V(7, 1.5),
220V(7, 2),
221V(8, 1),
222V(8, 1.5),
223V(8, 2)
224};
225const std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
226assert(static_cast<std::size_t>(std::distance(m.begin(), m.end())) == m.size());
227assert(static_cast<std::size_t>(std::distance(m.cbegin(), m.cend())) == m.size());
228assert(static_cast<std::size_t>(std::distance(m.rbegin(), m.rend())) == m.size());
229assert(static_cast<std::size_t>(std::distance(m.crbegin(), m.crend())) == m.size());
230std::map<int, double, std::less<int>, min_allocator<V>>::const_iterator i;
231i = m.begin();
232for (int j = 1; static_cast<std::size_t>(j) <= m.size(); ++j, ++i)
233{
234assert(i->first == j);
235assert(i->second == 1);
236}
237assert(i == m.end());
238for (int j = m.size(); j >= 1; --j)
239{
240--i;
241assert(i->first == j);
242assert(i->second == 1);
243}
244assert(i == m.begin());
245}
246#endif
247#if TEST_STD_VER > 11
248{ // N3644 testing
249typedef std::map<int, double> C;
250C::iterator ii1{}, ii2{};
251C::iterator ii4 = ii1;
252C::const_iterator cii{};
253assert ( ii1 == ii2 );
254assert ( ii1 == ii4 );
255
256assert (!(ii1 != ii2 ));
257
258assert ( (ii1 == cii ));
259assert ( (cii == ii1 ));
260assert (!(ii1 != cii ));
261assert (!(cii != ii1 ));
262}
263#endif
264
265return 0;
266}
267