2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / cons / 4.cc
blob12564b9ef535ce036b64073f75b7b455100997cc
1 // 1999-06-29 bkoz
3 // Copyright (C) 1999-2001, 2002, 2003 Free Software Foundation, Inc.
4 //
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 2, or (at your option)
9 // any later version.
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 COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // 23.2.4.1 vector constructors, copy, and assignment
23 #include <vector>
24 #include <string>
25 #include <testsuite_allocator.h>
26 #include <testsuite_hooks.h>
28 using __gnu_test::copy_tracker;
29 using __gnu_test::allocation_tracker;
30 using __gnu_test::tracker_alloc;
31 using __gnu_test::copy_constructor;
32 using __gnu_test::assignment_operator;
34 // @fn test_default_ctor_exception_gurantee This test verifies that if
35 // one of the vector's contained objects throws an exception from its
36 // constructor while the vector is being constructed and filled with
37 // default values, all memory is returned to the allocator whence it
38 // came.
39 void
40 test_default_ctor_exception_gurantee()
42 // setup
43 bool test __attribute__((unused)) = true;
44 typedef copy_tracker T;
45 typedef std::vector<T, tracker_alloc<T> > X;
47 copy_tracker::reset();
48 copy_constructor::throw_on(3);
49 allocation_tracker::resetCounts();
51 // run test
52 try
54 X a(7);
55 VERIFY(false);
57 catch (...)
61 // assert postconditions
62 VERIFY( allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal() );
64 // teardown
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.
71 void
72 test_copy_ctor_exception_gurantee()
74 // setup
75 bool test __attribute__((unused)) = true;
76 typedef copy_tracker T;
77 typedef std::vector<T, tracker_alloc<T> > X;
79 allocation_tracker::resetCounts();
81 X a(7);
82 copy_tracker::reset();
83 copy_constructor::throw_on(3);
85 // run test
86 try
88 X u(a);
89 VERIFY(false);
91 catch (...)
96 // assert postconditions
97 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
99 // teardown
100 copy_tracker::reset();
101 allocation_tracker::resetCounts();
104 // operator=()
106 // case 1: lhs.size() > rhs.size()
107 // case 2: lhs.size() < rhs.size() < lhs.capacity()
108 // case 3: lhs.capacity() < rhs.size()
110 void
111 test_assignment_operator_1()
113 // setup
114 bool test __attribute__((unused)) = true;
115 typedef copy_tracker T;
116 typedef std::vector<T, tracker_alloc<T> > X;
118 X r(9);
119 X a(r.size() - 2);
120 copy_tracker::reset();
121 allocation_tracker::resetCounts();
123 // preconditions
124 VERIFY(r.size() > a.size());
126 // run test
127 r = a;
129 // assert postconditions
130 VERIFY(r == a);
131 VERIFY(allocation_tracker::allocationTotal() == 0);
133 // teardown
134 copy_tracker::reset();
135 allocation_tracker::resetCounts();
138 void
139 test_assignment_operator_2()
141 // setup
142 bool test __attribute__((unused)) = true;
143 typedef copy_tracker T;
144 typedef std::vector<T, tracker_alloc<T> > X;
146 X r(1);
147 r.reserve(17);
148 X a(r.size() + 7);
149 copy_tracker::reset();
150 allocation_tracker::resetCounts();
152 // preconditions
153 VERIFY(r.size() < a.size());
154 VERIFY(a.size() < r.capacity());
156 // run test
157 r = a;
159 // assert postconditions
160 VERIFY(r == a);
161 VERIFY(allocation_tracker::allocationTotal() == 0);
163 // teardown
164 copy_tracker::reset();
165 allocation_tracker::resetCounts();
168 void
169 test_assignment_operator_3()
171 // setup
172 bool test __attribute__((unused)) = true;
173 typedef copy_tracker T;
174 typedef std::vector<T, tracker_alloc<T> > X;
176 allocation_tracker::resetCounts();
178 X r(1);
179 X a(r.capacity() + 7);
180 copy_tracker::reset();
182 // preconditions
183 VERIFY(r.capacity() < a.size());
185 // run test
186 r = a;
188 // assert postconditions
189 VERIFY(r == a);
191 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
193 // teardown
194 copy_tracker::reset();
195 allocation_tracker::resetCounts();
198 void
199 test_assignment_operator_3_exception_guarantee()
201 // setup
202 bool test __attribute__((unused)) = true;
203 typedef copy_tracker T;
204 typedef std::vector<T, tracker_alloc<T> > X;
206 allocation_tracker::resetCounts();
208 X r(1);
209 X a(r.capacity() + 7);
210 copy_tracker::reset();
211 copy_constructor::throw_on(3);
213 // preconditions
214 VERIFY(r.capacity() < a.size());
216 // run test
219 r = a;
220 VERIFY(false);
222 catch (...)
227 // assert postconditions
228 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
230 // teardown
231 copy_tracker::reset();
232 allocation_tracker::resetCounts();
235 // fill assign()
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
243 void
244 test_fill_assign_1()
246 // setup
247 bool test __attribute__((unused)) = true;
248 typedef copy_tracker T;
249 typedef std::vector<T, tracker_alloc<T> > X;
251 X a(7);
252 X::size_type old_size = a.size();
253 X::size_type new_size = old_size - 2;
254 const T t;
256 copy_tracker::reset();
257 allocation_tracker::resetCounts();
259 // run test
260 a.assign(new_size, t);
262 // assert postconditions
263 VERIFY(a.size() == new_size);
264 VERIFY(allocation_tracker::allocationTotal() == 0);
266 // teardown
267 copy_tracker::reset();
268 allocation_tracker::resetCounts();
271 void
272 test_fill_assign_2()
274 // setup
275 bool test __attribute__((unused)) = true;
276 typedef copy_tracker T;
277 typedef std::vector<T, tracker_alloc<T> > X;
279 X a(7);
280 a.reserve(11);
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;
284 const T t;
286 copy_tracker::reset();
287 allocation_tracker::resetCounts();
289 // assert preconditions
290 VERIFY(old_size < new_size);
291 VERIFY(new_size <= old_capacity);
293 // run test
294 a.assign(new_size, t);
296 // assert postconditions
297 VERIFY(a.size() == new_size);
298 VERIFY(allocation_tracker::allocationTotal() == 0);
300 // teardown
301 copy_tracker::reset();
302 allocation_tracker::resetCounts();
305 void
306 test_fill_assign_3()
308 // setup
309 bool test __attribute__((unused)) = true;
310 typedef copy_tracker T;
311 typedef std::vector<T, tracker_alloc<T> > X;
313 allocation_tracker::resetCounts();
315 X a(7);
316 X::size_type old_capacity = a.capacity();
317 X::size_type new_size = old_capacity + 4;
318 const T t;
320 copy_tracker::reset();
322 // assert preconditions
323 VERIFY(new_size > old_capacity);
325 // run test
326 a.assign(new_size, t);
328 // assert postconditions
329 VERIFY(a.size() == new_size);
332 VERIFY(allocation_tracker::allocationTotal() > 0);
333 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
335 // teardown
336 copy_tracker::reset();
337 allocation_tracker::resetCounts();
340 void
341 test_fill_assign_3_exception_guarantee()
343 // setup
344 bool test __attribute__((unused)) = true;
345 typedef copy_tracker T;
346 typedef std::vector<T, tracker_alloc<T> > X;
348 allocation_tracker::resetCounts();
350 X a(7);
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;
354 const T t;
356 copy_tracker::reset();
357 copy_constructor::throw_on(3);
359 // assert preconditions
360 VERIFY(new_size > old_capacity);
362 // run test
365 a.assign(new_size, t);
366 VERIFY(false);
368 catch (...)
372 // assert postconditions
373 VERIFY(a.size() == old_size);
374 VERIFY(a.capacity() == old_capacity);
377 VERIFY(allocation_tracker::allocationTotal() > 0);
378 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
380 // teardown
381 copy_tracker::reset();
382 allocation_tracker::resetCounts();
385 void
386 test_fill_assign_4()
388 // setup
389 bool test __attribute__((unused)) = true;
390 typedef copy_tracker T;
391 typedef std::vector<T, tracker_alloc<T> > X;
393 X a(7);
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 allocation_tracker::resetCounts();
401 // run test
402 a.assign(new_size, new_value);
404 // assert postconditions
405 VERIFY(a.size() == new_size);
406 VERIFY(allocation_tracker::allocationTotal() == 0);
408 // teardown
409 copy_tracker::reset();
410 allocation_tracker::resetCounts();
413 // range assign()
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
423 void
424 test_range_assign_1()
426 // @TODO
429 void
430 test_range_assign_2()
432 // setup
433 bool test __attribute__((unused)) = true;
434 typedef copy_tracker T;
435 typedef std::vector<T, tracker_alloc<T> > X;
437 X a(7);
438 X b(3);
440 copy_tracker::reset();
441 allocation_tracker::resetCounts();
443 // assert preconditions
444 VERIFY(b.size() < a.capacity());
446 // run test
447 a.assign(b.begin(), b.end());
449 // assert postconditions
450 VERIFY(a.size() == b.size());
451 VERIFY(a == b);
452 VERIFY(allocation_tracker::allocationTotal() == 0);
454 // teardown
455 copy_tracker::reset();
456 allocation_tracker::resetCounts();
459 void
460 test_range_assign_3()
462 // setup
463 bool test __attribute__((unused)) = true;
464 typedef copy_tracker T;
465 typedef std::vector<T, tracker_alloc<T> > X;
467 X a(7);
468 a.reserve(a.size() + 7);
469 X b(a.size() + 3);
471 copy_tracker::reset();
472 allocation_tracker::resetCounts();
474 // assert preconditions
475 VERIFY(a.size() < b.size());
476 VERIFY(b.size() < a.capacity());
478 // run test
479 a.assign(b.begin(), b.end());
481 // assert postconditions
482 VERIFY(a.size() == b.size());
483 VERIFY(a == b);
484 VERIFY(allocation_tracker::allocationTotal() == 0);
486 // teardown
487 copy_tracker::reset();
488 allocation_tracker::resetCounts();
491 void
492 test_range_assign_4()
494 // setup
495 bool test __attribute__((unused)) = true;
496 typedef copy_tracker T;
497 typedef std::vector<T, tracker_alloc<T> > X;
499 allocation_tracker::resetCounts();
501 X a(7);
502 X b(a.capacity() + 7);
504 copy_tracker::reset();
506 // assert preconditions
507 VERIFY(b.size() > a.capacity());
509 // run test
510 a.assign(b.begin(), b.end());
512 // assert postconditions
513 VERIFY(a.size() == b.size());
514 VERIFY(a == b);
516 VERIFY(allocation_tracker::allocationTotal() > 0);
517 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
519 // teardown
520 copy_tracker::reset();
521 allocation_tracker::resetCounts();
524 void
525 test_range_assign_4_exception_guarantee()
527 // setup
528 bool test __attribute__((unused)) = true;
529 typedef copy_tracker T;
530 typedef std::vector<T, tracker_alloc<T> > X;
532 allocation_tracker::resetCounts();
534 X a(7);
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());
543 // run test
546 a.assign(b.begin(), b.end());
547 VERIFY(false);
549 catch (...)
554 // assert postconditions
555 VERIFY(allocation_tracker::allocationTotal() > 0);
556 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
558 // teardown
559 copy_tracker::reset();
560 allocation_tracker::resetCounts();
564 int main()
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();
583 return 0;