Better random number facilities in UnitTest

This commit is contained in:
Vinnie Falco
2013-09-12 19:41:09 -07:00
parent 550b8e55ee
commit e96ce99d3d
3 changed files with 27 additions and 7 deletions

View File

@@ -69,10 +69,12 @@ void UnitTest::shutdown()
{
}
ScopedPointer <UnitTest::Suite>& UnitTest::run (UnitTests* const runner)
ScopedPointer <UnitTest::Suite>& UnitTest::run (
UnitTests* const runner)
{
bassert (runner != nullptr);
m_runner = runner;
m_random = m_runner->m_random;
m_suite = new Suite (m_className, m_packageName);
@@ -191,6 +193,14 @@ void UnitTest::failException ()
m_runner->onFailure ();
}
Random& UnitTest::random()
{
// This method's only valid while the test is being run!
bassert (m_runner != nullptr);
return m_random;
}
//------------------------------------------------------------------------------
void UnitTest::finishCase ()
@@ -339,13 +349,17 @@ UnitTests::TestList UnitTests::selectStartupTests (TestList const& tests) const
return list;
}
void UnitTests::runSelectedTests (String const& match, TestList const& tests)
void UnitTests::runSelectedTests (String const& match, TestList const& tests, int64 randomSeed)
{
runTests (selectTests (match, tests));
runTests (selectTests (match, tests), randomSeed);
}
void UnitTests::runTests (TestList const& tests)
void UnitTests::runTests (TestList const& tests, int64 randomSeed)
{
if (randomSeed == 0)
randomSeed = Random().nextInt (0x7fffffff);
m_random = Random (randomSeed);
m_results = new Results;
for (int i = 0; i < tests.size (); ++i)
{

View File

@@ -311,6 +311,9 @@ public:
void logReport (StringArray const& report);
/** Returns a shared RNG that all unit tests should use. */
Random& random();
private:
void finishCase ();
@@ -322,6 +325,7 @@ private:
UnitTests* m_runner;
ScopedPointer <Suite> m_suite;
ScopedPointer <Case> m_case;
Random m_random;
};
//==============================================================================
@@ -467,13 +471,14 @@ public:
@param tests An optional parameter containing a list of tests to match.
*/
void runSelectedTests (String const& match = "",
TestList const& tests = UnitTest::getAllTests ());
TestList const& tests = UnitTest::getAllTests (),
int64 randomSeed = 0);
/** Runs the specified list of tests.
@note The tests are run regardless of the run settings.
@param tests The list of tests to run.
*/
void runTests (TestList const& tests);
void runTests (TestList const& tests, int64 randomSeed = 0);
protected:
friend class UnitTest;
@@ -503,6 +508,7 @@ private:
private:
bool m_assertOnFailure;
ScopedPointer <Results> m_results;
Random m_random;
};
#endif

View File

@@ -30,7 +30,7 @@
You can create a Random object and use it to generate a sequence of random numbers.
*/
class BEAST_API Random : LeakChecked <Random>
class Random
{
public:
//==============================================================================