namespace HeuristicLab.Tests.Interpreter.Expressions { using System; using HeuristicLab.Algorithms.PushGP.Expressions; using HeuristicLab.Algorithms.PushGP.Stack; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class FloatExpressionTests : CommonTests { private const double delta = 0.00001; protected override string TypeName { get { return "FLOAT"; } } protected override IStack Stack { get { return this.interpreter.FloatStack; } } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestAdd() { this.interpreter.FloatStack.Push(5.3, 5.3); this.interpreter.Interpret(new FloatAddExpression()); Assert.AreEqual(10.6, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestAddWithInsufficientArguments() { this.TestWithInsufficientArguments("+", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestSubtract() { this.interpreter.FloatStack.Push(10.2, 5.3); this.interpreter.Interpret(new FloatSubtractExpression()); Assert.AreEqual(4.9, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestSubractWithInsufficientArguments() { this.TestWithInsufficientArguments("-", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestMultiply() { this.interpreter.FloatStack.Push(9.9, 3.3); this.interpreter.Interpret(new FloatMultiplyExpression()); Assert.AreEqual(32.67, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestMultiplyWithInsufficientArguments() { this.TestWithInsufficientArguments("*", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestDivide() { this.interpreter.FloatStack.Push(9.9, 3.3); this.interpreter.Interpret(new FloatDivideExpression()); Assert.AreEqual(3.0, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestDivideWithInsufficientArguments() { this.TestWithInsufficientArguments("/", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestModulo() { this.interpreter.FloatStack.Push(11.6, 5.3); this.interpreter.Interpret(new FloatModuloExpression()); Assert.AreEqual(1, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestModuloWithInsufficientArguments() { this.TestWithInsufficientArguments("%", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestMin() { this.interpreter.FloatStack.Push(10.2, 5.3); this.interpreter.Interpret(new FloatMinExpression()); Assert.AreEqual(5.3, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestMinWithInsufficientArguments() { this.TestWithInsufficientArguments("MIN", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestMax() { this.interpreter.FloatStack.Push(10.3, 5.3); this.interpreter.Interpret(new FloatMaxExpression()); Assert.AreEqual(10.3, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestMaxWithInsufficientArguments() { this.TestWithInsufficientArguments("MAX", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestSmallerThan() { this.interpreter.FloatStack.Push(10.2, 5.3); this.interpreter.Interpret(new FloatSmallerThanExpression()); Assert.AreEqual(false, this.interpreter.BooleanStack.Top); this.TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestSmallerThanWithInsufficientArguments() { this.TestWithInsufficientArguments("<", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestGreaterThan() { this.interpreter.FloatStack.Push(10.2, 5.3); this.interpreter.Interpret(new FloatGreaterThanExpression()); Assert.AreEqual(true, this.interpreter.BooleanStack.Top); this.TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestGreaterThanWithInsufficientArguments() { this.TestWithInsufficientArguments(">", 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestFromBooleanTrue() { this.interpreter.BooleanStack.Push(true); this.interpreter.Interpret(new FloatFromBooleanExpression()); Assert.AreEqual(1d, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestFromBooleanFalse() { this.interpreter.BooleanStack.Push(false); this.interpreter.Interpret(new FloatFromBooleanExpression()); Assert.AreEqual(0d, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestFromBooleanWithInsufficientArguments() { this.TestWithInsufficientArguments("FROMBOOLEAN"); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestFromInteger() { this.interpreter.IntegerStack.Push(5); this.interpreter.Interpret(new FloatFromIntegerExpression()); Assert.AreEqual(5d, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestFromIntegerWithInsufficientArguments() { this.TestWithInsufficientArguments("FROMINTEGER"); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestSine() { this.interpreter.FloatStack.Push(Math.PI / 2); this.interpreter.Interpret(new FloatSineExpression()); Assert.AreEqual(1, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestSineWithInsufficientArguments() { this.TestWithInsufficientArguments("SIN"); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestCosine() { this.interpreter.FloatStack.Push(Math.PI); this.interpreter.Interpret(new FloatCosineExpression()); Assert.AreEqual(-1, this.interpreter.FloatStack.Top, delta); this.TestStackCounts(floatStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("FloatExpressionTest")] public void TestCosineWithInsufficientArguments() { this.TestWithInsufficientArguments("COS"); } protected override double[] GetValues(int count) { var values = new double[count]; for (var i = 0; i < count; i++) values[i] = i * 0.9; return values; } protected override void CheckOtherStacksAreEmpty() { this.TestStackCounts(floatStack: null); } } }