using System; using Azimuth; using UnitTestSharp; namespace Konis.UnitTests { public class SlabTests : TestFixture { private static readonly Scalar PositiveInfinity = Scalar.PositiveInfinity; private static readonly Scalar NegativeInfinity = Scalar.NegativeInfinity; public class CtorTests : TestFixture { public void KeepsTheNormalAndTheRange() { var slab = new Slab(new Vector3(0, 0.6, 0.8), 1, 2); CheckEqual((Scalar)0, slab.Normal.X); CheckEqual((Scalar)0.6, slab.Normal.Y); CheckEqual((Scalar)0.8, slab.Normal.Z); CheckEqual((Scalar)1, slab.Min); CheckEqual((Scalar)2, slab.Max); } public void InfiniteEnds_AreKept() { var slab = new Slab(new Vector3(1, 0, 0), NegativeInfinity, PositiveInfinity); CheckEqual(NegativeInfinity, slab.Min); CheckEqual(PositiveInfinity, slab.Max); } public void ARangeOfOnePoint_IsAllowed() { var slab = new Slab(new Vector3(1, 0, 0), 3, 3); CheckEqual((Scalar)3, slab.Min); CheckEqual((Scalar)3, slab.Max); } public void ANormalWithOnlyRoundingError_IsAllowed() { double side = 1.0 / Math.Sqrt(3.0); var slab = new Slab(new Vector3(side, side, side), 0, 1); CheckEqual((Scalar)side, slab.Normal.X); } public void ANormalOffLengthOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new Slab(new Vector3(1.001, 0, 0), 0, 1); } public void AZeroNormal_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new Slab(new Vector3(0, 0, 0), 0, 1); } public void ANormalWithANaN_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new Slab(new Vector3(double.NaN, 0, 1), 0, 1); } public void ANormalWithAnInfinity_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new Slab(new Vector3(double.PositiveInfinity, 0, 0), 0, 1); } public void AMinimumThatIsNotANumber_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new Slab(new Vector3(1, 0, 0), double.NaN, 1); } public void AMaximumThatIsNotANumber_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new Slab(new Vector3(1, 0, 0), 0, double.NaN); } public void AMinimumAboveTheMaximum_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new Slab(new Vector3(1, 0, 0), 2, 1); } } public class AboveTests : TestFixture { public void IsOpenAtTheTopOnTheAxis() { Slab slab = Slab.Above(Axis.Y, 5); CheckEqual((Scalar)0, slab.Normal.X); CheckEqual((Scalar)1, slab.Normal.Y); CheckEqual((Scalar)0, slab.Normal.Z); CheckEqual((Scalar)5, slab.Min); CheckEqual(PositiveInfinity, slab.Max); } public void AnAxisThatIsNotOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Slab.Above((Axis)7, 0); } } public class BelowTests : TestFixture { public void IsOpenAtTheBottomOnTheAxis() { Slab slab = Slab.Below(Axis.X, 5); CheckEqual((Scalar)1, slab.Normal.X); CheckEqual(NegativeInfinity, slab.Min); CheckEqual((Scalar)5, slab.Max); } public void AnAxisThatIsNotOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Slab.Below((Axis)7, 0); } } public class BetweenTests : TestFixture { public void CoversTheRangeOnTheAxis() { Slab slab = Slab.Between(Axis.Z, -1, 4); CheckEqual((Scalar)0, slab.Normal.X); CheckEqual((Scalar)0, slab.Normal.Y); CheckEqual((Scalar)1, slab.Normal.Z); CheckEqual((Scalar)(-1), slab.Min); CheckEqual((Scalar)4, slab.Max); } public void AMinimumAboveTheMaximum_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Slab.Between(Axis.Z, 4, -1); } public void AnAxisThatIsNotOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Slab.Between((Axis)7, 0, 1); } } public class BetweenDirectionTests : TestFixture { public void NormalizesTheDirection() { Slab slab = Slab.Between(new Vector3(0, 3, 4), 1, 2); CheckEqual((Scalar)0, slab.Normal.X); CheckEqual((Scalar)0.6, slab.Normal.Y); CheckEqual((Scalar)0.8, slab.Normal.Z); } public void KeepsTheEndsAsDistancesWhateverTheDirectionsLength() { Slab slab = Slab.Between(new Vector3(0, 0, 10), -1, 4); CheckEqual((Scalar)(-1), slab.Min); CheckEqual((Scalar)4, slab.Max); } public void InfiniteEnds_AreKept() { Slab slab = Slab.Between(new Vector3(2, 0, 0), NegativeInfinity, PositiveInfinity); CheckEqual(NegativeInfinity, slab.Min); CheckEqual(PositiveInfinity, slab.Max); } public void ADirectionAlreadyOfLengthOne_IsKept() { Slab slab = Slab.Between(new Vector3(0, 1, 0), 0, 1); CheckEqual((Scalar)1, slab.Normal.Y); } public void AZeroDirection_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Slab.Between(new Vector3(0, 0, 0), 0, 1); } public void AnInfiniteDirection_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Slab.Between(new Vector3(PositiveInfinity, 0, 0), 0, 1); } public void ADirectionWithNaN_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Slab.Between(new Vector3(Scalar.NaN, 0, 0), 0, 1); } public void AMinimumAboveTheMaximum_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Slab.Between(new Vector3(1, 0, 0), 4, -1); } } public class AlongTests : TestFixture { public void IsThePointDottedWithTheNormal() { var slab = new Slab(new Vector3(0, 0.6, 0.8), 0, 1); CheckEqual((Scalar)10, slab.Along(new Vector3(100, 10, 5))); } } public class RelativeToTests : TestFixture { public void ShiftsTheRangeByTheOriginsDistanceAlongTheNormal() { Slab slab = Slab.Between(Axis.Y, 2, 6); Slab moved = slab.RelativeTo(new Vector3(100, 1, 100)); CheckEqual((Scalar)1, moved.Min); CheckEqual((Scalar)5, moved.Max); } public void KeepsTheNormal() { var slab = new Slab(new Vector3(0, 0.6, 0.8), 0, 1); Slab moved = slab.RelativeTo(new Vector3(1, 2, 3)); CheckEqual((Scalar)0.6, moved.Normal.Y); CheckEqual((Scalar)0.8, moved.Normal.Z); } public void LeavesAnOpenEndOpen() { Slab moved = Slab.Above(Axis.Z, 3).RelativeTo(new Vector3(0, 0, 1)); CheckEqual((Scalar)2, moved.Min); CheckEqual(PositiveInfinity, moved.Max); } } } }