From 7675422264a6617ec2e7cb38dfe8df95c5421a0e Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Fri, 11 Feb 2022 15:35:03 +0100 Subject: [PATCH] Translated tests for Random/RandomIntRange --- Test/CMakeLists.txt | 1 + Test/Random_RandomIntRange.cpp | 186 +++++++++++++++++---------------- 2 files changed, 97 insertions(+), 90 deletions(-) diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt index 3a3fd04..c050679 100644 --- a/Test/CMakeLists.txt +++ b/Test/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(Tests Random__RandomFloat.cpp Random__RandomInteger.cpp Random__RandomRange.cpp + Random_RandomIntRange.cpp ) target_link_libraries(Tests Eule) diff --git a/Test/Random_RandomIntRange.cpp b/Test/Random_RandomIntRange.cpp index 9591665..6a7c8de 100644 --- a/Test/Random_RandomIntRange.cpp +++ b/Test/Random_RandomIntRange.cpp @@ -1,100 +1,106 @@ -#include "CppUnitTest.h" +#include "Catch2.h" #include "../_TestingUtilities/Testutil.h" -#include "../Eule/Random.h" +#include #include #include -using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Eule; -namespace _Random +// Checks that a random integer is never outside the specification, two positive values +TEST_CASE(__FILE__"/Never_Outside_Specification__pos__pos", "[Random][RandomIntRange]") { - TEST_CLASS(_RandomIntRange) - { - public: - // Checks that a random integer is never outside the specification, two positive values - TEST_METHOD(Never_Outside_Specification__pos__pos) - { - // Test 1000 random integers - for (std::size_t i = 0; i < 1000; i++) - { - int rnd = Random::RandomIntRange(49, 99); + // Test 1000 random integers + for (std::size_t i = 0; i < 1000; i++) + { + int rnd = Random::RandomIntRange(49, 99); - Assert::IsTrue(rnd >= 49, L"rnd too small"); - Assert::IsTrue(rnd <= 99, L"rnd too big"); - } + INFO("rnd too small"); + REQUIRE(rnd >= 49); + INFO("rnd too big"); + REQUIRE(rnd <= 99); + } - return; - } - - // Checks that a random integer is never outside the specification, negative minimum - TEST_METHOD(Never_Outside_Specification__neg__pos) - { - // Test 1000 random integers - for (std::size_t i = 0; i < 1000; i++) - { - int rnd = Random::RandomIntRange(-39, 99); - - Assert::IsTrue(rnd >= -39, L"rnd too small"); - Assert::IsTrue(rnd <= 99, L"rnd too big"); - } - - return; - } - - // Checks that a random integer is never outside the specification, two negative values - TEST_METHOD(Never_Outside_Specification__neg__neg) - { - // Test 1000 random integers - for (std::size_t i = 0; i < 1000; i++) - { - int rnd = Random::RandomIntRange(-39, -10); - - Assert::IsTrue(rnd >= -39, L"rnd too small"); - Assert::IsTrue(rnd <= -10, L"rnd too big"); - } - - return; - } - - // Checks that the random intrange method also returns the supplied limits - TEST_METHOD(Inclusivity) - { - // Test 1000 random integers - // The chance that any number [0,9] will not drop at least once is basically 0 - - std::array foundDigits; - foundDigits.fill(false); - - for (std::size_t i = 0; i < 1000; i++) - { - int randomVal = Random::RandomIntRange(0, 9); - foundDigits[randomVal] = true; - } - - // Check that each value has been rolled at least once - for (const bool& b : foundDigits) - Assert::IsTrue(b); - - return; - } - - // Checks that the produced integer distribution shows a big standard deviation - TEST_METHOD(Big_Standard_Deviation) - { - // Setup - std::vector rands; - rands.resize(1000); - - // Exercise - // Create 1000 random values - std::generate_n(rands.data(), rands.size(), []()->int { return Random::RandomIntRange(100, (int)4e9); }); - - // Verify - const double stddev = Testutil::Stddev(rands); - Assert::IsTrue(stddev >= 1000000, (std::wstringstream() << stddev).str().c_str()); - - return; - } - }; + return; } + +// Checks that a random integer is never outside the specification, negative minimum +TEST_CASE(__FILE__"/Never_Outside_Specification__neg__pos", "[Random][RandomIntRange]") +{ + // Test 1000 random integers + for (std::size_t i = 0; i < 1000; i++) + { + int rnd = Random::RandomIntRange(-39, 99); + + INFO("rnd too small"); + REQUIRE(rnd >= -39); + INFO("rnd too big"); + REQUIRE(rnd <= 99); + } + + return; +} + +// Checks that a random integer is never outside the specification, two negative values +TEST_CASE(__FILE__"/Never_Outside_Specification__neg__neg", "[Random][RandomIntRange]") +{ + // Test 1000 random integers + for (std::size_t i = 0; i < 1000; i++) + { + int rnd = Random::RandomIntRange(-39, -10); + + INFO("rnd too small"); + REQUIRE(rnd >= -39); + INFO("rnd too big"); + REQUIRE(rnd <= -10); + } + + return; +} + +// Checks that the random intrange method also returns the supplied limits +TEST_CASE(__FILE__"/Inclusivity", "[Random][RandomIntRange]") +{ + // Test 1000 random integers + // The chance that any number [0,9] will not drop at least once is basically 0 + + std::array foundDigits; + foundDigits.fill(false); + + for (std::size_t i = 0; i < 1000; i++) + { + int randomVal = Random::RandomIntRange(0, 9); + foundDigits[randomVal] = true; + } + + // Check that each value has been rolled at least once + for (const bool& b : foundDigits) + REQUIRE(b); + + return; +} + +// Checks that the produced integer distribution shows a big standard deviation +TEST_CASE(__FILE__"/Big_Standard_Deviation", "[Random][RandomIntRange]") +{ + // Setup + std::vector rands; + rands.resize(1000); + + // Exercise + // Create 1000 random values + std::generate_n(rands.data(), rands.size(), []()->int { return Random::RandomIntRange(100, (int)4e9); }); + + // Verify + std::stringstream ss; + ss << "["; + for (int i : rands) + ss << i << ", "; + ss << "]"; + + const double stddev = Testutil::Stddev(rands); + INFO(ss.str()); + REQUIRE(stddev >= 1000000); + + return; +} +