diff --git a/modules/juce_core/maths/juce_BigInteger.cpp b/modules/juce_core/maths/juce_BigInteger.cpp index b749c7d7f0..e270823398 100644 --- a/modules/juce_core/maths/juce_BigInteger.cpp +++ b/modules/juce_core/maths/juce_BigInteger.cpp @@ -873,12 +873,12 @@ void BigInteger::inverseModulo (const BigInteger& modulus) while (! a2.isOne()) { - BigInteger temp1, temp2, multiplier (a1); + BigInteger temp1, multiplier (a1); multiplier.divideBy (a2, temp1); temp1 = a2; temp1 *= multiplier; - temp2 = a1; + BigInteger temp2 (a1); temp2 -= temp1; a1 = a2; a2 = temp2; diff --git a/modules/juce_core/maths/juce_Random.cpp b/modules/juce_core/maths/juce_Random.cpp index 850d4d346e..3b4118d376 100644 --- a/modules/juce_core/maths/juce_Random.cpp +++ b/modules/juce_core/maths/juce_Random.cpp @@ -137,4 +137,42 @@ void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numB arrayToChange.setBit (startBit + numBits, nextBool()); } +//============================================================================== +#if JUCE_UNIT_TESTS + +class RandomTests : public UnitTest +{ +public: + RandomTests() : UnitTest ("Random") {} + + void runTest() + { + beginTest ("Random"); + + for (int j = 10; --j >= 0;) + { + Random r; + r.setSeedRandomly(); + + for (int i = 20; --i >= 0;) + { + expect (r.nextDouble() >= 0.0 && r.nextDouble() < 1.0); + expect (r.nextFloat() >= 0.0f && r.nextFloat() < 1.0f); + expect (r.nextInt (5) >= 0 && r.nextInt (5) < 5); + expect (r.nextInt (1) == 0); + + int n = r.nextInt (50) + 1; + expect (r.nextInt (n) >= 0 && r.nextInt (n) < n); + + n = r.nextInt (0x7ffffffe) + 1; + expect (r.nextInt (n) >= 0 && r.nextInt (n) < n); + } + } + } +}; + +static RandomTests randomTests; + +#endif + END_JUCE_NAMESPACE diff --git a/modules/juce_core/unit_tests/juce_UnitTest.cpp b/modules/juce_core/unit_tests/juce_UnitTest.cpp index 00e0a6740f..6626100126 100644 --- a/modules/juce_core/unit_tests/juce_UnitTest.cpp +++ b/modules/juce_core/unit_tests/juce_UnitTest.cpp @@ -76,7 +76,9 @@ void UnitTest::expect (const bool result, const String& failureMessage) //============================================================================== UnitTestRunner::UnitTestRunner() - : currentTest (nullptr), assertOnFailure (false) + : currentTest (nullptr), + assertOnFailure (false), + logPasses (false) { } @@ -84,6 +86,16 @@ UnitTestRunner::~UnitTestRunner() { } +void UnitTestRunner::setAssertOnFailure (bool shouldAssert) noexcept +{ + assertOnFailure = shouldAssert; +} + +void UnitTestRunner::setPassesAreLogged (bool shouldDisplayPasses) noexcept +{ + logPasses = shouldDisplayPasses; +} + int UnitTestRunner::getNumResults() const noexcept { return results.size(); @@ -98,10 +110,9 @@ void UnitTestRunner::resultsUpdated() { } -void UnitTestRunner::runTests (const Array& tests, const bool assertOnFailure_) +void UnitTestRunner::runTests (const Array& tests) { results.clear(); - assertOnFailure = assertOnFailure_; resultsUpdated(); for (int i = 0; i < tests.size(); ++i) @@ -119,9 +130,9 @@ void UnitTestRunner::runTests (const Array& tests, const bool assertO endTest(); } -void UnitTestRunner::runAllTests (const bool assertOnFailure_) +void UnitTestRunner::runAllTests() { - runTests (UnitTest::getAllTests(), assertOnFailure_); + runTests (UnitTest::getAllTests()); } void UnitTestRunner::logMessage (const String& message) @@ -135,11 +146,11 @@ void UnitTestRunner::beginNewTest (UnitTest* const test, const String& subCatego currentTest = test; TestResult* const r = new TestResult(); + results.add (r); r->unitTestName = test->getName(); r->subcategoryName = subCategory; r->passes = 0; r->failures = 0; - results.add (r); logMessage ("-----------------------------------------------------------------"); logMessage ("Starting test: " + r->unitTestName + " / " + subCategory + "..."); @@ -180,9 +191,12 @@ void UnitTestRunner::addPass() r->passes++; - String message ("Test "); - message << (r->failures + r->passes) << " passed"; - logMessage (message); + if (logPasses) + { + String message ("Test "); + message << (r->failures + r->passes) << " passed"; + logMessage (message); + } } resultsUpdated(); diff --git a/modules/juce_core/unit_tests/juce_UnitTest.h b/modules/juce_core/unit_tests/juce_UnitTest.h index f11d93e353..942d984033 100644 --- a/modules/juce_core/unit_tests/juce_UnitTest.h +++ b/modules/juce_core/unit_tests/juce_UnitTest.h @@ -198,12 +198,22 @@ public: The tests are performed in order, and the results are logged. To run all the registered UnitTest objects that exist, use runAllTests(). */ - void runTests (const Array& tests, bool assertOnFailure); + void runTests (const Array& tests); /** Runs all the UnitTest objects that currently exist. This calls runTests() for all the objects listed in UnitTest::getAllTests(). */ - void runAllTests (bool assertOnFailure); + void runAllTests(); + + /** Sets a flag to indicate whether an assertion should be triggered if a test fails. + This is true by default. + */ + void setAssertOnFailure (bool shouldAssert) noexcept; + + /** Sets a flag to indicate whether successful tests should be logged. + By default, this is set to false, so that only failures will be displayed in the log. + */ + void setPassesAreLogged (bool shouldDisplayPasses) noexcept; //============================================================================== /** Contains the results of a test. @@ -257,7 +267,7 @@ private: UnitTest* currentTest; String currentSubCategory; OwnedArray results; - bool assertOnFailure; + bool assertOnFailure, logPasses; void beginNewTest (UnitTest* test, const String& subCategory); void endTest();