using MiscUtil; using NUnit.Framework; namespace System { /// /// A set of tests for determining proper operation of the following /// operators: + (binary), - (unary and binary), * , / /// /// The type that is to be tested. public abstract class MathematicalOperatorTests { [Test, Description("A test for checking the binary addition (+) operator.")] public void BinaryAdditionTest() { if(default(T).Equals(ExpectedBinaryAdditionResult) == false) { { T result = Operator.Add(CreateX(), CreateY()); Assert.AreEqual(ExpectedBinaryAdditionResult, result, "X + Y did not produce the expected result."); } { T result = Operator.Add(CreateY(), CreateX()); Assert.AreEqual(OppositeExpectedBinaryAdditionResult, result, "Y + X did not produce the expected result."); } } else Assert.Pass("Type does not implement binary addition operator."); } [Test, Description("A test for checking the binary subtraction (-) operator.")] public void BinarySubtractionTest() { if (default(T).Equals(ExpectedBinarySubtractionResult) == false) { { T result = Operator.Subtract(CreateX(), CreateY()); Assert.AreEqual(ExpectedBinarySubtractionResult, result, "X - Y did not produce the expected result."); } { T result = Operator.Subtract(CreateY(), CreateX()); Assert.AreEqual(OppositeExpectedBinarySubtractionResult, result, "Y - X did not produce the expected result."); } } else Assert.Pass("Type does not implement binary subtraction operator."); } [Test, Description("A test for checking the negation (-) operator.")] public void NegationTest() { if (default(T).Equals(ExpectedNegationResult) == false) { { T result = Operator.Negate(CreateX()); Assert.AreEqual(ExpectedNegationResult, result, "-X + Y did not produce the expected result."); } } else Assert.Pass("Type does not implement negation operator."); } [Test, Description("A test for checking the multiplication operator.")] public void MultiplicationTest() { if (default(T).Equals(ExpectedMultiplicationResult) == false) { { T result = Operator.Multiply(CreateX(), CreateY()); Assert.AreEqual(ExpectedMultiplicationResult, result, "X * Y did not produce the expected result."); } { T result = Operator.Multiply(CreateY(), CreateX()); Assert.AreEqual(OppositeExpectedMultiplicationResult, result, "Y * X did not produce the expected result."); } } else Assert.Pass("Type does not implement multiplication operator."); } [Test, Description("A test for checking the division operator.")] public void DivisionTest() { if (default(T).Equals(ExpectedDivisionResult) == false) { { T result = Operator.Divide(CreateX(), CreateY()); Assert.AreEqual(ExpectedDivisionResult, result, "X / Y did not produce the expected result."); } { T result = Operator.Divide(CreateY(), CreateX()); Assert.AreEqual(OppositeExpectedDivisionResult, result, "Y / X did not produce the expected result."); } } else Assert.Pass("Type does not implement division operator."); } // TODO: Tests for the increment (++x), decrement (--x), and unary plus (+x) operators need to be implemented. /// /// Creates the first operand or 'X'. /// /// The instance to be tested. protected abstract T CreateX(); /// /// Creates the second operand or 'Y'. /// /// The instance to be tested. protected abstract T CreateY(); /// /// Returns the result that is expected from the operation: /// + . If this property /// returns the default(T) value, then it is assumed that the type /// does not implement a binary addition /// operation and it is therefore not tested. /// protected abstract T ExpectedBinaryAdditionResult { get; } /// /// Returns the result that is expected from the operation: /// + . By default, /// this property returns the value of /// . In other words, by /// default, the test assumes the operation being tested on type /// is commutative. /// protected virtual T OppositeExpectedBinaryAdditionResult { get { return ExpectedBinaryAdditionResult; } } /// /// Returns the result that is expected from the operation: /// - . If this property /// returns the default(T) value, then it is assumed that the type /// does not implement a binary subtraction /// operation and it is therefore not tested. /// protected abstract T ExpectedBinarySubtractionResult { get; } /// /// Returns the result that is expected from the operation: /// - . If this property /// returns the default(T) value, then it is assumed that the type /// does not implement a binary subtraction /// operation and it is therefore not tested. /// protected abstract T OppositeExpectedBinarySubtractionResult { get; } /// /// Returns the result that is expected from the operation: /// -. If this property returns the default(T) /// value, then it is assumed that the type /// does not implement a negation operation and it is therefore not /// tested. /// protected abstract T ExpectedNegationResult { get; } /// /// Returns the result that is expected from the operation: /// * . If this property /// returns the default(T) value, then it is assumed that the type /// does not implement a multiplication /// operation and it is therefore not tested. /// protected abstract T ExpectedMultiplicationResult { get; } /// /// Returns the result that is expected from the operation: /// * . By default, /// this property returns the value of /// . In other words, by /// default, the test assumes the operation being tested on type /// is commutative. /// protected virtual T OppositeExpectedMultiplicationResult { get { return ExpectedMultiplicationResult; } } /// /// Returns the result that is expected from the operation: /// / . If this property /// returns the default(T) value, then it is assumed that the type /// does not implement a division /// operation and it is therefore not tested. /// protected abstract T ExpectedDivisionResult { get; } /// /// Returns the result that is expected from the operation: /// / . If this property /// returns the default(T) value, then it is assumed that the type /// does not implement a division /// operation and it is therefore not tested. /// protected abstract T OppositeExpectedDivisionResult { get; } } }