Add TracketMutex unit test

This commit is contained in:
Vinnie Falco
2013-08-23 17:49:14 -07:00
parent 04f9270772
commit db05b9ff04
3 changed files with 103 additions and 0 deletions

View File

@@ -102,6 +102,11 @@ void UnitTest::logMessage (String const& message)
m_runner->logMessage (message);
}
void UnitTest::logReport (StringArray const& report)
{
m_runner->logReport (report);
}
void UnitTest::beginTestCase (String const& name)
{
finishCase ();
@@ -364,6 +369,12 @@ void UnitTests::logMessage (const String& message)
Logger::writeToLog (message);
}
void UnitTests::logReport (StringArray const& report)
{
for (int i = 0; i < report.size (); ++i)
logMessage (report [i]);
}
void UnitTests::runTest (UnitTest& test)
{
try

View File

@@ -309,6 +309,8 @@ public:
*/
void logMessage (const String& message);
void logReport (StringArray const& report);
private:
void finishCase ();
@@ -490,6 +492,11 @@ protected:
*/
virtual void logMessage (String const& message);
/** Logs a report about the current test progress.
This calls logMessage for each String.
*/
virtual void logReport (StringArray const& report);
private:
void runTest (UnitTest& test);

View File

@@ -474,3 +474,88 @@ String TrackedMutex::makeSourceLocation (char const* fileName, int lineNumber) n
return sourceLocation;
}
//==============================================================================
namespace detail
{
class TrackedMutexUnitTests : public UnitTest
{
public:
typedef TrackedMutexType <CriticalSection> Mutex;
struct LockingThread : public Thread
{
Mutex& m_m1;
Mutex& m_m2;
WaitableEvent m_start;
explicit LockingThread (String name, Mutex& m1, Mutex& m2)
: Thread (name)
, m_m1 (m1)
, m_m2 (m2)
{
startThread ();
}
void waitForStart ()
{
m_start.wait ();
}
void run ()
{
Mutex::ScopedLockType l2 (m_m2, __FILE__, __LINE__);
{
Mutex::ScopedLockType l1 (m_m1, __FILE__, __LINE__);
m_start.signal ();
{
Mutex::ScopedUnlockType ul1 (m_m1, __FILE__, __LINE__);
this->wait ();
}
}
}
};
//--------------------------------------------------------------------------
void report (String name)
{
beginTestCase (name);
StringArray report;
TrackedMutex::generateGlobalBlockedReport (report);
logReport (report);
pass ();
}
void runTest ()
{
Mutex m1 ("M1", __FILE__, __LINE__);
Mutex m2 ("M2", __FILE__, __LINE__);
{
Mutex::ScopedLockType l1 (m1, __FILE__, __LINE__);
LockingThread t1 ("T1", m1, m2);
{
Mutex::ScopedUnlockType ul1 (m1, __FILE__, __LINE__);
t1.waitForStart ();
}
report ("#1");
{
t1.notify ();
Mutex::ScopedUnlockType ul1 (m1, __FILE__, __LINE__);
t1.waitForThreadToExit ();
}
}
}
TrackedMutexUnitTests () : UnitTest ("TrackedMutex", "beast", runManual)
{
}
};
static TrackedMutexUnitTests trackedMutexUnitTests;
} // namespace detail