]>
Commit | Line | Data |
---|---|---|
bb2ae697 PE |
1 | // 2001-12-27 pme |
2 | // | |
8d59b230 | 3 | // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. |
bb2ae697 PE |
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 2, or (at your option) | |
9 | // any later version. | |
10 | // | |
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. | |
15 | // | |
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, | |
19 | // USA. | |
20 | ||
21 | // 23.2.1.1 deque constructors, copy, and assignment | |
22 | ||
23 | #include <deque> | |
162c7cd9 SW |
24 | #include <iterator> |
25 | #include <sstream> | |
26 | #include <testsuite_allocator.h> | |
bb2ae697 PE |
27 | #include <testsuite_hooks.h> |
28 | ||
8d59b230 BK |
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; | |
36 | ||
37 | typedef std::deque<counter> gdeque; | |
bb2ae697 | 38 | |
162c7cd9 | 39 | bool test = true; |
bb2ae697 | 40 | |
162c7cd9 | 41 | // see http://gcc.gnu.org/ml/libstdc++/2001-11/msg00139.html |
bb2ae697 PE |
42 | void |
43 | test01() | |
44 | { | |
45 | assert_count (0); | |
46 | { | |
47 | gdeque d(10); | |
48 | assert_count (10); | |
49 | } | |
50 | assert_count (0); | |
51 | } | |
52 | ||
162c7cd9 SW |
53 | |
54 | // 23.2.1 required types | |
55 | // | |
56 | // A missing required type will cause a compile failure. | |
57 | // | |
58 | void | |
59 | requiredTypesCheck() | |
60 | { | |
61 | typedef int T; | |
62 | typedef std::deque<T> X; | |
63 | ||
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; | |
76 | } | |
77 | ||
78 | ||
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. | |
85 | // | |
86 | // @verbatim | |
87 | // 23.2.1.1 default ctor/dtor | |
88 | // effects: | |
89 | // 23.2.1.1 constructs an empty deque using the specified allocator | |
90 | // postconditions: | |
91 | // 23.1 table 65 u.size() == 0 | |
92 | // throws: | |
93 | // complexity: | |
94 | // 23.1 table 65 constant | |
95 | // | |
96 | // 23.2.1.2 bool empty() const | |
97 | // semantics: | |
98 | // 23.1 table 65 a.size() == 0 | |
99 | // 23.1 (7) a.begin() == a.end() | |
100 | // throws: | |
101 | // complexity: | |
102 | // 23.1 table 65 constant | |
103 | // | |
104 | // 23.2.1.2 size_type size() const | |
105 | // semantics: | |
106 | // 23.1 table 65 a.end() - a.begin() | |
107 | // throws: | |
108 | // complexity: | |
109 | // 23.1 table 65(A) should be constant | |
110 | // | |
111 | // 23.2.1 iterator begin() | |
112 | // const_iterator begin() const | |
113 | // iterator end() | |
114 | // const_iterator end() const | |
115 | // throws: | |
116 | // 23.1 (10) pt. 4 does not throw | |
117 | // complexity: | |
118 | // 23.1 table 65 constant | |
119 | // @endverbatim | |
120 | void | |
121 | defaultConstructorCheckPOD() | |
122 | { | |
123 | // setup | |
124 | typedef int T; | |
125 | typedef std::deque<T> X; | |
126 | ||
127 | // run test | |
128 | X u; | |
129 | ||
130 | // assert postconditions | |
131 | VERIFY(u.empty()); | |
132 | VERIFY(0 == u.size()); | |
133 | VERIFY(u.begin() == u.end()); | |
134 | VERIFY(0 == std::distance(u.begin(), u.end())); | |
135 | ||
136 | // teardown | |
137 | } | |
138 | ||
139 | ||
140 | void | |
141 | defaultConstructorCheck() | |
142 | { | |
143 | // setup | |
8d59b230 | 144 | typedef copy_tracker T; |
162c7cd9 SW |
145 | typedef std::deque<T> X; |
146 | ||
8d59b230 | 147 | copy_tracker::reset(); |
162c7cd9 SW |
148 | |
149 | // run test | |
150 | const X u; | |
151 | ||
152 | // assert postconditions | |
153 | VERIFY(u.empty()); | |
154 | VERIFY(0 == u.size()); | |
155 | VERIFY(u.begin() == u.end()); | |
156 | VERIFY(0 == std::distance(u.begin(), u.end())); | |
157 | ||
158 | // teardown | |
159 | } | |
160 | ||
161 | ||
162 | // @fn copyConstructorCheck() | |
163 | // Explicitly checks the deque copy constructor. Continues verificaton of | |
164 | // ancillary member functions documented under defaultConstructorCheck(). | |
165 | // | |
166 | // This check also tests the push_back() member function. | |
167 | // | |
168 | // @verbatim | |
169 | // 23.2.1 copy constructor | |
170 | // effects: | |
171 | // postconditions: | |
172 | // 22.1.1 table 65 a == X(a) | |
173 | // u == a | |
174 | // throws: | |
175 | // complexity: | |
176 | // 22.1.1 table 65 linear | |
177 | // @endverbatim | |
178 | void | |
179 | copyConstructorCheck() | |
180 | { | |
181 | // setup | |
8d59b230 | 182 | typedef copy_tracker T; |
162c7cd9 SW |
183 | typedef std::deque<T> X; |
184 | ||
185 | const int copyBaseSize = 17; // arbitrary | |
186 | ||
187 | X a; | |
188 | for (int i = 0; i < copyBaseSize; ++i) | |
189 | a.push_back(i); | |
8d59b230 | 190 | copy_tracker::reset(); |
162c7cd9 SW |
191 | |
192 | // assert preconditions | |
193 | VERIFY(!a.empty()); | |
194 | VERIFY(copyBaseSize == a.size()); | |
195 | VERIFY(a.begin() != a.end()); | |
196 | VERIFY(copyBaseSize == std::distance(a.begin(), a.end())); | |
197 | ||
198 | // run test | |
199 | X u = a; | |
200 | ||
201 | // assert postconditions | |
202 | VERIFY(u == a); | |
8d59b230 | 203 | VERIFY(copyBaseSize == copy_constructor::count()); |
162c7cd9 SW |
204 | |
205 | // teardown | |
206 | } | |
207 | ||
208 | ||
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 | |
214 | // problem. | |
215 | // | |
216 | // 23.2.1.1 fill constructor | |
217 | // complexity: | |
218 | // 23.2.1.1 linear in N | |
219 | void | |
220 | fillConstructorCheck() | |
221 | { | |
222 | // setup | |
8d59b230 | 223 | typedef copy_tracker T; |
162c7cd9 SW |
224 | typedef std::deque<T> X; |
225 | ||
226 | const X::size_type n(23); | |
227 | const X::value_type t(111); | |
228 | ||
8d59b230 | 229 | copy_tracker::reset(); |
162c7cd9 SW |
230 | |
231 | // run test | |
232 | X a(n, t); | |
233 | ||
234 | // assert postconditions | |
235 | VERIFY(n == a.size()); | |
8d59b230 | 236 | VERIFY(n == copy_constructor::count()); |
162c7cd9 SW |
237 | |
238 | // teardown | |
239 | } | |
240 | ||
241 | ||
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. | |
245 | // | |
246 | // 23.1.1 (9) fill constructor looking like a range constructor | |
247 | void | |
248 | fillConstructorCheck2() | |
249 | { | |
8d59b230 | 250 | typedef copy_tracker T; |
162c7cd9 SW |
251 | typedef std::deque<T> X; |
252 | ||
253 | const int f = 23; | |
254 | const int l = 111; | |
255 | ||
8d59b230 | 256 | copy_tracker::reset(); |
162c7cd9 SW |
257 | |
258 | X a(f, l); | |
259 | ||
260 | VERIFY(f == a.size()); | |
8d59b230 | 261 | VERIFY(f == copy_constructor::count()); |
162c7cd9 SW |
262 | } |
263 | ||
264 | ||
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. | |
270 | // | |
271 | // 23.2.1.1 range constructor, forward iterators | |
272 | void | |
273 | rangeConstructorCheckForwardIterator() | |
274 | { | |
275 | // setup | |
8d59b230 | 276 | typedef copy_tracker T; |
162c7cd9 SW |
277 | typedef std::deque<T> X; |
278 | ||
279 | const X::size_type n(726); | |
280 | const X::value_type t(307); | |
281 | X source(n, t); | |
282 | X::iterator i = source.begin(); | |
283 | X::iterator j = source.end(); | |
284 | X::size_type rangeSize = std::distance(i, j); | |
285 | ||
8d59b230 | 286 | copy_tracker::reset(); |
162c7cd9 SW |
287 | |
288 | // test | |
289 | X a(i, j); | |
290 | ||
291 | // assert postconditions | |
292 | VERIFY(rangeSize == a.size()); | |
8d59b230 | 293 | VERIFY(copy_constructor::count() <= rangeSize); |
162c7cd9 SW |
294 | } |
295 | ||
296 | ||
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. | |
301 | // | |
302 | // 23.2.1.1 range constructor, input iterators | |
303 | void | |
304 | rangeConstructorCheckInputIterator() | |
305 | { | |
8d59b230 | 306 | typedef copy_tracker T; |
162c7cd9 SW |
307 | typedef std::deque<T> X; |
308 | ||
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; | |
313 | ||
8d59b230 | 314 | copy_tracker::reset(); |
162c7cd9 SW |
315 | |
316 | X a(i, j); | |
317 | ||
318 | VERIFY(rangeSize == a.size()); | |
8d59b230 | 319 | VERIFY(copy_constructor::count() <= (2 * rangeSize)); |
162c7cd9 SW |
320 | } |
321 | ||
322 | ||
323 | // 23.2.1 copy assignment | |
324 | void | |
325 | copyAssignmentCheck() | |
326 | { | |
8d59b230 | 327 | typedef copy_tracker T; |
162c7cd9 SW |
328 | typedef std::deque<T> X; |
329 | ||
330 | const X::size_type n(18); | |
331 | const X::value_type t(1023); | |
332 | X a(n, t); | |
333 | X r; | |
334 | ||
8d59b230 | 335 | copy_tracker::reset(); |
162c7cd9 SW |
336 | |
337 | r = a; | |
338 | ||
339 | VERIFY(r == a); | |
8d59b230 | 340 | VERIFY(n == copy_constructor::count()); |
162c7cd9 SW |
341 | } |
342 | ||
343 | ||
344 | // 23.2.1.1 fill assignment | |
345 | // | |
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. | |
351 | void | |
352 | fillAssignmentCheck() | |
353 | { | |
8d59b230 | 354 | typedef copy_tracker T; |
162c7cd9 SW |
355 | typedef std::deque<T> X; |
356 | ||
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); | |
361 | ||
362 | X a(starting_size, starting_value); | |
8d59b230 | 363 | copy_tracker::reset(); |
162c7cd9 SW |
364 | |
365 | // preconditions | |
366 | VERIFY(starting_size == a.size()); | |
367 | ||
368 | // test | |
369 | a.assign(n, t); | |
370 | ||
371 | // postconditions | |
372 | VERIFY(n == a.size()); | |
8d59b230 BK |
373 | VERIFY(n == (copy_constructor::count() + assignment_operator::count())); |
374 | VERIFY(starting_size == (destructor::count() + assignment_operator::count())); | |
162c7cd9 SW |
375 | } |
376 | ||
377 | ||
378 | // @verbatim | |
379 | // 23.2.1 range assignment | |
380 | // 23.2.1.1 deque constructors, copy, and assignment | |
381 | // effects: | |
382 | // Constructs a deque equal to the range [first, last), using the | |
383 | // specified allocator. | |
384 | // | |
385 | // template<typename InputIterator> | |
386 | // assign(InputIterator first, InputIterator last); | |
387 | // | |
388 | // is equivalent to | |
389 | // | |
390 | // erase(begin(), end()); | |
391 | // insert(begin(), first, last); | |
392 | // | |
393 | // postconditions: | |
394 | // throws: | |
395 | // complexity: | |
396 | // forward iterators: N calls to the copy constructor, 0 reallocations | |
397 | // input iterators: 2N calls to the copy constructor, log(N) reallocations | |
398 | // @endverbatim | |
399 | void | |
400 | rangeAssignmentCheck() | |
401 | { | |
8d59b230 | 402 | typedef copy_tracker T; |
162c7cd9 SW |
403 | typedef std::deque<T> X; |
404 | ||
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); | |
409 | ||
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); | |
414 | ||
415 | X a(starting_size, starting_value); | |
416 | VERIFY(starting_size == a.size()); | |
417 | ||
8d59b230 | 418 | copy_tracker::reset(); |
162c7cd9 SW |
419 | |
420 | a.assign(i, j); | |
421 | ||
422 | VERIFY(source == a); | |
8d59b230 BK |
423 | VERIFY(rangeSize == (copy_constructor::count() + assignment_operator::count())); |
424 | VERIFY(starting_size == (destructor::count() + assignment_operator::count())); | |
162c7cd9 SW |
425 | } |
426 | ||
427 | ||
428 | // 23.1 (10) range assignment | |
429 | // 23.2.1.3 with exception | |
430 | void | |
431 | rangeAssignmentCheckWithException() | |
432 | { | |
433 | // setup | |
8d59b230 | 434 | typedef copy_tracker T; |
162c7cd9 SW |
435 | typedef std::deque<T> X; |
436 | ||
437 | // test | |
438 | // What does "no effects" mean? | |
439 | } | |
440 | ||
441 | ||
442 | // 23.1.1 (9) fill assignment looking like a range assignment | |
443 | void | |
444 | fillAssignmentCheck2() | |
445 | { | |
446 | // setup | |
8d59b230 | 447 | typedef copy_tracker T; |
162c7cd9 SW |
448 | typedef std::deque<T> X; |
449 | ||
450 | // test | |
451 | // What does "no effects" mean? | |
452 | } | |
453 | ||
454 | // Verify that the default deque constructor offers the basic exception | |
455 | // guarantee. | |
456 | void | |
457 | test_default_ctor_exception_safety() | |
458 | { | |
459 | // setup | |
8d59b230 BK |
460 | typedef copy_tracker T; |
461 | typedef std::deque<T, tracker_alloc<T> > X; | |
162c7cd9 SW |
462 | |
463 | T::reset(); | |
8d59b230 BK |
464 | copy_constructor::throw_on(3); |
465 | allocation_tracker::resetCounts(); | |
162c7cd9 SW |
466 | |
467 | // test | |
468 | try | |
469 | { | |
470 | X a(7); | |
471 | VERIFY(("no exception thrown", false)); | |
472 | } | |
473 | catch (...) | |
474 | { | |
475 | } | |
476 | ||
477 | // assert postconditions | |
8d59b230 | 478 | VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal()); |
162c7cd9 SW |
479 | |
480 | // teardown | |
481 | } | |
482 | ||
483 | // Verify that the copy constructor offers the basic exception guarantee. | |
484 | void | |
485 | test_copy_ctor_exception_safety() | |
486 | { | |
487 | // setup | |
8d59b230 BK |
488 | typedef copy_tracker T; |
489 | typedef std::deque<T, tracker_alloc<T> > X; | |
162c7cd9 | 490 | |
8d59b230 | 491 | allocation_tracker::resetCounts(); |
162c7cd9 SW |
492 | { |
493 | X a(7); | |
494 | T::reset(); | |
8d59b230 | 495 | copy_constructor::throw_on(3); |
162c7cd9 SW |
496 | |
497 | ||
498 | // test | |
499 | try | |
500 | { | |
501 | X u(a); | |
502 | VERIFY(("no exception thrown", false)); | |
503 | } | |
504 | catch (...) | |
505 | { | |
506 | } | |
507 | } | |
508 | ||
509 | // assert postconditions | |
8d59b230 | 510 | VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal()); |
162c7cd9 SW |
511 | |
512 | // teardown | |
513 | } | |
514 | ||
515 | ||
bb2ae697 PE |
516 | int main() |
517 | { | |
162c7cd9 SW |
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(); | |
534 | ||
535 | // specific bug fix checks | |
bb2ae697 PE |
536 | test01(); |
537 | ||
162c7cd9 | 538 | return !test; |
bb2ae697 | 539 | } |