From 5e579b5abce70fc1ead45161e1b9ad7ac44a7102 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 2 Jan 2009 08:07:55 +0100 Subject: [PATCH] simple test creating 20 threads... --- tests/40components.tests | 5 ++ tests/lib/Makefile.am | 1 + tests/lib/thread-wrapper-test.cpp | 117 ++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tests/lib/thread-wrapper-test.cpp diff --git a/tests/40components.tests b/tests/40components.tests index a9bcad005..c9666cb21 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -332,6 +332,11 @@ return: 0 END +TEST "Create 20 Threads passing context" ThreadWrapper_test < Testgroup=ALL diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index 0e9efe563..52dff04b0 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -63,6 +63,7 @@ test_lib_SOURCES = \ $(testlib_srcdir)/sync-classlock-test.cpp \ $(testlib_srcdir)/sync-locking-test.cpp \ $(testlib_srcdir)/sync-waiting-test.cpp \ + $(testlib_srcdir)/thread-wrapper-test.cpp \ $(testlib_srcdir)/test/cmdlinewrappertest.cpp \ $(testlib_srcdir)/test/testoptiontest.cpp \ $(testlib_srcdir)/vectortransfertest.cpp \ diff --git a/tests/lib/thread-wrapper-test.cpp b/tests/lib/thread-wrapper-test.cpp new file mode 100644 index 000000000..30bd1e24c --- /dev/null +++ b/tests/lib/thread-wrapper-test.cpp @@ -0,0 +1,117 @@ +/* + ThreadWrapper(Test) - starting threads and passing context + + Copyright (C) Lumiera.org + 2008, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +* *****************************************************/ + + +#include "lib/test/run.hpp" + +#include "include/symbol.hpp" +#include "lib/thread-wrapper.hpp" +#include "lib/sync.hpp" + +#include + +using std::tr1::bind; +using test::Test; + + + +namespace lumiera { + namespace test { + + namespace { // private test classes and data... + + ulong sum; + ulong checksum; + + const uint NUM_THREADS = 20; + const uint MAX_RAND_SUMMAND = 100; + + uint + createVal() ///< generating test values, remembering the sum + { + uint val(rand() % MAX_RAND_SUMMAND); + checksum += val; + return val; + } + + + struct TestThread : Thread + { + TestThread() + : Thread("test Thread creation", + bind (&TestThread::theOperation, this, createVal(), createVal())) + { } // note the binding (functor object) is passed as anonymous temporary + + + void + theOperation (uint a, uint b) ///< the actual operation running in a separate thread + { + Lock sync(this); // *not* a recursive lock, because parent unlocks prior to invoking the operation + sum += (a+b); + } + }; + + } // (End) test classes and data.... + + + + + + + + + + + /************************************************************************** + * @test use the Lumiera backend to create some new threads, utilising the + * lumiera::Thread wrapper for binding to an arbitrary operation + * and passing the appropriate context. + * + * @see lumiera::Thread + * @see threads.h + */ + class ThreadWrapper_test : public Test + { + + virtual void + run (Arg) + { + sum = checksum = 0; + TestThread instances[NUM_THREADS] SIDEEFFECT; + + ASSERT (0 < sum); + ASSERT (sum==checksum); + } + + + }; + + + + /** Register this test class... */ + LAUNCHER (ThreadWrapper_test, "function common"); + + + + } // namespace test + +} // namespace lumiera