3 #ifndef CDSTEST_HDR_LAZY_H
4 #define CDSTEST_HDR_LAZY_H
6 #include "cppunit/cppunit_proxy.h"
7 #include <cds/container/details/lazy_list_base.h>
10 namespace cc = cds::container;
11 namespace co = cds::container::opt;
13 class LazyListTestHeader: public CppUnitMini::TestCase
17 int nEnsureExistsCall;
40 item(int key, int val)
61 bool operator ()(const T& v1, const T& v2 ) const
63 return v1.key() < v2.key();
67 bool operator ()(const T& v1, const Q& v2 ) const
73 bool operator ()(const Q& v1, const T& v2 ) const
81 int operator ()(const T& v1, const T& v2 ) const
83 if ( v1.key() < v2.key() )
85 return v1.key() > v2.key() ? 1 : 0;
89 int operator ()(const T& v1, const Q& v2 ) const
93 return v1.key() > v2 ? 1 : 0;
97 int operator ()(const Q& v1, const T& v2 ) const
101 return v1 > v2.key() ? 1 : 0;
105 struct insert_functor {
106 void operator ()( item& i )
108 i.nVal = i.nKey * 1033;
111 struct dummy_insert_functor {
112 void operator ()( item& /*i*/ )
114 // This functor should not be called
115 TestCase::current_test()->error( "CPPUNIT_ASSERT", "dummy_insert_functor should not be called", __FILE__, __LINE__ );
119 static void insert_function( item& i )
121 i.nVal = i.nKey * 1024;
123 static void dummy_insert_function( item& /*i*/ )
125 // This function should not be called
126 TestCase::current_test()->error( "CPPUNIT_ASSERT", "dummy_insert_function should not be called", __FILE__, __LINE__ );
129 struct erase_functor {
130 unsigned int nEraseCall;
136 void operator()( item const& /*i*/)
143 unsigned int m_nMultiplier;
145 check_value( unsigned int nMultiplier )
146 : m_nMultiplier( nMultiplier )
149 check_value( const check_value& s )
150 : m_nMultiplier( s.m_nMultiplier )
153 void operator()( item& i, int )
155 CPPUNIT_ASSERT_CURRENT( int(i.nKey * m_nMultiplier) == i.nVal );
159 struct check_exact_value {
162 check_exact_value( int nExpected )
163 : m_nExpected( nExpected )
166 check_exact_value( check_exact_value const& s)
167 : m_nExpected( s.m_nExpected )
170 void operator()( item& i, int )
172 CPPUNIT_ASSERT_CURRENT( i.nVal == m_nExpected );
176 struct dummy_check_value {
177 void operator()( item& /*i*/, int )
179 // This functor should not be called
180 TestCase::current_test()->error( "CPPUNIT_ASSERT", "dummy_check_value should not be called", __FILE__, __LINE__ );
184 struct ensure_functor {
185 void operator()( bool /*bNew*/, item& i, int /*n*/ )
187 i.nVal = i.nKey * 1024;
191 static void ensure_func( bool /*bNew*/, item& i, int n )
210 template <typename T1, typename T2>
211 bool operator()( T1 const& t1, T2 const& t2 ) const
213 return t1.nKey < t2.nKey;
218 template <class OrdList>
219 void test_with( OrdList& l )
221 typedef typename OrdList::value_type value_type;
223 // The list should be empty
224 CPPUNIT_ASSERT( l.empty() );
227 CPPUNIT_ASSERT( l.insert( 50 ) );
228 CPPUNIT_ASSERT( l.insert( item( 25 )) );
229 CPPUNIT_ASSERT( l.insert( item( 100 )) );
231 // insert failed - such key exists
232 CPPUNIT_ASSERT( !l.insert( 50 ) );
233 CPPUNIT_ASSERT( !l.insert( item( 100 )) );
237 // The list should not be empty
238 CPPUNIT_ASSERT( !l.empty() );
240 // and now the list is empty
241 CPPUNIT_ASSERT( l.empty() );
243 // Test insert with functor
245 CPPUNIT_ASSERT( l.insert( 100, insert_functor() ) );
249 CPPUNIT_ASSERT( l.insert( item( 25 ), std::ref( f ) ) );
250 CPPUNIT_ASSERT( !l.insert( item( 100 ), std::ref( f ) ) );
252 // Test insert with function
253 CPPUNIT_ASSERT( l.insert( 50, insert_function ));
254 CPPUNIT_ASSERT( !l.insert( 25, dummy_insert_function ));
255 CPPUNIT_ASSERT( !l.insert( 100, dummy_insert_functor() ));
257 // The list should not be empty
258 CPPUNIT_ASSERT( !l.empty() );
260 // Check inserted values
265 CPPUNIT_ASSERT( l.find( 100 ));
266 CPPUNIT_ASSERT( l.find( i, check_value(1033) ));
270 CPPUNIT_ASSERT( l.find_with( 25, lt<value_type>() ));
271 CPPUNIT_ASSERT( l.find_with( i, lt<value_type>(), std::ref( f ) ) );
274 CPPUNIT_ASSERT( l.find( 50 ));
275 CPPUNIT_ASSERT( l.find( i, check_value(1024) ));
278 CPPUNIT_ASSERT( !l.find_with( 10, lt<value_type>() ));
279 CPPUNIT_ASSERT( !l.find_with( i, lt<value_type>(), dummy_check_value() ));
281 CPPUNIT_ASSERT( !l.find( 75 ));
282 CPPUNIT_ASSERT( !l.find( i, dummy_check_value() ));
284 CPPUNIT_ASSERT( !l.find( 150 ));
285 CPPUNIT_ASSERT( !l.find( i, dummy_check_value() ));
288 // The list should not be empty
289 CPPUNIT_ASSERT( !l.empty() );
291 // and now the list is empty
292 CPPUNIT_ASSERT( l.empty() );
296 std::pair<bool, bool> ensureResult;
298 ensureResult = l.ensure( 100, ensure_functor() );
299 CPPUNIT_ASSERT( ensureResult.first );
300 CPPUNIT_ASSERT( ensureResult.second );
302 ensureResult = l.ensure( 200, std::ref( f ) );
303 CPPUNIT_ASSERT( ensureResult.first );
304 CPPUNIT_ASSERT( ensureResult.second );
306 ensureResult = l.ensure( 50, ensure_func );
307 CPPUNIT_ASSERT( ensureResult.first );
308 CPPUNIT_ASSERT( ensureResult.second );
312 CPPUNIT_ASSERT( l.find( i, check_value(1024) ));
314 CPPUNIT_ASSERT( l.find( i, check_value(1033) ));
316 CPPUNIT_ASSERT( l.find( i, check_value(1024) ));
318 // ensure existing key
319 ensureResult = l.ensure( 200, ensure_func );
320 CPPUNIT_ASSERT( ensureResult.first );
321 CPPUNIT_ASSERT( !ensureResult.second );
323 CPPUNIT_ASSERT( l.find( i, check_value(1033) ));
325 ensureResult = l.ensure( 50, ensure_functor() );
326 CPPUNIT_ASSERT( ensureResult.first );
327 CPPUNIT_ASSERT( !ensureResult.second );
329 CPPUNIT_ASSERT( l.find( i, check_value(1024) ));
332 // erase test (list: 50, 100, 200)
333 CPPUNIT_ASSERT( !l.empty() );
334 CPPUNIT_ASSERT( l.insert(160));
335 CPPUNIT_ASSERT( l.insert(250));
336 CPPUNIT_ASSERT( !l.empty() );
338 CPPUNIT_ASSERT( !l.erase( 150 ));
340 CPPUNIT_ASSERT( l.erase( 100 ));
341 CPPUNIT_ASSERT( !l.erase( 100 ));
343 CPPUNIT_ASSERT( l.erase_with( 200, lt<value_type>() ));
344 CPPUNIT_ASSERT( !l.erase_with( 200, lt<value_type>() ));
348 CPPUNIT_ASSERT( ef.nEraseCall == 0 );
349 CPPUNIT_ASSERT( l.erase_with( 160, lt<value_type>(), std::ref(ef) ));
350 CPPUNIT_ASSERT( ef.nEraseCall == 1 );
351 CPPUNIT_ASSERT( !l.erase_with( 160, lt<value_type>(), std::ref(ef) ));
352 CPPUNIT_ASSERT( ef.nEraseCall == 1 );
354 CPPUNIT_ASSERT( l.erase( 250, std::ref(ef) ));
355 CPPUNIT_ASSERT( ef.nEraseCall == 2 );
356 CPPUNIT_ASSERT( !l.erase( 250, std::ref(ef) ));
357 CPPUNIT_ASSERT( ef.nEraseCall == 2 );
360 CPPUNIT_ASSERT( l.erase( 50 ));
361 CPPUNIT_ASSERT( !l.erase( 50 ));
363 CPPUNIT_ASSERT( l.empty() );
367 CPPUNIT_ASSERT( l.empty() );
372 CPPUNIT_ASSERT( l.emplace( 501 ) );
373 CPPUNIT_ASSERT( l.emplace( 251, 152 ));
374 CPPUNIT_ASSERT( l.emplace( item( 1001 )) );
376 // insert failed - such key exists
377 CPPUNIT_ASSERT( !l.emplace( 501, 2 ) );
378 CPPUNIT_ASSERT( !l.emplace( 251, 10) );
381 CPPUNIT_ASSERT( l.find( i, check_exact_value(501*2) ));
383 CPPUNIT_ASSERT( l.find( i, check_exact_value(152) ));
385 CPPUNIT_ASSERT( l.find( i, check_exact_value(1001*2) ));
388 CPPUNIT_ASSERT( l.empty() );
394 for ( int i = 0; i < nCount; ++i )
395 CPPUNIT_ASSERT( l.insert( i ) );
398 typename OrdList::iterator it( l.begin() );
399 typename OrdList::const_iterator cit( l.cbegin() );
400 CPPUNIT_CHECK( it == cit );
401 CPPUNIT_CHECK( it != l.end() );
402 CPPUNIT_CHECK( it != l.cend() );
403 CPPUNIT_CHECK( cit != l.end() );
404 CPPUNIT_CHECK( cit != l.cend() );
406 CPPUNIT_CHECK( it != cit );
407 CPPUNIT_CHECK( it != l.end() );
408 CPPUNIT_CHECK( it != l.cend() );
409 CPPUNIT_CHECK( cit != l.end() );
410 CPPUNIT_CHECK( cit != l.cend() );
412 CPPUNIT_CHECK( it == cit );
413 CPPUNIT_CHECK( it != l.end() );
414 CPPUNIT_CHECK( it != l.cend() );
415 CPPUNIT_CHECK( cit != l.end() );
416 CPPUNIT_CHECK( cit != l.cend() );
420 for ( typename OrdList::iterator it = l.begin(), itEnd = l.end(); it != itEnd; ++it, ++i ) {
422 CPPUNIT_ASSERT( it->nKey == i );
425 // Check that we have visited all items
426 for ( int i = 0; i < nCount; ++i )
427 CPPUNIT_ASSERT( l.find( i, check_value(2) ));
430 CPPUNIT_ASSERT( l.empty() );
433 for ( int i = 0; i < nCount; ++i )
434 CPPUNIT_ASSERT( l.insert(i) );
437 const OrdList& rl = l;
438 for ( typename OrdList::const_iterator it = rl.begin(), itEnd = rl.end(); it != itEnd; ++it, ++i ) {
439 // it->nVal = i * 2 ; // not!
440 CPPUNIT_ASSERT( it->nKey == i );
443 // Check that we have visited all items
444 for ( int i = 0; i < nCount; ++i )
445 CPPUNIT_ASSERT( l.find_with( i, lt<value_type>(), check_value(2) ));
448 CPPUNIT_ASSERT( l.empty() );
452 template <class OrdList>
455 typedef typename OrdList::guarded_ptr guarded_ptr;
456 typedef typename OrdList::value_type value_type;
461 static int const nLimit = 20;
463 for ( int i = 0; i < nLimit; i++ )
465 std::random_shuffle( arr, arr + nLimit );
468 for ( int i = 0; i < nLimit; ++i )
472 for ( int i = 0; i < nLimit; ++i ) {
476 CPPUNIT_ASSERT( gp );
477 CPPUNIT_ASSERT( !gp.empty());
478 CPPUNIT_CHECK( gp->nKey == nKey );
479 CPPUNIT_CHECK( gp->nVal == nKey * 2 );
482 gp = l.extract( nKey );
483 CPPUNIT_ASSERT( gp );
484 CPPUNIT_ASSERT( !gp.empty());
485 CPPUNIT_CHECK( gp->nKey == nKey );
486 CPPUNIT_CHECK( gp->nVal == nKey*2 );
490 CPPUNIT_CHECK( !gp );
491 CPPUNIT_CHECK( gp.empty());
492 CPPUNIT_CHECK( !l.extract( nKey));
493 CPPUNIT_CHECK( gp.empty());
495 CPPUNIT_ASSERT( l.empty());
496 CPPUNIT_CHECK( !l.get(arr[0]));
497 CPPUNIT_CHECK( gp.empty());
498 CPPUNIT_CHECK( !l.extract( arr[0]));
499 CPPUNIT_CHECK( gp.empty());
502 // extract_with/get_with
503 for ( int i = 0; i < nLimit; ++i )
507 for ( int i = 0; i < nLimit; ++i ) {
509 other_item key( nKey );
511 gp = l.get_with( key, other_less() );
512 CPPUNIT_ASSERT( gp );
513 CPPUNIT_ASSERT( !gp.empty());
514 CPPUNIT_CHECK( gp->nKey == nKey );
515 CPPUNIT_CHECK( gp->nVal == nKey * 2 );
518 gp = l.extract_with( key, other_less() );
519 CPPUNIT_ASSERT( gp );
520 CPPUNIT_ASSERT( !gp.empty());
521 CPPUNIT_CHECK( gp->nKey == nKey );
522 CPPUNIT_CHECK( gp->nVal == nKey*2 );
525 gp = l.get_with( key, other_less() );
526 CPPUNIT_CHECK( !gp );
527 CPPUNIT_CHECK( gp.empty());
528 CPPUNIT_CHECK( !l.extract_with( key, other_less()));
529 CPPUNIT_CHECK( gp.empty());
531 CPPUNIT_ASSERT( l.empty());
532 CPPUNIT_CHECK( !l.get_with(other_item(arr[0]), other_less()));
533 CPPUNIT_CHECK( gp.empty());
534 CPPUNIT_CHECK( !l.extract_with( other_item(arr[0]), other_less()));
535 CPPUNIT_CHECK( gp.empty());
540 template <class OrdList>
546 static int const nLimit = 20;
548 typedef typename OrdList::rcu_lock rcu_lock;
549 typedef typename OrdList::value_type value_type;
550 typedef typename OrdList::gc rcu_type;
554 for (int i = 0; i < nLimit; ++i)
556 std::random_shuffle( a, a + nLimit );
559 for ( int i = 0; i < nLimit; ++i )
560 CPPUNIT_ASSERT( l.insert( a[i] ) );
562 typename OrdList::exempt_ptr ep;
564 for ( int i = 0; i < nLimit; ++i ) {
567 value_type * pGet = l.get( a[i] );
568 CPPUNIT_ASSERT( pGet != nullptr );
569 CPPUNIT_CHECK( pGet->nKey == a[i] );
570 CPPUNIT_CHECK( pGet->nVal == a[i] * 2 );
572 ep = l.extract( a[i] );
573 CPPUNIT_ASSERT( ep );
574 CPPUNIT_ASSERT( !ep.empty() );
575 CPPUNIT_CHECK( ep->nKey == a[i] );
576 CPPUNIT_CHECK( (*ep).nVal == a[i] * 2 );
581 CPPUNIT_CHECK( l.get( a[i] ) == nullptr );
582 CPPUNIT_CHECK( !l.extract( a[i] ));
585 CPPUNIT_ASSERT( l.empty() );
589 CPPUNIT_CHECK( l.get( a[0] ) == nullptr );
590 ep = l.extract( a[0] );
591 CPPUNIT_CHECK( !ep );
592 CPPUNIT_CHECK( ep.empty() );
595 // extract_with/get_with
596 for ( int i = 0; i < nLimit; ++i ) {
597 CPPUNIT_ASSERT( l.insert( a[i] ) );
600 for ( int i = 0; i < nLimit; ++i ) {
601 other_item itm( a[i] );
604 value_type * pGet = l.get_with( itm, other_less() );
605 CPPUNIT_ASSERT( pGet != nullptr );
606 CPPUNIT_CHECK( pGet->nKey == a[i] );
607 CPPUNIT_CHECK( pGet->nVal == a[i] * 2 );
609 ep = l.extract_with( itm, other_less() );
610 CPPUNIT_ASSERT( ep );
611 CPPUNIT_ASSERT( !ep.empty() );
612 CPPUNIT_CHECK( ep->nKey == a[i] );
613 CPPUNIT_CHECK( ep->nVal == a[i] * 2 );
618 CPPUNIT_CHECK( l.get_with( itm, other_less() ) == nullptr );
619 ep = l.extract_with( itm, other_less() );
620 CPPUNIT_CHECK( !ep );
621 CPPUNIT_CHECK( ep.empty() );
624 CPPUNIT_ASSERT( l.empty() );
628 CPPUNIT_CHECK( l.get_with( other_item( 0 ), other_less() ) == nullptr );
629 CPPUNIT_CHECK( !l.extract_with( other_item(0), other_less() ));
630 CPPUNIT_CHECK( ep.empty() );
635 template <class OrdList>
638 typedef OrdList list;
639 typedef typename list::value_type value_type;
640 typedef std::pair<typename list::iterator, bool> ensure_result;
642 typename list::iterator it;
645 CPPUNIT_ASSERT( l.empty() );
646 CPPUNIT_ASSERT( l.insert(50) != l.end() );
647 CPPUNIT_ASSERT( !l.empty() );
649 ensure_result eres = l.ensure( item(100, 33) );
650 CPPUNIT_ASSERT( eres.second );
651 CPPUNIT_ASSERT( eres.first != l.end() );
652 CPPUNIT_ASSERT( l.insert( item(150) ) != l.end() );
654 CPPUNIT_ASSERT( l.insert(100) == l.end() );
655 eres = l.ensure( item(50, 33) );
656 CPPUNIT_ASSERT( !eres.second );
657 CPPUNIT_ASSERT( eres.first->nVal == eres.first->nKey * 2 );
658 eres.first->nVal = 63;
661 CPPUNIT_ASSERT( it == l.end() );
664 CPPUNIT_ASSERT( it != l.end() );
665 CPPUNIT_ASSERT( it->nKey == 50 );
666 CPPUNIT_ASSERT( it->nVal == 63 );
669 CPPUNIT_ASSERT( it != l.end() );
670 CPPUNIT_ASSERT( it->nKey == 100 );
671 CPPUNIT_ASSERT( it->nVal == 33 );
673 it = l.find_with( 150, lt<value_type>() );
674 CPPUNIT_ASSERT( it != l.end() );
675 CPPUNIT_ASSERT( it->nKey == 150 );
676 CPPUNIT_ASSERT( it->nVal == it->nKey * 2 );
678 CPPUNIT_ASSERT( !l.empty() );
680 CPPUNIT_ASSERT( l.empty() );
683 CPPUNIT_ASSERT( l.emplace( 501 ) != l.end());
684 CPPUNIT_ASSERT( l.emplace( 251, 152 ) != l.end());
685 CPPUNIT_ASSERT( l.emplace( item( 1001 )) != l.end());
687 // insert failed - such key exists
688 CPPUNIT_ASSERT( l.emplace( 501, 2 ) == l.end());
689 CPPUNIT_ASSERT( l.emplace( 251, 10) == l.end());
692 CPPUNIT_ASSERT( it != l.end() );
693 CPPUNIT_ASSERT( it->nKey == 501 );
694 CPPUNIT_ASSERT( it->nVal == 501 * 2 );
696 it = l.find_with( 251, lt<value_type>() );
697 CPPUNIT_ASSERT( it != l.end() );
698 CPPUNIT_ASSERT( it->nKey == 251 );
699 CPPUNIT_ASSERT( it->nVal == 152 );
702 CPPUNIT_ASSERT( it != l.end() );
703 CPPUNIT_ASSERT( it->nKey == 1001 );
704 CPPUNIT_ASSERT( it->nVal == 1001 * 2 );
707 typename OrdList::iterator it( l.begin() );
708 typename OrdList::const_iterator cit( l.cbegin() );
709 CPPUNIT_CHECK( it == cit );
710 CPPUNIT_CHECK( it != l.end() );
711 CPPUNIT_CHECK( it != l.cend() );
712 CPPUNIT_CHECK( cit != l.end() );
713 CPPUNIT_CHECK( cit != l.cend() );
715 CPPUNIT_CHECK( it != cit );
716 CPPUNIT_CHECK( it != l.end() );
717 CPPUNIT_CHECK( it != l.cend() );
718 CPPUNIT_CHECK( cit != l.end() );
719 CPPUNIT_CHECK( cit != l.cend() );
721 CPPUNIT_CHECK( it == cit );
722 CPPUNIT_CHECK( it != l.end() );
723 CPPUNIT_CHECK( it != l.cend() );
724 CPPUNIT_CHECK( cit != l.end() );
725 CPPUNIT_CHECK( cit != l.cend() );
730 CPPUNIT_ASSERT( l.empty() );
745 void RCU_GPI_cmpmix();
750 void RCU_GPB_cmpmix();
755 void RCU_GPT_cmpmix();
760 void RCU_SHB_cmpmix();
765 void RCU_SHT_cmpmix();
773 CPPUNIT_TEST_SUITE(LazyListTestHeader)
775 CPPUNIT_TEST(HP_less)
776 CPPUNIT_TEST(HP_cmpmix)
779 CPPUNIT_TEST(DHP_cmp)
780 CPPUNIT_TEST(DHP_less)
781 CPPUNIT_TEST(DHP_cmpmix)
784 CPPUNIT_TEST(RCU_GPI_cmp)
785 CPPUNIT_TEST(RCU_GPI_less)
786 CPPUNIT_TEST(RCU_GPI_cmpmix)
787 CPPUNIT_TEST(RCU_GPI_ic)
789 CPPUNIT_TEST(RCU_GPB_cmp)
790 CPPUNIT_TEST(RCU_GPB_less)
791 CPPUNIT_TEST(RCU_GPB_cmpmix)
792 CPPUNIT_TEST(RCU_GPB_ic)
794 CPPUNIT_TEST(RCU_GPT_cmp)
795 CPPUNIT_TEST(RCU_GPT_less)
796 CPPUNIT_TEST(RCU_GPT_cmpmix)
797 CPPUNIT_TEST(RCU_GPT_ic)
799 CPPUNIT_TEST(RCU_SHB_cmp)
800 CPPUNIT_TEST(RCU_SHB_less)
801 CPPUNIT_TEST(RCU_SHB_cmpmix)
802 CPPUNIT_TEST(RCU_SHB_ic)
804 CPPUNIT_TEST(RCU_SHT_cmp)
805 CPPUNIT_TEST(RCU_SHT_less)
806 CPPUNIT_TEST(RCU_SHT_cmpmix)
807 CPPUNIT_TEST(RCU_SHT_ic)
809 CPPUNIT_TEST(NOGC_cmp)
810 CPPUNIT_TEST(NOGC_less)
811 CPPUNIT_TEST(NOGC_cmpmix)
812 CPPUNIT_TEST(NOGC_ic)
813 CPPUNIT_TEST_SUITE_END()
816 } // namespace ordlist
818 #endif // #ifndef CDSTEST_HDR_LAZY_H