Fixed iterator issues in set/map
[libcds.git] / tests / test-hdr / map / hdr_map.h
1 //$$CDS-header$$
2
3 #ifndef __CDSTEST_HDR_MAP_H
4 #define __CDSTEST_HDR_MAP_H
5 #include "size_check.h"
6
7 #include "cppunit/cppunit_proxy.h"
8 #include <cds/os/timer.h>
9 #include <cds/opt/hash.h>
10 #include <functional>   // ref
11 #include <algorithm>    // random_shuffle
12
13 namespace cds { namespace container {}}
14
15 namespace map {
16     using misc::check_size;
17
18     namespace cc = cds::container;
19     namespace co = cds::opt;
20
21     // MichaelHashSet based on MichaelList
22     class HashMapHdrTest: public CppUnitMini::TestCase
23     {
24     public:
25         typedef int key_type;
26
27         struct value_type {
28             int m_val;
29
30             value_type()
31                 : m_val(0)
32             {}
33
34             value_type( int n )
35                 : m_val( n )
36             {}
37
38             value_type( value_type&& v )
39                 : m_val( v.m_val )
40             {}
41
42             value_type( value_type const& v )
43                 : m_val( v.m_val )
44             {}
45
46             value_type& operator=( value_type const& v )
47             {
48                 m_val = v.m_val;
49                 return *this;
50             }
51         };
52
53         typedef std::pair<key_type const, value_type> pair_type;
54
55         struct less
56         {
57             bool operator ()(int v1, int v2 ) const
58             {
59                 return v1 < v2;
60             }
61         };
62
63         struct cmp {
64             int operator ()(int v1, int v2 ) const
65             {
66                 if ( v1 < v2 )
67                     return -1;
68                 return v1 > v2 ? 1 : 0;
69             }
70         };
71
72         struct equal {
73             bool operator ()(int v1, int v2 ) const
74             {
75                 return v1 == v2;
76             }
77         };
78
79         struct hash_int {
80             size_t operator()( int i ) const
81             {
82                 return co::v::hash<int>()( i );
83             }
84
85             template <typename T>
86             size_t operator()( T const& i ) const
87             {
88                 return co::v::hash<int>()( i.nKey );
89             }
90         };
91
92         struct simple_item_counter {
93             size_t  m_nCount;
94
95             simple_item_counter()
96                 : m_nCount(0)
97             {}
98
99             size_t operator ++()
100             {
101                 return ++m_nCount;
102             }
103
104             size_t operator --()
105             {
106                 return --m_nCount;
107             }
108
109             void reset()
110             {
111                 m_nCount = 0;
112             }
113
114             operator size_t() const
115             {
116                 return m_nCount;
117             }
118         };
119
120         template <typename Map>
121         struct insert_functor
122         {
123             typedef typename Map::value_type pair_type;
124
125             // insert ftor
126             void operator()( pair_type& item )
127             {
128                 item.second.m_val = item.first * 3;
129             }
130
131             // ensure ftor
132             void operator()( bool bNew, pair_type& item )
133             {
134                 if ( bNew )
135                     item.second.m_val = item.first * 2;
136                 else
137                     item.second.m_val = item.first * 5;
138             }
139         };
140
141         struct check_value {
142             int     m_nExpected;
143
144             check_value( int nExpected )
145                 : m_nExpected( nExpected )
146             {}
147
148             template <typename T>
149             void operator ()( T& pair )
150             {
151                 CPPUNIT_ASSERT_CURRENT( pair.second.m_val == m_nExpected );
152             }
153             template <typename T, typename Q>
154             void operator ()( T& pair, Q )
155             {
156                 CPPUNIT_ASSERT_CURRENT( pair.second.m_val == m_nExpected );
157             }
158         };
159
160         struct extract_functor
161         {
162             int *   m_pVal;
163             void operator()( pair_type const& val )
164             {
165                 *m_pVal = val.second.m_val;
166             }
167         };
168
169         struct other_item {
170             int nKey;
171
172             other_item( int key )
173                 : nKey(key)
174             {}
175         };
176
177         struct other_less
178         {
179             bool operator ()(int v1, other_item const& v2 ) const
180             {
181                 return v1 < v2.nKey;
182             }
183             bool operator ()(other_item const& v1, int v2 ) const
184             {
185                 return v1.nKey < v2;
186             }
187         };
188
189
190         template <class Map>
191         void test_int()
192         {
193             Map m( 100, 4 );
194
195             test_int_with(m);
196
197             // extract/get test
198             CPPUNIT_ASSERT( m.empty() );
199             {
200                 const int nLimit = 100;
201                 typename Map::guarded_ptr gp;
202                 int arrRandom[nLimit];
203                 for ( int i = 0; i < nLimit; ++i )
204                     arrRandom[i] = i;
205                 std::random_shuffle( arrRandom, arrRandom + nLimit );
206
207                 for ( int i = 0; i < nLimit; ++i )
208                     CPPUNIT_ASSERT( m.insert( arrRandom[i], arrRandom[i] ));
209
210                 for ( int i = 0; i < nLimit; ++i ) {
211                     int nKey = arrRandom[i];
212                     CPPUNIT_ASSERT( m.get(gp, nKey ));
213                     CPPUNIT_ASSERT( !gp.empty());
214                     CPPUNIT_CHECK( gp->first == nKey );
215                     CPPUNIT_CHECK( gp->second.m_val == nKey );
216
217                     CPPUNIT_ASSERT( m.extract(gp, nKey));
218                     CPPUNIT_ASSERT( !gp.empty());
219                     CPPUNIT_CHECK( gp->first == nKey );
220                     CPPUNIT_CHECK( gp->second.m_val == nKey );
221                     CPPUNIT_CHECK( !m.get(gp, nKey));
222                     gp.release();
223
224                     CPPUNIT_CHECK( !m.extract(gp, nKey));
225                     CPPUNIT_CHECK( gp.empty());
226                 }
227                 CPPUNIT_ASSERT( m.empty() );
228
229                 for ( int i = 0; i < nLimit; ++i )
230                     CPPUNIT_ASSERT( m.insert( arrRandom[i], arrRandom[i] ));
231
232                 for ( int i = 0; i < nLimit; ++i ) {
233                     int nKey = arrRandom[i];
234                     CPPUNIT_ASSERT( m.get_with(gp, other_item(nKey), other_less() ));
235                     CPPUNIT_ASSERT( !gp.empty());
236                     CPPUNIT_CHECK( gp->first == nKey );
237                     CPPUNIT_CHECK( gp->second.m_val == nKey );
238
239                     CPPUNIT_ASSERT( m.extract_with(gp, other_item(nKey), other_less() ));
240                     CPPUNIT_ASSERT( !gp.empty());
241                     CPPUNIT_CHECK( gp->first == nKey );
242                     CPPUNIT_CHECK( gp->second.m_val == nKey );
243                     CPPUNIT_CHECK( !m.get_with(gp, other_item(nKey), other_less() ));
244                     gp.release();
245
246                     CPPUNIT_CHECK( !m.extract_with(gp, other_item(nKey), other_less() ));
247                     CPPUNIT_CHECK( gp.empty());
248                 }
249                 CPPUNIT_ASSERT( m.empty() );
250             }
251
252             // iterator test
253             test_iter<Map>();
254         }
255
256         template <class Map>
257         void test_rcu()
258         {
259             Map m( 52, 4 );
260
261             test_int_with(m);
262
263             // extract/get test
264             {
265                 typedef typename Map::gc    rcu;
266                 typedef typename Map::rcu_lock rcu_lock;
267                 typedef typename Map::value_type value_type;
268                 typename Map::exempt_ptr ep;
269
270                 static size_t const nLimit = 100;
271                 int arr[nLimit];
272                 for ( size_t i = 0; i < nLimit; ++i )
273                     arr[i] = (int) i;
274                 std::random_shuffle( arr, arr + nLimit );
275
276                 for ( size_t i = 0; i < nLimit; ++i )
277                     CPPUNIT_ASSERT( m.insert( arr[i], arr[i] ));
278
279                 for ( size_t i = 0; i < nLimit; i += 2 ) {
280                     value_type * pVal;
281                     int nKey = arr[i];
282                     {
283                         rcu_lock l;
284                         pVal = m.get( nKey );
285                         CPPUNIT_ASSERT( pVal != nullptr );
286                         CPPUNIT_CHECK( pVal->first == nKey );
287                         CPPUNIT_CHECK( pVal->second.m_val == nKey );
288
289                         CPPUNIT_ASSERT( m.extract( ep, nKey ));
290                         CPPUNIT_ASSERT( !ep.empty() );
291                         CPPUNIT_CHECK( pVal->first == ep->first );
292                         CPPUNIT_CHECK( pVal->second.m_val == ep->second.m_val );
293                     }
294                     ep.release();
295                     {
296                         rcu_lock l;
297                         CPPUNIT_CHECK( m.get( nKey ) == nullptr );
298                         CPPUNIT_CHECK( !m.extract( ep, nKey ));
299                         CPPUNIT_CHECK( ep.empty() );
300
301                         nKey = arr[i+1];
302                         pVal = m.get_with( other_item(nKey), other_less() );
303                         CPPUNIT_ASSERT( pVal != nullptr );
304                         CPPUNIT_CHECK( pVal->first == nKey );
305                         CPPUNIT_CHECK( pVal->second.m_val == nKey );
306
307                         CPPUNIT_ASSERT( m.extract_with( ep, other_item(nKey), other_less() ));
308                         CPPUNIT_ASSERT( !ep.empty() );
309                         CPPUNIT_CHECK( pVal->first == ep->first );
310                         CPPUNIT_CHECK( pVal->second.m_val == (*ep).second.m_val );
311                     }
312                     ep.release();
313                     {
314                         rcu_lock l;
315                         CPPUNIT_CHECK( m.get_with( other_item(nKey), other_less() ) == nullptr );
316                         CPPUNIT_CHECK( !m.extract_with( ep, other_item(nKey), other_less() ));
317                         CPPUNIT_CHECK( ep.empty() );
318                     }
319                 }
320                 CPPUNIT_CHECK( m.empty() );
321                 CPPUNIT_CHECK( check_size( m, 0 ));
322                 {
323                     rcu_lock l;
324                     CPPUNIT_CHECK( m.get( int(nLimit / 2) ) == nullptr );
325                     CPPUNIT_CHECK( !m.extract( ep, int(nLimit / 2) ));
326                     CPPUNIT_CHECK( ep.empty() );
327                 }
328             }
329
330             // iterator test
331             test_iter<Map>();
332         }
333
334         template <class Map>
335         void test_int_with( Map& m )
336         {
337             std::pair<bool, bool> ensureResult;
338
339             // insert
340             CPPUNIT_ASSERT( m.empty() );
341             CPPUNIT_ASSERT( check_size( m, 0 ));
342             CPPUNIT_ASSERT( !m.find(25) );
343             CPPUNIT_ASSERT( m.insert( 25 ) )    ;   // value = 0
344             CPPUNIT_ASSERT( m.find(25) );
345             CPPUNIT_ASSERT( !m.empty() );
346             CPPUNIT_ASSERT( check_size( m, 1 ));
347             CPPUNIT_ASSERT( m.find(25) );
348
349             CPPUNIT_ASSERT( !m.insert( 25 ) );
350             CPPUNIT_ASSERT( !m.empty() );
351             CPPUNIT_ASSERT( check_size( m, 1 ));
352
353             CPPUNIT_ASSERT( !m.find_with(10, less()) );
354             CPPUNIT_ASSERT( m.insert( 10, 10 ) );
355             CPPUNIT_ASSERT( !m.empty() );
356             CPPUNIT_ASSERT( check_size( m, 2 ));
357             CPPUNIT_ASSERT( m.find_with(10, less()) );
358
359             CPPUNIT_ASSERT( !m.insert( 10, 20 ) );
360             CPPUNIT_ASSERT( !m.empty() );
361             CPPUNIT_ASSERT( check_size( m, 2 ));
362
363             CPPUNIT_ASSERT( !m.find(30) );
364             CPPUNIT_ASSERT( m.insert_key( 30, insert_functor<Map>() ) )    ; // value = 90
365             CPPUNIT_ASSERT( !m.empty() );
366             CPPUNIT_ASSERT( check_size( m, 3 ));
367             CPPUNIT_ASSERT( m.find(30) );
368
369             CPPUNIT_ASSERT( !m.insert_key( 10, insert_functor<Map>() ) );
370             CPPUNIT_ASSERT( !m.insert_key( 25, insert_functor<Map>() ) );
371             CPPUNIT_ASSERT( !m.insert_key( 30, insert_functor<Map>() ) );
372
373             // ensure (new key)
374             CPPUNIT_ASSERT( !m.find(27) );
375             ensureResult = m.ensure( 27, insert_functor<Map>() ) ;   // value = 54
376             CPPUNIT_ASSERT( ensureResult.first );
377             CPPUNIT_ASSERT( ensureResult.second );
378             CPPUNIT_ASSERT( m.find(27) );
379
380             // find test
381             check_value chk(10);
382             CPPUNIT_ASSERT( m.find( 10, std::ref(chk) ));
383             chk.m_nExpected = 0;
384             CPPUNIT_ASSERT( m.find_with( 25, less(), std::ref( chk ) ) );
385             chk.m_nExpected = 90;
386             CPPUNIT_ASSERT( m.find( 30, std::ref( chk ) ) );
387             chk.m_nExpected = 54;
388             CPPUNIT_ASSERT( m.find( 27, std::ref( chk ) ) );
389
390             ensureResult = m.ensure( 10, insert_functor<Map>() ) ;   // value = 50
391             CPPUNIT_ASSERT( ensureResult.first );
392             CPPUNIT_ASSERT( !ensureResult.second );
393             chk.m_nExpected = 50;
394             CPPUNIT_ASSERT( m.find( 10, std::ref( chk ) ) );
395
396             // erase test
397             CPPUNIT_ASSERT( !m.find(100) );
398             CPPUNIT_ASSERT( !m.erase( 100 )) ;  // not found
399
400             CPPUNIT_ASSERT( m.find(25) );
401             CPPUNIT_ASSERT( check_size( m, 4 ));
402             CPPUNIT_ASSERT( m.erase( 25 ));
403             CPPUNIT_ASSERT( !m.empty() );
404             CPPUNIT_ASSERT( check_size( m, 3 ));
405             CPPUNIT_ASSERT( !m.find(25) );
406             CPPUNIT_ASSERT( !m.erase( 25 ));
407
408             CPPUNIT_ASSERT( !m.find(258) );
409             CPPUNIT_ASSERT( m.insert(258))
410             CPPUNIT_ASSERT( check_size( m, 4 ));
411             CPPUNIT_ASSERT( m.find_with(258, less()) );
412             CPPUNIT_ASSERT( m.erase_with( 258, less() ));
413             CPPUNIT_ASSERT( !m.empty() );
414             CPPUNIT_ASSERT( check_size( m, 3 ));
415             CPPUNIT_ASSERT( !m.find(258) );
416             CPPUNIT_ASSERT( !m.erase_with( 258, less() ));
417
418             int nVal;
419             extract_functor ext;
420             ext.m_pVal = &nVal;
421
422             CPPUNIT_ASSERT( !m.find(29) );
423             CPPUNIT_ASSERT( m.insert(29, 290));
424             CPPUNIT_ASSERT( check_size( m, 4 ));
425             CPPUNIT_ASSERT( m.erase_with( 29, less(), std::ref( ext ) ) );
426             CPPUNIT_ASSERT( !m.empty() );
427             CPPUNIT_ASSERT( check_size( m, 3 ));
428             CPPUNIT_ASSERT( nVal == 290 );
429             nVal = -1;
430             CPPUNIT_ASSERT( !m.erase_with( 29, less(), std::ref( ext ) ) );
431             CPPUNIT_ASSERT( nVal == -1 );
432
433             CPPUNIT_ASSERT( m.erase( 30, std::ref( ext ) ) );
434             CPPUNIT_ASSERT( !m.empty() );
435             CPPUNIT_ASSERT( check_size( m, 2 ));
436             CPPUNIT_ASSERT( nVal == 90 );
437             nVal = -1;
438             CPPUNIT_ASSERT( !m.erase( 30, std::ref( ext ) ) );
439             CPPUNIT_ASSERT( nVal == -1 );
440
441             m.clear();
442             CPPUNIT_ASSERT( m.empty() );
443             CPPUNIT_ASSERT( check_size( m, 0 ));
444
445             // emplace test
446             CPPUNIT_ASSERT( m.emplace(126) ) ; // key = 126, val = 0
447             CPPUNIT_ASSERT( m.emplace(137, 731))    ;   // key = 137, val = 731
448             CPPUNIT_ASSERT( m.emplace( 149, value_type(941) ))   ;   // key = 149, val = 941
449
450             CPPUNIT_ASSERT( !m.empty() );
451             CPPUNIT_ASSERT( check_size( m, 3 ));
452
453             chk.m_nExpected = 0;
454             CPPUNIT_ASSERT( m.find( 126, std::ref(chk) ));
455             chk.m_nExpected = 731;
456             CPPUNIT_ASSERT( m.find_with( 137, less(), std::ref(chk) ));
457             chk.m_nExpected = 941;
458             CPPUNIT_ASSERT( m.find( 149, std::ref(chk) ));
459
460             CPPUNIT_ASSERT( !m.emplace(126, 621)) ; // already in map
461             chk.m_nExpected = 0;
462             CPPUNIT_ASSERT( m.find( 126, std::ref(chk) ));
463             CPPUNIT_ASSERT( !m.empty() );
464             CPPUNIT_ASSERT( check_size( m, 3 ));
465
466             m.clear();
467             CPPUNIT_ASSERT( m.empty() );
468             CPPUNIT_ASSERT( check_size( m, 0 ));
469         }
470
471
472         template <class Map>
473         void test_int_nogc()
474         {
475             typedef typename Map::iterator          iterator;
476             typedef typename Map::const_iterator    const_iterator;
477
478             {
479                 Map m( 52, 4 );
480
481                 CPPUNIT_ASSERT( m.empty() );
482                 CPPUNIT_ASSERT( check_size( m, 0 ));
483
484                 CPPUNIT_ASSERT( m.find(10) == m.end() );
485                 iterator it = m.insert( 10 );
486                 CPPUNIT_ASSERT( it != m.end() );
487                 CPPUNIT_ASSERT( !m.empty() );
488                 CPPUNIT_ASSERT( check_size( m, 1 ));
489                 CPPUNIT_ASSERT( m.find(10) == it );
490                 CPPUNIT_ASSERT( it->first == 10 );
491                 CPPUNIT_ASSERT( it->second.m_val == 0 );
492
493                 CPPUNIT_ASSERT( m.find(100) == m.end() );
494                 it = m.insert( 100, 200 );
495                 CPPUNIT_ASSERT( it != m.end() );
496                 CPPUNIT_ASSERT( !m.empty() );
497                 CPPUNIT_ASSERT( check_size( m, 2 ));
498                 CPPUNIT_ASSERT( m.find_with(100, less()) == it );
499                 CPPUNIT_ASSERT( it->first == 100 );
500                 CPPUNIT_ASSERT( it->second.m_val == 200 );
501
502                 CPPUNIT_ASSERT( m.find(55) == m.end() );
503                 it = m.insert_key( 55, insert_functor<Map>() );
504                 CPPUNIT_ASSERT( it != m.end() );
505                 CPPUNIT_ASSERT( !m.empty() );
506                 CPPUNIT_ASSERT( check_size( m, 3 ));
507                 CPPUNIT_ASSERT( m.find(55) == it );
508                 CPPUNIT_ASSERT( it->first == 55 );
509                 CPPUNIT_ASSERT( it->second.m_val == 55 * 3 );
510
511                 CPPUNIT_ASSERT( m.insert( 55 ) == m.end() );
512                 CPPUNIT_ASSERT( m.insert( 55, 10 ) == m.end() );
513                 CPPUNIT_ASSERT( m.insert_key( 55, insert_functor<Map>()) == m.end() );
514
515                 CPPUNIT_ASSERT( m.find(10) != m.end() );
516                 std::pair<iterator, bool> ensureResult = m.ensure( 10 );
517                 CPPUNIT_ASSERT( ensureResult.first != m.end() );
518                 CPPUNIT_ASSERT( !ensureResult.second  );
519                 CPPUNIT_ASSERT( !m.empty() );
520                 ensureResult.first->second.m_val = ensureResult.first->first * 5;
521                 CPPUNIT_ASSERT( check_size( m, 3 ));
522                 CPPUNIT_ASSERT( m.find(10) == ensureResult.first );
523                 it = m.find(10);
524                 CPPUNIT_ASSERT( it != m.end() );
525                 CPPUNIT_ASSERT( it->second.m_val == 50 );
526
527                 CPPUNIT_ASSERT( m.find(120) == m.end() );
528                 ensureResult = m.ensure( 120 );
529                 CPPUNIT_ASSERT( ensureResult.first != m.end() );
530                 CPPUNIT_ASSERT( ensureResult.second  );
531                 CPPUNIT_ASSERT( !m.empty() );
532                 CPPUNIT_ASSERT( check_size( m, 4 ));
533                 ensureResult.first->second.m_val = ensureResult.first->first * 5;
534                 CPPUNIT_ASSERT( m.find_with(120, less()) == ensureResult.first );
535                 it = m.find_with(120, less());
536                 CPPUNIT_ASSERT( it != m.end() );
537                 CPPUNIT_ASSERT( it->second.m_val == 120 * 5 );
538                 CPPUNIT_ASSERT( m.find_with(120, less()) == m.find(120) );
539
540                 // emplace test
541                 it = m.emplace( 151 ) ;  // key = 151,  val = 0
542                 CPPUNIT_ASSERT( it != m.end() );
543                 CPPUNIT_ASSERT( it->first == 151 );
544                 CPPUNIT_ASSERT( it->second.m_val == 0 );
545
546                 it = m.emplace( 174, 471 ) ; // key == 174, val = 471
547                 CPPUNIT_ASSERT( it != m.end() );
548                 CPPUNIT_ASSERT( it->first == 174 );
549                 CPPUNIT_ASSERT( it->second.m_val == 471 );
550
551                 it = m.emplace( 190, value_type(91)) ; // key == 190, val = 19
552                 CPPUNIT_ASSERT( it != m.end() );
553                 CPPUNIT_ASSERT( it->first == 190 );
554                 CPPUNIT_ASSERT( it->second.m_val == 91 );
555
556                 it = m.emplace( 151, 1051 );
557                 CPPUNIT_ASSERT( it == m.end());
558
559                 it = m.find( 174 );
560                 CPPUNIT_ASSERT( it != m.end() );
561                 CPPUNIT_ASSERT( it->first == 174 );
562                 CPPUNIT_ASSERT( it->second.m_val == 471 );
563
564                 it = m.find( 190 );
565                 CPPUNIT_ASSERT( it != m.end() );
566                 CPPUNIT_ASSERT( it->first == 190 );
567                 CPPUNIT_ASSERT( it->second.m_val == 91 );
568
569                 it = m.find( 151 );
570                 CPPUNIT_ASSERT( it != m.end() );
571                 CPPUNIT_ASSERT( it->first == 151 );
572                 CPPUNIT_ASSERT( it->second.m_val == 0 );
573             }
574
575             // iterator test
576
577             {
578                 Map m( 52, 4 );
579
580                 for ( int i = 0; i < 500; ++i ) {
581                     CPPUNIT_ASSERT( m.insert( i, i * 2 ) != m.end() );
582                 }
583                 CPPUNIT_ASSERT( check_size( m, 500 ));
584
585                 {
586                     typename Map::iterator it( m.begin() );
587                     typename Map::const_iterator cit( m.cbegin() );
588                     CPPUNIT_CHECK( it == cit );
589                     CPPUNIT_CHECK( it != m.end() );
590                     CPPUNIT_CHECK( it != m.cend() );
591                     CPPUNIT_CHECK( cit != m.end() );
592                     CPPUNIT_CHECK( cit != m.cend() );
593                     ++it;
594                     CPPUNIT_CHECK( it != cit );
595                     CPPUNIT_CHECK( it != m.end() );
596                     CPPUNIT_CHECK( it != m.cend() );
597                     CPPUNIT_CHECK( cit != m.end() );
598                     CPPUNIT_CHECK( cit != m.cend() );
599                     ++cit;
600                     CPPUNIT_CHECK( it == cit );
601                     CPPUNIT_CHECK( it != m.end() );
602                     CPPUNIT_CHECK( it != m.cend() );
603                     CPPUNIT_CHECK( cit != m.end() );
604                     CPPUNIT_CHECK( cit != m.cend() );
605                 }
606
607
608                 for ( iterator it = m.begin(), itEnd = m.end(); it != itEnd; ++it ) {
609                     iterator it2 = it;
610                     CPPUNIT_CHECK( it2 == it );
611                     CPPUNIT_CHECK( it2 == itEnd );
612                     CPPUNIT_ASSERT( it->first * 2 == (*it).second.m_val );
613                     it->second = it->first;
614                 }
615
616                 Map const& refMap = m;
617                 for ( const_iterator it = refMap.begin(), itEnd = refMap.end(); it != itEnd; ++it ) {
618                     CPPUNIT_ASSERT( it->first == it->second.m_val );
619                     CPPUNIT_ASSERT( (*it).first == (*it).second.m_val );
620                 }
621             }
622         }
623
624         template <class Map>
625         void test_iter()
626         {
627             typedef typename Map::iterator          iterator;
628             typedef typename Map::const_iterator    const_iterator;
629
630             Map s( 100, 4 );
631
632             const int nMaxCount = 500;
633             for ( int i = 0; i < nMaxCount; ++i ) {
634                 CPPUNIT_ASSERT( s.insert( i, i * 2 ));
635             }
636
637             {
638                 typename Map::iterator it( s.begin() );
639                 typename Map::const_iterator cit( s.cbegin() );
640                 CPPUNIT_CHECK( it == cit );
641                 CPPUNIT_CHECK( it != s.end() );
642                 CPPUNIT_CHECK( it != s.cend() );
643                 CPPUNIT_CHECK( cit != s.end() );
644                 CPPUNIT_CHECK( cit != s.cend() );
645                 ++it;
646                 CPPUNIT_CHECK( it != cit );
647                 CPPUNIT_CHECK( it != s.end() );
648                 CPPUNIT_CHECK( it != s.cend() );
649                 CPPUNIT_CHECK( cit != s.end() );
650                 CPPUNIT_CHECK( cit != s.cend() );
651                 ++cit;
652                 CPPUNIT_CHECK( it == cit );
653                 CPPUNIT_CHECK( it != s.end() );
654                 CPPUNIT_CHECK( it != s.cend() );
655                 CPPUNIT_CHECK( cit != s.end() );
656                 CPPUNIT_CHECK( cit != s.cend() );
657             }
658
659             int nCount = 0;
660             for ( iterator it = s.begin(), itEnd = s.end(); it != itEnd; ++it ) {
661                 CPPUNIT_ASSERT( it->first * 2 == it->second.m_val );
662                 CPPUNIT_ASSERT( (*it).first * 2 == (*it).second.m_val );
663                 it->second.m_val = it->first;
664                 ++nCount;
665             }
666             CPPUNIT_ASSERT( nCount == nMaxCount );
667
668             Map const& refSet = s;
669             nCount = 0;
670             for ( const_iterator it = refSet.begin(), itEnd = refSet.end(); it != itEnd; ++it ) {
671                 CPPUNIT_ASSERT( it->first == it->second.m_val );
672                 CPPUNIT_ASSERT( (*it).first == (*it).second.m_val );
673                 ++nCount;
674             }
675             CPPUNIT_ASSERT( nCount == nMaxCount );
676         }
677
678
679         void Michael_HP_cmp();
680         void Michael_HP_less();
681         void Michael_HP_cmpmix();
682
683         void Michael_DHP_cmp();
684         void Michael_DHP_less();
685         void Michael_DHP_cmpmix();
686
687         void Michael_RCU_GPI_cmp();
688         void Michael_RCU_GPI_less();
689         void Michael_RCU_GPI_cmpmix();
690
691         void Michael_RCU_GPB_cmp();
692         void Michael_RCU_GPB_less();
693         void Michael_RCU_GPB_cmpmix();
694
695         void Michael_RCU_GPT_cmp();
696         void Michael_RCU_GPT_less();
697         void Michael_RCU_GPT_cmpmix();
698
699         void Michael_RCU_SHB_cmp();
700         void Michael_RCU_SHB_less();
701         void Michael_RCU_SHB_cmpmix();
702
703         void Michael_RCU_SHT_cmp();
704         void Michael_RCU_SHT_less();
705         void Michael_RCU_SHT_cmpmix();
706
707         void Michael_nogc_cmp();
708         void Michael_nogc_less();
709         void Michael_nogc_cmpmix();
710
711         void Lazy_HP_cmp();
712         void Lazy_HP_less();
713         void Lazy_HP_cmpmix();
714
715         void Lazy_DHP_cmp();
716         void Lazy_DHP_less();
717         void Lazy_DHP_cmpmix();
718
719         void Lazy_RCU_GPI_cmp();
720         void Lazy_RCU_GPI_less();
721         void Lazy_RCU_GPI_cmpmix();
722
723         void Lazy_RCU_GPB_cmp();
724         void Lazy_RCU_GPB_less();
725         void Lazy_RCU_GPB_cmpmix();
726
727         void Lazy_RCU_GPT_cmp();
728         void Lazy_RCU_GPT_less();
729         void Lazy_RCU_GPT_cmpmix();
730
731         void Lazy_RCU_SHB_cmp();
732         void Lazy_RCU_SHB_less();
733         void Lazy_RCU_SHB_cmpmix();
734
735         void Lazy_RCU_SHT_cmp();
736         void Lazy_RCU_SHT_less();
737         void Lazy_RCU_SHT_cmpmix();
738
739         void Lazy_nogc_cmp();
740         void Lazy_nogc_less();
741         void Lazy_nogc_cmpmix();
742
743         void Split_HP_cmp();
744         void Split_HP_less();
745         void Split_HP_cmpmix();
746         void Split_HP_cmpmix_stat();
747
748         void Split_DHP_cmp();
749         void Split_DHP_less();
750         void Split_DHP_cmpmix();
751         void Split_DHP_cmpmix_stat();
752
753         void Split_RCU_GPI_cmp();
754         void Split_RCU_GPI_less();
755         void Split_RCU_GPI_cmpmix();
756         void Split_RCU_GPI_cmpmix_stat();
757
758         void Split_RCU_GPB_cmp();
759         void Split_RCU_GPB_less();
760         void Split_RCU_GPB_cmpmix();
761         void Split_RCU_GPB_cmpmix_stat();
762
763         void Split_RCU_GPT_cmp();
764         void Split_RCU_GPT_less();
765         void Split_RCU_GPT_cmpmix();
766         void Split_RCU_GPT_cmpmix_stat();
767
768         void Split_RCU_SHB_cmp();
769         void Split_RCU_SHB_less();
770         void Split_RCU_SHB_cmpmix();
771         void Split_RCU_SHB_cmpmix_stat();
772
773         void Split_RCU_SHT_cmp();
774         void Split_RCU_SHT_less();
775         void Split_RCU_SHT_cmpmix();
776         void Split_RCU_SHT_cmpmix_stat();
777
778         void Split_nogc_cmp();
779         void Split_nogc_less();
780         void Split_nogc_cmpmix();
781         void Split_nogc_cmpmix_stat();
782
783         void Split_Lazy_HP_cmp();
784         void Split_Lazy_HP_less();
785         void Split_Lazy_HP_cmpmix();
786         void Split_Lazy_HP_cmpmix_stat();
787
788         void Split_Lazy_DHP_cmp();
789         void Split_Lazy_DHP_less();
790         void Split_Lazy_DHP_cmpmix();
791         void Split_Lazy_DHP_cmpmix_stat();
792
793         void Split_Lazy_RCU_GPI_cmp();
794         void Split_Lazy_RCU_GPI_less();
795         void Split_Lazy_RCU_GPI_cmpmix();
796         void Split_Lazy_RCU_GPI_cmpmix_stat();
797
798         void Split_Lazy_RCU_GPB_cmp();
799         void Split_Lazy_RCU_GPB_less();
800         void Split_Lazy_RCU_GPB_cmpmix();
801         void Split_Lazy_RCU_GPB_cmpmix_stat();
802
803         void Split_Lazy_RCU_GPT_cmp();
804         void Split_Lazy_RCU_GPT_less();
805         void Split_Lazy_RCU_GPT_cmpmix();
806         void Split_Lazy_RCU_GPT_cmpmix_stat();
807
808         void Split_Lazy_RCU_SHB_cmp();
809         void Split_Lazy_RCU_SHB_less();
810         void Split_Lazy_RCU_SHB_cmpmix();
811         void Split_Lazy_RCU_SHB_cmpmix_stat();
812
813         void Split_Lazy_RCU_SHT_cmp();
814         void Split_Lazy_RCU_SHT_less();
815         void Split_Lazy_RCU_SHT_cmpmix();
816         void Split_Lazy_RCU_SHT_cmpmix_stat();
817
818         void Split_Lazy_nogc_cmp();
819         void Split_Lazy_nogc_less();
820         void Split_Lazy_nogc_cmpmix();
821         void Split_Lazy_nogc_cmpmix_stat();
822
823         CPPUNIT_TEST_SUITE(HashMapHdrTest)
824             CPPUNIT_TEST(Michael_HP_cmp)
825             CPPUNIT_TEST(Michael_HP_less)
826             CPPUNIT_TEST(Michael_HP_cmpmix)
827
828             CPPUNIT_TEST(Michael_DHP_cmp)
829             CPPUNIT_TEST(Michael_DHP_less)
830             CPPUNIT_TEST(Michael_DHP_cmpmix)
831
832             CPPUNIT_TEST(Michael_RCU_GPI_cmp)
833             CPPUNIT_TEST(Michael_RCU_GPI_less)
834             CPPUNIT_TEST(Michael_RCU_GPI_cmpmix)
835
836             CPPUNIT_TEST(Michael_RCU_GPB_cmp)
837             CPPUNIT_TEST(Michael_RCU_GPB_less)
838             CPPUNIT_TEST(Michael_RCU_GPB_cmpmix)
839
840             CPPUNIT_TEST(Michael_RCU_SHT_cmp)
841             CPPUNIT_TEST(Michael_RCU_SHT_less)
842             CPPUNIT_TEST(Michael_RCU_SHT_cmpmix)
843
844             CPPUNIT_TEST(Michael_RCU_SHB_cmp)
845             CPPUNIT_TEST(Michael_RCU_SHB_less)
846             CPPUNIT_TEST(Michael_RCU_SHB_cmpmix)
847
848             CPPUNIT_TEST(Michael_RCU_GPT_cmp)
849             CPPUNIT_TEST(Michael_RCU_GPT_less)
850             CPPUNIT_TEST(Michael_RCU_GPT_cmpmix)
851
852             CPPUNIT_TEST(Michael_nogc_cmp)
853             CPPUNIT_TEST(Michael_nogc_less)
854             CPPUNIT_TEST(Michael_nogc_cmpmix)
855
856             CPPUNIT_TEST(Lazy_HP_cmp)
857             CPPUNIT_TEST(Lazy_HP_less)
858             CPPUNIT_TEST(Lazy_HP_cmpmix)
859
860             CPPUNIT_TEST(Lazy_DHP_cmp)
861             CPPUNIT_TEST(Lazy_DHP_less)
862             CPPUNIT_TEST(Lazy_DHP_cmpmix)
863
864             CPPUNIT_TEST(Lazy_RCU_GPI_cmp)
865             CPPUNIT_TEST(Lazy_RCU_GPI_less)
866             CPPUNIT_TEST(Lazy_RCU_GPI_cmpmix)
867
868             CPPUNIT_TEST(Lazy_RCU_GPB_cmp)
869             CPPUNIT_TEST(Lazy_RCU_GPB_less)
870             CPPUNIT_TEST(Lazy_RCU_GPB_cmpmix)
871
872             CPPUNIT_TEST(Lazy_RCU_GPT_cmp)
873             CPPUNIT_TEST(Lazy_RCU_GPT_less)
874             CPPUNIT_TEST(Lazy_RCU_GPT_cmpmix)
875
876             CPPUNIT_TEST(Lazy_RCU_SHB_cmp)
877             CPPUNIT_TEST(Lazy_RCU_SHB_less)
878             CPPUNIT_TEST(Lazy_RCU_SHB_cmpmix)
879
880             CPPUNIT_TEST(Lazy_RCU_SHT_cmp)
881             CPPUNIT_TEST(Lazy_RCU_SHT_less)
882             CPPUNIT_TEST(Lazy_RCU_SHT_cmpmix)
883
884             CPPUNIT_TEST(Lazy_nogc_cmp)
885             CPPUNIT_TEST(Lazy_nogc_less)
886             CPPUNIT_TEST(Lazy_nogc_cmpmix)
887
888             CPPUNIT_TEST(Split_HP_cmp)
889             CPPUNIT_TEST(Split_HP_less)
890             CPPUNIT_TEST(Split_HP_cmpmix)
891             CPPUNIT_TEST( Split_HP_cmpmix_stat )
892
893             CPPUNIT_TEST(Split_DHP_cmp)
894             CPPUNIT_TEST(Split_DHP_less)
895             CPPUNIT_TEST(Split_DHP_cmpmix)
896             CPPUNIT_TEST( Split_DHP_cmpmix_stat )
897
898             CPPUNIT_TEST(Split_RCU_GPI_cmp)
899             CPPUNIT_TEST(Split_RCU_GPI_less)
900             CPPUNIT_TEST(Split_RCU_GPI_cmpmix)
901             CPPUNIT_TEST( Split_RCU_GPI_cmpmix_stat )
902
903             CPPUNIT_TEST(Split_RCU_GPB_cmp)
904             CPPUNIT_TEST(Split_RCU_GPB_less)
905             CPPUNIT_TEST(Split_RCU_GPB_cmpmix)
906             CPPUNIT_TEST( Split_RCU_GPB_cmpmix_stat )
907
908             CPPUNIT_TEST(Split_RCU_GPT_cmp)
909             CPPUNIT_TEST(Split_RCU_GPT_less)
910             CPPUNIT_TEST(Split_RCU_GPT_cmpmix)
911             CPPUNIT_TEST( Split_RCU_GPT_cmpmix_stat )
912
913             CPPUNIT_TEST(Split_RCU_SHB_cmp)
914             CPPUNIT_TEST(Split_RCU_SHB_less)
915             CPPUNIT_TEST(Split_RCU_SHB_cmpmix)
916             CPPUNIT_TEST( Split_RCU_SHB_cmpmix_stat )
917
918             CPPUNIT_TEST(Split_RCU_SHT_cmp)
919             CPPUNIT_TEST(Split_RCU_SHT_less)
920             CPPUNIT_TEST(Split_RCU_SHT_cmpmix)
921             CPPUNIT_TEST( Split_RCU_SHT_cmpmix_stat )
922
923             CPPUNIT_TEST(Split_nogc_cmp)
924             CPPUNIT_TEST(Split_nogc_less)
925             CPPUNIT_TEST(Split_nogc_cmpmix)
926             CPPUNIT_TEST( Split_nogc_cmpmix_stat )
927
928             CPPUNIT_TEST(Split_Lazy_HP_cmp)
929             CPPUNIT_TEST(Split_Lazy_HP_less)
930             CPPUNIT_TEST(Split_Lazy_HP_cmpmix)
931             CPPUNIT_TEST( Split_Lazy_HP_cmpmix_stat )
932
933             CPPUNIT_TEST(Split_Lazy_DHP_cmp)
934             CPPUNIT_TEST(Split_Lazy_DHP_less)
935             CPPUNIT_TEST(Split_Lazy_DHP_cmpmix)
936             CPPUNIT_TEST( Split_Lazy_DHP_cmpmix_stat )
937
938             CPPUNIT_TEST(Split_Lazy_RCU_GPI_cmp)
939             CPPUNIT_TEST(Split_Lazy_RCU_GPI_less)
940             CPPUNIT_TEST(Split_Lazy_RCU_GPI_cmpmix)
941             CPPUNIT_TEST( Split_Lazy_RCU_GPI_cmpmix_stat )
942
943             CPPUNIT_TEST(Split_Lazy_RCU_GPB_cmp)
944             CPPUNIT_TEST(Split_Lazy_RCU_GPB_less)
945             CPPUNIT_TEST(Split_Lazy_RCU_GPB_cmpmix)
946             CPPUNIT_TEST( Split_Lazy_RCU_GPB_cmpmix_stat )
947
948             CPPUNIT_TEST(Split_Lazy_RCU_GPT_cmp)
949             CPPUNIT_TEST(Split_Lazy_RCU_GPT_less)
950             CPPUNIT_TEST(Split_Lazy_RCU_GPT_cmpmix)
951             CPPUNIT_TEST( Split_Lazy_RCU_GPT_cmpmix_stat )
952
953             CPPUNIT_TEST(Split_Lazy_RCU_SHB_cmp)
954             CPPUNIT_TEST(Split_Lazy_RCU_SHB_less)
955             CPPUNIT_TEST(Split_Lazy_RCU_SHB_cmpmix)
956             CPPUNIT_TEST( Split_Lazy_RCU_SHB_cmpmix_stat )
957
958             CPPUNIT_TEST(Split_Lazy_RCU_SHT_cmp)
959             CPPUNIT_TEST(Split_Lazy_RCU_SHT_less)
960             CPPUNIT_TEST(Split_Lazy_RCU_SHT_cmpmix)
961             CPPUNIT_TEST( Split_Lazy_RCU_SHT_cmpmix_stat )
962
963             CPPUNIT_TEST(Split_Lazy_nogc_cmp)
964             CPPUNIT_TEST(Split_Lazy_nogc_less)
965             CPPUNIT_TEST(Split_Lazy_nogc_cmpmix)
966             CPPUNIT_TEST( Split_Lazy_nogc_cmpmix_stat )
967             CPPUNIT_TEST_SUITE_END()
968
969     };
970 }   // namespace map
971
972 #endif // #ifndef __CDSTEST_HDR_MAP_H