From 7b0c6c62bcc45eeadd81ff9baecee293223daa08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deronne?= Date: Wed, 31 Jan 2024 12:39:43 +0100 Subject: [PATCH] core: Add test for largest extreme value random distribution --- .../test/random-variable-stream-test-suite.cc | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/core/test/random-variable-stream-test-suite.cc b/src/core/test/random-variable-stream-test-suite.cc index a65060ae2..1ce937472 100644 --- a/src/core/test/random-variable-stream-test-suite.cc +++ b/src/core/test/random-variable-stream-test-suite.cc @@ -3042,6 +3042,62 @@ LaplacianTestCase::DoRun() } } +/** + * \ingroup rng-tests + * Test case for largest extreme value distribution random variable stream generator + */ +class LargestExtremeValueTestCase : public TestCaseBase +{ + public: + LargestExtremeValueTestCase(); + + private: + void DoRun() override; + + /** + * Tolerance for testing rng values against expectation, + * as a fraction of mean value. + */ + static constexpr double TOLERANCE{1e-2}; +}; + +LargestExtremeValueTestCase::LargestExtremeValueTestCase() + : TestCaseBase("Largest Extreme Value Random Variable Stream Generator") +{ +} + +void +LargestExtremeValueTestCase::DoRun() +{ + NS_LOG_FUNCTION(this); + SetTestSuiteSeed(); + + double mu = 2.0; + double scale = 1.0; + + // Create RNG with the specified range. + auto x = CreateObject(); + x->SetAttribute("Location", DoubleValue(mu)); + x->SetAttribute("Scale", DoubleValue(scale)); + + // Calculate the mean of these values. + auto valueMean = Average(x); + + // Calculate the variance of these values. + auto valueVariance = Variance(x, valueMean); + + // Test that values have approximately the right mean value. + const auto expectedMean = LargestExtremeValueRandomVariable::GetMean(mu, scale); + NS_TEST_ASSERT_MSG_EQ_TOL(valueMean, expectedMean, TOLERANCE, "Wrong mean value."); + + // Test that values have approximately the right variance value. + const auto expectedVariance = LargestExtremeValueRandomVariable::GetVariance(scale); + NS_TEST_ASSERT_MSG_EQ_TOL(valueVariance, + expectedVariance, + TOLERANCE * expectedVariance, + "Wrong variance value."); +} + /** * \ingroup rng-tests * RandomVariableStream test suite, covering all random number variable @@ -3100,6 +3156,7 @@ RandomVariableSuite::RandomVariableSuite() AddTestCase(new BinomialAntitheticTestCase); AddTestCase(new ShuffleElementsTest); AddTestCase(new LaplacianTestCase); + AddTestCase(new LargestExtremeValueTestCase); } static RandomVariableSuite randomVariableSuite; //!< Static variable for test initialization