public VariableBinOpVariable(Method method, Primitives primitive) { Random rand = new Random(); int nested = rand.Next(100); if (nested < method.AssociatedClass.Percent) { leftOprnd = new VariableBinOpVariable(method, primitive); binOp = new BinaryOperator(primitive); rightOprnd = new VariableBinOpVariable(method, primitive); } else { leftOprnd = VariableGenerator.getRandomizedVariable(method, primitive); binOp = new BinaryOperator(primitive); rightOprnd = VariableGenerator.getRandomizedVariable(method, primitive); } // removing expressions of the form: (i5-i5) to avoid {i19%(i5-i5)}expr. if (leftOprnd.ToString().Equals(rightOprnd.ToString())) { //rightOprnd = new Literal(primitive); //String test = rightOprnd.ToString().SubstringSpecial(rightOprnd.ToString().Length-1, rightOprnd.ToString().Length); //test = test; //Primitives t = Primitives.OTHER; //String testValue = t.ToString(); string pattern = "[^0-9]+"; string replacement = ""; Regex rgx = new Regex(pattern); string test = rgx.Replace(rightOprnd.ToString(), replacement); Primitives t = Primitives.OTHER; rightOprnd = new Literal(t, Convert.ToInt32(test)); } else { bool testfalse = false; } // for division and modulo operations, keeping only literals in the right expr. // i5%i3 => i5%constantValue OR f2/f4 => f2/constantValue bool test1 = binOp.ToString().Equals("/") || binOp.ToString().Equals("%"); //if (binOp.ToString().Equals("/") || binOp.ToString().Equals("%")) //{ // do // { //FIXME: only handles int for now. // rightOprnd = new Literal(primitive,Int32.MaxValue); // rightOprnd.ToString().EndsWith("0"); // }while ( rightOprnd.ToString().EndsWith("0")); //avoiding divide by (0) //} }
public VariableBinOpField(Method method, Primitives primitive) { Random rand = new Random(); int option = rand.Next(100) % 2; int nested = 0; switch (option) { case 0: //var binOp field nested = rand.Next(100); if (nested < method.AssociatedClass.Percent) { leftOprnd = new VariableBinOpField(method, primitive); binOp = new BinaryOperator(primitive); rightOprnd = new VariableBinOpField(method, primitive); } else { leftOprnd = VariableGenerator.getRandomizedVariable(method, primitive); binOp = new BinaryOperator(primitive); rightOprnd = FieldGenerator.getRandomField(method.AssociatedClass, primitive, method.Static); } break; case 1: // field binOp var nested = rand.Next(100); if (nested < method.AssociatedClass.Percent) { leftOprnd = new VariableBinOpField(method, primitive); binOp = new BinaryOperator(primitive); rightOprnd = new VariableBinOpField(method, primitive); } else { leftOprnd = VariableGenerator.getRandomizedVariable(method, primitive); binOp = new BinaryOperator(primitive); rightOprnd = FieldGenerator.getRandomField(method.AssociatedClass, primitive, method.Static); } break; } // for division and modulo operations, keeping only literals in the right expr. // i5%i3 => i5%constantValue OR f2/f4 => f2/constantValue if (binOp.ToString().Equals("/") || binOp.ToString().Equals("%")) { do { //FIXME: only handles int for now. rightOprnd = new Literal(primitive); }while (rightOprnd.ToString().Contains("(0)")); //avoiding divide by (0) } }
public VariableBinOpVariable(Method method, Primitives primitive) { Random rand = new Random(); int nested = rand.Next(10); //if (nested < method.AssociatedClass.Percent) //{ // leftOprnd = new VariableBinOpVariable(method, primitive); // binOp = new BinaryOperator(primitive); // rightOprnd = new VariableBinOpVariable(method, primitive); //} //else { leftOprnd = VariableGenerator.getRandomizedVariable(method, primitive); binOp = new BinaryOperator(primitive); rightOprnd = VariableGenerator.getRandomizedVariable(method, primitive); } // removing expressions of the form: (i5-i5) to avoid {i19%(i5-i5)}expr. if (leftOprnd.ToString().Equals(rightOprnd.ToString())) { rightOprnd = new Literal(primitive); } // for division and modulo operations, keeping only literals in the right expr. // i5%i3 => i5%constantValue OR f2/f4 => f2/constantValue //if (binOp.ToString().Equals("/") || binOp.ToString().Equals("%")) //{ // do // { //FIXME: only handles int for now. // //rightOprnd = new Literal(primitive); // rightOprnd.ToString().Replace("(0)", "(1)"); // }while (rightOprnd.ToString().Contains("(0)")); //avoiding divide by (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"; } } }