Esempio n. 1
0
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        // create a namespace
        CodeNamespace ns = new CodeNamespace("NS");

        ns.Imports.Add(new CodeNamespaceImport("System"));
        cu.Namespaces.Add(ns);

        // create a class
        CodeTypeDeclaration class1 = new CodeTypeDeclaration();

        class1.Name    = "Test";
        class1.IsClass = true;
        ns.Types.Add(class1);

        if (Supports(provider, GeneratorSupport.TryCatchStatements))
        {
            // try catch statement with just finally
            // GENERATE (C#):
            //       public static int FirstScenario(int a) {
            //            try {
            //            }
            //            finally {
            //                a = (a + 5);
            //            }
            //            return a;
            //        }
            AddScenario("CheckFirstScenario");
            CodeMemberMethod cmm = new CodeMemberMethod();
            cmm.Name       = "FirstScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);

            CodeTryCatchFinallyStatement tcfstmt = new CodeTryCatchFinallyStatement();
            tcfstmt.FinallyStatements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("a"), new
                                                                  CodeBinaryOperatorExpression(new CodeArgumentReferenceExpression("a"), CodeBinaryOperatorType.Add,
                                                                                               new CodePrimitiveExpression(5))));
            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("a")));
            class1.Members.Add(cmm);

            // in VB (a = a/a) generates an warning if a is integer. Cast the expression just for VB language.
            CodeBinaryOperatorExpression cboExpression   = new CodeBinaryOperatorExpression(new CodeArgumentReferenceExpression("a"), CodeBinaryOperatorType.Divide, new CodeArgumentReferenceExpression("a"));
            CodeAssignStatement          assignStatement = null;
            if (provider is Microsoft.VisualBasic.VBCodeProvider)
            {
                assignStatement = new CodeAssignStatement(new CodeArgumentReferenceExpression("a"), new CodeCastExpression(typeof(int), cboExpression));
            }
            else
            {
                assignStatement = new CodeAssignStatement(new CodeArgumentReferenceExpression("a"), cboExpression);
            }

            // try catch statement with just catch
            // GENERATE (C#):
            //        public static int SecondScenario(int a, string exceptionMessage) {
            //            try {
            //                a = (a / a);
            //            }
            //            catch (System.Exception e) {
            //                a = 3;
            //                exceptionMessage = e.ToString();
            //            }
            //            finally {
            //                a = (a + 1);
            //            }
            //            return a;
            //        }
            AddScenario("CheckSecondScenario");
            cmm            = new CodeMemberMethod();
            cmm.Name       = "SecondScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param          = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);
            cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "exceptionMessage"));

            tcfstmt = new CodeTryCatchFinallyStatement();
            CodeCatchClause catchClause = new CodeCatchClause("e");
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("a"),
                                                               new CodePrimitiveExpression(3)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("e"), "ToString")));
            tcfstmt.CatchClauses.Add(catchClause);
            tcfstmt.FinallyStatements.Add(CDHelper.CreateIncrementByStatement(new CodeArgumentReferenceExpression("a"), 1));

            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("a")));

            class1.Members.Add(cmm);

            // try catch statement with multiple catches
            // GENERATE (C#):
            //        public static int ThirdScenario(int a, string exceptionMessage) {
            //            try {
            //                a = (a / a);
            //            }
            //            catch (System.ArgumentNullException e) {
            //                a = 10;
            //                exceptionMessage = e.ToString();
            //            }
            //            catch (System.DivideByZeroException f) {
            //                exceptionMessage = f.ToString();
            //                a = 9;
            //            }
            //            return a;
            //        }
            AddScenario("CheckThirdScenario");
            cmm            = new CodeMemberMethod();
            cmm.Name       = "ThirdScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param          = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);
            cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "exceptionMessage"));

            tcfstmt     = new CodeTryCatchFinallyStatement();
            catchClause = new CodeCatchClause("e", new CodeTypeReference(typeof(ArgumentNullException)));
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("a"),
                                                               new CodePrimitiveExpression(9)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("e"), "ToString")));
            tcfstmt.CatchClauses.Add(catchClause);

            // add a second catch clause
            catchClause = new CodeCatchClause("f", new CodeTypeReference(typeof(Exception)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("f"), "ToString")));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("a"),
                                                               new CodePrimitiveExpression(9)));
            tcfstmt.CatchClauses.Add(catchClause);

            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("a")));
            class1.Members.Add(cmm);

            // catch throws exception
            // GENERATE (C#):
            //        public static int FourthScenario(int a) {
            //            try {
            //                a = (a / a);
            //            }
            //            catch (System.Exception e) {
            //                // Error handling
            //                throw e;
            //            }
            //            return a;
            //        }
            AddScenario("CheckFourthScenario");
            cmm            = new CodeMemberMethod();
            cmm.Name       = "FourthScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param          = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);

            tcfstmt     = new CodeTryCatchFinallyStatement();
            catchClause = new CodeCatchClause("e");
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeCommentStatement("Error handling"));
            catchClause.Statements.Add(new CodeThrowExceptionStatement(new CodeArgumentReferenceExpression("e")));
            tcfstmt.CatchClauses.Add(catchClause);
            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("a")));
            class1.Members.Add(cmm);
        }
    }
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        // GENERATES (C#):
        //  namespace Namespace1 {
        //      using System;
        //
        //
        //      public class Class1 : object {
        //
        //          public int ReturnMethod(int intInput) {

        CodeNamespace ns = new CodeNamespace("Namespace1");

        ns.Imports.Add(new CodeNamespaceImport("System"));
        cu.Namespaces.Add(ns);

        CodeTypeDeclaration class1 = new CodeTypeDeclaration();

        class1.Name = "Class1";
        class1.BaseTypes.Add(new CodeTypeReference(typeof(object)));
        ns.Types.Add(class1);

        AddScenario("CheckReturnMethod", "Tests varying operators.");
        CodeMemberMethod retMethod = new CodeMemberMethod();

        retMethod.Name       = "ReturnMethod";
        retMethod.Attributes = (retMethod.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
        retMethod.ReturnType = new CodeTypeReference(typeof(int));
        retMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "intInput"));

        // GENERATES (C#):
        //              int x1 = ((18
        //                          / (6 - 4))
        //                          * intInput);
        //
        // in VB (a = a/a) generates an warning if a is integer. Cast the expression just for VB language.
        CodeBinaryOperatorExpression cboExpression = new CodeBinaryOperatorExpression(
            new CodeBinaryOperatorExpression(
                new CodePrimitiveExpression(18),
                CodeBinaryOperatorType.Divide,
                new CodeBinaryOperatorExpression(
                    new CodePrimitiveExpression(6),
                    CodeBinaryOperatorType.Subtract,
                    new CodePrimitiveExpression(4))),
            CodeBinaryOperatorType.Multiply,
            new CodeArgumentReferenceExpression("intInput"));

        CodeVariableDeclarationStatement variableDeclaration = null;

        if (provider is Microsoft.VisualBasic.VBCodeProvider)
        {
            variableDeclaration = new CodeVariableDeclarationStatement(typeof(int), "x1", new CodeCastExpression(typeof(int), cboExpression));
        }
        else
        {
            variableDeclaration = new CodeVariableDeclarationStatement(typeof(int), "x1", cboExpression);
        }

        retMethod.Statements.Add(variableDeclaration);

        // GENERATES (C#):
        //              int x2 = (19 % 8);
        retMethod.Statements.Add(
            new CodeVariableDeclarationStatement(
                typeof(int),
                "x2",
                new CodeBinaryOperatorExpression(
                    new CodePrimitiveExpression(19),
                    CodeBinaryOperatorType.Modulus,
                    new CodePrimitiveExpression(8))));

        // GENERATES (C#):
        //              int x3 = ((15 & 35)
        //                          | 129);
        retMethod.Statements.Add(
            new CodeVariableDeclarationStatement(
                typeof(int),
                "x3",
                new CodeBinaryOperatorExpression(
                    new CodeBinaryOperatorExpression(
                        new CodePrimitiveExpression(15),
                        CodeBinaryOperatorType.BitwiseAnd,
                        new CodePrimitiveExpression(35)),
                    CodeBinaryOperatorType.BitwiseOr,
                    new CodePrimitiveExpression(129))));

        // GENERATES (C#):
        //              int x4 = 0;
        retMethod.Statements.Add(
            new CodeVariableDeclarationStatement(
                typeof(int),
                "x4",
                new CodePrimitiveExpression(0)));

        // GENERATES (C#):
        //              if (((x2 == 3)
        //                          || (x3 < 129))) {
        //                  x4 = (x4 + 1);
        //              }
        //              else {
        //                  x4 = (x4 + 2);
        //              }

        retMethod.Statements.Add(
            new CodeConditionStatement(
                new CodeBinaryOperatorExpression(
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("x2"),
                        CodeBinaryOperatorType.ValueEquality,
                        new CodePrimitiveExpression(3)),
                    CodeBinaryOperatorType.BooleanOr,
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("x3"),
                        CodeBinaryOperatorType.LessThan,
                        new CodePrimitiveExpression(129))),
                CDHelper.CreateIncrementByStatement("x4", 1),
                CDHelper.CreateIncrementByStatement("x4", 2)));

        // GENERATES (C#):
        //              if (((x2 > -1)
        //                          && (x3 >= 5000))) {
        //                  x4 = (x4 + 4);
        //              }
        //              else {
        //                  x4 = (x4 + 8);
        //              }
        retMethod.Statements.Add(
            new CodeConditionStatement(
                new CodeBinaryOperatorExpression(
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("x2"),
                        CodeBinaryOperatorType.GreaterThan,
                        new CodePrimitiveExpression(-1)),
                    CodeBinaryOperatorType.BooleanAnd,
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("x3"),
                        CodeBinaryOperatorType.GreaterThanOrEqual,
                        new CodePrimitiveExpression(5000))),
                CDHelper.CreateIncrementByStatement("x4", 4),
                CDHelper.CreateIncrementByStatement("x4", 8)));

        // GENERATES (C#):
        //              if (((x2 <= 3)
        //                          && (x3 != 1))) {
        //                  x4 = (x4 + 16);
        //              }
        //              else {
        //                  x4 = (x4 + 32);
        //              }
        retMethod.Statements.Add(
            new CodeConditionStatement(
                new CodeBinaryOperatorExpression(
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("x2"),
                        CodeBinaryOperatorType.LessThanOrEqual,
                        new CodePrimitiveExpression(3)),
                    CodeBinaryOperatorType.BooleanAnd,
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("x3"),
                        CodeBinaryOperatorType.IdentityInequality,
                        new CodePrimitiveExpression(1))),
                CDHelper.CreateIncrementByStatement("x4", 16),
                CDHelper.CreateIncrementByStatement("x4", 32)));


        // GENERATES (C#):
        //              return (x1
        //                          + (x2
        //                          + (x3 + x4)));
        //          }
        retMethod.Statements.Add(
            new CodeMethodReturnStatement(
                new CodeBinaryOperatorExpression(
                    new CodeVariableReferenceExpression("x1"),
                    CodeBinaryOperatorType.Add,
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("x2"),
                        CodeBinaryOperatorType.Add,
                        new CodeBinaryOperatorExpression(
                            new CodeVariableReferenceExpression("x3"),
                            CodeBinaryOperatorType.Add,
                            new CodeVariableReferenceExpression("x4"))))));
        class1.Members.Add(retMethod);

        //
        // GENERATES (C#):
        //          public int SecondReturnMethod(int intInput) {
        //              // To test CodeBinaryOperatorType.IdentityEquality operator
        //              if ((((Object)(intInput)) == ((Object)(5)))) {
        //                  return 5;
        //              }
        //              else {
        //                  return 4;
        //              }
        //          }
        //      }
        AddScenario("CheckSecondReturnMethod", "Tests identity equality.");
        retMethod            = new CodeMemberMethod();
        retMethod.Name       = "SecondReturnMethod";
        retMethod.Attributes = (retMethod.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
        retMethod.ReturnType = new CodeTypeReference(typeof(int));
        retMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "intInput"));

        retMethod.Statements.Add(new CodeCommentStatement("To test CodeBinaryOperatorType.IdentiEquality operator"));
        retMethod.Statements.Add(
            new CodeConditionStatement(
                new CodeBinaryOperatorExpression(new CodeCastExpression("Object",
                                                                        new CodeArgumentReferenceExpression("intInput")),
                                                 CodeBinaryOperatorType.IdentityEquality, new CodeCastExpression("Object",
                                                                                                                 new CodePrimitiveExpression(5))),
                new CodeStatement[] { new CodeMethodReturnStatement(new
                                                                    CodePrimitiveExpression(5)) }, new CodeStatement[] { new
                                                                                                                         CodeMethodReturnStatement(new CodePrimitiveExpression(4)) }));
        class1.Members.Add(retMethod);

        // GENERATES (C#):
        //      public class Class2 : object {
        //      }
        //  }

        /*class1 = new CodeTypeDeclaration ();
         * class1.Name = "Class2";
         * class1.BaseTypes.Add (new CodeTypeReference (typeof (object)));
         * ns.Types.Add (class1);*/
    }
Esempio n. 3
0
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        // GENERATES (C#):
        //
        //  namespace NSPC {
        //    public class DelegateClass {
        //
        //        public virtual int Sum(
        //                    int val1,
        //                    int val2,
        //                    int val3,
        //                    int val4,
        //                    int val5,
        //                    int val6,
        //                    int val7,
        //                    int val8,
        //                    int val9,
        //                    int val10,
        //                    int val11,
        //                    int val12,
        //                    int val13,
        //                    int val14,
        //                    int val15,
        //                    int val16,
        //                    int val17,
        //                    int val18,
        //                    int val19,
        //                    int val20) {
        //            int mySum = 0;
        //            mySum = (mySum + val1);
        //            mySum = (mySum + val2);
        //            mySum = (mySum + val3);
        //            mySum = (mySum + val4);
        //            mySum = (mySum + val5);
        //            mySum = (mySum + val6);
        //            mySum = (mySum + val7);
        //            mySum = (mySum + val8);
        //            mySum = (mySum + val9);
        //            mySum = (mySum + val10);
        //            mySum = (mySum + val11);
        //            mySum = (mySum + val12);
        //            mySum = (mySum + val13);
        //            mySum = (mySum + val14);
        //            mySum = (mySum + val15);
        //            mySum = (mySum + val16);
        //            mySum = (mySum + val17);
        //            mySum = (mySum + val18);
        //            mySum = (mySum + val19);
        //            mySum = (mySum + val20);
        //            return mySum;
        //        }
        //
        //        public virtual int Do() {
        //            MyDelegate myDel = new DelegateClass.MyDelegate(this.Sum);
        //            return myDel(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765);
        //        }
        //    }
        //
        //    public delegate int MyDelegate(
        //                    int val1,
        //                    int val2,
        //                    int val3,
        //                    int val4,
        //                    int val5,
        //                    int val6,
        //                    int val7,
        //                    int val8,
        //                    int val9,
        //                    int val10,
        //                    int val11,
        //                    int val12,
        //                    int val13,
        //                    int val14,
        //                    int val15,
        //                    int val16,
        //                    int val17,
        //                    int val18,
        //                    int val19,
        //                    int val20);
        //    }

        CodeNamespace nspace = new CodeNamespace("NSPC");

        cu.Namespaces.Add(nspace);

        // only produce code if the generator can declare delegates
        if (Supports(provider, GeneratorSupport.DeclareDelegates))
        {
            CodeTypeDeclaration class1 = new CodeTypeDeclaration("DelegateClass");
            class1.IsClass = true;
            nspace.Types.Add(class1);

            CodeTypeDelegate td = new CodeTypeDelegate("MyDelegate");
            td.ReturnType = new CodeTypeReference(typeof(Int32));
            for (int i = 1; i <= 5; i++)
            {
                td.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(Int32)), "val" + i));
            }
            nspace.Types.Add(td);

            CodeMemberMethod cmm = new CodeMemberMethod();
            cmm.Name       = "Sum";
            cmm.ReturnType = new CodeTypeReference(typeof(Int32));
            for (int i = 1; i <= 5; i++)
            {
                cmm.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(Int32)), "val" + i));
            }
            cmm.Attributes = MemberAttributes.Public;

            cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "mySum", new CodePrimitiveExpression(0)));

            for (int i = 1; i <= 5; i++)
            {
                cmm.Statements.Add(CDHelper.CreateIncrementByStatement("mySum", new CodeArgumentReferenceExpression("val" + i)));
            }

            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("mySum")));

            class1.Members.Add(cmm);

#if !WHIDBEY
            if (!(provider is VBCodeProvider))
            {
#endif
            AddScenario("CheckDo", "Check Do()'s return value.");
            cmm            = new CodeMemberMethod();
            cmm.Name       = "Do";
            cmm.ReturnType = new CodeTypeReference(typeof(Int32));
            cmm.Attributes = MemberAttributes.Public;

            cmm.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("MyDelegate"), "myDel",
                                                                    new CodeDelegateCreateExpression(new CodeTypeReference("NSPC.MyDelegate"),
                                                                                                     new CodeThisReferenceExpression(), "Sum")));

            CodeDelegateInvokeExpression delegateInvoke = new CodeDelegateInvokeExpression();
            delegateInvoke.TargetObject = new CodeVariableReferenceExpression("myDel");
            for (int i = 1; i <= 5; i++)
            {
                delegateInvoke.Parameters.Add(new CodePrimitiveExpression(fib(i)));
            }
            cmm.Statements.Add(new CodeMethodReturnStatement(delegateInvoke));

            class1.Members.Add(cmm);
#if !WHIDBEY
        }
#endif
        }
    }
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        // GENERATES (C#):
        //
        //  namespace NSPC {
        //
        //      public class ClassWithMethod {
        //
        //          public static int ReturnMethod(int intInput) {
        //              if (((intInput <= 3)
        //                          && (intInput == 2))) {
        //                  intInput = (intInput + 16);
        //              }
        //              else {
        //                  intInput = (intInput + 1);
        //              }
        //              if ((intInput <= 10)) {
        //                  intInput = (intInput + 11);
        //              }
        //              return intInput;
        //          }
        //      }
        //  }

        AddScenario("CheckReturnMethod1", "Check return value of ReturnMethod()");
        AddScenario("CheckReturnMethod2", "Check return value of ReturnMethod()");
        CodeNamespace nspace = new CodeNamespace("NSPC");

        cu.Namespaces.Add(nspace);

        CodeTypeDeclaration class1 = new CodeTypeDeclaration("ClassWithMethod");

        class1.IsClass = true;
        nspace.Types.Add(class1);

        CodeMemberMethod retMethod = new CodeMemberMethod();

        retMethod.Name       = "ReturnMethod";
        retMethod.Attributes = MemberAttributes.Public | MemberAttributes.Static;
        retMethod.ReturnType = new CodeTypeReference(typeof(int));
        retMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "intInput"));
        retMethod.Statements.Add(
            new CodeConditionStatement(
                new CodeBinaryOperatorExpression(
                    new CodeBinaryOperatorExpression(
                        new CodeArgumentReferenceExpression("intInput"),
                        CodeBinaryOperatorType.LessThanOrEqual,
                        new CodePrimitiveExpression(3)),
                    CodeBinaryOperatorType.BooleanAnd,
                    new CodeBinaryOperatorExpression(
                        new CodeArgumentReferenceExpression("intInput"),
                        CodeBinaryOperatorType.ValueEquality,
                        new CodePrimitiveExpression(2))),
                CDHelper.CreateIncrementByStatement(new CodeArgumentReferenceExpression("intInput"), 16),
                CDHelper.CreateIncrementByStatement(new CodeArgumentReferenceExpression("intInput"), 1)));
        retMethod.Statements.Add(new CodeConditionStatement(
                                     new CodeBinaryOperatorExpression(new CodeArgumentReferenceExpression("intInput"), CodeBinaryOperatorType.LessThanOrEqual,
                                                                      new CodePrimitiveExpression(10)),
                                     new CodeAssignStatement(new CodeArgumentReferenceExpression("intInput"),
                                                             new CodeBinaryOperatorExpression(new CodeArgumentReferenceExpression("intInput"),
                                                                                              CodeBinaryOperatorType.Add, new CodePrimitiveExpression(11)))));
        retMethod.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("intInput")));
        class1.Members.Add(retMethod);
    }
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        CodeNamespace ns = new CodeNamespace("Namespace1");

        cu.Namespaces.Add(ns);

        // Generate Class1
        CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");

        class1.IsClass = true;
        ns.Types.Add(class1);

        // GENERATES (C#):
        //       public int ArrayMethod(int parameter) {
        //                int arraySize = 3;
        //                int[] array1;
        //                int[] array2 = new int[3];
        //                int[] array3 = new int[] {1,
        //                        4,
        //                        9};
        //                array1 = new int[arraySize];
        //                int retValue = 0;
        //                int i;
        //                for (i = 0; (i < array1.Length); i = (i + 1)) {
        //                    array1[i] = (i * i);
        //                    array2[i] = (array1[i] - i);
        //                    retValue = retValue + array1[i];
        //                    retValue = retValue + array2[i];
        //                    retValue = retValue + array3[i];
        //                }
        //                return retValue;
        //            }
        AddScenario("ArrayMethod", "Tests sized arrays, initialized arrays, standard arrays of small size");
        CodeMemberMethod arrayMethod = new CodeMemberMethod();

        arrayMethod.Name = "ArrayMethod";
        arrayMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "parameter"));
        arrayMethod.Attributes = (arrayMethod.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
        arrayMethod.ReturnType = new CodeTypeReference(typeof(System.Int32));
        arrayMethod.Statements.Add(
            new CodeVariableDeclarationStatement(typeof(int), "arraySize", new CodePrimitiveExpression(3)));

        arrayMethod.Statements.Add(
            new CodeVariableDeclarationStatement(typeof(int[]), "array1"));

        arrayMethod.Statements.Add(
            new CodeVariableDeclarationStatement(
                new CodeTypeReference("System.Int32", 1),
                "array2",
                new CodeArrayCreateExpression(typeof(int[]), new CodePrimitiveExpression(3))));

        arrayMethod.Statements.Add(
            new CodeVariableDeclarationStatement(
                new CodeTypeReference("System.Int32", 1),
                "array3",
                new CodeArrayCreateExpression(
                    new CodeTypeReference("System.Int32", 1),
                    new CodeExpression[] {
            new CodePrimitiveExpression(1),
            new CodePrimitiveExpression(4),
            new CodePrimitiveExpression(9)
        })));

        arrayMethod.Statements.Add(
            new CodeAssignStatement(
                new CodeVariableReferenceExpression("array1"),
                new CodeArrayCreateExpression(typeof(int[]), new CodeVariableReferenceExpression("arraySize"))));

        arrayMethod.Statements.Add(
            new CodeVariableDeclarationStatement(typeof(System.Int32), "retValue", new CodePrimitiveExpression(0)));

        arrayMethod.Statements.Add(
            new CodeVariableDeclarationStatement(typeof(int), "i"));

        arrayMethod.Statements.Add(
            new CodeIterationStatement(
                new CodeAssignStatement(new CodeVariableReferenceExpression("i"), new CodePrimitiveExpression(0)),
                new CodeBinaryOperatorExpression(
                    new CodeVariableReferenceExpression("i"),
                    CodeBinaryOperatorType.LessThan,
                    new CodePropertyReferenceExpression(
                        new CodeVariableReferenceExpression("array1"),
                        "Length")),
                new CodeAssignStatement(
                    new CodeVariableReferenceExpression("i"),
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("i"),
                        CodeBinaryOperatorType.Add,
                        new CodePrimitiveExpression(1))),
                new CodeAssignStatement(
                    new CodeArrayIndexerExpression(
                        new CodeVariableReferenceExpression("array1"),
                        new CodeVariableReferenceExpression("i")),
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("i"),
                        CodeBinaryOperatorType.Multiply,
                        new CodeVariableReferenceExpression("i"))),
                new CodeAssignStatement(
                    new CodeArrayIndexerExpression(
                        new CodeVariableReferenceExpression("array2"),
                        new CodeVariableReferenceExpression("i")),
                    new CodeBinaryOperatorExpression(
                        new CodeArrayIndexerExpression(
                            new CodeVariableReferenceExpression("array1"),
                            new CodeVariableReferenceExpression("i")),
                        CodeBinaryOperatorType.Subtract,
                        new CodeVariableReferenceExpression("i"))),
                CDHelper.CreateIncrementByStatement("retValue",
                                                    CDHelper.CreateArrayRef("array1", "i")),
                CDHelper.CreateIncrementByStatement("retValue",
                                                    CDHelper.CreateArrayRef("array2", "i")),
                CDHelper.CreateIncrementByStatement("retValue",
                                                    CDHelper.CreateArrayRef("array3", "i"))));

        arrayMethod.Statements.Add(
            new CodeMethodReturnStatement(new CodeVariableReferenceExpression("retValue")));
        class1.Members.Add(arrayMethod);
    }
Esempio n. 6
0
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        // GENERATES (C#):
        //  namespace Namespace1 {
        //      using System;
        //
        //
        //      public class Class1 : object {
        //
        //          public int ReturnMethod(int intInput) {

        AddScenario("ReturnMethod", "Verifies binary operators in a series of operations.");
        CodeNamespace ns = new CodeNamespace("Namespace1");

        ns.Imports.Add(new CodeNamespaceImport("System"));
        cu.Namespaces.Add(ns);

        CodeTypeDeclaration class1 = new CodeTypeDeclaration();

        class1.Name = "Class1";
        class1.BaseTypes.Add(new CodeTypeReference(typeof(object)));
        ns.Types.Add(class1);

        CodeMemberMethod retMethod = new CodeMemberMethod();

        retMethod.Name       = "ReturnMethod";
        retMethod.Attributes = (retMethod.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
        retMethod.ReturnType = new CodeTypeReference(typeof(int));
        retMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "intInput"));

        // GENERATES (C#):
        //              int x1;
        //              x1 = 6 - 4;
        //              double x1d = x1;
        //              x1d = 18 / x1d;
        //              x1 = (int) x1d;
        //              x1 = x1 * intInput;

        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "x1"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("x1", 6, CodeBinaryOperatorType.Subtract, 4));
        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(double), "x1d", new CodeVariableReferenceExpression("x1")));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("x1d", 18, CodeBinaryOperatorType.Divide, "x1d"));
        retMethod.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("x1"),
                                                         new CodeCastExpression(typeof(int), new CodeVariableReferenceExpression("x1d"))));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("x1", "x1", CodeBinaryOperatorType.Multiply,
                                                                        new CodeArgumentReferenceExpression("intInput")));

        // GENERATES (C#):
        //              int x2;
        //              x2 = (19 % 8);
        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "x2"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("x2", 19, CodeBinaryOperatorType.Modulus, 8));

        // GENERATES (C#):
        //              int x3;
        //              x3 = 15 & 35;
        //              x3 = x3 | 129;
        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "x3"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("x3", 15, CodeBinaryOperatorType.BitwiseAnd, 35));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("x3", "x3", CodeBinaryOperatorType.BitwiseOr, 129));

        // GENERATES (C#):
        //              int x4 = 0;
        retMethod.Statements.Add(
            new CodeVariableDeclarationStatement(
                typeof(int),
                "x4",
                new CodePrimitiveExpression(0)));

        // GENERATES (C#):
        //              bool res1;
        //              res1 = x2 == 3;
        //              bool res2;
        //              res2 = x3 < 129;
        //              bool res3;
        //              res3 = res1 || res2;
        //              if (res3) {
        //                  x4 = (x4 + 1);
        //              }
        //              else {
        //                  x4 = (x4 + 2);
        //              }
        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res1"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res1", "x2", CodeBinaryOperatorType.ValueEquality, 3));

        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res2"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res2", "x3", CodeBinaryOperatorType.LessThan, 129));

        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res3"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res3", "res1", CodeBinaryOperatorType.BooleanOr, "res2"));

        retMethod.Statements.Add(
            new CodeConditionStatement(new CodeVariableReferenceExpression("res3"),
                                       new CodeStatement [] { CDHelper.CreateIncrementByStatement("x4", 1) },
                                       new CodeStatement [] { CDHelper.CreateIncrementByStatement("x4", 2) }));

        // GENERATES (C#):
        //              bool res4;
        //              res4 = x2 > -1;
        //              bool res5;
        //              res5 = x3 > 5000;
        //              bool res6;
        //              res6 = res4 && res5;
        //              if (res6) {
        //                  x4 = (x4 + 4);
        //              }
        //              else {
        //                  x4 = (x4 + 8);
        //              }
        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res4"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res4", "x2", CodeBinaryOperatorType.GreaterThan, -1));

        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res5"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res5", "x3", CodeBinaryOperatorType.GreaterThanOrEqual, 5000));

        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res6"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res6", "res4", CodeBinaryOperatorType.BooleanAnd, "res5"));

        retMethod.Statements.Add(
            new CodeConditionStatement(
                new CodeVariableReferenceExpression("res6"),
                new CodeStatement [] { CDHelper.CreateIncrementByStatement("x4", 4) },
                new CodeStatement [] { CDHelper.CreateIncrementByStatement("x4", 8) }));

        // GENERATES (C#):
        //              bool res7;
        //              res7 = x2 < 3;
        //              bool res8;
        //              res8 = x3 != 1;
        //              bool res9;
        //              res9 = res7 && res8;
        //              if (res9) {
        //                  x4 = (x4 + 16);
        //              }
        //              else {
        //                  x4 = (x4 + 32);
        //              }
        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res7"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res7", "x2", CodeBinaryOperatorType.LessThanOrEqual, 3));

        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res8"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res8", "x3", CodeBinaryOperatorType.IdentityInequality, 1));

        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "res9"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("res9", "res7", CodeBinaryOperatorType.BooleanAnd, "res8"));

        retMethod.Statements.Add(
            new CodeConditionStatement(
                new CodeVariableReferenceExpression("res9"),
                new CodeStatement [] { CDHelper.CreateIncrementByStatement("x4", 16) },
                new CodeStatement [] { CDHelper.CreateIncrementByStatement("x4", 32) }));


        // GENERATES (C#):
        //              int theSum;
        //              theSum = x1 + x2;
        //              theSum = theSum + x3;
        //              theSum = theSum + x4;
        //              return theSum;
        //          }
        //
        retMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "theSum"));
        retMethod.Statements.Add(CDHelper.CreateBinaryOperatorStatement("theSum", "x1", CodeBinaryOperatorType.Add, "x2"));
        retMethod.Statements.Add(CDHelper.CreateIncrementByStatement("theSum", "x3"));
        retMethod.Statements.Add(CDHelper.CreateIncrementByStatement("theSum", "x4"));

        retMethod.Statements.Add(new CodeMethodReturnStatement(
                                     new CodeVariableReferenceExpression("theSum")));
        class1.Members.Add(retMethod);
    }