File: test_insert.h

package info (click to toggle)
stlport4.6 4.6.2-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 7,056 kB
  • ctags: 16,390
  • sloc: ansic: 46,190; cpp: 18,805; sh: 266; asm: 93; perl: 58; makefile: 10
file content (551 lines) | stat: -rw-r--r-- 16,970 bytes parent folder | download | duplicates (4)
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/***********************************************************************************
	test_insert.h
	
 * Copyright (c) 1997
 * Mark of the Unicorn, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Mark of the Unicorn makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.

***********************************************************************************/
#ifndef test_insert_H_
#define test_insert_H_

# include "Prefix.h"
# if defined (EH_NEW_HEADERS)
#  include <utility>
#  include <vector>
#  include <cassert>
#  include <climits>
# else
#  include <vector.h>
#  include <assert.h>
#  include <limits.h>
# endif
#include "random_number.h"
#include "nc_alloc.h"
#include "ThrowCompare.h"

// A classification system for containers, for verification
struct container_tag {};
struct sequence_container_tag  {};
struct associative_container_tag  {}; 

struct set_tag  {};
struct multiset_tag  {};
struct map_tag {};
struct multimap_tag {};

template <class C, class Iter>
size_t CountNewItems( const C&, const Iter& firstNew, 
                      const Iter& lastNew, sequence_container_tag )
{
    size_t dist = 0;
#if 0 //def __SUNPRO_CC
    EH_DISTANCE( firstNew, lastNew, dist );
#else
    EH_DISTANCE( Iter(firstNew), Iter(lastNew), dist );
#endif
    return dist;
}

template <class C, class Iter>
size_t CountNewItems( const C& c, const Iter& firstNew, 
                      const Iter& lastNew, multimap_tag )
{
    return CountNewItems( c, firstNew, lastNew, sequence_container_tag() );
}

template <class C, class Iter>
size_t CountNewItems( const C& c, const Iter& firstNew, 
                      const Iter& lastNew, multiset_tag )
{
    return CountNewItems( c, firstNew, lastNew, sequence_container_tag() );
}


template <class C, class Iter, class KeyOfValue>
#ifdef __BORLANDC__
size_t CountUniqueItems_Aux( const C& original, const Iter& firstNew, 
#else
size_t CountUniqueItems_Aux( const C& original, Iter firstNew, 
#endif
                             Iter lastNew, const KeyOfValue& keyOfValue )
{
	typedef typename C::key_type key;
	typedef typename C::const_iterator const_iter;
	typedef EH_STD::vector<key> key_list;
	typedef typename key_list::iterator key_iterator;
	key_list keys;
	size_t dist = 0;
#ifdef __SUNPRO_CC
	EH_DISTANCE( firstNew, lastNew, dist );
#else
	EH_DISTANCE( Iter(firstNew), Iter(lastNew), dist );
#endif
	keys.reserve( dist );
	for ( Iter x = firstNew; x != lastNew; ++x )
		keys.push_back( keyOfValue(*x) );
		
	EH_STD::sort( keys.begin(), keys.end() );
	key_iterator last = EH_STD::unique( keys.begin(), keys.end() );
	 
    size_t cnt = 0;
    for ( key_iterator tmp = keys.begin(); tmp != last; ++tmp )
    {
        if ( const_iter(original.find( *tmp )) == const_iter(original.end()) )
            ++cnt;
    }
    return cnt;
}

#if ! defined (__SGI_STL)
EH_BEGIN_NAMESPACE
template <class T>
struct identity
{
	const T& operator()( const T& x ) const { return x; }
};
# if ! defined (__KCC)
template <class _Pair>
struct select1st : public unary_function<_Pair, typename _Pair::first_type> {
  const typename _Pair::first_type& operator()(const _Pair& __x) const {
    return __x.first;
  }
};
# endif
EH_END_NAMESPACE
#endif

template <class C, class Iter>
size_t CountUniqueItems( const C& original, const Iter& firstNew, 
                         const Iter& lastNew, set_tag )
{
    typedef typename C::value_type value_type;
    return CountUniqueItems_Aux( original, firstNew, lastNew, 
                                 EH_STD::identity<value_type>() );
}

template <class C, class Iter>
size_t CountUniqueItems( const C& original, const Iter& firstNew, 
                         const Iter& lastNew, map_tag )
{
#ifdef EH_MULTI_CONST_TEMPLATE_ARG_BUG
    return CountUniqueItems_Aux( original, firstNew, lastNew, 
                                 EH_SELECT1ST_HINT<C::value_type, C::key_type>() );
#else
    typedef typename C::value_type value_type;
    return CountUniqueItems_Aux( original, firstNew, lastNew, 
                                 EH_STD::select1st<value_type>() );
#endif
}

template <class C, class Iter>
size_t CountNewItems( const C& original, const Iter& firstNew, 
                      const Iter& lastNew, map_tag )
{
    return CountUniqueItems( original, firstNew, lastNew, 
                             container_category( original ) );
}

template <class C, class Iter>
size_t CountNewItems( const C& original, const Iter& firstNew, 
                      const Iter& lastNew, set_tag )
{
    return CountUniqueItems( original, firstNew, lastNew, 
                             container_category( original ) );
}

template <class C, class SrcIter>
inline void VerifyInsertion( const C& original, const C& result, 
                             const SrcIter& firstNew, const SrcIter& lastNew, 
                             size_t, associative_container_tag )
{
    typedef typename C::const_iterator DstIter;
    DstIter first1 = original.begin();
    DstIter first2 = result.begin();

    DstIter* from_orig = new DstIter[original.size()];
    DstIter* last_from_orig = from_orig;
    
    // fbp : for hashed containers, the following is needed :
    while ( first2 != result.end() )
    {
        EH_STD::pair<DstIter, DstIter> p = EH_STD::mismatch( first1, original.end(), first2 );
        if ( p.second != result.end() )
        {
            SrcIter srcItem = EH_STD::find( SrcIter(firstNew), SrcIter(lastNew), *p.second );
			
            if (srcItem == lastNew)
            {                           
                // not found in input range, probably re-ordered from the orig
                DstIter* tmp;
                tmp = EH_STD::find( from_orig, last_from_orig, p.first );
                
                // if already here, exclude
                if (tmp != last_from_orig)
                {
                    EH_STD::copy(tmp+1, last_from_orig, tmp);
                    last_from_orig--;
                }
                else
                {
                    // register re-ordered element
                    DstIter dstItem;
                    dstItem = EH_STD::find( first1, original.end(), *p.first );
                    EH_ASSERT( dstItem != original.end() );
                    *last_from_orig = dstItem;
                    last_from_orig++;
                    ++p.first;
                }
            }
            ++p.second;
        }
        first1 = p.first;
        first2 = p.second;
    }
	
    delete [] from_orig;
}

// VC++ 
template <class C, class SrcIter>
inline void VerifyInsertion(
    const C& original, const C& result, const SrcIter& firstNew, 
    const SrcIter& lastNew, size_t, set_tag )
{
    VerifyInsertion( original, result, firstNew, lastNew, 
                     size_t(0), associative_container_tag() );
}

template <class C, class SrcIter>
inline void VerifyInsertion(const C& original, const C& result, 
                            const SrcIter& firstNew, const SrcIter& lastNew, 
                            size_t, multiset_tag )
{
    VerifyInsertion( original, result, firstNew, lastNew, 
                     size_t(0), associative_container_tag() );
}

template <class C, class SrcIter>
inline void VerifyInsertion(
    const C& original, const C& result, const SrcIter& firstNew, 
    const SrcIter& lastNew, size_t, map_tag )
{
    VerifyInsertion( original, result, firstNew, lastNew, 
                     size_t(0), associative_container_tag() );
}

template <class C, class SrcIter>
inline void VerifyInsertion(
    const C& original, const C& result, const SrcIter& firstNew, 
    const SrcIter& lastNew, size_t, multimap_tag )
{
    VerifyInsertion( original, result, firstNew, lastNew, 
                     size_t(0), associative_container_tag() );
}

template <class C, class SrcIter>
void VerifyInsertion(
# ifdef _MSC_VER
    const C& original, const C& result, SrcIter firstNew, 
    SrcIter lastNew, size_t insPos, sequence_container_tag )
# else
    const C& original, const C& result, const SrcIter& firstNew, 
    const SrcIter& lastNew, size_t insPos, sequence_container_tag )
# endif
{
    typename C::const_iterator p1 = original.begin();
    typename C::const_iterator p2 = result.begin();
    SrcIter tmp(firstNew);
    
    for ( size_t n = 0; n < insPos; n++, ++p1, ++p2)
        EH_ASSERT( *p1 == *p2 );

    for (; tmp != lastNew; ++p2, ++tmp ) {
        EH_ASSERT(p2 != result.end());
        EH_ASSERT(*p2 == *tmp);
    }

    for (;  p2 != result.end(); ++p1, ++p2 )
        EH_ASSERT( *p1 == *p2 );
    EH_ASSERT( p1 == original.end() );
}

template <class C, class SrcIter>
inline void VerifyInsertion( const C& original, const C& result, 
                             const SrcIter& firstNew, 
                             const SrcIter& lastNew, size_t insPos )
{
    EH_ASSERT( result.size() == original.size() + 
            CountNewItems( original, firstNew, lastNew, 
                           container_category(original) ) );
    VerifyInsertion( original, result, firstNew, lastNew, insPos, 
                     container_category(original) );
}

template <class C, class Value>
void VerifyInsertN( const C& original, const C& result, size_t insCnt, 
                    const Value& val, size_t insPos )
{
    typename C::const_iterator p1 = original.begin();
    typename C::const_iterator p2 = result.begin();
	(void)val;		//*TY 02/06/2000 - to suppress unused variable warning under nondebug build

    for ( size_t n = 0; n < insPos; n++ )
        EH_ASSERT( *p1++ == *p2++ );

    while ( insCnt-- > 0 )
    {
        EH_ASSERT(p2 != result.end());
        EH_ASSERT(*p2 == val );
        ++p2;
    }
	
    while ( p2 != result.end() ) {
        EH_ASSERT( *p1 == *p2 );
        ++p1; ++p2;
    }
    EH_ASSERT( p1 == original.end() );
}

template <class C>
void prepare_insert_n( C&, size_t ) {}

// Metrowerks 1.8 compiler requires that specializations appear first (!!)
// or it won't call them. Fixed in 1.9, though.
inline void MakeRandomValue(bool& b) { b = bool(random_number(2)); }

template<class T>
inline void MakeRandomValue(T&) {}

template <class C>
struct test_insert_one
{
    test_insert_one( const C& orig, int pos =-1 )
        : original( orig ), fPos( random_number( orig.size() ))
    {
        MakeRandomValue( fInsVal );
        if ( pos != -1 )
        {
            fPos = size_t(pos);
            if ( pos == 0 )
                gTestController.SetCurrentTestName("single insertion at begin()");
            else
                gTestController.SetCurrentTestName("single insertion at end()");
        }
        else
            gTestController.SetCurrentTestName("single insertion at random position");
    }
	
    void operator()( C& c ) const
    {
        prepare_insert_n( c, (size_t)1 );
        typename C::iterator pos = c.begin();
        EH_STD::advance( pos, size_t(fPos) );
        c.insert( pos, fInsVal );
		
        // Prevent simulated failures during verification
        gTestController.CancelFailureCountdown();
        // Success. Check results.
        VerifyInsertion( original, c, &fInsVal, 1+&fInsVal, fPos );
    }
private:
    typename C::value_type fInsVal;
    const C& original;
    size_t fPos;
};

template <class C>
struct test_insert_n
{
    test_insert_n( const C& orig, size_t insCnt, int pos =-1 )
        : original( orig ), fPos( random_number( orig.size() )), fInsCnt(insCnt)
    {
        MakeRandomValue( fInsVal );
        if (pos!=-1)
        {
            fPos=size_t(pos);
            if (pos==0)
                gTestController.SetCurrentTestName("n-ary insertion at begin()");
            else
                gTestController.SetCurrentTestName("n-ary insertion at end()");
        }
        else
            gTestController.SetCurrentTestName("n-ary insertion at random position");
    }
	
    void operator()( C& c ) const
    {
        prepare_insert_n( c, fInsCnt );
        typename C::iterator pos = c.begin();
        EH_STD::advance( pos, fPos );
        c.insert( pos, fInsCnt, fInsVal );
		
        // Prevent simulated failures during verification
        gTestController.CancelFailureCountdown();
        // Success. Check results.
        VerifyInsertN( original, c, fInsCnt, fInsVal, fPos );
    }
private:
    typename C::value_type fInsVal;
    const C& original;
    size_t fPos;
    size_t fInsCnt;
};

template <class C>
struct test_insert_value
{
    test_insert_value( const C& orig )
        : original( orig )
    {
        MakeRandomValue( fInsVal );
        gTestController.SetCurrentTestName("insertion or random value");
    }
	
    void operator()( C& c ) const
    {
        c.insert( fInsVal );
		
        // Prevent simulated failures during verification
        gTestController.CancelFailureCountdown();
        // Success. Check results.
        VerifyInsertion( original, c, &fInsVal, 1+&fInsVal, size_t(0) );
    }
private:
    typename C::value_type fInsVal;
    const C& original;
};

template <class C>
struct test_insert_noresize
{
    test_insert_noresize( const C& orig )
        : original( orig )
    {
        MakeRandomValue( fInsVal );
        gTestController.SetCurrentTestName("insertion of random value without resize");
    }

    void operator()( C& c ) const
    {
        c.insert_noresize( fInsVal );

        // Prevent simulated failures during verification
        gTestController.CancelFailureCountdown();
        // Success. Check results.
        VerifyInsertion( original, c, &fInsVal, 1+&fInsVal, size_t(0) );
    }
private:
    typename C::value_type fInsVal;
    const C& original;
};

template <class C, class Position, class Iter>
void do_insert_range( C& c_inst, Position offset, 
                      Iter first, Iter last, sequence_container_tag )
{
    typedef typename C::iterator CIter;
    CIter pos = c_inst.begin();
    EH_STD::advance( pos, offset );
    c_inst.insert( pos, first, last );
}

template <class C, class Position, class Iter>
void do_insert_range( C& c, Position, 
                      Iter first, Iter last, associative_container_tag )
{
    c.insert( first, last );
}

template <class C, class Position, class Iter>
void do_insert_range( C& c, Position, Iter first, Iter last, multiset_tag )
{
    c.insert( first, last );
}

template <class C, class Position, class Iter>
void do_insert_range( C& c, Position, Iter first, Iter last, multimap_tag )
{
    c.insert( first, last );
}

template <class C, class Position, class Iter>
void do_insert_range( C& c, Position, Iter first, Iter last, set_tag )
{
    c.insert( first, last );
}

template <class C, class Position, class Iter>
void do_insert_range( C& c, Position, Iter first, Iter last, map_tag )
{
    c.insert( first, last );
}

/*
template <class C, class Iter>
void prepare_insert_range( C&, size_t, Iter, Iter) {}
*/

template <class C, class Iter>
struct test_insert_range
{
    test_insert_range( const C& orig, Iter first, Iter last, int pos=-1 )
        : fFirst( first ),
          fLast( last ),
          original( orig ),
          fPos( random_number( orig.size() ))
    {
        gTestController.SetCurrentTestName("range insertion");
        if ( pos != -1 )
        {
            fPos = size_t(pos);
            if ( pos == 0 )
                gTestController.SetCurrentTestName("range insertion at begin()");
            else
                gTestController.SetCurrentTestName("range insertion at end()");
        }
        else
            gTestController.SetCurrentTestName("range insertion at random position");
    }

    void operator()( C& c ) const
    {
//        prepare_insert_range( c, fPos, fFirst, fLast );
        do_insert_range( c, fPos, fFirst, fLast, container_category(c) );
		
        // Prevent simulated failures during verification
        gTestController.CancelFailureCountdown();
        // Success. Check results.
        VerifyInsertion( original, c, fFirst, fLast, fPos );
    }
private:
    Iter fFirst, fLast;
    const C& original;
    size_t fPos;
};

template <class C, class Iter>
test_insert_range<C, Iter> insert_range_tester( const C& orig, const Iter& first, const Iter& last )
{
    return test_insert_range<C, Iter>( orig, first, last );
}

template <class C, class Iter>
test_insert_range<C, Iter> insert_range_at_begin_tester( const C& orig, const Iter& first, const Iter& last )
{
	return test_insert_range<C, Iter>( orig, first, last , 0);
}

template <class C, class Iter>
test_insert_range<C, Iter> insert_range_at_end_tester( const C& orig, const Iter& first, const Iter& last )
{
	return test_insert_range<C, Iter>( orig, first, last , (int)orig.size());
}
#endif // test_insert_H_