using System.Linq; namespace HeuristicLab.Tests.Interpreter.Expressions { using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class StringExpressionTests : CommonTests { protected override string TypeName { get { return "STRING"; } } protected override IPushStack Stack { get { return interpreter.StringStack; } } protected override string[] GetValues(int count) { return Enumerable .Range(0, count) .Select(_ => interpreter.Configuration.ErcOptions.StringErcOptions.GetErcValue(interpreter.Random)) .ToArray(); } protected override void CheckOtherStacksAreEmpty() { TestStackCounts(stringStack: null); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestFromInteger() { interpreter.IntegerStack.Push(123, -123); interpreter.Run(new StringFromIntegerExpression()); interpreter.Run(new StringFromIntegerExpression()); interpreter.Run(new StringFromIntegerExpression()); Assert.AreEqual("123", interpreter.StringStack[0]); Assert.AreEqual("-123", interpreter.StringStack[1]); TestStackCounts(stringStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestFromFloat() { interpreter.FloatStack.Push(123.45, -123.45); interpreter.Run(new StringFromFloatExpression()); interpreter.Run(new StringFromFloatExpression()); interpreter.Run(new StringFromFloatExpression()); Assert.AreEqual("123.45", interpreter.StringStack[0]); Assert.AreEqual("-123.45", interpreter.StringStack[1]); TestStackCounts(stringStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestFromBoolean() { interpreter.BooleanStack.Push(true, false); interpreter.Run(new StringFromBooleanExpression()); interpreter.Run(new StringFromBooleanExpression()); Assert.AreEqual("True", interpreter.StringStack[0]); Assert.AreEqual("False", interpreter.StringStack[1]); TestStackCounts(stringStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestFromChar() { interpreter.CharStack.Push(' ', 'a'); interpreter.Run(new StringFromCharExpression()); interpreter.Run(new StringFromCharExpression()); Assert.AreEqual(" ", interpreter.StringStack[0]); Assert.AreEqual("a", interpreter.StringStack[1]); TestStackCounts(stringStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestConcat() { interpreter.StringStack.Push("test", "123"); interpreter.Run(new StringConcatExpression()); Assert.AreEqual("test123", interpreter.StringStack[0]); TestStackCounts(stringStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestConjChar() { interpreter.CharStack.Push('3', '2', '1'); interpreter.StringStack.Push("test"); interpreter.Run(new StringConjCharExpression()); interpreter.Run(new StringConjCharExpression()); interpreter.Run(new StringConjCharExpression()); Assert.AreEqual("test123", interpreter.StringStack[0]); TestStackCounts(stringStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestTake() { interpreter.IntegerStack.Push(3, -3, 7, 0); interpreter.StringStack.Push("test"); interpreter.Run(new StringTakeExpression()); Assert.AreEqual("", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.Run(new StringTakeExpression()); Assert.AreEqual("tes", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.Run(new StringTakeExpression()); Assert.AreEqual("tes", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.Run(new StringTakeExpression()); Assert.AreEqual("tes", interpreter.StringStack[0]); TestStackCounts(stringStack: 4); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestFirst() { interpreter.StringStack.Push("test"); interpreter.Run(new StringFirstExpression()); Assert.AreEqual("t", interpreter.StringStack[0]); TestStackCounts(stringStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestLast() { interpreter.StringStack.Push("test"); interpreter.Run(new StringLastExpression()); Assert.AreEqual("t", interpreter.StringStack[0]); TestStackCounts(stringStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestNth() { interpreter.IntegerStack.Push(2, -2, 6); interpreter.StringStack.Push("test"); interpreter.Run(new StringNthExpression()); Assert.AreEqual("s", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.Run(new StringNthExpression()); Assert.AreEqual("s", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.Run(new StringNthExpression()); Assert.AreEqual("s", interpreter.StringStack[0]); TestStackCounts(stringStack: 3); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestRest() { interpreter.StringStack.Push("test"); interpreter.Run(new StringRestExpression()); Assert.AreEqual("est", interpreter.StringStack[0]); TestStackCounts(stringStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestButLast() { interpreter.StringStack.Push("test"); interpreter.Run(new StringButLastExpression()); Assert.AreEqual("tes", interpreter.StringStack[0]); TestStackCounts(stringStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestLength() { interpreter.StringStack.Push("test"); interpreter.Run(new StringLengthExpression()); Assert.AreEqual(4, interpreter.IntegerStack.Top); TestStackCounts(stringStack: 0, integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestReverse() { interpreter.StringStack.Push("test"); interpreter.Run(new StringReverseExpression()); Assert.AreEqual("tset", interpreter.StringStack[0]); TestStackCounts(stringStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestParseToChars() { interpreter.StringStack.Push("test"); interpreter.Run(new StringParseToCharsExpression()); Assert.AreEqual("t", interpreter.StringStack[0]); Assert.AreEqual("s", interpreter.StringStack[1]); Assert.AreEqual("e", interpreter.StringStack[2]); Assert.AreEqual("t", interpreter.StringStack[3]); TestStackCounts(stringStack: 4); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestSplit() { interpreter.StringStack.Push("test1 test2"); interpreter.Run(new StringSplitExpression()); Assert.AreEqual("test1", interpreter.StringStack[0]); Assert.AreEqual("test2", interpreter.StringStack[1]); TestStackCounts(stringStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestEmptyString() { interpreter.StringStack.Push("test1", ""); interpreter.Run(new StringEmptyStringExpression()); interpreter.Run(new StringEmptyStringExpression()); Assert.AreEqual(false, interpreter.BooleanStack[0]); Assert.AreEqual(true, interpreter.BooleanStack[1]); TestStackCounts(stringStack: 0, booleanStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestContains() { interpreter.StringStack.Push(new[] { "test1", "est", "test", "abc", "test", "" }); interpreter.Run(new StringContainsExpression()); interpreter.Run(new StringContainsExpression()); interpreter.Run(new StringContainsExpression()); Assert.AreEqual(true, interpreter.BooleanStack[0]); Assert.AreEqual(false, interpreter.BooleanStack[1]); Assert.AreEqual(true, interpreter.BooleanStack[2]); TestStackCounts(stringStack: 0, booleanStack: 3); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestContainsChar() { interpreter.CharStack.Push('e', 'a'); interpreter.StringStack.Push("test", "test"); interpreter.Run(new StringContainsCharExpression()); interpreter.Run(new StringContainsCharExpression()); Assert.AreEqual(true, interpreter.BooleanStack[0]); Assert.AreEqual(false, interpreter.BooleanStack[1]); TestStackCounts(stringStack: 0, charStack: 0, booleanStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestIndexOfChar() { interpreter.CharStack.Push('e', 'a'); interpreter.StringStack.Push("test", "test"); interpreter.Run(new StringIndexOfCharExpression()); interpreter.Run(new StringIndexOfCharExpression()); Assert.AreEqual(1, interpreter.IntegerStack[0]); Assert.AreEqual(-1, interpreter.IntegerStack[1]); TestStackCounts(stringStack: 0, charStack: 0, integerStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestOccurrencesOfChar() { interpreter.CharStack.Push('t', 'a'); interpreter.StringStack.Push("test", "test"); interpreter.Run(new StringOccurrencesOfCharExpression()); interpreter.Run(new StringOccurrencesOfCharExpression()); Assert.AreEqual(2, interpreter.IntegerStack[0]); Assert.AreEqual(0, interpreter.IntegerStack[1]); TestStackCounts(stringStack: 0, charStack: 0, integerStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestReplace() { interpreter.StringStack.Push("test", "es", "12"); interpreter.Run(new StringReplaceExpression()); Assert.AreEqual("t12t", interpreter.StringStack[0]); interpreter.StringStack.Push("test test", "es", "12"); interpreter.Run(new StringReplaceExpression()); Assert.AreEqual("t12t t12t", interpreter.StringStack[0]); interpreter.StringStack.Push("test", "ab", "12"); interpreter.Run(new StringReplaceExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push("test", "", "12"); interpreter.Run(new StringReplaceExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push("test", "ab", ""); interpreter.Run(new StringReplaceExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push("test", "es", ""); interpreter.Run(new StringReplaceExpression()); Assert.AreEqual("tt", interpreter.StringStack[0]); TestStackCounts(stringStack: 6); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestReplaceFirst() { interpreter.StringStack.Push("test", "es", "12"); interpreter.Run(new StringReplaceFirstExpression()); Assert.AreEqual("t12t", interpreter.StringStack[0]); interpreter.StringStack.Push("test test", "es", "12"); interpreter.Run(new StringReplaceFirstExpression()); Assert.AreEqual("t12t test", interpreter.StringStack[0]); interpreter.StringStack.Push("test", "ab", "12"); interpreter.Run(new StringReplaceFirstExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push("test", "", "12"); interpreter.Run(new StringReplaceFirstExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push("test", "ab", ""); interpreter.Run(new StringReplaceFirstExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push("test", "es", ""); interpreter.Run(new StringReplaceFirstExpression()); Assert.AreEqual("tt", interpreter.StringStack[0]); TestStackCounts(stringStack: 6); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestReplaceChar() { interpreter.StringStack.Push("test"); interpreter.CharStack.Push('t', 'T'); interpreter.Run(new StringReplaceCharExpression()); Assert.AreEqual("TesT", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.CharStack.Push('a', 'T'); interpreter.Run(new StringReplaceCharExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push(""); interpreter.CharStack.Push('t', 'T'); interpreter.Run(new StringReplaceCharExpression()); Assert.AreEqual("", interpreter.StringStack[0]); TestStackCounts(stringStack: 3); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestReplaceFirstChar() { interpreter.StringStack.Push("test"); interpreter.CharStack.Push('t', 'T'); interpreter.Run(new StringReplaceFirstCharExpression()); Assert.AreEqual("Test", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.CharStack.Push('a', 'T'); interpreter.Run(new StringReplaceFirstCharExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push(""); interpreter.CharStack.Push('t', 'T'); interpreter.Run(new StringReplaceFirstCharExpression()); Assert.AreEqual("", interpreter.StringStack[0]); TestStackCounts(stringStack: 3); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestRemoveChar() { interpreter.StringStack.Push("test"); interpreter.CharStack.Push('t'); interpreter.Run(new StringRemoveCharExpression()); Assert.AreEqual("es", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.CharStack.Push('a'); interpreter.Run(new StringRemoveCharExpression()); Assert.AreEqual("test", interpreter.StringStack[0]); interpreter.StringStack.Push(""); interpreter.CharStack.Push('t'); interpreter.Run(new StringRemoveCharExpression()); Assert.AreEqual("", interpreter.StringStack[0]); TestStackCounts(stringStack: 3); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestSetChar() { interpreter.StringStack.Push("test"); interpreter.CharStack.Push('x'); interpreter.IntegerStack.Push(2); interpreter.Run(new StringSetCharExpression()); Assert.AreEqual("text", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.CharStack.Push('x'); interpreter.IntegerStack.Push(-2); interpreter.Run(new StringSetCharExpression()); Assert.AreEqual("text", interpreter.StringStack[0]); interpreter.StringStack.Push("test"); interpreter.CharStack.Push('x'); interpreter.IntegerStack.Push(6); interpreter.Run(new StringSetCharExpression()); Assert.AreEqual("text", interpreter.StringStack[0]); interpreter.StringStack.Push(""); interpreter.CharStack.Push('x'); interpreter.IntegerStack.Push(2); interpreter.Run(new StringSetCharExpression()); Assert.AreEqual("", interpreter.StringStack[0]); interpreter.StringStack.Push("s"); interpreter.CharStack.Push('x'); interpreter.IntegerStack.Push(2); interpreter.Run(new StringSetCharExpression()); Assert.AreEqual("x", interpreter.StringStack[0]); TestStackCounts(stringStack: 5); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StringExpressionTest")] public void TestIterate() { interpreter.StringStack.Push("test"); interpreter.IntegerStack.Push(0); interpreter.Run(new PushProgram(new Expression[] { new IntegerIncExpression(), new StringIterateExpression(), })); Assert.AreEqual(4, interpreter.IntegerStack[0]); TestStackCounts(integerStack: 1, charStack: 4); } } }