Eule/Test/Random__RandomRange.cpp

80 lines
2.1 KiB
C++
Raw Normal View History

2021-11-15 11:32:27 +01:00
#include "CppUnitTest.h"
#include "../_TestingUtilities/Testutil.h"
#include "../Eule/Random.h"
2021-11-15 11:32:27 +01:00
#include <array>
#include <sstream>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Eule;
namespace _Random
2021-11-15 11:32:27 +01:00
{
TEST_CLASS(_RandomRange)
{
public:
// Checks that a random double is never outside the specification, two positive values
TEST_METHOD(Random_Doublerange_Never_Outside_Specification__pos__pos)
{
// Test 1000 random integers
for (std::size_t i = 0; i < 1000; i++)
{
double rnd = Random::RandomRange(49.0, 99.0);
2021-11-15 11:32:27 +01:00
Assert::IsTrue(rnd >= 49.0, L"rnd too small");
Assert::IsTrue(rnd <= 99.0, L"rnd too big");
}
return;
}
// Checks that a random double is never outside the specification, negative minimum
TEST_METHOD(Random_Doublerange_Never_Outside_Specification__neg__pos)
{
// Test 1000 random integers
for (std::size_t i = 0; i < 1000; i++)
{
double rnd = Random::RandomRange(-39.0, 99.0);
2021-11-15 11:32:27 +01:00
Assert::IsTrue(rnd >= -39.0, L"rnd too small");
Assert::IsTrue(rnd <= 99.0, L"rnd too big");
}
return;
}
// Checks that a random double is never outside the specification, two negative values
TEST_METHOD(Random_Doublerange_Never_Outside_Specification__neg__neg)
{
// Test 1000 random integers
for (std::size_t i = 0; i < 1000; i++)
{
double rnd = Random::RandomRange(-39.0, -10.0);
2021-11-15 11:32:27 +01:00
Assert::IsTrue(rnd >= -39.0, L"rnd too small");
Assert::IsTrue(rnd <= -10.0, L"rnd too big");
}
return;
}
// Checks that the produced double-precision floating point distribution shows a big standard deviation
TEST_METHOD(Double_Big_Standard_Deviation)
{
// Setup
std::vector<double> rands;
rands.resize(100);
// Exercise
// Create 1000 random values
std::generate_n(rands.data(), rands.size(), []()->double { return Random::RandomRange(100, 4e9); });
2021-11-15 11:32:27 +01:00
// Verify
const double stddev = Testutil::Stddev(rands);
Assert::IsTrue(stddev >= 1000000, (std::wstringstream() << stddev).str().c_str());
return;
}
};
}