test_async_dispatch.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // (C) Copyright 2008-10 Anthony Williams
  2. // 2015 Oliver Kowalke
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <utility>
  8. #include <memory>
  9. #include <stdexcept>
  10. #include <string>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/fiber/all.hpp>
  13. struct A {
  14. A() = default;
  15. A( A const&) = delete;
  16. A & operator=( A const&) = delete;
  17. A( A && other) :
  18. value{ other.value } {
  19. other.value = 0;
  20. }
  21. A & operator=( A && other) {
  22. if ( this == & other) return * this;
  23. value = other.value;
  24. other.value = 0;
  25. return * this;
  26. }
  27. int value{ 0 };
  28. };
  29. struct X {
  30. int value;
  31. void foo( int i) {
  32. value = i;
  33. }
  34. };
  35. void fn1() {
  36. }
  37. int fn2( int i) {
  38. return i;
  39. }
  40. int & fn3( int & i) {
  41. return i;
  42. }
  43. A fn4( A && a) {
  44. return std::forward< A >( a);
  45. }
  46. void test_async_1() {
  47. boost::fibers::future< void > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn1);
  48. BOOST_CHECK( f1.valid() );
  49. f1.get();
  50. }
  51. void test_async_2() {
  52. int i = 3;
  53. boost::fibers::future< int > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn2, i);
  54. BOOST_CHECK( f1.valid() );
  55. BOOST_CHECK( i == f1.get());
  56. }
  57. void test_async_3() {
  58. int i = 7;
  59. boost::fibers::future< int& > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn3, std::ref( i) );
  60. BOOST_CHECK( f1.valid() );
  61. BOOST_CHECK( & i == & f1.get());
  62. }
  63. void test_async_4() {
  64. A a1;
  65. a1.value = 7;
  66. boost::fibers::future< A > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn4, std::move( a1) );
  67. BOOST_CHECK( f1.valid() );
  68. A a2 = f1.get();
  69. BOOST_CHECK( 7 == a2.value);
  70. }
  71. void test_async_5() {
  72. X x = {0};
  73. BOOST_CHECK( 0 == x.value);
  74. boost::fibers::future< void > f1 = boost::fibers::async(
  75. boost::fibers::launch::dispatch,
  76. std::bind( & X::foo, std::ref( x), 3) );
  77. BOOST_CHECK( f1.valid() );
  78. f1.get();
  79. BOOST_CHECK( 3 == x.value);
  80. }
  81. void test_async_6() {
  82. X x = {0};
  83. BOOST_CHECK( 0 == x.value);
  84. boost::fibers::future< void > f1 = boost::fibers::async(
  85. boost::fibers::launch::dispatch,
  86. std::bind( & X::foo, std::ref( x), std::placeholders::_1), 3);
  87. BOOST_CHECK( f1.valid() );
  88. f1.get();
  89. BOOST_CHECK( 3 == x.value);
  90. }
  91. void test_async_stack_alloc() {
  92. boost::fibers::future< void > f1 = boost::fibers::async(
  93. boost::fibers::launch::dispatch,
  94. std::allocator_arg,
  95. boost::fibers::fixedsize_stack{},
  96. fn1);
  97. BOOST_CHECK( f1.valid() );
  98. f1.get();
  99. }
  100. void test_async_std_alloc() {
  101. struct none {};
  102. boost::fibers::future< void > f1 = boost::fibers::async(
  103. boost::fibers::launch::dispatch,
  104. std::allocator_arg,
  105. boost::fibers::fixedsize_stack{},
  106. std::allocator< none >{},
  107. fn1);
  108. BOOST_CHECK( f1.valid() );
  109. f1.get();
  110. }
  111. boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) {
  112. boost::unit_test_framework::test_suite* test =
  113. BOOST_TEST_SUITE("Boost.Fiber: async test suite");
  114. test->add(BOOST_TEST_CASE(test_async_1));
  115. test->add(BOOST_TEST_CASE(test_async_2));
  116. test->add(BOOST_TEST_CASE(test_async_3));
  117. test->add(BOOST_TEST_CASE(test_async_4));
  118. test->add(BOOST_TEST_CASE(test_async_5));
  119. test->add(BOOST_TEST_CASE(test_async_6));
  120. test->add(BOOST_TEST_CASE(test_async_stack_alloc));
  121. test->add(BOOST_TEST_CASE(test_async_std_alloc));
  122. return test;
  123. }