X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=tests%2Fthread.cpp;fp=tests%2Fthread.cpp;h=9529af85caacaf9e85fba0ad867fe0a2338a9148;hp=0000000000000000000000000000000000000000;hb=120023d8da0aabcb803a87111608ce84c94661f8;hpb=79482ba7aea1b79c7a310c940cc0292532ef3bcb diff --git a/tests/thread.cpp b/tests/thread.cpp new file mode 100644 index 0000000..9529af8 --- /dev/null +++ b/tests/thread.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class ThreadTests: public Test::RegisteredTest +{ +private: + std::vector data; + +public: + ThreadTests(); + + static const char *get_name() { return "Thread"; } + +private: + void single(); + void multiple(); +}; + + +class TestThread: public Thread +{ +private: + int *start; + unsigned count; + unsigned step; + int value; + bool done; + +public: + TestThread(int *, unsigned, unsigned, int); + +private: + virtual void main(); + +public: + bool is_done() const { return done; } +}; + + +ThreadTests::ThreadTests(): + data(1000000) +{ + add(&ThreadTests::single, "Single thread"); + add(&ThreadTests::multiple, "Multiple threads"); +} + +void ThreadTests::single() +{ + fill(data.begin(), data.end(), -1); + + TestThread thread(&data[0], data.size(), 1, 1); + unsigned wait = 100; + while(wait && !thread.is_done()) + { + Time::sleep(100*Time::msec); + --wait; + } + + if(!wait) + fail("Thread did not finish"); + + thread.join(); + + for(vector::iterator i=data.begin(); i!=data.end(); ++i) + if(*i!=1) + fail("Invalid data"); +} + +void ThreadTests::multiple() +{ + fill(data.begin(), data.end(), -1); + + list threads; + for(unsigned i=0; i<10; ++i) + threads.push_back(new TestThread(&data[i], data.size()/10, 10, i+1)); + unsigned wait = 100; + while(wait && !threads.empty()) + { + Time::sleep(100*Time::msec); + for(list::iterator i=threads.begin(); i!=threads.end();) + { + if((*i)->is_done()) + { + (*i)->join(); + delete *i; + threads.erase(i++); + } + else + ++i; + } + --wait; + } + + if(!wait) + fail("Threads did not finish"); + + for(unsigned i=0; i(i%10+1)) + fail("Invalid data"); +} + + +TestThread::TestThread(int *s, unsigned c, unsigned t, int v): + start(s), + count(c), + step(t), + value(v), + done(false) +{ + launch(); +} + +void TestThread::main() +{ + int *ptr = start; + for(unsigned i=0; i