3 // Copyright (C) 2001-2013 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 3, 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 COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // 23.2.1.1 deque constructors, copy, and assignment
25 #include <testsuite_allocator.h>
26 #include <testsuite_hooks.h>
28 using __gnu_test::copy_tracker
;
29 using __gnu_test::tracker_allocator_counter
;
30 using __gnu_test::tracker_allocator
;
31 using __gnu_test::copy_constructor
;
32 using __gnu_test::assignment_operator
;
33 using __gnu_test::object_counter
;
34 using __gnu_test::destructor
;
36 typedef std::deque
<object_counter
> gdeque
;
38 bool test
__attribute__((unused
)) = true;
40 // 23.2.1 required types
42 // A missing required type will cause a compile failure.
48 typedef std::deque
<T
> X
;
50 typedef X::reference reference
;
51 typedef X::const_reference const_reference
;
52 typedef X::iterator iterator
;
53 typedef X::const_iterator const_iterator
;
54 typedef X::size_type size_type
;
55 typedef X::difference_type difference_type
;
56 typedef X::value_type value_type
;
57 typedef X::allocator_type allocator_type
;
58 typedef X::pointer pointer
;
59 typedef X::const_pointer const_pointer
;
60 typedef X::reverse_iterator reverse_iterator
;
61 typedef X::const_reverse_iterator const_reverse_iterator
;
65 // @fn defaultConstructorCheck
66 // Explicitly checks the default deque constructor and destructor for both
67 // trivial and non-trivial types. In addition, the size() and empty()
68 // member functions are explicitly checked here since it should be their
69 // first use. Checking those functions means checking the begin() and
70 // end() and their const brethren functions as well.
73 // 23.2.1.1 default ctor/dtor
75 // 23.2.1.1 constructs an empty deque using the specified allocator
77 // 23.1 table 65 u.size() == 0
80 // 23.1 table 65 constant
82 // 23.2.1.2 bool empty() const
84 // 23.1 table 65 a.size() == 0
85 // 23.1 (7) a.begin() == a.end()
88 // 23.1 table 65 constant
90 // 23.2.1.2 size_type size() const
92 // 23.1 table 65 a.end() - a.begin()
95 // 23.1 table 65(A) should be constant
97 // 23.2.1 iterator begin()
98 // const_iterator begin() const
100 // const_iterator end() const
102 // 23.1 (10) pt. 4 does not throw
104 // 23.1 table 65 constant
107 defaultConstructorCheckPOD()
111 typedef std::deque
<T
> X
;
116 // assert postconditions
118 VERIFY(0 == u
.size());
119 VERIFY(u
.begin() == u
.end());
120 VERIFY(0 == std::distance(u
.begin(), u
.end()));
127 defaultConstructorCheck()
130 typedef copy_tracker T
;
131 typedef std::deque
<T
> X
;
133 copy_tracker::reset();
138 // assert postconditions
140 VERIFY(0 == u
.size());
141 VERIFY(u
.begin() == u
.end());
142 VERIFY(0 == std::distance(u
.begin(), u
.end()));
148 // @fn copyConstructorCheck()
149 // Explicitly checks the deque copy constructor. Continues verificaton of
150 // ancillary member functions documented under defaultConstructorCheck().
152 // This check also tests the push_back() member function.
155 // 23.2.1 copy constructor
158 // 22.1.1 table 65 a == X(a)
162 // 22.1.1 table 65 linear
165 copyConstructorCheck()
168 typedef copy_tracker T
;
169 typedef std::deque
<T
> X
;
171 const std::size_t copyBaseSize
= 17; // arbitrary
174 for (std::size_t i
= 0; i
< copyBaseSize
; ++i
)
176 copy_tracker::reset();
178 // assert preconditions
180 VERIFY(copyBaseSize
== a
.size());
181 VERIFY(a
.begin() != a
.end());
182 VERIFY( copyBaseSize
== static_cast<std::size_t>(std::distance(a
.begin(), a
.end())) );
187 // assert postconditions
189 VERIFY(copyBaseSize
== copy_constructor::count());
195 // @fn fillConstructorCheck()
196 // This test explicitly verifies the basic fill constructor. Like the default
197 // constructor, later tests depend on the fill constructor working correctly.
198 // That means this explicit test should precede the later tests so the error
199 // message given on assertion failure can be more helpful n tracking the
202 // 23.2.1.1 fill constructor
204 // 23.2.1.1 linear in N
206 fillConstructorCheck()
209 typedef copy_tracker T
;
210 typedef std::deque
<T
> X
;
212 const X::size_type
n(23);
213 const X::value_type
t(111);
215 copy_tracker::reset();
220 // assert postconditions
221 VERIFY(n
== a
.size());
222 VERIFY(n
== copy_constructor::count());
228 // @fn fillConstructorCheck2()
229 // Explicit check for fill constructors masqueraded as range constructors as
230 // elucidated in clause 23.1.1 paragraph 9 of the standard.
232 // 23.1.1 (9) fill constructor looking like a range constructor
234 fillConstructorCheck2()
236 typedef copy_tracker T
;
237 typedef std::deque
<T
> X
;
239 const std::size_t f
= 23;
240 const std::size_t l
= 111;
242 copy_tracker::reset();
246 VERIFY(f
== a
.size());
247 VERIFY(f
== copy_constructor::count());
251 // @fn rangeConstructorCheckForwardIterator()
252 // This test copies from one deque to another to force the copy
253 // constructor for T to be used because the compiler will kindly
254 // elide copies if the default constructor can be used with
255 // type conversions. Trust me.
257 // 23.2.1.1 range constructor, forward iterators
259 rangeConstructorCheckForwardIterator()
262 typedef copy_tracker T
;
263 typedef std::deque
<T
> X
;
265 const X::size_type
n(726);
266 const X::value_type
t(307);
268 X::iterator i
= source
.begin();
269 X::iterator j
= source
.end();
270 X::size_type rangeSize
= std::distance(i
, j
);
272 copy_tracker::reset();
277 // assert postconditions
278 VERIFY(rangeSize
== a
.size());
279 VERIFY(copy_constructor::count() <= rangeSize
);
283 // @fn rangeConstructorCheckInputIterator()
284 // An explicit check for range construction on an input iterator
285 // range, which the standard expounds upon as having a different
286 // complexity than forward iterators.
288 // 23.2.1.1 range constructor, input iterators
290 rangeConstructorCheckInputIterator()
292 typedef copy_tracker T
;
293 typedef std::deque
<T
> X
;
295 std::istringstream
ibuf("1234567890123456789");
296 const X::size_type rangeSize
= ibuf
.str().size();
297 std::istream_iterator
<char> i(ibuf
);
298 std::istream_iterator
<char> j
;
300 copy_tracker::reset();
304 VERIFY(rangeSize
== a
.size());
305 VERIFY(copy_constructor::count() <= (2 * rangeSize
));
309 // 23.2.1 copy assignment
311 copyAssignmentCheck()
313 typedef copy_tracker T
;
314 typedef std::deque
<T
> X
;
316 const X::size_type
n(18);
317 const X::value_type
t(1023);
321 copy_tracker::reset();
326 VERIFY(n
== copy_constructor::count());
330 // 23.2.1.1 fill assignment
332 // The complexity check must check dtors+copyAssign and
333 // copyCtor+copyAssign because that's the way the SGI implementation
334 // works. Dunno if it's true standard compliant (which specifies fill
335 // assignment in terms of erase and insert only), but it should work
336 // as (most) users expect and is more efficient.
338 fillAssignmentCheck()
340 typedef copy_tracker T
;
341 typedef std::deque
<T
> X
;
343 const X::size_type
starting_size(10);
344 const X::value_type
starting_value(66);
345 const X::size_type
n(23);
346 const X::value_type
t(111);
348 X
a(starting_size
, starting_value
);
349 copy_tracker::reset();
352 VERIFY(starting_size
== a
.size());
358 VERIFY(n
== a
.size());
359 VERIFY(n
== (copy_constructor::count() + assignment_operator::count()));
360 VERIFY(starting_size
== (destructor::count() + assignment_operator::count()));
365 // 23.2.1 range assignment
366 // 23.2.1.1 deque constructors, copy, and assignment
368 // Constructs a deque equal to the range [first, last), using the
369 // specified allocator.
371 // template<typename InputIterator>
372 // assign(InputIterator first, InputIterator last);
376 // erase(begin(), end());
377 // insert(begin(), first, last);
382 // forward iterators: N calls to the copy constructor, 0 reallocations
383 // input iterators: 2N calls to the copy constructor, log(N) reallocations
386 rangeAssignmentCheck()
388 typedef copy_tracker T
;
389 typedef std::deque
<T
> X
;
391 const X::size_type
source_size(726);
392 const X::value_type
source_value(307);
393 const X::size_type
starting_size(10);
394 const X::value_type
starting_value(66);
396 X
source(source_size
, source_value
);
397 X::iterator i
= source
.begin();
398 X::iterator j
= source
.end();
399 X::size_type rangeSize
= std::distance(i
, j
);
401 X
a(starting_size
, starting_value
);
402 VERIFY(starting_size
== a
.size());
404 copy_tracker::reset();
409 VERIFY(rangeSize
== (copy_constructor::count() + assignment_operator::count()));
410 VERIFY(starting_size
== (destructor::count() + assignment_operator::count()));
414 // 23.1 (10) range assignment
415 // 23.2.1.3 with exception
417 rangeAssignmentCheckWithException()
420 typedef copy_tracker T
;
421 typedef std::deque
<T
> X
;
424 // What does "no effects" mean?
428 // 23.1.1 (9) fill assignment looking like a range assignment
430 fillAssignmentCheck2()
433 typedef copy_tracker T
;
434 typedef std::deque
<T
> X
;
437 // What does "no effects" mean?
440 // Verify that the default deque constructor offers the basic exception
443 test_default_ctor_exception_safety()
446 typedef copy_tracker T
;
447 typedef std::deque
<T
, tracker_allocator
<T
> > X
;
450 copy_constructor::throw_on(3);
451 tracker_allocator_counter::reset();
464 // assert postconditions
465 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
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_allocator
<T
> > X
;
478 tracker_allocator_counter::reset();
482 copy_constructor::throw_on(3);
496 // assert postconditions
497 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
504 // basic functionality and standard conformance checks
505 requiredTypesCheck();
506 defaultConstructorCheckPOD();
507 defaultConstructorCheck();
508 test_default_ctor_exception_safety();
509 copyConstructorCheck();
510 test_copy_ctor_exception_safety();
511 fillConstructorCheck();
512 fillConstructorCheck2();
513 rangeConstructorCheckInputIterator();
514 rangeConstructorCheckForwardIterator();
515 copyAssignmentCheck();
516 fillAssignmentCheck();
517 fillAssignmentCheck2();
518 rangeAssignmentCheck();
519 rangeAssignmentCheckWithException();