Example #1
0
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, String newValueName)
        {
            // TODO assert that arguments.Count is correct.
            StringBuilder b       = new StringBuilder();
            string        retType =
                SourceCodePrinting.ToCodeString(fconstructor.DeclaringType);

            b.Append(retType + " " + newValueName + " = ");
            b.Append(" new " + SourceCodePrinting.ToCodeString(fconstructor.DeclaringType));
            b.Append("(");
            for (int i = 0; i < arguments.Count; i++)
            {
                if (i > 0)
                {
                    b.Append(" , ");
                }

                // Cast.
                b.Append("(");
                b.Append(SourceCodePrinting.ToCodeString(ParameterTypes[i]));
                b.Append(")");
                b.Append(arguments[i]);
            }
            b.Append(");");
            return(b.ToString());
        }
 public override string ToCSharpCode(ReadOnlyCollection <string> arguments, String newValueName)
 {
     return
         (arguments[0]
          + "."
          + ffield.Name
          + " = "
          + "("
          + SourceCodePrinting.ToCodeString(ffield.FieldType)
          + ")"
          + arguments[1]
          + ";");
 }
Example #3
0
 public override string ToCSharpCode(ReadOnlyCollection <string> arguments, string newValueName, bool useRandoopContracts, ContractState canGenerateContractAssertions)
 {
     return
         (arguments[0]
          + "."
          + ffield.Name
          + " = "
          + "("
          + SourceCodePrinting.ToCodeString(ffield.FieldType)
          + ")"
          + arguments[1]
          + ";");
 }
Example #4
0
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, string newValueName, bool useRandoopContracts, ContractState canGenerateContractAssertions)
        {
            StringBuilder b = new StringBuilder();

            b.Append(
                SourceCodePrinting.ToCodeString(this.ftype)
                + " "
                + newValueName
                + " = "
                + this.ToString()
                + ";");
            return(b.ToString());
        }
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, String newValueName)
        {
            StringBuilder b = new StringBuilder();

            b.Append(
                SourceCodePrinting.ToCodeString(this.ftype)
                + " "
                + newValueName
                + " = "
                + this.ToString()
                + ";");
            return(b.ToString());
        }
Example #6
0
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, string newValueName, bool useRandoopContracts, ContractState contractStates
                                            )
        {
            // TODO assert that arguments.Count is correct.
            StringBuilder code    = new StringBuilder();
            string        retType =
                SourceCodePrinting.ToCodeString(constructor.DeclaringType);

            code.Append(retType + " " + newValueName + " = ");
            code.Append("new " + SourceCodePrinting.ToCodeString(constructor.DeclaringType));
            code.Append("(");
            for (int i = 0; i < arguments.Count; i++)
            {
                if (i > 0)
                {
                    code.Append(" , ");
                }

                // Cast.
                code.Append("(");
                code.Append(SourceCodePrinting.ToCodeString(ParameterTypes[i]));
                code.Append(")");
                code.Append(arguments[i]);
            }
            code.Append(");");

            var assertion = string.Empty;

            if (useRandoopContracts)
            {
                assertion = new RandoopContractAssertionGenerator().Compute(constructor, newValueName, contractStates);
            }

            code.Append(assertion);

            return(code.ToString());
        }
        /// <summary>
        /// Method returns "string_rep_not_available" when no string representation is available
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (fvalue == null)
            {
                return("null");
            }

            if (fvalue.GetType().Equals(typeof(string)))
            {
                return(SourceCodePrinting.toSourceCodeString((string)fvalue));
            }

            string fvalueToString = null;
            Type   fvalueType     = null;

            try
            {
                fvalueToString = fvalue.ToString();
                fvalueType     = fvalue.GetType();
            }
            catch (Exception)
            {
                return("not_printable_ToString_Crashed");
            }

            if (fvalueType.IsPrimitive)
            {
                if (fvalueType.Equals(typeof(bool)))
                {
                    return("(" + ftype.ToString() + ")" + (fvalueToString.ToLower()));
                }

                if (fvalueType.Equals(typeof(byte)))
                {
                    return("(" + ftype.ToString() + ")" + SourceCodePrinting.ToCodeString2((byte)fvalue));
                }

                if (fvalueType.Equals(typeof(short)))
                {
                    return("(" + ftype.ToString() + ")" + SourceCodePrinting.ToCodeString2((short)fvalue));
                }

                if (fvalueType.Equals(typeof(int)))
                {
                    return("(" + ftype.ToString() + ")" + SourceCodePrinting.ToCodeString2((int)fvalue));
                }

                if (fvalueType.Equals(typeof(long)))
                {
                    return("(" + ftype.ToString() + ")" + SourceCodePrinting.ToCodeString2((long)fvalue));
                }

                if (fvalueType.Equals(typeof(float)))
                {
                    return("(" + ftype.ToString() + ")" + SourceCodePrinting.ToCodeString2((float)fvalue));
                }

                if (fvalueType.Equals(typeof(double)))
                {
                    return("(" + ftype.ToString() + ")" + SourceCodePrinting.ToCodeString2((double)fvalue));
                }

                if (fvalueType.Equals(typeof(char)))
                {
                    return("\'" + fvalueToString + "\'");
                }

                else
                {
                    return("(" + ftype.ToString() + ") (" + fvalueToString + ")");
                }
            }

            if (ftype.BaseType.Equals(systemEnum))
            {
                return(SourceCodePrinting.ToCodeString(ftype) + "." + fvalueToString);
            }

            if (fvalueType.IsValueType)
            {
                //check to see if this is the default value

                bool vEqArray = false;
                try
                {
                    ValueType v = fvalue as ValueType;
                    vEqArray = v.Equals(Array.CreateInstance(ftype, 1).GetValue(0));
                }
                catch (Exception)
                {
                    // Let vEqArray stay false.
                }

                if (vEqArray)
                {
                    string tStr = "typeof(" + SourceCodePrinting.ToCodeString(ftype) + ")";
                    string s    = "(" + SourceCodePrinting.ToCodeString(ftype) + ")(System.Array.CreateInstance(" + tStr + ", 1)).GetValue(0)";
                    return(s);
                }
                else
                {
                    try
                    {
                        //just perform a typecast to be sure (e.g. 1.1 is not recognized as a float unless cast
                        return("(" + SourceCodePrinting.ToCodeString(ftype) + ")(" + fvalueToString + ")");
                    }
                    catch
                    {
                        //This operation raises an exception sometime
                        //Util.Warning("The default value of ValueType<" + ftype.Name + "> is not" + fvalue.ToString());

                        return("\"__randoop_string_rep_not_available_forType<" + ftype.Name + ">\"");
                    }
                }
            }

            //just plain unlucky in this case
            return("\"__randoop_string_rep_not_available_forType<" + ftype.Name + ">\"");
        }
Example #8
0
        // arguments[0] is the receiver. if static method, arguments[0] ignored.
        //
        // TODO: This method can be largely improved. For one, it should
        // be broken up into smaller methods depending on the nature of
        // the method (regular method, operator, property, etc.).
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, string newValueName, bool useRandoopContracts, ContractState contractStates)
        {
            StringBuilder code = new StringBuilder();

            //return value
            if (MethodIsVoid() == false)
            {
                string retType = SourceCodePrinting.ToCodeString(method.ReturnType);
                code.Append(retType + " " + newValueName + " = ");
            }

            //for overloaded operators
            bool   isOverloadedOp = false;
            string overloadOp     = "";

            bool   isCastOp = false;
            string castOp   = "";

            if (method.IsStatic)
            {
                //dont append the type name in case of operator overloading
                //operator overloading?

                if (ReflectionUtils.IsOverloadedOperator(method, out overloadOp))
                {
                    isOverloadedOp = true;
                }
                else if (ReflectionUtils.IsCastOperator(method, out castOp))
                {
                    isCastOp = true;
                }
                else if (method.IsSpecialName && method.Name.StartsWith("op_"))
                {
                    castOp = "(NOT HANDLED: " + method.ToString() + ")"; // TODO improve this message.
                }
                else
                {
                    isOverloadedOp = false;
                    isCastOp       = false;
                    code.Append(SourceCodePrinting.ToCodeString(method.DeclaringType));
                    code.Append(".");
                }
            }
            else
            {
                Type methodDeclaringType = ParameterTypes[0];

                code.Append("((");
                code.Append(SourceCodePrinting.ToCodeString(methodDeclaringType));
                code.Append(")");
                code.Append(arguments[0]);
                code.Append(")");
                code.Append(".");
            }

            //check if its a property
            //TODO: setter should not have a return value
            bool isSetItem = false;
            bool isItemOf  = false;
            bool isGetItem = false;
            bool isChars   = false;

            if (method.IsSpecialName)
            {
                HandleSpecialName(code, isOverloadedOp, isCastOp, ref isSetItem, ref isItemOf, ref isGetItem);
            }
            else
            {
                code.Append(method.Name);
                code.Append("(");
            }

            //arguments tuples

            for (int i = 1; i < arguments.Count; i++)
            {
                //we also need to remove the typename that comes with the static method
                if (isOverloadedOp && i == 2)
                {
                    code.Append(overloadOp);
                }
                else if (isCastOp && i == 1)
                {
                    code.Append(castOp);
                }
                else if (isSetItem && i == arguments.Count - 1)
                {
                    code.Append("] = ");
                }
                else
                {
                    if (i > 1)
                    {
                        code.Append(", ");
                    }
                }


                // Cast.
                code.Append("(");
                code.Append(SourceCodePrinting.ToCodeString(ParameterTypes[i]));
                code.Append(")");
                code.Append(arguments[i]);
            }

            if (!method.IsSpecialName)
            {
                code.Append(")");
            }
            else if (isGetItem || isChars || isItemOf)
            {
                code.Append("]");
            }
            code.Append(";");

            #region assertion
            ////[email protected] adds for capture return value for regression assertion -- start////
            //CASE1 -- output-plan-only stage: the execution of the plan hasn't happen yet
            //CASE2 -- execution has happened: return value is "not null & primitive+string"
            //CASE3 -- execution has happened: return value is "not null & not primitive+string"
            //CASE4 -- execution has happened: return value is "null"
            //CASE5 -- execution has happened: execution fails, return value is "null"("RANDOOPFAIL")

            //CASE1
            //if (timesReturnValRetrived >= ReturnValue.Count) //timesExecuted == ReturnValue.Count
            if (timesReturnValRetrieved >= timesExecuted)
            {
                Logger.Debug(method.Name + "[" + timesReturnValRetrieved.ToString()
                             + "+] didn't execute yet. Output-plan-only stage.");

                return(code.ToString()); //skip adding regression assertion
            }

            Logger.Debug(method.Name + "[" + timesReturnValRetrieved.ToString()
                         + "] get-return-value stage.");

            object tempval = ReturnValue[timesReturnValRetrieved];  // timesReturnValRetrived == ReturnValue.Count - 1

            //b.Append("\t//" + retType + "?=" + tempval.GetType().ToString() + ";\n"); //for debug

            var assertion = string.Empty;
            if (useRandoopContracts)
            {
                assertion = new RandoopContractAssertionGenerator().Compute(method, newValueName, arguments[0], contractStates);
            }

            if (assertion.Contains("Assert.IsTrue") == false)
            {
                assertion = new RegressionAssertionGenerator().GenerateRegressionAssertion(tempval, newValueName, timesReturnValRetrieved);
            }

            code.Append(assertion);

            timesReturnValRetrieved++;

            ////[email protected] adds for capture return value for regression assertion -- end////
            #endregion assertion

            return(code.ToString());
        }
Example #9
0
        // arguments[0] is the receiver. if static method, arguments[0] ignored.
        //
        // TODO: This method can be largely improved. For one, it should
        // be broken up into smaller methods depending on the nature of
        // the method (regular method, operator, property, etc.).
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, String newValueName)
        {
            StringBuilder b = new StringBuilder();
            //return value
            string retType = method.ReturnType.ToString();

            if (!method.ReturnType.Equals(typeof(void)))
            {
                retType = SourceCodePrinting.ToCodeString(method.ReturnType);
                b.Append(retType + " " + newValueName + " = ");
            }



            //for overloaded operators
            bool   isOverloadedOp = false;
            string overloadOp     = "";

            bool   isCastOp = false;
            string castOp   = "";

            if (method.IsStatic)
            {
                //dont append the type name in case of operator overloading
                //operator overloading?

                if (ReflectionUtils.IsOverloadedOperator(method, out overloadOp))
                {
                    isOverloadedOp = true;
                }
                else if (ReflectionUtils.IsCastOperator(method, out castOp))
                {
                    isCastOp = true;
                }
                else if (method.IsSpecialName && method.Name.StartsWith("op_"))
                {
                    castOp = "(NOT HANDLED: " + method.ToString() + ")"; // TODO improve this message.
                }
                else
                {
                    isOverloadedOp = false;
                    isCastOp       = false;
                    b.Append(SourceCodePrinting.ToCodeString(method.DeclaringType));
                    b.Append(".");
                }
            }
            else
            {
                Type methodDeclaringType = ParameterTypes[0];

                b.Append("((");
                b.Append(SourceCodePrinting.ToCodeString(methodDeclaringType));
                b.Append(")");
                b.Append(arguments[0]);
                b.Append(")");
                b.Append(".");
            }

            //check if its a property
            //TODO: setter should not have a return value
            bool isSetItem = false;
            bool isItemOf  = false;
            bool isGetItem = false;
            bool isChars   = false;

            if (method.IsSpecialName)
            {
                string s = (method.Name);

                bool isDefaultProperty = false;
                foreach (MemberInfo mi in method.DeclaringType.GetDefaultMembers())
                {
                    if (!(mi is PropertyInfo))
                    {
                        continue;
                    }

                    PropertyInfo pi = mi as PropertyInfo;
                    if (method.Equals(pi.GetGetMethod()))
                    {
                        isDefaultProperty = true;
                        isGetItem         = true;
                        b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                        b.Append("[");
                    }
                    else if (method.Equals(pi.GetSetMethod()))
                    {
                        isDefaultProperty = true;
                        isSetItem         = true;
                        b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                        b.Append("[");
                    }
                }

                if (isDefaultProperty)
                {
                    // Already processed.
                }
                else if (s.StartsWith("get_ItemOf"))
                {
                    isItemOf = true;
                    b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                    b.Append("[");
                }
                //shuvendu: Important: replace get_Item with [] but get_ItemType with ItemType()
                // The last clause is because some classes define an "Item" property
                // that has no index.
                else if (s.Equals("get_Item") && method.GetParameters().Length > 0)
                {
                    isGetItem = true;
                    b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                    b.Append("[");
                }
                // The last clause is because some classes define an "Item" property
                // that has no index.
                else if ((s.Equals("set_Item") || s.Equals("set_ItemOf")) && method.GetParameters().Length > 1)
                {
                    isSetItem = true;
                    b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                    b.Append("[");
                }
                else if ((s.StartsWith("get_")))
                {
                    s = s.Replace("get_", "");
                    b.Append(s);
                    //                        b.Append("(");
                }
                else if (s.StartsWith("set_"))
                {
                    s = s.Replace("set_", "");
                    b.Append(s + " = ");
                }
                else if (isOverloadedOp)
                {
                    // Nothing to do here.
                }
                else if (isCastOp)
                {
                    // Nothing to do here.
                }
                else
                {
                    // Operator not handled in output generation.
                }
            }
            else
            {
                b.Append(method.Name);
                b.Append("(");
            }

            //arguments tuples

            for (int i = 1; i < arguments.Count; i++)
            {
                //we also need to remove the typename that comes with the static method
                if (isOverloadedOp && i == 2)
                {
                    b.Append(overloadOp);
                }
                else if (isCastOp && i == 1)
                {
                    b.Append(castOp);
                }
                else if (isSetItem && i == arguments.Count - 1)
                {
                    b.Append("] = ");
                }
                else
                {
                    if (i > 1)
                    {
                        b.Append(", ");
                    }
                }


                // Cast.
                b.Append("(");
                b.Append(SourceCodePrinting.ToCodeString(ParameterTypes[i]));
                b.Append(")");
                b.Append(arguments[i]);
            }

            if (!method.IsSpecialName)
            {
                b.Append(")");
            }
            else if (isGetItem || isChars || isItemOf)
            {
                b.Append("]");
            }
            b.Append(" ;");
            return(b.ToString());
        }
Example #10
0
        // arguments[0] is the receiver. if static method, arguments[0] ignored.
        //
        // TODO: This method can be largely improved. For one, it should
        // be broken up into smaller methods depending on the nature of
        // the method (regular method, operator, property, etc.).
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, String newValueName)
        {
            StringBuilder b = new StringBuilder();
            //return value
            string retType = method.ReturnType.ToString();

            if (!method.ReturnType.Equals(typeof(void))) //if return value is null, no newValueName was given as input
            {
                retType = SourceCodePrinting.ToCodeString(method.ReturnType);
                b.Append(retType + " " + newValueName + " = ");
            }

            //for overloaded operators
            bool   isOverloadedOp = false;
            string overloadOp     = "";

            bool   isCastOp = false;
            string castOp   = "";

            if (method.IsStatic)
            {
                //dont append the type name in case of operator overloading
                //operator overloading?

                if (ReflectionUtils.IsOverloadedOperator(method, out overloadOp))
                {
                    isOverloadedOp = true;
                }
                else if (ReflectionUtils.IsCastOperator(method, out castOp))
                {
                    isCastOp = true;
                }
                else if (method.IsSpecialName && method.Name.StartsWith("op_"))
                {
                    castOp = "(NOT HANDLED: " + method.ToString() + ")"; // TODO improve this message.
                }
                else
                {
                    isOverloadedOp = false;
                    isCastOp       = false;
                    b.Append(SourceCodePrinting.ToCodeString(method.DeclaringType));
                    b.Append(".");
                }
            }
            else
            {
                Type methodDeclaringType = ParameterTypes[0];

                b.Append("((");
                b.Append(SourceCodePrinting.ToCodeString(methodDeclaringType));
                b.Append(")");
                b.Append(arguments[0]);
                b.Append(")");
                b.Append(".");
            }

            //check if its a property
            //TODO: setter should not have a return value
            bool isSetItem = false;
            bool isItemOf  = false;
            bool isGetItem = false;
            bool isChars   = false;

            if (method.IsSpecialName)
            {
                string s = (method.Name);

                bool isDefaultProperty = false;
                foreach (MemberInfo mi in method.DeclaringType.GetDefaultMembers())
                {
                    if (!(mi is PropertyInfo))
                    {
                        continue;
                    }

                    PropertyInfo pi = mi as PropertyInfo;
                    if (method.Equals(pi.GetGetMethod()))
                    {
                        isDefaultProperty = true;
                        isGetItem         = true;
                        b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                        b.Append("[");
                    }
                    else if (method.Equals(pi.GetSetMethod()))
                    {
                        isDefaultProperty = true;
                        isSetItem         = true;
                        b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                        b.Append("[");
                    }
                }

                if (isDefaultProperty)
                {
                    // Already processed.
                }
                else if (s.StartsWith("get_ItemOf"))
                {
                    isItemOf = true;
                    b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                    b.Append("[");
                }
                //shuvendu: Important: replace get_Item with [] but get_ItemType with ItemType()
                // The last clause is because some classes define an "Item" property
                // that has no index.
                else if (s.Equals("get_Item") && method.GetParameters().Length > 0)
                {
                    isGetItem = true;
                    b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                    b.Append("[");
                }
                // The last clause is because some classes define an "Item" property
                // that has no index.
                else if ((s.Equals("set_Item") || s.Equals("set_ItemOf")) && method.GetParameters().Length > 1)
                {
                    isSetItem = true;
                    b.Remove(b.Length - 1, 1); // Remove the "." that was inserted above.
                    b.Append("[");
                }
                else if ((s.StartsWith("get_")))
                {
                    s = s.Replace("get_", "");
                    b.Append(s);
                    //                        b.Append("(");
                }
                else if (s.StartsWith("set_"))
                {
                    s = s.Replace("set_", "");
                    b.Append(s + " = ");
                }
                else if (isOverloadedOp)
                {
                    // Nothing to do here.
                }
                else if (isCastOp)
                {
                    // Nothing to do here.
                }
                else
                {
                    // Operator not handled in output generation.
                }
            }
            else
            {
                b.Append(method.Name);
                b.Append("(");
            }

            //arguments tuples

            for (int i = 1; i < arguments.Count; i++)
            {
                //we also need to remove the typename that comes with the static method
                if (isOverloadedOp && i == 2)
                {
                    b.Append(overloadOp);
                }
                else if (isCastOp && i == 1)
                {
                    b.Append(castOp);
                }
                else if (isSetItem && i == arguments.Count - 1)
                {
                    b.Append("] = ");
                }
                else
                {
                    if (i > 1)
                    {
                        b.Append(", ");
                    }
                }


                // Cast.
                b.Append("(");
                b.Append(SourceCodePrinting.ToCodeString(ParameterTypes[i]));
                b.Append(")");
                b.Append(arguments[i]);
            }

            if (!method.IsSpecialName)
            {
                b.Append(")");
            }
            else if (isGetItem || isChars || isItemOf)
            {
                b.Append("]");
            }
            b.Append(" ;");

            #region regression assertion
            ////[email protected] adds for capture return value for regression assertion -- start////
            //CASE1 -- output-plan-only stage: the execution of the plan hasn't happen yet
            //CASE2 -- execution has happened: return value is "not null & primitive+string"
            //CASE3 -- execution has happened: return value is "not null & not primitive+string"
            //CASE4 -- execution has happened: return value is "null"
            //CASE5 -- execution has happened: execution fails, return value is "null"("RANDOOPFAIL")

            //CASE1
            //if (this.timesReturnValRetrived >= this.ReturnValue.Count) //timesExecuted == ReturnValue.Count
            if (this.timesReturnValRetrived >= this.timesExecuted)
            {
                Console.WriteLine(this.method.Name + "[" + this.timesReturnValRetrived.ToString()
                                  + "+] didn't execute yet. Output-plan-only stage.");

                return(b.ToString()); //skip adding regression assertion
            }

            Console.WriteLine(this.method.Name + "[" + this.timesReturnValRetrived.ToString()
                              + "] get-return-value stage.");

            object tempval = this.ReturnValue[this.timesReturnValRetrived];  // timesReturnValRetrived == ReturnValue.Count - 1

            //b.Append("\t//" + retType + "?=" + tempval.GetType().ToString() + ";\n"); //for debug

            string regressionAssert = GenRegressionAssertion(tempval, newValueName);

            b.Append(regressionAssert);

            this.timesReturnValRetrived++;

            ////[email protected] adds for capture return value for regression assertion -- end////
            #endregion regression assertion

            return(b.ToString());
        }