public void CombineFilterWithHiddenBehindIfAndExtraDependentStatements() { // When we move an if statement, if there are extra statements and they depend on the code // we want to move, then we can't move them. // Top level guy. This is the unique filter statement. var filterUnique = new StatementFilter(new ValSimple("fUnique", typeof(bool))); // Next, we will do the two common ones. var f1 = new StatementFilter(new ValSimple("f1", typeof(bool))); var f2 = new StatementFilter(new ValSimple("f1", typeof(bool))); var p = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var a1 = new StatementAssign(p, new ValSimple("5", typeof(int))); var a2 = new StatementAssign(p, new ValSimple("5", typeof(int))); f1.Add(a1); f1.Add(p); f2.Add(a2); f2.Add(p); // Now, a unique assignment. This can't be lifted b.c. it is hidden behind a different if statement in // the outside (the filterUnique). var pSpecial = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var aUnique = new StatementAssign(pSpecial, p); f1.Add(aUnique); f1.Add(pSpecial); filterUnique.Add(f1); var topLevel = new StatementInlineBlock(); topLevel.Add(filterUnique); topLevel.Add(f2); Console.WriteLine("Before optimization:"); foreach (var l in topLevel.CodeItUp()) { Console.WriteLine(l); } // The combine should fail. Assert.IsFalse(f2.TryCombineStatement(f1, null), "The two are different if statements, so it should have failed"); Console.WriteLine("After optimization:"); foreach (var l in topLevel.CodeItUp()) { Console.WriteLine(l); } // But some statements should have been moved! (note that f1 normally has two statements). Assert.AreEqual(2, f1.Statements.Count()); Assert.AreEqual(1, f2.Statements.Count()); }
public void SingleStatementCodeWithNoBrackets() { StatementInlineBlock b = new StatementInlineBlock(blockShouldBeBraced: false); b.Add(new Statements.StatementSimpleStatement("Bork")); var r = b.CodeItUp().ToArray(); Assert.AreEqual(1, r.Length, "# of statements"); }
public void TestSimpleVariableCodingNoDeclAndDecl() { StatementInlineBlock b = new StatementInlineBlock(); b.Add(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int))); b.Add(new Statements.StatementSimpleStatement("Bork")); var r = b.CodeItUp().ToArray(); Assert.AreEqual(4, r.Length, "# of statements"); }
public void TestSimpleVariableCodingNoDecl() { // No statements - so there should be no declares. StatementInlineBlock b = new StatementInlineBlock(); b.Add(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int))); var r = b.CodeItUp().ToArray(); Assert.AreEqual(0, r.Length, "# of statements"); }
public void TestSimpleCodeing() { StatementInlineBlock b = new StatementInlineBlock(); b.Add(new StatementSimpleStatement("junk;")); var r = b.CodeItUp().ToArray(); Assert.AreEqual(3, r.Length, "incorrect number of lines"); Assert.AreEqual("{", r[0], "open bracket"); Assert.AreEqual("}", r[2], "close bracket"); Assert.AreEqual(" junk;", r[1], "statement"); }
public void TestSimpleVariableCoding() { StatementInlineBlock b = new StatementInlineBlock(); b.Add(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int))); b.Add(new Statements.StatementSimpleStatement("bork")); var r = b.CodeItUp().ToArray(); Assert.AreEqual(4, r.Length, "incorrect number of lines"); Assert.AreEqual("{", r[0], "open bracket"); Assert.AreEqual("}", r[3], "close bracket"); Assert.IsTrue(r[1].EndsWith("= 0;")); }
public void TestCombineWithAlteredValue() { // This variable will be modified in an assignment statement. var varToBeModified = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var statementModifier = new StatementAssign(varToBeModified, new ValSimple("1", typeof(int))); // Next, we access this variable in an if statement. var finalVar = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var assignment = new StatementAssign(finalVar, varToBeModified); var checkVar = DeclarableParameter.CreateDeclarableParameterExpression(typeof(bool)); var ifUsesModifiedValue = new StatementFilter(new ValSimple(checkVar.RawValue, typeof(bool))); ifUsesModifiedValue.Add(assignment); var ifNoUsesModifiedValue = new StatementFilter(new ValSimple(checkVar.RawValue, typeof(bool))); // Ok, now create the two sets of top level statements. var blockWithModified = new StatementInlineBlock(); var blockWithoutModified = new StatementInlineBlock(); blockWithModified.Add(varToBeModified); blockWithModified.Add(finalVar); blockWithModified.Add(statementModifier); blockWithModified.Add(checkVar); blockWithoutModified.Add(checkVar); blockWithModified.Add(ifUsesModifiedValue); blockWithoutModified.Add(ifNoUsesModifiedValue); // Combine var r = blockWithoutModified.TryCombineStatement(blockWithModified, null); Assert.IsTrue(r, "try combine result"); foreach (var s in blockWithoutModified.CodeItUp()) { System.Diagnostics.Trace.WriteLine(s); } // Make sure the checkVar guy comes after the modified statement. var topLevelStatementForAssign = findStatementThatContains(blockWithoutModified, assignment); var posOfUse = findStatementIndex(blockWithoutModified, topLevelStatementForAssign); var posOfMod = findStatementIndex(blockWithoutModified, statementModifier); Assert.IsTrue(posOfMod < posOfUse, string.Format("Modification happens after use. modification: {0} use {1}", posOfMod, posOfUse)); }
public void BlockStaticDefinition() { var b = new StatementInlineBlock(); var p = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); p.DeclareAsStatic = true; b.Add(p); b.Add(new StatementAssign(p, new ValSimple("10", typeof(int)))); var r = b.CodeItUp().ToArray(); Assert.AreEqual(1, r.Where(l => l.Contains("static int aInt32")).Count()); }
public void DeclarationsAreMovedCorrectlyWhenStatementsReassigned() { // In this new world of moving things around, we move declaration and statements, but they aren't really connected. // So we should make sure that declaration aren't moved accidentally when they shouldn't be. // Inline block at the top var topLevel1 = new StatementInlineBlock(); var topLevel2 = new StatementInlineBlock(); // Top level guy. This is the unique filter statement. var filterUnique = new StatementFilter(new ValSimple("fUnique", typeof(bool))); topLevel1.Add(filterUnique); // Next, we will do the two common ones. var f1 = new StatementFilter(new ValSimple("f1", typeof(bool))); var f2 = new StatementFilter(new ValSimple("f1", typeof(bool))); var p1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var p2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); filterUnique.Add(p1); topLevel2.Add(p2); var a1 = new StatementAssign(p1, new ValSimple("5", typeof(int))); var a2 = new StatementAssign(p2, new ValSimple("5", typeof(int))); f1.Add(a1); f2.Add(a2); filterUnique.Add(f1); topLevel2.Add(f2); Console.WriteLine("Before optimization (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("Before optimization (what is being merged):"); topLevel1.DumpCodeToConsole(); Assert.IsTrue(f2.TryCombineStatement(f1, null), "Two of the same if statements, and the combine should have worked"); Console.WriteLine("After optimization:"); foreach (var l in topLevel2.CodeItUp()) { Console.WriteLine(l); } Assert.AreEqual(1, f2.Statements.Count()); }
public void TestCodeItUp() { StatementInlineBlock b = new StatementInlineBlock(); Assert.AreEqual(0, b.CodeItUp().Count(), "Expect nothing for an empty inline block"); }
public void TestCombinePreserveOrder() { // We will have two if statements to do the combination with. They basically "hide" the modification. var checkVar1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(bool)); var if1s1 = new StatementFilter(new ValSimple(checkVar1.RawValue, typeof(bool))); var if1s2 = new StatementFilter(new ValSimple(checkVar1.RawValue, typeof(bool))); var checkVar2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(bool)); var if2s1 = new StatementFilter(new ValSimple(checkVar2.RawValue, typeof(bool))); var if2s2 = new StatementFilter(new ValSimple(checkVar2.RawValue, typeof(bool))); var blockWithModified = new StatementInlineBlock(); var blockWithoutModified = new StatementInlineBlock(); blockWithModified.Add(checkVar1); blockWithModified.Add(checkVar2); blockWithoutModified.Add(checkVar1); blockWithoutModified.Add(checkVar2); // Not the opposite order we put them in here! blockWithModified.Add(if1s1); blockWithModified.Add(if2s1); blockWithoutModified.Add(if2s2); blockWithoutModified.Add(if1s2); // Have the modified if statement contain the modification now. var varToBeModified = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var statementModifier = new StatementAssign(varToBeModified, new ValSimple("1", typeof(int))); blockWithModified.Add(varToBeModified); if1s1.Add(statementModifier); // Next, we need to use the variable in the second if statement. Which, since it is like the first, should be pushed back up there. var finalVar = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var assignment = new StatementAssign(finalVar, varToBeModified); blockWithModified.Add(finalVar); if2s1.Add(assignment); // Combine var r = blockWithoutModified.TryCombineStatement(blockWithModified, null); Assert.IsTrue(r, "try combine result"); foreach (var s in blockWithoutModified.CodeItUp()) { System.Diagnostics.Trace.WriteLine(s); } // Make sure the checkVar guy comes after the modified statement. var topLevelStatementForAssign = findStatementThatContains(blockWithoutModified, assignment); var posOfUse = findStatementIndex(blockWithoutModified, topLevelStatementForAssign); var topLevelStatementForModification = findStatementThatContains(blockWithoutModified, statementModifier); var posOfMod = findStatementIndex(blockWithoutModified, topLevelStatementForModification); Assert.IsTrue(posOfMod < posOfUse, string.Format("Modification happens after use. modification: {0} use {1}", posOfMod, posOfUse)); }
public void TestCombineMinimalOrdering() { // We will have two if statements to do the combination with. They basically "hide" the modification. var checkVar1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(bool)); var if1s1 = new StatementFilter(new ValSimple(checkVar1.RawValue, typeof(bool))); var if1s2 = new StatementFilter(new ValSimple(checkVar1.RawValue, typeof(bool))); var checkVar2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(bool)); var if2s1 = new StatementFilter(new ValSimple(checkVar2.RawValue, typeof(bool))); var if2s2 = new StatementFilter(new ValSimple(checkVar2.RawValue, typeof(bool))); var dummyVar = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var blockWithModified = new StatementInlineBlock(); var blockWithoutModified = new StatementInlineBlock(); blockWithModified.Add(checkVar1); blockWithModified.Add(checkVar2); blockWithModified.Add(dummyVar); blockWithoutModified.Add(checkVar1); blockWithoutModified.Add(checkVar2); blockWithoutModified.Add(dummyVar); // Not the opposite order we put them in here! blockWithModified.Add(if1s1); blockWithModified.Add(if2s1); blockWithoutModified.Add(if2s2); blockWithoutModified.Add(if1s2); if1s1.Add(new StatementAssign(dummyVar, new ValSimple("1", typeof(int)))); if2s1.Add(new StatementAssign(dummyVar, new ValSimple("2", typeof(int)))); if1s2.Add(new StatementAssign(dummyVar, new ValSimple("3", typeof(int)))); if2s2.Add(new StatementAssign(dummyVar, new ValSimple("4", typeof(int)))); // Have the modified if statement contain the modification now. var varToBeModified = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var statementModifier = new StatementAssign(varToBeModified, new ValSimple("1", typeof(int))); blockWithModified.Add(varToBeModified); if1s1.Add(statementModifier); // Next, we need to use the variable in the second if statement. Which, since it is like the first, should be pushed back up there. var finalVar = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var assignment = new StatementAssign(finalVar, varToBeModified); blockWithModified.Add(finalVar); if2s1.Add(assignment); // Combine var r = blockWithoutModified.TryCombineStatement(blockWithModified, null); Assert.IsTrue(r, "try combine result"); foreach (var s in blockWithoutModified.CodeItUp()) { System.Diagnostics.Trace.WriteLine(s); } // Make sure the checkVar guy comes after the modified statement. // To get this right (the 3 should be a 2), we need to implement a full blown statement optimizer. Assert.AreEqual(3, blockWithoutModified.Statements.Where(s => s is StatementFilter).Count(), "# of if statements."); }