From 4dc9dc7641578b239c2ca7db3b01e9e5d255313e Mon Sep 17 00:00:00 2001 From: Mike Krinkin Date: Sat, 28 Mar 2015 16:14:16 +0300 Subject: [PATCH] Add michael map test with unordered lazy list. --- tests/test-hdr/map/hdr_map.h | 154 ++++++++++++++++++ .../map/hdr_michael_map_lazy_nogc.cpp | 24 +++ 2 files changed, 178 insertions(+) diff --git a/tests/test-hdr/map/hdr_map.h b/tests/test-hdr/map/hdr_map.h index 611533ad..327090db 100644 --- a/tests/test-hdr/map/hdr_map.h +++ b/tests/test-hdr/map/hdr_map.h @@ -635,6 +635,158 @@ namespace map { } } + template + void test_int_nogc_unordered() + { + typedef typename Map::iterator iterator; + typedef typename Map::const_iterator const_iterator; + + { + Map m( 52, 4 ); + + CPPUNIT_ASSERT( m.empty() ); + CPPUNIT_ASSERT( check_size( m, 0 )); + + CPPUNIT_ASSERT( m.find(10) == m.end() ); + iterator it = m.insert( 10 ); + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( !m.empty() ); + CPPUNIT_ASSERT( check_size( m, 1 )); + CPPUNIT_ASSERT( m.find(10) == it ); + CPPUNIT_ASSERT( it->first == 10 ); + CPPUNIT_ASSERT( it->second.m_val == 0 ); + + CPPUNIT_ASSERT( m.find(100) == m.end() ); + it = m.insert( 100, 200 ); + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( !m.empty() ); + CPPUNIT_ASSERT( check_size( m, 2 )); + CPPUNIT_ASSERT( m.find_with(100, equal()) == it ); + CPPUNIT_ASSERT( it->first == 100 ); + CPPUNIT_ASSERT( it->second.m_val == 200 ); + + CPPUNIT_ASSERT( m.find(55) == m.end() ); + it = m.insert_with( 55, insert_functor() ); + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( !m.empty() ); + CPPUNIT_ASSERT( check_size( m, 3 )); + CPPUNIT_ASSERT( m.find(55) == it ); + CPPUNIT_ASSERT( it->first == 55 ); + CPPUNIT_ASSERT( it->second.m_val == 55 * 3 ); + + CPPUNIT_ASSERT( m.insert( 55 ) == m.end() ); + CPPUNIT_ASSERT( m.insert( 55, 10 ) == m.end() ); + CPPUNIT_ASSERT( m.insert_with( 55, insert_functor()) == m.end() ); + + CPPUNIT_ASSERT( m.find(10) != m.end() ); + std::pair ensureResult = m.ensure( 10 ); + CPPUNIT_ASSERT( ensureResult.first != m.end() ); + CPPUNIT_ASSERT( !ensureResult.second ); + CPPUNIT_ASSERT( !m.empty() ); + ensureResult.first->second.m_val = ensureResult.first->first * 5; + CPPUNIT_ASSERT( check_size( m, 3 )); + CPPUNIT_ASSERT( m.find(10) == ensureResult.first ); + it = m.find(10); + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( it->second.m_val == 50 ); + + CPPUNIT_ASSERT( m.find(120) == m.end() ); + ensureResult = m.ensure( 120 ); + CPPUNIT_ASSERT( ensureResult.first != m.end() ); + CPPUNIT_ASSERT( ensureResult.second ); + CPPUNIT_ASSERT( !m.empty() ); + CPPUNIT_ASSERT( check_size( m, 4 )); + ensureResult.first->second.m_val = ensureResult.first->first * 5; + CPPUNIT_ASSERT( m.find_with(120, equal()) == ensureResult.first ); + it = m.find_with(120, equal()); + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( it->second.m_val == 120 * 5 ); + CPPUNIT_ASSERT( m.find_with(120, equal()) == m.find(120) ); + + // emplace test + it = m.emplace( 151 ) ; // key = 151, val = 0 + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( it->first == 151 ); + CPPUNIT_ASSERT( it->second.m_val == 0 ); + + it = m.emplace( 174, 471 ) ; // key == 174, val = 471 + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( it->first == 174 ); + CPPUNIT_ASSERT( it->second.m_val == 471 ); + + it = m.emplace( 190, value_type(91)) ; // key == 190, val = 19 + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( it->first == 190 ); + CPPUNIT_ASSERT( it->second.m_val == 91 ); + + it = m.emplace( 151, 1051 ); + CPPUNIT_ASSERT( it == m.end()); + + it = m.find( 174 ); + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( it->first == 174 ); + CPPUNIT_ASSERT( it->second.m_val == 471 ); + + it = m.find( 190 ); + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( it->first == 190 ); + CPPUNIT_ASSERT( it->second.m_val == 91 ); + + it = m.find( 151 ); + CPPUNIT_ASSERT( it != m.end() ); + CPPUNIT_ASSERT( it->first == 151 ); + CPPUNIT_ASSERT( it->second.m_val == 0 ); + } + + // iterator test + + { + Map m( 52, 4 ); + + for ( int i = 0; i < 500; ++i ) { + CPPUNIT_ASSERT( m.insert( i, i * 2 ) != m.end() ); + } + CPPUNIT_ASSERT( check_size( m, 500 )); + + { + typename Map::iterator it( m.begin() ); + typename Map::const_iterator cit( m.cbegin() ); + CPPUNIT_CHECK( it == cit ); + CPPUNIT_CHECK( it != m.end() ); + CPPUNIT_CHECK( it != m.cend() ); + CPPUNIT_CHECK( cit != m.end() ); + CPPUNIT_CHECK( cit != m.cend() ); + ++it; + CPPUNIT_CHECK( it != cit ); + CPPUNIT_CHECK( it != m.end() ); + CPPUNIT_CHECK( it != m.cend() ); + CPPUNIT_CHECK( cit != m.end() ); + CPPUNIT_CHECK( cit != m.cend() ); + ++cit; + CPPUNIT_CHECK( it == cit ); + CPPUNIT_CHECK( it != m.end() ); + CPPUNIT_CHECK( it != m.cend() ); + CPPUNIT_CHECK( cit != m.end() ); + CPPUNIT_CHECK( cit != m.cend() ); + } + + + for ( iterator it = m.begin(), itEnd = m.end(); it != itEnd; ++it ) { + iterator it2 = it; + CPPUNIT_CHECK( it2 == it ); + CPPUNIT_CHECK( it2 != itEnd ); + CPPUNIT_ASSERT( it->first * 2 == (*it).second.m_val ); + it->second = it->first; + } + + Map const& refMap = m; + for ( const_iterator it = refMap.begin(), itEnd = refMap.end(); it != itEnd; ++it ) { + CPPUNIT_ASSERT( it->first == it->second.m_val ); + CPPUNIT_ASSERT( (*it).first == (*it).second.m_val ); + } + } + } + template void test_iter() { @@ -752,6 +904,7 @@ namespace map { void Lazy_nogc_cmp(); void Lazy_nogc_less(); + void Lazy_nogc_equal(); void Lazy_nogc_cmpmix(); void Split_HP_cmp(); @@ -897,6 +1050,7 @@ namespace map { CPPUNIT_TEST(Lazy_nogc_cmp) CPPUNIT_TEST(Lazy_nogc_less) + CPPUNIT_TEST(Lazy_nogc_equal) CPPUNIT_TEST(Lazy_nogc_cmpmix) CPPUNIT_TEST(Split_HP_cmp) diff --git a/tests/test-hdr/map/hdr_michael_map_lazy_nogc.cpp b/tests/test-hdr/map/hdr_michael_map_lazy_nogc.cpp index eba7232a..9c9fb03c 100644 --- a/tests/test-hdr/map/hdr_michael_map_lazy_nogc.cpp +++ b/tests/test-hdr/map/hdr_michael_map_lazy_nogc.cpp @@ -26,6 +26,12 @@ namespace map { typedef HashMapHdrTest::cmp compare; typedef HashMapHdrTest::less less; }; + + struct nogc_equal_traits: public cc::lazy_list::traits + { + typedef HashMapHdrTest::equal equal_to; + static const bool sort = false; + }; } void HashMapHdrTest::Lazy_nogc_cmp() @@ -64,6 +70,24 @@ namespace map { test_int_nogc< opt_map >(); } + void HashMapHdrTest::Lazy_nogc_equal() + { + typedef cc::LazyKVList< cds::gc::nogc, int, HashMapHdrTest::value_type, nogc_equal_traits > list; + + // traits-based version + typedef cc::MichaelHashMap< cds::gc::nogc, list, map_traits > map; + test_int_nogc_unordered< map >(); + + // option-based version + typedef cc::MichaelHashMap< cds::gc::nogc, list, + cc::michael_map::make_traits< + cc::opt::hash< hash_int > + ,cc::opt::item_counter< simple_item_counter > + >::type + > opt_map; + test_int_nogc_unordered< opt_map >(); + } + void HashMapHdrTest::Lazy_nogc_cmpmix() { typedef cc::LazyKVList< cds::gc::nogc, int, HashMapHdrTest::value_type, nogc_cmpmix_traits > list; -- 2.34.1