]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/test/test_async_dispatch.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / fiber / test / test_async_dispatch.cpp
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
8 #include <utility>
9 #include <memory>
10 #include <stdexcept>
11 #include <string>
12
13 #include <boost/test/unit_test.hpp>
14
15 #include <boost/fiber/all.hpp>
16
17 struct A {
18 A() = default;
19
20 A( A const&) = delete;
21 A & operator=( A const&) = delete;
22
23 A( A && other) :
24 value{ other.value } {
25 other.value = 0;
26 }
27
28 A & operator=( A && other) {
29 if ( this == & other) return * this;
30 value = other.value;
31 other.value = 0;
32 return * this;
33 }
34
35 int value{ 0 };
36 };
37
38 struct X {
39 int value;
40
41 void foo( int i) {
42 value = i;
43 }
44 };
45
46 void fn1() {
47 }
48
49 int fn2( int i) {
50 return i;
51 }
52
53 int & fn3( int & i) {
54 return i;
55 }
56
57 A fn4( A && a) {
58 return std::forward< A >( a);
59 }
60
61 void test_async_1() {
62 boost::fibers::future< void > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn1);
63 BOOST_CHECK( f1.valid() );
64
65 f1.get();
66 }
67
68 void test_async_2() {
69 int i = 3;
70 boost::fibers::future< int > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn2, i);
71 BOOST_CHECK( f1.valid() );
72
73 BOOST_CHECK( i == f1.get());
74 }
75
76 void test_async_3() {
77 int i = 7;
78 boost::fibers::future< int& > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn3, std::ref( i) );
79 BOOST_CHECK( f1.valid() );
80
81 BOOST_CHECK( & i == & f1.get());
82 }
83
84 void test_async_4() {
85 A a1;
86 a1.value = 7;
87 boost::fibers::future< A > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn4, std::move( a1) );
88 BOOST_CHECK( f1.valid() );
89
90 A a2 = f1.get();
91 BOOST_CHECK( 7 == a2.value);
92 }
93
94 void test_async_5() {
95 X x = {0};
96 BOOST_CHECK( 0 == x.value);
97 boost::fibers::future< void > f1 = boost::fibers::async(
98 boost::fibers::launch::dispatch,
99 std::bind( & X::foo, std::ref( x), 3) );
100 BOOST_CHECK( f1.valid() );
101
102 f1.get();
103 BOOST_CHECK( 3 == x.value);
104 }
105
106 void test_async_6() {
107 X x = {0};
108 BOOST_CHECK( 0 == x.value);
109 boost::fibers::future< void > f1 = boost::fibers::async(
110 boost::fibers::launch::dispatch,
111 std::bind( & X::foo, std::ref( x), std::placeholders::_1), 3);
112 BOOST_CHECK( f1.valid() );
113
114 f1.get();
115 BOOST_CHECK( 3 == x.value);
116 }
117
118
119 boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) {
120 boost::unit_test_framework::test_suite* test =
121 BOOST_TEST_SUITE("Boost.Fiber: async test suite");
122
123 test->add(BOOST_TEST_CASE(test_async_1));
124 test->add(BOOST_TEST_CASE(test_async_2));
125 test->add(BOOST_TEST_CASE(test_async_3));
126 test->add(BOOST_TEST_CASE(test_async_4));
127 test->add(BOOST_TEST_CASE(test_async_5));
128 test->add(BOOST_TEST_CASE(test_async_6));
129
130 return test;
131 }