Exemplo n.º 1
0
 private void randomizeIsArray()
 {
     if (ProgGenUtil.allowArray.Equals("yes"))
     {
         isArray = ProgGenUtil.coinFlip();
     }
     else
     {
         isArray = false;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// API to generate a method for a class.
        /// </summary>
        /// <param name="generator"> </param>
        /// <param name="maxNumberOfParams"> </param>
        /// <param name="classList"> </param>
        /// <param name="name"> </param>
        /// <param name="maxLocPerMethod"> </param>
        /// <param name="maxNestedIfs"> </param>
        /// <param name="maxAllowedMethodCalls"> </param>
        /// <param name="needMethodBody">
        /// @return </param>
        public static Method generateMethod(ClassGenerator generator, int maxNumberOfParams, List <ClassGenerator> classList, string name, int maxLocPerMethod, int maxNestedIfs, int maxAllowedMethodCalls, bool needMethodBody)
        {
            bool isStatic = ProgGenUtil.coinFlip();

            //we need minimum of two parameters
            int numberOfParams = ProgGenUtil.getRandomIntInRange(maxNumberOfParams);

            //if(numberOfParams<2)
            if (numberOfParams < ProgGenUtil.minNoOfParameters)
            {
                numberOfParams = ProgGenUtil.minNoOfParameters;
            }

            return(new Method(isStatic, numberOfParams, generator, classList, name, maxLocPerMethod, maxNestedIfs, maxAllowedMethodCalls, needMethodBody));
        }
Exemplo n.º 3
0
        public static Statement getRandomizedStatement(Method method, List <ClassGenerator> classList)
        {
            Statement stmt   = new Statement();
            Random    rand   = new Random();
            int       option = 0;

            option = rand.Next(100) % 3;
            switch (option)
            {
            case 0:             // Assignment statement
                stmt.stmt = (new AssignmentExpression(method)).ToString();
                //FIXME: if there is no local variable available to assign, it will simply use print statement.
                // following line will add an extra line to the LOC
                method.Loc = method.Loc + 1;
                break;

            case 1:             // Print statements: Max. 5 lines in a block
                stmt.stmt = (new PrintStatement(method)).ToString();
                break;

            case 2:             // method calls: restrict it to MAX_ALLOWED_METH_CALL
                int methCalledCounterValue = method.MethodCallCounter + 1;
                method.MethodCallCounter = methCalledCounterValue;
                if (methCalledCounterValue < method.MaxAllowedMethodCalls)
                {
                    if (ProgGenUtil.coinFlip())
                    {
                        stmt.stmt += ProgGenUtil.getMethodCall(method, classList);
                    }
                    else
                    {
                        //wire it to variables in scope
                        Operand lhs;
                        Random  random = new Random();

                        HashSet <Type.Primitives> validPrimitivesInScope = ProgGenUtil.getValidPrimitivesInScope(method);

                        //Pick a type
                        object[] primitivesArray = validPrimitivesInScope.ToArray();

                        if (primitivesArray.Length == 0)
                        {
                            stmt.stmt += ProgGenUtil.getMethodCall(method, classList);
                            break;
                        }

                        Type.Primitives selectedPrimitive = (Type.Primitives)primitivesArray[random.Next(primitivesArray.Length)];

                        // Introducing any variable
                        lhs = VariableGenerator.getRandomizedVariable(method, selectedPrimitive);

                        string methodCall = ProgGenUtil.getMethodCallForReturnType(method, classList, new Type(selectedPrimitive, ""), lhs);
                        stmt.stmt += methodCall + "\n";
                    }
                }
                else
                {
                    stmt.stmt = (new PrintStatement(method)).ToString();
                }
                break;

            default:
                stmt.stmt = (new IfStmtIfStmt(method, classList)).ToString();
                break;
            }

            return(stmt);
        }
Exemplo n.º 4
0
 private void randomizeIsStatic()
 {
     isStatic = ProgGenUtil.coinFlip();
 }
Exemplo n.º 5
0
        public AssignmentExpression(Method method)
        {
            Random rand = new Random();

            //should we use field or variable?
            int OptionVarOrField = rand.Next(100) % 2;

            if (OptionVarOrField == 0)
            {
                // Introducing any variable
                lhs = VariableGenerator.getRandomizedVariable(method);
            }
            else
            {
                //Introducing any field
                lhs = FieldGenerator.getRandomField(method.AssociatedClass, method.Static);

                if (lhs == null)
                {
                    //if no field is present, introduce variable
                    lhs = VariableGenerator.getRandomizedVariable(method);
                }
            }

            // if no variable is present, just use print statements
            if (lhs == null)
            {
                output += (new PrintStatement(method)).ToString();
                return;
            }


            Primitives primitive = lhs.type.getType();

            //If the primitive is an object, invoke constructor
            if (primitive == Primitives.OBJECT)
            {
                output += lhs + " = new " + ProgGenUtil.getClassToConstruct(lhs.Type.ToString(), method.ClassList) + "();\n";
                return;
            }

            //Randomly choose to nest operation or not
            int option = rand.Next(100);

            if (option > method.AssociatedClass.Percent)
            {
                leftExpr  = new NormalExpression(method, primitive);
                rightExpr = new NormalExpression(method, primitive);
                Operator binOp = new BinaryOperator(primitive);

                // Removing variable from right expr. if binOp is / or %
                // i6=(i3/2)%(i3-5)  => i6=(i3/2)%constant
                //if (binOp.ToString().Equals("/") || binOp.ToString().Equals("%"))
                //{
                //    do
                //    { //FIXME: only handles int for now.
                //        rightExpr = new Literal(primitive,Int32.MaxValue);
                //    }while (rightExpr.ToString().Contains("(0)")); //avoiding divide by (0)
                //}

                output += lhs + " = (" + lhs.Type + ")(" + leftExpr + binOp.ToString() + rightExpr + ");\n";
            }
            else
            {
                if (ProgGenUtil.useQueries)
                {
                    if (ProgGenUtil.coinFlip())
                    {
                        expr = new NormalExpression(method, primitive);

                        //we don't want assignments statements like this: i8 = i8
                        while (lhs.Equals(expr.ToString()))
                        {
                            expr = new NormalExpression(method, lhs.type.getType());
                        }
                        output += lhs + " = (" + lhs.Type + ")" + expr.ToString() + ";\n";
                    }
                    else
                    {
                        Console.WriteLine("Trying to fetch literal from database for : " + lhs.Type);
                        StringBuilder builder = new StringBuilder();
                        Query         query   = getQueryForType(primitive);
                        if (query == null)
                        {
                            //no query results
                            output += lhs + " = (" + lhs.Type + ")" + new Literal(primitive, Int32.MaxValue) + ";\n";
                            return;
                        }

                        builder.Append("try{");
                        builder.Append("ResultSet rs = DBUtil.getDBUtil().executeQuery(");
                        builder.Append("\"" + query.QueryString + "\"");
                        builder.Append(");\n");

                        builder.Append("rs.last();\n");
                        builder.Append("int rowToSelect = new Random().nextInt(rs.getRow());\n");
                        builder.Append("rs.first();\n");

                        builder.Append("for(int rowToSelectCounter=0; rowToSelectCounter<rowToSelect;rowToSelectCounter++)");
                        builder.Append("{ 	rs.next();	}\n");

                        List <QueryResult> queryResultsForType = getColumnNumbersForType(query, primitive);
                        if (queryResultsForType.Count == 0)
                        {
                            //no query results present for expected type.
                            output += lhs + " = (" + lhs.Type + ")" + new Literal(primitive, Int32.MaxValue) + ";\n";
                            return;
                        }

                        int         resultToSelect = (new Random()).Next(queryResultsForType.Count);
                        QueryResult selectedResult = queryResultsForType[resultToSelect];

                        string result = getValueFromResultSet(primitive, selectedResult);
                        if (result == null)
                        {
                            output += lhs + " = (" + lhs.Type + ")" + new Literal(primitive, Int32.MaxValue) + ";\n";
                            return;
                        }
                        builder.Append(lhs + " = (" + lhs.Type + ")" + result + "\n\n");
                        builder.Append("} catch(Exception e) { e.printStackTrace(); }\n\n");
                        builder.Append("Console.WriteLine(" + lhs + ");\n");
                        output += builder.ToString();
                    }
                }
                else
                {
                    expr = new NormalExpression(method, primitive);

                    //we don't want assignments statements like this: i8 = i8
                    while (lhs.Equals(expr.ToString()))
                    {
                        expr = new NormalExpression(method, lhs.type.getType());
                    }
                    output += lhs + " = (" + lhs.Type + ")" + expr.ToString() + ";\n";
                }
            }
        }
Exemplo n.º 6
0
        private string getValueFromResultSet(Primitives primitive, QueryResult result)
        {
            bool fetchUsingSeqNumber = ProgGenUtil.coinFlip();

            if (primitive == Primitives.INT)
            {
                if (fetchUsingSeqNumber)
                {
                    return("rs.getInt(" + result.SeqNumber + ");\n");
                }
                return("rs.getInt(\"" + result.Name + "\");\n");
            }
            else if (primitive == Primitives.BYTE)
            {
                if (fetchUsingSeqNumber)
                {
                    return("rs.getByte(" + result.SeqNumber + ");\n");
                }
                return("rs.getByte(\"" + result.Name + "\");\n");
            }
            else if (primitive == Primitives.CHAR)
            {
                if (fetchUsingSeqNumber)
                {
                    return("rs.getString(" + result.SeqNumber + ");\n");
                }
                return("rs.getString(\"" + result.Name + "\");\n");
            }
            else if (primitive == Primitives.DOUBLE)
            {
                if (fetchUsingSeqNumber)
                {
                    return("rs.getDouble(" + result.SeqNumber + ");\n");
                }
                return("rs.getDouble(\"" + result.Name + "\");\n");
            }
            else if (primitive == Primitives.FLOAT)
            {
                if (fetchUsingSeqNumber)
                {
                    return("rs.getFloat(" + result.SeqNumber + ");\n");
                }
                return("rs.getFloat(\"" + result.Name + "\");\n");
            }
            else if (primitive == Primitives.LONG)
            {
                if (fetchUsingSeqNumber)
                {
                    return("rs.getLong(" + result.SeqNumber + ");\n");
                }
                return("rs.getLong(\"" + result.Name + "\");\n");
            }
            else if (primitive == Primitives.SHORT)
            {
                if (fetchUsingSeqNumber)
                {
                    return("rs.getShort(" + result.SeqNumber + ");\n");
                }
                return("rs.getShort(\"" + result.Name + "\");\n");
            }
            else if (primitive == Primitives.STRING)
            {
                if (fetchUsingSeqNumber)
                {
                    return("rs.getString(" + result.SeqNumber + ");\n");
                }
                return("rs.getString(\"" + result.Name + "\");\n");
            }
            return(null);
        }