Replace NULL with nullptr
[libcds.git] / tests / test-hdr / ordered_list / hdr_intrusive_michael.h
1 //$$CDS-header$$
2
3 #include "cppunit/cppunit_proxy.h"
4 #include <cds/intrusive/michael_list_base.h>
5
6 namespace ordlist {
7     namespace ci = cds::intrusive;
8     namespace co = cds::opt;
9
10     class IntrusiveMichaelListHeaderTest: public CppUnitMini::TestCase
11     {
12     public:
13
14         struct stat {
15             int nDisposeCount;
16             int nEnsureExistsCall;
17             int nEnsureNewCall;
18             int nFindCall;
19             int nEraseCall;
20
21             stat()
22                 : nDisposeCount(0)
23                 , nEnsureExistsCall(0)
24                 , nEnsureNewCall(0)
25                 , nFindCall(0)
26                 , nEraseCall(0)
27             {}
28
29             stat( const stat& s )
30             {
31                 *this = s;
32             }
33
34             stat& operator =(const stat& s)
35             {
36                 memcpy( this, &s, sizeof(s));
37                 return *this;
38             }
39         };
40
41         template <typename GC>
42         struct base_int_item: public ci::michael_list::node< GC >
43         {
44             int nKey;
45             int nVal;
46
47             mutable stat    s;
48
49             base_int_item()
50             {}
51
52             base_int_item(int key, int val)
53                 : nKey( key )
54                 , nVal(val)
55                 , s()
56             {}
57
58             base_int_item(const base_int_item& v )
59                 : nKey( v.nKey )
60                 , nVal( v.nVal )
61                 , s()
62             {}
63
64             const int& key() const
65             {
66                 return nKey;
67             }
68         };
69
70         template <typename GC>
71         struct member_int_item
72         {
73             int nKey;
74             int nVal;
75
76             ci::michael_list::node< GC > hMember;
77
78             mutable stat s;
79
80             member_int_item()
81             {}
82
83             member_int_item(int key, int val)
84                 : nKey( key )
85                 , nVal(val)
86                 , s()
87             {}
88
89             member_int_item(const member_int_item& v )
90                 : nKey( v.nKey )
91                 , nVal( v.nVal )
92                 , s()
93             {}
94
95             const int& key() const
96             {
97                 return nKey;
98             }
99         };
100
101         template <typename T>
102         struct less
103         {
104             bool operator ()(const T& v1, const T& v2 ) const
105             {
106                 return v1.key() < v2.key();
107             }
108
109             template <typename Q>
110             bool operator ()(const T& v1, const Q& v2 ) const
111             {
112                 return v1.key() < v2;
113             }
114
115             template <typename Q>
116             bool operator ()(const Q& v1, const T& v2 ) const
117             {
118                 return v1 < v2.key();
119             }
120         };
121
122         struct other_item {
123             int nKey;
124
125             other_item( int n )
126                 : nKey(n)
127             {}
128         };
129
130         struct other_less {
131             template <typename T, typename Q>
132             bool operator()( T const& i1, Q const& i2) const
133             {
134                 return i1.nKey < i2.nKey;
135             }
136         };
137
138         template <typename T>
139         struct cmp {
140             int operator ()(const T& v1, const T& v2 ) const
141             {
142                 if ( v1.key() < v2.key() )
143                     return -1;
144                 return v1.key() > v2.key() ? 1 : 0;
145             }
146
147             template <typename Q>
148             int operator ()(const T& v1, const Q& v2 ) const
149             {
150                 if ( v1.key() < v2 )
151                     return -1;
152                 return v1.key() > v2 ? 1 : 0;
153             }
154
155             template <typename Q>
156             int operator ()(const Q& v1, const T& v2 ) const
157             {
158                 if ( v1 < v2.key() )
159                     return -1;
160                 return v1 > v2.key() ? 1 : 0;
161             }
162         };
163
164         struct faked_disposer
165         {
166             template <typename T>
167             void operator ()( T * p )
168             {
169                 ++p->s.nDisposeCount;
170             }
171         };
172
173         struct ensure_functor
174         {
175             template <typename T>
176             void operator ()(bool bNew, T& item, T& val )
177             {
178                 if ( bNew )
179                     ++item.s.nEnsureNewCall;
180                 else
181                     ++item.s.nEnsureExistsCall;
182             }
183         };
184
185         struct find_functor
186         {
187             template <typename T, typename Q>
188             void operator ()( T& item, Q& val )
189             {
190                 ++item.s.nFindCall;
191             }
192         };
193
194         struct erase_functor
195         {
196             template <typename T>
197             void operator()( T const& item )
198             {
199                 item.s.nEraseCall++;
200             }
201         };
202
203         template <class OrdList>
204         void test_int_common()
205         {
206             typedef typename OrdList::value_type    value_type;
207
208             value_type v1( 10, 50 );
209             value_type v2( 5, 25  );
210             value_type v3( 20, 100 );
211             {
212                 OrdList l;
213                 CPPUNIT_ASSERT( l.empty() );
214
215                 CPPUNIT_ASSERT( l.insert( v1 ))     ;   // true
216                 CPPUNIT_ASSERT( l.find( v1.key() ));
217
218                 CPPUNIT_ASSERT( v1.s.nFindCall == 0 );
219                 CPPUNIT_ASSERT( l.find( v1.key(), find_functor() ));
220                 CPPUNIT_ASSERT( v1.s.nFindCall == 1 );
221
222                 CPPUNIT_ASSERT( !l.find( v2.key() ));
223                 CPPUNIT_ASSERT( !l.find_with( v3.key(), less<value_type>() ));
224                 CPPUNIT_ASSERT( !l.empty() );
225
226                 CPPUNIT_ASSERT( !l.insert( v1 ))    ;   // assertion "is_empty" is not raised since pNext is nullptr
227
228                 {
229                     value_type v( v1 );
230                     CPPUNIT_ASSERT( !l.insert( v )) ;   // false
231                 }
232
233                 std::pair<bool, bool> ret = l.ensure( v2, ensure_functor() );
234                 CPPUNIT_ASSERT( ret.first );
235                 CPPUNIT_ASSERT( ret.second );
236                 CPPUNIT_ASSERT( v2.s.nEnsureNewCall == 1 );
237                 CPPUNIT_ASSERT( v2.s.nEnsureExistsCall == 0 );
238
239                 //CPPUNIT_ASSERT( !l.insert( v2 ))    ;   // assertion "is_empty"
240
241                 CPPUNIT_ASSERT( l.find_with( v1.key(), less<value_type>() )) ;   // true
242
243                 CPPUNIT_ASSERT( v1.s.nFindCall == 1 );
244                 CPPUNIT_ASSERT( l.find_with( v1.key(), less<value_type>(), find_functor() ));
245                 CPPUNIT_ASSERT( v1.s.nFindCall == 2 );
246
247                 CPPUNIT_ASSERT( l.find( v2.key() ));
248
249                 CPPUNIT_ASSERT( v2.s.nFindCall == 0 );
250                 CPPUNIT_ASSERT( l.find( v2.key(), find_functor() ));
251                 CPPUNIT_ASSERT( v2.s.nFindCall == 1 );
252
253                 CPPUNIT_ASSERT( !l.find( v3.key() ));
254
255                 {
256                     CPPUNIT_ASSERT( v2.s.nEnsureExistsCall == 0 );
257                     CPPUNIT_ASSERT( v2.s.nEnsureNewCall == 1 );
258
259                     value_type v( v2 );
260                     ret = l.ensure( v, ensure_functor() );
261
262                     CPPUNIT_ASSERT( ret.first );
263                     CPPUNIT_ASSERT( !ret.second );
264                     CPPUNIT_ASSERT( v2.s.nEnsureExistsCall == 1 );
265                     CPPUNIT_ASSERT( v2.s.nEnsureNewCall == 1 );
266                     CPPUNIT_ASSERT( v.s.nEnsureExistsCall == 0 );
267                     CPPUNIT_ASSERT( v.s.nEnsureNewCall == 0 );
268                 }
269
270                 CPPUNIT_ASSERT( !l.empty() );
271
272                 CPPUNIT_ASSERT( l.insert( v3 ))     ;   // true
273                 CPPUNIT_ASSERT( l.find( v3.key() ));
274
275                 CPPUNIT_ASSERT( v3.s.nFindCall == 0 );
276                 CPPUNIT_ASSERT( l.find( v3.key(), find_functor() ));
277                 CPPUNIT_ASSERT( v3.s.nFindCall == 1 );
278
279                 CPPUNIT_ASSERT( l.unlink( v2 ) );
280                 CPPUNIT_ASSERT( l.find( v1.key() )) ;   // true
281                 CPPUNIT_ASSERT( !l.find( v2.key() )) ;   // true
282                 CPPUNIT_ASSERT( l.find( v3.key() )) ;   // true
283                 CPPUNIT_ASSERT( !l.empty() );
284                 CPPUNIT_ASSERT( !l.unlink( v2 ) );
285
286                 {
287                     // v1 key is in the list but v NODE is not in the list
288                     value_type v( v1 );
289                     CPPUNIT_ASSERT( !l.unlink( v ) );
290                 }
291
292                 CPPUNIT_ASSERT( l.unlink( v1 ) );
293                 CPPUNIT_ASSERT( !l.unlink( v1 ) );
294                 CPPUNIT_ASSERT( !l.find( v1.key() ));
295                 CPPUNIT_ASSERT( !l.find( v2.key() ));
296                 CPPUNIT_ASSERT( l.find( v3.key() ));
297                 CPPUNIT_ASSERT( !l.empty() );
298                 CPPUNIT_ASSERT( !l.unlink( v1 ) );
299                 CPPUNIT_ASSERT( !l.unlink( v2 ) );
300
301                 CPPUNIT_ASSERT( l.unlink( v3 ) );
302                 CPPUNIT_ASSERT( !l.find_with( v1.key(), less<value_type>() ));
303                 CPPUNIT_ASSERT( !l.find_with( v2.key(), less<value_type>(), find_functor() ));
304                 CPPUNIT_ASSERT( !l.find( v3.key(), find_functor() ));
305                 CPPUNIT_ASSERT( l.empty() );
306                 CPPUNIT_ASSERT( !l.unlink( v1 ) );
307                 CPPUNIT_ASSERT( !l.unlink( v2 ) );
308                 CPPUNIT_ASSERT( !l.unlink( v3 ) );
309
310                 // Apply retired pointer to clean links
311                 OrdList::gc::force_dispose();
312
313                 stat s( v3.s );
314                 ret = l.ensure( v3, ensure_functor() );
315                 CPPUNIT_ASSERT( ret.first );
316                 CPPUNIT_ASSERT( ret.second );
317                 CPPUNIT_ASSERT( v3.s.nEnsureNewCall == s.nEnsureNewCall + 1);
318                 CPPUNIT_ASSERT( v3.s.nEnsureExistsCall == s.nEnsureExistsCall );
319                 CPPUNIT_ASSERT( !l.empty() );
320
321                 s = v2.s;
322                 ret = l.ensure( v2, ensure_functor() );
323                 CPPUNIT_ASSERT( ret.first );
324                 CPPUNIT_ASSERT( ret.second );
325                 CPPUNIT_ASSERT( v2.s.nEnsureNewCall == s.nEnsureNewCall + 1);
326                 CPPUNIT_ASSERT( v2.s.nEnsureExistsCall == s.nEnsureExistsCall );
327                 CPPUNIT_ASSERT( !l.empty() );
328
329                 s = v1.s;
330                 ret = l.ensure( v1, ensure_functor() );
331                 CPPUNIT_ASSERT( ret.first );
332                 CPPUNIT_ASSERT( ret.second );
333                 CPPUNIT_ASSERT( v1.s.nEnsureNewCall == s.nEnsureNewCall + 1);
334                 CPPUNIT_ASSERT( v1.s.nEnsureExistsCall == s.nEnsureExistsCall );
335                 CPPUNIT_ASSERT( !l.empty() );
336
337                 // Erase test
338                 CPPUNIT_ASSERT( v1.s.nEraseCall == 0 );
339                 CPPUNIT_ASSERT( l.erase( v1.key(), erase_functor()) );
340                 CPPUNIT_ASSERT( v1.s.nEraseCall == 1 );
341                 //CPPUNIT_ASSERT( v1.s.nDisposeCount == 0 );
342                 CPPUNIT_ASSERT( !l.empty() );
343
344                 CPPUNIT_ASSERT( l.erase_with( v2.key(), less<value_type>() ) );
345                 CPPUNIT_ASSERT( !l.erase( v2.key()));
346                 //CPPUNIT_ASSERT( v2.s.nDisposeCount == 0 );
347                 CPPUNIT_ASSERT( !l.empty() );
348
349                 CPPUNIT_ASSERT( v2.s.nEraseCall == 0 );
350                 CPPUNIT_ASSERT( !l.erase( v2, erase_functor() ));
351                 CPPUNIT_ASSERT( v2.s.nEraseCall == 0 );
352                 CPPUNIT_ASSERT( !l.erase( v1 ));
353                 //CPPUNIT_ASSERT( v2.s.nDisposeCount == 0 );
354                 CPPUNIT_ASSERT( !l.empty() );
355
356                 CPPUNIT_ASSERT( v3.s.nEraseCall == 0 );
357                 CPPUNIT_ASSERT( l.erase_with( v3, less<value_type>(), erase_functor() ));
358                 CPPUNIT_ASSERT( v3.s.nEraseCall == 1 );
359                 //CPPUNIT_ASSERT( v3.s.nDisposeCount == 0 );
360                 CPPUNIT_ASSERT( l.empty() );
361
362                 // Apply retired pointer to clean links
363                 OrdList::gc::force_dispose();
364
365                 // Unlink test
366                 CPPUNIT_ASSERT( l.insert( v1 ));
367                 CPPUNIT_ASSERT( l.insert( v3 ));
368                 CPPUNIT_ASSERT( !l.empty() );
369                 CPPUNIT_ASSERT( !l.unlink( v2 ));
370                 CPPUNIT_ASSERT( l.unlink( v1 ));
371                 CPPUNIT_ASSERT( !l.unlink( v1 ));
372                 CPPUNIT_ASSERT( l.unlink( v3 ));
373                 CPPUNIT_ASSERT( !l.unlink( v3 ));
374                 CPPUNIT_ASSERT( l.empty() );
375
376                 // Apply retired pointer
377                 OrdList::gc::force_dispose();
378                 CPPUNIT_ASSERT( v1.s.nDisposeCount == 3 );
379                 CPPUNIT_ASSERT( v2.s.nDisposeCount == 2 );
380                 CPPUNIT_ASSERT( v3.s.nDisposeCount == 3 );
381
382                 // Destructor test (call disposer)
383                 CPPUNIT_ASSERT( l.insert( v1 ));
384                 CPPUNIT_ASSERT( l.insert( v3 ));
385                 CPPUNIT_ASSERT( l.insert( v2 ));
386
387                 // Iterator test
388                 // begin/end
389                 {
390                     typename OrdList::iterator it = l.begin();
391                     CPPUNIT_ASSERT( it != l.end() );
392                     CPPUNIT_ASSERT( it->nKey == v2.nKey );
393                     CPPUNIT_ASSERT( it->nVal == v2.nVal );
394                     CPPUNIT_ASSERT( ++it != l.end() );
395                     CPPUNIT_ASSERT( it->nKey == v1.nKey );
396                     CPPUNIT_ASSERT( it->nVal == v1.nVal );
397                     CPPUNIT_ASSERT( ++it != l.end() );
398                     CPPUNIT_ASSERT( it->nKey == v3.nKey );
399                     CPPUNIT_ASSERT( it->nVal == v3.nVal );
400                     CPPUNIT_ASSERT( ++it == l.end() );
401                 }
402
403                 // cbegin/cend
404                 {
405                     typename OrdList::const_iterator it = l.cbegin();
406                     CPPUNIT_ASSERT( it != l.cend() );
407                     CPPUNIT_ASSERT( it->nKey == v2.nKey );
408                     CPPUNIT_ASSERT( it->nVal == v2.nVal );
409                     CPPUNIT_ASSERT( ++it != l.cend() );
410                     CPPUNIT_ASSERT( it->nKey == v1.nKey );
411                     CPPUNIT_ASSERT( it->nVal == v1.nVal );
412                     CPPUNIT_ASSERT( ++it != l.cend() );
413                     CPPUNIT_ASSERT( it->nKey == v3.nKey );
414                     CPPUNIT_ASSERT( it->nVal == v3.nVal );
415                     CPPUNIT_ASSERT( ++it == l.cend() );
416                 }
417
418                 // const begin/end
419                 {
420                     OrdList const & lref = l;
421                     typename OrdList::const_iterator it = lref.begin();
422                     CPPUNIT_ASSERT( it != l.end() );
423                     CPPUNIT_ASSERT( it->nKey == v2.nKey );
424                     CPPUNIT_ASSERT( it->nVal == v2.nVal );
425                     CPPUNIT_ASSERT( ++it != lref.end() );
426                     CPPUNIT_ASSERT( it->nKey == v1.nKey );
427                     CPPUNIT_ASSERT( it->nVal == v1.nVal );
428                     CPPUNIT_ASSERT( ++it != l.end() );
429                     CPPUNIT_ASSERT( it->nKey == v3.nKey );
430                     CPPUNIT_ASSERT( it->nVal == v3.nVal );
431                     CPPUNIT_ASSERT( ++it == l.end() );
432                 }
433             }
434
435             // Apply retired pointer
436             OrdList::gc::force_dispose();
437
438             CPPUNIT_ASSERT( v1.s.nDisposeCount == 4 );
439             CPPUNIT_ASSERT( v2.s.nDisposeCount == 3 );
440             CPPUNIT_ASSERT( v3.s.nDisposeCount == 4 );
441         }
442
443         template <class OrdList>
444         void test_int()
445         {
446             test_int_common<OrdList>();
447
448             OrdList l;
449             typename OrdList::guarded_ptr gp;
450
451             static int const nLimit = 20;
452             typename OrdList::value_type arrItem[nLimit];
453
454
455             {
456                 int a[nLimit];
457                 for (int i = 0; i < nLimit; ++i)
458                     a[i]=i;
459                 std::random_shuffle( a, a + nLimit );
460
461                 for (int i = 0; i < nLimit; ++i) {
462                     arrItem[i].nKey = a[i];
463                     arrItem[i].nVal = a[i] * 2;
464                 }
465
466                 // extract/get
467                 for ( int i = 0; i < nLimit; ++i )
468                     CPPUNIT_ASSERT( l.insert( arrItem[i] ) );
469
470                 for ( int i=0; i < nLimit; ++i ) {
471                     CPPUNIT_ASSERT( l.get( gp, arrItem[i].nKey ));
472                     CPPUNIT_ASSERT( !gp.empty());
473                     CPPUNIT_ASSERT( gp->nKey == arrItem[i].nKey );
474                     CPPUNIT_ASSERT( gp->nVal == arrItem[i].nVal );
475                     gp.release();
476
477                     CPPUNIT_ASSERT( l.extract( gp, arrItem[i].nKey ));
478                     CPPUNIT_ASSERT( !gp.empty());
479                     CPPUNIT_ASSERT( gp->nKey == arrItem[i].nKey );
480                     CPPUNIT_ASSERT( gp->nVal == arrItem[i].nVal );
481                     gp.release();
482
483                     CPPUNIT_ASSERT( !l.get( gp, arrItem[i].nKey ));
484                     CPPUNIT_ASSERT( gp.empty());
485                     CPPUNIT_ASSERT( !l.extract( gp, arrItem[i].nKey ));
486                     CPPUNIT_ASSERT( gp.empty());
487                 }
488                 CPPUNIT_ASSERT( l.empty() );
489                 CPPUNIT_ASSERT( !l.get( gp, nLimit/2 ));
490                 CPPUNIT_ASSERT( gp.empty());
491                 CPPUNIT_ASSERT( !l.extract( gp, nLimit/2 ));
492                 CPPUNIT_ASSERT( gp.empty());
493
494                 // Apply retired pointer
495                 OrdList::gc::force_dispose();
496
497                 // extract_with/get_with
498                 for ( int i = 0; i < nLimit; ++i )
499                     CPPUNIT_ASSERT( l.insert( arrItem[i] ) );
500
501                 for ( int i=0; i < nLimit; ++i ) {
502                     other_item itm( arrItem[i].nKey );
503                     CPPUNIT_ASSERT( l.get_with( gp, itm, other_less() ));
504                     CPPUNIT_ASSERT( !gp.empty());
505                     CPPUNIT_ASSERT( gp->nKey == arrItem[i].nKey );
506                     CPPUNIT_ASSERT( gp->nVal == arrItem[i].nVal );
507                     gp.release();
508
509                     CPPUNIT_ASSERT( l.extract_with( gp, itm, other_less() ));
510                     CPPUNIT_ASSERT( !gp.empty());
511                     CPPUNIT_ASSERT( gp->nKey == arrItem[i].nKey );
512                     CPPUNIT_ASSERT( gp->nVal == arrItem[i].nVal );
513                     gp.release();
514
515                     CPPUNIT_ASSERT( !l.get_with( gp, itm, other_less() ));
516                     CPPUNIT_ASSERT( gp.empty());
517                     CPPUNIT_ASSERT( !l.extract_with( gp, itm, other_less() ));
518                     CPPUNIT_ASSERT( gp.empty());
519                 }
520                 CPPUNIT_ASSERT( l.empty() );
521                 CPPUNIT_ASSERT( !l.get_with( gp, other_item(nLimit/2), other_less() ));
522                 CPPUNIT_ASSERT( gp.empty());
523                 CPPUNIT_ASSERT( !l.extract_with( gp, other_item(nLimit/2), other_less() ));
524                 CPPUNIT_ASSERT( gp.empty());
525
526                 // Apply retired pointer
527                 OrdList::gc::force_dispose();
528
529                 for ( int i=0; i < nLimit; i++ ) {
530                     CPPUNIT_ASSERT( arrItem[i].s.nDisposeCount == 2 );
531                 }
532             }
533         }
534
535         template <class OrdList>
536         void test_rcu_int()
537         {
538             test_int_common<OrdList>();
539
540             OrdList l;
541             static int const nLimit = 20;
542             typename OrdList::value_type arrItem[nLimit];
543
544             typedef typename OrdList::rcu_lock rcu_lock;
545             typedef typename OrdList::value_type value_type;
546             typedef typename OrdList::gc rcu_type;
547
548             {
549                 int a[nLimit];
550                 for (int i = 0; i < nLimit; ++i)
551                     a[i]=i;
552                 std::random_shuffle( a, a + nLimit );
553
554                 for (int i = 0; i < nLimit; ++i) {
555                     arrItem[i].nKey = a[i];
556                     arrItem[i].nVal = a[i] * 2;
557                 }
558
559                 // extract/get
560                 for ( int i = 0; i < nLimit; ++i )
561                     CPPUNIT_ASSERT( l.insert( arrItem[i] ) );
562
563                 typename OrdList::exempt_ptr ep;
564
565                 for ( int i = 0; i < nLimit; ++i ) {
566                     {
567                         rcu_lock lock;
568                         value_type * pGet = l.get( a[i] );
569                         CPPUNIT_ASSERT( pGet != nullptr );
570                         CPPUNIT_CHECK( pGet->nKey == a[i] );
571                         CPPUNIT_CHECK( pGet->nVal == a[i] * 2 );
572
573                         CPPUNIT_ASSERT( l.extract( ep, a[i] ));
574                         CPPUNIT_ASSERT( !ep.empty() );
575                         CPPUNIT_CHECK( ep->nKey == a[i] );
576                         CPPUNIT_CHECK( (*ep).nVal == a[i] * 2 );
577                     }
578                     ep.release();
579                     {
580                         rcu_lock lock;
581                         CPPUNIT_CHECK( l.get( a[i] ) == nullptr );
582                         CPPUNIT_CHECK( !l.extract( ep, a[i] ));
583                         CPPUNIT_CHECK( ep.empty() );
584                     }
585                 }
586                 CPPUNIT_ASSERT( l.empty() );
587
588                 {
589                     rcu_lock lock;
590                     CPPUNIT_CHECK( l.get( a[0] ) == nullptr );
591                     CPPUNIT_CHECK( !l.extract( ep, a[0] ) );
592                     CPPUNIT_CHECK( ep.empty() );
593                 }
594                 // Apply retired pointer
595                 OrdList::gc::force_dispose();
596
597                 // extract_with/get_with
598                 for ( int i = 0; i < nLimit; ++i ) {
599                     CPPUNIT_ASSERT( l.insert( arrItem[i] ) );
600                 }
601
602                 for ( int i = 0; i < nLimit; ++i ) {
603                     other_item itm( a[i] );
604                     {
605                         rcu_lock lock;
606                         value_type * pGet = l.get_with( itm, other_less() );
607                         CPPUNIT_ASSERT( pGet != nullptr );
608                         CPPUNIT_CHECK( pGet->nKey == a[i] );
609                         CPPUNIT_CHECK( pGet->nVal == a[i] * 2 );
610
611                         CPPUNIT_ASSERT( l.extract_with( ep, itm, other_less() ));
612                         CPPUNIT_ASSERT( !ep.empty() );
613                         CPPUNIT_CHECK( ep->nKey == a[i] );
614                         CPPUNIT_CHECK( ep->nVal == a[i] * 2 );
615                     }
616                     ep.release();
617                     {
618                         rcu_lock lock;
619                         CPPUNIT_CHECK( l.get_with( itm, other_less() ) == nullptr );
620                         CPPUNIT_CHECK( !l.extract_with( ep, itm, other_less() ));
621                         CPPUNIT_CHECK( ep.empty() );
622                     }
623                 }
624                 CPPUNIT_ASSERT( l.empty() );
625
626                 {
627                     rcu_lock lock;
628                     CPPUNIT_CHECK( l.get_with( other_item( 0 ), other_less() ) == nullptr );
629                     CPPUNIT_CHECK( !l.extract_with( ep, other_item(0), other_less() ));
630                     CPPUNIT_CHECK( ep.empty() );
631                 }
632                 // Apply retired pointer
633                 OrdList::gc::force_dispose();
634             }
635         }
636
637         template <class OrdList>
638         void test_nogc_int()
639         {
640             typedef typename OrdList::value_type    value_type;
641             {
642                 value_type v1( 10, 50 );
643                 value_type v2( 5, 25  );
644                 value_type v3( 20, 100 );
645                 {
646                     OrdList l;
647                     CPPUNIT_ASSERT( l.empty() );
648
649                     CPPUNIT_ASSERT( l.insert( v1 ))     ;   // true
650                     CPPUNIT_ASSERT( l.find( v1.key() ) == &v1 );
651
652                     CPPUNIT_ASSERT( v1.s.nFindCall == 0 );
653                     CPPUNIT_ASSERT( l.find( v1.key(), find_functor() ));
654                     CPPUNIT_ASSERT( v1.s.nFindCall == 1 );
655
656                     CPPUNIT_ASSERT( l.find_with( v2.key(), less<value_type>() ) == nullptr );
657                     CPPUNIT_ASSERT( !l.find_with( v3.key(), less<value_type>(), find_functor() ));
658                     CPPUNIT_ASSERT( !l.empty() );
659
660                     CPPUNIT_ASSERT( !l.insert( v1 ))    ;   // assertion "is_empty" is not raised since pNext is nullptr
661
662                     {
663                         value_type v( v1 );
664                         CPPUNIT_ASSERT( !l.insert( v )) ;   // false
665                     }
666
667                     std::pair<bool, bool> ret = l.ensure( v2, ensure_functor() );
668                     CPPUNIT_ASSERT( ret.first );
669                     CPPUNIT_ASSERT( ret.second );
670                     CPPUNIT_ASSERT( v2.s.nEnsureNewCall == 1 );
671                     CPPUNIT_ASSERT( v2.s.nEnsureExistsCall == 0 );
672
673                     //CPPUNIT_ASSERT( !l.insert( v2 ))    ;   // assertion "is_empty"
674
675                     CPPUNIT_ASSERT( l.find( v1.key() ) == &v1 ) ;   // true
676
677                     CPPUNIT_ASSERT( v1.s.nFindCall == 1 );
678                     CPPUNIT_ASSERT( l.find( v1.key(), find_functor() ));
679                     CPPUNIT_ASSERT( v1.s.nFindCall == 2 );
680
681                     CPPUNIT_ASSERT( l.find( v2.key() ) == &v2 );
682
683                     CPPUNIT_ASSERT( v2.s.nFindCall == 0 );
684                     CPPUNIT_ASSERT( l.find( v2.key(), find_functor() ));
685                     CPPUNIT_ASSERT( v2.s.nFindCall == 1 );
686
687                     CPPUNIT_ASSERT( !l.find( v3.key() ));
688
689                     {
690                         value_type v( v2 );
691                         ret = l.ensure( v, ensure_functor() );
692
693                         CPPUNIT_ASSERT( ret.first );
694                         CPPUNIT_ASSERT( !ret.second );
695                         CPPUNIT_ASSERT( v2.s.nEnsureExistsCall == 1 );
696                         CPPUNIT_ASSERT( v.s.nEnsureExistsCall == 0 && v.s.nEnsureNewCall == 0 );
697                     }
698
699                     CPPUNIT_ASSERT( !l.empty() );
700
701                     CPPUNIT_ASSERT( l.insert( v3 ))     ;   // true
702                     CPPUNIT_ASSERT( l.find( v3.key() ) == &v3 );
703
704                     CPPUNIT_ASSERT( v3.s.nFindCall == 0 );
705                     CPPUNIT_ASSERT( l.find( v3.key(), find_functor() ));
706                     CPPUNIT_ASSERT( v3.s.nFindCall == 1 );
707
708                     {
709                         typename OrdList::iterator it = l.begin();
710                         CPPUNIT_ASSERT( it != l.end() );
711                         CPPUNIT_ASSERT( it->nKey == v2.nKey );
712                         CPPUNIT_ASSERT( it->nVal == v2.nVal );
713                         CPPUNIT_ASSERT( ++it != l.end() );
714                         CPPUNIT_ASSERT( it->nKey == v1.nKey );
715                         CPPUNIT_ASSERT( it->nVal == v1.nVal );
716                         CPPUNIT_ASSERT( it++ != l.end() );
717                         CPPUNIT_ASSERT( it->nKey == v3.nKey );
718                         CPPUNIT_ASSERT( it->nVal == v3.nVal );
719                         CPPUNIT_ASSERT( it++ != l.end() );
720                         CPPUNIT_ASSERT( it == l.end() );
721                     }
722
723                     {
724                         OrdList const & lref = l;
725                         typename OrdList::const_iterator it = lref.begin();
726                         CPPUNIT_ASSERT( it != l.end() );
727                         CPPUNIT_ASSERT( it->nKey == v2.nKey );
728                         CPPUNIT_ASSERT( it->nVal == v2.nVal );
729                         CPPUNIT_ASSERT( ++it != lref.end() );
730                         CPPUNIT_ASSERT( it->nKey == v1.nKey );
731                         CPPUNIT_ASSERT( it->nVal == v1.nVal );
732                         CPPUNIT_ASSERT( it++ != l.end() );
733                         CPPUNIT_ASSERT( it->nKey == v3.nKey );
734                         CPPUNIT_ASSERT( it->nVal == v3.nVal );
735                         CPPUNIT_ASSERT( it++ != lref.end() );
736                         CPPUNIT_ASSERT( it == l.end() );
737                     }
738                 }
739
740                 // Disposer called on list destruction
741                 CPPUNIT_ASSERT( v1.s.nDisposeCount == 1 );
742                 CPPUNIT_ASSERT( v2.s.nDisposeCount == 1 );
743                 CPPUNIT_ASSERT( v3.s.nDisposeCount == 1 );
744             }
745         }
746
747         void HP_base_cmp();
748         void HP_base_less();
749         void HP_base_cmpmix();
750         void HP_base_ic();
751         void HP_member_cmp();
752         void HP_member_less();
753         void HP_member_cmpmix();
754         void HP_member_ic();
755
756         void PTB_base_cmp();
757         void PTB_base_less();
758         void PTB_base_cmpmix();
759         void PTB_base_ic();
760         void PTB_member_cmp();
761         void PTB_member_less();
762         void PTB_member_cmpmix();
763         void PTB_member_ic();
764
765         void HRC_base_cmp();
766         void HRC_base_less();
767         void HRC_base_cmpmix();
768         void HRC_base_ic();
769
770         void RCU_GPI_base_cmp();
771         void RCU_GPI_base_less();
772         void RCU_GPI_base_cmpmix();
773         void RCU_GPI_base_ic();
774         void RCU_GPI_member_cmp();
775         void RCU_GPI_member_less();
776         void RCU_GPI_member_cmpmix();
777         void RCU_GPI_member_ic();
778
779         void RCU_GPB_base_cmp();
780         void RCU_GPB_base_less();
781         void RCU_GPB_base_cmpmix();
782         void RCU_GPB_base_ic();
783         void RCU_GPB_member_cmp();
784         void RCU_GPB_member_less();
785         void RCU_GPB_member_cmpmix();
786         void RCU_GPB_member_ic();
787
788         void RCU_GPT_base_cmp();
789         void RCU_GPT_base_less();
790         void RCU_GPT_base_cmpmix();
791         void RCU_GPT_base_ic();
792         void RCU_GPT_member_cmp();
793         void RCU_GPT_member_less();
794         void RCU_GPT_member_cmpmix();
795         void RCU_GPT_member_ic();
796
797         void RCU_SHB_base_cmp();
798         void RCU_SHB_base_less();
799         void RCU_SHB_base_cmpmix();
800         void RCU_SHB_base_ic();
801         void RCU_SHB_member_cmp();
802         void RCU_SHB_member_less();
803         void RCU_SHB_member_cmpmix();
804         void RCU_SHB_member_ic();
805
806         void RCU_SHT_base_cmp();
807         void RCU_SHT_base_less();
808         void RCU_SHT_base_cmpmix();
809         void RCU_SHT_base_ic();
810         void RCU_SHT_member_cmp();
811         void RCU_SHT_member_less();
812         void RCU_SHT_member_cmpmix();
813         void RCU_SHT_member_ic();
814
815         void nogc_base_cmp();
816         void nogc_base_less();
817         void nogc_base_cmpmix();
818         void nogc_base_ic();
819         void nogc_member_cmp();
820         void nogc_member_less();
821         void nogc_member_cmpmix();
822         void nogc_member_ic();
823
824
825         CPPUNIT_TEST_SUITE(IntrusiveMichaelListHeaderTest)
826             CPPUNIT_TEST(HP_base_cmp)
827             CPPUNIT_TEST(HP_base_less)
828             CPPUNIT_TEST(HP_base_cmpmix)
829             CPPUNIT_TEST(HP_base_ic)
830             CPPUNIT_TEST(HP_member_cmp)
831             CPPUNIT_TEST(HP_member_less)
832             CPPUNIT_TEST(HP_member_cmpmix)
833             CPPUNIT_TEST(HP_member_ic)
834
835             CPPUNIT_TEST(PTB_base_cmp)
836             CPPUNIT_TEST(PTB_base_less)
837             CPPUNIT_TEST(PTB_base_cmpmix)
838             CPPUNIT_TEST(PTB_base_ic)
839             CPPUNIT_TEST(PTB_member_cmp)
840             CPPUNIT_TEST(PTB_member_less)
841             CPPUNIT_TEST(PTB_member_cmpmix)
842             CPPUNIT_TEST(PTB_member_ic)
843
844             CPPUNIT_TEST(HRC_base_cmp)
845             CPPUNIT_TEST(HRC_base_less)
846             CPPUNIT_TEST(HRC_base_cmpmix)
847             CPPUNIT_TEST(HRC_base_ic)
848
849             CPPUNIT_TEST(RCU_GPI_base_cmp)
850             CPPUNIT_TEST(RCU_GPI_base_less)
851             CPPUNIT_TEST(RCU_GPI_base_cmpmix)
852             CPPUNIT_TEST(RCU_GPI_base_ic)
853             CPPUNIT_TEST(RCU_GPI_member_cmp)
854             CPPUNIT_TEST(RCU_GPI_member_less)
855             CPPUNIT_TEST(RCU_GPI_member_cmpmix)
856             CPPUNIT_TEST(RCU_GPI_member_ic)
857
858             CPPUNIT_TEST(RCU_GPB_base_cmp)
859             CPPUNIT_TEST(RCU_GPB_base_less)
860             CPPUNIT_TEST(RCU_GPB_base_cmpmix)
861             CPPUNIT_TEST(RCU_GPB_base_ic)
862             CPPUNIT_TEST(RCU_GPB_member_cmp)
863             CPPUNIT_TEST(RCU_GPB_member_less)
864             CPPUNIT_TEST(RCU_GPB_member_cmpmix)
865             CPPUNIT_TEST(RCU_GPB_member_ic)
866
867             CPPUNIT_TEST(RCU_GPT_base_cmp)
868             CPPUNIT_TEST(RCU_GPT_base_less)
869             CPPUNIT_TEST(RCU_GPT_base_cmpmix)
870             CPPUNIT_TEST(RCU_GPT_base_ic)
871             CPPUNIT_TEST(RCU_GPT_member_cmp)
872             CPPUNIT_TEST(RCU_GPT_member_less)
873             CPPUNIT_TEST(RCU_GPT_member_cmpmix)
874             CPPUNIT_TEST(RCU_GPT_member_ic)
875
876             CPPUNIT_TEST(nogc_base_cmp)
877             CPPUNIT_TEST(nogc_base_less)
878             CPPUNIT_TEST(nogc_base_cmpmix)
879             CPPUNIT_TEST(nogc_base_ic)
880             CPPUNIT_TEST(nogc_member_cmp)
881             CPPUNIT_TEST(nogc_member_less)
882             CPPUNIT_TEST(nogc_member_cmpmix)
883             CPPUNIT_TEST(nogc_member_ic)
884
885         CPPUNIT_TEST_SUITE_END()
886     };
887 }   // namespace ordlist