Fortran: fix ALLOCATE with SOURCE of deferred character length [PR114019]
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / deque / cons / 2.cc
blobcf50df6bb02bbc93ca7a875c1a9780ce6de4cdd3
1 // 2001-12-27 pme
2 //
3 // Copyright (C) 2001-2024 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 3, 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 COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // 23.2.1.1 deque constructors, copy, and assignment
22 #include <deque>
23 #include <iterator>
24 #include <sstream>
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.
42 void
43 requiredTypesCheck()
45 typedef int T;
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.
70 // @verbatim
71 // 23.2.1.1 default ctor/dtor
72 // effects:
73 // 23.2.1.1 constructs an empty deque using the specified allocator
74 // postconditions:
75 // 23.1 table 65 u.size() == 0
76 // throws:
77 // complexity:
78 // 23.1 table 65 constant
80 // 23.2.1.2 bool empty() const
81 // semantics:
82 // 23.1 table 65 a.size() == 0
83 // 23.1 (7) a.begin() == a.end()
84 // throws:
85 // complexity:
86 // 23.1 table 65 constant
88 // 23.2.1.2 size_type size() const
89 // semantics:
90 // 23.1 table 65 a.end() - a.begin()
91 // throws:
92 // complexity:
93 // 23.1 table 65(A) should be constant
95 // 23.2.1 iterator begin()
96 // const_iterator begin() const
97 // iterator end()
98 // const_iterator end() const
99 // throws:
100 // 23.1 (10) pt. 4 does not throw
101 // complexity:
102 // 23.1 table 65 constant
103 // @endverbatim
104 void
105 defaultConstructorCheckPOD()
107 // setup
108 typedef int T;
109 typedef std::deque<T> X;
111 // run test
112 X u;
114 // assert postconditions
115 VERIFY(u.empty());
116 VERIFY(0 == u.size());
117 VERIFY(u.begin() == u.end());
118 VERIFY(0 == std::distance(u.begin(), u.end()));
120 // teardown
124 void
125 defaultConstructorCheck()
127 // setup
128 typedef copy_tracker T;
129 typedef std::deque<T> X;
131 copy_tracker::reset();
133 // run test
134 const X u;
136 // assert postconditions
137 VERIFY(u.empty());
138 VERIFY(0 == u.size());
139 VERIFY(u.begin() == u.end());
140 VERIFY(0 == std::distance(u.begin(), u.end()));
142 // teardown
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.
152 // @verbatim
153 // 23.2.1 copy constructor
154 // effects:
155 // postconditions:
156 // 22.1.1 table 65 a == X(a)
157 // u == a
158 // throws:
159 // complexity:
160 // 22.1.1 table 65 linear
161 // @endverbatim
162 void
163 copyConstructorCheck()
165 // setup
166 typedef copy_tracker T;
167 typedef std::deque<T> X;
169 const std::size_t copyBaseSize = 17; // arbitrary
171 X a;
172 for (std::size_t i = 0; i < copyBaseSize; ++i)
173 a.push_back(i);
174 copy_tracker::reset();
176 // assert preconditions
177 VERIFY(!a.empty());
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())) );
182 // run test
183 X u = a;
185 // assert postconditions
186 VERIFY(u == a);
187 VERIFY(copyBaseSize == copy_constructor::count());
189 // teardown
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
198 // problem.
200 // 23.2.1.1 fill constructor
201 // complexity:
202 // 23.2.1.1 linear in N
203 void
204 fillConstructorCheck()
206 // setup
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();
215 // run test
216 X a(n, t);
218 // assert postconditions
219 VERIFY(n == a.size());
220 VERIFY(n == copy_constructor::count());
222 // teardown
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
231 void
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();
242 X a(f, l);
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
256 void
257 rangeConstructorCheckForwardIterator()
259 // setup
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);
265 X source(n, t);
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();
272 // test
273 X a(i, j);
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
287 void
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();
300 X a(i, j);
302 VERIFY(rangeSize == a.size());
303 VERIFY(copy_constructor::count() <= (2 * rangeSize));
307 // 23.2.1 copy assignment
308 void
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);
316 X a(n, t);
317 X r;
319 copy_tracker::reset();
321 r = a;
323 VERIFY(r == a);
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.
335 void
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();
349 // preconditions
350 VERIFY(starting_size == a.size());
352 // test
353 a.assign(n, t);
355 // postconditions
356 VERIFY(n == a.size());
357 VERIFY(n == (copy_constructor::count() + assignment_operator::count()));
358 VERIFY(starting_size == (destructor::count() + assignment_operator::count()));
362 // @verbatim
363 // 23.2.1 range assignment
364 // 23.2.1.1 deque constructors, copy, and assignment
365 // effects:
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);
372 // is equivalent to
374 // erase(begin(), end());
375 // insert(begin(), first, last);
377 // postconditions:
378 // throws:
379 // complexity:
380 // forward iterators: N calls to the copy constructor, 0 reallocations
381 // input iterators: 2N calls to the copy constructor, log(N) reallocations
382 // @endverbatim
383 void
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();
404 a.assign(i, j);
406 VERIFY(source == a);
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
414 void
415 rangeAssignmentCheckWithException()
417 // setup
418 typedef copy_tracker T;
419 typedef std::deque<T> X;
421 // test
422 // What does "no effects" mean?
426 // 23.1.1 (9) fill assignment looking like a range assignment
427 void
428 fillAssignmentCheck2()
430 // setup
431 typedef copy_tracker T;
432 typedef std::deque<T> X;
434 // test
435 // What does "no effects" mean?
438 // Verify that the default deque constructor offers the basic exception
439 // guarantee.
440 void
441 test_default_ctor_exception_safety()
443 // setup
444 typedef copy_tracker T;
445 typedef std::deque<T, tracker_allocator<T> > X;
447 T::reset();
448 copy_constructor::throw_on(3);
449 tracker_allocator_counter::reset();
451 // test
454 T ref;
455 X a(7, ref);
456 VERIFY( false );
458 catch (...)
462 // assert postconditions
463 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
465 // teardown
468 // Verify that the copy constructor offers the basic exception guarantee.
469 void
470 test_copy_ctor_exception_safety()
472 // setup
473 typedef copy_tracker T;
474 typedef std::deque<T, tracker_allocator<T> > X;
476 tracker_allocator_counter::reset();
478 X a(7);
479 T::reset();
480 copy_constructor::throw_on(3);
483 // test
486 X u(a);
487 VERIFY(false);
489 catch (...)
494 // assert postconditions
495 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
497 // teardown
500 int main()
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();
518 return 0;