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, 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( 50, std::ref( chk ) ) );
131 CPPUNIT_ASSERT( l.find( 100, 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_with( 150, lt<key_type>(), 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.val().m_val == i * 2 );
244 it.val().m_val = i * 3;
247 // Check that we have visited all items
248 for ( int i = 0; i < nCount; ++i ) {
249 chk.m_nExpected = i * 3;
250 CPPUNIT_ASSERT( l.find( i, std::ref( chk ) ) );
254 CPPUNIT_ASSERT( l.empty() );
257 for ( int i = 0; i < nCount; ++i )
258 CPPUNIT_ASSERT( l.insert(i, i * 7) );
261 const OrdList& rl = l;
262 for ( typename OrdList::const_iterator it = rl.begin(), itEnd = rl.end(); it != itEnd; ++it, ++i ) {
263 CPPUNIT_ASSERT( it.key() == i );
264 CPPUNIT_ASSERT( it.val().m_val == i * 7 );
267 // Check that we have visited all items
268 for ( int i = nCount; i > 0; --i ) {
269 chk.m_nExpected = (i - 1) * 7;
270 CPPUNIT_ASSERT( l.find_with( i - 1, lt<key_type>(), std::ref( chk ) ) );
274 CPPUNIT_ASSERT( l.empty() );
278 template <class OrdList>
284 typedef typename OrdList::guarded_ptr guarded_ptr;
286 static int const nLimit = 20;
288 for ( int i = 0; i < nLimit; i++ )
290 std::random_shuffle( arr, arr + nLimit );
293 for ( int i = 0; i < nLimit; ++i )
294 l.insert( arr[i], arr[i] * 2 );
297 for ( int i = 0; i < nLimit; ++i ) {
300 CPPUNIT_ASSERT( l.get(gp, nKey));
301 CPPUNIT_ASSERT( !gp.empty());
302 CPPUNIT_CHECK( gp->first == nKey );
303 CPPUNIT_CHECK( gp->second.m_val == nKey * 2 );
306 CPPUNIT_ASSERT( l.extract(gp, nKey));
307 CPPUNIT_ASSERT( !gp.empty());
308 CPPUNIT_CHECK( gp->first == nKey );
309 CPPUNIT_CHECK( gp->second.m_val == nKey*2 );
312 CPPUNIT_CHECK( !l.get(gp, nKey));
313 CPPUNIT_CHECK( gp.empty());
314 CPPUNIT_CHECK( !l.extract( gp, nKey));
315 CPPUNIT_CHECK( gp.empty());
317 CPPUNIT_ASSERT( l.empty());
318 CPPUNIT_CHECK( !l.get(gp, arr[0]));
319 CPPUNIT_CHECK( gp.empty());
320 CPPUNIT_CHECK( !l.extract( gp, arr[0]));
321 CPPUNIT_CHECK( gp.empty());
324 // extract_with/get_with
325 for ( int i = 0; i < nLimit; ++i )
326 l.insert( arr[i], arr[i] * 2 );
329 for ( int i = 0; i < nLimit; ++i ) {
331 other_key key = float(nKey + 0.3);
333 CPPUNIT_ASSERT( l.get_with(gp, key, other_less()));
334 CPPUNIT_ASSERT( !gp.empty());
335 CPPUNIT_CHECK( gp->first == nKey );
336 CPPUNIT_CHECK( gp->second.m_val == nKey * 2 );
339 CPPUNIT_ASSERT( l.extract_with(gp, key, other_less()));
340 CPPUNIT_ASSERT( !gp.empty());
341 CPPUNIT_CHECK( gp->first == nKey );
342 CPPUNIT_CHECK( gp->second.m_val == nKey*2 );
345 CPPUNIT_CHECK( !l.get_with(gp, key, other_less()));
346 CPPUNIT_CHECK( gp.empty());
347 CPPUNIT_CHECK( !l.extract_with( gp, key, other_less()));
348 CPPUNIT_CHECK( gp.empty());
350 CPPUNIT_ASSERT( l.empty());
351 CPPUNIT_CHECK( !l.get_with(gp, 3.4f, other_less()));
352 CPPUNIT_CHECK( gp.empty());
353 CPPUNIT_CHECK( !l.extract_with( gp, 3.4f, other_less()));
354 CPPUNIT_CHECK( gp.empty());
358 template <class OrdList>
364 static int const nLimit = 20;
366 typedef typename OrdList::rcu_lock rcu_lock;
367 typedef typename OrdList::value_type value_type;
368 typedef typename OrdList::gc rcu_type;
372 for (int i = 0; i < nLimit; ++i)
374 std::random_shuffle( a, a + nLimit );
377 for ( int i = 0; i < nLimit; ++i )
378 CPPUNIT_ASSERT( l.insert( a[i], a[i]*2 ) );
380 typename OrdList::exempt_ptr ep;
382 for ( int i = 0; i < nLimit; ++i ) {
385 value_type * pGet = l.get( a[i] );
386 CPPUNIT_ASSERT( pGet != nullptr );
387 CPPUNIT_CHECK( pGet->first == a[i] );
388 CPPUNIT_CHECK( pGet->second.m_val == a[i] * 2 );
390 CPPUNIT_ASSERT( l.extract( ep, a[i] ));
391 CPPUNIT_ASSERT( !ep.empty() );
392 CPPUNIT_CHECK( ep->first == a[i] );
393 CPPUNIT_CHECK( (*ep).second.m_val == a[i] * 2 );
398 CPPUNIT_CHECK( l.get( a[i] ) == nullptr );
399 CPPUNIT_CHECK( !l.extract( ep, a[i] ));
400 CPPUNIT_CHECK( ep.empty() );
403 CPPUNIT_ASSERT( l.empty() );
407 CPPUNIT_CHECK( l.get( a[0] ) == nullptr );
408 CPPUNIT_CHECK( !l.extract( ep, a[0] ) );
409 CPPUNIT_CHECK( ep.empty() );
412 // extract_with/get_with
413 for ( int i = 0; i < nLimit; ++i ) {
414 CPPUNIT_ASSERT( l.insert( a[i], a[i]*2 ) );
417 for ( int i = 0; i < nLimit; ++i ) {
418 float itm = a[i] + 0.3f;
421 value_type * pGet = l.get_with( itm, other_less() );
422 CPPUNIT_ASSERT( pGet != nullptr );
423 CPPUNIT_CHECK( pGet->first == a[i] );
424 CPPUNIT_CHECK( pGet->second.m_val == a[i] * 2 );
426 CPPUNIT_ASSERT( l.extract_with( ep, itm, other_less() ));
427 CPPUNIT_ASSERT( !ep.empty() );
428 CPPUNIT_CHECK( ep->first == a[i] );
429 CPPUNIT_CHECK( ep->second.m_val == a[i] * 2 );
434 CPPUNIT_CHECK( l.get_with( itm, other_less()) == nullptr );
435 CPPUNIT_CHECK( !l.extract_with( ep, itm, other_less() ));
436 CPPUNIT_CHECK( ep.empty() );
439 CPPUNIT_ASSERT( l.empty() );
443 CPPUNIT_CHECK( l.get_with( 3.14f, other_less() ) == nullptr );
444 CPPUNIT_CHECK( !l.extract_with( ep, 3.14f, other_less() ));
445 CPPUNIT_CHECK( ep.empty() );
450 template <class OrdList>
453 typedef typename OrdList::value_type value_type;
454 typedef typename OrdList::iterator iterator;
460 CPPUNIT_ASSERT( l.empty() );
462 // insert / find test
463 CPPUNIT_ASSERT( l.find( 100 ) == l.end() );
464 CPPUNIT_ASSERT( l.insert( 100 ) != l.end() );
465 CPPUNIT_ASSERT( !l.empty() );
467 CPPUNIT_ASSERT( it != l.end() );
468 CPPUNIT_ASSERT( it.key() == 100 );
469 CPPUNIT_ASSERT( it.val().m_val == 0 );
471 CPPUNIT_ASSERT( l.find_with( 50, lt<key_type>() ) == l.end() );
472 CPPUNIT_ASSERT( l.insert( 50, 500 ) != l.end());
474 CPPUNIT_ASSERT( it != l.end() );
475 CPPUNIT_ASSERT( it.key() == 50 );
476 CPPUNIT_ASSERT( it.val().m_val == 500 );
478 CPPUNIT_ASSERT( l.insert( 50, 5 ) == l.end() );
480 CPPUNIT_ASSERT( it != l.end() );
481 CPPUNIT_ASSERT( it.key() == 50 );
482 CPPUNIT_ASSERT( it.val().m_val == 500 );
483 CPPUNIT_ASSERT( !l.empty() );
485 CPPUNIT_ASSERT( l.find( 150 ) == l.end() );
486 CPPUNIT_ASSERT( l.insert_key( 150, insert_functor() ) != l.end() );
488 CPPUNIT_ASSERT( it != l.end() );
489 CPPUNIT_ASSERT( it.key() == 150 );
490 CPPUNIT_ASSERT( it.val().m_val == 1500 );
492 CPPUNIT_ASSERT( it != l.end() );
493 CPPUNIT_ASSERT( it.key() == 100 );
494 CPPUNIT_ASSERT( it.val().m_val == 0 );
496 CPPUNIT_ASSERT( it != l.end() );
497 CPPUNIT_ASSERT( it.key() == 50 );
498 CPPUNIT_ASSERT( it.val().m_val == 500 );
501 CPPUNIT_ASSERT( it != l.end() );
502 CPPUNIT_ASSERT( it.key() == 50 );
503 CPPUNIT_ASSERT( it.val().m_val == 25 );
504 CPPUNIT_ASSERT( !l.empty() );
506 // ensure existing item
507 std::pair<iterator, bool> ensureResult;
508 ensureResult = l.ensure( 100 );
509 CPPUNIT_ASSERT( !ensureResult.second );
510 CPPUNIT_ASSERT( ensureResult.first.key() == 100 );
511 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 0 );
512 ensureResult.first.val().m_val = 5;
514 CPPUNIT_ASSERT( it != l.end() );
515 CPPUNIT_ASSERT( it.key() == 100 );
516 CPPUNIT_ASSERT( it.val().m_val == 5 );
518 CPPUNIT_ASSERT( !l.empty() );
521 ensureResult = l.ensure( 1000 );
522 CPPUNIT_ASSERT( ensureResult.second );
523 CPPUNIT_ASSERT( ensureResult.first.key() == 1000 );
524 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 0 );
525 ensureResult.first.val().m_val = 33;
526 ensureResult = l.ensure( 1000 );
527 CPPUNIT_ASSERT( !ensureResult.second );
528 CPPUNIT_ASSERT( ensureResult.first.key() == 1000 );
529 CPPUNIT_ASSERT( ensureResult.first.val().m_val == 33 );
533 CPPUNIT_ASSERT( l.empty() );
536 CPPUNIT_ASSERT( l.emplace( 501 ) != l.end());
537 CPPUNIT_ASSERT( l.emplace( 251, 152 ) != l.end());
539 // insert failed - such key exists
540 CPPUNIT_ASSERT( l.emplace( 501, 2 ) == l.end());
541 CPPUNIT_ASSERT( l.emplace( 251, 10) == l.end());
544 CPPUNIT_ASSERT( it != l.end() );
545 CPPUNIT_ASSERT( it.key() == 501 );
546 CPPUNIT_ASSERT( it.val().m_val == 0 );
549 CPPUNIT_ASSERT( it != l.end() );
550 CPPUNIT_ASSERT( it.key() == 251 );
551 CPPUNIT_ASSERT( it.val().m_val == 152 );
554 CPPUNIT_ASSERT( l.empty() );
559 for ( int i = 0; i < nCount; ++i )
560 CPPUNIT_ASSERT( l.insert(i, i * 2 ) != l.end() );
563 typename OrdList::iterator it( l.begin() );
564 typename OrdList::const_iterator cit( l.cbegin() );
565 CPPUNIT_CHECK( it == cit );
566 CPPUNIT_CHECK( it != l.end() );
567 CPPUNIT_CHECK( it != l.cend() );
568 CPPUNIT_CHECK( cit != l.end() );
569 CPPUNIT_CHECK( cit != l.cend() );
571 CPPUNIT_CHECK( it != cit );
572 CPPUNIT_CHECK( it != l.end() );
573 CPPUNIT_CHECK( it != l.cend() );
574 CPPUNIT_CHECK( cit != l.end() );
575 CPPUNIT_CHECK( cit != l.cend() );
577 CPPUNIT_CHECK( it == cit );
578 CPPUNIT_CHECK( it != l.end() );
579 CPPUNIT_CHECK( it != l.cend() );
580 CPPUNIT_CHECK( cit != l.end() );
581 CPPUNIT_CHECK( cit != l.cend() );
585 for ( typename OrdList::iterator iter = l.begin(), itEnd = l.end(); iter != itEnd; ++iter, ++i ) {
586 CPPUNIT_ASSERT( iter.key() == i );
587 CPPUNIT_ASSERT( iter->first == i );
588 CPPUNIT_ASSERT( (*iter).first == i );
590 CPPUNIT_ASSERT( iter.val().m_val == i * 2 );
591 CPPUNIT_ASSERT( iter->second.m_val == i * 2 );
592 CPPUNIT_ASSERT( (*iter).second.m_val == i * 2 );
594 iter.val().m_val = i * 3;
597 // Check that we have visited all items
598 for ( int i = 0; i < nCount; ++i ) {
600 CPPUNIT_ASSERT( it != l.end() );
601 CPPUNIT_ASSERT( it.key() == i );
602 CPPUNIT_ASSERT( it.val().m_val == i * 3 );
606 CPPUNIT_ASSERT( l.empty() );
609 for ( int i = 0; i < nCount; ++i )
610 CPPUNIT_ASSERT( l.insert(i, i * 7) != l.end() );
613 const OrdList& rl = l;
614 for ( typename OrdList::const_iterator iter = rl.begin(), itEnd = rl.end(); iter != itEnd; ++iter, ++i ) {
615 CPPUNIT_ASSERT( iter.key() == i );
616 CPPUNIT_ASSERT( iter->first == i );
617 CPPUNIT_ASSERT( (*iter).first == i );
619 CPPUNIT_ASSERT( iter.val().m_val == i * 7 );
620 CPPUNIT_ASSERT( iter->second.m_val == i * 7 );
621 CPPUNIT_ASSERT( (*iter).second.m_val == i * 7 );
622 // it.val().m_val = i * 3 ; // error: const-iterator
626 CPPUNIT_ASSERT( l.empty() );
644 void RCU_GPI_cmpmix();
649 void RCU_GPB_cmpmix();
654 void RCU_GPT_cmpmix();
659 void RCU_SHB_cmpmix();
664 void RCU_SHT_cmpmix();
672 CPPUNIT_TEST_SUITE(LazyKVListTestHeader)
674 CPPUNIT_TEST(HP_less)
675 CPPUNIT_TEST(HP_cmpmix)
678 CPPUNIT_TEST(DHP_cmp)
679 CPPUNIT_TEST(DHP_less)
680 CPPUNIT_TEST(DHP_cmpmix)
683 CPPUNIT_TEST(RCU_GPI_cmp)
684 CPPUNIT_TEST(RCU_GPI_less)
685 CPPUNIT_TEST(RCU_GPI_cmpmix)
686 CPPUNIT_TEST(RCU_GPI_ic)
688 CPPUNIT_TEST(RCU_GPB_cmp)
689 CPPUNIT_TEST(RCU_GPB_less)
690 CPPUNIT_TEST(RCU_GPB_cmpmix)
691 CPPUNIT_TEST(RCU_GPB_ic)
693 CPPUNIT_TEST(RCU_GPT_cmp)
694 CPPUNIT_TEST(RCU_GPT_less)
695 CPPUNIT_TEST(RCU_GPT_cmpmix)
696 CPPUNIT_TEST(RCU_GPT_ic)
698 CPPUNIT_TEST(RCU_SHB_cmp)
699 CPPUNIT_TEST(RCU_SHB_less)
700 CPPUNIT_TEST(RCU_SHB_cmpmix)
701 CPPUNIT_TEST(RCU_SHB_ic)
703 CPPUNIT_TEST(RCU_SHT_cmp)
704 CPPUNIT_TEST(RCU_SHT_less)
705 CPPUNIT_TEST(RCU_SHT_cmpmix)
706 CPPUNIT_TEST(RCU_SHT_ic)
708 CPPUNIT_TEST(NOGC_cmp)
709 CPPUNIT_TEST(NOGC_less)
710 CPPUNIT_TEST(NOGC_cmpmix)
711 CPPUNIT_TEST(NOGC_ic)
712 CPPUNIT_TEST_SUITE_END()
715 } // namespace ordlist