diff --git a/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs b/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs index 43a283be..ae63a0d9 100644 --- a/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs +++ b/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs @@ -51,6 +51,9 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests protected override IEnumerable GetContractVerificationTests() { yield return CreateFactoryReturnsCorrectTypeTest("FactoryReturnsCorrectType"); + yield return CreateInitChecksForNullTest("InitChecksForNull"); + yield return CreateInitChecksForMatchingCountTest("InitChecksForMatchingCount"); + yield return CreateConstructorInitShortcutTest("ConstructorInitShortcut"); yield return CreateConsistentCapabilityBehaviorTest("ConsistentCapabilityBehavior"); yield return CreateInterpolationMatchesNodePointsTest("InterpolationMatchesNodePoints"); yield return CreateLinearBehaviorTest("LinearBehavior"); @@ -89,6 +92,56 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests }); } + private Test CreateInitChecksForNullTest(string name) + { + return new TestCase(name, () => + { + var points = new List { 1, 2, 3, 4, 5 }; + var values = new List { 10, 20, 30, 40, 50 }; + + Assert.DoesNotThrow(() => Factory(points, values)); + + Assert.Throws(typeof(ArgumentNullException), () => Factory(points, null)); + Assert.Throws(typeof(ArgumentNullException), () => Factory(null, values)); + Assert.Throws(typeof(ArgumentNullException), () => Factory(null, null)); + }); + } + + private Test CreateInitChecksForMatchingCountTest(string name) + { + return new TestCase(name, () => + { + var points = new List { 1, 2, 3, 4, 5 }; + var valuesOk = new List { 10, 20, 30, 40, 50 }; + var valuesFail1 = new List { 10, 20, 30, 40 }; + var valuesFail2 = new List { 10, 20, 30, 40, 50, 60 }; + + Assert.DoesNotThrow(() => Factory(points, valuesOk)); + + Assert.Throws(typeof(ArgumentException), () => Factory(points, valuesFail1)); + Assert.Throws(typeof(ArgumentException), () => Factory(points, valuesFail2)); + }); + } + + private Test CreateConstructorInitShortcutTest(string name) + { + return new TestCase(name, () => + { + var points = new List { 1, 2, 3, 4, 5 }; + var values = new List { 10, 20, 30, 40, 50 }; + + var ctor = typeof(TInterpolation).GetConstructor( + new[] { typeof (IList), typeof (IList) } + ); + + var interpolation = (IInterpolation)ctor.Invoke( + new[] { points, values } + ); + + Assert.AreApproximatelyEqual(20, interpolation.Interpolate(2), 1e-12); + }); + } + private Test CreateConsistentCapabilityBehaviorTest(string name) { return new TestCase(name, () => diff --git a/src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs b/src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs index 84aa973b..777ad64b 100644 --- a/src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs +++ b/src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs @@ -96,6 +96,11 @@ namespace MathNet.Numerics.Interpolation.Algorithms IList samplePoints, IList sampleValues) { + if (null == samplePoints) + { + throw new ArgumentNullException("samplePoints"); + } + Initialize(samplePoints, sampleValues, Math.Min(3, samplePoints.Count - 1)); }