llvm-project

Форк
0
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

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

256
        assert (!(ii1 != ii2 ));
257

258
        assert ( (ii1 == cii ));
259
        assert ( (cii == ii1 ));
260
        assert (!(ii1 != cii ));
261
        assert (!(cii != ii1 ));
262
    }
263
#endif
264

265
  return 0;
266
}
267

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.