Renamed test namespace
[libcds.git] / tests / test-hdr / deque / hdr_fcdeque.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/cppunit_proxy.h"
32 #include "cppunit/test_beans.h"
33 #include <cds/container/fcdeque.h>
34 #include <boost/container/deque.hpp>
35
36 namespace deque {
37
38     class HdrFCDeque: public CppUnitMini::TestCase
39     {
40         template <class Deque>
41         void test_with( Deque& dq )
42         {
43             size_t const c_nSize = 100;
44
45             // push_front/pop_front
46             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
47                 CPPUNIT_CHECK( dq.push_front( i ) );
48             CPPUNIT_CHECK( dq.size() == c_nSize );
49
50             size_t nCount = 0;
51             int val;
52             while ( !dq.empty() ) {
53                 CPPUNIT_CHECK( dq.pop_front( val ) );
54                 ++nCount;
55                 CPPUNIT_CHECK( static_cast<int>(c_nSize - nCount) == val );
56             }
57             CPPUNIT_CHECK( nCount == c_nSize );
58
59             // push_back/pop_back
60             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
61                 CPPUNIT_CHECK( dq.push_back( i ) );
62             CPPUNIT_CHECK( dq.size() == c_nSize );
63
64             nCount = 0;
65             while ( !dq.empty() ) {
66                 CPPUNIT_CHECK( dq.pop_back( val ) );
67                 ++nCount;
68                 CPPUNIT_CHECK( static_cast<int>(c_nSize - nCount) == val );
69             }
70             CPPUNIT_CHECK( nCount == c_nSize );
71
72             // push_back/pop_front
73             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
74                 CPPUNIT_CHECK( dq.push_back( i ) );
75             CPPUNIT_CHECK( dq.size() == c_nSize );
76
77             nCount = 0;
78             while ( !dq.empty() ) {
79                 CPPUNIT_CHECK( dq.pop_front( val ) );
80                 CPPUNIT_CHECK( static_cast<int>( nCount ) == val );
81                 ++nCount;
82             }
83             CPPUNIT_CHECK( nCount == c_nSize );
84
85             // push_front/pop_back
86             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
87                 CPPUNIT_CHECK( dq.push_front( i ) );
88             CPPUNIT_CHECK( dq.size() == c_nSize );
89
90             nCount = 0;
91             while ( !dq.empty() ) {
92                 CPPUNIT_CHECK( dq.pop_back( val ) );
93                 CPPUNIT_CHECK( static_cast<int>( nCount ) == val );
94                 ++nCount;
95             }
96             CPPUNIT_CHECK( nCount == c_nSize );
97
98             // clear
99             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
100                 CPPUNIT_CHECK( dq.push_front( i ) );
101             CPPUNIT_CHECK( dq.size() == c_nSize );
102
103             CPPUNIT_CHECK( !dq.empty() );
104             dq.clear();
105             CPPUNIT_CHECK( dq.empty() );
106         }
107
108         template <class Deque>
109         void test()
110         {
111             Deque dq;
112             test_with( dq );
113         }
114
115         void fcDeque()
116         {
117             typedef cds::container::FCDeque<int> deque_type;
118             test<deque_type>();
119         }
120
121         void fcDeque_elimination()
122         {
123             typedef cds::container::FCDeque<int, std::deque<int>,
124                 cds::container::fcdeque::make_traits<
125                     cds::opt::enable_elimination< true >
126                 >::type
127             > deque_type;
128             test<deque_type>();
129         }
130
131         void fcDeque_stat()
132         {
133             typedef cds::container::FCDeque<int, std::deque<int>,
134                 cds::container::fcdeque::make_traits<
135                     cds::opt::stat< cds::container::fcdeque::stat<> >
136                 >::type
137             > deque_type;
138             test<deque_type>();
139         }
140
141         void fcDeque_mutex()
142         {
143             struct deque_traits : public
144                 cds::container::fcdeque::make_traits<
145                     cds::opt::enable_elimination< true >
146                 >::type
147             {
148                 typedef std::mutex lock_type;
149             };
150             typedef cds::container::FCDeque<int, std::deque<int>, deque_traits > deque_type;
151             test<deque_type>();
152         }
153
154         void fcDeque_boost()
155         {
156             typedef cds::container::FCDeque<int, boost::container::deque<int> > deque_type;
157             test<deque_type>();
158         }
159
160         void fcDeque_boost_elimination()
161         {
162             typedef cds::container::FCDeque<int, boost::container::deque<int>,
163                 cds::container::fcdeque::make_traits<
164                     cds::opt::enable_elimination< true >
165                 >::type
166             > deque_type;
167             test<deque_type>();
168         }
169
170         void fcDeque_boost_stat()
171         {
172             typedef cds::container::FCDeque<int, boost::container::deque<int>,
173                 cds::container::fcdeque::make_traits<
174                     cds::opt::stat< cds::container::fcdeque::stat<> >
175                 >::type
176             > deque_type;
177             test<deque_type>();
178         }
179
180         void fcDeque_boost_mutex()
181         {
182             typedef cds::container::FCDeque<int, boost::container::deque<int>,
183                 cds::container::fcdeque::make_traits<
184                     cds::opt::enable_elimination< true >
185                     ,cds::opt::lock_type< std::mutex >
186                 >::type
187             > deque_type;
188             test<deque_type>();
189         }
190
191         CPPUNIT_TEST_SUITE(HdrFCDeque)
192             CPPUNIT_TEST(fcDeque)
193             CPPUNIT_TEST(fcDeque_elimination)
194             CPPUNIT_TEST(fcDeque_stat)
195             CPPUNIT_TEST(fcDeque_mutex)
196             CPPUNIT_TEST(fcDeque_boost)
197             CPPUNIT_TEST(fcDeque_boost_elimination)
198             CPPUNIT_TEST(fcDeque_boost_stat)
199             CPPUNIT_TEST(fcDeque_boost_mutex)
200         CPPUNIT_TEST_SUITE_END()
201     };
202
203 } // namespace deque
204
205 CPPUNIT_TEST_SUITE_REGISTRATION(deque::HdrFCDeque);