using System; using Chernobyl.Mathematics.Vectors; using Chernobyl.Measures.Speed; using Chernobyl.Measures.Time; using NUnit.Framework; namespace Chernobyl.Mathematics.Measures { public class VelocityTests { [Test, Description("A test that ensures the " + "Velocity(Vector3d, Metres) constructor sets the proper " + "value.")] public void ConstructorVector3dMetresPerSecondsTest() { Vector3d direction = new Vector3d(1, 2, 3); direction.Normalize(); Metres metresPerSecond = new Metres(2); Velocity velocity = new Velocity(direction, metresPerSecond); Assert.AreEqual(direction * metresPerSecond.Value, velocity.Value, "The value returned by Velocity.Value is not the expected value."); } [Test, Description("A test that ensures the " + "Velocity(Vector3d) constructor sets the proper value.")] public void ConstructorVector3dTest() { Vector3d direction = new Vector3d(1, 2, 3); direction.Normalize(); Velocity velocity = new Velocity(direction); Assert.AreEqual(direction, velocity.Value, "The value returned by Velocity.Value is not the expected value."); } [Test, Description("A test to ensure the implicit Velocity to Vector3d " + "conversion works as expected.")] public void ImplicitVelocityToVector3dConversionTest() { Velocity velocity = new Velocity(new Vector3d(2.89, 5.6, 3.4)); Vector3d value = velocity; Assert.AreEqual(velocity.Value, value, "The value returned by the implicit Velocity to Vector3d conversion " + "is not the expected value."); } [Test, Description("A test to ensure the implicit Vector3d to Velocity " + "conversion works as expected.")] public void ExplicitDoubleToMetresConversionTest() { Vector3d value = new Vector3d(2.89, 5.6, 3.4); Velocity metres = (Velocity)value; Assert.AreEqual(value, metres.Value, "The value returned by the explicit Vector3d to Velocity conversion " + "is not the expected value."); } [Test, Description("A test to ensure the explicit Velocity to Metres " + "conversion works as expected.")] public void ExplicitVelocityToVector3dConversionTest() { Velocity velocity = new Velocity(new Vector3d(2.89, 5.6, 3.4)); Metres value = (Metres)velocity; Assert.AreEqual(velocity.Speed, value, "The value returned by the explicit Velocity to Metres " + "conversion is not the expected value."); } [TestFixture, Description("Tests for the IEquatable and Object " + "implementations of the Velocity type.")] public class EquatableTests : EquatableTests { public EquatableTests() { _directionAndSpeed = new Vector3d(2.89, 5.6, 3.4); _directionAndSpeed.Normalize(); } protected override IEquatable CreateEquatable() { return new Velocity(_directionAndSpeed); } protected override Velocity CreateEqualObject() { return new Velocity(_directionAndSpeed); } protected override Velocity CreateNotEqualObject() { return new Velocity(new Vector3d(5.6, 3.6, 3.4)); } protected override string ExpectedToStringResult { get { return _directionAndSpeed + " at " + (Metres)_directionAndSpeed.Length; } } Vector3d _directionAndSpeed; } [TestFixture, Description("Tests for the Velocity type's mathematical " + "operator overloads.")] public class MathematicalOperatorTests : MathematicalOperatorTests { public MathematicalOperatorTests() { _x = new Vector3d(2.89, 5.6, 3.4); _y = new Vector3d(3.3, 344.4, 867.3); } [Test, Description("A test for checking the multiplication operator.")] public void ScalarMultiplicationTest() { Velocity velocity = new Velocity(new Vector3d(2.89, 5.6, 3.4)); Metres metersPerSecond = new Metres(3.4); { Velocity result = velocity * metersPerSecond; Assert.AreEqual(velocity.Value * metersPerSecond.Value, result.Value, "X * Y did not produce the expected result."); } { Velocity result = metersPerSecond * velocity; Assert.AreEqual(metersPerSecond.Value * velocity.Value, result.Value, "Y * X did not produce the expected result."); } } protected override Velocity CreateX() { return new Velocity(_x); } protected override Velocity CreateY() { return new Velocity(_y); } protected override Velocity ExpectedBinaryAdditionResult { get { return new Velocity(_x + _y); } } protected override Velocity ExpectedBinarySubtractionResult { get { return new Velocity(_x - _y); } } protected override Velocity OppositeExpectedBinarySubtractionResult { get { return new Velocity(_y - _x); } } protected override Velocity ExpectedNegationResult { get { return new Velocity(-_x); } } protected override Velocity ExpectedMultiplicationResult { // Return default since Velocity doesn't support this operation. get { return default(Velocity); } } protected override Velocity ExpectedDivisionResult { // Return default since Velocity doesn't support this operation. get { return default(Velocity); } } protected override Velocity OppositeExpectedDivisionResult { // Return default since Velocity doesn't support this operation. get { return default(Velocity); } } /// /// The form of . /// readonly Vector3d _x; /// /// The form of . /// readonly Vector3d _y; } [TestFixture, Description("Tests for the Velocity type's equality " + "operator overloads.")] public class EqualityOperatorTests : EqualityOperatorTests { protected override Velocity CreateX() { return new Velocity(new Vector3d(2.89, 5.6, 3.4)); } protected override Velocity CreateEqual() { return new Velocity(new Vector3d(2.89, 5.6, 3.4)); } protected override Velocity CreateNotEqual() { return new Velocity(new Vector3d(325.6, 25.6, 98.1)); } } } }