2 // Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
11 #include <boost/assert.hpp>
12 #include <boost/test/unit_test.hpp>
14 #include <boost/fiber/all.hpp>
30 moveable( moveable
&& other
) :
37 moveable
& operator=( moveable
&& other
) {
38 if ( this == & other
) return * this;
48 boost::fibers::unbounded_channel
< int > c
;
49 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( 1) );
52 void test_push_closed() {
53 boost::fibers::unbounded_channel
< int > c
;
55 BOOST_CHECK( boost::fibers::channel_op_status::closed
== c
.push( 1) );
59 boost::fibers::unbounded_channel
< int > c
;
61 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
62 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop( v2
) );
63 BOOST_CHECK_EQUAL( v1
, v2
);
66 void test_pop_closed() {
67 boost::fibers::unbounded_channel
< int > c
;
69 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
71 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop( v2
) );
72 BOOST_CHECK_EQUAL( v1
, v2
);
73 BOOST_CHECK( boost::fibers::channel_op_status::closed
== c
.pop( v2
) );
76 void test_pop_success() {
77 boost::fibers::unbounded_channel
< int > c
;
79 boost::fibers::fiber
f1( boost::fibers::launch::dispatch
, [&c
,&v2
](){
80 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop( v2
) );
82 boost::fibers::fiber
f2( boost::fibers::launch::dispatch
, [&c
,v1
](){
83 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
87 BOOST_CHECK_EQUAL( v1
, v2
);
90 void test_value_pop() {
91 boost::fibers::unbounded_channel
< int > c
;
93 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
95 BOOST_CHECK_EQUAL( v1
, v2
);
98 void test_value_pop_closed() {
99 boost::fibers::unbounded_channel
< int > c
;
101 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
104 BOOST_CHECK_EQUAL( v1
, v2
);
108 } catch ( boost::fibers::fiber_error
const&) {
111 BOOST_CHECK( thrown
);
114 void test_value_pop_success() {
115 boost::fibers::unbounded_channel
< int > c
;
117 boost::fibers::fiber
f1( boost::fibers::launch::dispatch
, [&c
,&v2
](){
120 boost::fibers::fiber
f2( boost::fibers::launch::dispatch
, [&c
,v1
](){
121 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
125 BOOST_CHECK_EQUAL( v1
, v2
);
128 void test_try_pop() {
129 boost::fibers::unbounded_channel
< int > c
;
131 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
132 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.try_pop( v2
) );
133 BOOST_CHECK_EQUAL( v1
, v2
);
136 void test_try_pop_closed() {
137 boost::fibers::unbounded_channel
< int > c
;
139 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
141 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.try_pop( v2
) );
142 BOOST_CHECK_EQUAL( v1
, v2
);
143 BOOST_CHECK( boost::fibers::channel_op_status::closed
== c
.try_pop( v2
) );
146 void test_try_pop_success() {
147 boost::fibers::unbounded_channel
< int > c
;
149 boost::fibers::fiber
f1( boost::fibers::launch::dispatch
, [&c
,&v2
](){
150 while ( boost::fibers::channel_op_status::success
!= c
.try_pop( v2
) );
152 boost::fibers::fiber
f2( boost::fibers::launch::dispatch
, [&c
,v1
](){
153 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
157 BOOST_CHECK_EQUAL( v1
, v2
);
160 void test_pop_wait_for() {
161 boost::fibers::unbounded_channel
< int > c
;
163 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
164 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop_wait_for( v2
, std::chrono::seconds( 1) ) );
165 BOOST_CHECK_EQUAL( v1
, v2
);
168 void test_pop_wait_for_closed() {
169 boost::fibers::unbounded_channel
< int > c
;
171 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
173 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop_wait_for( v2
, std::chrono::seconds( 1) ) );
174 BOOST_CHECK_EQUAL( v1
, v2
);
175 BOOST_CHECK( boost::fibers::channel_op_status::closed
== c
.pop_wait_for( v2
, std::chrono::seconds( 1) ) );
178 void test_pop_wait_for_success() {
179 boost::fibers::unbounded_channel
< int > c
;
181 boost::fibers::fiber
f1( boost::fibers::launch::dispatch
, [&c
,&v2
](){
182 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop_wait_for( v2
, std::chrono::seconds( 1) ) );
184 boost::fibers::fiber
f2( boost::fibers::launch::dispatch
, [&c
,v1
](){
185 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
189 BOOST_CHECK_EQUAL( v1
, v2
);
192 void test_pop_wait_for_timeout() {
193 boost::fibers::unbounded_channel
< int > c
;
195 boost::fibers::fiber
f( boost::fibers::launch::dispatch
, [&c
,&v
](){
196 BOOST_CHECK( boost::fibers::channel_op_status::timeout
== c
.pop_wait_for( v
, std::chrono::seconds( 1) ) );
201 void test_pop_wait_until() {
202 boost::fibers::unbounded_channel
< int > c
;
204 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
205 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop_wait_until( v2
,
206 std::chrono::system_clock::now() + std::chrono::seconds( 1) ) );
207 BOOST_CHECK_EQUAL( v1
, v2
);
210 void test_pop_wait_until_closed() {
211 boost::fibers::unbounded_channel
< int > c
;
213 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
215 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop_wait_until( v2
,
216 std::chrono::system_clock::now() + std::chrono::seconds( 1) ) );
217 BOOST_CHECK_EQUAL( v1
, v2
);
218 BOOST_CHECK( boost::fibers::channel_op_status::closed
== c
.pop_wait_until( v2
,
219 std::chrono::system_clock::now() + std::chrono::seconds( 1) ) );
222 void test_pop_wait_until_success() {
223 boost::fibers::unbounded_channel
< int > c
;
225 boost::fibers::fiber
f1( boost::fibers::launch::dispatch
, [&c
,&v2
](){
226 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop_wait_until( v2
,
227 std::chrono::system_clock::now() + std::chrono::seconds( 1) ) );
229 boost::fibers::fiber
f2( boost::fibers::launch::dispatch
, [&c
,v1
](){
230 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( v1
) );
234 BOOST_CHECK_EQUAL( v1
, v2
);
237 void test_pop_wait_until_timeout() {
238 boost::fibers::unbounded_channel
< int > c
;
240 boost::fibers::fiber
f( boost::fibers::launch::dispatch
, [&c
,&v
](){
241 BOOST_CHECK( boost::fibers::channel_op_status::timeout
== c
.pop_wait_until( v
,
242 std::chrono::system_clock::now() + std::chrono::seconds( 1) ) );
247 void test_moveable() {
248 boost::fibers::unbounded_channel
< moveable
> c
;
250 BOOST_CHECK( m1
.state
);
251 BOOST_CHECK_EQUAL( 3, m1
.value
);
252 BOOST_CHECK( ! m2
.state
);
253 BOOST_CHECK_EQUAL( -1, m2
.value
);
254 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.push( std::move( m1
) ) );
255 BOOST_CHECK( ! m1
.state
);
256 BOOST_CHECK( ! m2
.state
);
257 BOOST_CHECK( boost::fibers::channel_op_status::success
== c
.pop( m2
) );
258 BOOST_CHECK( ! m1
.state
);
259 BOOST_CHECK_EQUAL( -1, m1
.value
);
260 BOOST_CHECK( m2
.state
);
261 BOOST_CHECK_EQUAL( 3, m2
.value
);
264 boost::unit_test::test_suite
* init_unit_test_suite( int, char* []) {
265 boost::unit_test::test_suite
* test
=
266 BOOST_TEST_SUITE("Boost.Fiber: unbounded_channel test suite");
268 test
->add( BOOST_TEST_CASE( & test_push
) );
269 test
->add( BOOST_TEST_CASE( & test_push_closed
) );
270 test
->add( BOOST_TEST_CASE( & test_pop
) );
271 test
->add( BOOST_TEST_CASE( & test_pop_closed
) );
272 test
->add( BOOST_TEST_CASE( & test_pop_success
) );
273 test
->add( BOOST_TEST_CASE( & test_value_pop
) );
274 test
->add( BOOST_TEST_CASE( & test_value_pop_closed
) );
275 test
->add( BOOST_TEST_CASE( & test_value_pop_success
) );
276 test
->add( BOOST_TEST_CASE( & test_try_pop
) );
277 test
->add( BOOST_TEST_CASE( & test_try_pop_closed
) );
278 test
->add( BOOST_TEST_CASE( & test_try_pop_success
) );
279 test
->add( BOOST_TEST_CASE( & test_pop_wait_for
) );
280 test
->add( BOOST_TEST_CASE( & test_pop_wait_for_closed
) );
281 test
->add( BOOST_TEST_CASE( & test_pop_wait_for_success
) );
282 test
->add( BOOST_TEST_CASE( & test_pop_wait_for_timeout
) );
283 test
->add( BOOST_TEST_CASE( & test_pop_wait_until
) );
284 test
->add( BOOST_TEST_CASE( & test_pop_wait_until_closed
) );
285 test
->add( BOOST_TEST_CASE( & test_pop_wait_until_success
) );
286 test
->add( BOOST_TEST_CASE( & test_pop_wait_until_timeout
) );
287 test
->add( BOOST_TEST_CASE( & test_moveable
) );