2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "cppunit/thread.h"
32 #include "queue/queue_type.h"
33 #include "queue/queue_defs.h"
38 The test checks the behaviour of bounded queue when it is almost full.
39 Many algorithms says the queue is full when it is not, and vice versa.
43 #define TEST_BOUNDED( Q, V ) void Q() { test< Types<V>::Q >(); }
45 namespace ns_BoundedQueue_Fullness {
46 static size_t s_nThreadCount = 8;
47 static size_t s_nQueueSize = 1024;
48 static size_t s_nPassCount = 1000000;
50 using namespace ns_BoundedQueue_Fullness;
52 class BoundedQueue_Fullness: public CppUnitMini::TestCase
54 template <class Queue>
55 class Thread: public CppUnitMini::TestThread
57 virtual TestThread * clone()
59 return new Thread( *this );
68 Thread( CppUnitMini::ThreadPool& pool, Queue& q )
69 : CppUnitMini::TestThread( pool )
73 : CppUnitMini::TestThread( src )
74 , m_Queue( src.m_Queue )
77 BoundedQueue_Fullness& getTest()
79 return reinterpret_cast<BoundedQueue_Fullness&>( m_Pool.m_Test );
84 cds::threading::Manager::attachThread();
88 cds::threading::Manager::detachThread();
93 m_fTime = m_Timer.duration();
97 for ( size_t i = 0; i < s_nPassCount; ++i ) {
98 if ( !m_Queue.push( i ))
101 if ( !m_Queue.pop( item ))
104 m_fTime = m_Timer.duration() - m_fTime;
109 template <class Queue>
110 void analyze( CppUnitMini::ThreadPool& pool, Queue& testQueue )
113 size_t nPushError = 0;
114 size_t nPopError = 0;
115 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
116 Thread<Queue> * pThread = reinterpret_cast<Thread<Queue> *>(*it);
117 fTime += pThread->m_fTime;
118 nPushError += pThread->m_nPushError;
119 nPopError += pThread->m_nPopError;
121 CPPUNIT_MSG( " Duration=" << (fTime / s_nThreadCount) );
122 CPPUNIT_MSG( " Errors: push=" << nPushError << ", pop=" << nPopError );
123 CPPUNIT_CHECK( !testQueue.empty());
124 CPPUNIT_CHECK( nPushError == 0 );
125 CPPUNIT_CHECK( nPopError == 0 );
128 template <class Queue>
131 Queue testQueue( s_nQueueSize );
133 CppUnitMini::ThreadPool pool( *this );
134 pool.add( new Thread<Queue>( pool, testQueue ), s_nThreadCount );
136 size_t nSize = testQueue.capacity() - s_nThreadCount;
137 for ( size_t i = 0; i < nSize; ++i )
140 CPPUNIT_MSG( " Thread count=" << s_nThreadCount << ", push/pop pairs=" << s_nPassCount
142 << ", queue capacity=" << testQueue.capacity() << " ...");
145 analyze( pool, testQueue );
147 CPPUNIT_MSG( testQueue.statistics() );
149 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
150 s_nThreadCount = cfg.getULong("ThreadCount", 8 );
151 s_nQueueSize = cfg.getULong("QueueSize", 1024 );
152 s_nPassCount = cfg.getULong( "PassCount", 1000000 );
156 CDSUNIT_DECLARE_TsigasCycleQueue( size_t )
157 CDSUNIT_DECLARE_VyukovMPMCCycleQueue( size_t )
159 CPPUNIT_TEST_SUITE( BoundedQueue_Fullness )
160 CDSUNIT_TEST_TsigasCycleQueue
161 CDSUNIT_TEST_VyukovMPMCCycleQueue
162 CPPUNIT_TEST_SUITE_END();
167 CPPUNIT_TEST_SUITE_REGISTRATION(queue::BoundedQueue_Fullness );