Improved SkipListSet unit tests
[libcds.git] / test / unit / set / test_skiplist_rcu.h
index ca931319e2c03559c208bd28440935fe20d2a716..d5b3864ea3cdbbb9289ef6f637bae1ede31adfb2 100644 (file)
@@ -43,6 +43,78 @@ public:
     typedef cds::urcu::gc<RCU> rcu_type;
 
 protected:
+    template <typename Set>
+    void test( Set& s )
+    {
+        // Precondition: set is empty
+        // Postcondition: set is empty
+
+        base_class::test( s );
+
+        ASSERT_TRUE( s.empty() );
+        ASSERT_CONTAINER_SIZE( s, 0 );
+
+        typedef typename Set::value_type value_type;
+
+        size_t const nSetSize = base_class::kSize;
+        std::vector< value_type > data;
+        std::vector< size_t> indices;
+        data.reserve( nSetSize );
+        indices.reserve( nSetSize );
+        for ( size_t key = 0; key < nSetSize; ++key ) {
+            data.push_back( value_type( static_cast<int>(key) ) );
+            indices.push_back( key );
+        }
+        shuffle( indices.begin(), indices.end() );
+
+        for ( auto i : indices ) {
+            ASSERT_TRUE( s.insert( data[i] ) );
+        }
+        ASSERT_FALSE( s.empty() );
+        ASSERT_CONTAINER_SIZE( s, nSetSize );
+
+        typedef typename Set::exempt_ptr exempt_ptr;
+        exempt_ptr xp;
+
+        // extract_min
+        size_t nCount = 0;
+        int nKey = -1;
+        while ( !s.empty() ) {
+            xp = s.extract_min();
+            ASSERT_FALSE( !xp );
+            EXPECT_EQ( nKey + 1, xp->key() );
+            ++nCount;
+            nKey = xp->key();
+        }
+        xp.release();
+        EXPECT_EQ( nCount, nSetSize );
+
+        ASSERT_TRUE( s.empty() );
+        ASSERT_CONTAINER_SIZE( s, 0 );
+
+        // extract_max
+        for ( auto i : indices ) {
+            ASSERT_TRUE( s.insert( data[i] ) );
+        }
+        ASSERT_FALSE( s.empty() );
+        ASSERT_CONTAINER_SIZE( s, nSetSize );
+
+        nCount = 0;
+        nKey = nSetSize;
+        while ( !s.empty() ) {
+            xp = s.extract_max();
+            ASSERT_FALSE( !xp );
+            EXPECT_EQ( nKey - 1, xp->key() );
+            ++nCount;
+            nKey = xp->key();
+        }
+        xp.release();
+        EXPECT_EQ( nCount, nSetSize );
+
+        ASSERT_TRUE( s.empty() );
+        ASSERT_CONTAINER_SIZE( s, 0 );
+    }
+
     void SetUp()
     {
         RCU::Construct();