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_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.
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.
74 // 23.2.1.1 default ctor/dtor
76 // 23.2.1.1 constructs an empty deque using the specified allocator
78 // 23.1 table 65 u.size() == 0
81 // 23.1 table 65 constant
83 // 23.2.1.2 bool empty() const
85 // 23.1 table 65 a.size() == 0
86 // 23.1 (7) a.begin() == a.end()
89 // 23.1 table 65 constant
91 // 23.2.1.2 size_type size() const
93 // 23.1 table 65 a.end() - a.begin()
96 // 23.1 table 65(A) should be constant
98 // 23.2.1 iterator begin()
99 // const_iterator begin() const
101 // const_iterator end() const
103 // 23.1 (10) pt. 4 does not throw
105 // 23.1 table 65 constant
108 defaultConstructorCheckPOD()
112 typedef std::deque
<T
> X
;
117 // assert postconditions
119 VERIFY(0 == u
.size());
120 VERIFY(u
.begin() == u
.end());
121 VERIFY(0 == std::distance(u
.begin(), u
.end()));
128 defaultConstructorCheck()
131 typedef copy_tracker T
;
132 typedef std::deque
<T
> X
;
134 copy_tracker::reset();
139 // assert postconditions
141 VERIFY(0 == u
.size());
142 VERIFY(u
.begin() == u
.end());
143 VERIFY(0 == std::distance(u
.begin(), u
.end()));
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.
156 // 23.2.1 copy constructor
159 // 22.1.1 table 65 a == X(a)
163 // 22.1.1 table 65 linear
166 copyConstructorCheck()
169 typedef copy_tracker T
;
170 typedef std::deque
<T
> X
;
172 const std::size_t copyBaseSize
= 17; // arbitrary
175 for (std::size_t i
= 0; i
< copyBaseSize
; ++i
)
177 copy_tracker::reset();
179 // assert preconditions
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())) );
188 // assert postconditions
190 VERIFY(copyBaseSize
== copy_constructor::count());
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
203 // 23.2.1.1 fill constructor
205 // 23.2.1.1 linear in N
207 fillConstructorCheck()
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();
221 // assert postconditions
222 VERIFY(n
== a
.size());
223 VERIFY(n
== copy_constructor::count());
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
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();
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
260 rangeConstructorCheckForwardIterator()
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);
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();
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
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();
305 VERIFY(rangeSize
== a
.size());
306 VERIFY(copy_constructor::count() <= (2 * rangeSize
));
310 // 23.2.1 copy assignment
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);
322 copy_tracker::reset();
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.
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();
353 VERIFY(starting_size
== a
.size());
359 VERIFY(n
== a
.size());
360 VERIFY(n
== (copy_constructor::count() + assignment_operator::count()));
361 VERIFY(starting_size
== (destructor::count() + assignment_operator::count()));
366 // 23.2.1 range assignment
367 // 23.2.1.1 deque constructors, copy, and assignment
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);
377 // erase(begin(), end());
378 // insert(begin(), first, last);
383 // forward iterators: N calls to the copy constructor, 0 reallocations
384 // input iterators: 2N calls to the copy constructor, log(N) reallocations
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();
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
418 rangeAssignmentCheckWithException()
421 typedef copy_tracker T
;
422 typedef std::deque
<T
> X
;
425 // What does "no effects" mean?
429 // 23.1.1 (9) fill assignment looking like a range assignment
431 fillAssignmentCheck2()
434 typedef copy_tracker T
;
435 typedef std::deque
<T
> X
;
438 // What does "no effects" mean?
441 // Verify that the default deque constructor offers the basic exception
444 test_default_ctor_exception_safety()
447 typedef copy_tracker T
;
448 typedef std::deque
<T
, tracker_alloc
<T
> > X
;
451 copy_constructor::throw_on(3);
452 allocation_tracker::resetCounts();
464 // assert postconditions
465 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
470 // Verify that the copy constructor offers the basic exception guarantee.
472 test_copy_ctor_exception_safety()
475 typedef copy_tracker T
;
476 typedef std::deque
<T
, tracker_alloc
<T
> > X
;
478 allocation_tracker::resetCounts();
482 copy_constructor::throw_on(3);
496 // assert postconditions
497 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
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();