From e6ac48bf5f5a66ec97b7771715e81157c62b23fb Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 17 Jun 2012 19:30:23 +0200 Subject: [PATCH] Tests: make integral transform tests deterministic (by seed) --- .../IntegralTransformsTests/FourierTest.cs | 10 ++++++++-- .../IntegralTransformsTests/HartleyTest.cs | 10 ++++++++-- .../InverseTransformTest.cs | 14 ++++++++++---- .../MatchingNaiveTransformTest.cs | 14 ++++++++++---- .../IntegralTransformsTests/ParsevalTheoremTest.cs | 12 +++++++++--- 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/UnitTests/IntegralTransformsTests/FourierTest.cs b/src/UnitTests/IntegralTransformsTests/FourierTest.cs index e628f971..afbc6d8a 100644 --- a/src/UnitTests/IntegralTransformsTests/FourierTest.cs +++ b/src/UnitTests/IntegralTransformsTests/FourierTest.cs @@ -43,7 +43,13 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests /// /// Continuous uniform distribution. /// - private readonly IContinuousDistribution _uniform = new ContinuousUniform(-1, 1); + private IContinuousDistribution GetUniform(int seed) + { + return new ContinuousUniform(-1, 1) + { + RandomSource = new Random(seed) + }; + } /// /// Naive transforms real sine correctly. @@ -87,7 +93,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests [Test] public void Radix2ThrowsWhenNotPowerOfTwo() { - var samples = SignalGenerator.Random((u, v) => new Complex(u, v), _uniform, 0x7F); + var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x7F); var dft = new DiscreteFourierTransform(); diff --git a/src/UnitTests/IntegralTransformsTests/HartleyTest.cs b/src/UnitTests/IntegralTransformsTests/HartleyTest.cs index 7a1b571f..b21dfe45 100644 --- a/src/UnitTests/IntegralTransformsTests/HartleyTest.cs +++ b/src/UnitTests/IntegralTransformsTests/HartleyTest.cs @@ -43,7 +43,13 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests /// /// Continuous uniform distribution. /// - private readonly IContinuousDistribution _uniform = new ContinuousUniform(-1, 1); + private IContinuousDistribution GetUniform(int seed) + { + return new ContinuousUniform(-1, 1) + { + RandomSource = new Random(seed) + }; + } /// /// Verify if matches DFT. @@ -80,7 +86,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests public void NaiveMatchesDft(HartleyOptions hartleyOptions, FourierOptions fourierOptions) { var dht = new DiscreteHartleyTransform(); - var samples = SignalGenerator.Random(x => x, _uniform, 0x80); + var samples = SignalGenerator.Random(x => x, GetUniform(1), 0x80); VerifyMatchesDft( samples, diff --git a/src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs b/src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs index 54970389..0f9f23dc 100644 --- a/src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs +++ b/src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs @@ -43,7 +43,13 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests /// /// Continuous uniform distribution. /// - private readonly IContinuousDistribution _uniform = new ContinuousUniform(-1, 1); + private IContinuousDistribution GetUniform(int seed) + { + return new ContinuousUniform(-1, 1) + { + RandomSource = new Random(seed) + }; + } /// /// Verify if is reversible complex. @@ -58,7 +64,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests Func forward, Func inverse) { - var samples = SignalGenerator.Random((u, v) => new Complex(u, v), _uniform, count); + var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), count); var work = new Complex[samples.Length]; samples.CopyTo(work, 0); @@ -84,7 +90,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests Func forward, Func inverse) { - var samples = SignalGenerator.Random(x => x, _uniform, count); + var samples = SignalGenerator.Random(x => x, GetUniform(1), count); var work = new double[samples.Length]; samples.CopyTo(work, 0); @@ -187,7 +193,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests [Test] public void FourierDefaultTransformIsReversible() { - var samples = SignalGenerator.Random((u, v) => new Complex(u, v), _uniform, 0x7FFF); + var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x7FFF); var work = new Complex[samples.Length]; samples.CopyTo(work, 0); diff --git a/src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs b/src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs index bb943024..6f3aac6c 100644 --- a/src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs +++ b/src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs @@ -43,7 +43,13 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests /// /// Continuous uniform distribution. /// - private readonly IContinuousDistribution _uniform = new ContinuousUniform(-1, 1); + private IContinuousDistribution GetUniform(int seed) + { + return new ContinuousUniform(-1, 1) + { + RandomSource = new Random(seed) + }; + } /// /// Verify matches naive complex. @@ -102,7 +108,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests public void FourierRadix2MatchesNaiveOnRandom(FourierOptions options) { var dft = new DiscreteFourierTransform(); - var samples = SignalGenerator.Random((u, v) => new Complex(u, v), _uniform, 0x80); + var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x80); VerifyMatchesNaiveComplex( samples, @@ -152,7 +158,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests public void FourierBluesteinMatchesNaiveOnRandomPowerOfTwo(FourierOptions options) { var dft = new DiscreteFourierTransform(); - var samples = SignalGenerator.Random((u, v) => new Complex(u, v), _uniform, 0x80); + var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x80); VerifyMatchesNaiveComplex( samples, @@ -177,7 +183,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests public void FourierBluesteinMatchesNaiveOnRandomNonPowerOfTwo(FourierOptions options) { var dft = new DiscreteFourierTransform(); - var samples = SignalGenerator.Random((u, v) => new Complex(u, v), _uniform, 0x7F); + var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x7F); VerifyMatchesNaiveComplex( samples, diff --git a/src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs b/src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs index 6f6ca0ac..719eff93 100644 --- a/src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs +++ b/src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs @@ -44,7 +44,13 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests /// /// Continuous uniform distribution. /// - private readonly IContinuousDistribution _uniform = new ContinuousUniform(-1, 1); + private IContinuousDistribution GetUniform(int seed) + { + return new ContinuousUniform(-1, 1) + { + RandomSource = new System.Random(seed) + }; + } /// /// Fourier default transform satisfies parsevals theorem. @@ -54,7 +60,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests [TestCase(0x7FF)] public void FourierDefaultTransformSatisfiesParsevalsTheorem(int count) { - var samples = SignalGenerator.Random((u, v) => new Complex(u, v), _uniform, count); + var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), count); var timeSpaceEnergy = (from s in samples select s.MagnitudeSquared()).Mean(); @@ -77,7 +83,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests [TestCase(0x1F)] public void HartleyDefaultNaiveSatisfiesParsevalsTheorem(int count) { - var samples = SignalGenerator.Random(x => x, _uniform, count); + var samples = SignalGenerator.Random(x => x, GetUniform(1), count); var timeSpaceEnergy = (from s in samples select s * s).Mean();