Moved queue unit tests to gtest framework
[libcds.git] / test / unit / queue / intrusive_optqueue_hp.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 "test_intrusive_msqueue.h"
32
33 #include <cds/gc/hp.h>
34 #include <cds/intrusive/optimistic_queue.h>
35 #include <vector>
36
37 namespace {
38     namespace ci = cds::intrusive;
39     typedef cds::gc::HP gc_type;
40
41
42     class IntrusiveOptQueue_HP : public cds_test::intrusive_msqueue
43     {
44         typedef cds_test::intrusive_msqueue base_class;
45
46     protected:
47         typedef typename base_class::base_hook_item< ci::optimistic_queue::node<gc_type>> base_item_type;
48         typedef typename base_class::member_hook_item< ci::optimistic_queue::node<gc_type>> member_item_type;
49
50         void SetUp()
51         {
52             typedef ci::OptimisticQueue< gc_type, base_item_type > queue_type;
53
54             cds::gc::hp::GarbageCollector::Construct( queue_type::c_nHazardPtrCount, 1, 16 );
55             cds::threading::Manager::attachThread();
56         }
57
58         void TearDown()
59         {
60             cds::threading::Manager::detachThread();
61             cds::gc::hp::GarbageCollector::Destruct( true );
62         }
63
64         template <typename V>
65         void check_array( V& arr )
66         {
67             for ( size_t i = 0; i < arr.size() - 1; ++i ) {
68                 ASSERT_EQ( arr[i].nDisposeCount, 2 );
69             }
70             ASSERT_EQ( arr.back().nDisposeCount, 1 );
71         }
72     };
73
74     TEST_F( IntrusiveOptQueue_HP, defaulted )
75     {
76         typedef cds::intrusive::OptimisticQueue< gc_type, base_item_type,
77             typename ci::optimistic_queue::make_traits<
78                 ci::opt::disposer< mock_disposer >
79             >::type
80         > test_queue;
81
82         std::vector<base_item_type> arr;
83         arr.resize(100);
84         {
85             test_queue q;
86             test(q, arr);
87         }
88         gc_type::scan();
89         check_array( arr );
90     }
91
92     TEST_F( IntrusiveOptQueue_HP, base_hook )
93     {
94         typedef cds::intrusive::OptimisticQueue< gc_type, base_item_type,
95             typename ci::optimistic_queue::make_traits<
96                 ci::opt::disposer< mock_disposer >
97                 ,ci::opt::hook< ci::optimistic_queue::base_hook< ci::opt::gc<gc_type>>>
98             >::type
99         > test_queue;
100
101         std::vector<base_item_type> arr;
102         arr.resize(100);
103         {
104             test_queue q;
105             test(q, arr);
106         }
107         gc_type::scan();
108         check_array( arr );
109     }
110
111     TEST_F( IntrusiveOptQueue_HP, base_item_counting )
112     {
113         typedef cds::intrusive::OptimisticQueue< gc_type, base_item_type,
114             typename ci::optimistic_queue::make_traits<
115                 ci::opt::disposer< mock_disposer >
116                 , cds::opt::item_counter< cds::atomicity::item_counter >
117                 , ci::opt::hook< ci::optimistic_queue::base_hook< ci::opt::gc<gc_type>>>
118             >::type
119         > test_queue;
120
121         std::vector<base_item_type> arr;
122         arr.resize(100);
123         {
124             test_queue q;
125             test(q, arr);
126         }
127         gc_type::scan();
128         check_array( arr );
129     }
130
131     TEST_F( IntrusiveOptQueue_HP, base_stat )
132     {
133         struct traits : public ci::optimistic_queue::traits
134         {
135             typedef mock_disposer disposer;
136             typedef cds::atomicity::item_counter item_counter;
137             typedef ci::optimistic_queue::stat<> stat;
138             typedef cds::opt::v::sequential_consistent memory_model;
139         };
140         typedef cds::intrusive::OptimisticQueue< gc_type, base_item_type, traits > test_queue;
141
142         std::vector<base_item_type> arr;
143         arr.resize(100);
144         {
145             test_queue q;
146             test(q, arr);
147         }
148         gc_type::scan();
149         check_array( arr );
150     }
151
152     TEST_F( IntrusiveOptQueue_HP, member_hook )
153     {
154         typedef cds::intrusive::OptimisticQueue< gc_type, member_item_type,
155             typename ci::optimistic_queue::make_traits<
156                 ci::opt::disposer< mock_disposer >
157                 ,ci::opt::hook< ci::optimistic_queue::member_hook<
158                     offsetof( member_item_type, hMember ),
159                     ci::opt::gc<gc_type>
160                 >>
161             >::type
162         > test_queue;
163
164         std::vector<member_item_type> arr;
165         arr.resize( 100 );
166         {
167             test_queue q;
168             test( q, arr );
169         }
170         gc_type::scan();
171         check_array( arr );
172     }
173
174     TEST_F( IntrusiveOptQueue_HP, member_hook_stat )
175     {
176         struct traits : public ci::optimistic_queue::traits
177         {
178             typedef ci::optimistic_queue::member_hook<
179                 offsetof( member_item_type, hMember ),
180                 ci::opt::gc<gc_type>
181             > hook;
182             typedef mock_disposer disposer;
183             typedef cds::atomicity::item_counter item_counter;
184             typedef ci::optimistic_queue::stat<> stat;
185             typedef cds::opt::v::sequential_consistent memory_model;
186         };
187         typedef cds::intrusive::OptimisticQueue< gc_type, member_item_type, traits > test_queue;
188
189         std::vector<member_item_type> arr;
190         arr.resize( 100 );
191         {
192             test_queue q;
193             test( q, arr );
194         }
195         gc_type::scan();
196         check_array( arr );
197     }
198
199 } // namespace
200