Issue #48: added std::move for pop() function of stack/queue/pqueue
[libcds.git] / tests / test-hdr / stack / hdr_fcstack.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 <cds/container/fcstack.h>
33
34 #include <vector>
35 #include <list>
36
37 namespace stack {
38
39     class TestFCStack: public CppUnitMini::TestCase
40     {
41         template <class Stack>
42         void test()
43         {
44             Stack s;
45             test_with( s );
46         }
47
48         template <class Stack>
49         void test_with( Stack& stack)
50         {
51             typedef typename Stack::value_type  value_type;
52             value_type v;
53
54             CPPUNIT_ASSERT( stack.empty() );
55             CPPUNIT_ASSERT( stack.size() == 0 );
56
57             CPPUNIT_ASSERT( stack.push(1));
58             CPPUNIT_ASSERT( !stack.empty() );
59             CPPUNIT_ASSERT( stack.size() == 1 );
60             CPPUNIT_ASSERT( stack.push(2));
61             CPPUNIT_ASSERT( !stack.empty() );
62             CPPUNIT_ASSERT( stack.size() == 2 );
63             CPPUNIT_ASSERT( stack.push(3));
64             CPPUNIT_ASSERT( !stack.empty() );
65             CPPUNIT_ASSERT( stack.size() == 3 );
66
67             CPPUNIT_ASSERT( stack.pop(v) );
68             CPPUNIT_ASSERT( v == 3 );
69             CPPUNIT_ASSERT( !stack.empty() );
70             CPPUNIT_ASSERT( stack.size() == 2 );
71             CPPUNIT_ASSERT( stack.pop(v) );
72             CPPUNIT_ASSERT( v == 2 );
73             CPPUNIT_ASSERT( !stack.empty() );
74             CPPUNIT_ASSERT( stack.size() == 1 );
75             CPPUNIT_ASSERT( stack.pop(v) );
76             CPPUNIT_ASSERT( v == 1 );
77             CPPUNIT_ASSERT( stack.empty() );
78             CPPUNIT_ASSERT( stack.size() == 0 );
79             v = 1000;
80             CPPUNIT_ASSERT( !stack.pop(v) );
81             CPPUNIT_ASSERT( v == 1000 );
82             CPPUNIT_ASSERT( stack.empty() );
83             CPPUNIT_ASSERT( stack.size() == 0 );
84
85             CPPUNIT_ASSERT( stack.push(10));
86             CPPUNIT_ASSERT( stack.push(20));
87             CPPUNIT_ASSERT( stack.push(30));
88             CPPUNIT_ASSERT( !stack.empty());
89             CPPUNIT_ASSERT( stack.size() == 3 );
90
91             while ( stack.pop(v) );
92
93             CPPUNIT_ASSERT( stack.empty() );
94             CPPUNIT_ASSERT( stack.size() == 0 );
95         }
96
97         void FCStack_default();
98         void FCStack_deque();
99         void FCStack_deque_elimination();
100         void FCStack_vector();
101         void FCStack_vector_elimination();
102         void FCStack_list();
103         void FCStack_list_elimination();
104
105         CPPUNIT_TEST_SUITE(TestFCStack);
106             CPPUNIT_TEST( FCStack_default )
107             CPPUNIT_TEST( FCStack_deque )
108             CPPUNIT_TEST( FCStack_deque_elimination )
109             CPPUNIT_TEST( FCStack_vector )
110             CPPUNIT_TEST( FCStack_vector_elimination )
111             CPPUNIT_TEST( FCStack_list )
112             CPPUNIT_TEST( FCStack_list_elimination )
113         CPPUNIT_TEST_SUITE_END();
114     };
115
116     void TestFCStack::FCStack_default()
117     {
118         typedef cds::container::FCStack< unsigned int > stack_type;
119         test<stack_type>();
120     }
121
122     void TestFCStack::FCStack_deque()
123     {
124         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int> > > stack_type;
125         test<stack_type>();
126     }
127
128     void TestFCStack::FCStack_deque_elimination()
129     {
130         struct stack_traits : public
131             cds::container::fcstack::make_traits <
132             cds::opt::enable_elimination < true >
133             > ::type
134         {};
135         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int> >, stack_traits > stack_type;
136         test<stack_type>();
137     }
138
139     void TestFCStack::FCStack_vector()
140     {
141         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int> > > stack_type;
142         test<stack_type>();
143     }
144
145     void TestFCStack::FCStack_vector_elimination()
146     {
147         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int> >,
148             cds::container::fcstack::make_traits<
149                 cds::opt::enable_elimination< true >
150             >::type
151         > stack_type;
152         test<stack_type>();
153     }
154
155     void TestFCStack::FCStack_list()
156     {
157         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int> > > stack_type;
158         test<stack_type>();
159     }
160
161     void TestFCStack::FCStack_list_elimination()
162     {
163         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int> >,
164             cds::container::fcstack::make_traits<
165                 cds::opt::enable_elimination< true >
166             >::type
167         > stack_type;
168         test<stack_type>();
169     }
170
171     CPPUNIT_TEST_SUITE_REGISTRATION(stack::TestFCStack);
172 }   // namespace stack
173