diff --git a/modules/beast_core/diagnostic/beast_UnitTest.cpp b/modules/beast_core/diagnostic/beast_UnitTest.cpp index b98859574..0585ec0ab 100644 --- a/modules/beast_core/diagnostic/beast_UnitTest.cpp +++ b/modules/beast_core/diagnostic/beast_UnitTest.cpp @@ -69,10 +69,12 @@ void UnitTest::shutdown() { } -ScopedPointer & UnitTest::run (UnitTests* const runner) +ScopedPointer & 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) { diff --git a/modules/beast_core/diagnostic/beast_UnitTest.h b/modules/beast_core/diagnostic/beast_UnitTest.h index aa5e6a542..7f1d67175 100644 --- a/modules/beast_core/diagnostic/beast_UnitTest.h +++ b/modules/beast_core/diagnostic/beast_UnitTest.h @@ -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 m_suite; ScopedPointer 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 m_results; + Random m_random; }; #endif diff --git a/modules/beast_core/maths/beast_Random.h b/modules/beast_core/maths/beast_Random.h index ff6df7e58..88af60b48 100644 --- a/modules/beast_core/maths/beast_Random.h +++ b/modules/beast_core/maths/beast_Random.h @@ -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 +class Random { public: //==============================================================================