namespace HeuristicLab.Tests.Interpreter.Expressions { using System.Collections.Generic; using System.Linq; using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; using Microsoft.VisualStudio.TestTools.UnitTesting; public abstract class VectorExpressionTests : CommonTests> { protected abstract IPushStack LiteralStack { get; } protected abstract IReadOnlyList GetLiterals(int count); [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorButLast() { var vector = GetValues(1)[0]; Stack.Push(vector); var result = vector.Take(vector.Count - 1); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".BUTLAST"); interpreter.Run(expression); Assert.IsTrue(result.SequenceEqual(Stack.Top)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorConcat() { var vectors = GetValues(2); Stack.Push(vectors); var result = vectors.SelectMany(x => x); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".CONCAT"); interpreter.Run(expression); Assert.IsTrue(result.SequenceEqual(Stack.Top)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorConj() { var vectors = GetValues(1); var literals = GetLiterals(1); Stack.Push(vectors); LiteralStack.Push(literals); var result = new List(vectors[0]); result.AddRange(literals); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".CONJ"); interpreter.Run(expression); Assert.IsTrue(result.SequenceEqual(Stack.Top)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorContains() { var vectors = Get2Different(); var literalContained = vectors[0].Last(); var literalNotContained = vectors[1].FirstOrDefault(x => !vectors[0].Contains(x)); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".CONTAINS"); Stack.Push(vectors[0], vectors[0]); LiteralStack.Push(literalContained, literalNotContained); interpreter.Run(expression); interpreter.Run(expression); Assert.IsTrue(interpreter.BooleanStack[0]); Assert.IsFalse(interpreter.BooleanStack[1]); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorEmpty() { var emptyVector = new List(); var noneEmptyVector = GetValues(1)[0]; var expression = ExpressionTable.GetStatelessExpression(TypeName + ".EMPTYVECTOR"); Stack.Push(emptyVector, noneEmptyVector); interpreter.Run(expression); interpreter.Run(expression); Assert.IsTrue(interpreter.BooleanStack[0]); Assert.IsFalse(interpreter.BooleanStack[1]); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorFirst() { var vector = GetValues(1)[0]; var emptyVector = new List(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".FIRST"); Stack.Push(emptyVector); interpreter.Run(expression); Assert.IsTrue(LiteralStack.IsEmpty); Stack.Push(vector); interpreter.Run(expression); Assert.AreEqual(vector.First(), LiteralStack.Top); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorIndexOf() { var vector = GetValues(1)[0].Distinct().ToList(); var emptyVector = new List(); var literal = vector.Last(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".INDEXOF"); Stack.Push(emptyVector); LiteralStack.Push(literal); interpreter.Run(expression); Assert.IsTrue(LiteralStack == interpreter.IntegerStack ? LiteralStack.Count == 1 : LiteralStack.IsEmpty); Stack.Push(vector); LiteralStack.Push(literal); interpreter.Run(expression); Assert.AreEqual(vector.Count - 1, interpreter.IntegerStack.Top); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorIterate() { var vector = GetValues(1)[0]; var emptyVector = new List(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".ITERATE"); var bodyExpression = new ExecNoopExpression(); Stack.Push(emptyVector); interpreter.Run(new PushProgram(new[] { expression, bodyExpression })); Assert.IsTrue(LiteralStack.IsEmpty); Assert.AreEqual(0, Stack.Top.Count); Stack.Push(vector); interpreter.Run(new PushProgram(new[] { bodyExpression, expression })); Assert.AreEqual(vector.Count, LiteralStack.Count); Assert.IsTrue(LiteralStack.Reverse().SequenceEqual(vector)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorLast() { var vector = GetValues(1)[0]; var emptyVector = new List(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".LAST"); Stack.Push(emptyVector); interpreter.Run(expression); Assert.IsTrue(LiteralStack.IsEmpty); Stack.Push(vector); interpreter.Run(expression); Assert.AreEqual(vector.Last(), LiteralStack.Top); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorLength() { var vector = GetValues(1)[0]; var emptyVector = new List(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".LENGTH"); Stack.Push(emptyVector); interpreter.Run(expression); Assert.AreEqual(0, interpreter.IntegerStack.Top); Stack.Push(vector); interpreter.Run(expression); Assert.AreEqual(vector.Count, interpreter.IntegerStack.Top); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorNth() { var vector = GetValues(1)[0]; var emptyVector = new List(); var index = vector.Count / 2; var expression = ExpressionTable.GetStatelessExpression(TypeName + ".NTH"); Stack.Push(emptyVector); interpreter.IntegerStack.Push(index); interpreter.Run(expression); Assert.IsTrue(LiteralStack == interpreter.IntegerStack ? LiteralStack.Count == 1 : LiteralStack.IsEmpty); Stack.Push(vector); interpreter.IntegerStack.Push(index); interpreter.Run(expression); Assert.AreEqual(vector[index], LiteralStack.Top); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorOccurrenceOf() { var vector = GetValues(1)[0]; var literal = vector.Last(); var result = vector.Count(x => x.Equals(literal)); var emptyVector = new List(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".OCCURRENCEOF"); Stack.Push(emptyVector); LiteralStack.Push(literal); interpreter.Run(expression); Assert.AreEqual(0, interpreter.IntegerStack.Top); Stack.Push(vector); LiteralStack.Push(literal); interpreter.Run(expression); Assert.AreEqual(result, interpreter.IntegerStack.Top); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorPushAll() { var vector = GetValues(1)[0]; var emptyVector = new List(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".PUSHALL"); Stack.Push(emptyVector); interpreter.Run(expression); Assert.IsTrue(LiteralStack.IsEmpty); Stack.Push(vector); interpreter.Run(expression); Assert.IsTrue(LiteralStack.Reverse().SequenceEqual(vector)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorRemove() { var vector = GetValues(1)[0]; var emptyVector = new List(); var literal = vector.Last(); var result = vector.Where(x => !x.Equals(literal)); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REMOVE"); Stack.Push(emptyVector); LiteralStack.Push(literal); interpreter.Run(expression); Assert.AreEqual(0, Stack.Top.Count); Stack.Push(vector); LiteralStack.Push(literal); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorReplace() { var vector = GetValues(1)[0]; var emptyVector = new List(); var literal = vector.Last(); var literalTarget = vector.First(); var result = vector.Select(x => x.Equals(literal) ? literalTarget : x); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REPLACE"); Stack.Push(emptyVector); LiteralStack.Push(literal, literalTarget); interpreter.Run(expression); Assert.AreEqual(0, Stack.Top.Count); Stack.Push(vector); LiteralStack.Push(literal, literalTarget); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorReplaceFirst() { var vector = GetValues(1)[0]; var emptyVector = new List(); var literal = vector.Last(); var literalTarget = vector.First(x => !x.Equals(literal)); var firstReplaced = false; var result = vector.Select(x => { if (!x.Equals(literal) || firstReplaced) return x; firstReplaced = true; return literalTarget; }).ToArray(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REPLACEFIRST"); Stack.Push(emptyVector); LiteralStack.Push(literal, literalTarget); interpreter.Run(expression); Assert.AreEqual(0, Stack.Top.Count); Stack.Push(vector); LiteralStack.Push(literal, literalTarget); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorRest() { var vector = GetValues(1)[0]; var emptyVector = new List(); var result = vector.Skip(1); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REST"); Stack.Push(emptyVector); interpreter.Run(expression); Assert.AreEqual(0, Stack.Top.Count); Stack.Push(vector); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorReverse() { var vector = GetValues(1)[0]; var emptyVector = new List(); var result = new List(vector); result.Reverse(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REVERSE"); Stack.Push(emptyVector); interpreter.Run(expression); Assert.AreEqual(emptyVector, Stack.Top); Stack.Push(vector); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorSet() { var vector = GetValues(1)[0]; var emptyVector = new List(); var index = vector.Count / 2; var literal = vector.Last(); var result = vector.ToArray(); result[index] = literal; var expression = ExpressionTable.GetStatelessExpression(TypeName + ".SET"); Stack.Push(emptyVector); LiteralStack.Push(literal); interpreter.IntegerStack.Push(index); interpreter.Run(expression); Assert.AreEqual(emptyVector, Stack.Top); Stack.Push(vector); interpreter.IntegerStack.Push(index); LiteralStack.Push(literal); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorSub() { var vector = GetValues(1)[0]; var emptyVector = new List(); var start = 0; var end = vector.Count / 2; var result = vector.Skip(start).Take(end).ToArray(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".SUB"); Stack.Push(emptyVector); interpreter.IntegerStack.Push(start, end); interpreter.Run(expression); Assert.AreEqual(emptyVector, Stack.Top); Stack.Push(vector); interpreter.IntegerStack.Push(start, end); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("VectorExpressionTest")] public void TestVectorTake() { var vector = GetValues(1)[0]; var emptyVector = new List(); var count = vector.Count / 2; var result = vector.Take(count).ToArray(); var expression = ExpressionTable.GetStatelessExpression(TypeName + ".TAKE"); Stack.Push(emptyVector); interpreter.IntegerStack.Push(count); interpreter.Run(expression); Assert.AreEqual(emptyVector, Stack.Top); Stack.Push(vector); interpreter.IntegerStack.Push(count); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); Stack.Push(vector); interpreter.IntegerStack.Push(count * -1); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); Stack.Push(vector); interpreter.IntegerStack.Push(count + vector.Count); interpreter.Run(expression); Assert.IsTrue(Stack.Top.SequenceEqual(result)); } } }