3 #include "cppunit/thread.h"
4 #include "queue/queue_type.h"
5 #include "queue/queue_defs.h"
10 The test checks the behaviour of bounded queue when it is almost full.
11 Many algorithms says the queue is full when it is not, and vice versa.
15 #define TEST_BOUNDED( Q, V ) void Q() { test< Types<V>::Q >(); }
17 namespace ns_BoundedQueue_Fullness {
18 static size_t s_nThreadCount = 8;
19 static size_t s_nQueueSize = 1024;
20 static size_t s_nPassCount = 1000000;
22 using namespace ns_BoundedQueue_Fullness;
24 class BoundedQueue_Fullness: public CppUnitMini::TestCase
26 template <class Queue>
27 class Thread: public CppUnitMini::TestThread
29 virtual TestThread * clone()
31 return new Thread( *this );
40 Thread( CppUnitMini::ThreadPool& pool, Queue& q )
41 : CppUnitMini::TestThread( pool )
45 : CppUnitMini::TestThread( src )
46 , m_Queue( src.m_Queue )
49 BoundedQueue_Fullness& getTest()
51 return reinterpret_cast<BoundedQueue_Fullness&>( m_Pool.m_Test );
56 cds::threading::Manager::attachThread();
60 cds::threading::Manager::detachThread();
65 m_fTime = m_Timer.duration();
69 for ( size_t i = 0; i < s_nPassCount; ++i ) {
70 if ( !m_Queue.push( i ))
73 if ( !m_Queue.pop( item ))
76 m_fTime = m_Timer.duration() - m_fTime;
81 template <class Queue>
82 void analyze( CppUnitMini::ThreadPool& pool, Queue& testQueue )
85 size_t nPushError = 0;
87 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
88 Thread<Queue> * pThread = reinterpret_cast<Thread<Queue> *>(*it);
89 fTime += pThread->m_fTime;
90 nPushError += pThread->m_nPushError;
91 nPopError += pThread->m_nPopError;
93 CPPUNIT_MSG( " Duration=" << (fTime / s_nThreadCount) );
94 CPPUNIT_MSG( " Errors: push=" << nPushError << ", pop=" << nPopError );
95 CPPUNIT_CHECK( !testQueue.empty());
96 CPPUNIT_CHECK( nPushError == 0 );
97 CPPUNIT_CHECK( nPopError == 0 );
100 template <class Queue>
103 Queue testQueue( s_nQueueSize );
105 CppUnitMini::ThreadPool pool( *this );
106 pool.add( new Thread<Queue>( pool, testQueue ), s_nThreadCount );
108 size_t nSize = testQueue.capacity() - s_nThreadCount;
109 for ( size_t i = 0; i < nSize; ++i )
112 CPPUNIT_MSG( " Thread count=" << s_nThreadCount << ", push/pop pairs=" << s_nPassCount
114 << ", queue capacity=" << testQueue.capacity() << " ...");
117 analyze( pool, testQueue );
119 CPPUNIT_MSG( testQueue.statistics() );
121 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
122 s_nThreadCount = cfg.getULong("ThreadCount", 8 );
123 s_nQueueSize = cfg.getULong("QueueSize", 1024 );
124 s_nPassCount = cfg.getULong( "PassCount", 1000000 );
128 CDSUNIT_DECLARE_TsigasCycleQueue( size_t )
129 CDSUNIT_DECLARE_VyukovMPMCCycleQueue( size_t )
131 CPPUNIT_TEST_SUITE( BoundedQueue_Fullness )
132 CDSUNIT_TEST_TsigasCycleQueue
133 CDSUNIT_TEST_VyukovMPMCCycleQueue
134 CPPUNIT_TEST_SUITE_END();
139 CPPUNIT_TEST_SUITE_REGISTRATION(queue::BoundedQueue_Fullness );