3 // Copyright (C) 1999-2001, 2002, 2003, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // 23.2.4.1 vector constructors, copy, and assignment
24 #include <testsuite_allocator.h>
25 #include <testsuite_hooks.h>
27 using __gnu_test::copy_tracker
;
28 using __gnu_test::tracker_allocator_counter
;
29 using __gnu_test::tracker_allocator
;
30 using __gnu_test::copy_constructor
;
31 using __gnu_test::assignment_operator
;
33 // @fn test_default_ctor_exception_gurantee This test verifies that if
34 // one of the vector's contained objects throws an exception from its
35 // constructor while the vector is being constructed and filled with
36 // default values, all memory is returned to the allocator whence it
39 test_default_ctor_exception_gurantee()
42 bool test
__attribute__((unused
)) = true;
43 typedef copy_tracker T
;
44 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
46 copy_tracker::reset();
47 copy_constructor::throw_on(3);
48 tracker_allocator_counter::reset();
61 // assert postconditions
62 VERIFY( tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count() );
67 // @fn test_copy_ctor_exception_gurantee This test verifies that if
68 // one of the vector's contained objects throws an exception from its
69 // constructor while the vector is being copy constructed, all memory
70 // is returned to the allocator whence it came.
72 test_copy_ctor_exception_gurantee()
75 bool test
__attribute__((unused
)) = true;
76 typedef copy_tracker T
;
77 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
79 tracker_allocator_counter::reset();
82 copy_tracker::reset();
83 copy_constructor::throw_on(3);
96 // assert postconditions
97 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
100 copy_tracker::reset();
101 tracker_allocator_counter::reset();
106 // case 1: lhs.size() > rhs.size()
107 // case 2: lhs.size() < rhs.size() < lhs.capacity()
108 // case 3: lhs.capacity() < rhs.size()
111 test_assignment_operator_1()
114 bool test
__attribute__((unused
)) = true;
115 typedef copy_tracker T
;
116 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
120 copy_tracker::reset();
121 tracker_allocator_counter::reset();
124 VERIFY(r
.size() > a
.size());
129 // assert postconditions
131 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
134 copy_tracker::reset();
135 tracker_allocator_counter::reset();
139 test_assignment_operator_2()
142 bool test
__attribute__((unused
)) = true;
143 typedef copy_tracker T
;
144 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
149 copy_tracker::reset();
150 tracker_allocator_counter::reset();
153 VERIFY(r
.size() < a
.size());
154 VERIFY(a
.size() < r
.capacity());
159 // assert postconditions
161 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
164 copy_tracker::reset();
165 tracker_allocator_counter::reset();
169 test_assignment_operator_3()
172 bool test
__attribute__((unused
)) = true;
173 typedef copy_tracker T
;
174 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
176 tracker_allocator_counter::reset();
179 X
a(r
.capacity() + 7);
180 copy_tracker::reset();
183 VERIFY(r
.capacity() < a
.size());
188 // assert postconditions
191 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
194 copy_tracker::reset();
195 tracker_allocator_counter::reset();
199 test_assignment_operator_3_exception_guarantee()
202 bool test
__attribute__((unused
)) = true;
203 typedef copy_tracker T
;
204 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
206 tracker_allocator_counter::reset();
209 X
a(r
.capacity() + 7);
210 copy_tracker::reset();
211 copy_constructor::throw_on(3);
214 VERIFY(r
.capacity() < a
.size());
227 // assert postconditions
228 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
231 copy_tracker::reset();
232 tracker_allocator_counter::reset();
237 // case 1: [23.2.4.1 (3)] n <= size()
238 // case 2: [23.2.4.1 (3)] size() < n <= capacity()
239 // case 3: [23.2.4.1 (3)] n > capacity()
240 // case 4: [23.2.4.1 (3)] n > capacity(), exception guarantees
241 // case 5: [23.1.1 (9)] fill assign disguised as a range assign
247 bool test
__attribute__((unused
)) = true;
248 typedef copy_tracker T
;
249 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
252 X::size_type old_size
= a
.size();
253 X::size_type new_size
= old_size
- 2;
256 copy_tracker::reset();
257 tracker_allocator_counter::reset();
260 a
.assign(new_size
, t
);
262 // assert postconditions
263 VERIFY(a
.size() == new_size
);
264 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
267 copy_tracker::reset();
268 tracker_allocator_counter::reset();
275 bool test
__attribute__((unused
)) = true;
276 typedef copy_tracker T
;
277 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
281 X::size_type old_size
= a
.size();
282 X::size_type old_capacity
= a
.capacity();
283 X::size_type new_size
= old_size
+ 2;
286 copy_tracker::reset();
287 tracker_allocator_counter::reset();
289 // assert preconditions
290 VERIFY(old_size
< new_size
);
291 VERIFY(new_size
<= old_capacity
);
294 a
.assign(new_size
, t
);
296 // assert postconditions
297 VERIFY(a
.size() == new_size
);
298 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
301 copy_tracker::reset();
302 tracker_allocator_counter::reset();
309 bool test
__attribute__((unused
)) = true;
310 typedef copy_tracker T
;
311 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
313 tracker_allocator_counter::reset();
316 X::size_type old_capacity
= a
.capacity();
317 X::size_type new_size
= old_capacity
+ 4;
320 copy_tracker::reset();
322 // assert preconditions
323 VERIFY(new_size
> old_capacity
);
326 a
.assign(new_size
, t
);
328 // assert postconditions
329 VERIFY(a
.size() == new_size
);
332 VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
333 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
336 copy_tracker::reset();
337 tracker_allocator_counter::reset();
341 test_fill_assign_3_exception_guarantee()
344 bool test
__attribute__((unused
)) = true;
345 typedef copy_tracker T
;
346 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
348 tracker_allocator_counter::reset();
351 X::size_type old_size
= a
.size();
352 X::size_type old_capacity
= a
.capacity();
353 X::size_type new_size
= old_capacity
+ 4;
356 copy_tracker::reset();
357 copy_constructor::throw_on(3);
359 // assert preconditions
360 VERIFY(new_size
> old_capacity
);
365 a
.assign(new_size
, t
);
372 // assert postconditions
373 VERIFY(a
.size() == old_size
);
374 VERIFY(a
.capacity() == old_capacity
);
377 VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
378 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
381 copy_tracker::reset();
382 tracker_allocator_counter::reset();
389 bool test
__attribute__((unused
)) = true;
390 typedef copy_tracker T
;
391 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
394 X::size_type old_size
= a
.size();
395 X::size_type new_size
= old_size
- 2;
396 X::size_type new_value
= 117;
398 copy_tracker::reset();
399 tracker_allocator_counter::reset();
402 a
.assign(new_size
, new_value
);
404 // assert postconditions
405 VERIFY(a
.size() == new_size
);
406 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
409 copy_tracker::reset();
410 tracker_allocator_counter::reset();
415 // case 1: [23.2.4.1 (2)] input iterator
416 // case 2: [23.2.4.1 (2)] forward iterator, distance(first, last) <= size()
417 // case 3: [23.2.4.1 (2)]
418 // forward iterator, size() < distance(first, last) <= capacity()
419 // case 4: [23.2.4.1 (2)] forward iterator, distance(first, last) > capacity()
420 // case 5: [23.2.4.1 (2)]
421 // forward iterator, distance(first, last) > capacity(),
422 // exception guarantees
424 test_range_assign_1()
430 test_range_assign_2()
433 bool test
__attribute__((unused
)) = true;
434 typedef copy_tracker T
;
435 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
440 copy_tracker::reset();
441 tracker_allocator_counter::reset();
443 // assert preconditions
444 VERIFY(b
.size() < a
.capacity());
447 a
.assign(b
.begin(), b
.end());
449 // assert postconditions
450 VERIFY(a
.size() == b
.size());
452 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
455 copy_tracker::reset();
456 tracker_allocator_counter::reset();
460 test_range_assign_3()
463 bool test
__attribute__((unused
)) = true;
464 typedef copy_tracker T
;
465 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
468 a
.reserve(a
.size() + 7);
471 copy_tracker::reset();
472 tracker_allocator_counter::reset();
474 // assert preconditions
475 VERIFY(a
.size() < b
.size());
476 VERIFY(b
.size() < a
.capacity());
479 a
.assign(b
.begin(), b
.end());
481 // assert postconditions
482 VERIFY(a
.size() == b
.size());
484 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
487 copy_tracker::reset();
488 tracker_allocator_counter::reset();
492 test_range_assign_4()
495 bool test
__attribute__((unused
)) = true;
496 typedef copy_tracker T
;
497 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
499 tracker_allocator_counter::reset();
502 X
b(a
.capacity() + 7);
504 copy_tracker::reset();
506 // assert preconditions
507 VERIFY(b
.size() > a
.capacity());
510 a
.assign(b
.begin(), b
.end());
512 // assert postconditions
513 VERIFY(a
.size() == b
.size());
516 VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
517 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
520 copy_tracker::reset();
521 tracker_allocator_counter::reset();
525 test_range_assign_4_exception_guarantee()
528 bool test
__attribute__((unused
)) = true;
529 typedef copy_tracker T
;
530 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
532 tracker_allocator_counter::reset();
535 X
b(a
.capacity() + 7);
537 copy_tracker::reset();
538 copy_constructor::throw_on(3);
540 // assert preconditions
541 VERIFY(b
.size() > a
.capacity());
546 a
.assign(b
.begin(), b
.end());
554 // assert postconditions
555 VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
556 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
559 copy_tracker::reset();
560 tracker_allocator_counter::reset();
566 test_default_ctor_exception_gurantee();
567 test_copy_ctor_exception_gurantee();
568 test_assignment_operator_1();
569 test_assignment_operator_2();
570 test_assignment_operator_3();
571 test_assignment_operator_3_exception_guarantee();
572 test_fill_assign_1();
573 test_fill_assign_2();
574 test_fill_assign_3();
575 test_fill_assign_3_exception_guarantee();
576 test_fill_assign_4();
577 test_range_assign_1();
578 test_range_assign_2();
579 test_range_assign_3();
580 test_range_assign_4();
581 test_range_assign_4_exception_guarantee();