1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
|
// (C) Copyright 2008-10 Anthony Williams
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <utility>
#include <memory>
#include <stdexcept>
#include <string>
#include <boost/test/unit_test.hpp>
#include <boost/fiber/all.hpp>
int gi = 7;
struct my_exception : public std::runtime_error {
my_exception() :
std::runtime_error("my_exception") {
}
};
struct A {
A() = default;
A( A const&) = delete;
A( A &&) = default;
A & operator=( A const&) = delete;
A & operator=( A &&) = default;
int value;
};
struct B {
bool bset{ false };
B() = default;
B( bool set) :
bset{ set } {
gi = 3;
}
~B() {
if ( bset) {
gi = -1;
}
}
B( B && other) :
bset{ other.bset } {
other.bset = false;
}
B & operator=( B && other) {
if ( this == & other) return * this;
bset = other.bset;
other.bset = false;
return * this;
}
B( B const&) = delete;
B & operator=( B const&) = delete;
};
void fn1( boost::fibers::promise< int > * p, int i) {
boost::this_fiber::yield();
p->set_value( i);
}
void fn2() {
boost::fibers::promise< int > p;
boost::fibers::future< int > f( p.get_future() );
boost::this_fiber::yield();
boost::fibers::fiber( boost::fibers::launch::dispatch, fn1, & p, 7).detach();
boost::this_fiber::yield();
BOOST_CHECK( 7 == f.get() );
}
int fn3() {
return 3;
}
void fn4() {
}
int fn5() {
boost::throw_exception( my_exception() );
return 3;
}
void fn6() {
boost::throw_exception( my_exception() );
}
int & fn7() {
return gi;
}
int fn8( int i) {
return i;
}
A fn9() {
A a;
a.value = 3;
return a;
}
A fn10() {
boost::throw_exception( my_exception() );
return A();
}
B fn11( bool set) {
B b( set);
return b;
}
// packaged_task
void test_packaged_task_create() {
// default constructed packaged_task is not valid
boost::fibers::packaged_task< int() > t1;
BOOST_CHECK( ! t1.valid() );
// packaged_task from function
boost::fibers::packaged_task< int() > t2( fn3);
BOOST_CHECK( t2.valid() );
}
// packaged_task
void test_packaged_task_create_move() {
// default constructed packaged_task is not valid
boost::fibers::packaged_task< A() > t1;
BOOST_CHECK( ! t1.valid() );
// packaged_task from function
boost::fibers::packaged_task< A() > t2( fn9);
BOOST_CHECK( t2.valid() );
}
void test_packaged_task_create_void() {
// default constructed packaged_task is not valid
boost::fibers::packaged_task< void() > t1;
BOOST_CHECK( ! t1.valid() );
// packaged_task from function
boost::fibers::packaged_task< void() > t2( fn4);
BOOST_CHECK( t2.valid() );
}
void test_packaged_task_move() {
boost::fibers::packaged_task< int() > t1( fn3);
BOOST_CHECK( t1.valid() );
// move construction
boost::fibers::packaged_task< int() > t2( std::move( t1) );
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
// move assignment
t1 = std::move( t2);
BOOST_CHECK( t1.valid() );
BOOST_CHECK( ! t2.valid() );
}
void test_packaged_task_move_move() {
boost::fibers::packaged_task< A() > t1( fn9);
BOOST_CHECK( t1.valid() );
// move construction
boost::fibers::packaged_task< A() > t2( std::move( t1) );
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
// move assignment
t1 = std::move( t2);
BOOST_CHECK( t1.valid() );
BOOST_CHECK( ! t2.valid() );
}
void test_packaged_task_move_void() {
boost::fibers::packaged_task< void() > t1( fn4);
BOOST_CHECK( t1.valid() );
// move construction
boost::fibers::packaged_task< void() > t2( std::move( t1) );
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
// move assignment
t1 = std::move( t2);
BOOST_CHECK( t1.valid() );
BOOST_CHECK( ! t2.valid() );
}
void test_packaged_task_swap() {
boost::fibers::packaged_task< int() > t1( fn3);
BOOST_CHECK( t1.valid() );
boost::fibers::packaged_task< int() > t2;
BOOST_CHECK( ! t2.valid() );
// swap
t1.swap( t2);
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
}
void test_packaged_task_swap_move() {
boost::fibers::packaged_task< A() > t1( fn9);
BOOST_CHECK( t1.valid() );
boost::fibers::packaged_task< A() > t2;
BOOST_CHECK( ! t2.valid() );
// swap
t1.swap( t2);
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
}
void test_packaged_task_swap_void() {
boost::fibers::packaged_task< void() > t1( fn4);
BOOST_CHECK( t1.valid() );
boost::fibers::packaged_task< void() > t2;
BOOST_CHECK( ! t2.valid() );
// swap
t1.swap( t2);
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
}
void test_packaged_task_reset() {
{
boost::fibers::packaged_task< int() > p( fn3);
boost::fibers::future< int > f( p.get_future() );
BOOST_CHECK( p.valid() );
p();
BOOST_CHECK( 3 == f.get() );
// reset
p.reset();
p();
f = p.get_future();
BOOST_CHECK( 3 == f.get() );
}
{
boost::fibers::packaged_task< int() > p;
bool thrown = false;
try {
p.reset();
} catch ( boost::fibers::packaged_task_uninitialized const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
}
void test_packaged_task_reset_destruction() {
gi = 0;
boost::fibers::packaged_task< B( bool) > p( fn11);
BOOST_CHECK( p.valid() );
BOOST_CHECK( 0 == gi);
p( true);
BOOST_CHECK( 3 == gi);
// reset
p.reset();
BOOST_CHECK( -1 == gi);
p( false);
BOOST_CHECK( 3 == gi);
// reset
p.reset();
BOOST_CHECK( 3 == gi);
}
void test_packaged_task_reset_move() {
{
boost::fibers::packaged_task< A() > p( fn9);
boost::fibers::future< A > f( p.get_future() );
BOOST_CHECK( p.valid() );
p();
BOOST_CHECK( 3 == f.get().value);
// reset
p.reset();
p();
f = p.get_future();
BOOST_CHECK( 3 == f.get().value);
}
{
boost::fibers::packaged_task< A() > p;
bool thrown = false;
try {
p.reset();
} catch ( boost::fibers::packaged_task_uninitialized const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
}
void test_packaged_task_reset_void() {
{
boost::fibers::packaged_task< void() > p( fn4);
boost::fibers::future< void > f( p.get_future() );
BOOST_CHECK( p.valid() );
p();
f.get();
// reset
p.reset();
p();
f = p.get_future();
f.get();
}
{
boost::fibers::packaged_task< void() > p;
bool thrown = false;
try {
p.reset();
} catch ( boost::fibers::packaged_task_uninitialized const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
}
void test_packaged_task_get_future() {
boost::fibers::packaged_task< int() > t1( fn3);
BOOST_CHECK( t1.valid() );
// retrieve future
boost::fibers::future< int > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// retrieve future a second time
bool thrown = false;
try {
f1 = t1.get_future();
} catch ( boost::fibers::future_already_retrieved const&) {
thrown = true;
}
BOOST_CHECK( thrown);
// move construction
boost::fibers::packaged_task< int() > t2( std::move( t1) );
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
// retrieve future from uninitialized
thrown = false;
try {
f1 = t1.get_future();
} catch ( boost::fibers::packaged_task_uninitialized const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
void test_packaged_task_get_future_move() {
boost::fibers::packaged_task< A() > t1( fn9);
BOOST_CHECK( t1.valid() );
// retrieve future
boost::fibers::future< A > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// retrieve future a second time
bool thrown = false;
try {
f1 = t1.get_future();
} catch ( boost::fibers::future_already_retrieved const&) {
thrown = true;
}
BOOST_CHECK( thrown);
// move construction
boost::fibers::packaged_task< A() > t2( std::move( t1) );
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
// retrieve future from uninitialized
thrown = false;
try {
f1 = t1.get_future();
} catch ( boost::fibers::packaged_task_uninitialized const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
void test_packaged_task_get_future_void() {
boost::fibers::packaged_task< void() > t1( fn4);
BOOST_CHECK( t1.valid() );
// retrieve future
boost::fibers::future< void > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// retrieve future a second time
bool thrown = false;
try {
f1 = t1.get_future();
} catch ( boost::fibers::future_already_retrieved const&) {
thrown = true;
}
BOOST_CHECK( thrown);
// move construction
boost::fibers::packaged_task< void() > t2( std::move( t1) );
BOOST_CHECK( ! t1.valid() );
BOOST_CHECK( t2.valid() );
// retrieve future from uninitialized
thrown = false;
try {
f1 = t1.get_future();
} catch ( boost::fibers::packaged_task_uninitialized const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
void test_packaged_task_exec() {
// promise takes a copyable as return type
boost::fibers::packaged_task< int() > t1( fn3);
BOOST_CHECK( t1.valid() );
boost::fibers::future< int > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// exec
t1();
BOOST_CHECK( 3 == f1.get() );
// exec a second time
bool thrown = false;
try {
t1();
} catch ( boost::fibers::promise_already_satisfied const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
void test_packaged_task_exec_move() {
// promise takes a copyable as return type
boost::fibers::packaged_task< A() > t1( fn9);
BOOST_CHECK( t1.valid() );
boost::fibers::future< A > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// exec
t1();
BOOST_CHECK( 3 == f1.get().value);
// exec a second time
bool thrown = false;
try {
t1();
} catch ( boost::fibers::promise_already_satisfied const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
void test_packaged_task_exec_param() {
// promise takes a copyable as return type
boost::fibers::packaged_task< int( int) > t1( fn8);
BOOST_CHECK( t1.valid() );
boost::fibers::future< int > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// exec
t1( 3);
BOOST_CHECK( 3 == f1.get() );
// exec a second time
bool thrown = false;
try {
t1( 7);
} catch ( boost::fibers::promise_already_satisfied const&) {
thrown = true;
}
BOOST_CHECK( thrown);
//TODO: packaged_task returns a moveable-only as return type
}
void test_packaged_task_exec_ref() {
// promise takes a copyable as return type
boost::fibers::packaged_task< int&() > t1( fn7);
BOOST_CHECK( t1.valid() );
boost::fibers::future< int& > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// exec
t1();
int & i = f1.get();
BOOST_CHECK( &gi == &i);
// exec a second time
bool thrown = false;
try {
t1();
} catch ( boost::fibers::promise_already_satisfied const&) {
thrown = true;
}
BOOST_CHECK( thrown);
//TODO: packaged_task returns a moveable-only as return type
}
void test_packaged_task_exec_void() {
// promise takes a copyable as return type
boost::fibers::packaged_task< void() > t1( fn4);
BOOST_CHECK( t1.valid() );
boost::fibers::future< void > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// set void
t1();
f1.get();
// exec a second time
bool thrown = false;
try {
t1();
} catch ( boost::fibers::promise_already_satisfied const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
void test_packaged_task_exception() {
// promise takes a copyable as return type
boost::fibers::packaged_task< int() > t1( fn5);
BOOST_CHECK( t1.valid() );
boost::fibers::future< int > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// exec
t1();
bool thrown = false;
try {
f1.get();
} catch ( my_exception const&) {
thrown = true;
}
BOOST_CHECK( thrown);
boost::fibers::packaged_task< int() > t2( fn5);
BOOST_CHECK( t2.valid() );
boost::fibers::future< int > f2 = t2.get_future();
BOOST_CHECK( f2.valid() );
// exec
t2();
BOOST_CHECK( f2.get_exception_ptr() );
thrown = false;
try
{ std::rethrow_exception( f2.get_exception_ptr() ); }
catch ( my_exception const&)
{ thrown = true; }
BOOST_CHECK( thrown);
}
void test_packaged_task_exception_move() {
// promise takes a moveable as return type
boost::fibers::packaged_task< A() > t1( fn10);
BOOST_CHECK( t1.valid() );
boost::fibers::future< A > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// exec
t1();
bool thrown = false;
try {
f1.get();
} catch ( my_exception const&) {
thrown = true;
}
BOOST_CHECK( thrown);
boost::fibers::packaged_task< A() > t2( fn10);
BOOST_CHECK( t2.valid() );
boost::fibers::future< A > f2 = t2.get_future();
BOOST_CHECK( f2.valid() );
// exec
t2();
BOOST_CHECK( f2.get_exception_ptr() );
thrown = false;
try
{ std::rethrow_exception( f2.get_exception_ptr() ); }
catch ( my_exception const&)
{ thrown = true; }
BOOST_CHECK( thrown);
}
void test_packaged_task_exception_void() {
// promise takes a copyable as return type
boost::fibers::packaged_task< void() > t1( fn6);
BOOST_CHECK( t1.valid() );
boost::fibers::future< void > f1 = t1.get_future();
BOOST_CHECK( f1.valid() );
// set void
t1();
bool thrown = false;
try {
f1.get();
} catch ( my_exception const&) {
thrown = true;
}
BOOST_CHECK( thrown);
boost::fibers::packaged_task< void() > t2( fn6);
BOOST_CHECK( t2.valid() );
boost::fibers::future< void > f2 = t2.get_future();
BOOST_CHECK( f2.valid() );
// exec
t2();
BOOST_CHECK( f2.get_exception_ptr() );
thrown = false;
try {
std::rethrow_exception( f2.get_exception_ptr() );
} catch ( my_exception const&) {
thrown = true;
}
BOOST_CHECK( thrown);
}
boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) {
boost::unit_test_framework::test_suite* test =
BOOST_TEST_SUITE("Boost.Fiber: packaged_task test suite");
test->add(BOOST_TEST_CASE(test_packaged_task_create));
test->add(BOOST_TEST_CASE(test_packaged_task_create_move));
test->add(BOOST_TEST_CASE(test_packaged_task_create_void));
test->add(BOOST_TEST_CASE(test_packaged_task_move));
test->add(BOOST_TEST_CASE(test_packaged_task_move_move));
test->add(BOOST_TEST_CASE(test_packaged_task_move_void));
test->add(BOOST_TEST_CASE(test_packaged_task_swap));
test->add(BOOST_TEST_CASE(test_packaged_task_swap_move));
test->add(BOOST_TEST_CASE(test_packaged_task_swap_void));
test->add(BOOST_TEST_CASE(test_packaged_task_reset));
test->add(BOOST_TEST_CASE(test_packaged_task_reset_destruction));
test->add(BOOST_TEST_CASE(test_packaged_task_reset_move));
test->add(BOOST_TEST_CASE(test_packaged_task_reset_void));
test->add(BOOST_TEST_CASE(test_packaged_task_get_future));
test->add(BOOST_TEST_CASE(test_packaged_task_get_future_move));
test->add(BOOST_TEST_CASE(test_packaged_task_get_future_void));
test->add(BOOST_TEST_CASE(test_packaged_task_exec));
test->add(BOOST_TEST_CASE(test_packaged_task_exec_move));
test->add(BOOST_TEST_CASE(test_packaged_task_exec_param));
test->add(BOOST_TEST_CASE(test_packaged_task_exec_ref));
test->add(BOOST_TEST_CASE(test_packaged_task_exec_void));
test->add(BOOST_TEST_CASE(test_packaged_task_exception));
test->add(BOOST_TEST_CASE(test_packaged_task_exception_move));
test->add(BOOST_TEST_CASE(test_packaged_task_exception_void));
return test;
}
|