From a9f06ccbd0cc70e53cc72741608e4c2ef5cd7a9c Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Thu, 2 Jul 2009 05:55:14 +0800 Subject: [PATCH] interpolation test: contract based unit tests Signed-off-by: Christoph Ruegg --- .../InterpolationContract.cs | 191 ++++++++++++++++++ .../InterpolationTests/InterpolationTest.cs | 54 +++++ .../Managed.UnitTests.csproj | 2 + src/Native.UnitTests/Native.UnitTests.csproj | 8 +- 4 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs create mode 100644 src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs diff --git a/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs b/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs new file mode 100644 index 00000000..2cf1a96b --- /dev/null +++ b/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs @@ -0,0 +1,191 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://mathnet.opensourcedotnet.info +// +// Copyright (c) 2009 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics.UnitTests.InterpolationTests +{ + using System; + using System.Collections.Generic; + using Gallio.Framework.Assertions; + using MbUnit.Framework; + using MbUnit.Framework.ContractVerifiers; + using Interpolation; + + internal class InterpolationContract : AbstractContract + where TInterpolation : IInterpolation + { + public Func, IList, IInterpolation> Factory { get; set; } + public int[] Order { get; set; } + + public InterpolationContract() + { + Order = new[] { 4 }; + } + + protected override IEnumerable GetContractVerificationTests() + { + yield return CreateFactoryReturnsCorrectTypeTest("FactoryReturnsCorrectType"); + yield return CreateConsistentCapabilityBehaviorTest("ConsistentCapabilityBehavior"); + yield return CreateInterpolationMatchesNodePointsTest("InterpolationMatchesNodePoints"); + yield return CreateCanDealWithLinearSamplesTest("CanDealWithLinearSamples"); + } + + private Test CreateFactoryReturnsCorrectTypeTest(string name) + { + return new TestCase(name, () => + { + var interpolation = Factory(new List { 1, 2, 3 }, new List { 10, 20, 30 }); + + AssertionHelper.Verify(() => + { + // verify returned interpolation has the expected type + if (interpolation.GetType() == typeof(TInterpolation)) + { + return null; + } + + return new AssertionFailureBuilder( + "Expected the factory to return the correct type.") + .AddRawLabeledValue("Interpolation Type", typeof(TInterpolation)) + .SetStackTrace(Context.GetStackTraceData()) + .ToAssertionFailure(); + }); + }); + } + + private Test CreateConsistentCapabilityBehaviorTest(string name) + { + return new TestCase(name, () => + { + var interpolation = Factory(new List { 1, 2, 3 }, new List { 10, 20, 30 }); + + // verify consistent differentiation capability + if (interpolation.SupportsDifferentiation) + { + double a, b; + Assert.DoesNotThrow(() => interpolation.Differentiate(1.2)); + Assert.DoesNotThrow(() => interpolation.Differentiate(1.2, out a, out b)); + } + else + { + double a, b; + + Assert.Throws( + typeof(NotSupportedException), + () => interpolation.Differentiate(1.2)); + + Assert.Throws( + typeof(NotSupportedException), + () => interpolation.Differentiate(1.2, out a, out b)); + } + + // verify consistent integration capability + if (interpolation.SupportsIntegration) + { + Assert.DoesNotThrow(() => interpolation.Integrate(1.2)); + } + else + { + Assert.Throws( + typeof(NotSupportedException), + () => interpolation.Integrate(1.2)); + } + }); + } + + private Test CreateInterpolationMatchesNodePointsTest(string name) + { + return new TestCase(name, () => + { + var points = new List { 1, 2, 2.3, 3, 8 }; + var values = new List { 50, 20, 30, 10, -20 }; + var interpolation = Factory(points, values); + + for (int i = 0; i < points.Count; i++) + { + Assert.AreApproximatelyEqual( + values[i], + interpolation.Interpolate(points[i]), + 1e-12); + } + }); + } + + private Test CreateCanDealWithLinearSamplesTest(string name) + { + return new TestCase(name, () => + { + const double yOffset = 2.0; + const double xOffset = 4.0; + Random random = new Random(); + + for (int k = 0; k < Order.Length; k++) + { + int order = Order[k]; + + // build linear samples + double[] points = new double[order]; + double[] values = new double[order]; + for (int i = 0; i < points.Length; i++) + { + points[i] = xOffset + i; + values[i] = yOffset + i; + } + + // build linear test vectors randomly between the sample points + double[] testPoints = new double[order + 1]; + double[] testValues = new double[order + 1]; + if (order == 1) + { + testPoints[0] = xOffset - random.NextDouble(); + testPoints[1] = xOffset + random.NextDouble(); + testValues[0] = testValues[1] = yOffset; + } + else + { + for (int i = 0; i < testPoints.Length; i++) + { + double z = (i - 1) + random.NextDouble(); + testPoints[i] = xOffset + z; + testValues[i] = yOffset + z; + } + } + + var interpolation = Factory(points, values); + + for (int i = 0; i < testPoints.Length; i++) + { + Assert.AreApproximatelyEqual( + testValues[i], + interpolation.Interpolate(testPoints[i]), + 1e-12); + } + } + }); + } + } +} diff --git a/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs b/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs new file mode 100644 index 00000000..d5cedabc --- /dev/null +++ b/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs @@ -0,0 +1,54 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://mathnet.opensourcedotnet.info +// +// Copyright (c) 2009 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics.UnitTests.InterpolationTests +{ + using System; + using MbUnit.Framework; + using MbUnit.Framework.ContractVerifiers; + using Interpolation; + using Interpolation.Algorithms; + + [TestFixture] + public class InterpolationTest + { + [VerifyContract] + public readonly IContract LinearSplineTests = new InterpolationContract() + { + Factory = Interpolation.CreateLinearSpline, + Order = new[] { 2, 3, 6 } + }; + + [VerifyContract] + public readonly IContract RationalPoleFreeTests = new InterpolationContract() + { + Factory = Interpolation.CreateRationalPoleFree, + Order = new[] { 1, 2, 6 } + }; + } +} diff --git a/src/Managed.UnitTests/Managed.UnitTests.csproj b/src/Managed.UnitTests/Managed.UnitTests.csproj index a626b3c6..369013b0 100644 --- a/src/Managed.UnitTests/Managed.UnitTests.csproj +++ b/src/Managed.UnitTests/Managed.UnitTests.csproj @@ -62,6 +62,8 @@ + + diff --git a/src/Native.UnitTests/Native.UnitTests.csproj b/src/Native.UnitTests/Native.UnitTests.csproj index ca324d9a..9557f2ca 100644 --- a/src/Native.UnitTests/Native.UnitTests.csproj +++ b/src/Native.UnitTests/Native.UnitTests.csproj @@ -72,12 +72,18 @@ DistributionTests\Continuous\NormalTests.cs + + InterpolationTests\InterpolationContract.cs + + + InterpolationTests\InterpolationTest.cs + PrecisionTest.cs SpecialFunctionsTest\ErfTests.cs - +