예제 #1
0
        public void TestExprNoStatements()
        {
            var val = new Variables.ValSimple("true", typeof(bool));
            var statement = new StatementFilter(val);

            var result = statement.CodeItUp().ToArray();
            Assert.AreEqual(0, result.Length, "no statements, so wasn't expecting any sort of output at all");
        }
예제 #2
0
        public void TestFalseExpr()
        {
            var val = new Variables.ValSimple("false", typeof(bool));
            var statement = new StatementFilter(val);
            statement.Add(new StatementSimpleStatement("dude"));

            var result = statement.CodeItUp().ToArray();
            Assert.AreEqual(0, result.Length, "Expect no statements for a false if statement");
        }
예제 #3
0
        public IEnumerable <Statement> GetAll(int PageNumber, int ItemsPerPage, StatementFilter Filters)
        {
            IQueryable <Statement> Statements = _context.Statements;

            Statements = ApplyFilters(Statements, Filters);

            Statements = Statements.OrderBy(s => s.Id)
                         .Skip((--PageNumber) * ItemsPerPage)
                         .Take(ItemsPerPage);

            return(Statements.ToList());
        }
예제 #4
0
        public void TestFirstBookingNextLevelIt()
        {
            var i   = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var gc  = new GeneratedCode();
            var ifs = new StatementFilter(new ValSimple("dude", typeof(bool)));

            gc.Add(ifs);
            gc.Add(i);
            var r = gc.FirstAllInScopeFromNow(new IDeclaredParameter[] { i });

            Assert.AreEqual(r, ifs, "Same code body.");
        }
예제 #5
0
        public void TestTrueExpr()
        {
            var val = new Variables.ValSimple("true", typeof(bool));
            var statement = new StatementFilter(val);
            statement.Add(new StatementSimpleStatement("dude"));

            var result = statement.CodeItUp().ToArray();
            Assert.AreEqual(3, result.Length, "true test means only the involved statements should be in here!");
            Assert.AreEqual("{", result[0], "open bracket");
            Assert.AreEqual("  dude;", result[1], "statement isn't in the proper spot");
            Assert.AreEqual("}", result[2], "end of block not right");
        }
예제 #6
0
        public IHttpActionResult GetAll(int PageNumber, int ItemsPerPage, [FromBody] StatementFilter Filters)
        {
            var Statements = _unitOfWork.StatementRepository.GetAll(PageNumber, ItemsPerPage, Filters);

            IEnumerable <StatementDTO> Data = Mapper.Map <IEnumerable <Statement>, IEnumerable <StatementDTO> >(Statements);
            int ItemsNumber = _unitOfWork.StatementRepository.Count(Filters);

            _unitOfWork.Dispose();

            return(Ok(new StatementsViewModel {
                ItemsNumber = ItemsNumber, Data = Data
            }));
        }
예제 #7
0
        public void TestExprWithStatement()
        {
            var val = new Variables.ValSimple("1 == 1", typeof(bool));
            var statement = new StatementFilter(val);
            statement.Add(new StatementSimpleStatement("dude"));

            var result = statement.CodeItUp().ToArray();
            Assert.AreEqual(4, result.Length, "no statements, so wasn't expecting any sort of output at all");
            Assert.AreEqual("if (1 == 1)", result[0], "if statement isn't correct");
            Assert.AreEqual("{", result[1], "open bracket");
            Assert.AreEqual("  dude;", result[2], "statement isn't in the proper spot");
            Assert.AreEqual("}", result[3], "end of block not right");
        }
예제 #8
0
        public void TestSimpleCombine()
        {
            var val1 = new Variables.ValSimple("true", typeof(bool));
            var s1 = new StatementFilter(val1);
            s1.Add(new StatementSimpleStatement("var1"));

            var val2 = new Variables.ValSimple("true", typeof(bool));
            var s2 = new StatementFilter(val2);
            s2.Add(new StatementSimpleStatement("var2"));

            Assert.IsTrue(s1.TryCombineStatement(s2, null), "statement should have combined");
            Assert.AreEqual(2, s1.Statements.Count(), "# of combined statements");
        }
예제 #9
0
        public void FilterStatementByDeposit()
        {
            var expected = new StringBuilder();

            expected.AppendLine(Header);
            expected.AppendLine("24/11/2019   200.00    200.00");
            expected.AppendLine("24/11/2019   100.00    300.00");

            sut.Deposit(new Money(200));
            sut.Deposit(new Money(100));
            sut.Deposit(new Money(9));
            sut.PrintStatement(StatementFilter.DepositedMoreThan(new Money(10)));
            var actual = sut.Printer.GetStringBuilder();

            Assert.Equal(expected.ToString(), actual.ToString());
        }
예제 #10
0
        public void FilterStatementByWithdrawAmount()
        {
            var expected = new StringBuilder();

            expected.AppendLine(Header);
            expected.AppendLine("24/11/2019   -20.00     80.00");
            expected.AppendLine("24/11/2019   -40.00     40.00");

            sut.Deposit(new Money(100));
            sut.Withdraw(new Money(20));
            sut.Withdraw(new Money(40));
            sut.Withdraw(new Money(10));
            sut.PrintStatement(StatementFilter.WithdrawnMoreThan(new Money(10)));
            var actual = sut.Printer.GetStringBuilder();

            Assert.Equal(expected.ToString(), actual.ToString());
        }
예제 #11
0
        public int Count(StatementFilter Filters)
        {
            if (Filters == null)
            {
                return(0);
            }

            IQueryable <Statement> Statements = _context.Statements;

            if (!string.IsNullOrWhiteSpace(Filters.Title))
            {
                Statements = Statements.Where(s => s.Title.StartsWith(Filters.Title) ||
                                              s.Title.Contains(Filters.Title) ||
                                              s.Title.EndsWith(Filters.Title));
            }

            return(Statements.Count());
        }
예제 #12
0
        public void FilterStatementByDate()
        {
            var expected = new StringBuilder();

            expected.AppendLine(Header);
            expected.AppendLine("24/11/2019   110.00    110.00");
            expected.AppendLine("24/11/2019   -20.00     90.00");

            sut.Deposit(new Money(110));
            sut.Withdraw(new Money(20));
            DateTime xMas = DateTime.Parse("25/12/2019");

            sut.Clock.Change(xMas);
            sut.Withdraw(new Money(50));
            sut.PrintStatement(StatementFilter.Before(xMas));

            var actual = sut.Printer.GetStringBuilder();

            Assert.Equal(expected.ToString(), actual.ToString());
        }
예제 #13
0
 public void TestFirstBookingNextLevelIt()
 {
     var i = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
     var gc = new GeneratedCode();
     var ifs = new StatementFilter(new ValSimple("dude", typeof(bool)));
     gc.Add(ifs);
     gc.Add(i);
     var r = gc.FirstAllInScopeFromNow(new IDeclaredParameter[] { i });
     Assert.AreEqual(r, ifs, "Same code body.");
 }
예제 #14
0
        private IQueryable <Statement> ApplyFilters(IQueryable <Statement> Statements, StatementFilter Filters)
        {
            if (Filters == null)
            {
                return(Statements);
            }

            if (!string.IsNullOrWhiteSpace(Filters.Title))
            {
                Statements = Statements.Where(s => s.Title.StartsWith(Filters.Title) ||
                                              s.Title.Contains(Filters.Title) ||
                                              s.Title.EndsWith(Filters.Title));
            }

            return(Statements);
        }
예제 #15
0
        public void TestSecondLevelCombine()
        {
            var val1 = new Variables.ValSimple("true", typeof(bool));
            var s1 = new StatementFilter(val1);

            var val11 = new Variables.ValSimple("true", typeof(bool));
            var s11 = new StatementFilter(val11);
            s11.Add(new StatementSimpleStatement("var11"));

            s1.Add(s11);

            var val2 = new Variables.ValSimple("true", typeof(bool));
            var s2 = new StatementFilter(val2);

            var val21 = new Variables.ValSimple("true", typeof(bool));
            var s21 = new StatementFilter(val21);
            s21.Add(new StatementSimpleStatement("var21"));

            s2.Add(s21);

            Assert.IsTrue(s1.TryCombineStatement(s2, null), "statement should have combined");
            Assert.AreEqual(1, s1.Statements.Count(), "# of combined statements");
            var deep = s1.Statements.First() as StatementInlineBlockBase;
            Assert.IsNotNull(deep, "couldn't find interior statement");
            Assert.AreEqual(2, deep.Statements.Count(), "Number of statements isn't right here");
        }
예제 #16
0
        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 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));
        }
예제 #18
0
        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();
        }
예제 #19
0
        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());
        }
예제 #20
0
        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());
        }
예제 #21
0
        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 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));
        }
예제 #23
0
        public void CombineFilterWithHiddenBehindIf()
        {
            // 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 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(p);
            f2.Add(p);
            f1.Add(a1);
            f2.Add(a2);

            filterUnique.Add(f1);

            Assert.IsTrue(f2.TryCombineStatement(f1, null), "Two of the same if statements, but the target is at a higher level than the merge");
            Assert.AreEqual(1, f2.Statements.Count());
        }
        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.");
        }
예제 #25
0
        public void CombineFilterAtSameLevelWithDifferentStatements()
        {
            // Two if statements with same "if", at the same level, and combine second with first.

            // 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("15", typeof(int)));
            f1.Add(a1);
            f1.Add(p1);
            f2.Add(a2);
            f2.Add(p2);


            Console.WriteLine("Before optimization:");
            foreach (var l in f1.CodeItUp())
            {
                Console.WriteLine(l);
            }

            Assert.IsTrue(f1.TryCombineStatement(f2, null), "Two of the same if statements, and the combine should have worked");

            Console.WriteLine("After optimization:");
            foreach (var l in f1.CodeItUp())
            {
                Console.WriteLine(l);
            }
            Assert.AreEqual(2, f1.Statements.Count());
        }