3 #include "cppunit/cppunit_proxy.h"
4 #include <cds/container/details/lazy_list_base.h>
7 namespace cc = cds::container;
8 namespace co = cds::container::opt;
10 class LazyKVListTestHeader: 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, boost::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( 50, boost::ref( chk ) ));
131 CPPUNIT_ASSERT( l.find( 100, boost::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_with( 150, lt<key_type>(), boost::ref( chk ) ));
140 CPPUNIT_ASSERT( l.find( 100, boost::ref( chk ) ));
141 chk.m_nExpected = 500;
142 CPPUNIT_ASSERT( l.find( 50, boost::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, boost::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, boost::ref( chk ) ));
169 bEnsureResult = l.ensure( 50, boost::ref( ef ));
171 CPPUNIT_ASSERT( bEnsureResult.first );
172 CPPUNIT_ASSERT( bEnsureResult.second );
173 chk.m_nExpected = 2500;
174 CPPUNIT_ASSERT( l.find( 50, boost::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>(), cds::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, cds::ref(cv) ));
206 cv.m_nExpected = 152;
207 CPPUNIT_ASSERT( l.find( 251, cds::ref(cv) ));
210 CPPUNIT_ASSERT( l.empty() );
215 for ( int i = 0; i < nCount; ++i )
216 CPPUNIT_ASSERT( l.insert(i, i * 2 ) );
219 for ( typename OrdList::iterator it = l.begin(), itEnd = l.end(); it != itEnd; ++it, ++i ) {
220 CPPUNIT_ASSERT( it.key() == i );
221 CPPUNIT_ASSERT( it.val().m_val == i * 2 );
222 it.val().m_val = i * 3;
225 // Check that we have visited all items
226 for ( int i = 0; i < nCount; ++i ) {
227 chk.m_nExpected = i * 3;
228 CPPUNIT_ASSERT( l.find( i, boost::ref(chk) ));
232 CPPUNIT_ASSERT( l.empty() );
235 for ( int i = 0; i < nCount; ++i )
236 CPPUNIT_ASSERT( l.insert(i, i * 7) );
239 const OrdList& rl = l;
240 for ( typename OrdList::const_iterator it = rl.begin(), itEnd = rl.end(); it != itEnd; ++it, ++i ) {
241 CPPUNIT_ASSERT( it.key() == i );
242 CPPUNIT_ASSERT( it.val().m_val == i * 7 );
245 // Check that we have visited all items
246 for ( int i = nCount; i > 0; --i ) {
247 chk.m_nExpected = (i - 1) * 7;
248 CPPUNIT_ASSERT( l.find_with( i - 1, lt<key_type>(), boost::ref(chk) ));
252 CPPUNIT_ASSERT( l.empty() );
256 template <class OrdList>
262 typedef typename OrdList::guarded_ptr guarded_ptr;
264 static int const nLimit = 20;
266 for ( int i = 0; i < nLimit; i++ )
268 std::random_shuffle( arr, arr + nLimit );
271 for ( int i = 0; i < nLimit; ++i )
272 l.insert( arr[i], arr[i] * 2 );
275 for ( int i = 0; i < nLimit; ++i ) {
278 CPPUNIT_ASSERT( l.get(gp, nKey));
279 CPPUNIT_ASSERT( !gp.empty());
280 CPPUNIT_CHECK( gp->first == nKey );
281 CPPUNIT_CHECK( gp->second.m_val == nKey * 2 );
284 CPPUNIT_ASSERT( l.extract(gp, nKey));
285 CPPUNIT_ASSERT( !gp.empty());
286 CPPUNIT_CHECK( gp->first == nKey );
287 CPPUNIT_CHECK( gp->second.m_val == nKey*2 );
290 CPPUNIT_CHECK( !l.get(gp, nKey));
291 CPPUNIT_CHECK( gp.empty());
292 CPPUNIT_CHECK( !l.extract( gp, nKey));
293 CPPUNIT_CHECK( gp.empty());
295 CPPUNIT_ASSERT( l.empty());
296 CPPUNIT_CHECK( !l.get(gp, arr[0]));
297 CPPUNIT_CHECK( gp.empty());
298 CPPUNIT_CHECK( !l.extract( gp, arr[0]));
299 CPPUNIT_CHECK( gp.empty());
302 // extract_with/get_with
303 for ( int i = 0; i < nLimit; ++i )
304 l.insert( arr[i], arr[i] * 2 );
307 for ( int i = 0; i < nLimit; ++i ) {
309 other_key key = float(nKey + 0.3);
311 CPPUNIT_ASSERT( l.get_with(gp, key, other_less()));
312 CPPUNIT_ASSERT( !gp.empty());
313 CPPUNIT_CHECK( gp->first == nKey );
314 CPPUNIT_CHECK( gp->second.m_val == nKey * 2 );
317 CPPUNIT_ASSERT( l.extract_with(gp, key, other_less()));
318 CPPUNIT_ASSERT( !gp.empty());
319 CPPUNIT_CHECK( gp->first == nKey );
320 CPPUNIT_CHECK( gp->second.m_val == nKey*2 );
323 CPPUNIT_CHECK( !l.get_with(gp, key, other_less()));
324 CPPUNIT_CHECK( gp.empty());
325 CPPUNIT_CHECK( !l.extract_with( gp, key, other_less()));
326 CPPUNIT_CHECK( gp.empty());
328 CPPUNIT_ASSERT( l.empty());
329 CPPUNIT_CHECK( !l.get_with(gp, 3.4f, other_less()));
330 CPPUNIT_CHECK( gp.empty());
331 CPPUNIT_CHECK( !l.extract_with( gp, 3.4f, other_less()));
332 CPPUNIT_CHECK( gp.empty());
336 template <class OrdList>
342 static int const nLimit = 20;
344 typedef typename OrdList::rcu_lock rcu_lock;
345 typedef typename OrdList::value_type value_type;
346 typedef typename OrdList::gc rcu_type;
350 for (int i = 0; i < nLimit; ++i)
352 std::random_shuffle( a, a + nLimit );
355 for ( int i = 0; i < nLimit; ++i )
356 CPPUNIT_ASSERT( l.insert( a[i], a[i]*2 ) );
358 typename OrdList::exempt_ptr ep;
360 for ( int i = 0; i < nLimit; ++i ) {
363 value_type * pGet = l.get( a[i] );
364 CPPUNIT_ASSERT( pGet != nullptr );
365 CPPUNIT_CHECK( pGet->first == a[i] );
366 CPPUNIT_CHECK( pGet->second.m_val == a[i] * 2 );
368 CPPUNIT_ASSERT( l.extract( ep, a[i] ));
369 CPPUNIT_ASSERT( !ep.empty() );
370 CPPUNIT_CHECK( ep->first == a[i] );
371 CPPUNIT_CHECK( (*ep).second.m_val == a[i] * 2 );
376 CPPUNIT_CHECK( l.get( a[i] ) == nullptr );
377 CPPUNIT_CHECK( !l.extract( ep, a[i] ));
378 CPPUNIT_CHECK( ep.empty() );
381 CPPUNIT_ASSERT( l.empty() );
385 CPPUNIT_CHECK( l.get( a[0] ) == nullptr );
386 CPPUNIT_CHECK( !l.extract( ep, a[0] ) );
387 CPPUNIT_CHECK( ep.empty() );
390 // extract_with/get_with
391 for ( int i = 0; i < nLimit; ++i ) {
392 CPPUNIT_ASSERT( l.insert( a[i], a[i]*2 ) );
395 for ( int i = 0; i < nLimit; ++i ) {
396 float itm = a[i] + 0.3f;
399 value_type * pGet = l.get_with( itm, other_less() );
400 CPPUNIT_ASSERT( pGet != nullptr );
401 CPPUNIT_CHECK( pGet->first == a[i] );
402 CPPUNIT_CHECK( pGet->second.m_val == a[i] * 2 );
404 CPPUNIT_ASSERT( l.extract_with( ep, itm, other_less() ));
405 CPPUNIT_ASSERT( !ep.empty() );
406 CPPUNIT_CHECK( ep->first == a[i] );
407 CPPUNIT_CHECK( ep->second.m_val == a[i] * 2 );
412 CPPUNIT_CHECK( l.get_with( itm, other_less()) == nullptr );
413 CPPUNIT_CHECK( !l.extract_with( ep, itm, other_less() ));
414 CPPUNIT_CHECK( ep.empty() );
417 CPPUNIT_ASSERT( l.empty() );
421 CPPUNIT_CHECK( l.get_with( 3.14f, other_less() ) == nullptr );
422 CPPUNIT_CHECK( !l.extract_with( ep, 3.14f, other_less() ));
423 CPPUNIT_CHECK( ep.empty() );
428 template <class OrdList>
431 typedef typename OrdList::value_type value_type;
432 typedef typename OrdList::iterator iterator;
438 CPPUNIT_ASSERT( l.empty() );
440 // insert / find test
441 CPPUNIT_ASSERT( l.find( 100 ) == l.end() );
442 CPPUNIT_ASSERT( l.insert( 100 ) != l.end() );
443 CPPUNIT_ASSERT( !l.empty() );
445 CPPUNIT_ASSERT( it != l.end() );
446 CPPUNIT_ASSERT( it.key() == 100 );
447 CPPUNIT_ASSERT( it.val().m_val == 0 );
449 CPPUNIT_ASSERT( l.find_with( 50, lt<key_type>() ) == l.end() );
450 CPPUNIT_ASSERT( l.insert( 50, 500 ) != l.end());
452 CPPUNIT_ASSERT( it != l.end() );
453 CPPUNIT_ASSERT( it.key() == 50 );
454 CPPUNIT_ASSERT( it.val().m_val == 500 );
456 CPPUNIT_ASSERT( l.insert( 50, 5 ) == l.end() );
458 CPPUNIT_ASSERT( it != l.end() );
459 CPPUNIT_ASSERT( it.key() == 50 );
460 CPPUNIT_ASSERT( it.val().m_val == 500 );
461 CPPUNIT_ASSERT( !l.empty() );
463 CPPUNIT_ASSERT( l.find( 150 ) == l.end() );
464 CPPUNIT_ASSERT( l.insert_key( 150, insert_functor() ) != l.end() );
466 CPPUNIT_ASSERT( it != l.end() );
467 CPPUNIT_ASSERT( it.key() == 150 );
468 CPPUNIT_ASSERT( it.val().m_val == 1500 );
470 CPPUNIT_ASSERT( it != l.end() );
471 CPPUNIT_ASSERT( it.key() == 100 );
472 CPPUNIT_ASSERT( it.val().m_val == 0 );
474 CPPUNIT_ASSERT( it != l.end() );
475 CPPUNIT_ASSERT( it.key() == 50 );
476 CPPUNIT_ASSERT( it.val().m_val == 500 );
479 CPPUNIT_ASSERT( it != l.end() );
480 CPPUNIT_ASSERT( it.key() == 50 );
481 CPPUNIT_ASSERT( it.val().m_val == 25 );
482 CPPUNIT_ASSERT( !l.empty() );
484 // ensure existing item
485 std::pair<iterator, bool> ensureResult;
486 ensureResult = l.ensure( 100 );
487 CPPUNIT_ASSERT( !ensureResult.second );
488 CPPUNIT_ASSERT( ensureResult.first.key() == 100 );
489 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 0 );
490 ensureResult.first.val().m_val = 5;
492 CPPUNIT_ASSERT( it != l.end() );
493 CPPUNIT_ASSERT( it.key() == 100 );
494 CPPUNIT_ASSERT( it.val().m_val == 5 );
496 CPPUNIT_ASSERT( !l.empty() );
499 ensureResult = l.ensure( 1000 );
500 CPPUNIT_ASSERT( ensureResult.second );
501 CPPUNIT_ASSERT( ensureResult.first.key() == 1000 );
502 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 0 );
503 ensureResult.first.val().m_val = 33;
504 ensureResult = l.ensure( 1000 );
505 CPPUNIT_ASSERT( !ensureResult.second );
506 CPPUNIT_ASSERT( ensureResult.first.key() == 1000 );
507 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 33 );
511 CPPUNIT_ASSERT( l.empty() );
514 CPPUNIT_ASSERT( l.emplace( 501 ) != l.end());
515 CPPUNIT_ASSERT( l.emplace( 251, 152 ) != l.end());
517 // insert failed - such key exists
518 CPPUNIT_ASSERT( l.emplace( 501, 2 ) == l.end());
519 CPPUNIT_ASSERT( l.emplace( 251, 10) == l.end());
522 CPPUNIT_ASSERT( it != l.end() );
523 CPPUNIT_ASSERT( it.key() == 501 );
524 CPPUNIT_ASSERT( it.val().m_val == 0 );
527 CPPUNIT_ASSERT( it != l.end() );
528 CPPUNIT_ASSERT( it.key() == 251 );
529 CPPUNIT_ASSERT( it.val().m_val == 152 );
532 CPPUNIT_ASSERT( l.empty() );
537 for ( int i = 0; i < nCount; ++i )
538 CPPUNIT_ASSERT( l.insert(i, i * 2 ) != l.end() );
541 for ( typename OrdList::iterator iter = l.begin(), itEnd = l.end(); iter != itEnd; ++iter, ++i ) {
542 CPPUNIT_ASSERT( iter.key() == i );
543 CPPUNIT_ASSERT( iter->first == i );
544 CPPUNIT_ASSERT( (*iter).first == i );
546 CPPUNIT_ASSERT( iter.val().m_val == i * 2 );
547 CPPUNIT_ASSERT( iter->second.m_val == i * 2 );
548 CPPUNIT_ASSERT( (*iter).second.m_val == i * 2 );
550 iter.val().m_val = i * 3;
553 // Check that we have visited all items
554 for ( int i = 0; i < nCount; ++i ) {
556 CPPUNIT_ASSERT( it != l.end() );
557 CPPUNIT_ASSERT( it.key() == i );
558 CPPUNIT_ASSERT( it.val().m_val == i * 3 );
562 CPPUNIT_ASSERT( l.empty() );
565 for ( int i = 0; i < nCount; ++i )
566 CPPUNIT_ASSERT( l.insert(i, i * 7) != l.end() );
569 const OrdList& rl = l;
570 for ( typename OrdList::const_iterator iter = rl.begin(), itEnd = rl.end(); iter != itEnd; ++iter, ++i ) {
571 CPPUNIT_ASSERT( iter.key() == i );
572 CPPUNIT_ASSERT( iter->first == i );
573 CPPUNIT_ASSERT( (*iter).first == i );
575 CPPUNIT_ASSERT( iter.val().m_val == i * 7 );
576 CPPUNIT_ASSERT( iter->second.m_val == i * 7 );
577 CPPUNIT_ASSERT( (*iter).second.m_val == i * 7 );
578 // it.val().m_val = i * 3 ; // error: const-iterator
582 CPPUNIT_ASSERT( l.empty() );
600 void RCU_GPI_cmpmix();
605 void RCU_GPB_cmpmix();
610 void RCU_GPT_cmpmix();
615 void RCU_SHB_cmpmix();
620 void RCU_SHT_cmpmix();
633 CPPUNIT_TEST_SUITE(LazyKVListTestHeader)
635 CPPUNIT_TEST(HP_less)
636 CPPUNIT_TEST(HP_cmpmix)
639 CPPUNIT_TEST(PTB_cmp)
640 CPPUNIT_TEST(PTB_less)
641 CPPUNIT_TEST(PTB_cmpmix)
644 CPPUNIT_TEST(HRC_cmp)
645 CPPUNIT_TEST(HRC_less)
646 CPPUNIT_TEST(HRC_cmpmix)
649 CPPUNIT_TEST(RCU_GPI_cmp)
650 CPPUNIT_TEST(RCU_GPI_less)
651 CPPUNIT_TEST(RCU_GPI_cmpmix)
652 CPPUNIT_TEST(RCU_GPI_ic)
654 CPPUNIT_TEST(RCU_GPB_cmp)
655 CPPUNIT_TEST(RCU_GPB_less)
656 CPPUNIT_TEST(RCU_GPB_cmpmix)
657 CPPUNIT_TEST(RCU_GPB_ic)
659 CPPUNIT_TEST(RCU_GPT_cmp)
660 CPPUNIT_TEST(RCU_GPT_less)
661 CPPUNIT_TEST(RCU_GPT_cmpmix)
662 CPPUNIT_TEST(RCU_GPT_ic)
664 CPPUNIT_TEST(RCU_SHB_cmp)
665 CPPUNIT_TEST(RCU_SHB_less)
666 CPPUNIT_TEST(RCU_SHB_cmpmix)
667 CPPUNIT_TEST(RCU_SHB_ic)
669 CPPUNIT_TEST(RCU_SHT_cmp)
670 CPPUNIT_TEST(RCU_SHT_less)
671 CPPUNIT_TEST(RCU_SHT_cmpmix)
672 CPPUNIT_TEST(RCU_SHT_ic)
674 CPPUNIT_TEST(NOGC_cmp)
675 CPPUNIT_TEST(NOGC_less)
676 CPPUNIT_TEST(NOGC_cmpmix)
677 CPPUNIT_TEST(NOGC_ic)
678 CPPUNIT_TEST_SUITE_END()
681 } // namespace ordlist