25 #ifndef WTHREADEDFUNCTION_TEST_H
26 #define WTHREADEDFUNCTION_TEST_H
30 #include <cxxtest/TestSuite.h>
32 #include "../WThreadedFunction.h"
33 #include "../WSharedObject.h"
73 for(
int i = 1; i <= *
m_input.get() && !shutdown(); ++i )
145 boost::shared_ptr< FuncType > func(
new FuncType( 5 ) );
150 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
152 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
154 TS_ASSERT_EQUALS( f.
status(), W_THREADS_FINISHED );
156 TS_ASSERT_EQUALS( func->getResult(), 15 );
160 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
163 TS_ASSERT_EQUALS( func->getResult(), 15 );
166 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
169 TS_ASSERT_EQUALS( func->getResult(), 30 );
176 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
178 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
180 TS_ASSERT_EQUALS( f.
status(), W_THREADS_FINISHED );
182 TS_ASSERT_EQUALS( func->getResult(), 30 );
189 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
191 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
193 TS_ASSERT_EQUALS( f.
status(), W_THREADS_FINISHED );
195 TS_ASSERT_EQUALS( func->getResult(), 75 );
205 boost::shared_ptr< FuncType > func(
new FuncType( 100000000 ) );
208 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
210 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
212 TS_ASSERT_EQUALS( f.
status(), W_THREADS_STOP_REQUESTED );
214 TS_ASSERT_EQUALS( f.
status(), W_THREADS_ABORTED );
216 TS_ASSERT( func->stopped() );
225 boost::shared_ptr< FuncType > func(
new FuncType( 5 ) );
228 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
230 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
232 TS_ASSERT_EQUALS( f.
status(), W_THREADS_FINISHED );
234 TS_ASSERT_EQUALS( func->getResult(), 90 );
253 TS_ASSERT_EQUALS( f.
status(), W_THREADS_ABORTED );
270 #endif // WTHREADEDFUNCTION_TEST_H
WSharedObject< bool > m_stopped
thread stopped?
WSharedObject< int > m_result
the result
Creates threads that computes a function in a multithreaded fashion.
FuncType(int value)
Constructor, initialize some stuff.
Tests the WThreadedFunction class.
virtual void run()
Starts the threads.
void operator()(std::size_t, std::size_t, WBoolFlag const &shutdown)
This is the actual thread function.
boost::shared_ptr< int const > m_input
the input data
int getResult()
A method to extract the result.
WSharedObject< int > m_exceptionCounter
a counter
void reset()
Reset everything.
void handleException(WException const &)
Exception callback.
void testExceptionHandling()
Exceptions should lead to the status beeing changed to W_THREADS_ABORTED.
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
void testStopCondition()
The stop condition should be notified correctly.
void testMultipleThreads()
A function computed by multiple threads should correctly set its status and compute the correct resul...
boost::shared_ptr< WCondition > getThreadsDoneCondition()
Returns a condition that gets fired when all threads have finished.
WThreadedFunctionStatus status()
Get the status of the threads.
void testStopThreads()
Status should be set correctly when threads are ordered to stop.
void subscribeExceptionSignal(ExceptionFunction func)
Subscribe a function to an exception signal.
virtual void wait()
Wait for all threads to stop.
void operator()(std::size_t, std::size_t, WBoolFlag &)
The function.
virtual void stop()
Request all threads to stop.
A function that throws exceptions.
bool stopped()
Check if the thread was ordered to stop.