]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/debug/checks.h
Update copyright years in libstdc++-v3/
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / debug / checks.h
1 // Copyright (C) 2010-2014 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17 //
18
19 #include <vector>
20 #include <deque>
21 #include <list>
22 #ifndef _GLIBCXX_DEBUG
23 # include <debug/vector>
24 # include <debug/deque>
25 # include <debug/list>
26 #endif
27 #include <testsuite_hooks.h>
28
29 namespace __gnu_test
30 {
31 template<typename _Tp>
32 struct CopyableValueType
33 {
34 typedef _Tp value_type;
35 };
36
37 template<typename _Tp1, typename _Tp2>
38 struct CopyableValueType<std::pair<const _Tp1, _Tp2> >
39 {
40 typedef std::pair<_Tp1, _Tp2> value_type;
41 };
42
43 template<typename _Tp>
44 struct generate_unique
45 {
46 typedef _Tp value_type;
47
48 value_type build()
49 {
50 static value_type _S_;
51 ++_S_;
52 return _S_;
53 }
54 };
55
56 template<typename _Tp1, typename _Tp2>
57 struct generate_unique<std::pair<_Tp1, _Tp2> >
58 {
59 typedef _Tp1 first_type;
60 typedef _Tp2 second_type;
61 typedef std::pair<_Tp1, _Tp2> pair_type;
62
63 pair_type build()
64 {
65 static first_type _S_1;
66 static second_type _S_2;
67 ++_S_1;
68 ++_S_2;
69 return pair_type(_S_1, _S_2);
70 }
71 };
72
73 // Check that invalid range of pointers is detected
74 template<typename _Tp>
75 void
76 check_assign1()
77 {
78 bool test __attribute__((unused)) = true;
79
80 typedef _Tp cont_type;
81 typedef typename cont_type::value_type cont_val_type;
82 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
83 typedef std::vector<val_type> vector_type;
84
85 generate_unique<val_type> gu;
86
87 vector_type v;
88 for (int i = 0; i != 5; ++i)
89 v.push_back(gu.build());
90 VERIFY(v.size() == 5);
91
92 const val_type* first = &v.front() + 1;
93 const val_type* last = first + 2;
94
95 cont_type c1;
96 c1.assign(first, last);
97 VERIFY(c1.size() == 2);
98
99 cont_type c2;
100 c2.assign(last, first); // Expected failure
101 }
102
103 // Check that invalid range of debug random iterators is detected
104 template<typename _Tp>
105 void
106 check_assign2()
107 {
108 bool test __attribute__((unused)) = true;
109
110 typedef _Tp cont_type;
111 typedef typename cont_type::value_type cont_val_type;
112 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
113 typedef std::vector<val_type> vector_type;
114
115 generate_unique<val_type> gu;
116
117 vector_type v;
118 for (int i = 0; i != 5; ++i)
119 v.push_back(gu.build());
120 VERIFY(v.size() == 5);
121
122 typename vector_type::iterator first = v.begin() + 1;
123 typename vector_type::iterator last = first + 2;
124 cont_type c1;
125 c1.assign(first, last);
126 VERIFY(c1.size() == 2);
127
128 cont_type c2;
129 c2.assign(last, first); // Expected failure
130 }
131
132 // Check that invalid range of debug not random iterators is detected
133 template<typename _Tp>
134 void
135 check_assign3()
136 {
137 bool test __attribute__((unused)) = true;
138
139 typedef _Tp cont_type;
140 typedef typename cont_type::value_type cont_val_type;
141 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
142 typedef std::list<val_type> list_type;
143
144 generate_unique<val_type> gu;
145
146 list_type l;
147 for (int i = 0; i != 5; ++i)
148 l.push_back(gu.build());
149 VERIFY(l.size() == 5);
150
151 typename list_type::iterator first = l.begin(); ++first;
152 typename list_type::iterator last = first; ++last; ++last;
153 cont_type c1;
154 c1.assign(first, last);
155 VERIFY(c1.size() == 2);
156
157 cont_type c2;
158 c2.assign(last, first); // Expected failure
159 }
160
161 // Check that invalid range of pointers is detected
162 template<typename _Tp>
163 void
164 check_construct1()
165 {
166 bool test __attribute__((unused)) = true;
167
168 typedef _Tp cont_type;
169 typedef typename cont_type::value_type cont_val_type;
170 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
171 typedef std::vector<val_type> vector_type;
172
173 generate_unique<val_type> gu;
174
175 vector_type v;
176 for (int i = 0; i != 5; ++i)
177 v.push_back(gu.build());
178 VERIFY(v.size() == 5);
179
180 val_type *first = &v.front() + 1;
181 val_type *last = first + 2;
182 cont_type c1(first, last);
183 VERIFY(c1.size() == 2);
184
185 cont_type c2(last, first); // Expected failure
186 }
187
188 // Check that invalid range of debug random iterators is detected
189 template<typename _Tp>
190 void
191 check_construct2()
192 {
193 bool test __attribute__((unused)) = true;
194
195 typedef _Tp cont_type;
196 typedef typename cont_type::value_type cont_val_type;
197 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
198 typedef std::vector<val_type> vector_type;
199
200 generate_unique<val_type> gu;
201
202 vector_type v;
203 for (int i = 0; i != 5; ++i)
204 v.push_back(gu.build());
205 VERIFY(v.size() == 5);
206
207 typename vector_type::iterator first = v.begin() + 1;
208 typename vector_type::iterator last = first + 2;
209 cont_type c1(first, last);
210 VERIFY(c1.size() == 2);
211
212 cont_type c2(last, first); // Expected failure
213 }
214
215 // Check that invalid range of debug not random iterators is detected
216 template<typename _Tp>
217 void
218 check_construct3()
219 {
220 bool test __attribute__((unused)) = true;
221
222 typedef _Tp cont_type;
223 typedef typename cont_type::value_type cont_val_type;
224 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
225 typedef std::list<val_type> list_type;
226
227 generate_unique<val_type> gu;
228
229 list_type l;
230 for (int i = 0; i != 5; ++i)
231 l.push_back(gu.build());
232 VERIFY(l.size() == 5);
233
234 typename list_type::iterator first = l.begin(); ++first;
235 typename list_type::iterator last = first; ++last; ++last;
236 cont_type c1(first, last);
237 VERIFY(c1.size() == 2);
238
239 cont_type c2(last, first); // Expected failure
240 }
241
242 template <typename _Cont>
243 struct InsertRangeHelper
244 {
245 template <typename _It>
246 static void
247 Insert(_Cont& cont, _It first, _It last)
248 { cont.insert(first, last); }
249 };
250
251 template <typename _Cont>
252 struct InsertRangeHelperAux
253 {
254 template <typename _It>
255 static void
256 Insert(_Cont& cont, _It first, _It last)
257 { cont.insert(cont.begin(), first, last); }
258 };
259
260 template <typename _Tp1, typename _Tp2>
261 struct InsertRangeHelper<std::vector<_Tp1, _Tp2> >
262 : InsertRangeHelperAux<std::vector<_Tp1, _Tp2> >
263 { };
264
265 template <typename _Tp1, typename _Tp2>
266 struct InsertRangeHelper<std::deque<_Tp1, _Tp2> >
267 : InsertRangeHelperAux<std::deque<_Tp1, _Tp2> >
268 { };
269
270 template <typename _Tp1, typename _Tp2>
271 struct InsertRangeHelper<std::list<_Tp1, _Tp2> >
272 : InsertRangeHelperAux<std::list<_Tp1, _Tp2> >
273 { };
274
275 #ifndef _GLIBCXX_DEBUG
276 template <typename _Tp1, typename _Tp2>
277 struct InsertRangeHelper<__gnu_debug::vector<_Tp1, _Tp2> >
278 : InsertRangeHelperAux<__gnu_debug::vector<_Tp1, _Tp2> >
279 { };
280
281 template <typename _Tp1, typename _Tp2>
282 struct InsertRangeHelper<__gnu_debug::deque<_Tp1, _Tp2> >
283 : InsertRangeHelperAux<__gnu_debug::deque<_Tp1, _Tp2> >
284 { };
285
286 template <typename _Tp1, typename _Tp2>
287 struct InsertRangeHelper<__gnu_debug::list<_Tp1, _Tp2> >
288 : InsertRangeHelperAux<__gnu_debug::list<_Tp1, _Tp2> >
289 { };
290 #endif
291
292 template<typename _Tp>
293 void
294 check_insert1()
295 {
296 bool test __attribute__((unused)) = true;
297
298 typedef _Tp cont_type;
299 typedef typename cont_type::value_type cont_val_type;
300 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
301 typedef std::vector<val_type> vector_type;
302
303 generate_unique<val_type> gu;
304
305 vector_type v;
306 for (int i = 0; i != 5; ++i)
307 v.push_back(gu.build());
308 VERIFY(v.size() == 5);
309
310 const val_type* first = &v.front() + 1;
311 const val_type* last = first + 2;
312
313 cont_type c1;
314 InsertRangeHelper<cont_type>::Insert(c1, first, last);
315 VERIFY(c1.size() == 2);
316
317 cont_type c2;
318 InsertRangeHelper<cont_type>::Insert(c2, last, first); // Expected failure
319 }
320
321 template<typename _Tp>
322 void
323 check_insert2()
324 {
325 bool test __attribute__((unused)) = true;
326
327 typedef _Tp cont_type;
328 typedef typename cont_type::value_type cont_val_type;
329 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
330 typedef std::vector<val_type> vector_type;
331
332 generate_unique<val_type> gu;
333
334 vector_type v;
335 for (int i = 0; i != 5; ++i)
336 v.push_back(gu.build());
337 VERIFY(v.size() == 5);
338
339 typename vector_type::iterator first = v.begin() + 1;
340 typename vector_type::iterator last = first + 2;
341
342 cont_type c1;
343 InsertRangeHelper<cont_type>::Insert(c1, first, last);
344 VERIFY(c1.size() == 2);
345
346 cont_type c2;
347 InsertRangeHelper<cont_type>::Insert(c2, last, first); // Expected failure
348 }
349
350 template<typename _Tp>
351 void
352 check_insert3()
353 {
354 bool test __attribute__((unused)) = true;
355
356 typedef _Tp cont_type;
357 typedef typename cont_type::value_type cont_val_type;
358 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
359 typedef std::list<val_type> list_type;
360
361 generate_unique<val_type> gu;
362
363 list_type l;
364 for (int i = 0; i != 5; ++i)
365 l.push_back(gu.build());
366 VERIFY(l.size() == 5);
367
368 typename list_type::iterator first = l.begin(); ++first;
369 typename list_type::iterator last = first; ++last; ++last;
370
371 cont_type c1;
372 InsertRangeHelper<cont_type>::Insert(c1, first, last);
373 VERIFY(c1.size() == 2);
374
375 cont_type c2;
376 InsertRangeHelper<cont_type>::Insert(c2, last, first); // Expected failure
377 }
378
379 template<typename _Tp>
380 void
381 check_insert4()
382 {
383 bool test __attribute__((unused)) = true;
384
385 typedef _Tp cont_type;
386 typedef typename cont_type::value_type cont_val_type;
387 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
388 typedef std::list<val_type> list_type;
389
390 generate_unique<val_type> gu;
391
392 list_type l;
393 for (int i = 0; i != 5; ++i)
394 l.push_back(gu.build());
395 VERIFY(l.size() == 5);
396
397 typename list_type::iterator first = l.begin(); ++first;
398 typename list_type::iterator last = first; ++last; ++last;
399
400 cont_type c1;
401 InsertRangeHelper<cont_type>::Insert(c1, l.begin(), l.end());
402 VERIFY(c1.size() == 5);
403
404 c1.insert(c1.begin(), c1.begin(), c1.end()); // Expected failure.
405 }
406
407 template<typename _Tp>
408 void use_invalid_iterator()
409 {
410 bool test __attribute__((unused)) = true;
411
412 typedef _Tp cont_type;
413 typedef typename cont_type::value_type cont_val_type;
414 typedef typename CopyableValueType<cont_val_type>::value_type val_type;
415 generate_unique<val_type> gu;
416
417 cont_type c;
418 for (size_t i = 0; i != 5; ++i)
419 c.insert(gu.build());
420
421 typename cont_type::iterator it = c.begin();
422 cont_val_type val = *it;
423 c.clear();
424 VERIFY( *it == val );
425 }
426 }
427