Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50465 - sandbox/interthreads/libs/interthreads/test
From: vicente.botet_at_[hidden]
Date: 2009-01-04 13:04:10


Author: viboes
Date: 2009-01-04 13:04:09 EST (Sun, 04 Jan 2009)
New Revision: 50465
URL: http://svn.boost.org/trac/boost/changeset/50465

Log:
interthreads version 0.2
Adding threader/joiner
Adding Asynchronous Executors fmk
Added:
   sandbox/interthreads/libs/interthreads/test/test_basic_threader.cpp (contents, props changed)
   sandbox/interthreads/libs/interthreads/test/test_launcher.cpp (contents, props changed)
   sandbox/interthreads/libs/interthreads/test/test_threader.cpp (contents, props changed)
Text files modified:
   sandbox/interthreads/libs/interthreads/test/Jamfile.v2 | 19 +++++++++++++------
   1 files changed, 13 insertions(+), 6 deletions(-)

Modified: sandbox/interthreads/libs/interthreads/test/Jamfile.v2
==============================================================================
--- sandbox/interthreads/libs/interthreads/test/Jamfile.v2 (original)
+++ sandbox/interthreads/libs/interthreads/test/Jamfile.v2 2009-01-04 13:04:09 EST (Sun, 04 Jan 2009)
@@ -21,27 +21,34 @@
 
 project
     : requirements <library>/boost/test//boost_unit_test_framework/<link>static
- <library>/boost/thread//boost_thread/<link>static
+ <library>../build//boost_interthreads
+
+ <include>.
       <include>../../..
       <threading>multi
       <target-os>cygwin
       <threadapi>pthread
       <variant>debug
+ <define>BOOST_THREAD_HAS_THREAD_ATTR
 
     ;
 
-rule interthreads-run ( sources )
+rule interthreads-run ( sources * )
 {
     return
- [ run $(sources) ../build//boost_interthreads ]
-# [ run $(sources) ../build//boost_interthreads/<link>static
-# : : : : $(sources[1]:B)_lib ]
+ [ run $(sources) : : : <link>static ]
+# [ run $(sources) ../../../../libs/thread/build//boost_thread : : : : $(sources[1]:B)_lib ]
     ;
 }
 
+
+
 {
     test-suite "tests"
         :
+ [ interthreads-run test_basic_threader.cpp ]
+ [ interthreads-run test_launcher.cpp ]
+ [ interthreads-run test_threader.cpp ]
           [ interthreads-run test_thread_decorator.cpp ]
           [ interthreads-run test_thread_shared_ptr.cpp ]
     ;
@@ -49,7 +56,7 @@
         :
           [ interthreads-run ../example/hello_world.cpp ]
           [ interthreads-run ../example/mono_thread_id.cpp ]
- [ interthreads-run ../example/basic_keep_alive.cpp ]
+ [ interthreads-run ../example/basic_keep_alive.cpp ../example/async_ostream.cpp ]
           [ interthreads-run ../example/multiple_algorithms.cpp ]
           
           

Added: sandbox/interthreads/libs/interthreads/test/test_basic_threader.cpp
==============================================================================
--- (empty file)
+++ sandbox/interthreads/libs/interthreads/test/test_basic_threader.cpp 2009-01-04 13:04:09 EST (Sun, 04 Jan 2009)
@@ -0,0 +1,70 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2008. 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)
+//
+// See http://www.boost.org/libs/sync for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include "boost/interthreads/basic_threader.hpp"
+
+#include <iostream>
+#include <boost/test/unit_test.hpp>
+
+using namespace boost::unit_test;
+namespace bith = boost::interthreads;
+
+int test_value;
+void simple_thread() {
+ test_value=999;
+}
+
+void simple_thread_1(unsigned i) {
+ test_value=i;
+}
+
+void test_dir_fork() {
+ bith::basic_threader thr;
+ test_value=0;
+ boost::thread thrd = thr.fork(simple_thread);
+ thrd.join();
+ BOOST_CHECK_EQUAL(test_value, 999);
+}
+
+void test_dir_fork_1() {
+ bith::basic_threader thr;
+ test_value=0;
+ boost::thread thrd = thr.fork(boost::bind(simple_thread_1, 2));
+ thrd.join();
+ BOOST_CHECK_EQUAL(test_value, 2);
+}
+
+void test_indir_fork() {
+ bith::basic_threader thr;
+ test_value=0;
+ boost::thread thrd = bith::fork(thr, simple_thread);
+ thrd.join();
+ BOOST_CHECK_EQUAL(test_value, 999);
+}
+
+void test_indir_fork_1() {
+ bith::basic_threader thr;
+ test_value=0;
+ boost::thread thrd = fork(thr, simple_thread_1, 2);
+ thrd.join();
+ BOOST_CHECK_EQUAL(test_value, 2);
+}
+
+
+test_suite* init_unit_test_suite(int, char*[])
+{
+ test_suite* test = BOOST_TEST_SUITE("basic_threader");
+ test->add(BOOST_TEST_CASE(&test_dir_fork));
+ test->add(BOOST_TEST_CASE(&test_dir_fork_1));
+ test->add(BOOST_TEST_CASE(&test_indir_fork));
+ test->add(BOOST_TEST_CASE(&test_indir_fork_1));
+ return test;
+}
+

Added: sandbox/interthreads/libs/interthreads/test/test_launcher.cpp
==============================================================================
--- (empty file)
+++ sandbox/interthreads/libs/interthreads/test/test_launcher.cpp 2009-01-04 13:04:09 EST (Sun, 04 Jan 2009)
@@ -0,0 +1,76 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2008. 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)
+//
+// See http://www.boost.org/libs/sync for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include "boost/interthreads/launcher.hpp"
+
+#include <iostream>
+#include <boost/test/unit_test.hpp>
+
+using namespace boost::unit_test;
+namespace bith = boost::interthreads;
+
+int test_value;
+int simple_thread() {
+ test_value=999;
+ return test_value;
+}
+
+int simple_thread_1(unsigned i) {
+ test_value=i;
+ return test_value;
+}
+
+void test_dir_fork() {
+ bith::launcher ae;
+ test_value=0;
+ boost::unique_future<int> fut = ae.fork(simple_thread);
+ int res_value = fut.get();
+ BOOST_CHECK_EQUAL(test_value, 999);
+ BOOST_CHECK_EQUAL(res_value, 999);
+}
+
+void test_dir_fork_1() {
+ bith::launcher ae;
+ test_value=0;
+ boost::unique_future<int> fut = ae.fork(boost::bind(simple_thread_1, 2));
+ int res_value = fut.get();
+ BOOST_CHECK_EQUAL(test_value, 2);
+ BOOST_CHECK_EQUAL(res_value, 2);
+}
+
+void test_indir_fork() {
+ bith::launcher ae;
+ test_value=0;
+ boost::unique_future<int> fut = bith::fork(ae, simple_thread);
+ int res_value = fut.get();
+ BOOST_CHECK_EQUAL(test_value, 999);
+ BOOST_CHECK_EQUAL(res_value, 999);
+}
+
+void test_indir_fork_1() {
+ bith::launcher ae;
+ test_value=0;
+ boost::unique_future<int> fut = fork(ae, simple_thread_1, 2);
+ int res_value = fut.get();
+ BOOST_CHECK_EQUAL(test_value, 2);
+ BOOST_CHECK_EQUAL(res_value, 2);
+}
+
+
+test_suite* init_unit_test_suite(int, char*[])
+{
+ test_suite* test = BOOST_TEST_SUITE("launcher");
+ test->add(BOOST_TEST_CASE(&test_dir_fork));
+ test->add(BOOST_TEST_CASE(&test_dir_fork_1));
+ test->add(BOOST_TEST_CASE(&test_indir_fork));
+ test->add(BOOST_TEST_CASE(&test_indir_fork_1));
+ return test;
+}
+

Added: sandbox/interthreads/libs/interthreads/test/test_threader.cpp
==============================================================================
--- (empty file)
+++ sandbox/interthreads/libs/interthreads/test/test_threader.cpp 2009-01-04 13:04:09 EST (Sun, 04 Jan 2009)
@@ -0,0 +1,213 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2008. 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)
+//
+// See http://www.boost.org/libs/sync for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+
+#include "boost/thread/thread_time.hpp"
+#include "boost/thread/mutex.hpp"
+#include "boost/thread/locks.hpp"
+#include <boost/thread/xtime.hpp>
+#include "boost/interthreads/threader.hpp"
+#include "boost/interthreads/algorithm.hpp"
+
+#include <iostream>
+#include <boost/test/unit_test.hpp>
+
+using namespace boost::unit_test;
+namespace bith = boost::interthreads;
+
+namespace
+{
+inline boost::xtime delay(int secs, int msecs=0, int nsecs=0)
+{
+ const int MILLISECONDS_PER_SECOND = 1000;
+ const int NANOSECONDS_PER_SECOND = 1000000000;
+ const int NANOSECONDS_PER_MILLISECOND = 1000000;
+
+ boost::xtime xt;
+ if (boost::TIME_UTC != boost::xtime_get (&xt, boost::TIME_UTC))
+ BOOST_ERROR ("boost::xtime_get != boost::TIME_UTC");
+
+ nsecs += xt.nsec;
+ msecs += nsecs / NANOSECONDS_PER_MILLISECOND;
+ secs += msecs / MILLISECONDS_PER_SECOND;
+ nsecs += (msecs % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
+ xt.nsec = nsecs % NANOSECONDS_PER_SECOND;
+ xt.sec += secs + (nsecs / NANOSECONDS_PER_SECOND);
+
+ return xt;
+}
+}
+
+void sleep(int sec)
+{
+ boost::xtime t;
+ boost::xtime_get(&t,1);
+ t.sec += sec;
+ boost::thread::sleep(t);
+}
+
+int test_value;
+int simple_thread() {
+ test_value=999;
+ sleep(3);
+ return test_value;
+}
+
+int simple_thread_1(unsigned i) {
+ test_value=i;
+ sleep(5);
+ return test_value;
+}
+
+
+void test_dir_fork() {
+ bith::threader ae;
+ test_value=0;
+ bith::joiner<int> act = ae.fork(simple_thread);
+ BOOST_CHECK_EQUAL(act.interruption_requested(), false);
+ BOOST_CHECK_EQUAL(act.is_ready(), false);
+ BOOST_CHECK_EQUAL(act.has_value(), false);
+ BOOST_CHECK_EQUAL(act.has_exception(), false);
+ BOOST_CHECK_EQUAL(act.joinable(), true);
+ act.detach();
+ BOOST_CHECK_EQUAL(act.joinable(), false);
+ int res_value = act.get();
+ BOOST_CHECK_EQUAL(test_value, 999);
+ BOOST_CHECK_EQUAL(res_value, 999);
+}
+
+void test_dir_fork_1() {
+ bith::threader ae;
+ test_value=0;
+ bith::joiner<int> act = ae.fork(boost::bind(simple_thread_1, 2));
+ act.join();
+ int res_value = act.get();
+ BOOST_CHECK_EQUAL(test_value, 2);
+ BOOST_CHECK_EQUAL(res_value, 2);
+}
+
+void test_indir_fork() {
+ bith::threader ae;
+ test_value=0;
+ bith::joiner<int> act = bith::fork(ae, simple_thread);
+ act.join_until(delay(6));
+ int res_value = act.get();
+ BOOST_CHECK_EQUAL(test_value, 999);
+ BOOST_CHECK_EQUAL(res_value, 999);
+}
+
+void test_indir_fork_1() {
+ bith::threader ae;
+ test_value=0;
+ bith::joiner<int> act = bith::fork(ae, simple_thread_1, 2);
+ //act.wait_until( boost::get_system_time()+boost::posix_time::seconds(6));
+ act.wait_for( boost::posix_time::seconds(6));
+ BOOST_CHECK_EQUAL(act.is_ready(), true);
+ BOOST_CHECK_EQUAL(act.has_value(), true);
+ BOOST_CHECK_EQUAL(act.has_exception(), false);
+ int res_value = act.get();
+ BOOST_CHECK_EQUAL(test_value, 2);
+ BOOST_CHECK_EQUAL(res_value, 2);
+}
+
+struct non_copyable_functor
+ : boost::noncopyable
+{
+ unsigned value;
+ typedef unsigned result_type;
+
+ non_copyable_functor():
+ value(0)
+ {}
+
+ unsigned operator()()
+ {
+ value=999;
+ return value;
+ }
+};
+void do_test_creation_through_reference_wrapper()
+{
+ bith::threader ae;
+ non_copyable_functor f;
+ bith::joiner<unsigned> act = bith::fork(ae, boost::bind(boost::ref(f)));
+
+ unsigned res = act.get();
+ BOOST_CHECK_EQUAL(res, 999u);
+ BOOST_CHECK_EQUAL(f.value, 999u);
+}
+
+struct copyable_functor
+// : std::nullary_function<int>
+{
+ unsigned value;
+ typedef int result_type;
+
+ copyable_functor():
+ value(0)
+ {}
+
+ int operator()()
+ {
+ value=999;
+ return value;
+ }
+};
+void do_test_creation_through_functor()
+{
+ bith::threader ae;
+ copyable_functor f;
+ bith::joiner<int> act = bith::fork(ae, f);
+
+ int res = act.get();
+ BOOST_CHECK_EQUAL(res, 999);
+}
+
+bool interruption_point_thread(boost::mutex* m,bool* failed)
+{
+ boost::mutex::scoped_lock lk(*m);
+ boost::this_thread::interruption_point();
+ *failed=true;
+ return failed;
+}
+
+void do_test_thread_interrupts_at_interruption_point() {
+ bith::threader ae;
+ boost::mutex m;
+ bool failed=false;
+ boost::mutex::scoped_lock lk(m);
+ bith::joiner<bool> act = bith::fork(ae, interruption_point_thread, &m,&failed);
+ act.interrupt();
+ BOOST_CHECK_EQUAL(act.interruption_requested(), true);
+ lk.unlock();
+ act.join();
+ BOOST_CHECK(!failed);
+}
+
+void do_test_fork_all() {
+ bith::threader ae;
+// bith::result_of::fork_all<bith::threader,simple_thread,simple_thread_1(unsigned)> tple =
+// bith::fork_all(ae, simple_thread, boost::bind(simple_thread_1, 2));
+
+}
+
+test_suite* init_unit_test_suite(int, char*[])
+{
+ test_suite* test = BOOST_TEST_SUITE("threader");
+ test->add(BOOST_TEST_CASE(&test_dir_fork));
+ test->add(BOOST_TEST_CASE(&test_dir_fork_1));
+ test->add(BOOST_TEST_CASE(&test_indir_fork));
+ test->add(BOOST_TEST_CASE(&test_indir_fork_1));
+ test->add(BOOST_TEST_CASE(&do_test_thread_interrupts_at_interruption_point));
+ test->add(BOOST_TEST_CASE(&do_test_creation_through_functor));
+ test->add(BOOST_TEST_CASE(&do_test_creation_through_reference_wrapper));
+ test->add(BOOST_TEST_CASE(&do_test_fork_all));
+ 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