using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnitTestSharp; using Azimuth.Polynomials; namespace Azimuth.UnitTests.Polynomials { class RootListTests : TestFixture { public class Ctors : TestFixture { public void FromList() { Scalar?[] vals = { 20, 10, null, null, 10, 50, -2, -10 }; var rootList = new RootList(vals); CheckEqual(6, rootList.Roots.Count); for (int i = 0, j = 0; i < vals.Length; ++i) { if (!vals[i].HasValue) continue; CheckEqual(vals[i], rootList.Roots[j++]); } } public void EmptyCtor() { var rootList = new RootList(); CheckEqual(0, rootList.Roots.Count); } public void OneRoot_NotNull() { var rootList = new RootList(10); CheckEqual(1, rootList.Roots.Count); CheckEqual(10, rootList.Roots[0]); } public void OneRoot_Null() { var rootList = new RootList((Scalar?)null); CheckEqual(0, rootList.Roots.Count); } public void TwoRoot_NotNull() { var rootList = new RootList(20, 10); CheckEqual(2, rootList.Roots.Count); CheckEqual(20, rootList.Roots[0]); CheckEqual(10, rootList.Roots[1]); } public void TwoRoot_OneNull() { var rootList = new RootList((Scalar?)null, 10); CheckEqual(1, rootList.Roots.Count); CheckEqual(10, rootList.Roots[0]); } public void TwoRoot_TwoNull() { var rootList = new RootList((Scalar?)null, (Scalar?)null); CheckEqual(0, rootList.Roots.Count); } public void ThreeRoot_ThreeNull() { var rootList = new RootList((Scalar?)null, (Scalar?)null, (Scalar?)null); CheckEqual(0, rootList.Roots.Count); } public void ThreeRoot_TwoNull() { var rootList = new RootList((Scalar?)null, 7, (Scalar?)null); CheckEqual(1, rootList.Roots.Count); CheckEqual(7, rootList.Roots[0]); } public void ThreeRoot_OneNull() { var rootList = new RootList((Scalar?)null, 7, -3); CheckEqual(2, rootList.Roots.Count); CheckEqual(7, rootList.Roots[0]); CheckEqual(-3, rootList.Roots[1]); } public void ThreeRoot_NoneNull() { var rootList = new RootList(2, 7, -3); CheckEqual(3, rootList.Roots.Count); CheckEqual(2, rootList.Roots[0]); CheckEqual(7, rootList.Roots[1]); CheckEqual(-3, rootList.Roots[2]); } public void FourRoot_NoneNull() { var rootList = new RootList(2, 7, -3, 11); CheckEqual(4, rootList.Roots.Count); CheckEqual(2, rootList.Roots[0]); CheckEqual(7, rootList.Roots[1]); CheckEqual(-3, rootList.Roots[2]); CheckEqual(11, rootList.Roots[3]); } public void FourRoot_OneNull() { var rootList = new RootList(2, (Scalar?)null, -3, 11); CheckEqual(3, rootList.Roots.Count); CheckEqual(2, rootList.Roots[0]); CheckEqual(-3, rootList.Roots[1]); CheckEqual(11, rootList.Roots[2]); } public void FourRoot_TwoNull() { var rootList = new RootList(2, (Scalar?)null, -3, (Scalar?)null); CheckEqual(2, rootList.Roots.Count); CheckEqual(2, rootList.Roots[0]); CheckEqual(-3, rootList.Roots[1]); } public void FourRoot_ThreeNull() { var rootList = new RootList((Scalar?)null, (Scalar?)null, -3, (Scalar?)null); CheckEqual(1, rootList.Roots.Count); CheckEqual(-3, rootList.Roots[0]); } public void FourRoot_FourNull() { var rootList = new RootList((Scalar?)null, (Scalar?)null, (Scalar?)null, (Scalar?)null); CheckEqual(0, rootList.Roots.Count); } } public class Equality : TestFixture { public void Basic() { RootList r1 = new Scalar?[] { 10, 20, 10, null, -2, 3, null }; RootList r2 = new Scalar?[] { 10, 20, 10, null, -2, 3, null }; Check(r1.Equals(r2)); Check(r2.Equals(r1)); } public void DifferentSizes() { RootList r1 = new Scalar?[] { 10, 20, 10, null, -2, 3, null }; RootList r2 = new Scalar?[] { 10, 20, 10 }; CheckFalse(r1.Equals(r2)); CheckFalse(r2.Equals(r1)); } public void DifferentMultiplicity() { RootList r1 = new Scalar?[] { 10, 20, 10, null, -2, 3, null }; RootList r2 = new Scalar?[] { 10, 20, 20, null, -2, 3, null }; CheckFalse(r1.Equals(r2)); CheckFalse(r2.Equals(r1)); } public void Empty() { RootList r1 = new Scalar?[] { }; RootList r2 = new Scalar?[] { }; Check(r1.Equals(r2)); Check(r2.Equals(r1)); } public void DifferentInitialSizesSameRealRoots() { RootList r1 = new Scalar?[] { 10, 20, 10, null, -2, 3, null }; RootList r2 = new Scalar?[] { 10, 20, 10, -2, 3 }; Check(r1.Equals(r2)); Check(r2.Equals(r1)); } } public class LeastPositive : TestFixture { public void Basic() { Scalar?[] vals = { 20, 10, null, null, 10, 50, -2, -10 }; CheckEqual(10, new RootList(vals).LeastPositiveRoot); } public void Empty() { Scalar?[] vals = { null, null, null }; CheckNull(new RootList(vals).LeastPositiveRoot); } public void SingleNegative() { Scalar?[] vals = { -2 }; CheckNull(new RootList(vals).LeastPositiveRoot); } public void SinglePositive() { Scalar?[] vals = { 2 }; CheckEqual(2, new RootList(vals).LeastPositiveRoot); } public void SingleZero() { Scalar?[] vals = { 0 }; CheckNull(new RootList(vals).LeastPositiveRoot); } public void SlightlyNegativeButApproxZero() { Scalar?[] vals = { 1e-16, -1e-16 }; CheckEqual(1e-16, new RootList(vals).LeastPositiveRoot.Value.Value); } } public class LeastPositiveOrZeroRootTests : TestFixture { public void Basic() { Scalar?[] vals = { 20, 10, null, null, 10, 50, -2, -10 }; CheckEqual(10, new RootList(vals).LeastPositiveOrZeroRoot); } public void Empty() { Scalar?[] vals = { null, null, null }; CheckNull(new RootList(vals).LeastPositiveOrZeroRoot); } public void SingleNegative() { Scalar?[] vals = { -2 }; CheckNull(new RootList(vals).LeastPositiveOrZeroRoot); } public void SinglePositive() { Scalar?[] vals = { 2 }; CheckEqual(2, new RootList(vals).LeastPositiveOrZeroRoot); } public void SingleZero() { Scalar?[] vals = { 0 }; CheckEqual(0, new RootList(vals).LeastPositiveOrZeroRoot); } public void SlightlyNegativeButApproxZero() { Scalar?[] vals = { 1e-16, -1e-16 }; CheckEqual(-1e-16, new RootList(vals).LeastPositiveOrZeroRoot.Value.Value); } } public class ToStringTests : TestFixture { public void Basic() { RootList roots = new Scalar?[] { 10, 20, null, null, 10, 50, -2 }; CheckEqual("(-2, 10, 10, 20, 50)", roots.ToString()); } public void Empty() { RootList roots = new Scalar?[] { null, null, null }; CheckEqual("({empty})", roots.ToString()); } } } }