61bce4095aba97f37f226d993b7b99c868c0bdab
[libcds.git] / tests / unit / queue / bounded_queue_fulness.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.     
29 */
30
31 #include "cppunit/thread.h"
32 #include "queue/queue_type.h"
33 #include "queue/queue_defs.h"
34
35
36 /*
37     Bounded queue test.
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.
40 */
41 namespace queue {
42
43 #define TEST_BOUNDED( Q, V )    void Q() { test< Types<V>::Q >(); }
44
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;
49     }
50     using namespace ns_BoundedQueue_Fullness;
51
52     class BoundedQueue_Fullness: public CppUnitMini::TestCase
53     {
54         template <class Queue>
55         class Thread: public CppUnitMini::TestThread
56         {
57             virtual TestThread *    clone()
58             {
59                 return new Thread( *this );
60             }
61         public:
62             Queue&              m_Queue;
63             double              m_fTime;
64             size_t              m_nPushError;
65             size_t              m_nPopError;
66
67         public:
68             Thread( CppUnitMini::ThreadPool& pool, Queue& q )
69                 : CppUnitMini::TestThread( pool )
70                 , m_Queue( q )
71             {}
72             Thread( Thread& src )
73                 : CppUnitMini::TestThread( src )
74                 , m_Queue( src.m_Queue )
75             {}
76
77             BoundedQueue_Fullness&  getTest()
78             {
79                 return reinterpret_cast<BoundedQueue_Fullness&>( m_Pool.m_Test );
80             }
81
82             virtual void init()
83             {
84                 cds::threading::Manager::attachThread();
85             }
86             virtual void fini()
87             {
88                 cds::threading::Manager::detachThread();
89             }
90
91             virtual void test()
92             {
93                 m_fTime = m_Timer.duration();
94
95                 m_nPushError = 0;
96                 m_nPopError = 0;
97                 for ( size_t i = 0; i < s_nPassCount; ++i ) {
98                     if ( !m_Queue.push( i ))
99                         ++m_nPushError;
100                     size_t item;
101                     if ( !m_Queue.pop( item ))
102                         ++m_nPopError;
103                 }
104                 m_fTime = m_Timer.duration() - m_fTime;
105             }
106         };
107
108     protected:
109         template <class Queue>
110         void analyze( CppUnitMini::ThreadPool& pool, Queue& testQueue  )
111         {
112             double fTime = 0;
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;
120             }
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 );
126         }
127
128         template <class Queue>
129         void test()
130         {
131             Queue testQueue( s_nQueueSize );
132
133             CppUnitMini::ThreadPool pool( *this );
134             pool.add( new Thread<Queue>( pool, testQueue ), s_nThreadCount );
135
136             size_t nSize = testQueue.capacity() - s_nThreadCount;
137             for ( size_t i = 0; i < nSize; ++i )
138                 testQueue.push( i );
139
140             CPPUNIT_MSG( "   Thread count=" << s_nThreadCount << ", push/pop pairs=" << s_nPassCount
141
142                          << ", queue capacity=" << testQueue.capacity() << " ...");
143             pool.run();
144
145             analyze( pool, testQueue );
146
147             CPPUNIT_MSG( testQueue.statistics() );
148         }
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 );
153         }
154
155     protected:
156         CDSUNIT_DECLARE_TsigasCycleQueue( size_t )
157         CDSUNIT_DECLARE_VyukovMPMCCycleQueue( size_t )
158
159         CPPUNIT_TEST_SUITE( BoundedQueue_Fullness )
160             CDSUNIT_TEST_TsigasCycleQueue
161             CDSUNIT_TEST_VyukovMPMCCycleQueue
162         CPPUNIT_TEST_SUITE_END();
163     };
164
165 } // namespace queue
166
167 CPPUNIT_TEST_SUITE_REGISTRATION(queue::BoundedQueue_Fullness );