3 #include "cppunit/cppunit_proxy.h"
4 #include <cds/container/details/michael_list_base.h>
7 namespace cc = cds::container;
8 namespace co = cds::container::opt;
10 class MichaelListTestHeader: public CppUnitMini::TestCase
14 int nEnsureExistsCall;
37 item(int key, int val)
58 bool operator ()(const T& v1, const T& v2 ) const
60 return v1.key() < v2.key();
64 bool operator ()(const T& v1, const Q& v2 ) const
70 bool operator ()(const Q& v1, const T& v2 ) const
78 int operator ()(const T& v1, const T& v2 ) const
80 if ( v1.key() < v2.key() )
82 return v1.key() > v2.key() ? 1 : 0;
86 int operator ()(const T& v1, const Q& v2 ) const
90 return v1.key() > v2 ? 1 : 0;
94 int operator ()(const Q& v1, const T& v2 ) const
98 return v1 > v2.key() ? 1 : 0;
102 struct insert_functor {
103 void operator ()( item& i )
105 i.nVal = i.nKey * 1033;
108 struct dummy_insert_functor {
109 void operator ()( item& /*i*/ )
111 // This functor should not be called
112 TestCase::current_test()->error( "CPPUNIT_ASSERT", "dummy_insert_functor should not be called", __FILE__, __LINE__ );
116 struct erase_functor {
117 unsigned int nEraseCall;
123 void operator()( item const& /*i*/)
129 static void insert_function( item& i )
131 i.nVal = i.nKey * 1024;
133 static void dummy_insert_function( item& /*i*/ )
135 // This function should not be called
136 TestCase::current_test()->error( "CPPUNIT_ASSERT", "dummy_insert_function should not be called", __FILE__, __LINE__ );
141 unsigned int m_nMultiplier;
143 check_value( unsigned int nMultiplier )
144 : m_nMultiplier( nMultiplier )
147 check_value( const check_value& s )
148 : m_nMultiplier( s.m_nMultiplier )
151 void operator()( item& i, int )
153 CPPUNIT_ASSERT_CURRENT( int(i.nKey * m_nMultiplier) == i.nVal );
157 struct check_exact_value {
160 check_exact_value( int nExpected )
161 : m_nExpected( nExpected )
164 check_exact_value( check_exact_value const& s)
165 : m_nExpected( s.m_nExpected )
168 void operator()( item& i, int )
170 CPPUNIT_ASSERT_CURRENT( i.nVal == m_nExpected );
174 struct dummy_check_value {
175 void operator()( item& /*i*/, int )
177 // This functor should not be called
178 TestCase::current_test()->error( "CPPUNIT_ASSERT", "dummy_check_value should not be called", __FILE__, __LINE__ );
182 struct ensure_functor {
183 void operator()( bool /*bNew*/, item& i, int n )
185 i.nVal = i.nKey * 1024;
189 static void ensure_func( bool /*bNew*/, item& i, int n )
208 template <typename T1, typename T2>
209 bool operator()( T1 const& t1, T2 const& t2 ) const
211 return t1.nKey < t2.nKey;
216 template <class OrdList>
217 void test_with( OrdList& l )
219 typedef typename OrdList::value_type value_type;
221 // The list should be empty
222 CPPUNIT_ASSERT( l.empty() );
225 CPPUNIT_ASSERT( l.insert( 50 ) );
226 CPPUNIT_ASSERT( l.insert( item( 25 )) );
227 CPPUNIT_ASSERT( l.insert( item( 100 )) );
229 // insert failed - such key exists
230 CPPUNIT_ASSERT( !l.insert( 50 ) );
231 CPPUNIT_ASSERT( !l.insert( item( 100 )) );
235 // The list should not be empty
236 CPPUNIT_ASSERT( !l.empty() );
238 // and now the list is empty
239 CPPUNIT_ASSERT( l.empty() );
241 // Test insert with functor
243 CPPUNIT_ASSERT( l.insert( 100, insert_functor() ) );
247 CPPUNIT_ASSERT( l.insert( item( 25 ), std::ref( f ) ) );
248 CPPUNIT_ASSERT( !l.insert( item( 100 ), std::ref( f ) ) );
250 // Test insert with function
251 CPPUNIT_ASSERT( l.insert( 50, insert_function ));
252 CPPUNIT_ASSERT( !l.insert( 25, dummy_insert_function ));
253 CPPUNIT_ASSERT( !l.insert( 100, dummy_insert_functor() ));
255 // The list should not be empty
256 CPPUNIT_ASSERT( !l.empty() );
258 // Check inserted values
262 CPPUNIT_ASSERT( l.find( 100 ));
263 CPPUNIT_ASSERT( l.find( i, check_value(1033) ));
267 CPPUNIT_ASSERT( l.find_with( 25, lt<value_type>() ));
268 CPPUNIT_ASSERT( l.find_with( i, lt<value_type>(), std::ref( f ) ) );
271 CPPUNIT_ASSERT( l.find( 50 ));
272 CPPUNIT_ASSERT( l.find( i, check_value(1024) ));
275 CPPUNIT_ASSERT( !l.find_with( 10, lt<value_type>() ));
276 CPPUNIT_ASSERT( !l.find_with( i, lt<value_type>(), dummy_check_value() ));
278 CPPUNIT_ASSERT( !l.find( 75 ));
279 CPPUNIT_ASSERT( !l.find( i, dummy_check_value() ));
281 CPPUNIT_ASSERT( !l.find( 150 ));
282 CPPUNIT_ASSERT( !l.find( i, dummy_check_value() ));
285 // The list should not be empty
286 CPPUNIT_ASSERT( !l.empty() );
288 // and now the list is empty
289 CPPUNIT_ASSERT( l.empty() );
293 std::pair<bool, bool> ensureResult;
295 ensureResult = l.ensure( 100, ensure_functor() );
296 CPPUNIT_ASSERT( ensureResult.first );
297 CPPUNIT_ASSERT( ensureResult.second );
299 ensureResult = l.ensure( 200, std::ref( f ) );
300 CPPUNIT_ASSERT( ensureResult.first );
301 CPPUNIT_ASSERT( ensureResult.second );
303 ensureResult = l.ensure( 50, ensure_func );
304 CPPUNIT_ASSERT( ensureResult.first );
305 CPPUNIT_ASSERT( ensureResult.second );
309 CPPUNIT_ASSERT( l.find( i, check_value(1024) ));
311 CPPUNIT_ASSERT( l.find( i, check_value(1033) ));
313 CPPUNIT_ASSERT( l.find( i, check_value(1024) ));
315 // ensure existing key
316 ensureResult = l.ensure( 200, ensure_func );
317 CPPUNIT_ASSERT( ensureResult.first );
318 CPPUNIT_ASSERT( !ensureResult.second );
320 CPPUNIT_ASSERT( l.find( i, check_value(1033) ));
322 ensureResult = l.ensure( 50, ensure_functor() );
323 CPPUNIT_ASSERT( ensureResult.first );
324 CPPUNIT_ASSERT( !ensureResult.second );
326 CPPUNIT_ASSERT( l.find( i, check_value(1024) ));
329 // erase test (list: 50, 100, 200)
330 CPPUNIT_ASSERT( !l.empty() );
331 CPPUNIT_ASSERT( l.insert(160));
332 CPPUNIT_ASSERT( l.insert(250));
333 CPPUNIT_ASSERT( !l.empty() );
335 CPPUNIT_ASSERT( !l.erase( 150 ));
337 CPPUNIT_ASSERT( l.erase( 100 ));
338 CPPUNIT_ASSERT( !l.erase( 100 ));
340 CPPUNIT_ASSERT( l.erase_with( 200, lt<value_type>() ));
341 CPPUNIT_ASSERT( !l.erase_with( 200, lt<value_type>() ));
345 CPPUNIT_ASSERT( ef.nEraseCall == 0 );
346 CPPUNIT_ASSERT( l.erase_with( 160, lt<value_type>(), std::ref(ef) ));
347 CPPUNIT_ASSERT( ef.nEraseCall == 1 );
348 CPPUNIT_ASSERT( !l.erase_with( 160, lt<value_type>(), std::ref(ef) ));
349 CPPUNIT_ASSERT( ef.nEraseCall == 1 );
351 CPPUNIT_ASSERT( l.erase( 250, std::ref(ef) ));
352 CPPUNIT_ASSERT( ef.nEraseCall == 2 );
353 CPPUNIT_ASSERT( !l.erase( 250, std::ref(ef) ));
354 CPPUNIT_ASSERT( ef.nEraseCall == 2 );
357 CPPUNIT_ASSERT( l.erase( 50 ));
358 CPPUNIT_ASSERT( !l.erase( 50 ));
360 CPPUNIT_ASSERT( l.empty() );
364 CPPUNIT_ASSERT( l.empty() );
370 CPPUNIT_ASSERT( l.emplace( 501 ) );
371 CPPUNIT_ASSERT( l.emplace( 251, 152 ));
372 CPPUNIT_ASSERT( l.emplace( item( 1001 )) );
374 // insert failed - such key exists
375 CPPUNIT_ASSERT( !l.emplace( 501, 2 ) );
376 CPPUNIT_ASSERT( !l.emplace( 251, 10) );
379 CPPUNIT_ASSERT( l.find( i, check_exact_value(501*2) ));
381 CPPUNIT_ASSERT( l.find( i, check_exact_value(152) ));
383 CPPUNIT_ASSERT( l.find( i, check_exact_value(1001*2) ));
386 CPPUNIT_ASSERT( l.empty() );
392 for ( int i = 0; i < nCount; ++i )
393 CPPUNIT_ASSERT( l.insert(i) );
396 typename OrdList::iterator it( l.begin() );
397 typename OrdList::const_iterator cit( l.cbegin() );
398 CPPUNIT_CHECK( it == cit );
399 CPPUNIT_CHECK( it != l.end() );
400 CPPUNIT_CHECK( it != l.cend() );
401 CPPUNIT_CHECK( cit != l.end() );
402 CPPUNIT_CHECK( cit != l.cend() );
404 CPPUNIT_CHECK( it != cit );
405 CPPUNIT_CHECK( it != l.end() );
406 CPPUNIT_CHECK( it != l.cend() );
407 CPPUNIT_CHECK( cit != l.end() );
408 CPPUNIT_CHECK( cit != l.cend() );
410 CPPUNIT_CHECK( it == cit );
411 CPPUNIT_CHECK( it != l.end() );
412 CPPUNIT_CHECK( it != l.cend() );
413 CPPUNIT_CHECK( cit != l.end() );
414 CPPUNIT_CHECK( cit != l.cend() );
418 for ( typename OrdList::iterator it = l.begin(), itEnd = l.end(); it != itEnd; ++it, ++i ) {
420 CPPUNIT_ASSERT( it->nKey == i );
423 // Check that we have visited all items
424 for ( int i = 0; i < nCount; ++i )
425 CPPUNIT_ASSERT( l.find( i, check_value(2) ));
428 CPPUNIT_ASSERT( l.empty() );
431 for ( int i = 0; i < nCount; ++i )
432 CPPUNIT_ASSERT( l.insert(i) );
435 const OrdList& rl = l;
436 for ( typename OrdList::const_iterator it = rl.begin(), itEnd = rl.end(); it != itEnd; ++it, ++i ) {
437 // it->nVal = i * 2 ; // not!
438 CPPUNIT_ASSERT( it->nKey == i );
441 // Check that we have visited all items
442 for ( int i = 0; i < nCount; ++i )
443 CPPUNIT_ASSERT( l.find( i, check_value(2) ));
446 CPPUNIT_ASSERT( l.empty() );
450 template <typename OrdList>
453 typedef typename OrdList::guarded_ptr guarded_ptr;
454 typedef typename OrdList::value_type value_type;
459 static int const nLimit = 20;
461 for ( int i = 0; i < nLimit; i++ )
463 std::random_shuffle( arr, arr + nLimit );
466 for ( int i = 0; i < nLimit; ++i )
470 for ( int i = 0; i < nLimit; ++i ) {
473 CPPUNIT_ASSERT( l.get(gp, nKey));
474 CPPUNIT_ASSERT( !gp.empty());
475 CPPUNIT_CHECK( gp->nKey == nKey );
476 CPPUNIT_CHECK( gp->nVal == nKey * 2 );
479 CPPUNIT_ASSERT( l.extract(gp, nKey));
480 CPPUNIT_ASSERT( !gp.empty());
481 CPPUNIT_CHECK( gp->nKey == nKey );
482 CPPUNIT_CHECK( gp->nVal == nKey*2 );
485 CPPUNIT_CHECK( !l.get(gp, nKey));
486 CPPUNIT_CHECK( gp.empty());
487 CPPUNIT_CHECK( !l.extract( gp, nKey));
488 CPPUNIT_CHECK( gp.empty());
490 CPPUNIT_ASSERT( l.empty());
491 CPPUNIT_CHECK( !l.get(gp, arr[0]));
492 CPPUNIT_CHECK( gp.empty());
493 CPPUNIT_CHECK( !l.extract( gp, arr[0]));
494 CPPUNIT_CHECK( gp.empty());
497 // extract_with/get_with
498 for ( int i = 0; i < nLimit; ++i )
502 for ( int i = 0; i < nLimit; ++i ) {
504 other_item key( nKey );
506 CPPUNIT_ASSERT( l.get_with(gp, key, other_less()));
507 CPPUNIT_ASSERT( !gp.empty());
508 CPPUNIT_CHECK( gp->nKey == nKey );
509 CPPUNIT_CHECK( gp->nVal == nKey * 2 );
512 CPPUNIT_ASSERT( l.extract_with(gp, key, other_less()));
513 CPPUNIT_ASSERT( !gp.empty());
514 CPPUNIT_CHECK( gp->nKey == nKey );
515 CPPUNIT_CHECK( gp->nVal == nKey*2 );
518 CPPUNIT_CHECK( !l.get_with(gp, key, other_less()));
519 CPPUNIT_CHECK( gp.empty());
520 CPPUNIT_CHECK( !l.extract_with( gp, key, other_less()));
521 CPPUNIT_CHECK( gp.empty());
523 CPPUNIT_ASSERT( l.empty());
524 CPPUNIT_CHECK( !l.get_with(gp, other_item(arr[0]), other_less()));
525 CPPUNIT_CHECK( gp.empty());
526 CPPUNIT_CHECK( !l.extract_with( gp, other_item(arr[0]), other_less()));
527 CPPUNIT_CHECK( gp.empty());
531 template <typename OrdList>
537 static int const nLimit = 20;
539 typedef typename OrdList::rcu_lock rcu_lock;
540 typedef typename OrdList::value_type value_type;
541 typedef typename OrdList::gc rcu_type;
545 for (int i = 0; i < nLimit; ++i)
547 std::random_shuffle( a, a + nLimit );
550 for ( int i = 0; i < nLimit; ++i )
551 CPPUNIT_ASSERT( l.insert( a[i] ) );
553 typename OrdList::exempt_ptr ep;
555 for ( int i = 0; i < nLimit; ++i ) {
558 value_type * pGet = l.get( a[i] );
559 CPPUNIT_ASSERT( pGet != nullptr );
560 CPPUNIT_CHECK( pGet->nKey == a[i] );
561 CPPUNIT_CHECK( pGet->nVal == a[i] * 2 );
563 ep = l.extract( a[i] );
564 CPPUNIT_ASSERT( ep );
565 CPPUNIT_ASSERT( !ep.empty() );
566 CPPUNIT_CHECK( ep->nKey == a[i] );
567 CPPUNIT_CHECK( (*ep).nVal == a[i] * 2 );
572 CPPUNIT_CHECK( l.get( a[i] ) == nullptr );
573 ep = l.extract( a[i] );
574 CPPUNIT_CHECK( !ep );
575 CPPUNIT_CHECK( ep.empty() );
578 CPPUNIT_ASSERT( l.empty() );
582 CPPUNIT_CHECK( l.get( a[0] ) == nullptr );
583 CPPUNIT_CHECK( !l.extract( a[0] ) );
584 CPPUNIT_CHECK( ep.empty() );
587 // extract_with/get_with
588 for ( int i = 0; i < nLimit; ++i ) {
589 CPPUNIT_ASSERT( l.insert( a[i] ) );
592 for ( int i = 0; i < nLimit; ++i ) {
593 other_item itm( a[i] );
596 value_type * pGet = l.get_with( itm, other_less() );
597 CPPUNIT_ASSERT( pGet != nullptr );
598 CPPUNIT_CHECK( pGet->nKey == a[i] );
599 CPPUNIT_CHECK( pGet->nVal == a[i] * 2 );
601 ep = l.extract_with( itm, other_less() );
602 CPPUNIT_ASSERT( ep );
603 CPPUNIT_ASSERT( !ep.empty() );
604 CPPUNIT_CHECK( ep->nKey == a[i] );
605 CPPUNIT_CHECK( ep->nVal == a[i] * 2 );
610 CPPUNIT_CHECK( l.get_with( itm, other_less() ) == nullptr );
611 ep = l.extract_with( itm, other_less() );
612 CPPUNIT_CHECK( !ep );
613 CPPUNIT_CHECK( ep.empty() );
616 CPPUNIT_ASSERT( l.empty() );
620 CPPUNIT_CHECK( l.get_with( other_item( 0 ), other_less() ) == nullptr );
621 CPPUNIT_CHECK( !l.extract_with( other_item(0), other_less() ));
622 CPPUNIT_CHECK( ep.empty() );
628 template <class OrdList>
631 typedef OrdList list;
632 typedef typename list::value_type value_type;
633 typedef std::pair<typename list::iterator, bool> ensure_result;
635 typename list::iterator it;
638 CPPUNIT_ASSERT( l.empty() );
639 CPPUNIT_ASSERT( l.insert(50) != l.end() );
640 CPPUNIT_ASSERT( !l.empty() );
642 ensure_result eres = l.ensure( item(100, 33) );
643 CPPUNIT_ASSERT( eres.second );
644 CPPUNIT_ASSERT( eres.first != l.end() );
645 CPPUNIT_ASSERT( l.insert( item(150) ) != l.end() );
647 CPPUNIT_ASSERT( l.insert(100) == l.end() );
648 eres = l.ensure( item(50, 33) );
649 CPPUNIT_ASSERT( !eres.second );
650 CPPUNIT_ASSERT( eres.first->nVal == eres.first->nKey * 2 );
651 eres.first->nVal = 63;
654 CPPUNIT_ASSERT( it == l.end() );
657 CPPUNIT_ASSERT( it != l.end() );
658 CPPUNIT_ASSERT( it->nKey == 50 );
659 CPPUNIT_ASSERT( it->nVal == 63 );
661 it = l.find_with( 100, lt<value_type>() );
662 CPPUNIT_ASSERT( it != l.end() );
663 CPPUNIT_ASSERT( it->nKey == 100 );
664 CPPUNIT_ASSERT( it->nVal == 33 );
667 CPPUNIT_ASSERT( it != l.end() );
668 CPPUNIT_ASSERT( it->nKey == 150 );
669 CPPUNIT_ASSERT( it->nVal == it->nKey * 2 );
671 CPPUNIT_ASSERT( !l.empty() );
673 CPPUNIT_ASSERT( l.empty() );
676 CPPUNIT_ASSERT( l.emplace( 501 ) != l.end() );
677 CPPUNIT_ASSERT( l.emplace( 251, 152 ) != l.end());
678 CPPUNIT_ASSERT( l.emplace( item( 1001 )) != l.end() );
680 // insert failed - such key exists
681 CPPUNIT_ASSERT( l.emplace( 501, 2 ) == l.end() );
682 CPPUNIT_ASSERT( l.emplace( 251, 10) == l.end() );
685 CPPUNIT_ASSERT( it != l.end() );
686 CPPUNIT_ASSERT( it->nKey == 501 );
687 CPPUNIT_ASSERT( it->nVal == 501 * 2 );
690 CPPUNIT_ASSERT( it != l.end() );
691 CPPUNIT_ASSERT( it->nKey == 251 );
692 CPPUNIT_ASSERT( it->nVal == 152 );
695 CPPUNIT_ASSERT( it != l.end() );
696 CPPUNIT_ASSERT( it->nKey == 1001 );
697 CPPUNIT_ASSERT( it->nVal == 1001 * 2 );
700 typename OrdList::iterator it( l.begin() );
701 typename OrdList::const_iterator cit( l.cbegin() );
702 CPPUNIT_CHECK( it == cit );
703 CPPUNIT_CHECK( it != l.end() );
704 CPPUNIT_CHECK( it != l.cend() );
705 CPPUNIT_CHECK( cit != l.end() );
706 CPPUNIT_CHECK( cit != l.cend() );
708 CPPUNIT_CHECK( it != cit );
709 CPPUNIT_CHECK( it != l.end() );
710 CPPUNIT_CHECK( it != l.cend() );
711 CPPUNIT_CHECK( cit != l.end() );
712 CPPUNIT_CHECK( cit != l.cend() );
714 CPPUNIT_CHECK( it == cit );
715 CPPUNIT_CHECK( it != l.end() );
716 CPPUNIT_CHECK( it != l.cend() );
717 CPPUNIT_CHECK( cit != l.end() );
718 CPPUNIT_CHECK( cit != l.cend() );
723 CPPUNIT_ASSERT( l.empty() );
738 void RCU_GPI_cmpmix();
743 void RCU_GPB_cmpmix();
748 void RCU_GPT_cmpmix();
753 void RCU_SHB_cmpmix();
758 void RCU_SHT_cmpmix();
766 CPPUNIT_TEST_SUITE(MichaelListTestHeader)
768 CPPUNIT_TEST(HP_less)
769 CPPUNIT_TEST(HP_cmpmix)
772 CPPUNIT_TEST(DHP_cmp)
773 CPPUNIT_TEST(DHP_less)
774 CPPUNIT_TEST(DHP_cmpmix)
777 CPPUNIT_TEST(RCU_GPI_cmp)
778 CPPUNIT_TEST(RCU_GPI_less)
779 CPPUNIT_TEST(RCU_GPI_cmpmix)
780 CPPUNIT_TEST(RCU_GPI_ic)
782 CPPUNIT_TEST(RCU_GPB_cmp)
783 CPPUNIT_TEST(RCU_GPB_less)
784 CPPUNIT_TEST(RCU_GPB_cmpmix)
785 CPPUNIT_TEST(RCU_GPB_ic)
787 CPPUNIT_TEST(RCU_GPT_cmp)
788 CPPUNIT_TEST(RCU_GPT_less)
789 CPPUNIT_TEST(RCU_GPT_cmpmix)
790 CPPUNIT_TEST(RCU_GPT_ic)
792 CPPUNIT_TEST(RCU_SHB_cmp)
793 CPPUNIT_TEST(RCU_SHB_less)
794 CPPUNIT_TEST(RCU_SHB_cmpmix)
795 CPPUNIT_TEST(RCU_SHB_ic)
797 CPPUNIT_TEST(RCU_SHT_cmp)
798 CPPUNIT_TEST(RCU_SHT_less)
799 CPPUNIT_TEST(RCU_SHT_cmpmix)
800 CPPUNIT_TEST(RCU_SHT_ic)
802 CPPUNIT_TEST(NOGC_cmp)
803 CPPUNIT_TEST(NOGC_less)
804 CPPUNIT_TEST(NOGC_cmpmix)
805 CPPUNIT_TEST(NOGC_ic)
806 CPPUNIT_TEST_SUITE_END()
809 } // namespace ordlist