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