3 #include "cppunit/cppunit_proxy.h"
4 #include <cds/container/lazy_list_base.h>
7 namespace cc = cds::container;
8 namespace co = cds::container::opt;
10 class LazyListTestHeader: 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 static void insert_function( item& i )
118 i.nVal = i.nKey * 1024;
120 static void dummy_insert_function( item& i )
122 // This function should not be called
123 TestCase::current_test()->error( "CPPUNIT_ASSERT", "dummy_insert_function should not be called", __FILE__, __LINE__ );
126 struct erase_functor {
127 unsigned int nEraseCall;
133 void operator()( item const& /*i*/)
140 unsigned int m_nMultiplier;
142 check_value( unsigned int nMultiplier )
143 : m_nMultiplier( nMultiplier )
146 check_value( const check_value& s )
147 : m_nMultiplier( s.m_nMultiplier )
150 void operator()( item& i, int )
152 CPPUNIT_ASSERT_CURRENT( int(i.nKey * m_nMultiplier) == i.nVal );
156 struct check_exact_value {
159 check_exact_value( int nExpected )
160 : m_nExpected( nExpected )
163 check_exact_value( check_exact_value const& s)
164 : m_nExpected( s.m_nExpected )
167 void operator()( item& i, int )
169 CPPUNIT_ASSERT_CURRENT( i.nVal == m_nExpected );
173 struct dummy_check_value {
174 void operator()( item& i, int )
176 // This functor should not be called
177 TestCase::current_test()->error( "CPPUNIT_ASSERT", "dummy_check_value should not be called", __FILE__, __LINE__ );
181 struct ensure_functor {
182 void operator()( bool bNew, item& i, int n )
184 i.nVal = i.nKey * 1024;
188 static void ensure_func( bool bNew, item& i, int n )
207 template <typename T1, typename T2>
208 bool operator()( T1 const& t1, T2 const& t2 ) const
210 return t1.nKey < t2.nKey;
215 template <class OrdList>
216 void test_with( OrdList& l )
218 typedef typename OrdList::value_type value_type;
220 // The list should be empty
221 CPPUNIT_ASSERT( l.empty() );
224 CPPUNIT_ASSERT( l.insert( 50 ) );
225 CPPUNIT_ASSERT( l.insert( item( 25 )) );
226 CPPUNIT_ASSERT( l.insert( item( 100 )) );
228 // insert failed - such key exists
229 CPPUNIT_ASSERT( !l.insert( 50 ) );
230 CPPUNIT_ASSERT( !l.insert( item( 100 )) );
234 // The list should not be empty
235 CPPUNIT_ASSERT( !l.empty() );
237 // and now the list is empty
238 CPPUNIT_ASSERT( l.empty() );
240 // Test insert with functor
242 CPPUNIT_ASSERT( l.insert( 100, insert_functor() ) );
246 CPPUNIT_ASSERT( l.insert( item(25), boost::ref(f)) );
247 CPPUNIT_ASSERT( !l.insert( item(100), boost::ref(f)) );
249 // Test insert with function
250 CPPUNIT_ASSERT( l.insert( 50, insert_function ));
251 CPPUNIT_ASSERT( !l.insert( 25, dummy_insert_function ));
252 CPPUNIT_ASSERT( !l.insert( 100, dummy_insert_functor() ));
254 // The list should not be empty
255 CPPUNIT_ASSERT( !l.empty() );
257 // 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>(), boost::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, boost::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>(), cds::ref(ef) ));
347 CPPUNIT_ASSERT( ef.nEraseCall == 1 );
348 CPPUNIT_ASSERT( !l.erase_with( 160, lt<value_type>(), cds::ref(ef) ));
349 CPPUNIT_ASSERT( ef.nEraseCall == 1 );
351 CPPUNIT_ASSERT( l.erase( 250, cds::ref(ef) ));
352 CPPUNIT_ASSERT( ef.nEraseCall == 2 );
353 CPPUNIT_ASSERT( !l.erase( 250, cds::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() );
366 #ifdef CDS_EMPLACE_SUPPORT
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() );
393 for ( int i = 0; i < nCount; ++i )
394 CPPUNIT_ASSERT( l.insert(i) );
397 for ( typename OrdList::iterator it = l.begin(), itEnd = l.end(); it != itEnd; ++it, ++i ) {
399 CPPUNIT_ASSERT( it->nKey == i );
402 // Check that we have visited all items
403 for ( int i = 0; i < nCount; ++i )
404 CPPUNIT_ASSERT( l.find( i, check_value(2) ));
407 CPPUNIT_ASSERT( l.empty() );
410 for ( int i = 0; i < nCount; ++i )
411 CPPUNIT_ASSERT( l.insert(i) );
414 const OrdList& rl = l;
415 for ( typename OrdList::const_iterator it = rl.begin(), itEnd = rl.end(); it != itEnd; ++it, ++i ) {
416 // it->nVal = i * 2 ; // not!
417 CPPUNIT_ASSERT( it->nKey == i );
420 // Check that we have visited all items
421 for ( int i = 0; i < nCount; ++i )
422 CPPUNIT_ASSERT( l.find_with( i, lt<value_type>(), check_value(2) ));
425 CPPUNIT_ASSERT( l.empty() );
429 template <class OrdList>
432 typedef typename OrdList::guarded_ptr guarded_ptr;
433 typedef typename OrdList::value_type value_type;
438 static int const nLimit = 20;
440 for ( int i = 0; i < nLimit; i++ )
442 std::random_shuffle( arr, arr + nLimit );
445 for ( int i = 0; i < nLimit; ++i )
449 for ( int i = 0; i < nLimit; ++i ) {
452 CPPUNIT_ASSERT( l.get(gp, nKey));
453 CPPUNIT_ASSERT( !gp.empty());
454 CPPUNIT_CHECK( gp->nKey == nKey );
455 CPPUNIT_CHECK( gp->nVal == nKey * 2 );
458 CPPUNIT_ASSERT( l.extract(gp, nKey));
459 CPPUNIT_ASSERT( !gp.empty());
460 CPPUNIT_CHECK( gp->nKey == nKey );
461 CPPUNIT_CHECK( gp->nVal == nKey*2 );
464 CPPUNIT_CHECK( !l.get(gp, nKey));
465 CPPUNIT_CHECK( gp.empty());
466 CPPUNIT_CHECK( !l.extract( gp, nKey));
467 CPPUNIT_CHECK( gp.empty());
469 CPPUNIT_ASSERT( l.empty());
470 CPPUNIT_CHECK( !l.get(gp, arr[0]));
471 CPPUNIT_CHECK( gp.empty());
472 CPPUNIT_CHECK( !l.extract( gp, arr[0]));
473 CPPUNIT_CHECK( gp.empty());
476 // extract_with/get_with
477 for ( int i = 0; i < nLimit; ++i )
481 for ( int i = 0; i < nLimit; ++i ) {
483 other_item key( nKey );
485 CPPUNIT_ASSERT( l.get_with(gp, key, other_less()));
486 CPPUNIT_ASSERT( !gp.empty());
487 CPPUNIT_CHECK( gp->nKey == nKey );
488 CPPUNIT_CHECK( gp->nVal == nKey * 2 );
491 CPPUNIT_ASSERT( l.extract_with(gp, key, other_less()));
492 CPPUNIT_ASSERT( !gp.empty());
493 CPPUNIT_CHECK( gp->nKey == nKey );
494 CPPUNIT_CHECK( gp->nVal == nKey*2 );
497 CPPUNIT_CHECK( !l.get_with(gp, key, other_less()));
498 CPPUNIT_CHECK( gp.empty());
499 CPPUNIT_CHECK( !l.extract_with( gp, key, other_less()));
500 CPPUNIT_CHECK( gp.empty());
502 CPPUNIT_ASSERT( l.empty());
503 CPPUNIT_CHECK( !l.get_with(gp, other_item(arr[0]), other_less()));
504 CPPUNIT_CHECK( gp.empty());
505 CPPUNIT_CHECK( !l.extract_with( gp, other_item(arr[0]), other_less()));
506 CPPUNIT_CHECK( gp.empty());
511 template <class OrdList>
517 static int const nLimit = 20;
519 typedef typename OrdList::rcu_lock rcu_lock;
520 typedef typename OrdList::value_type value_type;
521 typedef typename OrdList::gc rcu_type;
525 for (int i = 0; i < nLimit; ++i)
527 std::random_shuffle( a, a + nLimit );
530 for ( int i = 0; i < nLimit; ++i )
531 CPPUNIT_ASSERT( l.insert( a[i] ) );
533 typename OrdList::exempt_ptr ep;
535 for ( int i = 0; i < nLimit; ++i ) {
538 value_type * pGet = l.get( a[i] );
539 CPPUNIT_ASSERT( pGet != NULL );
540 CPPUNIT_CHECK( pGet->nKey == a[i] );
541 CPPUNIT_CHECK( pGet->nVal == a[i] * 2 );
543 CPPUNIT_ASSERT( l.extract( ep, a[i] ));
544 CPPUNIT_ASSERT( !ep.empty() );
545 CPPUNIT_CHECK( ep->nKey == a[i] );
546 CPPUNIT_CHECK( (*ep).nVal == a[i] * 2 );
551 CPPUNIT_CHECK( l.get( a[i]) == NULL );
552 CPPUNIT_CHECK( !l.extract( ep, a[i] ));
553 CPPUNIT_CHECK( ep.empty() );
556 CPPUNIT_ASSERT( l.empty() );
560 CPPUNIT_CHECK( l.get( a[0] ) == NULL );
561 CPPUNIT_CHECK( !l.extract( ep, a[0] ) );
562 CPPUNIT_CHECK( ep.empty() );
565 // extract_with/get_with
566 for ( int i = 0; i < nLimit; ++i ) {
567 CPPUNIT_ASSERT( l.insert( a[i] ) );
570 for ( int i = 0; i < nLimit; ++i ) {
571 other_item itm( a[i] );
574 value_type * pGet = l.get_with( itm, other_less() );
575 CPPUNIT_ASSERT( pGet != NULL );
576 CPPUNIT_CHECK( pGet->nKey == a[i] );
577 CPPUNIT_CHECK( pGet->nVal == a[i] * 2 );
579 CPPUNIT_ASSERT( l.extract_with( ep, itm, other_less() ));
580 CPPUNIT_ASSERT( !ep.empty() );
581 CPPUNIT_CHECK( ep->nKey == a[i] );
582 CPPUNIT_CHECK( ep->nVal == a[i] * 2 );
587 CPPUNIT_CHECK( l.get_with( itm, other_less()) == NULL );
588 CPPUNIT_CHECK( !l.extract_with( ep, itm, other_less() ));
589 CPPUNIT_CHECK( ep.empty() );
592 CPPUNIT_ASSERT( l.empty() );
596 CPPUNIT_CHECK( l.get_with( other_item(0), other_less() ) == NULL );
597 CPPUNIT_CHECK( !l.extract_with( ep, other_item(0), other_less() ));
598 CPPUNIT_CHECK( ep.empty() );
603 template <class OrdList>
606 typedef OrdList list;
607 typedef typename list::value_type value_type;
608 typedef std::pair<typename list::iterator, bool> ensure_result;
610 typename list::iterator it;
613 CPPUNIT_ASSERT( l.empty() );
614 CPPUNIT_ASSERT( l.insert(50) != l.end() );
615 CPPUNIT_ASSERT( !l.empty() );
617 ensure_result eres = l.ensure( item(100, 33) );
618 CPPUNIT_ASSERT( eres.second );
619 CPPUNIT_ASSERT( eres.first != l.end() );
620 CPPUNIT_ASSERT( l.insert( item(150) ) != l.end() );
622 CPPUNIT_ASSERT( l.insert(100) == l.end() );
623 eres = l.ensure( item(50, 33) );
624 CPPUNIT_ASSERT( !eres.second );
625 CPPUNIT_ASSERT( eres.first->nVal == eres.first->nKey * 2 );
626 eres.first->nVal = 63;
629 CPPUNIT_ASSERT( it == l.end() );
632 CPPUNIT_ASSERT( it != l.end() );
633 CPPUNIT_ASSERT( it->nKey == 50 );
634 CPPUNIT_ASSERT( it->nVal == 63 );
637 CPPUNIT_ASSERT( it != l.end() );
638 CPPUNIT_ASSERT( it->nKey == 100 );
639 CPPUNIT_ASSERT( it->nVal == 33 );
641 it = l.find_with( 150, lt<value_type>() );
642 CPPUNIT_ASSERT( it != l.end() );
643 CPPUNIT_ASSERT( it->nKey == 150 );
644 CPPUNIT_ASSERT( it->nVal == it->nKey * 2 );
646 CPPUNIT_ASSERT( !l.empty() );
648 CPPUNIT_ASSERT( l.empty() );
650 #ifdef CDS_EMPLACE_SUPPORT
652 CPPUNIT_ASSERT( l.emplace( 501 ) != l.end());
653 CPPUNIT_ASSERT( l.emplace( 251, 152 ) != l.end());
654 CPPUNIT_ASSERT( l.emplace( item( 1001 )) != l.end());
656 // insert failed - such key exists
657 CPPUNIT_ASSERT( l.emplace( 501, 2 ) == l.end());
658 CPPUNIT_ASSERT( l.emplace( 251, 10) == l.end());
661 CPPUNIT_ASSERT( it != l.end() );
662 CPPUNIT_ASSERT( it->nKey == 501 );
663 CPPUNIT_ASSERT( it->nVal == 501 * 2 );
665 it = l.find_with( 251, lt<value_type>() );
666 CPPUNIT_ASSERT( it != l.end() );
667 CPPUNIT_ASSERT( it->nKey == 251 );
668 CPPUNIT_ASSERT( it->nVal == 152 );
671 CPPUNIT_ASSERT( it != l.end() );
672 CPPUNIT_ASSERT( it->nKey == 1001 );
673 CPPUNIT_ASSERT( it->nVal == 1001 * 2 );
676 CPPUNIT_ASSERT( l.empty() );
698 void RCU_GPI_cmpmix();
703 void RCU_GPB_cmpmix();
708 void RCU_GPT_cmpmix();
713 void RCU_SHB_cmpmix();
718 void RCU_SHT_cmpmix();
726 CPPUNIT_TEST_SUITE(LazyListTestHeader)
728 CPPUNIT_TEST(HP_less)
729 CPPUNIT_TEST(HP_cmpmix)
732 CPPUNIT_TEST(PTB_cmp)
733 CPPUNIT_TEST(PTB_less)
734 CPPUNIT_TEST(PTB_cmpmix)
737 CPPUNIT_TEST(HRC_cmp)
738 CPPUNIT_TEST(HRC_less)
739 CPPUNIT_TEST(HRC_cmpmix)
742 CPPUNIT_TEST(RCU_GPI_cmp)
743 CPPUNIT_TEST(RCU_GPI_less)
744 CPPUNIT_TEST(RCU_GPI_cmpmix)
745 CPPUNIT_TEST(RCU_GPI_ic)
747 CPPUNIT_TEST(RCU_GPB_cmp)
748 CPPUNIT_TEST(RCU_GPB_less)
749 CPPUNIT_TEST(RCU_GPB_cmpmix)
750 CPPUNIT_TEST(RCU_GPB_ic)
752 CPPUNIT_TEST(RCU_GPT_cmp)
753 CPPUNIT_TEST(RCU_GPT_less)
754 CPPUNIT_TEST(RCU_GPT_cmpmix)
755 CPPUNIT_TEST(RCU_GPT_ic)
757 CPPUNIT_TEST(RCU_SHB_cmp)
758 CPPUNIT_TEST(RCU_SHB_less)
759 CPPUNIT_TEST(RCU_SHB_cmpmix)
760 CPPUNIT_TEST(RCU_SHB_ic)
762 CPPUNIT_TEST(RCU_SHT_cmp)
763 CPPUNIT_TEST(RCU_SHT_less)
764 CPPUNIT_TEST(RCU_SHT_cmpmix)
765 CPPUNIT_TEST(RCU_SHT_ic)
767 CPPUNIT_TEST(NOGC_cmp)
768 CPPUNIT_TEST(NOGC_less)
769 CPPUNIT_TEST(NOGC_cmpmix)
770 CPPUNIT_TEST(NOGC_ic)
771 CPPUNIT_TEST_SUITE_END()
774 } // namespace ordlist