Start moving to gtest framework
[libcds.git] / test / unit / stack / 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 <gtest/gtest.h>
32 #include <cds/container/fcstack.h>
33
34 #include <vector>
35 #include <list>
36
37 namespace {
38     class FCStack : public ::testing::Test
39     {
40     protected:
41         template <class Stack>
42         void test()
43         {
44             typedef typename Stack::value_type  value_type;
45             Stack stack;
46             value_type v;
47
48             ASSERT_TRUE( stack.empty() );
49             ASSERT_EQ( stack.size(), 0 );
50
51             ASSERT_TRUE( stack.push( 1 ) );
52             ASSERT_TRUE( !stack.empty() );
53             ASSERT_EQ( stack.size(), 1 );
54             ASSERT_TRUE( stack.push( 2 ) );
55             ASSERT_TRUE( !stack.empty() );
56             ASSERT_EQ( stack.size(), 2 );
57             ASSERT_TRUE( stack.push( 3 ) );
58             ASSERT_TRUE( !stack.empty() );
59             ASSERT_EQ( stack.size(), 3 );
60
61             ASSERT_TRUE( stack.pop( v ) );
62             EXPECT_EQ( v, 3 );
63             ASSERT_TRUE( !stack.empty() );
64             ASSERT_EQ( stack.size(), 2 );
65             ASSERT_TRUE( stack.pop( v ) );
66             EXPECT_EQ( v, 2 );
67             ASSERT_TRUE( !stack.empty() );
68             ASSERT_EQ( stack.size(), 1 );
69             ASSERT_TRUE( stack.pop( v ) );
70             EXPECT_EQ( v, 1 );
71             ASSERT_TRUE( stack.empty() );
72             ASSERT_EQ( stack.size(), 0 );
73             v = 1000;
74             ASSERT_TRUE( !stack.pop( v ) );
75             EXPECT_EQ( v, 1000 );
76             ASSERT_TRUE( stack.empty() );
77             ASSERT_EQ( stack.size(), 0 );
78
79             ASSERT_TRUE( stack.push( 10 ) );
80             ASSERT_TRUE( stack.push( 20 ) );
81             ASSERT_TRUE( stack.push( 30 ) );
82             ASSERT_TRUE( !stack.empty() );
83             ASSERT_EQ( stack.size(), 3 );
84
85             while ( stack.pop( v ) );
86
87             ASSERT_TRUE( stack.empty() );
88             ASSERT_EQ( stack.size(), 0 );
89         }
90     };
91
92     TEST_F( FCStack, default_stack )
93     {
94         typedef cds::container::FCStack< unsigned int > stack_type;
95         test<stack_type>();
96     }
97
98     TEST_F( FCStack, deque_based )
99     {
100         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>> stack_type;
101         test<stack_type>();
102     }
103
104     TEST_F( FCStack, deque_elimination )
105     {
106         struct stack_traits : public
107             cds::container::fcstack::make_traits <
108             cds::opt::enable_elimination < true >
109             > ::type
110         {};
111         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
112         test<stack_type>();
113     }
114
115     TEST_F( FCStack, vector_based )
116     {
117         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>> stack_type;
118         test<stack_type>();
119     }
120
121     TEST_F( FCStack, vector_elimination )
122     {
123         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
124             cds::container::fcstack::make_traits<
125             cds::opt::enable_elimination< true >
126             >::type
127         > stack_type;
128         test<stack_type>();
129     }
130
131     TEST_F( FCStack, list_based )
132     {
133         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>> stack_type;
134         test<stack_type>();
135     }
136
137     TEST_F( FCStack, list_elimination )
138     {
139         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
140             cds::container::fcstack::make_traits<
141             cds::opt::enable_elimination< true >
142             >::type
143         > stack_type;
144         test<stack_type>();
145     }
146 } // namespace