1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
// 2001-12-27 pme
//
// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 23.2.1.1 deque constructors, copy, and assignment
#include <deque>
#include <iterator>
#include <sstream>
#include <testsuite_allocator.h>
#include <testsuite_hooks.h>
using __gnu_test::copy_tracker;
using __gnu_test::allocation_tracker;
using __gnu_test::tracker_alloc;
using __gnu_test::copy_constructor;
using __gnu_test::assignment_operator;
using __gnu_test::counter;
using __gnu_test::destructor;
typedef std::deque<counter> gdeque;
bool test __attribute__((unused)) = true;
// 23.2.1 required types
//
// A missing required type will cause a compile failure.
//
void
requiredTypesCheck()
{
typedef int T;
typedef std::deque<T> X;
typedef X::reference reference;
typedef X::const_reference const_reference;
typedef X::iterator iterator;
typedef X::const_iterator const_iterator;
typedef X::size_type size_type;
typedef X::difference_type difference_type;
typedef X::value_type value_type;
typedef X::allocator_type allocator_type;
typedef X::pointer pointer;
typedef X::const_pointer const_pointer;
typedef X::reverse_iterator reverse_iterator;
typedef X::const_reverse_iterator const_reverse_iterator;
}
// @fn defaultConstructorCheck
// Explicitly checks the default deque constructor and destructor for both
// trivial and non-trivial types. In addition, the size() and empty()
// member functions are explicitly checked here since it should be their
// first use. Checking those functions means checking the begin() and
// end() and their const brethren functions as well.
//
// @verbatim
// 23.2.1.1 default ctor/dtor
// effects:
// 23.2.1.1 constructs an empty deque using the specified allocator
// postconditions:
// 23.1 table 65 u.size() == 0
// throws:
// complexity:
// 23.1 table 65 constant
//
// 23.2.1.2 bool empty() const
// semantics:
// 23.1 table 65 a.size() == 0
// 23.1 (7) a.begin() == a.end()
// throws:
// complexity:
// 23.1 table 65 constant
//
// 23.2.1.2 size_type size() const
// semantics:
// 23.1 table 65 a.end() - a.begin()
// throws:
// complexity:
// 23.1 table 65(A) should be constant
//
// 23.2.1 iterator begin()
// const_iterator begin() const
// iterator end()
// const_iterator end() const
// throws:
// 23.1 (10) pt. 4 does not throw
// complexity:
// 23.1 table 65 constant
// @endverbatim
void
defaultConstructorCheckPOD()
{
// setup
typedef int T;
typedef std::deque<T> X;
// run test
X u;
// assert postconditions
VERIFY(u.empty());
VERIFY(0 == u.size());
VERIFY(u.begin() == u.end());
VERIFY(0 == std::distance(u.begin(), u.end()));
// teardown
}
void
defaultConstructorCheck()
{
// setup
typedef copy_tracker T;
typedef std::deque<T> X;
copy_tracker::reset();
// run test
const X u;
// assert postconditions
VERIFY(u.empty());
VERIFY(0 == u.size());
VERIFY(u.begin() == u.end());
VERIFY(0 == std::distance(u.begin(), u.end()));
// teardown
}
// @fn copyConstructorCheck()
// Explicitly checks the deque copy constructor. Continues verificaton of
// ancillary member functions documented under defaultConstructorCheck().
//
// This check also tests the push_back() member function.
//
// @verbatim
// 23.2.1 copy constructor
// effects:
// postconditions:
// 22.1.1 table 65 a == X(a)
// u == a
// throws:
// complexity:
// 22.1.1 table 65 linear
// @endverbatim
void
copyConstructorCheck()
{
// setup
typedef copy_tracker T;
typedef std::deque<T> X;
const std::size_t copyBaseSize = 17; // arbitrary
X a;
for (std::size_t i = 0; i < copyBaseSize; ++i)
a.push_back(i);
copy_tracker::reset();
// assert preconditions
VERIFY(!a.empty());
VERIFY(copyBaseSize == a.size());
VERIFY(a.begin() != a.end());
VERIFY( copyBaseSize == static_cast<std::size_t>(std::distance(a.begin(), a.end())) );
// run test
X u = a;
// assert postconditions
VERIFY(u == a);
VERIFY(copyBaseSize == copy_constructor::count());
// teardown
}
// @fn fillConstructorCheck()
// This test explicitly verifies the basic fill constructor. Like the default
// constructor, later tests depend on the fill constructor working correctly.
// That means this explicit test should preceed the later tests so the error
// message given on assertion failure can be more helpful n tracking the
// problem.
//
// 23.2.1.1 fill constructor
// complexity:
// 23.2.1.1 linear in N
void
fillConstructorCheck()
{
// setup
typedef copy_tracker T;
typedef std::deque<T> X;
const X::size_type n(23);
const X::value_type t(111);
copy_tracker::reset();
// run test
X a(n, t);
// assert postconditions
VERIFY(n == a.size());
VERIFY(n == copy_constructor::count());
// teardown
}
// @fn fillConstructorCheck2()
// Explicit check for fill constructors masqueraded as range constructors as
// elucidated in clause 23.1.1 paragraph 9 of the standard.
//
// 23.1.1 (9) fill constructor looking like a range constructor
void
fillConstructorCheck2()
{
typedef copy_tracker T;
typedef std::deque<T> X;
const std::size_t f = 23;
const std::size_t l = 111;
copy_tracker::reset();
X a(f, l);
VERIFY(f == a.size());
VERIFY(f == copy_constructor::count());
}
// @fn rangeConstructorCheckForwardIterator()
// This test copies from one deque to another to force the copy
// constructor for T to be used because the compiler will kindly
// elide copies if the default constructor can be used with
// type conversions. Trust me.
//
// 23.2.1.1 range constructor, forward iterators
void
rangeConstructorCheckForwardIterator()
{
// setup
typedef copy_tracker T;
typedef std::deque<T> X;
const X::size_type n(726);
const X::value_type t(307);
X source(n, t);
X::iterator i = source.begin();
X::iterator j = source.end();
X::size_type rangeSize = std::distance(i, j);
copy_tracker::reset();
// test
X a(i, j);
// assert postconditions
VERIFY(rangeSize == a.size());
VERIFY(copy_constructor::count() <= rangeSize);
}
// @fn rangeConstructorCheckInputIterator()
// An explicit check for range construction on an input iterator
// range, which the standard expounds upon as having a different
// complexity than forward iterators.
//
// 23.2.1.1 range constructor, input iterators
void
rangeConstructorCheckInputIterator()
{
typedef copy_tracker T;
typedef std::deque<T> X;
std::istringstream ibuf("1234567890123456789");
const X::size_type rangeSize = ibuf.str().size();
std::istream_iterator<char> i(ibuf);
std::istream_iterator<char> j;
copy_tracker::reset();
X a(i, j);
VERIFY(rangeSize == a.size());
VERIFY(copy_constructor::count() <= (2 * rangeSize));
}
// 23.2.1 copy assignment
void
copyAssignmentCheck()
{
typedef copy_tracker T;
typedef std::deque<T> X;
const X::size_type n(18);
const X::value_type t(1023);
X a(n, t);
X r;
copy_tracker::reset();
r = a;
VERIFY(r == a);
VERIFY(n == copy_constructor::count());
}
// 23.2.1.1 fill assignment
//
// The complexity check must check dtors+copyAssign and
// copyCtor+copyAssign because that's the way the SGI implementation
// works. Dunno if it's true standard compliant (which specifies fill
// assignment in terms of erase and insert only), but it should work
// as (most) users expect and is more efficient.
void
fillAssignmentCheck()
{
typedef copy_tracker T;
typedef std::deque<T> X;
const X::size_type starting_size(10);
const X::value_type starting_value(66);
const X::size_type n(23);
const X::value_type t(111);
X a(starting_size, starting_value);
copy_tracker::reset();
// preconditions
VERIFY(starting_size == a.size());
// test
a.assign(n, t);
// postconditions
VERIFY(n == a.size());
VERIFY(n == (copy_constructor::count() + assignment_operator::count()));
VERIFY(starting_size == (destructor::count() + assignment_operator::count()));
}
// @verbatim
// 23.2.1 range assignment
// 23.2.1.1 deque constructors, copy, and assignment
// effects:
// Constructs a deque equal to the range [first, last), using the
// specified allocator.
//
// template<typename InputIterator>
// assign(InputIterator first, InputIterator last);
//
// is equivalent to
//
// erase(begin(), end());
// insert(begin(), first, last);
//
// postconditions:
// throws:
// complexity:
// forward iterators: N calls to the copy constructor, 0 reallocations
// input iterators: 2N calls to the copy constructor, log(N) reallocations
// @endverbatim
void
rangeAssignmentCheck()
{
typedef copy_tracker T;
typedef std::deque<T> X;
const X::size_type source_size(726);
const X::value_type source_value(307);
const X::size_type starting_size(10);
const X::value_type starting_value(66);
X source(source_size, source_value);
X::iterator i = source.begin();
X::iterator j = source.end();
X::size_type rangeSize = std::distance(i, j);
X a(starting_size, starting_value);
VERIFY(starting_size == a.size());
copy_tracker::reset();
a.assign(i, j);
VERIFY(source == a);
VERIFY(rangeSize == (copy_constructor::count() + assignment_operator::count()));
VERIFY(starting_size == (destructor::count() + assignment_operator::count()));
}
// 23.1 (10) range assignment
// 23.2.1.3 with exception
void
rangeAssignmentCheckWithException()
{
// setup
typedef copy_tracker T;
typedef std::deque<T> X;
// test
// What does "no effects" mean?
}
// 23.1.1 (9) fill assignment looking like a range assignment
void
fillAssignmentCheck2()
{
// setup
typedef copy_tracker T;
typedef std::deque<T> X;
// test
// What does "no effects" mean?
}
// Verify that the default deque constructor offers the basic exception
// guarantee.
void
test_default_ctor_exception_safety()
{
// setup
typedef copy_tracker T;
typedef std::deque<T, tracker_alloc<T> > X;
T::reset();
copy_constructor::throw_on(3);
allocation_tracker::resetCounts();
// test
try
{
X a(7);
VERIFY( false );
}
catch (...)
{
}
// assert postconditions
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
// teardown
}
// Verify that the copy constructor offers the basic exception guarantee.
void
test_copy_ctor_exception_safety()
{
// setup
typedef copy_tracker T;
typedef std::deque<T, tracker_alloc<T> > X;
allocation_tracker::resetCounts();
{
X a(7);
T::reset();
copy_constructor::throw_on(3);
// test
try
{
X u(a);
VERIFY(false);
}
catch (...)
{
}
}
// assert postconditions
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
// teardown
}
int main()
{
// basic functionality and standard conformance checks
requiredTypesCheck();
defaultConstructorCheckPOD();
defaultConstructorCheck();
test_default_ctor_exception_safety();
copyConstructorCheck();
test_copy_ctor_exception_safety();
fillConstructorCheck();
fillConstructorCheck2();
rangeConstructorCheckInputIterator();
rangeConstructorCheckForwardIterator();
copyAssignmentCheck();
fillAssignmentCheck();
fillAssignmentCheck2();
rangeAssignmentCheck();
rangeAssignmentCheckWithException();
return 0;
}
|