mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Better random number facilities in UnitTest
This commit is contained in:
@@ -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);
|
bassert (runner != nullptr);
|
||||||
m_runner = runner;
|
m_runner = runner;
|
||||||
|
m_random = m_runner->m_random;
|
||||||
|
|
||||||
m_suite = new Suite (m_className, m_packageName);
|
m_suite = new Suite (m_className, m_packageName);
|
||||||
|
|
||||||
@@ -191,6 +193,14 @@ void UnitTest::failException ()
|
|||||||
m_runner->onFailure ();
|
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 ()
|
void UnitTest::finishCase ()
|
||||||
@@ -339,13 +349,17 @@ UnitTests::TestList UnitTests::selectStartupTests (TestList const& tests) const
|
|||||||
return list;
|
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;
|
m_results = new Results;
|
||||||
for (int i = 0; i < tests.size (); ++i)
|
for (int i = 0; i < tests.size (); ++i)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -311,6 +311,9 @@ public:
|
|||||||
|
|
||||||
void logReport (StringArray const& report);
|
void logReport (StringArray const& report);
|
||||||
|
|
||||||
|
/** Returns a shared RNG that all unit tests should use. */
|
||||||
|
Random& random();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void finishCase ();
|
void finishCase ();
|
||||||
|
|
||||||
@@ -322,6 +325,7 @@ private:
|
|||||||
UnitTests* m_runner;
|
UnitTests* m_runner;
|
||||||
ScopedPointer <Suite> m_suite;
|
ScopedPointer <Suite> m_suite;
|
||||||
ScopedPointer <Case> m_case;
|
ScopedPointer <Case> m_case;
|
||||||
|
Random m_random;
|
||||||
};
|
};
|
||||||
|
|
||||||
//==============================================================================
|
//==============================================================================
|
||||||
@@ -467,13 +471,14 @@ public:
|
|||||||
@param tests An optional parameter containing a list of tests to match.
|
@param tests An optional parameter containing a list of tests to match.
|
||||||
*/
|
*/
|
||||||
void runSelectedTests (String const& 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.
|
/** Runs the specified list of tests.
|
||||||
@note The tests are run regardless of the run settings.
|
@note The tests are run regardless of the run settings.
|
||||||
@param tests The list of tests to run.
|
@param tests The list of tests to run.
|
||||||
*/
|
*/
|
||||||
void runTests (TestList const& tests);
|
void runTests (TestList const& tests, int64 randomSeed = 0);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend class UnitTest;
|
friend class UnitTest;
|
||||||
@@ -503,6 +508,7 @@ private:
|
|||||||
private:
|
private:
|
||||||
bool m_assertOnFailure;
|
bool m_assertOnFailure;
|
||||||
ScopedPointer <Results> m_results;
|
ScopedPointer <Results> m_results;
|
||||||
|
Random m_random;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
You can create a Random object and use it to generate a sequence of random numbers.
|
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:
|
public:
|
||||||
//==============================================================================
|
//==============================================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user