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/all.hpp>
20 #include <boost/context/detail/config.hpp>
22 namespace ctx = boost::context;
29 void seh( bool & catched) {
33 } __except( EXCEPTION_EXECUTE_HANDLER) {
39 void fn1( void * vp) {
41 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
45 void fn2( int i, void * vp) {
47 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
51 void fn3( const char * what, void * vp) {
53 throw std::runtime_error( what);
54 } catch ( std::runtime_error const& e) {
57 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
61 void fn4( double d, void * vp) {
64 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
68 void fn6( void * vp) {
70 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
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() );
80 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
84 void fn7( void * vp) {
85 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
88 void * data = ( * mctx)( vp);
91 } catch ( std::runtime_error const& e) {
98 int foo( int i, void * vp) {
100 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
108 ctx::execution_context ectx( fn1);
109 boost::context::execution_context ctx( boost::context::execution_context::current() );
111 BOOST_CHECK_EQUAL( 3, value1);
114 void test_variadric() {
116 ctx::execution_context ectx( fn2, 5);
117 boost::context::execution_context ctx( boost::context::execution_context::current() );
119 BOOST_CHECK_EQUAL( 5, value1);
125 ctx::execution_context ectx( & X::foo, x, 7);
126 boost::context::execution_context ctx( boost::context::execution_context::current() );
128 BOOST_CHECK_EQUAL( 7, value1);
131 void test_exception() {
133 const char * what = "hello world";
134 ctx::execution_context ectx( fn3, what);
135 boost::context::execution_context ctx( boost::context::execution_context::current() );
137 BOOST_CHECK_EQUAL( std::string( what), value2);
141 bool catched = false;
142 ctx::execution_context ectx([&catched]( void * vp){
143 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
147 boost::context::execution_context ctx( boost::context::execution_context::current() );
149 BOOST_CHECK( catched);
156 ctx::execution_context ectx( fn4, d);
157 boost::context::execution_context ctx( boost::context::execution_context::current() );
159 BOOST_CHECK_EQUAL( 10.58, value3);
162 void test_stacked() {
165 ctx::execution_context ectx( fn5);
166 boost::context::execution_context ctx( boost::context::execution_context::current() );
168 BOOST_CHECK_EQUAL( 3, value1);
169 BOOST_CHECK_EQUAL( 3.14, value3);
172 void test_prealloc() {
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() );
181 BOOST_CHECK_EQUAL( 7, value1);
187 ctx::execution_context ectx( fn7);
188 boost::context::execution_context ctx( boost::context::execution_context::current() );
190 BOOST_CHECK_EQUAL( 3, value1);
191 BOOST_CHECK( value2.empty() );
192 std::string str("abc");
194 void * data = ectx( ctx::exec_ontop_arg,
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);
206 void test_ontop_exception() {
209 ctx::execution_context ectx( fn7);
210 boost::context::execution_context ctx( boost::context::execution_context::current() );
212 BOOST_CHECK_EQUAL( 3, value1);
213 const char * what = "hello world";
214 ectx( ctx::exec_ontop_arg,
216 throw std::runtime_error( what);
220 BOOST_CHECK_EQUAL( 3, value1);
221 BOOST_CHECK_EQUAL( std::string( what), value2);
225 void test_test_bug12215() {
226 ctx::execution_context ectx(
228 ctx::execution_context * mctx = static_cast< ctx::execution_context * >( vp);
229 char buffer[MAX_PATH];
230 GetModuleFileName( nullptr, buffer, MAX_PATH);
233 boost::context::execution_context ctx( boost::context::execution_context::current() );
238 boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
240 boost::unit_test::test_suite * test =
241 BOOST_TEST_SUITE("Boost.Context: execution_context v1 test suite");
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) );
253 test->add( BOOST_TEST_CASE( & test_test_bug12215) );