test_async_post.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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::post, 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::post, 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::post, 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::post, 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::post,
  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::post,
  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. boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) {
  92. boost::unit_test_framework::test_suite* test =
  93. BOOST_TEST_SUITE("Boost.Fiber: async test suite");
  94. test->add(BOOST_TEST_CASE(test_async_1));
  95. test->add(BOOST_TEST_CASE(test_async_2));
  96. test->add(BOOST_TEST_CASE(test_async_3));
  97. test->add(BOOST_TEST_CASE(test_async_4));
  98. test->add(BOOST_TEST_CASE(test_async_5));
  99. test->add(BOOST_TEST_CASE(test_async_6));
  100. return test;
  101. }