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();
60 // assert postconditions
61 VERIFY( tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count() );
66 // @fn test_copy_ctor_exception_gurantee This test verifies that if
67 // one of the vector's contained objects throws an exception from its
68 // constructor while the vector is being copy constructed, all memory
69 // is returned to the allocator whence it came.
71 test_copy_ctor_exception_gurantee()
74 bool test __attribute__((unused)) = true;
75 typedef copy_tracker T;
76 typedef std::vector<T, tracker_allocator<T> > X;
78 tracker_allocator_counter::reset();
81 copy_tracker::reset();
82 copy_constructor::throw_on(3);
95 // assert postconditions
96 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
99 copy_tracker::reset();
100 tracker_allocator_counter::reset();
105 // case 1: lhs.size() > rhs.size()
106 // case 2: lhs.size() < rhs.size() < lhs.capacity()
107 // case 3: lhs.capacity() < rhs.size()
110 test_assignment_operator_1()
113 bool test __attribute__((unused)) = true;
114 typedef copy_tracker T;
115 typedef std::vector<T, tracker_allocator<T> > X;
119 copy_tracker::reset();
120 tracker_allocator_counter::reset();
123 VERIFY(r.size() > a.size());
128 // assert postconditions
130 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
133 copy_tracker::reset();
134 tracker_allocator_counter::reset();
138 test_assignment_operator_2()
141 bool test __attribute__((unused)) = true;
142 typedef copy_tracker T;
143 typedef std::vector<T, tracker_allocator<T> > X;
148 copy_tracker::reset();
149 tracker_allocator_counter::reset();
152 VERIFY(r.size() < a.size());
153 VERIFY(a.size() < r.capacity());
158 // assert postconditions
160 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
163 copy_tracker::reset();
164 tracker_allocator_counter::reset();
168 test_assignment_operator_3()
171 bool test __attribute__((unused)) = true;
172 typedef copy_tracker T;
173 typedef std::vector<T, tracker_allocator<T> > X;
175 tracker_allocator_counter::reset();
178 X a(r.capacity() + 7);
179 copy_tracker::reset();
182 VERIFY(r.capacity() < a.size());
187 // assert postconditions
190 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
193 copy_tracker::reset();
194 tracker_allocator_counter::reset();
198 test_assignment_operator_3_exception_guarantee()
201 bool test __attribute__((unused)) = true;
202 typedef copy_tracker T;
203 typedef std::vector<T, tracker_allocator<T> > X;
205 tracker_allocator_counter::reset();
208 X a(r.capacity() + 7);
209 copy_tracker::reset();
210 copy_constructor::throw_on(3);
213 VERIFY(r.capacity() < a.size());
226 // assert postconditions
227 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
230 copy_tracker::reset();
231 tracker_allocator_counter::reset();
236 // case 1: [23.2.4.1 (3)] n <= size()
237 // case 2: [23.2.4.1 (3)] size() < n <= capacity()
238 // case 3: [23.2.4.1 (3)] n > capacity()
239 // case 4: [23.2.4.1 (3)] n > capacity(), exception guarantees
240 // case 5: [23.1.1 (9)] fill assign disguised as a range assign
246 bool test __attribute__((unused)) = true;
247 typedef copy_tracker T;
248 typedef std::vector<T, tracker_allocator<T> > X;
251 X::size_type old_size = a.size();
252 X::size_type new_size = old_size - 2;
255 copy_tracker::reset();
256 tracker_allocator_counter::reset();
259 a.assign(new_size, t);
261 // assert postconditions
262 VERIFY(a.size() == new_size);
263 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
266 copy_tracker::reset();
267 tracker_allocator_counter::reset();
274 bool test __attribute__((unused)) = true;
275 typedef copy_tracker T;
276 typedef std::vector<T, tracker_allocator<T> > X;
280 X::size_type old_size = a.size();
281 X::size_type old_capacity = a.capacity();
282 X::size_type new_size = old_size + 2;
285 copy_tracker::reset();
286 tracker_allocator_counter::reset();
288 // assert preconditions
289 VERIFY(old_size < new_size);
290 VERIFY(new_size <= old_capacity);
293 a.assign(new_size, t);
295 // assert postconditions
296 VERIFY(a.size() == new_size);
297 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
300 copy_tracker::reset();
301 tracker_allocator_counter::reset();
308 bool test __attribute__((unused)) = true;
309 typedef copy_tracker T;
310 typedef std::vector<T, tracker_allocator<T> > X;
312 tracker_allocator_counter::reset();
315 X::size_type old_capacity = a.capacity();
316 X::size_type new_size = old_capacity + 4;
319 copy_tracker::reset();
321 // assert preconditions
322 VERIFY(new_size > old_capacity);
325 a.assign(new_size, t);
327 // assert postconditions
328 VERIFY(a.size() == new_size);
331 VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
332 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
335 copy_tracker::reset();
336 tracker_allocator_counter::reset();
340 test_fill_assign_3_exception_guarantee()
343 bool test __attribute__((unused)) = true;
344 typedef copy_tracker T;
345 typedef std::vector<T, tracker_allocator<T> > X;
347 tracker_allocator_counter::reset();
350 X::size_type old_size = a.size();
351 X::size_type old_capacity = a.capacity();
352 X::size_type new_size = old_capacity + 4;
355 copy_tracker::reset();
356 copy_constructor::throw_on(3);
358 // assert preconditions
359 VERIFY(new_size > old_capacity);
364 a.assign(new_size, t);
371 // assert postconditions
372 VERIFY(a.size() == old_size);
373 VERIFY(a.capacity() == old_capacity);
376 VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
377 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
380 copy_tracker::reset();
381 tracker_allocator_counter::reset();
388 bool test __attribute__((unused)) = true;
389 typedef copy_tracker T;
390 typedef std::vector<T, tracker_allocator<T> > X;
393 X::size_type old_size = a.size();
394 X::size_type new_size = old_size - 2;
395 X::size_type new_value = 117;
397 copy_tracker::reset();
398 tracker_allocator_counter::reset();
401 a.assign(new_size, new_value);
403 // assert postconditions
404 VERIFY(a.size() == new_size);
405 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
408 copy_tracker::reset();
409 tracker_allocator_counter::reset();
414 // case 1: [23.2.4.1 (2)] input iterator
415 // case 2: [23.2.4.1 (2)] forward iterator, distance(first, last) <= size()
416 // case 3: [23.2.4.1 (2)]
417 // forward iterator, size() < distance(first, last) <= capacity()
418 // case 4: [23.2.4.1 (2)] forward iterator, distance(first, last) > capacity()
419 // case 5: [23.2.4.1 (2)]
420 // forward iterator, distance(first, last) > capacity(),
421 // exception guarantees
423 test_range_assign_1()
429 test_range_assign_2()
432 bool test __attribute__((unused)) = true;
433 typedef copy_tracker T;
434 typedef std::vector<T, tracker_allocator<T> > X;
439 copy_tracker::reset();
440 tracker_allocator_counter::reset();
442 // assert preconditions
443 VERIFY(b.size() < a.capacity());
446 a.assign(b.begin(), b.end());
448 // assert postconditions
449 VERIFY(a.size() == b.size());
451 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
454 copy_tracker::reset();
455 tracker_allocator_counter::reset();
459 test_range_assign_3()
462 bool test __attribute__((unused)) = true;
463 typedef copy_tracker T;
464 typedef std::vector<T, tracker_allocator<T> > X;
467 a.reserve(a.size() + 7);
470 copy_tracker::reset();
471 tracker_allocator_counter::reset();
473 // assert preconditions
474 VERIFY(a.size() < b.size());
475 VERIFY(b.size() < a.capacity());
478 a.assign(b.begin(), b.end());
480 // assert postconditions
481 VERIFY(a.size() == b.size());
483 VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
486 copy_tracker::reset();
487 tracker_allocator_counter::reset();
491 test_range_assign_4()
494 bool test __attribute__((unused)) = true;
495 typedef copy_tracker T;
496 typedef std::vector<T, tracker_allocator<T> > X;
498 tracker_allocator_counter::reset();
501 X b(a.capacity() + 7);
503 copy_tracker::reset();
505 // assert preconditions
506 VERIFY(b.size() > a.capacity());
509 a.assign(b.begin(), b.end());
511 // assert postconditions
512 VERIFY(a.size() == b.size());
515 VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
516 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
519 copy_tracker::reset();
520 tracker_allocator_counter::reset();
524 test_range_assign_4_exception_guarantee()
527 bool test __attribute__((unused)) = true;
528 typedef copy_tracker T;
529 typedef std::vector<T, tracker_allocator<T> > X;
531 tracker_allocator_counter::reset();
534 X b(a.capacity() + 7);
536 copy_tracker::reset();
537 copy_constructor::throw_on(3);
539 // assert preconditions
540 VERIFY(b.size() > a.capacity());
545 a.assign(b.begin(), b.end());
553 // assert postconditions
554 VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
555 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
558 copy_tracker::reset();
559 tracker_allocator_counter::reset();
565 test_default_ctor_exception_gurantee();
566 test_copy_ctor_exception_gurantee();
567 test_assignment_operator_1();
568 test_assignment_operator_2();
569 test_assignment_operator_3();
570 test_assignment_operator_3_exception_guarantee();
571 test_fill_assign_1();
572 test_fill_assign_2();
573 test_fill_assign_3();
574 test_fill_assign_3_exception_guarantee();
575 test_fill_assign_4();
576 test_range_assign_1();
577 test_range_assign_2();
578 test_range_assign_3();
579 test_range_assign_4();
580 test_range_assign_4_exception_guarantee();