2005-11-09 19:17:31 +03:00
|
|
|
#include "ExampleTest.h"
|
|
|
|
|
2002-07-11 07:34:41 +04:00
|
|
|
#include <ThreadedTestCaller.h>
|
|
|
|
#include <cppunit/Test.h>
|
|
|
|
#include <cppunit/TestCaller.h>
|
|
|
|
#include <cppunit/TestSuite.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <kernel/OS.h>
|
2002-07-19 10:45:28 +04:00
|
|
|
#include <TestUtils.h>
|
2002-07-11 07:34:41 +04:00
|
|
|
|
|
|
|
ExampleTest::ExampleTest(std::string name)
|
|
|
|
: BThreadedTestCase(name)
|
|
|
|
, fLocker(new BLocker())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CppUnit::Test*
|
|
|
|
ExampleTest::Suite() {
|
|
|
|
CppUnit::TestSuite *suite = new CppUnit::TestSuite("Yo");
|
|
|
|
BThreadedTestCaller<ExampleTest> *caller;
|
|
|
|
|
|
|
|
// Add a multithreaded test
|
|
|
|
ExampleTest *test = new ExampleTest("This name is never used, just so you know :-)");
|
2002-07-17 14:50:55 +04:00
|
|
|
caller = new BThreadedTestCaller<ExampleTest>("ExampleTests::MultiThreaded Test #1", test);
|
2002-07-11 07:34:41 +04:00
|
|
|
caller->addThread("A", &ExampleTest::TestFunc1);
|
|
|
|
caller->addThread("B", &ExampleTest::TestFunc2);
|
|
|
|
caller->addThread("C", &ExampleTest::TestFunc3);
|
|
|
|
suite->addTest(caller);
|
|
|
|
|
|
|
|
// And another
|
2002-07-17 14:50:55 +04:00
|
|
|
caller = new BThreadedTestCaller<ExampleTest>("ExampleTests::MultiThreaded Test #2");
|
2002-07-11 07:34:41 +04:00
|
|
|
caller->addThread("Thread1", &ExampleTest::TestFunc1);
|
|
|
|
caller->addThread("Thread2", &ExampleTest::TestFunc1);
|
|
|
|
caller->addThread("Thread3", &ExampleTest::TestFunc1);
|
|
|
|
suite->addTest(caller);
|
|
|
|
|
2002-07-19 10:45:28 +04:00
|
|
|
// And one that fails, if you're so inclined
|
|
|
|
caller = new BThreadedTestCaller<ExampleTest>("ExampleTests::MultiThreaded Failing Test");
|
|
|
|
caller->addThread("GoodThread1", &ExampleTest::TestFunc1);
|
|
|
|
caller->addThread("GoodThread2", &ExampleTest::TestFunc2);
|
|
|
|
caller->addThread("BadThread", &ExampleTest::FailureFunc);
|
|
|
|
suite->addTest(caller);
|
|
|
|
|
2002-07-11 07:34:41 +04:00
|
|
|
// And some single threaded ones
|
2002-07-17 14:50:55 +04:00
|
|
|
suite->addTest(new CppUnit::TestCaller<ExampleTest>("ExampleTests::SingleThreaded Test #1", &ExampleTest::TestFunc1));
|
|
|
|
suite->addTest(new CppUnit::TestCaller<ExampleTest>("ExampleTests::SingleThreaded Test #2", &ExampleTest::TestFunc2));
|
2002-07-11 07:34:41 +04:00
|
|
|
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int sleeptime = 10000;
|
|
|
|
|
|
|
|
void
|
|
|
|
ExampleTest::TestFunc1() {
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
// Get the lock and do our business
|
|
|
|
NextSubTest();
|
|
|
|
fLocker->Lock();
|
|
|
|
fNum += 10;
|
|
|
|
fLocker->Unlock();
|
|
|
|
snooze(sleeptime);
|
2002-07-17 14:50:55 +04:00
|
|
|
// Outputf("(1:%d)", i);
|
2002-07-11 07:34:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExampleTest::TestFunc2() {
|
|
|
|
for (int i = 0; i < 13; i++) {
|
|
|
|
// Get the lock and do our business
|
|
|
|
NextSubTest();
|
|
|
|
fLocker->Lock();
|
|
|
|
fNum *= 2;
|
|
|
|
fLocker->Unlock();
|
|
|
|
snooze(sleeptime);
|
2002-07-17 14:50:55 +04:00
|
|
|
// Outputf("(2:%d)", i);
|
2002-07-11 07:34:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExampleTest::TestFunc3() {
|
|
|
|
for (int i = 0; i < 15; i++) {
|
|
|
|
// Get the lock and do our business
|
|
|
|
NextSubTest();
|
|
|
|
fLocker->Lock();
|
|
|
|
fNum += 10;
|
|
|
|
fLocker->Unlock();
|
|
|
|
snooze(sleeptime);
|
2002-07-17 14:50:55 +04:00
|
|
|
// Outputf("(3:%d)", i);
|
2002-07-11 07:34:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-19 10:45:28 +04:00
|
|
|
void
|
|
|
|
ExampleTest::FailureFunc() {
|
|
|
|
CHK(true == false);
|
|
|
|
}
|
|
|
|
|