]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/context/test/test_execution_context_v1.impl
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / context / test / test_execution_context_v1.impl
1
2 // Copyright Oliver Kowalke 2009.
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)
6
7 #include <iostream>
8 #include <memory>
9 #include <sstream>
10 #include <stdexcept>
11 #include <string>
12 #include <utility>
13
14 #include <boost/array.hpp>
15 #include <boost/assert.hpp>
16 #include <boost/test/unit_test.hpp>
17 #include <boost/utility.hpp>
18
19 #include <boost/context/all.hpp>
20 #include <boost/context/detail/config.hpp>
21
22 namespace ctx = boost::context;
23
24 int value1 = 0;
25 std::string value2;
26 double value3 = 0.;
27
28 #ifdef BOOST_WINDOWS
29 void seh( bool & catched) {
30 __try {
31 int i = 1;
32 i /= 0;
33 } __except( EXCEPTION_EXECUTE_HANDLER) {
34 catched = true;
35 }
36 }
37 #endif
38
39 void fn1( void * vp) {
40 value1 = 3;
41 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
42 ( * mctx)();
43 }
44
45 void fn2( int i, void * vp) {
46 value1 = i;
47 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
48 ( * mctx)();
49 }
50
51 void fn3( const char * what, void * vp) {
52 try {
53 throw std::runtime_error( what);
54 } catch ( std::runtime_error const& e) {
55 value2 = e.what();
56 }
57 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
58 ( * mctx)();
59 }
60
61 void fn4( double d, void * vp) {
62 d += 3.45;
63 value3 = d;
64 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
65 ( * mctx)();
66 }
67
68 void fn6( void * vp) {
69 value1 = 3;
70 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
71 ( * mctx)();
72 }
73
74 void fn5( void * vp) {
75 std::cout << "fn5: entered" << std::endl;
76 ctx::execution_context ectx( fn6);
77 boost::context::execution_context ctx( boost::context::execution_context::current() );
78 ectx( & ctx);
79 value3 = 3.14;
80 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
81 ( * mctx)();
82 }
83
84 void fn7( void * vp) {
85 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
86 try {
87 value1 = 3;
88 void * data = ( * mctx)( vp);
89 value1 = 7;
90 ( * mctx)( data);
91 } catch ( std::runtime_error const& e) {
92 value2 = e.what();
93 }
94 ( * mctx)();
95 }
96
97 struct X {
98 int foo( int i, void * vp) {
99 value1 = i;
100 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
101 ( * mctx)();
102 return i;
103 }
104 };
105
106 void test_ectx() {
107 value1 = 0;
108 ctx::execution_context ectx( fn1);
109 boost::context::execution_context ctx( boost::context::execution_context::current() );
110 ectx( & ctx);
111 BOOST_CHECK_EQUAL( 3, value1);
112 }
113
114 void test_variadric() {
115 value1 = 0;
116 ctx::execution_context ectx( fn2, 5);
117 boost::context::execution_context ctx( boost::context::execution_context::current() );
118 ectx( & ctx);
119 BOOST_CHECK_EQUAL( 5, value1);
120 }
121
122 void test_memfn() {
123 value1 = 0;
124 X x;
125 ctx::execution_context ectx( & X::foo, x, 7);
126 boost::context::execution_context ctx( boost::context::execution_context::current() );
127 ectx( & ctx);
128 BOOST_CHECK_EQUAL( 7, value1);
129 }
130
131 void test_exception() {
132 {
133 const char * what = "hello world";
134 ctx::execution_context ectx( fn3, what);
135 boost::context::execution_context ctx( boost::context::execution_context::current() );
136 ectx( & ctx);
137 BOOST_CHECK_EQUAL( std::string( what), value2);
138 }
139 #ifdef BOOST_WINDOWS
140 {
141 bool catched = false;
142 ctx::execution_context ectx([&catched]( void * vp){
143 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
144 seh( catched);
145 ( * mctx)();
146 });
147 boost::context::execution_context ctx( boost::context::execution_context::current() );
148 ectx( & ctx);
149 BOOST_CHECK( catched);
150 }
151 #endif
152 }
153
154 void test_fp() {
155 double d = 7.13;
156 ctx::execution_context ectx( fn4, d);
157 boost::context::execution_context ctx( boost::context::execution_context::current() );
158 ectx( & ctx);
159 BOOST_CHECK_EQUAL( 10.58, value3);
160 }
161
162 void test_stacked() {
163 value1 = 0;
164 value3 = 0.;
165 ctx::execution_context ectx( fn5);
166 boost::context::execution_context ctx( boost::context::execution_context::current() );
167 ectx( & ctx);
168 BOOST_CHECK_EQUAL( 3, value1);
169 BOOST_CHECK_EQUAL( 3.14, value3);
170 }
171
172 void test_prealloc() {
173 value1 = 0;
174 ctx::default_stack alloc;
175 ctx::stack_context sctx( alloc.allocate() );
176 void * sp = static_cast< char * >( sctx.sp) - 10;
177 std::size_t size = sctx.size - 10;
178 ctx::execution_context ectx( std::allocator_arg, ctx::preallocated( sp, size, sctx), alloc, fn2, 7);
179 boost::context::execution_context ctx( boost::context::execution_context::current() );
180 ectx( & ctx);
181 BOOST_CHECK_EQUAL( 7, value1);
182 }
183
184 void test_ontop() {
185 value1 = 0;
186 value2 = "";
187 ctx::execution_context ectx( fn7);
188 boost::context::execution_context ctx( boost::context::execution_context::current() );
189 ectx( & ctx);
190 BOOST_CHECK_EQUAL( 3, value1);
191 BOOST_CHECK( value2.empty() );
192 std::string str("abc");
193 int i = 3;
194 void * data = ectx( ctx::exec_ontop_arg,
195 [str](void * data){
196 value2 = str;
197 return data;
198 },
199 & i);
200 BOOST_CHECK_EQUAL( 7, value1);
201 BOOST_CHECK_EQUAL( str, value2);
202 BOOST_CHECK_EQUAL( data, & i);
203 BOOST_CHECK_EQUAL( i, *( int*) data);
204 }
205
206 void test_ontop_exception() {
207 value1 = 0;
208 value2 = "";
209 ctx::execution_context ectx( fn7);
210 boost::context::execution_context ctx( boost::context::execution_context::current() );
211 ectx( & ctx);
212 BOOST_CHECK_EQUAL( 3, value1);
213 const char * what = "hello world";
214 ectx( ctx::exec_ontop_arg,
215 [what](void * data){
216 throw std::runtime_error( what);
217 return data;
218 },
219 nullptr);
220 BOOST_CHECK_EQUAL( 3, value1);
221 BOOST_CHECK_EQUAL( std::string( what), value2);
222 }
223
224 #ifdef BOOST_WINDOWS
225 void test_test_bug12215() {
226 ctx::execution_context ectx(
227 [](void * vp) {
228 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
229 char buffer[MAX_PATH];
230 GetModuleFileName( nullptr, buffer, MAX_PATH);
231 ( * mctx)();
232 });
233 boost::context::execution_context ctx( boost::context::execution_context::current() );
234 ectx( & ctx);
235 }
236 #endif
237
238 boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
239 {
240 boost::unit_test::test_suite * test =
241 BOOST_TEST_SUITE("Boost.Context: execution_context v1 test suite");
242
243 test->add( BOOST_TEST_CASE( & test_ectx) );
244 test->add( BOOST_TEST_CASE( & test_variadric) );
245 test->add( BOOST_TEST_CASE( & test_memfn) );
246 test->add( BOOST_TEST_CASE( & test_exception) );
247 test->add( BOOST_TEST_CASE( & test_fp) );
248 test->add( BOOST_TEST_CASE( & test_stacked) );
249 test->add( BOOST_TEST_CASE( & test_prealloc) );
250 test->add( BOOST_TEST_CASE( & test_ontop) );
251 test->add( BOOST_TEST_CASE( & test_ontop_exception) );
252 #ifdef BOOST_WINDOWS
253 test->add( BOOST_TEST_CASE( & test_test_bug12215) );
254 #endif
255
256 return test;
257 }