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)
14 #include <boost/array.hpp>
15 #include <boost/assert.hpp>
16 #include <boost/test/unit_test.hpp>
17 #include <boost/utility.hpp>
19 #include <boost/context/execution_context.hpp>
20 #include <boost/context/detail/config.hpp>
26 namespace ctx = boost::context;
33 void seh( bool & catched) {
37 } __except( EXCEPTION_EXECUTE_HANDLER) {
43 void fn1( void * vp) {
45 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
49 void fn2( int i, void * vp) {
51 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
55 void fn3( const char * what, void * vp) {
57 throw std::runtime_error( what);
58 } catch ( std::runtime_error const& e) {
61 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
65 void fn4( double d, void * vp) {
68 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
72 void fn6( void * vp) {
74 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
78 void fn5( void * vp) {
79 std::cout << "fn5: entered" << std::endl;
80 ctx::execution_context ectx( fn6);
81 boost::context::execution_context ctx( boost::context::execution_context::current() );
84 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
88 void fn7( void * vp) {
89 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
92 void * data = ( * mctx)( vp);
95 } catch ( std::runtime_error const& e) {
102 int foo( int i, void * vp) {
104 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
112 ctx::execution_context ectx( fn1);
113 boost::context::execution_context ctx( boost::context::execution_context::current() );
115 BOOST_CHECK_EQUAL( 3, value1);
118 void test_variadric() {
120 ctx::execution_context ectx( fn2, 5);
121 boost::context::execution_context ctx( boost::context::execution_context::current() );
123 BOOST_CHECK_EQUAL( 5, value1);
129 ctx::execution_context ectx( & X::foo, x, 7);
130 boost::context::execution_context ctx( boost::context::execution_context::current() );
132 BOOST_CHECK_EQUAL( 7, value1);
135 void test_exception() {
137 const char * what = "hello world";
138 ctx::execution_context ectx( fn3, what);
139 boost::context::execution_context ctx( boost::context::execution_context::current() );
141 BOOST_CHECK_EQUAL( std::string( what), value2);
145 bool catched = false;
146 ctx::execution_context ectx([&catched]( void * vp){
147 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
151 boost::context::execution_context ctx( boost::context::execution_context::current() );
153 BOOST_CHECK( catched);
160 ctx::execution_context ectx( fn4, d);
161 boost::context::execution_context ctx( boost::context::execution_context::current() );
163 BOOST_CHECK_EQUAL( 10.58, value3);
166 void test_stacked() {
169 ctx::execution_context ectx( fn5);
170 boost::context::execution_context ctx( boost::context::execution_context::current() );
172 BOOST_CHECK_EQUAL( 3, value1);
173 BOOST_CHECK_EQUAL( 3.14, value3);
176 void test_prealloc() {
178 ctx::default_stack alloc;
179 ctx::stack_context sctx( alloc.allocate() );
180 void * sp = static_cast< char * >( sctx.sp) - 10;
181 std::size_t size = sctx.size - 10;
182 ctx::execution_context ectx( std::allocator_arg, ctx::preallocated( sp, size, sctx), alloc, fn2, 7);
183 boost::context::execution_context ctx( boost::context::execution_context::current() );
185 BOOST_CHECK_EQUAL( 7, value1);
191 ctx::execution_context ectx( fn7);
192 boost::context::execution_context ctx( boost::context::execution_context::current() );
194 BOOST_CHECK_EQUAL( 3, value1);
195 BOOST_CHECK( value2.empty() );
196 std::string str("abc");
198 void * data = ectx( ctx::exec_ontop_arg,
204 BOOST_CHECK_EQUAL( 7, value1);
205 BOOST_CHECK_EQUAL( str, value2);
206 BOOST_CHECK_EQUAL( data, & i);
207 BOOST_CHECK_EQUAL( i, *( int*) data);
210 void test_ontop_exception() {
213 ctx::execution_context ectx( fn7);
214 boost::context::execution_context ctx( boost::context::execution_context::current() );
216 BOOST_CHECK_EQUAL( 3, value1);
217 const char * what = "hello world";
218 ectx( ctx::exec_ontop_arg,
220 throw std::runtime_error( what);
224 BOOST_CHECK_EQUAL( 3, value1);
225 BOOST_CHECK_EQUAL( std::string( what), value2);
229 void test_test_bug12215() {
230 ctx::execution_context ectx(
232 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
233 char buffer[MAX_PATH];
234 GetModuleFileName( nullptr, buffer, MAX_PATH);
237 boost::context::execution_context ctx( boost::context::execution_context::current() );
242 boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
244 boost::unit_test::test_suite * test =
245 BOOST_TEST_SUITE("Boost.Context: execution_context v1 test suite");
247 test->add( BOOST_TEST_CASE( & test_ectx) );
248 test->add( BOOST_TEST_CASE( & test_variadric) );
249 test->add( BOOST_TEST_CASE( & test_memfn) );
250 test->add( BOOST_TEST_CASE( & test_exception) );
251 test->add( BOOST_TEST_CASE( & test_fp) );
252 test->add( BOOST_TEST_CASE( & test_stacked) );
253 test->add( BOOST_TEST_CASE( & test_prealloc) );
254 test->add( BOOST_TEST_CASE( & test_ontop) );
255 test->add( BOOST_TEST_CASE( & test_ontop_exception) );
257 test->add( BOOST_TEST_CASE( & test_test_bug12215) );