Example #1
0
        private MethodSignature overRiddenMethod(List <ClassGenerator> classList, int loc)
        {
            List <MethodSignature> superClassMethodSignatures = this.superClass.methodSignatures;
            MethodSignature        methodToOverride           = superClassMethodSignatures[rand.Next(superClassMethodSignatures.Count)];
            int counter = this.superClass.numberOfMethods;

            while ((overriddenMethods.Contains(methodToOverride) || methodSignatures.Contains(methodToOverride)) && counter > 0)
            {
                methodToOverride = superClassMethodSignatures[rand.Next(superClassMethodSignatures.Count)];
                counter--;
            }

            if (counter == 0)
            {
                Console.WriteLine("overriddenMethod()::returning null due to lack of methods in super class.");
                return(null);
            }

            overriddenMethods.Add(methodToOverride);
            Type.Primitives returnType = methodToOverride.ReturnType;
            if (returnType != Type.Primitives.OBJECT)
            {
                returnTypeSet.Add(returnType);
            }
            return(methodToOverride);
        }
Example #2
0
        private static Variable getVariable(List <Variable> variables, Type.Primitives primitive)
        {
            List <Variable> typedVariableList = new List <Variable>();

            foreach (Variable @var in variables)
            {
                if (@var.Name.Equals("recursionCounter"))
                {
                    continue;
                }

                if (@var.Type.Type == primitive)
                {
                    typedVariableList.Add(@var);
                }
            }

            if (typedVariableList.Count == 0)
            {
                return(null);
            }

            int index = (new Random()).Next(typedVariableList.Count);

            return(typedVariableList[index]);
        }
Example #3
0
        private void generateMethodSignatures(List <ClassGenerator> classList)
        {
            for (int i = 0; i < numberOfMethods; i++)
            {
                if (this.superClass != null)
                {
                    //flip a coin(1 out of 3) to decide for method override
                    int choiceOverride = rand.Next(3);
                    if (choiceOverride == 0)
                    {
                        MethodSignature overridenMethod = overRiddenMethod(classList, locPerMethod);
                        if (overridenMethod != null)
                        {
                            methodSignatures.Add(overridenMethod);
                            continue;
                        }
                        //else fallthrough and generate normal class method
                    }
                }

                //Basically creating MethodSignature
                Method method = Method.generateMethod(this, ProgGenUtil.maxNoOfParameters, classList, this.fileName + "method" + i, locPerMethod, this.maxNestedIfs, this.maxAllowedMethodCalls, false);

                methodSignatures.Add(method.MethodSignature);
                Type.Primitives returnType = method.ReturnType;
                if (returnType != Type.Primitives.OBJECT)
                {
                    returnTypeSet.Add(returnType);
                }
            }
        }
Example #4
0
        /// <summary>
        /// returns a random field of a given type for a generated class.
        /// </summary>
        /// <param name="generator"> </param>
        /// <param name="primitive"> </param>
        /// <param name="isStatic">
        /// @return </param>
        public static Operand getRandomField(ClassGenerator generator, Type.Primitives primitive, bool isStatic)
        {
            Field field;

            field = getField(generator.Fields, primitive, isStatic);

            if (field == null)
            {
                return(new Literal(primitive));
            }

            generator.UsedFields.add(field);
            return(field);
        }
Example #5
0
        public static Operand getRandomizedVariable(Method method, Type.Primitives primitive)
        {
            Variable variable;

            variable = getVariable(method.ParameterList, primitive);

            if (variable == null)
            {
                return(new Literal(primitive));
            }

            method.UsedParameterList.Add(variable);
            return(variable);
        }
Example #6
0
 private void overrideInterfaceMethods(List <ClassGenerator> classList)
 {
     foreach (InterfaceGenerator interfaceGen in interfaceList)
     {
         foreach (MethodSignature signature in interfaceGen.MethodSignatures)
         {
             Type.Primitives returnType = signature.ReturnType;
             if (returnType != Type.Primitives.OBJECT)
             {
                 returnTypeSet.Add(returnType);
             }
         }
         methodSignatures.AddRange(interfaceGen.MethodSignatures);
     }
 }
Example #7
0
        private static Field getField(List <Field> fields, Type.Primitives primitive, bool isStatic)
        {
            List <Field> typedFieldList = new List <Field>();

            foreach (Field @var in fields)
            {
                if (@var.Type.Type == primitive && @var.Static == isStatic)
                {
                    typedFieldList.Add(@var);
                }
            }

            if (typedFieldList.Count == 0)
            {
                return(null);
            }

            int index = (new Random()).Next(typedFieldList.Count);

            return(typedFieldList[index]);
        }
Example #8
0
        public static HashSet <Type.Primitives> getPrimitivesOfVariables(Method method)
        {
            HashSet <Type.Primitives> primitiveSet  = new HashSet <Type.Primitives>();
            List <Variable>           parameterList = method.ParameterList;

            foreach (Variable @var in parameterList)
            {
                //ignore the recursionCounter
                if (@var.Name.Equals("recursionCounter"))
                {
                    continue;
                }

                Type.Primitives primitive = @var.Type.getType();
                //we don't want expressions based on Object type
                if (!(primitive == Type.Primitives.OBJECT))
                {
                    primitiveSet.Add(primitive);
                }
            }
            return(primitiveSet);
        }
Example #9
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);
        }