2023d420ed59099230c7f1852b800a5abb60e1ec
[libcds.git] / tests / test-hdr / queue / hdr_queue.h
1 //$$CDS-header$$
2
3 #ifndef __UNIT_QUEUE_SIMPLE_H
4 #define __UNIT_QUEUE_SIMPLE_H
5
6 #include "cppunit/cppunit_proxy.h"
7 #include <cds/details/defs.h>
8
9 namespace queue {
10
11     //
12     // Test queue operation in single thread mode
13     //
14     class Queue_TestHeader: public CppUnitMini::TestCase
15     {
16     protected:
17         template <class Queue>
18         void testNoItemCounter()
19         {
20             Queue   q;
21             test_with( q );
22             test_emplace( q );
23         }
24
25         template <class Queue>
26         void test_with( Queue& q )
27         {
28             int     it;
29             int     nPrev;
30
31             for ( size_t i = 0; i < 3; ++i ) {
32                 CPPUNIT_ASSERT( q.empty() );
33 #ifndef _DEBUG
34                 CPPUNIT_ASSERT( q.size() == 0 );
35 #endif
36                 CPPUNIT_ASSERT( q.enqueue( 1 ) );
37                 CPPUNIT_ASSERT( !q.empty() );
38                 CPPUNIT_ASSERT( q.push( 10 ) );
39                 CPPUNIT_ASSERT( !q.empty() );
40 #ifndef _DEBUG
41                 CPPUNIT_ASSERT( q.size() == 0 )     ;   // no queue's item counter!
42 #endif
43
44                 it = -1;
45                 CPPUNIT_ASSERT( q.pop( it ) );
46                 CPPUNIT_ASSERT( it == 1 );
47                 CPPUNIT_ASSERT( !q.empty() );
48                 CPPUNIT_ASSERT( q.dequeue( it ) );
49                 CPPUNIT_ASSERT( it == 10 );
50 #ifndef _DEBUG
51                 CPPUNIT_ASSERT( q.size() == 0 );
52 #endif
53                 CPPUNIT_ASSERT( q.empty() );
54                 it += 2009;
55                 nPrev = it;
56                 CPPUNIT_ASSERT( !q.dequeue( it ) );
57                 CPPUNIT_ASSERT( it == nPrev )       ;   // it must not be changed!
58             }
59         }
60
61         template <class Queue>
62         void test_emplace( Queue& q )
63         {
64             int     it;
65             for ( size_t i = 0; i < 3; ++i ) {
66                 CPPUNIT_ASSERT( q.emplace( static_cast<int>( i * 42 )) );
67                 CPPUNIT_ASSERT( !q.empty() );
68                 it = -1;
69                 CPPUNIT_ASSERT( q.pop( it ));
70                 CPPUNIT_ASSERT( it == static_cast<int>( i * 42 ));
71                 CPPUNIT_ASSERT( q.empty() );
72             }
73         }
74
75         template <class Queue>
76         void testWithItemCounter()
77         {
78             Queue   q;
79             test_ic_with( q );
80             test_emplace_ic( q );
81         }
82
83         template <class Queue>
84         void testFCQueue()
85         {
86             Queue   q;
87             test_ic_with( q );
88         }
89
90         template <class Queue>
91         void test_ic_with( Queue& q )
92         {
93             int     it;
94             int     nPrev;
95
96             for ( size_t i = 0; i < 3; ++i ) {
97                 CPPUNIT_ASSERT( q.empty() );
98                 CPPUNIT_ASSERT( q.size() == 0 );
99                 CPPUNIT_ASSERT( q.enqueue( 1 ) );
100                 CPPUNIT_ASSERT( q.size() == 1 );
101                 CPPUNIT_ASSERT( !q.empty() );
102                 CPPUNIT_ASSERT( q.push( 10 ) );
103                 CPPUNIT_ASSERT( !q.empty() );
104                 CPPUNIT_ASSERT( q.size() == 2 );
105
106                 it = -1;
107                 CPPUNIT_ASSERT( q.pop( it ) );
108                 CPPUNIT_ASSERT( it == 1 );
109                 CPPUNIT_ASSERT( !q.empty() );
110                 CPPUNIT_ASSERT( q.size() == 1 );
111                 CPPUNIT_ASSERT( q.dequeue( it ) );
112                 CPPUNIT_ASSERT( it == 10 );
113                 CPPUNIT_ASSERT( q.size() == 0 );
114                 CPPUNIT_ASSERT( q.empty() );
115                 CPPUNIT_ASSERT( q.size() == 0 );
116                 it += 2009;
117                 nPrev = it;
118                 CPPUNIT_ASSERT( !q.dequeue( it ) );
119                 CPPUNIT_ASSERT( it == nPrev )       ;   // it must not be changed!
120
121                 CPPUNIT_ASSERT( q.empty() );
122                 CPPUNIT_ASSERT( q.size() == 0 );
123             }
124         }
125
126         template <class Queue>
127         void test_emplace_ic( Queue& q )
128         {
129             int     it = 0;
130             for ( size_t i = 0; i < 3; ++i ) {
131                 CPPUNIT_ASSERT( q.emplace( (int) i * 10 ) );
132                 CPPUNIT_ASSERT( !q.empty() );
133                 CPPUNIT_ASSERT( q.size() == 1 );
134                 CPPUNIT_ASSERT( q.pop( it ));
135                 CPPUNIT_ASSERT( it == (int) i * 10 );
136                 CPPUNIT_ASSERT( q.empty() );
137                 CPPUNIT_ASSERT( q.size() == 0 );
138             }
139         }
140
141     public:
142         void MSQueue_HP();
143         void MSQueue_HP_relax();
144         void MSQueue_HP_seqcst();
145         void MSQueue_HP_relax_align();
146         void MSQueue_HP_seqcst_align();
147         void MSQueue_HP_Counted();
148         void MSQueue_HP_Counted_relax();
149         void MSQueue_HP_Counted_seqcst();
150         void MSQueue_HP_Counted_relax_align();
151         void MSQueue_HP_Counted_seqcst_align();
152
153         void MSQueue_DHP();
154         void MSQueue_DHP_relax();
155         void MSQueue_DHP_seqcst();
156         void MSQueue_DHP_relax_align();
157         void MSQueue_DHP_seqcst_align();
158         void MSQueue_DHP_Counted();
159         void MSQueue_DHP_Counted_relax();
160         void MSQueue_DHP_Counted_seqcst();
161         void MSQueue_DHP_Counted_relax_align();
162         void MSQueue_DHP_Counted_seqcst_align();
163
164         void MoirQueue_HP();
165         void MoirQueue_HP_relax();
166         void MoirQueue_HP_seqcst();
167         void MoirQueue_HP_relax_align();
168         void MoirQueue_HP_seqcst_align();
169         void MoirQueue_HP_Counted();
170         void MoirQueue_HP_Counted_relax();
171         void MoirQueue_HP_Counted_seqcst();
172         void MoirQueue_HP_Counted_relax_align();
173         void MoirQueue_HP_Counted_seqcst_align();
174
175         void MoirQueue_DHP();
176         void MoirQueue_DHP_relax();
177         void MoirQueue_DHP_seqcst();
178         void MoirQueue_DHP_relax_align();
179         void MoirQueue_DHP_seqcst_align();
180         void MoirQueue_DHP_Counted();
181         void MoirQueue_DHP_Counted_relax();
182         void MoirQueue_DHP_Counted_seqcst();
183         void MoirQueue_DHP_Counted_relax_align();
184         void MoirQueue_DHP_Counted_seqcst_align();
185
186         void OptimisticQueue_HP();
187         void OptimisticQueue_HP_relax();
188         void OptimisticQueue_HP_seqcst();
189         void OptimisticQueue_HP_relax_align();
190         void OptimisticQueue_HP_seqcst_align();
191         void OptimisticQueue_HP_Counted();
192         void OptimisticQueue_HP_Counted_relax();
193         void OptimisticQueue_HP_Counted_seqcst();
194         void OptimisticQueue_HP_Counted_relax_align();
195         void OptimisticQueue_HP_Counted_seqcst_align();
196
197         void OptimisticQueue_PTB();
198         void OptimisticQueue_PTB_relax();
199         void OptimisticQueue_PTB_seqcst();
200         void OptimisticQueue_PTB_relax_align();
201         void OptimisticQueue_PTB_seqcst_align();
202         void OptimisticQueue_PTB_Counted();
203         void OptimisticQueue_PTB_Counted_relax();
204         void OptimisticQueue_PTB_Counted_seqcst();
205         void OptimisticQueue_PTB_Counted_relax_align();
206         void OptimisticQueue_PTB_Counted_seqcst_align();
207
208         void BasketQueue_HP();
209         void BasketQueue_HP_relax();
210         void BasketQueue_HP_seqcst();
211         void BasketQueue_HP_relax_align();
212         void BasketQueue_HP_seqcst_align();
213         void BasketQueue_HP_Counted();
214         void BasketQueue_HP_Counted_relax();
215         void BasketQueue_HP_Counted_seqcst();
216         void BasketQueue_HP_Counted_relax_align();
217         void BasketQueue_HP_Counted_seqcst_align();
218
219         void BasketQueue_HRC();
220         void BasketQueue_HRC_relax();
221         void BasketQueue_HRC_seqcst();
222         void BasketQueue_HRC_relax_align();
223         void BasketQueue_HRC_seqcst_align();
224         void BasketQueue_HRC_Counted();
225         void BasketQueue_HRC_Counted_relax();
226         void BasketQueue_HRC_Counted_seqcst();
227         void BasketQueue_HRC_Counted_relax_align();
228         void BasketQueue_HRC_Counted_seqcst_align();
229
230         void BasketQueue_PTB();
231         void BasketQueue_PTB_relax();
232         void BasketQueue_PTB_seqcst();
233         void BasketQueue_PTB_relax_align();
234         void BasketQueue_PTB_seqcst_align();
235         void BasketQueue_PTB_Counted();
236         void BasketQueue_PTB_Counted_relax();
237         void BasketQueue_PTB_Counted_seqcst();
238         void BasketQueue_PTB_Counted_relax_align();
239         void BasketQueue_PTB_Counted_seqcst_align();
240
241         void FCQueue_deque();
242         void FCQueue_deque_elimination();
243         void FCQueue_deque_mutex();
244         void FCQueue_deque_stat();
245         void FCQueue_list();
246         void FCQueue_list_elimination();
247         void FCQueue_list_mutex();
248         void FCQueue_list_stat();
249
250         void Vyukov_MPMCCyclicQueue();
251         void Vyukov_MPMCCyclicQueue_Counted();
252
253         void RWQueue_();
254         void RWQueue_Counted();
255
256         CPPUNIT_TEST_SUITE(Queue_TestHeader)
257             CPPUNIT_TEST(MSQueue_HP);
258             CPPUNIT_TEST(MSQueue_HP_relax);
259             CPPUNIT_TEST(MSQueue_HP_seqcst);
260             CPPUNIT_TEST(MSQueue_HP_relax_align);
261             CPPUNIT_TEST(MSQueue_HP_seqcst_align);
262             CPPUNIT_TEST(MSQueue_HP_Counted);
263             CPPUNIT_TEST(MSQueue_HP_Counted_relax);
264             CPPUNIT_TEST(MSQueue_HP_Counted_seqcst);
265             CPPUNIT_TEST(MSQueue_HP_Counted_relax_align);
266             CPPUNIT_TEST(MSQueue_HP_Counted_seqcst_align);
267
268             CPPUNIT_TEST(MSQueue_DHP);
269             CPPUNIT_TEST(MSQueue_DHP_relax);
270             CPPUNIT_TEST(MSQueue_DHP_seqcst);
271             CPPUNIT_TEST(MSQueue_DHP_relax_align);
272             CPPUNIT_TEST(MSQueue_DHP_seqcst_align);
273             CPPUNIT_TEST(MSQueue_DHP_Counted);
274             CPPUNIT_TEST(MSQueue_DHP_Counted_relax);
275             CPPUNIT_TEST(MSQueue_DHP_Counted_seqcst);
276             CPPUNIT_TEST(MSQueue_DHP_Counted_relax_align);
277             CPPUNIT_TEST(MSQueue_DHP_Counted_seqcst_align);
278
279             CPPUNIT_TEST(MoirQueue_HP);
280             CPPUNIT_TEST(MoirQueue_HP_relax);
281             CPPUNIT_TEST(MoirQueue_HP_seqcst);
282             CPPUNIT_TEST(MoirQueue_HP_relax_align);
283             CPPUNIT_TEST(MoirQueue_HP_seqcst_align);
284             CPPUNIT_TEST(MoirQueue_HP_Counted);
285             CPPUNIT_TEST(MoirQueue_HP_Counted_relax);
286             CPPUNIT_TEST(MoirQueue_HP_Counted_seqcst);
287             CPPUNIT_TEST(MoirQueue_HP_Counted_relax_align);
288             CPPUNIT_TEST(MoirQueue_HP_Counted_seqcst_align);
289
290             CPPUNIT_TEST(MoirQueue_DHP);
291             CPPUNIT_TEST(MoirQueue_DHP_relax);
292             CPPUNIT_TEST(MoirQueue_DHP_seqcst);
293             CPPUNIT_TEST(MoirQueue_DHP_relax_align);
294             CPPUNIT_TEST(MoirQueue_DHP_seqcst_align);
295             CPPUNIT_TEST(MoirQueue_DHP_Counted);
296             CPPUNIT_TEST(MoirQueue_DHP_Counted_relax);
297             CPPUNIT_TEST(MoirQueue_DHP_Counted_seqcst);
298             CPPUNIT_TEST(MoirQueue_DHP_Counted_relax_align);
299             CPPUNIT_TEST(MoirQueue_DHP_Counted_seqcst_align);
300
301             CPPUNIT_TEST(OptimisticQueue_HP);
302             CPPUNIT_TEST(OptimisticQueue_HP_relax);
303             CPPUNIT_TEST(OptimisticQueue_HP_seqcst);
304             CPPUNIT_TEST(OptimisticQueue_HP_relax_align);
305             CPPUNIT_TEST(OptimisticQueue_HP_seqcst_align);
306             CPPUNIT_TEST(OptimisticQueue_HP_Counted);
307             CPPUNIT_TEST(OptimisticQueue_HP_Counted_relax);
308             CPPUNIT_TEST(OptimisticQueue_HP_Counted_seqcst);
309             CPPUNIT_TEST(OptimisticQueue_HP_Counted_relax_align);
310             CPPUNIT_TEST(OptimisticQueue_HP_Counted_seqcst_align);
311
312             CPPUNIT_TEST(OptimisticQueue_PTB);
313             CPPUNIT_TEST(OptimisticQueue_PTB_relax);
314             CPPUNIT_TEST(OptimisticQueue_PTB_seqcst);
315             CPPUNIT_TEST(OptimisticQueue_PTB_relax_align);
316             CPPUNIT_TEST(OptimisticQueue_PTB_seqcst_align);
317             CPPUNIT_TEST(OptimisticQueue_PTB_Counted);
318             CPPUNIT_TEST(OptimisticQueue_PTB_Counted_relax);
319             CPPUNIT_TEST(OptimisticQueue_PTB_Counted_seqcst);
320             CPPUNIT_TEST(OptimisticQueue_PTB_Counted_relax_align);
321             CPPUNIT_TEST(OptimisticQueue_PTB_Counted_seqcst_align);
322
323             CPPUNIT_TEST(BasketQueue_HP);
324             CPPUNIT_TEST(BasketQueue_HP_relax);
325             CPPUNIT_TEST(BasketQueue_HP_seqcst);
326             CPPUNIT_TEST(BasketQueue_HP_relax_align);
327             CPPUNIT_TEST(BasketQueue_HP_seqcst_align);
328             CPPUNIT_TEST(BasketQueue_HP_Counted);
329             CPPUNIT_TEST(BasketQueue_HP_Counted_relax);
330             CPPUNIT_TEST(BasketQueue_HP_Counted_seqcst);
331             CPPUNIT_TEST(BasketQueue_HP_Counted_relax_align);
332             CPPUNIT_TEST(BasketQueue_HP_Counted_seqcst_align);
333
334             CPPUNIT_TEST(BasketQueue_HRC);
335             CPPUNIT_TEST(BasketQueue_HRC_relax);
336             CPPUNIT_TEST(BasketQueue_HRC_seqcst);
337             CPPUNIT_TEST(BasketQueue_HRC_relax_align);
338             CPPUNIT_TEST(BasketQueue_HRC_seqcst_align);
339             CPPUNIT_TEST(BasketQueue_HRC_Counted);
340             CPPUNIT_TEST(BasketQueue_HRC_Counted_relax);
341             CPPUNIT_TEST(BasketQueue_HRC_Counted_seqcst);
342             CPPUNIT_TEST(BasketQueue_HRC_Counted_relax_align);
343             CPPUNIT_TEST(BasketQueue_HRC_Counted_seqcst_align);
344
345             CPPUNIT_TEST(BasketQueue_PTB);
346             CPPUNIT_TEST(BasketQueue_PTB_relax);
347             CPPUNIT_TEST(BasketQueue_PTB_seqcst);
348             CPPUNIT_TEST(BasketQueue_PTB_relax_align);
349             CPPUNIT_TEST(BasketQueue_PTB_seqcst_align);
350             CPPUNIT_TEST(BasketQueue_PTB_Counted);
351             CPPUNIT_TEST(BasketQueue_PTB_Counted_relax);
352             CPPUNIT_TEST(BasketQueue_PTB_Counted_seqcst);
353             CPPUNIT_TEST(BasketQueue_PTB_Counted_relax_align);
354             CPPUNIT_TEST(BasketQueue_PTB_Counted_seqcst_align);
355
356             CPPUNIT_TEST(FCQueue_deque)
357             CPPUNIT_TEST(FCQueue_deque_elimination)
358             CPPUNIT_TEST(FCQueue_deque_mutex)
359             CPPUNIT_TEST(FCQueue_deque_stat)
360             CPPUNIT_TEST(FCQueue_list)
361             CPPUNIT_TEST(FCQueue_list_elimination)
362             CPPUNIT_TEST(FCQueue_list_mutex)
363             CPPUNIT_TEST(FCQueue_list_stat)
364
365             CPPUNIT_TEST(RWQueue_);
366             CPPUNIT_TEST(RWQueue_Counted);
367
368             CPPUNIT_TEST(Vyukov_MPMCCyclicQueue);
369             CPPUNIT_TEST(Vyukov_MPMCCyclicQueue_Counted);
370         CPPUNIT_TEST_SUITE_END();
371
372     };
373 } // namespace queue
374
375 #endif // #ifndef __UNIT_QUEUE_SIMPLE_H