Add an UNSPEC_PROLOGUE_USE to prevent the link register from being considered dead.
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / deque_ctor.cc
blob50a813368bb722510909315910e00ceab183de17
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_cxx_test::copy_tracker;
30 using __gnu_cxx_test::allocation_tracker;
31 using __gnu_cxx_test::tracker_alloc;
32 using __gnu_cxx_test::copy_constructor;
33 using __gnu_cxx_test::assignment_operator;
34 using __gnu_cxx_test::counter;
35 using __gnu_cxx_test::destructor;
37 typedef std::deque<counter> gdeque;
39 bool test = true;
41 // see http://gcc.gnu.org/ml/libstdc++/2001-11/msg00139.html
42 void
43 test01()
45 assert_count (0);
47 gdeque d(10);
48 assert_count (10);
50 assert_count (0);
54 // 23.2.1 required types
56 // A missing required type will cause a compile failure.
58 void
59 requiredTypesCheck()
61 typedef int T;
62 typedef std::deque<T> X;
64 typedef X::reference reference;
65 typedef X::const_reference const_reference;
66 typedef X::iterator iterator;
67 typedef X::const_iterator const_iterator;
68 typedef X::size_type size_type;
69 typedef X::difference_type difference_type;
70 typedef X::value_type value_type;
71 typedef X::allocator_type allocator_type;
72 typedef X::pointer pointer;
73 typedef X::const_pointer const_pointer;
74 typedef X::reverse_iterator reverse_iterator;
75 typedef X::const_reverse_iterator const_reverse_iterator;
79 // @fn defaultConstructorCheck
80 // Explicitly checks the default deque constructor and destructor for both
81 // trivial and non-trivial types. In addition, the size() and empty()
82 // member functions are explicitly checked here since it should be their
83 // first use. Checking those functions means checking the begin() and
84 // end() and their const brethren functions as well.
86 // @verbatim
87 // 23.2.1.1 default ctor/dtor
88 // effects:
89 // 23.2.1.1 constructs an empty deque using the specified allocator
90 // postconditions:
91 // 23.1 table 65 u.size() == 0
92 // throws:
93 // complexity:
94 // 23.1 table 65 constant
96 // 23.2.1.2 bool empty() const
97 // semantics:
98 // 23.1 table 65 a.size() == 0
99 // 23.1 (7) a.begin() == a.end()
100 // throws:
101 // complexity:
102 // 23.1 table 65 constant
104 // 23.2.1.2 size_type size() const
105 // semantics:
106 // 23.1 table 65 a.end() - a.begin()
107 // throws:
108 // complexity:
109 // 23.1 table 65(A) should be constant
111 // 23.2.1 iterator begin()
112 // const_iterator begin() const
113 // iterator end()
114 // const_iterator end() const
115 // throws:
116 // 23.1 (10) pt. 4 does not throw
117 // complexity:
118 // 23.1 table 65 constant
119 // @endverbatim
120 void
121 defaultConstructorCheckPOD()
123 // setup
124 typedef int T;
125 typedef std::deque<T> X;
127 // run test
128 X u;
130 // assert postconditions
131 VERIFY(u.empty());
132 VERIFY(0 == u.size());
133 VERIFY(u.begin() == u.end());
134 VERIFY(0 == std::distance(u.begin(), u.end()));
136 // teardown
140 void
141 defaultConstructorCheck()
143 // setup
144 typedef copy_tracker T;
145 typedef std::deque<T> X;
147 copy_tracker::reset();
149 // run test
150 const X u;
152 // assert postconditions
153 VERIFY(u.empty());
154 VERIFY(0 == u.size());
155 VERIFY(u.begin() == u.end());
156 VERIFY(0 == std::distance(u.begin(), u.end()));
158 // teardown
162 // @fn copyConstructorCheck()
163 // Explicitly checks the deque copy constructor. Continues verificaton of
164 // ancillary member functions documented under defaultConstructorCheck().
166 // This check also tests the push_back() member function.
168 // @verbatim
169 // 23.2.1 copy constructor
170 // effects:
171 // postconditions:
172 // 22.1.1 table 65 a == X(a)
173 // u == a
174 // throws:
175 // complexity:
176 // 22.1.1 table 65 linear
177 // @endverbatim
178 void
179 copyConstructorCheck()
181 // setup
182 typedef copy_tracker T;
183 typedef std::deque<T> X;
185 const int copyBaseSize = 17; // arbitrary
187 X a;
188 for (int i = 0; i < copyBaseSize; ++i)
189 a.push_back(i);
190 copy_tracker::reset();
192 // assert preconditions
193 VERIFY(!a.empty());
194 VERIFY(copyBaseSize == a.size());
195 VERIFY(a.begin() != a.end());
196 VERIFY(copyBaseSize == std::distance(a.begin(), a.end()));
198 // run test
199 X u = a;
201 // assert postconditions
202 VERIFY(u == a);
203 VERIFY(copyBaseSize == copy_constructor::count());
205 // teardown
209 // @fn fillConstructorCheck()
210 // This test explicitly verifies the basic fill constructor. Like the default
211 // constructor, later tests depend on the fill constructor working correctly.
212 // That means this explicit test should preceed the later tests so the error
213 // message given on assertion failure can be more helpful n tracking the
214 // problem.
216 // 23.2.1.1 fill constructor
217 // complexity:
218 // 23.2.1.1 linear in N
219 void
220 fillConstructorCheck()
222 // setup
223 typedef copy_tracker T;
224 typedef std::deque<T> X;
226 const X::size_type n(23);
227 const X::value_type t(111);
229 copy_tracker::reset();
231 // run test
232 X a(n, t);
234 // assert postconditions
235 VERIFY(n == a.size());
236 VERIFY(n == copy_constructor::count());
238 // teardown
242 // @fn fillConstructorCheck2()
243 // Explicit check for fill constructors masqueraded as range constructors as
244 // elucidated in clause 23.1.1 paragraph 9 of the standard.
246 // 23.1.1 (9) fill constructor looking like a range constructor
247 void
248 fillConstructorCheck2()
250 typedef copy_tracker T;
251 typedef std::deque<T> X;
253 const int f = 23;
254 const int l = 111;
256 copy_tracker::reset();
258 X a(f, l);
260 VERIFY(f == a.size());
261 VERIFY(f == copy_constructor::count());
265 // @fn rangeConstructorCheckForwardIterator()
266 // This test copies from one deque to another to force the copy
267 // constructor for T to be used because the compiler will kindly
268 // elide copies if the default constructor can be used with
269 // type conversions. Trust me.
271 // 23.2.1.1 range constructor, forward iterators
272 void
273 rangeConstructorCheckForwardIterator()
275 // setup
276 typedef copy_tracker T;
277 typedef std::deque<T> X;
279 const X::size_type n(726);
280 const X::value_type t(307);
281 X source(n, t);
282 X::iterator i = source.begin();
283 X::iterator j = source.end();
284 X::size_type rangeSize = std::distance(i, j);
286 copy_tracker::reset();
288 // test
289 X a(i, j);
291 // assert postconditions
292 VERIFY(rangeSize == a.size());
293 VERIFY(copy_constructor::count() <= rangeSize);
297 // @fn rangeConstructorCheckInputIterator()
298 // An explicit check for range construction on an input iterator
299 // range, which the standard expounds upon as having a different
300 // complexity than forward iterators.
302 // 23.2.1.1 range constructor, input iterators
303 void
304 rangeConstructorCheckInputIterator()
306 typedef copy_tracker T;
307 typedef std::deque<T> X;
309 std::istringstream ibuf("1234567890123456789");
310 const X::size_type rangeSize = ibuf.str().size();
311 std::istream_iterator<char> i(ibuf);
312 std::istream_iterator<char> j;
314 copy_tracker::reset();
316 X a(i, j);
318 VERIFY(rangeSize == a.size());
319 VERIFY(copy_constructor::count() <= (2 * rangeSize));
323 // 23.2.1 copy assignment
324 void
325 copyAssignmentCheck()
327 typedef copy_tracker T;
328 typedef std::deque<T> X;
330 const X::size_type n(18);
331 const X::value_type t(1023);
332 X a(n, t);
333 X r;
335 copy_tracker::reset();
337 r = a;
339 VERIFY(r == a);
340 VERIFY(n == copy_constructor::count());
344 // 23.2.1.1 fill assignment
346 // The complexity check must check dtors+copyAssign and
347 // copyCtor+copyAssign because that's the way the SGI implementation
348 // works. Dunno if it's true standard compliant (which specifies fill
349 // assignment in terms of erase and insert only), but it should work
350 // as (most) users expect and is more efficient.
351 void
352 fillAssignmentCheck()
354 typedef copy_tracker T;
355 typedef std::deque<T> X;
357 const X::size_type starting_size(10);
358 const X::value_type starting_value(66);
359 const X::size_type n(23);
360 const X::value_type t(111);
362 X a(starting_size, starting_value);
363 copy_tracker::reset();
365 // preconditions
366 VERIFY(starting_size == a.size());
368 // test
369 a.assign(n, t);
371 // postconditions
372 VERIFY(n == a.size());
373 VERIFY(n == (copy_constructor::count() + assignment_operator::count()));
374 VERIFY(starting_size == (destructor::count() + assignment_operator::count()));
378 // @verbatim
379 // 23.2.1 range assignment
380 // 23.2.1.1 deque constructors, copy, and assignment
381 // effects:
382 // Constructs a deque equal to the range [first, last), using the
383 // specified allocator.
385 // template<typename InputIterator>
386 // assign(InputIterator first, InputIterator last);
388 // is equivalent to
390 // erase(begin(), end());
391 // insert(begin(), first, last);
393 // postconditions:
394 // throws:
395 // complexity:
396 // forward iterators: N calls to the copy constructor, 0 reallocations
397 // input iterators: 2N calls to the copy constructor, log(N) reallocations
398 // @endverbatim
399 void
400 rangeAssignmentCheck()
402 typedef copy_tracker T;
403 typedef std::deque<T> X;
405 const X::size_type source_size(726);
406 const X::value_type source_value(307);
407 const X::size_type starting_size(10);
408 const X::value_type starting_value(66);
410 X source(source_size, source_value);
411 X::iterator i = source.begin();
412 X::iterator j = source.end();
413 X::size_type rangeSize = std::distance(i, j);
415 X a(starting_size, starting_value);
416 VERIFY(starting_size == a.size());
418 copy_tracker::reset();
420 a.assign(i, j);
422 VERIFY(source == a);
423 VERIFY(rangeSize == (copy_constructor::count() + assignment_operator::count()));
424 VERIFY(starting_size == (destructor::count() + assignment_operator::count()));
428 // 23.1 (10) range assignment
429 // 23.2.1.3 with exception
430 void
431 rangeAssignmentCheckWithException()
433 // setup
434 typedef copy_tracker T;
435 typedef std::deque<T> X;
437 // test
438 // What does "no effects" mean?
442 // 23.1.1 (9) fill assignment looking like a range assignment
443 void
444 fillAssignmentCheck2()
446 // setup
447 typedef copy_tracker T;
448 typedef std::deque<T> X;
450 // test
451 // What does "no effects" mean?
454 // Verify that the default deque constructor offers the basic exception
455 // guarantee.
456 void
457 test_default_ctor_exception_safety()
459 // setup
460 typedef copy_tracker T;
461 typedef std::deque<T, tracker_alloc<T> > X;
463 T::reset();
464 copy_constructor::throw_on(3);
465 allocation_tracker::resetCounts();
467 // test
470 X a(7);
471 VERIFY(("no exception thrown", false));
473 catch (...)
477 // assert postconditions
478 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
480 // teardown
483 // Verify that the copy constructor offers the basic exception guarantee.
484 void
485 test_copy_ctor_exception_safety()
487 // setup
488 typedef copy_tracker T;
489 typedef std::deque<T, tracker_alloc<T> > X;
491 allocation_tracker::resetCounts();
493 X a(7);
494 T::reset();
495 copy_constructor::throw_on(3);
498 // test
501 X u(a);
502 VERIFY(("no exception thrown", false));
504 catch (...)
509 // assert postconditions
510 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
512 // teardown
516 int main()
518 // basic functionality and standard conformance checks
519 requiredTypesCheck();
520 defaultConstructorCheckPOD();
521 defaultConstructorCheck();
522 test_default_ctor_exception_safety();
523 copyConstructorCheck();
524 test_copy_ctor_exception_safety();
525 fillConstructorCheck();
526 fillConstructorCheck2();
527 rangeConstructorCheckInputIterator();
528 rangeConstructorCheckForwardIterator();
529 copyAssignmentCheck();
530 fillAssignmentCheck();
531 fillAssignmentCheck2();
532 rangeAssignmentCheck();
533 rangeAssignmentCheckWithException();
535 // specific bug fix checks
536 test01();
538 return !test;