Exemple #1
0
        public void CheckArrayReturnType()
        {
            var target = new TypeHandlerCPPCode();
            var gc     = new GeneratedCode();

            var param = Expression.Parameter(typeof(int), "p");
            var expr  = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), param);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.IsNotNull(result, "result!");
            var vname = result.RawValue;

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements that came back");
            var st1 = gc.CodeBody.Statements.First() as IStatement;

            var expected = new StringBuilder();

            expected.AppendFormat("{0} = p*2;", vname);
            Assert.AreEqual(expected.ToString(), st1.CodeItUp().First(), "statement line incorrect");

            Assert.AreEqual(1, gc.IncludeFiles.Count(), "# of include files");
            Assert.AreEqual("TLorentzVector.h", gc.IncludeFiles.First(), "include file name");
        }
Exemple #2
0
        public void CPPRenameVariables()
        {
            // two identical expressions
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E    = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            // Create call
            var e1      = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_1, p_eta, p_phi, p_E);
            var e1Value = target.CodeMethodCall(e1, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            // Now, extract the main statement.
            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements");
            var s1 = gc.CodeBody.Statements.First() as IStatement;

            // Make sure the variable is there and then isn't.
            var p_pt_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            Assert.IsTrue(gc.DumpCode().Where(l => l.Contains(p_pt_1.RawValue)).Any(), "the pt variable should be there.");
            Assert.IsFalse(gc.DumpCode().Where(l => l.Contains(p_pt_2.RawValue)).Any(), "the pt variable should be there.");

            s1.RenameVariable(p_pt_1.RawValue, p_pt_2.RawValue);
            Assert.IsFalse(gc.DumpCode().Where(l => l.Contains(p_pt_1.RawValue)).Any(), "the pt variable should be there.");
            Assert.IsTrue(gc.DumpCode().Where(l => l.Contains(p_pt_2.RawValue)).Any(), "the pt variable should be there.");
        }
Exemple #3
0
        public void TestCMVariables()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi = p_pt;
            var p_E   = p_eta;
            var expr  = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZ"), p_pt, p_eta, p_phi, p_E);

            var r = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var ccpstatement = gc.CodeBody.Statements.First();

            Assert.IsNotNull(ccpstatement);
            var cmInfo = ccpstatement as ICMStatementInfo;

            Assert.IsNotNull(cmInfo);
            Assert.AreEqual(1, cmInfo.ResultVariables.Count(), "# of result variables");
            Assert.AreEqual(r.RawValue, cmInfo.ResultVariables.First(), "Result variable name");
            Assert.AreEqual(2, cmInfo.DependentVariables.Count(), "# of dependent variables");
            Assert.IsTrue(cmInfo.DependentVariables.Contains(p_pt.RawValue), "doesn't have pt");
            Assert.IsTrue(cmInfo.DependentVariables.Contains(p_eta.RawValue), "Doesn't have eta");
        }
Exemple #4
0
        public void CPPNotIdentical()
        {
            // two identical expressions
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E   = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            // Create two identical calls
            var e1      = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt, p_eta, p_phi, p_E);
            var e1Value = target.CodeMethodCall(e1, gc, MEFUtilities.MEFContainer);

            var c2      = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var e2      = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), c2);
            var e2Value = target.CodeMethodCall(e2, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            // Now, extract the two main statements.
            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            var s1 = gc.CodeBody.Statements.First() as ICMStatementInfo;
            var s2 = gc.CodeBody.Statements.Skip(1).First() as ICMStatementInfo;

            // Now, see if we can do the requirement.
            var r = s1.RequiredForEquivalence(s2);

            Assert.IsFalse(r.Item1, "We should be able to do the translation");
        }
Exemple #5
0
        public void TestSimpleAddition()
        {
            MEFUtilities.Compose(new TypeHandlers.TypeHandlerCache());
            AggregateFromSeedResultOperator agg = new AggregateFromSeedResultOperator(Expression.Constant(1),
                                                                                      Expression.Lambda(Expression.MakeBinary(ExpressionType.Add, Expression.Parameter(typeof(int), "count"), Expression.Constant(1)),
                                                                                                        Expression.Parameter(typeof(int), "count")),
                                                                                      null);

            ROAggregate   processor = new ROAggregate();
            GeneratedCode gc        = new GeneratedCode();
            var           result    = ProcessResultOperator(processor, agg, null, gc);

            Assert.AreEqual(typeof(int), result.Type, "Expected the type to be an integer!");

            Assert.IsInstanceOfType(result, typeof(DeclarableParameter), "Expected a var simple!");
            var vs = result as DeclarableParameter;

            Assert.AreEqual("1", vs.InitialValue.RawValue, "Incorrect seed value");

            ///
            /// Now make sure the statements came back ok!
            ///

            gc.DumpCodeToConsole();
            Assert.AreEqual(0, gc.CodeBody.DeclaredVariables.Count(), "Expected no bookings");
            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "expected a statement!");
            Assert.IsInstanceOfType(gc.CodeBody.Statements.First(), typeof(Statements.StatementAggregate), "expected an assignment statement!");

            var           ass = gc.CodeBody.Statements.First() as Statements.StatementAggregate;
            StringBuilder bld = new StringBuilder();

            bld.AppendFormat("{0}+1", ass.ResultVariable.ParameterName);
            Assert.AreEqual(bld.ToString(), ass.Expression.RawValue, "the raw value of hte expression is not right");
        }
Exemple #6
0
        public void TestBasicProcessNew()
        {
            /// Test a very simple process new

            var    createTLZ = Expression.New(typeof(ROOTNET.NTLorentzVector).GetConstructor(new Type[0]));
            var    target    = new TypeHandlerROOT();
            IValue resultOfCall;
            var    gc   = new GeneratedCode();
            var    expr = target.ProcessNew(createTLZ, out resultOfCall, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.AreEqual(createTLZ.ToString(), expr.ToString(), "Returned expression");
            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of coded statements");
            var s1 = gc.CodeBody.Statements.First();
            var s2 = gc.CodeBody.Statements.Skip(1).First();

            Assert.IsInstanceOfType(s1, typeof(Statements.StatementSimpleStatement), "s1 type");
            Assert.IsInstanceOfType(s2, typeof(Statements.StatementSimpleStatement), "s1 type");
            var s1s = s1 as Statements.StatementSimpleStatement;
            var s2s = s2 as Statements.StatementSimpleStatement;

            Assert.IsTrue(s1s.Line.Contains("TLorentzVector"), "first line is not that good");
            Assert.IsTrue(s2s.Line.Contains("TLorentzVector *"), "second line is not that good");
        }
Exemple #7
0
        public void CPPTryCombineNotSame()
        {
            // two identical expressions
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E    = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            // Create two identical calls
            var e1      = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_1, p_eta, p_phi, p_E);
            var e1Value = target.CodeMethodCall(e1, gc, MEFUtilities.MEFContainer);

            var p_pt_2  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var e2      = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_2, p_eta, p_phi, p_E);
            var e2Value = target.CodeMethodCall(e2, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            // Now, extract the two main statements.
            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            var s1 = gc.CodeBody.Statements.First() as IStatement;
            var s2 = gc.CodeBody.Statements.Skip(1).First() as IStatement;

            // Now, try-combine should just "work", as it were.
            var opt = new OptTest();
            var r   = s1.TryCombineStatement(s2, opt);

            Assert.IsFalse(r);
        }
Exemple #8
0
        public void TestSimpleCodeAddon()
        {
            // See problems referencing return variables
            var target = new TypeHandlerCPPCode();
            var gc     = new GeneratedCode();

            var param = Expression.Parameter(typeof(int), "p");
            var expr  = Expression.Call(typeof(DoItClass).GetMethod("DoItWithArray"), param);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.AreEqual(typeof(float[]), result.Type);
        }
Exemple #9
0
        /// <summary>
        /// Do the work of executing the array parse
        /// </summary>
        /// <param name="e"></param>
        private static GeneratedCode ExecuteArrayParseOnExpression(Expression e)
        {
            var          gc = new GeneratedCode();
            var          cc = new CodeContext();
            IQuerySource s  = new DummyQueryReference()
            {
                ItemName = "q", ItemType = typeof(int)
            };
            var r = ArrayExpressionParser.ParseArrayExpression(s, e, gc, cc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();
            Assert.IsNotNull(cc.LoopVariable, "loop variable");

            return(gc);
        }
Exemple #10
0
        public void TestQueueTwice()
        {
            var t = new TypeHandlerROOT();

            var origRootObj = new ROOTNET.NTH1F("hi", "there", 10, 10.0, 20.0);
            var rootObj     = Expression.Constant(origRootObj);

            var gc      = new GeneratedCode();
            var result1 = t.ProcessConstantReference(rootObj, gc, MEFUtilities.MEFContainer);
            var result2 = t.ProcessConstantReference(rootObj, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.AreEqual(1, gc.VariablesToTransfer.Count(), "Variables to transfer");

            Assert.AreEqual(result1.RawValue, result2.RawValue, "Expected the same raw value for the same object in the same expression");
        }
Exemple #11
0
        public void RenameCPPResultVariable()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var param     = Expression.Parameter(typeof(int), "p");
            var paramplus = Expression.MakeBinary(ExpressionType.Add, param, Expression.Constant(1));
            var expr      = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), paramplus);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.CodeBody.RenameVariable(result.RawValue, "abogus_1234");

            gc.DumpCodeToConsole();

            Assert.IsTrue(gc.DumpCode().Where(s => s.Contains("int abogus_1234")).Any(), "Didn't find the variable name in the code");
        }
Exemple #12
0
        public void TestScopingBlock()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt  = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E   = Expression.Parameter(typeof(double), "EParam");
            var expr  = Expression.Call(typeof(TLZHelper).GetMethod("TestIF"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var ifstatement = gc.CodeBody.Statements.First().CodeItUp().First();

            Assert.IsFalse(ifstatement.EndsWith(";"), string.Format("Line '{0}' ends with a semicolon", ifstatement));
        }
Exemple #13
0
        public void TestReturnVariableDecl()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt  = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E   = Expression.Parameter(typeof(double), "EParam");
            var expr  = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZ"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var declStatement = gc.CodeBody.DeclaredVariables.ToArray();

            Assert.AreEqual(1, declStatement.Length);
        }
Exemple #14
0
        public void TestComplexArgumentReplacement()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt  = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E   = Expression.Parameter(typeof(double), "EParam");
            var expr  = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZ"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements total");
            var setStatement = gc.CodeBody.Statements.First().CodeItUp().Skip(1).First();

            Assert.IsNotNull(setStatement, "Bad type for 3rd statement");
            Assert.IsTrue(setStatement.Contains("SetPtEtaPhiE(ptParam, etaParam, phiParam, EParam)"), string.Format("Line '{0}' doesn't have correct set statement.", setStatement));
        }
Exemple #15
0
        public void TestArgReplacementAtStartAndEnd()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt  = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E   = Expression.Parameter(typeof(double), "EParam");
            var expr  = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements total");
            var atEnding = gc.CodeBody.Statements.First().CodeItUp().Skip(1).FirstOrDefault();

            Assert.IsNotNull(atEnding, "Bad type for 3rd statement");
            Assert.IsTrue(atEnding.EndsWith("ptParam"), string.Format("Line '{0}' doesn't ends with parameter replacement", atEnding));
        }
Exemple #16
0
        public void RenameCPPInputVariableVariable()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var param     = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var paramplus = Expression.MakeBinary(ExpressionType.Add, param, Expression.Constant(1));
            var expr      = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), paramplus);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            // Check the dependents.
            gc.CodeBody.RenameVariable(param.RawValue, "abogus_1234");

            gc.DumpCodeToConsole();

            var st = gc.CodeBody.Statements.First() as ICMStatementInfo;

            Assert.AreEqual(1, st.DependentVariables.Count(), "# of dependents");
            Assert.AreEqual("abogus_1234", st.DependentVariables.First());
        }
Exemple #17
0
        public void TestForUniqueReplacement()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt  = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E   = Expression.Parameter(typeof(double), "EParam");
            var expr  = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZ"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var declStatement = gc.CodeBody.Statements.First().CodeItUp().Skip(1).First();
            var setStatement  = gc.CodeBody.Statements.First().CodeItUp().Skip(2).First();

            Assert.IsFalse(declStatement.Contains("Unique"), string.Format("Line '{0}' contains a reference to a unique variable", declStatement));
            Assert.IsFalse(setStatement.Contains("Unique"), string.Format("Line '{0}' contains a reference to a unique variable", setStatement));
        }
Exemple #18
0
        public void TestSimpleTimesTwo()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var param     = Expression.Parameter(typeof(int), "p");
            var paramplus = Expression.MakeBinary(ExpressionType.Add, param, Expression.Constant(1));
            var expr      = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), paramplus);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var vname = result.RawValue;
            var st2   = gc.CodeBody.Statements.First().CodeItUp().First();

            var expected = new StringBuilder();

            expected.AppendFormat("{0} = (p+1)*2;", vname);
            Assert.AreEqual(expected.ToString(), st2, "statement line incorrect");
        }
Exemple #19
0
        public void TestTwoUniqueReplacements()
        {
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt  = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E   = Expression.Parameter(typeof(double), "EParam");
            var expr  = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZUniqueTest"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            foreach (var line in gc.CodeBody.Statements.First().CodeItUp())
            {
                Assert.IsNotNull(line, "bad statement type");
                Assert.IsFalse(line.Contains("Unique"), string.Format("Line '{0}' contains a reference to a unique variable", line));
            }
        }
Exemple #20
0
        public void CPPNeedSomeReplacements()
        {
            // two identical expressions
            var target  = new TypeHandlerCPPCode();
            var gc      = new GeneratedCode();
            var context = new CodeContext();

            var p_pt_1  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E_1   = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            // Create first call
            var e1      = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_1, p_eta_1, p_phi_1, p_E_1);
            var e1Value = target.CodeMethodCall(e1, gc, MEFUtilities.MEFContainer);

            var p_pt_2  = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E_2   = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            var e2      = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_2, p_eta_2, p_phi_2, p_E_2);
            var e2Value = target.CodeMethodCall(e2, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            // Now, extract the two main statements.
            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            var s1 = gc.CodeBody.Statements.First() as ICMStatementInfo;
            var s2 = gc.CodeBody.Statements.Skip(1).First() as ICMStatementInfo;

            // Now, see if we can do the requirement.
            var renames = new Tuple <string, string>[] { new Tuple <string, string>(p_pt_2.RawValue, p_pt_1.RawValue), new Tuple <string, string>(p_eta_2.RawValue, p_eta_1.RawValue) };
            var r       = s1.RequiredForEquivalence(s2, renames);

            Assert.IsTrue(r.Item1, "We should be able to do the translation");
            Assert.AreEqual(3, r.Item2.Count(), "# of variable translations required");
        }
Exemple #21
0
        public void CodeAddonWithStringArgument()
        {
            var target = new TypeHandlerCPPCode();
            var gc     = new GeneratedCode();

            var param = Expression.Parameter(typeof(string), "p");
            var expr  = Expression.Call(typeof(DoItClass).GetMethod("DoItWithAString"), param);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.IsNotNull(result, "result!");
            var vname = result.RawValue;

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements that came back");
            var st1 = gc.CodeBody.Statements.First() as IStatement;

            var expected = new StringBuilder();

            expected.AppendFormat("{0} = strlen(p);", vname);
            Assert.AreEqual(expected.ToString(), st1.CodeItUp().First(), "statement line incorrect");
        }
        public void TestSimpleCodeAddon()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();

            var param = Expression.Parameter(typeof(int), "p");
            var expr = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), param);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.IsNotNull(result, "result!");
            var vname = result.RawValue;

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements that came back");
            var st1 = gc.CodeBody.Statements.First() as IStatement;

            var expected = new StringBuilder();
            expected.AppendFormat("{0} = p*2;", vname);
            Assert.AreEqual(expected.ToString(), st1.CodeItUp().First(), "statement line incorrect");

            Assert.AreEqual(1, gc.IncludeFiles.Count(), "# of include files");
            Assert.AreEqual("TLorentzVector.h", gc.IncludeFiles.First(), "include file name");
        }
        public void CPPRenameVariables()
        {
            // two identical expressions
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            // Create call
            var e1 = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_1, p_eta, p_phi, p_E);
            var e1Value = target.CodeMethodCall(e1, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            // Now, extract the main statement.
            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements");
            var s1 = gc.CodeBody.Statements.First() as IStatement;

            // Make sure the variable is there and then isn't.
            var p_pt_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            Assert.IsTrue(gc.DumpCode().Where(l => l.Contains(p_pt_1.RawValue)).Any(), "the pt variable should be there.");
            Assert.IsFalse(gc.DumpCode().Where(l => l.Contains(p_pt_2.RawValue)).Any(), "the pt variable should be there.");

            s1.RenameVariable(p_pt_1.RawValue, p_pt_2.RawValue);
            Assert.IsFalse(gc.DumpCode().Where(l => l.Contains(p_pt_1.RawValue)).Any(), "the pt variable should be there.");
            Assert.IsTrue(gc.DumpCode().Where(l => l.Contains(p_pt_2.RawValue)).Any(), "the pt variable should be there.");
        }
        public void CPPTryCombineNotSame()
        {
            // two identical expressions
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            // Create two identical calls
            var e1 = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_1, p_eta, p_phi, p_E);
            var e1Value = target.CodeMethodCall(e1, gc, MEFUtilities.MEFContainer);

            var p_pt_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var e2 = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_2, p_eta, p_phi, p_E);
            var e2Value = target.CodeMethodCall(e2, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            // Now, extract the two main statements.
            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            var s1 = gc.CodeBody.Statements.First() as IStatement;
            var s2 = gc.CodeBody.Statements.Skip(1).First() as IStatement;

            // Now, try-combine should just "work", as it were.
            var opt = new OptTest();
            var r = s1.TryCombineStatement(s2, opt);
            Assert.IsFalse(r);
        }
        public void CPPNeedSomeReplacements()
        {
            // two identical expressions
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E_1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            // Create first call
            var e1 = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_1, p_eta_1, p_phi_1, p_E_1);
            var e1Value = target.CodeMethodCall(e1, gc, MEFUtilities.MEFContainer);

            var p_pt_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E_2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            var e2 = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt_2, p_eta_2, p_phi_2, p_E_2);
            var e2Value = target.CodeMethodCall(e2, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            // Now, extract the two main statements.
            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            var s1 = gc.CodeBody.Statements.First() as ICMStatementInfo;
            var s2 = gc.CodeBody.Statements.Skip(1).First() as ICMStatementInfo;

            // Now, see if we can do the requirement.
            var renames = new Tuple<string, string>[] { new Tuple<string, string>(p_pt_2.RawValue, p_pt_1.RawValue), new Tuple<string, string>(p_eta_2.RawValue, p_eta_1.RawValue) };
            var r = s1.RequiredForEquivalence(s2, renames);
            Assert.IsTrue(r.Item1, "We should be able to do the translation");
            Assert.AreEqual(3, r.Item2.Count(), "# of variable translations required");
        }
        public void NoLiftAlmostIdenticalLoopOutOfIfStatement()
        {
            var gc = new GeneratedCode();
            var c1 = AddLoop(gc);
            gc.Pop();
            AddIf(gc);
            var c2 = AddLoop(gc, addDependentStatement: true);
            gc.Pop();
            AddSum(gc, c1, c2);

            Console.WriteLine("Before lifting and optimization: ");
            gc.DumpCodeToConsole();

            StatementLifter.Optimize(gc);

            Console.WriteLine("After lifting and optimization: ");
            gc.DumpCodeToConsole();

            // Now check that things happened as we would expect them to happen.
            Assert.AreEqual(1, gc.CodeBody.Statements.Where(s => s is StatementForLoop).Count(), "# of for loops at outer level");
            Assert.AreEqual(1, gc.CodeBody.Statements.Where(s => s is StatementForLoop).Cast<StatementForLoop>().Where(s => s.Statements.Count() == 1).Count(), "# of statements inside first for loop");

            var ifStatement = gc.CodeBody.Statements.Where(s => s is StatementFilter).Cast<StatementFilter>().First();
            Assert.IsNotNull(ifStatement, "Finding if statement");
            Assert.AreEqual(2, ifStatement.Statements.Count(), "# of statements inside the if statement");
        }
        public void TestSimpleTimesTwo()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var param = Expression.Parameter(typeof(int), "p");
            var paramplus = Expression.MakeBinary(ExpressionType.Add, param, Expression.Constant(1));
            var expr = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), paramplus);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var vname = result.RawValue;
            var st2 = gc.CodeBody.Statements.First().CodeItUp().First();

            var expected = new StringBuilder();
            expected.AppendFormat("{0} = (p+1)*2;", vname);
            Assert.AreEqual(expected.ToString(), st2, "statement line incorrect");
        }
        public void DontCombineDependentLoops()
        {
            var gc = new GeneratedCode();
            var c1 = AddLoop(gc);
            gc.Pop();
            var c2 = AddLoop(gc, useCounter: c1);
            gc.Pop();

            Console.WriteLine("Before lifting");
            gc.DumpCodeToConsole();

            StatementLifter.Optimize(gc);

            Console.WriteLine("After lifting");
            gc.DumpCodeToConsole();

            Assert.AreEqual(2, gc.CodeBody.Statements.Where(s => s is StatementForLoop).Count(), "# of for loops");
        }
        public void TestTwoUniqueReplacements()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E = Expression.Parameter(typeof(double), "EParam");
            var expr = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZUniqueTest"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            foreach (var line in gc.CodeBody.Statements.First().CodeItUp())
            {
                Assert.IsNotNull(line, "bad statement type");
                Assert.IsFalse(line.Contains("Unique"), string.Format("Line '{0}' contains a reference to a unique variable", line));
            }
        }
        public void TestReturnVariableDecl()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E = Expression.Parameter(typeof(double), "EParam");
            var expr = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZ"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var declStatement = gc.CodeBody.DeclaredVariables.ToArray();
            Assert.AreEqual(1, declStatement.Length);
        }
        public void TestForUniqueReplacement()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E = Expression.Parameter(typeof(double), "EParam");
            var expr = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZ"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var declStatement = gc.CodeBody.Statements.First().CodeItUp().Skip(1).First();
            var setStatement = gc.CodeBody.Statements.First().CodeItUp().Skip(2).First();

            Assert.IsFalse(declStatement.Contains("Unique"), string.Format("Line '{0}' contains a reference to a unique variable", declStatement));
            Assert.IsFalse(setStatement.Contains("Unique"), string.Format("Line '{0}' contains a reference to a unique variable", setStatement));
        }
        public void RenameCPPInputVariableVariable()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var param = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var paramplus = Expression.MakeBinary(ExpressionType.Add, param, Expression.Constant(1));
            var expr = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), paramplus);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            // Check the dependents.
            gc.CodeBody.RenameVariable(param.RawValue, "abogus_1234");

            gc.DumpCodeToConsole();

            var st = gc.CodeBody.Statements.First() as ICMStatementInfo;
            Assert.AreEqual(1, st.DependentVariables.Count(), "# of dependents");
            Assert.AreEqual("abogus_1234", st.DependentVariables.First());
        }
        public void RenameCPPResultVariable()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var param = Expression.Parameter(typeof(int), "p");
            var paramplus = Expression.MakeBinary(ExpressionType.Add, param, Expression.Constant(1));
            var expr = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), paramplus);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);
            gc.CodeBody.RenameVariable(result.RawValue, "abogus_1234");

            gc.DumpCodeToConsole();

            Assert.IsTrue(gc.DumpCode().Where(s => s.Contains("int abogus_1234")).Any(), "Didn't find the variable name in the code");
        }
        /// <summary>Test stub for ProcessResultOperator(ResultOperatorBase, QueryModel, IGeneratedCode)</summary>
        internal GeneratedCode ProcessResultOperator(
            ROTakeSkipOperators target,
            ResultOperatorBase resultOperator,
            QueryModel queryModel,
            GeneratedCode codeEnv
            )
        {
            if (codeEnv.ResultValue != null)
            {
                throw new ArgumentException("this should not be null for this test");
            }
            if (codeEnv.CodeBody.DeclaredVariables == null)
            {
                throw new ArgumentException("Need this declare variables to be defined");
            }

            ///
            /// We always expect to be inside a loop - and depend on it for doing our declares, so add something...
            ///

            var inlineBlock = new StatementInlineBlock();

            codeEnv.Add(inlineBlock);

            ///
            /// Get the environment setup and run it
            ///

            CodeContext c = new CodeContext();

            c.SetLoopVariable(Expression.Variable(typeof(int), "d"), null);

            target.ProcessResultOperator(resultOperator, queryModel, codeEnv, c, MEFUtilities.MEFContainer);

            codeEnv.DumpCodeToConsole();

            ///
            /// First, there should be a counter now declared and ready to go in the current variable block - which will
            /// be the outer one for this test. If this is the outter most, then this is going to be burried.
            ///

            var declBlock = inlineBlock.Parent.Parent as IBookingStatementBlock;

            Assert.IsNotNull(declBlock, "Expecting a declaration block above!");

            Assert.AreEqual(1, inlineBlock.Statements.Count(), "Expected an if block/increment!");
            Assert.IsInstanceOfType(inlineBlock.Statements.First(), typeof(StatementIfOnCount), "if statement not found!");

            var s = inlineBlock.Statements.First() as StatementIfOnCount;

            bool isTopLevel = codeEnv.DumpCode().Where(l => l.Contains("static int")).Any();

            if (!isTopLevel)
            {
                Assert.AreEqual(1, declBlock.DeclaredVariables.Count(), "Expected only 1 variable to be declared");
                Assert.IsInstanceOfType(declBlock.DeclaredVariables.First(), typeof(DeclarableParameter), "Expected it to be a counter");
            }
            else
            {
                Assert.AreEqual(1, (s.Parent as IBookingStatementBlock).DeclaredVariables.Count());
            }

            string count = "";

            if (resultOperator is SkipResultOperator)
            {
                count = (resultOperator as SkipResultOperator).Count.ToString();
            }
            else if (resultOperator is TakeResultOperator)
            {
                count = (resultOperator as TakeResultOperator).Count.ToString();
            }
            Assert.AreEqual(count, s.Limit.RawValue, "bad count made it through");

            ///
            /// Finally, the current loop variable should be identical, and there should be no result set.
            ///

            Assert.IsNull(codeEnv.ResultValue, "result value");
            Assert.IsInstanceOfType(c.LoopVariable, typeof(ParameterExpression), "loop variable type");
            var lv = c.LoopVariable as ParameterExpression;

            Assert.AreEqual("d", lv.Name, "loop variable name");

            //
            // Dump everything and return. To force it out, add a dummy statement
            // (because if statements, etc., are smart enough to not print anything if they
            // are empty).
            //

            codeEnv.Add(new StatementSimpleStatement("fork = left"));
            codeEnv.DumpCodeToConsole();

            return(codeEnv);
        }
        private static void DoOptimizeTest(GeneratedCode v)
        {
            Console.WriteLine("Before Optimization:");
            Console.WriteLine("");
            v.DumpCodeToConsole();

            StatementLifter.Optimize(v);

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("After Optimization:");
            Console.WriteLine("");
            v.DumpCodeToConsole();
        }
        public void CombineRepeatedLoops()
        {
            var gc = new GeneratedCode();
            var c1 = AddLoop(gc);
            gc.Pop();
            var c2 = AddLoop(gc);
            gc.Pop();
            AddSum(gc, c1, c2);

            Console.WriteLine("Before lifting and optimization: ");
            gc.DumpCodeToConsole();

            StatementLifter.Optimize(gc);

            Console.WriteLine("After lifting and optimization: ");
            gc.DumpCodeToConsole();

            // Now check that things happened as we would expect them to happen.
            var ass = gc.CodeBody.Statements.Where(s => s is StatementAssign).Cast<StatementAssign>().First();
            Assert.IsNotNull(ass, "Finding the assignment statement");
            Assert.AreEqual($"{c1.RawValue}+{c1.RawValue}", ass.Expression.RawValue);
            Assert.AreEqual(1, gc.CodeBody.DeclaredVariables.Where(dv => dv.RawValue == c1.RawValue).Count(), "# of declared counters");
            Assert.AreEqual(1, gc.CodeBody.Statements.Where(s => s is StatementForLoop).Count(), "# of for loops");
            Assert.AreEqual(1, gc.CodeBody.Statements.WhereCast<IStatement, StatementForLoop>().Where(s => s.Statements.Count() == 1).Count(), "# of it statements with 2 sub-statements");
        }
        /// <summary>Test stub for ProcessResultOperator(ResultOperatorBase, QueryModel, IGeneratedCode)</summary>
        internal GeneratedCode ProcessResultOperator(
            ROTakeSkipOperators target,
            ResultOperatorBase resultOperator,
            QueryModel queryModel,
            GeneratedCode codeEnv
        )
        {
            if (codeEnv.ResultValue != null)
                throw new ArgumentException("this should not be null for this test");
            if (codeEnv.CodeBody.DeclaredVariables == null)
                throw new ArgumentException("Need this declare variables to be defined");

            ///
            /// We always expect to be inside a loop - and depend on it for doing our declares, so add something...
            /// 

            var inlineBlock = new StatementInlineBlock();
            codeEnv.Add(inlineBlock);

            ///
            /// Get the environment setup and run it
            /// 

            CodeContext c = new CodeContext();
            c.SetLoopVariable(Expression.Variable(typeof(int), "d"), null);

            target.ProcessResultOperator(resultOperator, queryModel, codeEnv, c, MEFUtilities.MEFContainer);

            codeEnv.DumpCodeToConsole();

            ///
            /// First, there should be a counter now declared and ready to go in the current variable block - which will
            /// be the outer one for this test. If this is the outter most, then this is going to be burried.
            /// 

            var declBlock = inlineBlock.Parent.Parent as IBookingStatementBlock;
            Assert.IsNotNull(declBlock, "Expecting a declaration block above!");

            Assert.AreEqual(1, inlineBlock.Statements.Count(), "Expected an if block/increment!");
            Assert.IsInstanceOfType(inlineBlock.Statements.First(), typeof(StatementIfOnCount), "if statement not found!");

            var s = inlineBlock.Statements.First() as StatementIfOnCount;

            bool isTopLevel = codeEnv.DumpCode().Where(l => l.Contains("static int")).Any();
            if (!isTopLevel)
            {
                Assert.AreEqual(1, declBlock.DeclaredVariables.Count(), "Expected only 1 variable to be declared");
                Assert.IsInstanceOfType(declBlock.DeclaredVariables.First(), typeof(DeclarableParameter), "Expected it to be a counter");
            } else
            {
                Assert.AreEqual(1, (s.Parent as IBookingStatementBlock).DeclaredVariables.Count());
            }

            string count = "";
            if (resultOperator is SkipResultOperator)
            {
                count = (resultOperator as SkipResultOperator).Count.ToString();
            }
            else if (resultOperator is TakeResultOperator)
            {
                count = (resultOperator as TakeResultOperator).Count.ToString();
            }
            Assert.AreEqual(count, s.Limit.RawValue, "bad count made it through");

            ///
            /// Finally, the current loop variable should be identical, and there should be no result set.
            /// 

            Assert.IsNull(codeEnv.ResultValue, "result value");
            Assert.IsInstanceOfType(c.LoopVariable, typeof(ParameterExpression), "loop variable type");
            var lv = c.LoopVariable as ParameterExpression;
            Assert.AreEqual("d", lv.Name, "loop variable name");

            //
            // Dump everything and return. To force it out, add a dummy statement
            // (because if statements, etc., are smart enough to not print anything if they
            // are empty).
            //

            codeEnv.Add(new StatementSimpleStatement("fork = left"));
            codeEnv.DumpCodeToConsole();

            return codeEnv;
        }
        public void CodeAddonWithStringArgument()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();

            var param = Expression.Parameter(typeof(string), "p");
            var expr = Expression.Call(typeof(DoItClass).GetMethod("DoItWithAString"), param);

            var result = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.IsNotNull(result, "result!");
            var vname = result.RawValue;

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements that came back");
            var st1 = gc.CodeBody.Statements.First() as IStatement;

            var expected = new StringBuilder();
            expected.AppendFormat("{0} = strlen(p);", vname);
            Assert.AreEqual(expected.ToString(), st1.CodeItUp().First(), "statement line incorrect");
        }
        public void TestLiftTwoStatements()
        {
            var v = new GeneratedCode();
            var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int)));
            v.Add(loop);
            v.Add(new StatementWithSideEffects(loopP));
            v.Add(new StatementWithNoSideEffects());
            v.Add(new StatementWithNoSideEffects());

            Console.WriteLine("Before optimization");
            v.DumpCodeToConsole();

            StatementLifter.Optimize(v);
            Console.WriteLine("After optimization");
            v.DumpCodeToConsole();

            var firstStatement = v.CodeBody.Statements.First();
            Assert.IsInstanceOfType(firstStatement, typeof(StatementWithNoSideEffects), "first statement");
            var thirdstatement = v.CodeBody.Statements.Skip(1).First();
            Assert.IsInstanceOfType(thirdstatement, typeof(StatementForLoop), "third statement");
        }
        public void DontLiftThroughTwoForStatements()
        {
            var gc = new GeneratedCode();

            var counter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            counter.InitialValue = new ValSimple("0", typeof(int));
            gc.Add(counter);

            // The two for loops
            var fc1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var for1 = new StatementForLoop(fc1, new ValSimple("5", typeof(int)));
            gc.Add(for1);

            var innerCounter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            innerCounter.InitialValue = new ValSimple("0", typeof(int));
            gc.Add(innerCounter);

            var fc2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var for2 = new StatementForLoop(fc2, new ValSimple("5", typeof(int)));
            gc.Add(for2);

            // Now, calculation based only on fc1
            var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var ass1 = new StatementAssign(a1, new ValSimple($"{fc1}*2", typeof(int), new IDeclaredParameter[] { fc1 }));
            gc.Add(ass1);
            var agg1 = new StatementAggregate(innerCounter, new ValSimple($"{innerCounter.RawValue}+{a1.RawValue}", typeof(int), new IDeclaredParameter[] { innerCounter, a1 }));
            gc.Add(agg1);

            // and the outer sum.
            gc.Pop();
            var agg2 = new StatementAggregate(counter, new ValSimple($"{counter.RawValue}+{innerCounter.RawValue}", typeof(int), new IDeclaredParameter[] { counter, innerCounter }));
            gc.Add(agg2);

            // Great!
            Console.WriteLine("Unoptimized");
            gc.DumpCodeToConsole();

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");

            StatementLifter.Optimize(gc);

            Console.WriteLine("Optimized");
            Console.WriteLine("");
            gc.DumpCodeToConsole();

            // Make sure the inner aggregate got lifted out properly.
            Assert.AreEqual(1, for2.Statements.Count(), "# of statements in the inner for loop");
        }
        public void NoLiftSimpleStatement()
        {
            var gc = new GeneratedCode();
            AddLoop(gc);

            Console.WriteLine("Before optimization");
            gc.DumpCodeToConsole();

            StatementLifter.Optimize(gc);

            Console.WriteLine("After optimization");
            gc.DumpCodeToConsole();

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements");
            Assert.AreEqual(1, gc.CodeBody.Statements.Where(s => s is StatementForLoop).Count(), "# of for loops");
        }
        public void LiftConstantStatementInLoop()
        {
            var gc = new GeneratedCode();
            var counter = AddLoop(gc, mainStatementType: MainStatementType.IsConstant);
            gc.Pop();

            Console.WriteLine("Before optimization");
            gc.DumpCodeToConsole();

            StatementLifter.Optimize(gc);

            Console.WriteLine("After optimization");
            gc.DumpCodeToConsole();

            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            Assert.AreEqual(1, gc.CodeBody.Statements.Where(s => s is StatementAssign).Count(), "# of assign statements");
            Assert.AreEqual(1, gc.CodeBody.Statements.Where(s => s is StatementForLoop).Cast<StatementForLoop>().Where(s => s.Statements.Count() == 0).Count(), "# of if statements with zero statements in it");
        }
        public void LiftNoSideEffectFromNestedIdenticalLoops()
        {
            var v = new GeneratedCode();

            var limit = new LINQToTTreeLib.Variables.ValSimple("10", typeof(int));
            var loopP1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop1 = new StatementForLoop(loopP1, limit);
            v.Add(loop1);
            var loopP2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop2 = new StatementForLoop(loopP2, limit);
            v.Add(loop2);
            v.Add(new StatementWithNoSideEffects());

            Console.WriteLine("Unoptimized:");
            v.DumpCodeToConsole();
            StatementLifter.Optimize(v);
            Console.WriteLine("");
            Console.WriteLine("Optimized:");
            v.DumpCodeToConsole();

            // Check to see if it got lifted.
            Assert.AreEqual(1, v.CodeBody.Statements.WhereCast<IStatement, StatementWithNoSideEffects>().Count(), "#of no side effect statements");
        }
        /// <summary>
        /// Do the work of executing the array parse
        /// </summary>
        /// <param name="e"></param>
        private static GeneratedCode ExecuteArrayParseOnExpression(Expression e)
        {
            var gc = new GeneratedCode();
            var cc = new CodeContext();
            IQuerySource s = new DummyQueryReference() { ItemName = "q", ItemType = typeof(int) };
            var r = ArrayExpressionParser.ParseArrayExpression(s, e, gc, cc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();
            Assert.IsNotNull(cc.LoopVariable, "loop variable");

            return gc;
        }
        public void TestComplexArgumentReplacement()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E = Expression.Parameter(typeof(double), "EParam");
            var expr = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZ"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements total");
            var setStatement = gc.CodeBody.Statements.First().CodeItUp().Skip(1).First();
            Assert.IsNotNull(setStatement, "Bad type for 3rd statement");
            Assert.IsTrue(setStatement.Contains("SetPtEtaPhiE(ptParam, etaParam, phiParam, EParam)"), string.Format("Line '{0}' doesn't have correct set statement.", setStatement));
        }
        public void TestArgReplacementAtStartAndEnd()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E = Expression.Parameter(typeof(double), "EParam");
            var expr = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "# of statements total");
            var atEnding = gc.CodeBody.Statements.First().CodeItUp().Skip(1).FirstOrDefault();
            Assert.IsNotNull(atEnding, "Bad type for 3rd statement");
            Assert.IsTrue(atEnding.EndsWith("ptParam"), string.Format("Line '{0}' doesn't ends with parameter replacement", atEnding));
        }
        public void LiftLoopInvarient()
        {
            var v = new GeneratedCode();

            var limit = new LINQToTTreeLib.Variables.ValSimple("5", typeof(int));
            var loopP1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var loop1 = new StatementForLoop(loopP1, limit);
            v.Add(loop1);

            var p2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var assign1 = new StatementAssign(p2, new ValSimple("f", typeof(int)));
            loop1.Add(p2);
            loop1.Add(assign1);

            Console.WriteLine("Unoptimized:");
            v.DumpCodeToConsole();

            StatementLifter.Optimize(v);

            Console.WriteLine("");
            Console.WriteLine("Optimized:");
            v.DumpCodeToConsole();

            Assert.AreEqual(0, loop1.Statements.Count());
        }
        public void CPPNotIdentical()
        {
            // two identical expressions
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_E = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));

            // Create two identical calls
            var e1 = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZBE"), p_pt, p_eta, p_phi, p_E);
            var e1Value = target.CodeMethodCall(e1, gc, MEFUtilities.MEFContainer);

            var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var e2 = Expression.Call(typeof(DoItClass).GetMethod("DoIt"), c2);
            var e2Value = target.CodeMethodCall(e2, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            // Now, extract the two main statements.
            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            var s1 = gc.CodeBody.Statements.First() as ICMStatementInfo;
            var s2 = gc.CodeBody.Statements.Skip(1).First() as ICMStatementInfo;

            // Now, see if we can do the requirement.
            var r = s1.RequiredForEquivalence(s2);
            Assert.IsFalse(r.Item1, "We should be able to do the translation");
        }
        public void TestCMVariables()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_eta = DeclarableParameter.CreateDeclarableParameterExpression(typeof(double));
            var p_phi = p_pt;
            var p_E = p_eta;
            var expr = Expression.Call(typeof(TLZHelper).GetMethod("CreateTLZ"), p_pt, p_eta, p_phi, p_E);

            var r = target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var ccpstatement = gc.CodeBody.Statements.First();
            Assert.IsNotNull(ccpstatement);
            var cmInfo = ccpstatement as ICMStatementInfo;
            Assert.IsNotNull(cmInfo);
            Assert.AreEqual(1, cmInfo.ResultVariables.Count(), "# of result variables");
            Assert.AreEqual(r.RawValue, cmInfo.ResultVariables.First(), "Result variable name");
            Assert.AreEqual(2, cmInfo.DependentVariables.Count(), "# of dependent variables");
            Assert.IsTrue(cmInfo.DependentVariables.Contains(p_pt.RawValue), "doesn't have pt");
            Assert.IsTrue(cmInfo.DependentVariables.Contains(p_eta.RawValue), "Doesn't have eta");
        }
        public void LiftConstantDefinedInLoop()
        {
            var gc = new GeneratedCode();
            var counter = AddLoop(gc, mainStatementType: MainStatementType.IsConstant, defineCounterInsideBlock: true);

            Console.WriteLine("Before optimization");
            gc.DumpCodeToConsole();

            StatementLifter.Optimize(gc);

            Console.WriteLine("After optimization");
            gc.DumpCodeToConsole();

            Assert.AreEqual(2, gc.CodeBody.Statements.Count(), "# of statements");
            Assert.AreEqual(1, gc.CodeBody.Statements.WhereCast<IStatement,StatementAssign>().Count(), "# of assign statements");
            Assert.AreEqual(1, gc.CodeBody.Statements.WhereCast<IStatement, StatementForLoop>().Where(s => s.Statements.Count() == 0).Count(), "# of if statements with zero statements in it");
            Assert.AreEqual(1, gc.CodeBody.DeclaredVariables.Count(), "# of declarations");
        }
        public void TestScopingBlock()
        {
            var target = new TypeHandlerCPPCode();
            var gc = new GeneratedCode();
            var context = new CodeContext();

            var p_pt = Expression.Parameter(typeof(double), "ptParam");
            var p_eta = Expression.Parameter(typeof(double), "etaParam");
            var p_phi = Expression.Parameter(typeof(double), "phiParam");
            var p_E = Expression.Parameter(typeof(double), "EParam");
            var expr = Expression.Call(typeof(TLZHelper).GetMethod("TestIF"), p_pt, p_eta, p_phi, p_E);

            target.CodeMethodCall(expr, gc, MEFUtilities.MEFContainer);

            gc.DumpCodeToConsole();

            var ifstatement = gc.CodeBody.Statements.First().CodeItUp().First();
            Assert.IsFalse(ifstatement.EndsWith(";"), string.Format("Line '{0}' ends with a semicolon", ifstatement));
        }
        public void TestSimpleAddition()
        {
            MEFUtilities.Compose(new TypeHandlers.TypeHandlerCache());
            AggregateFromSeedResultOperator agg = new AggregateFromSeedResultOperator(Expression.Constant(1),
                Expression.Lambda(Expression.MakeBinary(ExpressionType.Add, Expression.Parameter(typeof(int), "count"), Expression.Constant(1)),
                Expression.Parameter(typeof(int), "count")),
                null);

            ROAggregate processor = new ROAggregate();
            GeneratedCode gc = new GeneratedCode();
            var result = ProcessResultOperator(processor, agg, null, gc);

            Assert.AreEqual(typeof(int), result.Type, "Expected the type to be an integer!");

            Assert.IsInstanceOfType(result, typeof(DeclarableParameter), "Expected a var simple!");
            var vs = result as DeclarableParameter;
            Assert.AreEqual("1", vs.InitialValue.RawValue, "Incorrect seed value");

            ///
            /// Now make sure the statements came back ok!
            /// 

            gc.DumpCodeToConsole();
            Assert.AreEqual(0, gc.CodeBody.DeclaredVariables.Count(), "Expected no bookings");
            Assert.AreEqual(1, gc.CodeBody.Statements.Count(), "expected a statement!");
            Assert.IsInstanceOfType(gc.CodeBody.Statements.First(), typeof(Statements.StatementAggregate), "expected an assignment statement!");

            var ass = gc.CodeBody.Statements.First() as Statements.StatementAggregate;
            StringBuilder bld = new StringBuilder();
            bld.AppendFormat("{0}+1", ass.ResultVariable.ParameterName);
            Assert.AreEqual(bld.ToString(), ass.Expression.RawValue, "the raw value of hte expression is not right");
        }