issue#11: tests/unit: changed .h file guard prefix to CDSUNIT_xxx
[libcds.git] / tests / test-hdr / stack / hdr_intrusive_treiber_stack.h
1 //$$CDS-header$$
2
3 #include "cppunit/cppunit_proxy.h"
4 #include <cds/intrusive/details/single_link_struct.h>
5
6 namespace stack {
7
8     namespace ci = cds::intrusive;
9
10     class TestIntrusiveStack: public CppUnitMini::TestCase
11     {
12     public:
13         template <typename GC>
14         struct base_hook_item: public ci::single_link::node< GC >
15         {
16             int nVal;
17             int nDisposeCount;
18
19             base_hook_item()
20                 : nDisposeCount(0)
21             {}
22         };
23
24         template <typename GC>
25         struct member_hook_item
26         {
27             int nVal;
28             int nDisposeCount;
29             ci::single_link::node< GC > hMember;
30
31             member_hook_item()
32                 : nDisposeCount(0)
33             {}
34         };
35
36         struct faked_disposer
37         {
38             template <typename T>
39             void operator ()( T * p )
40             {
41                 ++p->nDisposeCount;
42             }
43         };
44
45         template <class Stack>
46         void test_elimination()
47         {
48             Stack stack( 2 );
49             test_with( stack );
50         }
51
52         template <class Stack>
53         void test()
54         {
55             Stack stack;
56             test_with(stack);
57         }
58
59
60         template <class Stack>
61         void test_with( Stack& stack )
62         {
63             typedef typename Stack::value_type  value_type;
64
65             CPPUNIT_ASSERT( stack.empty() );
66
67             value_type v1, v2, v3;
68             v1.nVal = 1;
69             v2.nVal = 2;
70             v3.nVal = 3;
71             CPPUNIT_ASSERT( stack.push(v1));
72             CPPUNIT_ASSERT( !stack.empty() );
73             CPPUNIT_ASSERT( stack.push(v2));
74             CPPUNIT_ASSERT( !stack.empty() );
75             CPPUNIT_ASSERT( stack.push(v3));
76             CPPUNIT_ASSERT( !stack.empty() );
77
78             value_type * pv;
79             pv = stack.pop();
80             CPPUNIT_ASSERT( pv != nullptr );
81             CPPUNIT_ASSERT( pv->nVal == 3 );
82             CPPUNIT_ASSERT( !stack.empty() );
83             pv = stack.pop();
84             CPPUNIT_ASSERT( pv != nullptr );
85             CPPUNIT_ASSERT( pv->nVal == 2 );
86             CPPUNIT_ASSERT( !stack.empty() );
87             pv = stack.pop();
88             CPPUNIT_ASSERT( pv != nullptr );
89             CPPUNIT_ASSERT( pv->nVal == 1 );
90             CPPUNIT_ASSERT( stack.empty() );
91             pv = stack.pop();
92             CPPUNIT_ASSERT( pv == nullptr );
93
94             CPPUNIT_ASSERT( v1.nDisposeCount == 0 );
95             CPPUNIT_ASSERT( v2.nDisposeCount == 0 );
96             CPPUNIT_ASSERT( v3.nDisposeCount == 0 );
97
98             stack.push(v1);
99             stack.push(v2);
100             stack.push(v3);
101
102             stack.clear();
103             CPPUNIT_ASSERT( stack.empty() );
104
105             Stack::gc::scan();
106             if ( !std::is_same<typename Stack::disposer, ci::opt::v::empty_disposer>::value ) {
107                 CPPUNIT_ASSERT( v1.nDisposeCount == 1 );
108                 CPPUNIT_ASSERT( v2.nDisposeCount == 1 );
109                 CPPUNIT_ASSERT( v3.nDisposeCount == 1 );
110             }
111         }
112
113         void Treiber_HP_default();
114         void Treiber_HP_base();
115         void Treiber_HP_base_disposer();
116         void Treiber_HP_member();
117         void Treiber_HP_member_disposer();
118         void Treiber_DHP_base();
119         void Treiber_DHP_base_disposer();
120         void Treiber_DHP_member();
121         void Treiber_DHP_member_disposer();
122
123         void Treiber_HP_default_relaxed();
124         void Treiber_HP_base_relaxed();
125         void Treiber_HP_base_disposer_relaxed();
126         void Treiber_HP_member_relaxed();
127         void Treiber_HP_member_disposer_relaxed();
128         void Treiber_DHP_base_relaxed();
129         void Treiber_DHP_base_disposer_relaxed();
130         void Treiber_DHP_member_relaxed();
131         void Treiber_DHP_member_disposer_relaxed();
132
133         void Elimination_HP_default();
134         void Elimination_HP_base();
135         void Elimination_HP_base_dyn();
136         void Elimination_HP_base_disposer();
137         void Elimination_HP_member();
138         void Elimination_HP_member_dyn();
139         void Elimination_HP_member_disposer();
140         void Elimination_DHP_base();
141         void Elimination_DHP_base_dyn();
142         void Elimination_DHP_base_disposer();
143         void Elimination_DHP_member();
144         void Elimination_DHP_member_dyn();
145         void Elimination_DHP_member_disposer();
146
147         void Elimination_HP_default_relaxed();
148         void Elimination_HP_base_relaxed();
149         void Elimination_HP_base_disposer_relaxed();
150         void Elimination_HP_member_relaxed();
151         void Elimination_HP_member_disposer_relaxed();
152         void Elimination_DHP_base_relaxed();
153         void Elimination_DHP_base_disposer_relaxed();
154         void Elimination_DHP_member_relaxed();
155         void Elimination_DHP_member_disposer_relaxed();
156
157         CPPUNIT_TEST_SUITE(TestIntrusiveStack)
158             CPPUNIT_TEST(Treiber_HP_default)
159             CPPUNIT_TEST(Treiber_HP_default_relaxed)
160             CPPUNIT_TEST(Treiber_HP_base)
161             CPPUNIT_TEST(Treiber_HP_base_relaxed)
162             CPPUNIT_TEST(Treiber_HP_base_disposer)
163             CPPUNIT_TEST(Treiber_HP_base_disposer_relaxed)
164             CPPUNIT_TEST(Treiber_HP_member)
165             CPPUNIT_TEST(Treiber_HP_member_relaxed)
166             CPPUNIT_TEST(Treiber_HP_member_disposer)
167             CPPUNIT_TEST(Treiber_HP_member_disposer_relaxed)
168             CPPUNIT_TEST(Treiber_DHP_base)
169             CPPUNIT_TEST(Treiber_DHP_base_relaxed)
170             CPPUNIT_TEST(Treiber_DHP_base_disposer)
171             CPPUNIT_TEST(Treiber_DHP_base_disposer_relaxed)
172             CPPUNIT_TEST(Treiber_DHP_member)
173             CPPUNIT_TEST(Treiber_DHP_member_relaxed)
174             CPPUNIT_TEST(Treiber_DHP_member_disposer)
175             CPPUNIT_TEST(Treiber_DHP_member_disposer_relaxed)
176
177             CPPUNIT_TEST(Elimination_HP_default)
178             CPPUNIT_TEST(Elimination_HP_default_relaxed)
179             CPPUNIT_TEST(Elimination_HP_base)
180             CPPUNIT_TEST(Elimination_HP_base_dyn)
181             CPPUNIT_TEST(Elimination_HP_base_relaxed)
182             CPPUNIT_TEST(Elimination_HP_base_disposer)
183             CPPUNIT_TEST(Elimination_HP_base_disposer_relaxed)
184             CPPUNIT_TEST(Elimination_HP_member)
185             CPPUNIT_TEST(Elimination_HP_member_dyn)
186             CPPUNIT_TEST(Elimination_HP_member_relaxed)
187             CPPUNIT_TEST(Elimination_HP_member_disposer)
188             CPPUNIT_TEST(Elimination_HP_member_disposer_relaxed)
189             CPPUNIT_TEST(Elimination_DHP_base)
190             CPPUNIT_TEST(Elimination_DHP_base_dyn)
191             CPPUNIT_TEST(Elimination_DHP_base_relaxed)
192             CPPUNIT_TEST(Elimination_DHP_base_disposer)
193             CPPUNIT_TEST(Elimination_DHP_base_disposer_relaxed)
194             CPPUNIT_TEST(Elimination_DHP_member)
195             CPPUNIT_TEST(Elimination_DHP_member_dyn)
196             CPPUNIT_TEST(Elimination_DHP_member_relaxed)
197             CPPUNIT_TEST(Elimination_DHP_member_disposer)
198             CPPUNIT_TEST(Elimination_DHP_member_disposer_relaxed)
199         CPPUNIT_TEST_SUITE_END()
200     };
201
202 } // namespace stack