3 // Copyright (C) 2001-2025 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 // 23.2.1 required types
40 // A missing required type will cause a compile failure.
46 typedef std::deque
<T
> X
;
48 typedef X::reference reference
;
49 typedef X::const_reference const_reference
;
50 typedef X::iterator iterator
;
51 typedef X::const_iterator const_iterator
;
52 typedef X::size_type size_type
;
53 typedef X::difference_type difference_type
;
54 typedef X::value_type value_type
;
55 typedef X::allocator_type allocator_type
;
56 typedef X::pointer pointer
;
57 typedef X::const_pointer const_pointer
;
58 typedef X::reverse_iterator reverse_iterator
;
59 typedef X::const_reverse_iterator const_reverse_iterator
;
63 // @fn defaultConstructorCheck
64 // Explicitly checks the default deque constructor and destructor for both
65 // trivial and non-trivial types. In addition, the size() and empty()
66 // member functions are explicitly checked here since it should be their
67 // first use. Checking those functions means checking the begin() and
68 // end() and their const brethren functions as well.
71 // 23.2.1.1 default ctor/dtor
73 // 23.2.1.1 constructs an empty deque using the specified allocator
75 // 23.1 table 65 u.size() == 0
78 // 23.1 table 65 constant
80 // 23.2.1.2 bool empty() const
82 // 23.1 table 65 a.size() == 0
83 // 23.1 (7) a.begin() == a.end()
86 // 23.1 table 65 constant
88 // 23.2.1.2 size_type size() const
90 // 23.1 table 65 a.end() - a.begin()
93 // 23.1 table 65(A) should be constant
95 // 23.2.1 iterator begin()
96 // const_iterator begin() const
98 // const_iterator end() const
100 // 23.1 (10) pt. 4 does not throw
102 // 23.1 table 65 constant
105 defaultConstructorCheckPOD()
109 typedef std::deque
<T
> X
;
114 // assert postconditions
116 VERIFY(0 == u
.size());
117 VERIFY(u
.begin() == u
.end());
118 VERIFY(0 == std::distance(u
.begin(), u
.end()));
125 defaultConstructorCheck()
128 typedef copy_tracker T
;
129 typedef std::deque
<T
> X
;
131 copy_tracker::reset();
136 // assert postconditions
138 VERIFY(0 == u
.size());
139 VERIFY(u
.begin() == u
.end());
140 VERIFY(0 == std::distance(u
.begin(), u
.end()));
146 // @fn copyConstructorCheck()
147 // Explicitly checks the deque copy constructor. Continues verificaton of
148 // ancillary member functions documented under defaultConstructorCheck().
150 // This check also tests the push_back() member function.
153 // 23.2.1 copy constructor
156 // 22.1.1 table 65 a == X(a)
160 // 22.1.1 table 65 linear
163 copyConstructorCheck()
166 typedef copy_tracker T
;
167 typedef std::deque
<T
> X
;
169 const std::size_t copyBaseSize
= 17; // arbitrary
172 for (std::size_t i
= 0; i
< copyBaseSize
; ++i
)
174 copy_tracker::reset();
176 // assert preconditions
178 VERIFY(copyBaseSize
== a
.size());
179 VERIFY(a
.begin() != a
.end());
180 VERIFY( copyBaseSize
== static_cast<std::size_t>(std::distance(a
.begin(), a
.end())) );
185 // assert postconditions
187 VERIFY(copyBaseSize
== copy_constructor::count());
193 // @fn fillConstructorCheck()
194 // This test explicitly verifies the basic fill constructor. Like the default
195 // constructor, later tests depend on the fill constructor working correctly.
196 // That means this explicit test should precede the later tests so the error
197 // message given on assertion failure can be more helpful n tracking the
200 // 23.2.1.1 fill constructor
202 // 23.2.1.1 linear in N
204 fillConstructorCheck()
207 typedef copy_tracker T
;
208 typedef std::deque
<T
> X
;
210 const X::size_type
n(23);
211 const X::value_type
t(111);
213 copy_tracker::reset();
218 // assert postconditions
219 VERIFY(n
== a
.size());
220 VERIFY(n
== copy_constructor::count());
226 // @fn fillConstructorCheck2()
227 // Explicit check for fill constructors masqueraded as range constructors as
228 // elucidated in clause 23.1.1 paragraph 9 of the standard.
230 // 23.1.1 (9) fill constructor looking like a range constructor
232 fillConstructorCheck2()
234 typedef copy_tracker T
;
235 typedef std::deque
<T
> X
;
237 const std::size_t f
= 23;
238 const std::size_t l
= 111;
240 copy_tracker::reset();
244 VERIFY(f
== a
.size());
245 VERIFY(f
== copy_constructor::count());
249 // @fn rangeConstructorCheckForwardIterator()
250 // This test copies from one deque to another to force the copy
251 // constructor for T to be used because the compiler will kindly
252 // elide copies if the default constructor can be used with
253 // type conversions. Trust me.
255 // 23.2.1.1 range constructor, forward iterators
257 rangeConstructorCheckForwardIterator()
260 typedef copy_tracker T
;
261 typedef std::deque
<T
> X
;
263 const X::size_type
n(726);
264 const X::value_type
t(307);
266 X::iterator i
= source
.begin();
267 X::iterator j
= source
.end();
268 X::size_type rangeSize
= std::distance(i
, j
);
270 copy_tracker::reset();
275 // assert postconditions
276 VERIFY(rangeSize
== a
.size());
277 VERIFY(copy_constructor::count() <= rangeSize
);
281 // @fn rangeConstructorCheckInputIterator()
282 // An explicit check for range construction on an input iterator
283 // range, which the standard expounds upon as having a different
284 // complexity than forward iterators.
286 // 23.2.1.1 range constructor, input iterators
288 rangeConstructorCheckInputIterator()
290 typedef copy_tracker T
;
291 typedef std::deque
<T
> X
;
293 std::istringstream
ibuf("1234567890123456789");
294 const X::size_type rangeSize
= ibuf
.str().size();
295 std::istream_iterator
<char> i(ibuf
);
296 std::istream_iterator
<char> j
;
298 copy_tracker::reset();
302 VERIFY(rangeSize
== a
.size());
303 VERIFY(copy_constructor::count() <= (2 * rangeSize
));
307 // 23.2.1 copy assignment
309 copyAssignmentCheck()
311 typedef copy_tracker T
;
312 typedef std::deque
<T
> X
;
314 const X::size_type
n(18);
315 const X::value_type
t(1023);
319 copy_tracker::reset();
324 VERIFY(n
== copy_constructor::count());
328 // 23.2.1.1 fill assignment
330 // The complexity check must check dtors+copyAssign and
331 // copyCtor+copyAssign because that's the way the SGI implementation
332 // works. Dunno if it's true standard compliant (which specifies fill
333 // assignment in terms of erase and insert only), but it should work
334 // as (most) users expect and is more efficient.
336 fillAssignmentCheck()
338 typedef copy_tracker T
;
339 typedef std::deque
<T
> X
;
341 const X::size_type
starting_size(10);
342 const X::value_type
starting_value(66);
343 const X::size_type
n(23);
344 const X::value_type
t(111);
346 X
a(starting_size
, starting_value
);
347 copy_tracker::reset();
350 VERIFY(starting_size
== a
.size());
356 VERIFY(n
== a
.size());
357 VERIFY(n
== (copy_constructor::count() + assignment_operator::count()));
358 VERIFY(starting_size
== (destructor::count() + assignment_operator::count()));
363 // 23.2.1 range assignment
364 // 23.2.1.1 deque constructors, copy, and assignment
366 // Constructs a deque equal to the range [first, last), using the
367 // specified allocator.
369 // template<typename InputIterator>
370 // assign(InputIterator first, InputIterator last);
374 // erase(begin(), end());
375 // insert(begin(), first, last);
380 // forward iterators: N calls to the copy constructor, 0 reallocations
381 // input iterators: 2N calls to the copy constructor, log(N) reallocations
384 rangeAssignmentCheck()
386 typedef copy_tracker T
;
387 typedef std::deque
<T
> X
;
389 const X::size_type
source_size(726);
390 const X::value_type
source_value(307);
391 const X::size_type
starting_size(10);
392 const X::value_type
starting_value(66);
394 X
source(source_size
, source_value
);
395 X::iterator i
= source
.begin();
396 X::iterator j
= source
.end();
397 X::size_type rangeSize
= std::distance(i
, j
);
399 X
a(starting_size
, starting_value
);
400 VERIFY(starting_size
== a
.size());
402 copy_tracker::reset();
407 VERIFY(rangeSize
== (copy_constructor::count() + assignment_operator::count()));
408 VERIFY(starting_size
== (destructor::count() + assignment_operator::count()));
412 // 23.1 (10) range assignment
413 // 23.2.1.3 with exception
415 rangeAssignmentCheckWithException()
418 typedef copy_tracker T
;
419 typedef std::deque
<T
> X
;
422 // What does "no effects" mean?
426 // 23.1.1 (9) fill assignment looking like a range assignment
428 fillAssignmentCheck2()
431 typedef copy_tracker T
;
432 typedef std::deque
<T
> X
;
435 // What does "no effects" mean?
438 // Verify that the default deque constructor offers the basic exception
441 test_default_ctor_exception_safety()
444 typedef copy_tracker T
;
445 typedef std::deque
<T
, tracker_allocator
<T
> > X
;
448 copy_constructor::throw_on(3);
449 tracker_allocator_counter::reset();
462 // assert postconditions
463 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
468 // Verify that the copy constructor offers the basic exception guarantee.
470 test_copy_ctor_exception_safety()
473 typedef copy_tracker T
;
474 typedef std::deque
<T
, tracker_allocator
<T
> > X
;
476 tracker_allocator_counter::reset();
480 copy_constructor::throw_on(3);
494 // assert postconditions
495 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
502 // basic functionality and standard conformance checks
503 requiredTypesCheck();
504 defaultConstructorCheckPOD();
505 defaultConstructorCheck();
506 test_default_ctor_exception_safety();
507 copyConstructorCheck();
508 test_copy_ctor_exception_safety();
509 fillConstructorCheck();
510 fillConstructorCheck2();
511 rangeConstructorCheckInputIterator();
512 rangeConstructorCheckForwardIterator();
513 copyAssignmentCheck();
514 fillAssignmentCheck();
515 fillAssignmentCheck2();
516 rangeAssignmentCheck();
517 rangeAssignmentCheckWithException();