public void CombineFilterWithHiddenBehindIfAndExtraIndependentStatementsAndDeclaredVariables() { // When we try and fail to combine if statements, make sure we don't leave dangling name // changes - that the declaration aren't totally renamed. // 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 p1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var p2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); f1.Add(p1); f2.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); // 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, new ValSimple("10", typeof(int))); f1.Add(pSpecial); f1.Add(aUnique); filterUnique.Add(f1); var topLevel1 = new StatementInlineBlock(); var topLevel2 = new StatementInlineBlock(); topLevel1.Add(filterUnique); topLevel2.Add(f2); Console.WriteLine("Before optimization (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("Before optimization (merge):"); topLevel1.DumpCodeToConsole(); // 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 (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("After optimization (merge):"); topLevel1.DumpCodeToConsole(); // Nothing should have been touched in f1 - double check. Assert.AreEqual(2, f1.Statements.Count()); Assert.AreEqual(2, f1.DeclaredVariables.Count()); }
public void CombineFilterWithHiddenBehindIfAndExtraIndependentStatements() { // Seen in the wild. We have two identical if statements, one outside, and one inside another // (different) if statement. It is OK to combine these two as the code is identical. // See test CombineFilterWithHiddenBehindIfAndExtraStatements for the case where at // least one statement needs to be left behind. // 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 p1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var p2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var a1 = new StatementAssign(p1, new ValSimple("5", typeof(int))); var a2 = new StatementAssign(p2, new ValSimple("5", typeof(int))); f1.Add(a1); f1.Add(p1); f2.Add(a2); f2.Add(p2); // 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, new ValSimple("10", typeof(int))); f1.Add(aUnique); f1.Add(pSpecial); filterUnique.Add(f1); var topLevel1 = new StatementInlineBlock(); var topLevel2 = new StatementInlineBlock(); topLevel1.Add(filterUnique); topLevel2.Add(f2); Console.WriteLine("Before optimization (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("Before optimization (merge):"); topLevel1.DumpCodeToConsole(); // 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 (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("After optimization (merge):"); topLevel1.DumpCodeToConsole(); // Nothing should have been touched in f1 - double check. Assert.AreEqual(2, f1.Statements.Count()); }
public void CombineFailsWhenNestedIsTarget() { // 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):"); topLevel1.DumpCodeToConsole(); Console.WriteLine("Before optimization (what is being merged):"); topLevel2.DumpCodeToConsole(); Assert.IsFalse(f1.TryCombineStatement(f2, null), "Two of the same if statements, and the combine should have worked"); Console.WriteLine("After optimization (target):"); topLevel1.DumpCodeToConsole(); Console.WriteLine("After optimization (merge):"); topLevel2.DumpCodeToConsole(); }
public void DeclarationsAreIgnoredDuringLowerLevelMove() { // 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 p = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); topLevel1.Add(p); topLevel2.Add(p); var a1 = new StatementAssign(p, new ValSimple("5", typeof(int))); var a2 = new StatementAssign(p, new ValSimple("5", typeof(int))); f1.Add(a1); f2.Add(a2); filterUnique.Add(f1); topLevel2.Add(f2); Console.WriteLine("Before optimization (target):"); topLevel1.DumpCodeToConsole(); Console.WriteLine("After optimization (merge):"); topLevel2.DumpCodeToConsole(); Assert.IsFalse(f1.TryCombineStatement(f2, null), "Two of the same if statements, but the one to be merged is not hidden behind other if statements!"); Assert.AreEqual(1, f1.Statements.Count()); Assert.AreEqual(1, f2.Statements.Count()); }
public void CombineFailsWhenNestedIsTarget() { // 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):"); topLevel1.DumpCodeToConsole(); Console.WriteLine("Before optimization (what is being merged):"); topLevel2.DumpCodeToConsole(); Assert.IsFalse(f1.TryCombineStatement(f2, null), "Two of the same if statements, and the combine should have worked"); Console.WriteLine("After optimization (target):"); topLevel1.DumpCodeToConsole(); Console.WriteLine("After optimization (merge):"); topLevel2.DumpCodeToConsole(); }
public void DeclarationsAreIgnoredDuringLowerLevelMove() { // 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 p = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); topLevel1.Add(p); topLevel2.Add(p); var a1 = new StatementAssign(p, new ValSimple("5", typeof(int))); var a2 = new StatementAssign(p, new ValSimple("5", typeof(int))); f1.Add(a1); f2.Add(a2); filterUnique.Add(f1); topLevel2.Add(f2); Console.WriteLine("Before optimization (target):"); topLevel1.DumpCodeToConsole(); Console.WriteLine("After optimization (merge):"); topLevel2.DumpCodeToConsole(); Assert.IsFalse(f1.TryCombineStatement(f2, null), "Two of the same if statements, but the one to be merged is not hidden behind other if statements!"); Assert.AreEqual(1, f1.Statements.Count()); Assert.AreEqual(1, f2.Statements.Count()); }
public void CombineFilterWithHiddenBehindIfAndExtraIndependentStatementsAndDeclaredVariables() { // When we try and fail to combine if statements, make sure we don't leave dangling name // changes - that the declaration aren't totally renamed. // 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 p1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var p2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); f1.Add(p1); f2.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); // 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, new ValSimple("10", typeof(int))); f1.Add(pSpecial); f1.Add(aUnique); filterUnique.Add(f1); var topLevel1 = new StatementInlineBlock(); var topLevel2 = new StatementInlineBlock(); topLevel1.Add(filterUnique); topLevel2.Add(f2); Console.WriteLine("Before optimization (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("Before optimization (merge):"); topLevel1.DumpCodeToConsole(); // 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 (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("After optimization (merge):"); topLevel1.DumpCodeToConsole(); // Nothing should have been touched in f1 - double check. Assert.AreEqual(2, f1.Statements.Count()); Assert.AreEqual(2, f1.DeclaredVariables.Count()); }
public void CombineFilterWithHiddenBehindIfAndExtraIndependentStatements() { // Seen in the wild. We have two identical if statements, one outside, and one inside another // (different) if statement. It is OK to combine these two as the code is identical. // See test CombineFilterWithHiddenBehindIfAndExtraStatements for the case where at // least one statement needs to be left behind. // 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 p1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var p2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var a1 = new StatementAssign(p1, new ValSimple("5", typeof(int))); var a2 = new StatementAssign(p2, new ValSimple("5", typeof(int))); f1.Add(a1); f1.Add(p1); f2.Add(a2); f2.Add(p2); // 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, new ValSimple("10", typeof(int))); f1.Add(aUnique); f1.Add(pSpecial); filterUnique.Add(f1); var topLevel1 = new StatementInlineBlock(); var topLevel2 = new StatementInlineBlock(); topLevel1.Add(filterUnique); topLevel2.Add(f2); Console.WriteLine("Before optimization (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("Before optimization (merge):"); topLevel1.DumpCodeToConsole(); // 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 (target):"); topLevel2.DumpCodeToConsole(); Console.WriteLine("After optimization (merge):"); topLevel1.DumpCodeToConsole(); // Nothing should have been touched in f1 - double check. Assert.AreEqual(2, f1.Statements.Count()); }