1 // Copyright (C) 2011 Tim Blechmann
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <boost/lockfree/lockfree_forward.hpp>
8
9 #include <boost/lockfree/queue.hpp>
10 #include <boost/thread.hpp>
11
12 #define BOOST_TEST_MAIN
13 #ifdef BOOST_LOCKFREE_INCLUDE_TESTS
14 #include <boost/test/included/unit_test.hpp>
15 #else
16 #include <boost/test/unit_test.hpp>
17 #endif
18
19 #include <memory>
20
21 #include "test_helpers.hpp"
22
23 using namespace boost;
24 using namespace boost::lockfree;
25 using namespace std;
26
BOOST_AUTO_TEST_CASE(simple_queue_test)27 BOOST_AUTO_TEST_CASE( simple_queue_test )
28 {
29 queue<int> f(64);
30
31 BOOST_WARN(f.is_lock_free());
32
33 BOOST_REQUIRE(f.empty());
34 f.push(1);
35 f.push(2);
36
37 int i1(0), i2(0);
38
39 BOOST_REQUIRE(f.pop(i1));
40 BOOST_REQUIRE_EQUAL(i1, 1);
41
42 BOOST_REQUIRE(f.pop(i2));
43 BOOST_REQUIRE_EQUAL(i2, 2);
44 BOOST_REQUIRE(f.empty());
45 }
46
BOOST_AUTO_TEST_CASE(simple_queue_test_capacity)47 BOOST_AUTO_TEST_CASE( simple_queue_test_capacity )
48 {
49 queue<int, capacity<64> > f;
50
51 BOOST_WARN(f.is_lock_free());
52
53 BOOST_REQUIRE(f.empty());
54 f.push(1);
55 f.push(2);
56
57 int i1(0), i2(0);
58
59 BOOST_REQUIRE(f.pop(i1));
60 BOOST_REQUIRE_EQUAL(i1, 1);
61
62 BOOST_REQUIRE(f.pop(i2));
63 BOOST_REQUIRE_EQUAL(i2, 2);
64 BOOST_REQUIRE(f.empty());
65 }
66
67
BOOST_AUTO_TEST_CASE(unsafe_queue_test)68 BOOST_AUTO_TEST_CASE( unsafe_queue_test )
69 {
70 queue<int> f(64);
71
72 BOOST_WARN(f.is_lock_free());
73 BOOST_REQUIRE(f.empty());
74
75 int i1(0), i2(0);
76
77 f.unsynchronized_push(1);
78 f.unsynchronized_push(2);
79
80 BOOST_REQUIRE(f.unsynchronized_pop(i1));
81 BOOST_REQUIRE_EQUAL(i1, 1);
82
83 BOOST_REQUIRE(f.unsynchronized_pop(i2));
84 BOOST_REQUIRE_EQUAL(i2, 2);
85 BOOST_REQUIRE(f.empty());
86 }
87
88
BOOST_AUTO_TEST_CASE(queue_consume_one_test)89 BOOST_AUTO_TEST_CASE( queue_consume_one_test )
90 {
91 queue<int> f(64);
92
93 BOOST_WARN(f.is_lock_free());
94 BOOST_REQUIRE(f.empty());
95
96 f.push(1);
97 f.push(2);
98
99 #ifdef BOOST_NO_CXX11_LAMBDAS
100 bool success1 = f.consume_one(test_equal(1));
101 bool success2 = f.consume_one(test_equal(2));
102 #else
103 bool success1 = f.consume_one([] (int i) {
104 BOOST_REQUIRE_EQUAL(i, 1);
105 });
106
107 bool success2 = f.consume_one([] (int i) {
108 BOOST_REQUIRE_EQUAL(i, 2);
109 });
110 #endif
111
112 BOOST_REQUIRE(success1);
113 BOOST_REQUIRE(success2);
114
115 BOOST_REQUIRE(f.empty());
116 }
117
BOOST_AUTO_TEST_CASE(queue_consume_all_test)118 BOOST_AUTO_TEST_CASE( queue_consume_all_test )
119 {
120 queue<int> f(64);
121
122 BOOST_WARN(f.is_lock_free());
123 BOOST_REQUIRE(f.empty());
124
125 f.push(1);
126 f.push(2);
127
128 #ifdef BOOST_NO_CXX11_LAMBDAS
129 size_t consumed = f.consume_all(dummy_functor());
130 #else
131 size_t consumed = f.consume_all([] (int i) {
132 });
133 #endif
134
135 BOOST_REQUIRE_EQUAL(consumed, 2u);
136
137 BOOST_REQUIRE(f.empty());
138 }
139
140
BOOST_AUTO_TEST_CASE(queue_convert_pop_test)141 BOOST_AUTO_TEST_CASE( queue_convert_pop_test )
142 {
143 queue<int*> f(128);
144 BOOST_REQUIRE(f.empty());
145 f.push(new int(1));
146 f.push(new int(2));
147 f.push(new int(3));
148 f.push(new int(4));
149
150 {
151 int * i1;
152
153 BOOST_REQUIRE(f.pop(i1));
154 BOOST_REQUIRE_EQUAL(*i1, 1);
155 delete i1;
156 }
157
158
159 {
160 boost::shared_ptr<int> i2;
161 BOOST_REQUIRE(f.pop(i2));
162 BOOST_REQUIRE_EQUAL(*i2, 2);
163 }
164
165 {
166 #ifdef BOOST_NO_AUTO_PTR
167 unique_ptr<int> i3;
168 #else
169 auto_ptr<int> i3;
170 #endif
171 BOOST_REQUIRE(f.pop(i3));
172
173 BOOST_REQUIRE_EQUAL(*i3, 3);
174 }
175
176 {
177 boost::shared_ptr<int> i4;
178 BOOST_REQUIRE(f.pop(i4));
179
180 BOOST_REQUIRE_EQUAL(*i4, 4);
181 }
182
183
184 BOOST_REQUIRE(f.empty());
185 }
186
BOOST_AUTO_TEST_CASE(reserve_test)187 BOOST_AUTO_TEST_CASE( reserve_test )
188 {
189 typedef boost::lockfree::queue< void* > memory_queue;
190
191 memory_queue ms(1);
192 ms.reserve(1);
193 ms.reserve_unsafe(1);
194 }
195