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 MichaelKVListTestHeader: public CppUnitMini::TestCase
29 bool operator ()(const T& v1, const T& v2 ) const
37 int operator ()(const T& v1, const T& v2 ) const
41 return v1 > v2 ? 1 : 0;
48 check_value( int nExpected )
49 : m_nExpected( nExpected )
53 void operator ()( T& pair )
55 CPPUNIT_ASSERT_CURRENT( pair.second.m_val == m_nExpected );
59 struct insert_functor {
61 void operator()( T& pair )
63 pair.second.m_val = pair.first * 10;
67 struct ensure_functor {
69 void operator()( bool /*bNew*/, T& pair )
71 pair.second.m_val = pair.first * 50;
75 struct erase_functor {
85 void operator()( T& i )
88 nVal = i.second.m_val;
92 typedef float other_key;
94 bool operator()( float f, int i ) const
98 bool operator()( int i, float f ) const
105 template <class OrdList>
106 void test_with( OrdList& l)
108 typedef typename OrdList::value_type value_type;
110 typename OrdList::iterator itTest;
111 typename OrdList::const_iterator citTest;
113 CPPUNIT_ASSERT( l.empty() );
115 // insert / find test
116 CPPUNIT_ASSERT( !l.find( 100 ));
117 CPPUNIT_ASSERT( l.insert( 100 ));
118 CPPUNIT_ASSERT( !l.empty() );
119 CPPUNIT_ASSERT( l.find( 100 ));
122 CPPUNIT_ASSERT( l.find( 100, std::ref( chk ) ) );
124 CPPUNIT_ASSERT( !l.find_with( 50, lt<key_type>() ));
125 CPPUNIT_ASSERT( l.insert( 50, 500 ));
126 CPPUNIT_ASSERT( l.find_with( 50, lt<key_type>() ));
127 CPPUNIT_ASSERT( !l.insert( 50, 5 ));
128 chk.m_nExpected = 500;
129 CPPUNIT_ASSERT( l.find_with( 50, lt<key_type>(), std::ref( chk ) ) );
131 CPPUNIT_ASSERT( l.find_with( 100, lt<key_type>(), std::ref( chk ) ) );
132 CPPUNIT_ASSERT( !l.empty() );
134 CPPUNIT_ASSERT( !l.find( 150 ));
135 CPPUNIT_ASSERT( l.insert_key( 150, insert_functor() ));
136 CPPUNIT_ASSERT( l.find( 150 ));
137 chk.m_nExpected = 1500;
138 CPPUNIT_ASSERT( l.find( 150, std::ref( chk ) ) );
140 CPPUNIT_ASSERT( l.find( 100, std::ref( chk ) ) );
141 chk.m_nExpected = 500;
142 CPPUNIT_ASSERT( l.find( 50, std::ref( chk ) ) );
143 CPPUNIT_ASSERT( !l.empty() );
147 CPPUNIT_ASSERT( !l.erase( 500 ));
148 CPPUNIT_ASSERT( !l.empty() );
150 CPPUNIT_ASSERT( l.find( 50 ));
153 l.erase( 50, std::ref( ef ) );
154 CPPUNIT_ASSERT( ef.nKey == 50 );
155 CPPUNIT_ASSERT( ef.nVal == 500 );
157 CPPUNIT_ASSERT( !l.find( 50 ));
160 std::pair<bool, bool> bEnsureResult;
161 bEnsureResult = l.ensure( 100, ensure_functor() );
162 CPPUNIT_ASSERT( bEnsureResult.first );
163 CPPUNIT_ASSERT( !bEnsureResult.second );
164 chk.m_nExpected = 5000;
165 CPPUNIT_ASSERT( l.find( 100, std::ref( chk ) ) );
169 bEnsureResult = l.ensure( 50, std::ref( ef ) );
171 CPPUNIT_ASSERT( bEnsureResult.first );
172 CPPUNIT_ASSERT( bEnsureResult.second );
173 chk.m_nExpected = 2500;
174 CPPUNIT_ASSERT( l.find( 50, std::ref( chk ) ) );
177 CPPUNIT_ASSERT( !l.empty() );
178 CPPUNIT_ASSERT( l.insert_key( 200, insert_functor() ));
179 CPPUNIT_ASSERT( l.insert( 25 ));
180 CPPUNIT_ASSERT( l.erase( 100 ));
181 CPPUNIT_ASSERT( l.erase( 150 ));
184 CPPUNIT_ASSERT( l.erase_with( 200, lt<key_type>(), std::ref(ef)) );
185 CPPUNIT_ASSERT( ef.nKey == 200 );
186 CPPUNIT_ASSERT( ef.nVal == 2000 );
188 CPPUNIT_ASSERT( l.erase_with( 25, lt<key_type>()))
189 CPPUNIT_ASSERT( l.erase( 50 ));
190 CPPUNIT_ASSERT( l.empty() );
194 CPPUNIT_ASSERT( l.empty() );
197 CPPUNIT_ASSERT( l.emplace( 501 ) );
198 CPPUNIT_ASSERT( l.emplace( 251, 152 ));
200 // insert failed - such key exists
201 CPPUNIT_ASSERT( !l.emplace( 501, 2 ) );
202 CPPUNIT_ASSERT( !l.emplace( 251, 10) );
205 CPPUNIT_ASSERT( l.find( 501, std::ref(cv) ));
206 cv.m_nExpected = 152;
207 CPPUNIT_ASSERT( l.find( 251, std::ref(cv) ));
210 CPPUNIT_ASSERT( l.empty() );
215 for ( int i = 0; i < nCount; ++i )
216 CPPUNIT_ASSERT( l.insert(i, i * 2 ) );
219 typename OrdList::iterator it( l.begin() );
220 typename OrdList::const_iterator cit( l.cbegin() );
221 CPPUNIT_CHECK( it == cit );
222 CPPUNIT_CHECK( it != l.end() );
223 CPPUNIT_CHECK( it != l.cend() );
224 CPPUNIT_CHECK( cit != l.end() );
225 CPPUNIT_CHECK( cit != l.cend() );
227 CPPUNIT_CHECK( it != cit );
228 CPPUNIT_CHECK( it != l.end() );
229 CPPUNIT_CHECK( it != l.cend() );
230 CPPUNIT_CHECK( cit != l.end() );
231 CPPUNIT_CHECK( cit != l.cend() );
233 CPPUNIT_CHECK( it == cit );
234 CPPUNIT_CHECK( it != l.end() );
235 CPPUNIT_CHECK( it != l.cend() );
236 CPPUNIT_CHECK( cit != l.end() );
237 CPPUNIT_CHECK( cit != l.cend() );
241 for ( typename OrdList::iterator it = l.begin(), itEnd = l.end(); it != itEnd; ++it, ++i ) {
242 CPPUNIT_ASSERT( it.key() == i );
243 CPPUNIT_ASSERT( it->first == i );
244 CPPUNIT_ASSERT( (*it).first == i );
246 CPPUNIT_ASSERT( it.val().m_val == i * 2 );
247 CPPUNIT_ASSERT( it->second.m_val == i * 2 );
248 CPPUNIT_ASSERT( (*it).second.m_val == i * 2 );
249 it.val().m_val = i * 3;
252 // Check that we have visited all items
253 for ( int i = 0; i < nCount; ++i ) {
254 chk.m_nExpected = i * 3;
255 CPPUNIT_ASSERT( l.find( i, std::ref( chk ) ) );
259 CPPUNIT_ASSERT( l.empty() );
262 for ( int i = 0; i < nCount; ++i )
263 CPPUNIT_ASSERT( l.insert(i, i * 7) );
266 const OrdList& rl = l;
267 for ( typename OrdList::const_iterator it = rl.begin(), itEnd = rl.end(); it != itEnd; ++it, ++i ) {
268 CPPUNIT_ASSERT( it.key() == i );
269 CPPUNIT_ASSERT( it->first == i );
270 CPPUNIT_ASSERT( (*it).first == i );
272 CPPUNIT_ASSERT( it.val().m_val == i * 7 );
273 CPPUNIT_ASSERT( it->second.m_val == i * 7 );
274 CPPUNIT_ASSERT( (*it).second.m_val == i * 7 );
277 // Check that we have visited all items
278 for ( int i = 0; i < nCount; ++i ) {
279 chk.m_nExpected = i * 7;
280 CPPUNIT_ASSERT( l.find_with( i, lt<key_type>(), std::ref( chk ) ) );
284 CPPUNIT_ASSERT( l.empty() );
288 template <class OrdList>
294 typedef typename OrdList::guarded_ptr guarded_ptr;
296 static int const nLimit = 20;
298 for ( int i = 0; i < nLimit; i++ )
300 std::random_shuffle( arr, arr + nLimit );
303 for ( int i = 0; i < nLimit; ++i )
304 l.insert( arr[i], arr[i] * 2 );
307 for ( int i = 0; i < nLimit; ++i ) {
311 CPPUNIT_ASSERT( gp );
312 CPPUNIT_ASSERT( !gp.empty());
313 CPPUNIT_CHECK( gp->first == nKey );
314 CPPUNIT_CHECK( gp->second.m_val == nKey * 2 );
316 gp = l.extract( nKey );
317 CPPUNIT_ASSERT( gp );
318 CPPUNIT_ASSERT( !gp.empty());
319 CPPUNIT_CHECK( gp->first == nKey );
320 CPPUNIT_CHECK( gp->second.m_val == nKey*2 );
323 CPPUNIT_CHECK( !gp );
324 CPPUNIT_CHECK( gp.empty());
325 CPPUNIT_CHECK( !l.extract( nKey));
326 CPPUNIT_CHECK( gp.empty());
328 CPPUNIT_ASSERT( l.empty());
329 CPPUNIT_CHECK( !l.get(arr[0]));
330 CPPUNIT_CHECK( gp.empty());
331 CPPUNIT_CHECK( !l.extract( arr[0]));
332 CPPUNIT_CHECK( gp.empty());
335 // extract_with/get_with
336 for ( int i = 0; i < nLimit; ++i )
337 l.insert( arr[i], arr[i] * 2 );
340 for ( int i = 0; i < nLimit; ++i ) {
342 other_key key = float(nKey + 0.3);
344 gp = l.get_with( key, other_less() );
345 CPPUNIT_ASSERT( gp );
346 CPPUNIT_ASSERT( !gp.empty());
347 CPPUNIT_CHECK( gp->first == nKey );
348 CPPUNIT_CHECK( gp->second.m_val == nKey * 2 );
350 gp = l.extract_with( key, other_less() );
351 CPPUNIT_ASSERT( gp );
352 CPPUNIT_ASSERT( !gp.empty());
353 CPPUNIT_CHECK( gp->first == nKey );
354 CPPUNIT_CHECK( gp->second.m_val == nKey*2 );
356 gp = l.get_with( key, other_less() );
357 CPPUNIT_CHECK( !gp );
358 CPPUNIT_CHECK( gp.empty());
359 CPPUNIT_CHECK( !l.extract_with( key, other_less()));
360 CPPUNIT_CHECK( gp.empty());
362 CPPUNIT_ASSERT( l.empty());
363 CPPUNIT_CHECK( !l.get_with( 3.4f, other_less()));
364 CPPUNIT_CHECK( gp.empty());
365 CPPUNIT_CHECK( !l.extract_with( 3.4f, other_less()));
366 CPPUNIT_CHECK( gp.empty());
370 template <class OrdList>
376 static int const nLimit = 20;
378 typedef typename OrdList::rcu_lock rcu_lock;
379 typedef typename OrdList::value_type value_type;
380 typedef typename OrdList::gc rcu_type;
384 for (int i = 0; i < nLimit; ++i)
386 std::random_shuffle( a, a + nLimit );
389 for ( int i = 0; i < nLimit; ++i )
390 CPPUNIT_ASSERT( l.insert( a[i], a[i]*2 ) );
392 typename OrdList::exempt_ptr ep;
394 for ( int i = 0; i < nLimit; ++i ) {
397 value_type * pGet = l.get( a[i] );
398 CPPUNIT_ASSERT( pGet != nullptr );
399 CPPUNIT_CHECK( pGet->first == a[i] );
400 CPPUNIT_CHECK( pGet->second.m_val == a[i] * 2 );
402 ep = l.extract( a[i] );
403 CPPUNIT_ASSERT( ep );
404 CPPUNIT_ASSERT( !ep.empty() );
405 CPPUNIT_CHECK( ep->first == a[i] );
406 CPPUNIT_CHECK( (*ep).second.m_val == a[i] * 2 );
411 CPPUNIT_CHECK( l.get( a[i] ) == nullptr );
412 ep = l.extract( a[i] );
413 CPPUNIT_CHECK( !ep );
414 CPPUNIT_CHECK( ep.empty() );
417 CPPUNIT_ASSERT( l.empty() );
421 CPPUNIT_CHECK( l.get( a[0] ) == nullptr );
422 CPPUNIT_CHECK( !l.extract( a[0] ) );
423 CPPUNIT_CHECK( ep.empty() );
426 // extract_with/get_with
427 for ( int i = 0; i < nLimit; ++i ) {
428 CPPUNIT_ASSERT( l.insert( a[i], a[i]*2 ) );
431 for ( int i = 0; i < nLimit; ++i ) {
432 float itm = a[i] + 0.3f;
435 value_type * pGet = l.get_with( itm, other_less() );
436 CPPUNIT_ASSERT( pGet != nullptr );
437 CPPUNIT_CHECK( pGet->first == a[i] );
438 CPPUNIT_CHECK( pGet->second.m_val == a[i] * 2 );
440 ep = l.extract_with( itm, other_less() );
441 CPPUNIT_ASSERT( ep );
442 CPPUNIT_ASSERT( !ep.empty() );
443 CPPUNIT_CHECK( ep->first == a[i] );
444 CPPUNIT_CHECK( ep->second.m_val == a[i] * 2 );
449 CPPUNIT_CHECK( l.get_with( itm, other_less() ) == nullptr );
450 ep = l.extract_with( itm, other_less() );
451 CPPUNIT_CHECK( !ep );
452 CPPUNIT_CHECK( ep.empty() );
455 CPPUNIT_ASSERT( l.empty() );
459 CPPUNIT_CHECK( l.get_with( 3.14f, other_less() ) == nullptr );
460 CPPUNIT_CHECK( !l.extract_with( 3.14f, other_less() ));
461 CPPUNIT_CHECK( ep.empty() );
467 template <class OrdList>
470 typedef typename OrdList::value_type value_type;
471 typedef typename OrdList::iterator iterator;
477 CPPUNIT_ASSERT( l.empty() );
479 // insert / find test
480 CPPUNIT_ASSERT( l.find( 100 ) == l.end() );
481 CPPUNIT_ASSERT( l.insert( 100 ) != l.end() );
482 CPPUNIT_ASSERT( !l.empty() );
483 it = l.find_with( 100, lt<key_type>() );
484 CPPUNIT_ASSERT( it != l.end() );
485 CPPUNIT_ASSERT( it.key() == 100 );
486 CPPUNIT_ASSERT( it.val().m_val == 0 );
488 CPPUNIT_ASSERT( l.find_with( 50, lt<key_type>() ) == l.end() );
489 CPPUNIT_ASSERT( l.insert( 50, 500 ) != l.end());
491 CPPUNIT_ASSERT( it != l.end() );
492 CPPUNIT_ASSERT( it.key() == 50 );
493 CPPUNIT_ASSERT( it.val().m_val == 500 );
495 CPPUNIT_ASSERT( l.insert( 50, 5 ) == l.end() );
497 CPPUNIT_ASSERT( it != l.end() );
498 CPPUNIT_ASSERT( it.key() == 50 );
499 CPPUNIT_ASSERT( it.val().m_val == 500 );
500 CPPUNIT_ASSERT( !l.empty() );
502 CPPUNIT_ASSERT( l.find( 150 ) == l.end() );
503 CPPUNIT_ASSERT( l.insert_key( 150, insert_functor() ) != l.end() );
505 CPPUNIT_ASSERT( it != l.end() );
506 CPPUNIT_ASSERT( it.key() == 150 );
507 CPPUNIT_ASSERT( it.val().m_val == 1500 );
509 CPPUNIT_ASSERT( it != l.end() );
510 CPPUNIT_ASSERT( it.key() == 100 );
511 CPPUNIT_ASSERT( it.val().m_val == 0 );
513 CPPUNIT_ASSERT( it != l.end() );
514 CPPUNIT_ASSERT( it.key() == 50 );
515 CPPUNIT_ASSERT( it.val().m_val == 500 );
518 CPPUNIT_ASSERT( it != l.end() );
519 CPPUNIT_ASSERT( it.key() == 50 );
520 CPPUNIT_ASSERT( it.val().m_val == 25 );
521 CPPUNIT_ASSERT( !l.empty() );
523 // ensure existing item
524 std::pair<iterator, bool> ensureResult;
525 ensureResult = l.ensure( 100 );
526 CPPUNIT_ASSERT( !ensureResult.second );
527 CPPUNIT_ASSERT( ensureResult.first.key() == 100 );
528 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 0 );
529 ensureResult.first.val().m_val = 5;
531 CPPUNIT_ASSERT( it != l.end() );
532 CPPUNIT_ASSERT( it.key() == 100 );
533 CPPUNIT_ASSERT( it.val().m_val == 5 );
535 CPPUNIT_ASSERT( !l.empty() );
538 ensureResult = l.ensure( 1000 );
539 CPPUNIT_ASSERT( ensureResult.second );
540 CPPUNIT_ASSERT( ensureResult.first.key() == 1000 );
541 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 0 );
542 ensureResult.first.val().m_val = 33;
543 ensureResult = l.ensure( 1000 );
544 CPPUNIT_ASSERT( !ensureResult.second );
545 CPPUNIT_ASSERT( ensureResult.first.key() == 1000 );
546 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 33 );
550 CPPUNIT_ASSERT( l.empty() );
553 CPPUNIT_ASSERT( l.emplace( 501 ) != l.end());
554 CPPUNIT_ASSERT( l.emplace( 251, 152 ) != l.end());
556 // insert failed - such key exists
557 CPPUNIT_ASSERT( l.emplace( 501, 2 ) == l.end());
558 CPPUNIT_ASSERT( l.emplace( 251, 10) == l.end());
561 CPPUNIT_ASSERT( it != l.end());
562 CPPUNIT_ASSERT( it.key() == 501 );
563 CPPUNIT_ASSERT( it.val().m_val == 0 );
566 CPPUNIT_ASSERT( it != l.end());
567 CPPUNIT_ASSERT( it.key() == 251 );
568 CPPUNIT_ASSERT( it.val().m_val == 152 );
571 CPPUNIT_ASSERT( l.empty() );
576 for ( int i = 0; i < nCount; ++i )
577 CPPUNIT_ASSERT( l.insert(i, i * 2 ) != l.end() );
580 typename OrdList::iterator it( l.begin() );
581 typename OrdList::const_iterator cit( l.cbegin() );
582 CPPUNIT_CHECK( it == cit );
583 CPPUNIT_CHECK( it != l.end() );
584 CPPUNIT_CHECK( it != l.cend() );
585 CPPUNIT_CHECK( cit != l.end() );
586 CPPUNIT_CHECK( cit != l.cend() );
588 CPPUNIT_CHECK( it != cit );
589 CPPUNIT_CHECK( it != l.end() );
590 CPPUNIT_CHECK( it != l.cend() );
591 CPPUNIT_CHECK( cit != l.end() );
592 CPPUNIT_CHECK( cit != l.cend() );
594 CPPUNIT_CHECK( it == cit );
595 CPPUNIT_CHECK( it != l.end() );
596 CPPUNIT_CHECK( it != l.cend() );
597 CPPUNIT_CHECK( cit != l.end() );
598 CPPUNIT_CHECK( cit != l.cend() );
602 for ( typename OrdList::iterator iter = l.begin(), itEnd = l.end(); iter != itEnd; ++iter, ++i ) {
603 CPPUNIT_ASSERT( iter.key() == i );
604 CPPUNIT_ASSERT( iter->first == i );
605 CPPUNIT_ASSERT( (*iter).first == i );
607 CPPUNIT_ASSERT( iter.val().m_val == i * 2 );
608 CPPUNIT_ASSERT( iter->second.m_val == i * 2 );
609 CPPUNIT_ASSERT( (*iter).second.m_val == i * 2 );
611 iter.val().m_val = i * 3;
614 // Check that we have visited all items
615 for ( int i = 0; i < nCount; ++i ) {
617 CPPUNIT_ASSERT( it != l.end() );
618 CPPUNIT_ASSERT( it.key() == i );
619 CPPUNIT_ASSERT( it.val().m_val == i * 3 );
623 CPPUNIT_ASSERT( l.empty() );
626 for ( int i = 0; i < nCount; ++i )
627 CPPUNIT_ASSERT( l.insert(i, i * 7) != l.end() );
630 const OrdList& rl = l;
631 for ( typename OrdList::const_iterator iter = rl.begin(), itEnd = rl.end(); iter != itEnd; ++iter, ++i ) {
632 CPPUNIT_ASSERT( iter.key() == i );
633 CPPUNIT_ASSERT( iter->first == i );
634 CPPUNIT_ASSERT( (*iter).first == i );
636 CPPUNIT_ASSERT( iter.val().m_val == i * 7 );
637 CPPUNIT_ASSERT( iter->second.m_val == i * 7 );
638 CPPUNIT_ASSERT( (*iter).second.m_val == i * 7 );
640 // it.val().m_val = i * 3 ; // error: const-iterator
644 CPPUNIT_ASSERT( l.empty() );
662 void RCU_GPI_cmpmix();
667 void RCU_GPB_cmpmix();
672 void RCU_GPT_cmpmix();
677 void RCU_SHB_cmpmix();
682 void RCU_SHT_cmpmix();
690 CPPUNIT_TEST_SUITE(MichaelKVListTestHeader)
692 CPPUNIT_TEST(HP_less)
693 CPPUNIT_TEST(HP_cmpmix)
696 CPPUNIT_TEST(DHP_cmp)
697 CPPUNIT_TEST(DHP_less)
698 CPPUNIT_TEST(DHP_cmpmix)
701 CPPUNIT_TEST(RCU_GPI_cmp)
702 CPPUNIT_TEST(RCU_GPI_less)
703 CPPUNIT_TEST(RCU_GPI_cmpmix)
704 CPPUNIT_TEST(RCU_GPI_ic)
706 CPPUNIT_TEST(RCU_GPB_cmp)
707 CPPUNIT_TEST(RCU_GPB_less)
708 CPPUNIT_TEST(RCU_GPB_cmpmix)
709 CPPUNIT_TEST(RCU_GPB_ic)
711 CPPUNIT_TEST(RCU_GPT_cmp)
712 CPPUNIT_TEST(RCU_GPT_less)
713 CPPUNIT_TEST(RCU_GPT_cmpmix)
714 CPPUNIT_TEST(RCU_GPT_ic)
716 CPPUNIT_TEST(RCU_SHB_cmp)
717 CPPUNIT_TEST(RCU_SHB_less)
718 CPPUNIT_TEST(RCU_SHB_cmpmix)
719 CPPUNIT_TEST(RCU_SHB_ic)
721 CPPUNIT_TEST(RCU_SHT_cmp)
722 CPPUNIT_TEST(RCU_SHT_less)
723 CPPUNIT_TEST(RCU_SHT_cmpmix)
724 CPPUNIT_TEST(RCU_SHT_ic)
726 CPPUNIT_TEST(NOGC_cmp)
727 CPPUNIT_TEST(NOGC_less)
728 CPPUNIT_TEST(NOGC_cmpmix)
729 CPPUNIT_TEST(NOGC_ic)
730 CPPUNIT_TEST_SUITE_END()
733 } // namespace ordlist