Added copyright and license
[libcds.git] / tests / test-hdr / queue / hdr_fcqueue.h
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 #ifndef CDSTEST_HDR_FCQUEUE_H
32 #define CDSTEST_HDR_FCQUEUE_H
33
34 #include "cppunit/cppunit_proxy.h"
35 #include <cds/details/defs.h>
36
37 namespace queue {
38
39     //
40     // Test queue operation in single thread mode
41     //
42     class HdrFCQueue: public CppUnitMini::TestCase
43     {
44     protected:
45         template <class Queue>
46         void testNoItemCounter()
47         {
48             Queue   q;
49             test_with( q );
50             test_emplace( q );
51         }
52
53         template <class Queue>
54         void test_with( Queue& q )
55         {
56             int     it;
57             int     nPrev;
58
59             for ( size_t i = 0; i < 3; ++i ) {
60                 CPPUNIT_ASSERT( q.empty() );
61 #ifndef _DEBUG
62                 CPPUNIT_ASSERT( q.size() == 0 );
63 #endif
64                 CPPUNIT_ASSERT( q.enqueue( 1 ) );
65                 CPPUNIT_ASSERT( !q.empty() );
66                 CPPUNIT_ASSERT( q.push( 10 ) );
67                 CPPUNIT_ASSERT( !q.empty() );
68 #ifndef _DEBUG
69                 CPPUNIT_ASSERT( q.size() == 0 )     ;   // no queue's item counter!
70 #endif
71
72                 it = -1;
73                 CPPUNIT_ASSERT( q.pop( it ) );
74                 CPPUNIT_ASSERT( it == 1 );
75                 CPPUNIT_ASSERT( !q.empty() );
76                 CPPUNIT_ASSERT( q.dequeue( it ) );
77                 CPPUNIT_ASSERT( it == 10 );
78 #ifndef _DEBUG
79                 CPPUNIT_ASSERT( q.size() == 0 );
80 #endif
81                 CPPUNIT_ASSERT( q.empty() );
82                 it += 2009;
83                 nPrev = it;
84                 CPPUNIT_ASSERT( !q.dequeue( it ) );
85                 CPPUNIT_ASSERT( it == nPrev )       ;   // it must not be changed!
86             }
87         }
88
89         template <class Queue>
90         void test_emplace( Queue& q )
91         {
92             int     it;
93             for ( size_t i = 0; i < 3; ++i ) {
94                 CPPUNIT_ASSERT( q.emplace( static_cast<int>( i * 42 )) );
95                 CPPUNIT_ASSERT( !q.empty() );
96                 it = -1;
97                 CPPUNIT_ASSERT( q.pop( it ));
98                 CPPUNIT_ASSERT( it == static_cast<int>( i * 42 ));
99                 CPPUNIT_ASSERT( q.empty() );
100             }
101         }
102
103         template <class Queue>
104         void testWithItemCounter()
105         {
106             Queue   q;
107             test_ic_with( q );
108             test_emplace_ic( q );
109         }
110
111         template <class Queue>
112         void testFCQueue()
113         {
114             Queue   q;
115             test_ic_with( q );
116         }
117
118         template <class Queue>
119         void test_ic_with( Queue& q )
120         {
121             int     it;
122             int     nPrev;
123
124             for ( size_t i = 0; i < 3; ++i ) {
125                 CPPUNIT_ASSERT( q.empty() );
126                 CPPUNIT_ASSERT( q.size() == 0 );
127                 CPPUNIT_ASSERT( q.enqueue( 1 ) );
128                 CPPUNIT_ASSERT( q.size() == 1 );
129                 CPPUNIT_ASSERT( !q.empty() );
130                 CPPUNIT_ASSERT( q.push( 10 ) );
131                 CPPUNIT_ASSERT( !q.empty() );
132                 CPPUNIT_ASSERT( q.size() == 2 );
133
134                 it = -1;
135                 CPPUNIT_ASSERT( q.pop( it ) );
136                 CPPUNIT_ASSERT( it == 1 );
137                 CPPUNIT_ASSERT( !q.empty() );
138                 CPPUNIT_ASSERT( q.size() == 1 );
139                 CPPUNIT_ASSERT( q.dequeue( it ) );
140                 CPPUNIT_ASSERT( it == 10 );
141                 CPPUNIT_ASSERT( q.size() == 0 );
142                 CPPUNIT_ASSERT( q.empty() );
143                 CPPUNIT_ASSERT( q.size() == 0 );
144                 it += 2009;
145                 nPrev = it;
146                 CPPUNIT_ASSERT( !q.dequeue( it ) );
147                 CPPUNIT_ASSERT( it == nPrev )       ;   // it must not be changed!
148
149                 CPPUNIT_ASSERT( q.empty() );
150                 CPPUNIT_ASSERT( q.size() == 0 );
151             }
152         }
153
154         template <class Queue>
155         void test_emplace_ic( Queue& q )
156         {
157             int     it = 0;
158             for ( size_t i = 0; i < 3; ++i ) {
159                 CPPUNIT_ASSERT( q.emplace( (int) i * 10 ) );
160                 CPPUNIT_ASSERT( !q.empty() );
161                 CPPUNIT_ASSERT( q.size() == 1 );
162                 CPPUNIT_ASSERT( q.pop( it ));
163                 CPPUNIT_ASSERT( it == (int) i * 10 );
164                 CPPUNIT_ASSERT( q.empty() );
165                 CPPUNIT_ASSERT( q.size() == 0 );
166             }
167         }
168
169     public:
170         void FCQueue_deque();
171         void FCQueue_deque_elimination();
172         void FCQueue_deque_mutex();
173         void FCQueue_deque_stat();
174         void FCQueue_list();
175         void FCQueue_list_elimination();
176         void FCQueue_list_mutex();
177         void FCQueue_list_stat();
178
179         CPPUNIT_TEST_SUITE(HdrFCQueue)
180             CPPUNIT_TEST(FCQueue_deque)
181             CPPUNIT_TEST(FCQueue_deque_elimination)
182             CPPUNIT_TEST(FCQueue_deque_mutex)
183             CPPUNIT_TEST(FCQueue_deque_stat)
184             CPPUNIT_TEST(FCQueue_list)
185             CPPUNIT_TEST(FCQueue_list_elimination)
186             CPPUNIT_TEST(FCQueue_list_mutex)
187             CPPUNIT_TEST(FCQueue_list_stat)
188         CPPUNIT_TEST_SUITE_END();
189
190     };
191 } // namespace queue
192
193 #endif // #ifndef CDSTEST_HDR_FCQUEUE_H