Add michael map test with unordered lazy list.
authorMike Krinkin <krinkin.m.u@gmail.com>
Sat, 28 Mar 2015 13:14:16 +0000 (16:14 +0300)
committerMike Krinkin <krinkin.m.u@gmail.com>
Sat, 28 Mar 2015 13:14:16 +0000 (16:14 +0300)
tests/test-hdr/map/hdr_map.h
tests/test-hdr/map/hdr_michael_map_lazy_nogc.cpp

index 611533ad6dacf6dc42999c3b05b4a2c74f663970..327090db17e15ed72a10f62f0973d1e5582f27f0 100644 (file)
@@ -635,6 +635,158 @@ namespace map {
             }
         }
 
+        template <class Map>
+        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<Map>() );
+                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<Map>()) == m.end() );
+
+                CPPUNIT_ASSERT( m.find(10) != m.end() );
+                std::pair<iterator, bool> 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 <class Map>
         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)
index eba7232a4ddf92ce43dbfbe6db33a50f5400c65b..9c9fb03c23030e95e70fcb126a905a09da5a4b1b 100644 (file)
@@ -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;