Adjusts sequential map test cases
[libcds.git] / test / stress / stack / intrusive_push_pop.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-2017
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 "intrusive_stack_push_pop.h"
32 #include "../misc/common.h"
33
34 namespace cds_test {
35     /*static*/ size_t intrusive_stack_push_pop::s_nPushThreadCount = 4;
36     /*static*/ size_t intrusive_stack_push_pop::s_nPopThreadCount = 4;
37     /*static*/ size_t intrusive_stack_push_pop::s_nStackSize = 10000000;
38     /*static*/ size_t intrusive_stack_push_pop::s_nEliminationSize = 4;
39     /*static*/ bool intrusive_stack_push_pop::s_bFCIterative = false;
40     /*static*/ unsigned int intrusive_stack_push_pop::s_nFCCombinePassCount = 64;
41     /*static*/ unsigned int intrusive_stack_push_pop::s_nFCCompactFactor = 1024;
42
43     /*static*/ atomics::atomic<size_t> intrusive_stack_push_pop::s_nWorkingProducers( 0 );
44
45     /*static*/ void intrusive_stack_push_pop::SetUpTestCase()
46     {
47         cds_test::config const& cfg = get_config( "IntrusiveStack_PushPop" );
48
49         s_nPushThreadCount = cfg.get_size_t( "PushThreadCount", s_nPushThreadCount );
50         s_nPopThreadCount = cfg.get_size_t( "PopThreadCount", s_nPopThreadCount );
51         s_nStackSize = cfg.get_size_t( "StackSize", s_nStackSize );
52         s_nEliminationSize = cfg.get_size_t( "EliminationSize", s_nEliminationSize );
53         s_bFCIterative = cfg.get_bool( "FCIterate", s_bFCIterative );
54         s_nFCCombinePassCount = cfg.get_uint( "FCCombinePassCount", s_nFCCombinePassCount );
55         s_nFCCompactFactor = cfg.get_uint( "FCCompactFactor", s_nFCCompactFactor );
56
57         if ( s_nPushThreadCount == 0 )
58             s_nPushThreadCount = 1;
59         if ( s_nPopThreadCount == 0 )
60             s_nPopThreadCount = 1;
61         if ( s_nEliminationSize == 0 )
62             s_nEliminationSize = 1;
63     }
64 } // namespace cds_test
65
66 namespace {
67     class intrusive_stack_push_pop : public cds_test::intrusive_stack_push_pop
68     {
69         typedef cds_test::intrusive_stack_push_pop base_class;
70     protected:
71         typedef base_class::value_type<> value_type;
72         typedef base_class::value_type< cds::intrusive::treiber_stack::node< cds::gc::HP >> hp_value_type;
73         typedef base_class::value_type< cds::intrusive::treiber_stack::node< cds::gc::DHP >> dhp_value_type;
74
75         template <typename Stack>
76         void test()
77         {
78             value_array<typename Stack::value_type> arrValue( s_nStackSize );
79             {
80                 Stack stack;
81                 do_test( stack, arrValue );
82             }
83             Stack::gc::force_dispose();
84         }
85
86         void check_elimination_stat( cds::intrusive::treiber_stack::empty_stat const& )
87         {}
88
89         void check_elimination_stat( cds::intrusive::treiber_stack::stat<> const& s )
90         {
91             EXPECT_EQ( s.m_PushCount.get() + s.m_ActivePushCollision.get() + s.m_PassivePushCollision.get(), s_nStackSize );
92             EXPECT_EQ( s.m_PopCount.get() + s.m_ActivePopCollision.get() + s.m_PassivePopCollision.get(), s_nStackSize );
93             EXPECT_EQ( s.m_PushCount.get(), s.m_PopCount.get());
94             EXPECT_EQ( s.m_ActivePopCollision.get(), s.m_PassivePushCollision.get());
95             EXPECT_EQ( s.m_ActivePushCollision.get(), s.m_PassivePopCollision.get());
96         }
97
98         template <typename Stack>
99         void test_elimination()
100         {
101             value_array<typename Stack::value_type> arrValue( s_nStackSize );
102             {
103                 Stack stack( s_nEliminationSize );
104                 do_test( stack, arrValue );
105                 check_elimination_stat( stack.statistics());
106             }
107             Stack::gc::force_dispose();
108         }
109
110         template <typename Stack>
111         void test_std()
112         {
113             value_array<typename Stack::value_type> arrValue( s_nStackSize );
114             Stack stack;
115             do_test( stack, arrValue );
116         }
117     };
118
119     // TreiberStack<cds::gc::HP>
120 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
121     TEST_F( test_fixture, stack_impl ) \
122     { \
123         typedef typename istack::Types<hp_value_type>::stack_impl stack_type; \
124         test< stack_type >(); \
125     }
126
127     CDSSTRESS_TreiberStack_HP( intrusive_stack_push_pop )
128
129 #undef CDSSTRESS_Stack_F
130
131     // TreiberStack<cds::gc::DHP>
132 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
133     TEST_F( test_fixture, stack_impl ) \
134     { \
135         typedef typename istack::Types<dhp_value_type>::stack_impl stack_type; \
136         test< stack_type >(); \
137     }
138
139     CDSSTRESS_TreiberStack_DHP( intrusive_stack_push_pop )
140
141 #undef CDSSTRESS_Stack_F
142
143     // TreiberStack<cds::gc::HP> + elimination enabled
144 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
145     TEST_F( test_fixture, stack_impl ) \
146     { \
147         typedef typename istack::Types<hp_value_type>::stack_impl stack_type; \
148         test_elimination< stack_type >(); \
149     }
150
151     CDSSTRESS_EliminationStack_HP( intrusive_stack_push_pop )
152
153 #undef CDSSTRESS_Stack_F
154
155
156     // TreiberStack<cds::gc::DHP> + elimination enabled
157 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
158     TEST_F( test_fixture, stack_impl ) \
159     { \
160         typedef typename istack::Types<dhp_value_type>::stack_impl stack_type; \
161         test_elimination< stack_type >(); \
162     }
163
164     CDSSTRESS_EliminationStack_DHP( intrusive_stack_push_pop )
165
166 #undef CDSSTRESS_Stack_F
167
168
169     // StdStack
170 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
171     TEST_F( test_fixture, stack_impl ) \
172     { \
173         typedef typename istack::Types<value_type>::stack_impl stack_type; \
174         test_std< stack_type >(); \
175     }
176
177     //CDSSTRESS_StdStack( intrusive_stack_push_pop )
178
179 #undef CDSSTRESS_Stack_F
180
181     //INSTANTIATE_TEST_CASE_P( a, intrusive_stack_push_pop, ::testing::Values(1));
182
183 } // namespace