Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58000 - sandbox/fiber/libs/fiber/test
From: oliver.kowalke_at_[hidden]
Date: 2009-11-28 06:44:57


Author: olli
Date: 2009-11-28 06:44:56 EST (Sat, 28 Nov 2009)
New Revision: 58000
URL: http://svn.boost.org/trac/boost/changeset/58000

Log:
-added

Added:
   sandbox/fiber/libs/fiber/test/test_interrupt.cpp (contents, props changed)
   sandbox/fiber/libs/fiber/test/test_scheduler.cpp (contents, props changed)

Added: sandbox/fiber/libs/fiber/test/test_interrupt.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/test/test_interrupt.cpp 2009-11-28 06:44:56 EST (Sat, 28 Nov 2009)
@@ -0,0 +1,242 @@
+
+// Copyright Oliver Kowalke 2009.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#include <sstream>
+#include <stdexcept>
+#include <string>
+
+#include <boost/test/unit_test.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/fiber.hpp>
+
+int value1 = 0;
+int value2 = 0;
+bool interrupted = false;
+
+void fn_1()
+{
+ try
+ {
+ for ( int i = 0; i < 5; ++i)
+ {
+ ++value1;
+ boost::this_fiber::interruption_point();
+ boost::this_fiber::yield();
+ }
+ }
+ catch ( boost::fibers::fiber_interrupted const&)
+ { interrupted = true; }
+}
+
+void fn_2()
+{
+ boost::this_fiber::disable_interruption disabler;
+ if ( boost::this_fiber::interruption_enabled() )
+ throw std::logic_error("interruption enabled");
+ for ( int i = 0; i < 5; ++i)
+ {
+ ++value1;
+ boost::this_fiber::interruption_point();
+ boost::this_fiber::yield();
+ }
+}
+
+void fn_3()
+{
+ try
+ {
+ boost::this_fiber::disable_interruption disabler;
+ if ( boost::this_fiber::interruption_enabled() )
+ throw std::logic_error("interruption enabled");
+ for ( int i = 0; i < 5; ++i)
+ {
+ ++value1;
+ boost::this_fiber::restore_interruption restorer( disabler);
+ boost::this_fiber::interruption_point();
+ boost::this_fiber::yield();
+ }
+ }
+ catch ( boost::fibers::fiber_interrupted const&)
+ { interrupted = true; }
+}
+
+void fn_5( boost::fiber f)
+{
+ for ( int i = 0; i < 5; ++i)
+ {
+ ++value2;
+ if ( i == 1) f.interrupt();
+ if ( i >= 1)
+ {
+ if ( ! f.interruption_requested() )
+ throw std::logic_error("");
+ }
+ boost::this_fiber::yield();
+ }
+}
+
+void test_case_1()
+{
+ value1 = 0;
+ value2 = 0;
+ interrupted = false;
+
+ boost::fibers::scheduler<> sched;
+
+ boost::fiber f( fn_1);
+ sched.submit_fiber( f);
+ sched.make_fiber( fn_5, f);
+
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( false, interrupted);
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( false, interrupted);
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( false, interrupted);
+ BOOST_CHECK_EQUAL( 2, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( false, interrupted);
+ BOOST_CHECK_EQUAL( 2, value1);
+ BOOST_CHECK_EQUAL( 2, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( true, interrupted);
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 2, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( true, interrupted);
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 3, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( true, interrupted);
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 4, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( true, interrupted);
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 5, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( true, interrupted);
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 5, value2);
+
+ BOOST_CHECK( ! sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( true, interrupted);
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 5, value2);
+}
+
+void test_case_2()
+{
+ value1 = 0;
+ value2 = 0;
+ interrupted = false;
+
+ boost::fibers::scheduler<> sched;
+
+ boost::fiber f( fn_2);
+ sched.submit_fiber( f);
+ sched.make_fiber( fn_5, f);
+
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+ BOOST_CHECK_EQUAL( false, interrupted);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+
+ for (;;)
+ {
+ while ( sched.run() );
+ if ( sched.empty() ) break;
+ }
+
+ BOOST_CHECK( ! sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 5, value1);
+ BOOST_CHECK_EQUAL( 5, value2);
+ BOOST_CHECK_EQUAL( false, interrupted);
+}
+
+void test_case_3()
+{
+ value1 = 0;
+ value2 = 0;
+ interrupted = false;
+
+ boost::fibers::scheduler<> sched;
+
+ boost::fiber f( fn_3);
+ sched.submit_fiber( f);
+ sched.make_fiber( fn_5, f);
+
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+ BOOST_CHECK_EQUAL( false, interrupted);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+
+ for (;;)
+ {
+ while ( sched.run() );
+ if ( sched.empty() ) break;
+ }
+
+ BOOST_CHECK( ! sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 5, value2);
+ BOOST_CHECK_EQUAL( true, interrupted);
+}
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test =
+ BOOST_TEST_SUITE("Boost.Fiber: interrupt test suite");
+
+ test->add( BOOST_TEST_CASE( & test_case_1) );
+ test->add( BOOST_TEST_CASE( & test_case_2) );
+ test->add( BOOST_TEST_CASE( & test_case_3) );
+
+ return test;
+}

Added: sandbox/fiber/libs/fiber/test/test_scheduler.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/test/test_scheduler.cpp 2009-11-28 06:44:56 EST (Sat, 28 Nov 2009)
@@ -0,0 +1,277 @@
+
+// Copyright Oliver Kowalke 2009.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#include <sstream>
+#include <string>
+
+#include <boost/ref.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/fiber.hpp>
+
+int value1 = 0;
+int value2 = 0;
+int value3 = 0;
+
+void zero_args_fn() {}
+
+void value1_fn()
+{ value1 = 1; }
+
+void value2_fn()
+{ value2 = 1; }
+
+void value3_fn()
+{ value3 = 1; }
+
+void sched1_fn()
+{
+ boost::fibers::scheduler<> sched;
+
+ sched.make_fiber( value1_fn);
+ sched.make_fiber( value2_fn);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ for (;;)
+ {
+ while ( sched.run() );
+ if ( sched.empty() ) break;
+ }
+
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+}
+
+void sched2_fn( boost::fibers::scheduler<> & sched)
+{
+ sched.make_fiber( value1_fn);
+ sched.make_fiber( value2_fn);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ for (;;)
+ {
+ while ( sched.run() );
+ if ( sched.empty() ) break;
+ }
+
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+}
+
+void sched3_fn( boost::fibers::scheduler<> & sched)
+{
+ sched.make_fiber( value1_fn);
+ sched.make_fiber( value2_fn);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ boost::this_fiber::yield();
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+
+ BOOST_CHECK( ! sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+}
+
+void test_case_1()
+{
+ value1 = 0;
+ value2 = 0;
+
+ boost::fibers::scheduler<> sched;
+
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK( ! sched.run() );
+
+ boost::fiber f( boost::fibers::make_fiber( zero_args_fn) );
+ BOOST_CHECK( ! f.is_alive() );
+ sched.submit_fiber( f);
+ BOOST_CHECK( f.is_alive() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+
+ sched.submit_fiber(
+ boost::fibers::make_fiber( zero_args_fn) );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+
+ BOOST_CHECK( ! f.is_alive() );
+}
+
+void test_case_2()
+{
+ value1 = 0;
+ value2 = 0;
+
+ boost::fibers::scheduler<> sched;
+
+ sched.make_fiber( value1_fn);
+ sched.make_fiber( value2_fn);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+
+ BOOST_CHECK( ! sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+}
+
+void test_case_3()
+{
+ value1 = 0;
+ value2 = 0;
+
+ boost::fibers::scheduler<> sched;
+
+ sched.make_fiber( sched1_fn);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ for (;;)
+ {
+ while ( sched.run() );
+ if ( sched.empty() ) break;
+ }
+
+ BOOST_CHECK( ! sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+}
+
+void test_case_4()
+{
+ value1 = 0;
+ value2 = 0;
+
+ boost::fibers::scheduler<> sched1, sched2;
+
+ sched1.make_fiber( sched2_fn, boost::ref( sched2) );
+ BOOST_CHECK( ! sched1.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched1.size() );
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ for (;;)
+ {
+ while ( sched1.run() );
+ if ( sched1.empty() ) break;
+ }
+
+ BOOST_CHECK( sched1.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched1.size() );
+ BOOST_CHECK( sched2.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched2.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+}
+
+void test_case_5()
+{
+ value1 = 0;
+ value2 = 0;
+ value3 = 0;
+
+ boost::fibers::scheduler<> sched1, sched2;
+
+ sched1.make_fiber( sched3_fn, boost::ref( sched2) );
+ sched1.make_fiber( value3_fn);
+ BOOST_CHECK( ! sched1.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched1.size() );
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+ BOOST_CHECK_EQUAL( 0, value3);
+
+ BOOST_CHECK( sched1.run() );
+ BOOST_CHECK( ! sched1.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched1.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+ BOOST_CHECK_EQUAL( 0, value3);
+
+ BOOST_CHECK( sched1.run() );
+ BOOST_CHECK( ! sched1.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched1.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+ BOOST_CHECK_EQUAL( 1, value3);
+
+ BOOST_CHECK( sched1.run() );
+ BOOST_CHECK( sched1.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched1.size() );
+ BOOST_CHECK( sched2.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched2.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+ BOOST_CHECK_EQUAL( 1, value3);
+}
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test =
+ BOOST_TEST_SUITE("Boost.Fiber: scheduler test suite");
+
+ test->add( BOOST_TEST_CASE( & test_case_1) );
+ test->add( BOOST_TEST_CASE( & test_case_2) );
+ test->add( BOOST_TEST_CASE( & test_case_3) );
+ test->add( BOOST_TEST_CASE( & test_case_4) );
+ test->add( BOOST_TEST_CASE( & test_case_5) );
+
+ return test;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk