3 // Copyright (C) 2001, 2002, 2003 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 2, 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 COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // 23.2.1.1 deque constructors, copy, and assignment
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
;
41 // see http://gcc.gnu.org/ml/libstdc++/2001-11/msg00139.html
54 // 23.2.1 required types
56 // A missing required type will cause a compile failure.
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.
87 // 23.2.1.1 default ctor/dtor
89 // 23.2.1.1 constructs an empty deque using the specified allocator
91 // 23.1 table 65 u.size() == 0
94 // 23.1 table 65 constant
96 // 23.2.1.2 bool empty() const
98 // 23.1 table 65 a.size() == 0
99 // 23.1 (7) a.begin() == a.end()
102 // 23.1 table 65 constant
104 // 23.2.1.2 size_type size() const
106 // 23.1 table 65 a.end() - a.begin()
109 // 23.1 table 65(A) should be constant
111 // 23.2.1 iterator begin()
112 // const_iterator begin() const
114 // const_iterator end() const
116 // 23.1 (10) pt. 4 does not throw
118 // 23.1 table 65 constant
121 defaultConstructorCheckPOD()
125 typedef std::deque
<T
> X
;
130 // assert postconditions
132 VERIFY(0 == u
.size());
133 VERIFY(u
.begin() == u
.end());
134 VERIFY(0 == std::distance(u
.begin(), u
.end()));
141 defaultConstructorCheck()
144 typedef copy_tracker T
;
145 typedef std::deque
<T
> X
;
147 copy_tracker::reset();
152 // assert postconditions
154 VERIFY(0 == u
.size());
155 VERIFY(u
.begin() == u
.end());
156 VERIFY(0 == std::distance(u
.begin(), u
.end()));
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.
169 // 23.2.1 copy constructor
172 // 22.1.1 table 65 a == X(a)
176 // 22.1.1 table 65 linear
179 copyConstructorCheck()
182 typedef copy_tracker T
;
183 typedef std::deque
<T
> X
;
185 const int copyBaseSize
= 17; // arbitrary
188 for (int i
= 0; i
< copyBaseSize
; ++i
)
190 copy_tracker::reset();
192 // assert preconditions
194 VERIFY(copyBaseSize
== a
.size());
195 VERIFY(a
.begin() != a
.end());
196 VERIFY(copyBaseSize
== std::distance(a
.begin(), a
.end()));
201 // assert postconditions
203 VERIFY(copyBaseSize
== copy_constructor::count());
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
216 // 23.2.1.1 fill constructor
218 // 23.2.1.1 linear in N
220 fillConstructorCheck()
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();
234 // assert postconditions
235 VERIFY(n
== a
.size());
236 VERIFY(n
== copy_constructor::count());
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
248 fillConstructorCheck2()
250 typedef copy_tracker T
;
251 typedef std::deque
<T
> X
;
256 copy_tracker::reset();
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
273 rangeConstructorCheckForwardIterator()
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);
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();
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
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();
318 VERIFY(rangeSize
== a
.size());
319 VERIFY(copy_constructor::count() <= (2 * rangeSize
));
323 // 23.2.1 copy assignment
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);
335 copy_tracker::reset();
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.
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();
366 VERIFY(starting_size
== a
.size());
372 VERIFY(n
== a
.size());
373 VERIFY(n
== (copy_constructor::count() + assignment_operator::count()));
374 VERIFY(starting_size
== (destructor::count() + assignment_operator::count()));
379 // 23.2.1 range assignment
380 // 23.2.1.1 deque constructors, copy, and assignment
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);
390 // erase(begin(), end());
391 // insert(begin(), first, last);
396 // forward iterators: N calls to the copy constructor, 0 reallocations
397 // input iterators: 2N calls to the copy constructor, log(N) reallocations
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();
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
431 rangeAssignmentCheckWithException()
434 typedef copy_tracker T
;
435 typedef std::deque
<T
> X
;
438 // What does "no effects" mean?
442 // 23.1.1 (9) fill assignment looking like a range assignment
444 fillAssignmentCheck2()
447 typedef copy_tracker T
;
448 typedef std::deque
<T
> X
;
451 // What does "no effects" mean?
454 // Verify that the default deque constructor offers the basic exception
457 test_default_ctor_exception_safety()
460 typedef copy_tracker T
;
461 typedef std::deque
<T
, tracker_alloc
<T
> > X
;
464 copy_constructor::throw_on(3);
465 allocation_tracker::resetCounts();
471 VERIFY(("no exception thrown", false));
477 // assert postconditions
478 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
483 // Verify that the copy constructor offers the basic exception guarantee.
485 test_copy_ctor_exception_safety()
488 typedef copy_tracker T
;
489 typedef std::deque
<T
, tracker_alloc
<T
> > X
;
491 allocation_tracker::resetCounts();
495 copy_constructor::throw_on(3);
502 VERIFY(("no exception thrown", false));
509 // assert postconditions
510 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
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