#include <Autolock.h>
#include <Looper.h>
#include <OS.h>
#include <TestSuiteAddon.h>
#include <ThreadedTestCase.h>
#include <ThreadedTestCaller.h>
#include <cppunit/TestSuite.h>
#include <cppunit/extensions/HelperMacros.h>
static const bigtime_t SNOOZE_TIME = 250000;
class AutolockLockerTest : public BThreadedTestCase {
public:
AutolockLockerTest(std::string name);
virtual ~AutolockLockerTest();
void Lock_Locker_MatchesThread();
void Construct_AutolockPtr_LocksLocker();
void Construct_AutolockRef_LocksLocker();
static CppUnit::Test* suite();
private:
BLocker* fLocker;
};
AutolockLockerTest::AutolockLockerTest(std::string name)
:
BThreadedTestCase(name),
fLocker(new BLocker)
{
}
AutolockLockerTest::~AutolockLockerTest()
{
delete fLocker;
}
void
AutolockLockerTest::Lock_Locker_MatchesThread()
{
CPPUNIT_ASSERT(fLocker->Lock());
CPPUNIT_ASSERT(fLocker->LockingThread() == find_thread(NULL));
snooze(SNOOZE_TIME);
delete fLocker;
fLocker = new BLocker;
{
BAutolock autolock(fLocker);
CPPUNIT_ASSERT(fLocker->IsLocked());
CPPUNIT_ASSERT(fLocker->LockingThread() == find_thread(NULL));
CPPUNIT_ASSERT(autolock.IsLocked());
}
CPPUNIT_ASSERT(fLocker->LockingThread() != find_thread(NULL));
{
BAutolock autolock(*fLocker);
CPPUNIT_ASSERT(fLocker->IsLocked());
CPPUNIT_ASSERT(fLocker->LockingThread() == find_thread(NULL));
CPPUNIT_ASSERT(autolock.IsLocked());
}
CPPUNIT_ASSERT(fLocker->LockingThread() != find_thread(NULL));
}
void
AutolockLockerTest::Construct_AutolockPtr_LocksLocker()
{
snooze(SNOOZE_TIME / 10);
BAutolock autolock(fLocker);
CPPUNIT_ASSERT(!autolock.IsLocked());
}
void
AutolockLockerTest::Construct_AutolockRef_LocksLocker()
{
snooze(SNOOZE_TIME / 10);
BAutolock autolock(*fLocker);
CPPUNIT_ASSERT(!autolock.IsLocked());
}
CppUnit::Test*
AutolockLockerTest::suite()
{
typedef BThreadedTestCaller<AutolockLockerTest> AutolockLockerTestCaller;
AutolockLockerTest* theTest = new AutolockLockerTest("");
AutolockLockerTestCaller* threadedTest
= new AutolockLockerTestCaller("BAutolock::Locker Test", theTest);
threadedTest->addThread("A", &AutolockLockerTest::Lock_Locker_MatchesThread);
threadedTest->addThread("B", &AutolockLockerTest::Construct_AutolockPtr_LocksLocker);
threadedTest->addThread("C", &AutolockLockerTest::Construct_AutolockRef_LocksLocker);
return threadedTest;
}
class AutolockLooperTest : public BThreadedTestCase {
public:
AutolockLooperTest(std::string name);
virtual ~AutolockLooperTest();
void Construct_AutolockPtr_LocksLooper();
static CppUnit::Test* suite();
private:
BLooper* fLooper;
};
AutolockLooperTest::AutolockLooperTest(std::string name)
:
BThreadedTestCase(name),
fLooper(new BLooper)
{
fLooper->Run();
}
AutolockLooperTest::~AutolockLooperTest()
{
if (fLooper != NULL) {
fLooper->Lock();
fLooper->Quit();
}
}
void
AutolockLooperTest::Construct_AutolockPtr_LocksLooper()
{
BAutolock* autolock = new BAutolock(fLooper);
CPPUNIT_ASSERT(fLooper->IsLocked());
CPPUNIT_ASSERT(fLooper->LockingThread() == find_thread(NULL));
CPPUNIT_ASSERT(autolock->IsLocked());
delete autolock;
CPPUNIT_ASSERT(fLooper->LockingThread() != find_thread(NULL));
}
CppUnit::Test*
AutolockLooperTest::suite()
{
typedef BThreadedTestCaller<AutolockLooperTest> AutolockLooperTestCaller;
AutolockLooperTest* theTest = new AutolockLooperTest("");
AutolockLooperTestCaller* threadedTest
= new AutolockLooperTestCaller("BAutolock::Looper Test", theTest);
threadedTest->addThread("A", &AutolockLooperTest::Construct_AutolockPtr_LocksLooper);
return threadedTest;
}
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AutolockLockerTest, getTestSuiteName());
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AutolockLooperTest, getTestSuiteName());