25 #ifndef WTHREADEDFUNCTION_TEST_H
26 #define WTHREADEDFUNCTION_TEST_H
31 #include <cxxtest/TestSuite.h>
33 #include "../WSharedObject.h"
34 #include "../WThreadedFunction.h"
74 for(
int i = 1; i <= *
m_input.get() && !shutdown(); ++i )
146 std::shared_ptr< FuncType > func(
new FuncType( 5 ) );
151 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
153 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
155 TS_ASSERT_EQUALS( f.
status(), W_THREADS_FINISHED );
157 TS_ASSERT_EQUALS( func->getResult(), 15 );
161 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
164 TS_ASSERT_EQUALS( func->getResult(), 15 );
167 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
170 TS_ASSERT_EQUALS( func->getResult(), 30 );
177 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
179 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
181 TS_ASSERT_EQUALS( f.
status(), W_THREADS_FINISHED );
183 TS_ASSERT_EQUALS( func->getResult(), 30 );
190 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
192 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
194 TS_ASSERT_EQUALS( f.
status(), W_THREADS_FINISHED );
196 TS_ASSERT_EQUALS( func->getResult(), 75 );
206 std::shared_ptr< FuncType > func(
new FuncType( 100000000 ) );
209 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
211 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
213 TS_ASSERT_EQUALS( f.
status(), W_THREADS_STOP_REQUESTED );
215 TS_ASSERT_EQUALS( f.
status(), W_THREADS_ABORTED );
217 TS_ASSERT( func->stopped() );
226 std::shared_ptr< FuncType > func(
new FuncType( 5 ) );
229 TS_ASSERT_EQUALS( f.
status(), W_THREADS_INITIALIZED );
231 TS_ASSERT_EQUALS( f.
status(), W_THREADS_RUNNING );
233 TS_ASSERT_EQUALS( f.
status(), W_THREADS_FINISHED );
235 TS_ASSERT_EQUALS( func->getResult(), 90 );
254 TS_ASSERT_EQUALS( f.
status(), 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.
std::shared_ptr< WCondition > getThreadsDoneCondition()
Returns a condition that gets fired when all threads have finished.
WThreadedFunctionStatus status()
Get the status of the threads.
void subscribeExceptionSignal(ExceptionFunction func)
Subscribe a function to an exception signal.
A function that throws exceptions.
void operator()(std::size_t, std::size_t, WBoolFlag &)
The function.
void operator()(std::size_t, std::size_t, WBoolFlag const &shutdown)
This is the actual thread function.
WSharedObject< int > m_result
the result
void reset()
Reset everything.
FuncType(int value)
Constructor, initialize some stuff.
bool stopped()
Check if the thread was ordered to stop.
int getResult()
A method to extract the result.
std::shared_ptr< int const > m_input
the input data
WSharedObject< bool > m_stopped
thread stopped?
Tests the WThreadedFunction class.
void testExceptionHandling()
Exceptions should lead to the status beeing changed to W_THREADS_ABORTED.
void handleException(WException const &)
Exception callback.
void testStopThreads()
Status should be set correctly when threads are ordered to stop.
void testMultipleThreads()
A function computed by multiple threads should correctly set its status and compute the correct resul...
WSharedObject< int > m_exceptionCounter
a counter
void testStopCondition()
The stop condition should be notified correctly.
Creates threads that computes a function in a multithreaded fashion.
virtual void wait()
Wait for all threads to stop.
virtual void stop()
Request all threads to stop.
virtual void run()
Starts the threads.