2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / deque / cons / 2.cc
blobe0e7108d20b55e28e9912e2cbd0d9dc4272bdd7c
1 // 2001-12-27 pme
2 //
3 // Copyright (C) 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.1.1 deque constructors, copy, and assignment
23 #include <deque>
24 #include <iterator>
25 #include <sstream>
26 #include <testsuite_allocator.h>
27 #include <testsuite_hooks.h>
29 using __gnu_test::copy_tracker;
30 using __gnu_test::allocation_tracker;
31 using __gnu_test::tracker_alloc;
32 using __gnu_test::copy_constructor;
33 using __gnu_test::assignment_operator;
34 using __gnu_test::counter;
35 using __gnu_test::destructor;
37 typedef std::deque<counter> gdeque;
39 bool test __attribute__((unused)) = true;
41 // 23.2.1 required types
43 // A missing required type will cause a compile failure.
45 void
46 requiredTypesCheck()
48 typedef int T;
49 typedef std::deque<T> X;
51 typedef X::reference reference;
52 typedef X::const_reference const_reference;
53 typedef X::iterator iterator;
54 typedef X::const_iterator const_iterator;
55 typedef X::size_type size_type;
56 typedef X::difference_type difference_type;
57 typedef X::value_type value_type;
58 typedef X::allocator_type allocator_type;
59 typedef X::pointer pointer;
60 typedef X::const_pointer const_pointer;
61 typedef X::reverse_iterator reverse_iterator;
62 typedef X::const_reverse_iterator const_reverse_iterator;
66 // @fn defaultConstructorCheck
67 // Explicitly checks the default deque constructor and destructor for both
68 // trivial and non-trivial types. In addition, the size() and empty()
69 // member functions are explicitly checked here since it should be their
70 // first use. Checking those functions means checking the begin() and
71 // end() and their const brethren functions as well.
73 // @verbatim
74 // 23.2.1.1 default ctor/dtor
75 // effects:
76 // 23.2.1.1 constructs an empty deque using the specified allocator
77 // postconditions:
78 // 23.1 table 65 u.size() == 0
79 // throws:
80 // complexity:
81 // 23.1 table 65 constant
83 // 23.2.1.2 bool empty() const
84 // semantics:
85 // 23.1 table 65 a.size() == 0
86 // 23.1 (7) a.begin() == a.end()
87 // throws:
88 // complexity:
89 // 23.1 table 65 constant
91 // 23.2.1.2 size_type size() const
92 // semantics:
93 // 23.1 table 65 a.end() - a.begin()
94 // throws:
95 // complexity:
96 // 23.1 table 65(A) should be constant
98 // 23.2.1 iterator begin()
99 // const_iterator begin() const
100 // iterator end()
101 // const_iterator end() const
102 // throws:
103 // 23.1 (10) pt. 4 does not throw
104 // complexity:
105 // 23.1 table 65 constant
106 // @endverbatim
107 void
108 defaultConstructorCheckPOD()
110 // setup
111 typedef int T;
112 typedef std::deque<T> X;
114 // run test
115 X u;
117 // assert postconditions
118 VERIFY(u.empty());
119 VERIFY(0 == u.size());
120 VERIFY(u.begin() == u.end());
121 VERIFY(0 == std::distance(u.begin(), u.end()));
123 // teardown
127 void
128 defaultConstructorCheck()
130 // setup
131 typedef copy_tracker T;
132 typedef std::deque<T> X;
134 copy_tracker::reset();
136 // run test
137 const X u;
139 // assert postconditions
140 VERIFY(u.empty());
141 VERIFY(0 == u.size());
142 VERIFY(u.begin() == u.end());
143 VERIFY(0 == std::distance(u.begin(), u.end()));
145 // teardown
149 // @fn copyConstructorCheck()
150 // Explicitly checks the deque copy constructor. Continues verificaton of
151 // ancillary member functions documented under defaultConstructorCheck().
153 // This check also tests the push_back() member function.
155 // @verbatim
156 // 23.2.1 copy constructor
157 // effects:
158 // postconditions:
159 // 22.1.1 table 65 a == X(a)
160 // u == a
161 // throws:
162 // complexity:
163 // 22.1.1 table 65 linear
164 // @endverbatim
165 void
166 copyConstructorCheck()
168 // setup
169 typedef copy_tracker T;
170 typedef std::deque<T> X;
172 const std::size_t copyBaseSize = 17; // arbitrary
174 X a;
175 for (std::size_t i = 0; i < copyBaseSize; ++i)
176 a.push_back(i);
177 copy_tracker::reset();
179 // assert preconditions
180 VERIFY(!a.empty());
181 VERIFY(copyBaseSize == a.size());
182 VERIFY(a.begin() != a.end());
183 VERIFY( copyBaseSize == static_cast<std::size_t>(std::distance(a.begin(), a.end())) );
185 // run test
186 X u = a;
188 // assert postconditions
189 VERIFY(u == a);
190 VERIFY(copyBaseSize == copy_constructor::count());
192 // teardown
196 // @fn fillConstructorCheck()
197 // This test explicitly verifies the basic fill constructor. Like the default
198 // constructor, later tests depend on the fill constructor working correctly.
199 // That means this explicit test should preceed the later tests so the error
200 // message given on assertion failure can be more helpful n tracking the
201 // problem.
203 // 23.2.1.1 fill constructor
204 // complexity:
205 // 23.2.1.1 linear in N
206 void
207 fillConstructorCheck()
209 // setup
210 typedef copy_tracker T;
211 typedef std::deque<T> X;
213 const X::size_type n(23);
214 const X::value_type t(111);
216 copy_tracker::reset();
218 // run test
219 X a(n, t);
221 // assert postconditions
222 VERIFY(n == a.size());
223 VERIFY(n == copy_constructor::count());
225 // teardown
229 // @fn fillConstructorCheck2()
230 // Explicit check for fill constructors masqueraded as range constructors as
231 // elucidated in clause 23.1.1 paragraph 9 of the standard.
233 // 23.1.1 (9) fill constructor looking like a range constructor
234 void
235 fillConstructorCheck2()
237 typedef copy_tracker T;
238 typedef std::deque<T> X;
240 const std::size_t f = 23;
241 const std::size_t l = 111;
243 copy_tracker::reset();
245 X a(f, l);
247 VERIFY(f == a.size());
248 VERIFY(f == copy_constructor::count());
252 // @fn rangeConstructorCheckForwardIterator()
253 // This test copies from one deque to another to force the copy
254 // constructor for T to be used because the compiler will kindly
255 // elide copies if the default constructor can be used with
256 // type conversions. Trust me.
258 // 23.2.1.1 range constructor, forward iterators
259 void
260 rangeConstructorCheckForwardIterator()
262 // setup
263 typedef copy_tracker T;
264 typedef std::deque<T> X;
266 const X::size_type n(726);
267 const X::value_type t(307);
268 X source(n, t);
269 X::iterator i = source.begin();
270 X::iterator j = source.end();
271 X::size_type rangeSize = std::distance(i, j);
273 copy_tracker::reset();
275 // test
276 X a(i, j);
278 // assert postconditions
279 VERIFY(rangeSize == a.size());
280 VERIFY(copy_constructor::count() <= rangeSize);
284 // @fn rangeConstructorCheckInputIterator()
285 // An explicit check for range construction on an input iterator
286 // range, which the standard expounds upon as having a different
287 // complexity than forward iterators.
289 // 23.2.1.1 range constructor, input iterators
290 void
291 rangeConstructorCheckInputIterator()
293 typedef copy_tracker T;
294 typedef std::deque<T> X;
296 std::istringstream ibuf("1234567890123456789");
297 const X::size_type rangeSize = ibuf.str().size();
298 std::istream_iterator<char> i(ibuf);
299 std::istream_iterator<char> j;
301 copy_tracker::reset();
303 X a(i, j);
305 VERIFY(rangeSize == a.size());
306 VERIFY(copy_constructor::count() <= (2 * rangeSize));
310 // 23.2.1 copy assignment
311 void
312 copyAssignmentCheck()
314 typedef copy_tracker T;
315 typedef std::deque<T> X;
317 const X::size_type n(18);
318 const X::value_type t(1023);
319 X a(n, t);
320 X r;
322 copy_tracker::reset();
324 r = a;
326 VERIFY(r == a);
327 VERIFY(n == copy_constructor::count());
331 // 23.2.1.1 fill assignment
333 // The complexity check must check dtors+copyAssign and
334 // copyCtor+copyAssign because that's the way the SGI implementation
335 // works. Dunno if it's true standard compliant (which specifies fill
336 // assignment in terms of erase and insert only), but it should work
337 // as (most) users expect and is more efficient.
338 void
339 fillAssignmentCheck()
341 typedef copy_tracker T;
342 typedef std::deque<T> X;
344 const X::size_type starting_size(10);
345 const X::value_type starting_value(66);
346 const X::size_type n(23);
347 const X::value_type t(111);
349 X a(starting_size, starting_value);
350 copy_tracker::reset();
352 // preconditions
353 VERIFY(starting_size == a.size());
355 // test
356 a.assign(n, t);
358 // postconditions
359 VERIFY(n == a.size());
360 VERIFY(n == (copy_constructor::count() + assignment_operator::count()));
361 VERIFY(starting_size == (destructor::count() + assignment_operator::count()));
365 // @verbatim
366 // 23.2.1 range assignment
367 // 23.2.1.1 deque constructors, copy, and assignment
368 // effects:
369 // Constructs a deque equal to the range [first, last), using the
370 // specified allocator.
372 // template<typename InputIterator>
373 // assign(InputIterator first, InputIterator last);
375 // is equivalent to
377 // erase(begin(), end());
378 // insert(begin(), first, last);
380 // postconditions:
381 // throws:
382 // complexity:
383 // forward iterators: N calls to the copy constructor, 0 reallocations
384 // input iterators: 2N calls to the copy constructor, log(N) reallocations
385 // @endverbatim
386 void
387 rangeAssignmentCheck()
389 typedef copy_tracker T;
390 typedef std::deque<T> X;
392 const X::size_type source_size(726);
393 const X::value_type source_value(307);
394 const X::size_type starting_size(10);
395 const X::value_type starting_value(66);
397 X source(source_size, source_value);
398 X::iterator i = source.begin();
399 X::iterator j = source.end();
400 X::size_type rangeSize = std::distance(i, j);
402 X a(starting_size, starting_value);
403 VERIFY(starting_size == a.size());
405 copy_tracker::reset();
407 a.assign(i, j);
409 VERIFY(source == a);
410 VERIFY(rangeSize == (copy_constructor::count() + assignment_operator::count()));
411 VERIFY(starting_size == (destructor::count() + assignment_operator::count()));
415 // 23.1 (10) range assignment
416 // 23.2.1.3 with exception
417 void
418 rangeAssignmentCheckWithException()
420 // setup
421 typedef copy_tracker T;
422 typedef std::deque<T> X;
424 // test
425 // What does "no effects" mean?
429 // 23.1.1 (9) fill assignment looking like a range assignment
430 void
431 fillAssignmentCheck2()
433 // setup
434 typedef copy_tracker T;
435 typedef std::deque<T> X;
437 // test
438 // What does "no effects" mean?
441 // Verify that the default deque constructor offers the basic exception
442 // guarantee.
443 void
444 test_default_ctor_exception_safety()
446 // setup
447 typedef copy_tracker T;
448 typedef std::deque<T, tracker_alloc<T> > X;
450 T::reset();
451 copy_constructor::throw_on(3);
452 allocation_tracker::resetCounts();
454 // test
457 X a(7);
458 VERIFY( false );
460 catch (...)
464 // assert postconditions
465 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
467 // teardown
470 // Verify that the copy constructor offers the basic exception guarantee.
471 void
472 test_copy_ctor_exception_safety()
474 // setup
475 typedef copy_tracker T;
476 typedef std::deque<T, tracker_alloc<T> > X;
478 allocation_tracker::resetCounts();
480 X a(7);
481 T::reset();
482 copy_constructor::throw_on(3);
485 // test
488 X u(a);
489 VERIFY(false);
491 catch (...)
496 // assert postconditions
497 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
499 // teardown
503 int main()
505 // basic functionality and standard conformance checks
506 requiredTypesCheck();
507 defaultConstructorCheckPOD();
508 defaultConstructorCheck();
509 test_default_ctor_exception_safety();
510 copyConstructorCheck();
511 test_copy_ctor_exception_safety();
512 fillConstructorCheck();
513 fillConstructorCheck2();
514 rangeConstructorCheckInputIterator();
515 rangeConstructorCheckForwardIterator();
516 copyAssignmentCheck();
517 fillAssignmentCheck();
518 fillAssignmentCheck2();
519 rangeAssignmentCheck();
520 rangeAssignmentCheckWithException();
521 return 0;