From 72bdf8cff05d79370098397a6b80a33e1dc3e420 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 26 Sep 2020 21:53:09 +0200 Subject: [PATCH] Initial --- .../Double/Distributions/Exponential.cs | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 FastRngTests/Double/Distributions/Exponential.cs diff --git a/FastRngTests/Double/Distributions/Exponential.cs b/FastRngTests/Double/Distributions/Exponential.cs new file mode 100644 index 0000000..6f41ee8 --- /dev/null +++ b/FastRngTests/Double/Distributions/Exponential.cs @@ -0,0 +1,89 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Threading.Tasks; +using FastRng.Double; +using NUnit.Framework; + +namespace FastRngTests.Double.Distributions +{ + [ExcludeFromCodeCoverage] + public class Exponential + { + [Test] + [Category(TestCategories.COVER)] + [Category(TestCategories.NORMAL)] + public async Task TestExponentialDistribution01() + { + const double MEAN = 1.5; + const double VARIANCE = MEAN * MEAN; + + var dist = new FastRng.Double.Distributions.Exponential{ Mean = MEAN }; + var stats = new RunningStatistics(); + var rng = new MultiThreadedRng(); + + for (var n = 0; n < 100_000; n++) + stats.Push(await rng.NextNumber(dist)); + + rng.StopProducer(); + TestContext.WriteLine($"mean={MEAN} vs. {stats.Mean}"); + TestContext.WriteLine($"variance={VARIANCE} vs {stats.Variance}"); + + Assert.That(stats.Mean, Is.EqualTo(MEAN).Within(0.4), "Mean is out of range"); + Assert.That(stats.Variance, Is.EqualTo(VARIANCE).Within(0.4), "Variance is out of range"); + } + + [Test] + [Category(TestCategories.COVER)] + [Category(TestCategories.NORMAL)] + public async Task TestExponentialGeneratorWithRange01() + { + var rng = new MultiThreadedRng(); + var samples = new double[1_000]; + for (var n = 0; n < samples.Length; n++) + samples[n] = await rng.NextNumber(-1.0, 1.0, new FastRng.Double.Distributions.Exponential()); + + rng.StopProducer(); + Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); + Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max out of range"); + } + + [Test] + [Category(TestCategories.COVER)] + [Category(TestCategories.NORMAL)] + public async Task TestExponentialGeneratorWithRange02() + { + var rng = new MultiThreadedRng(); + var samples = new double[1_000]; + for (var n = 0; n < samples.Length; n++) + samples[n] = await rng.NextNumber(0.0, 1.0, new FastRng.Double.Distributions.Exponential()); + + rng.StopProducer(); + Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); + Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); + } + + [Test] + [Category(TestCategories.COVER)] + [Category(TestCategories.NORMAL)] + public void ParameterTest01() + { + var dist = new FastRng.Double.Distributions.Exponential(); + + Assert.Throws(() => dist.Mean = 0); + Assert.Throws(() => dist.Mean = -78); + Assert.DoesNotThrow(() => dist.Mean = 0.0001); + Assert.DoesNotThrow(() => dist.Mean = 4); + } + + [Test] + [Category(TestCategories.COVER)] + [Category(TestCategories.NORMAL)] + public async Task NoRandomNumberGenerator01() + { + var dist = new FastRng.Double.Distributions.Exponential(); + Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); + Assert.That(await dist.GetDistributedValue(), Is.NaN); + } + } +} \ No newline at end of file